Update
This commit is contained in:
parent
5b5c3b4cc3
commit
81013fa710
@ -139,76 +139,20 @@ namespace PSTW_CentralSystem.Controllers.API
|
|||||||
{
|
{
|
||||||
string FormDate = reportQuery.FormDate!;
|
string FormDate = reportQuery.FormDate!;
|
||||||
string DeptId = reportQuery.DeptId!;
|
string DeptId = reportQuery.DeptId!;
|
||||||
var currentProductBalance = new List<ProductReport>();
|
|
||||||
var result = await _centralDbContext.Products
|
|
||||||
.Include(p => p.Items)
|
|
||||||
.Select(p => new { p.ProductName, p.QuantityJSON, p.Items})
|
|
||||||
.Where(p => p.QuantityJSON != null)
|
|
||||||
.ToListAsync();
|
|
||||||
var quantityJsonDict = new Dictionary<string, Dictionary<string,int>>();
|
|
||||||
foreach (var item in result)
|
|
||||||
{
|
|
||||||
quantityJsonDict[item.ProductName] = JsonConvert.DeserializeObject<Dictionary<string, int>>(item.QuantityJSON!)!;
|
|
||||||
if (!quantityJsonDict.TryGetValue(item.ProductName, out var deptDict))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (deptDict.TryGetValue(DeptId, out var quantity))
|
|
||||||
{
|
|
||||||
currentProductBalance.Add(new ProductReport
|
|
||||||
{
|
|
||||||
ProductName = item.ProductName,
|
|
||||||
Quantity = quantity,
|
|
||||||
TotalPrice = item.Items!.Sum(i => i.ConvertPrice * i.Quantity)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
DateTime parsedDate = DateTime.Parse(FormDate);
|
DateTime parsedDate = DateTime.Parse(FormDate);
|
||||||
|
var currentProductBalance = new List<ProductReport>();
|
||||||
var thisMonthMovementIn = await _centralDbContext.ItemMovements
|
var productData = await _centralDbContext.Products
|
||||||
.Include(m => m.Item)
|
.Where(p => p.QuantityJSON != null)
|
||||||
.Include(m => m.Item!.Product)
|
.Select(p => new
|
||||||
.Include(m => m.Item!.Department)
|
|
||||||
.Where(m => m.Date.Month == parsedDate.Month && m.Date.Year == parsedDate.Year && m.Item!.Department!.DepartmentCode == DeptId && (m.Action == "Stock In" || m.Action == "Register"))
|
|
||||||
.Select(m => new
|
|
||||||
{
|
{
|
||||||
ProductName = m.Item!.Product!.ProductName,
|
p.ProductName,
|
||||||
QuantityIn = m.Quantity??0,
|
p.QuantityJSON,
|
||||||
PriceIn = m.Quantity * m.Item!.ConvertPrice
|
|
||||||
})
|
})
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
foreach (var p in productData)
|
||||||
var thisMonthMovementOut = await _centralDbContext.ItemMovements
|
|
||||||
.Include(m => m.Item)
|
|
||||||
.Include(m => m.Item!.Product)
|
|
||||||
.Include(m => m.Item!.Department)
|
|
||||||
.Where(m => m.Date.Month == parsedDate.Month && m.Date.Year == parsedDate.Year && m.Item!.Department!.DepartmentCode == DeptId && (m.Action == "Stock Out" || m.Action == "Assign"))
|
|
||||||
.Select(m => new
|
|
||||||
{
|
|
||||||
ProductName = m.Item!.Product!.ProductName,
|
|
||||||
QuantityOut = m.Quantity??0,
|
|
||||||
PriceOut = m.Quantity * m.Item!.ConvertPrice
|
|
||||||
})
|
|
||||||
.ToListAsync();
|
|
||||||
|
|
||||||
//value from currentProductBalance - value from thisMonthMovementOut + value from thisMonthMovementIn
|
|
||||||
foreach (var item in currentProductBalance)
|
|
||||||
{
|
{
|
||||||
var movementIn = thisMonthMovementIn.FirstOrDefault(m => m.ProductName == item.ProductName);
|
var deptDict = JsonConvert.DeserializeObject<Dictionary<string, int>>(p.QuantityJSON!);
|
||||||
if (movementIn != null && item.Quantity > 0)
|
|
||||||
{
|
|
||||||
item.Quantity -= movementIn.QuantityIn;
|
|
||||||
item.TotalPrice -= (float)movementIn.PriceIn!;
|
|
||||||
}
|
|
||||||
var movementOut = thisMonthMovementOut.FirstOrDefault(m => m.ProductName == item.ProductName);
|
|
||||||
if (movementOut != null && item.Quantity > 0)
|
|
||||||
{
|
|
||||||
item.Quantity += movementOut.QuantityOut;
|
|
||||||
item.TotalPrice += (float)movementOut.PriceOut!;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var departmentUsers = await _centralDbContext.Users
|
var departmentUsers = await _centralDbContext.Users
|
||||||
.Include(u => u.Department)
|
.Include(u => u.Department)
|
||||||
.Where(u => u.Department!.DepartmentCode == DeptId)
|
.Where(u => u.Department!.DepartmentCode == DeptId)
|
||||||
@ -295,7 +239,7 @@ namespace PSTW_CentralSystem.Controllers.API
|
|||||||
.Select(u => new
|
.Select(u => new
|
||||||
{
|
{
|
||||||
Department = u.DepartmentName,
|
Department = u.DepartmentName,
|
||||||
Items = latestStationItemMovements
|
Items = latestStoreItemMovements
|
||||||
.Where(m => m != null && m.ToStore > 0 && m.NextStore!.StoreName.Contains(u.DepartmentName))
|
.Where(m => m != null && m.ToStore > 0 && m.NextStore!.StoreName.Contains(u.DepartmentName))
|
||||||
.Select(m => new
|
.Select(m => new
|
||||||
{
|
{
|
||||||
@ -309,7 +253,7 @@ namespace PSTW_CentralSystem.Controllers.API
|
|||||||
UniqueID = m.Item.UniqueID,
|
UniqueID = m.Item.UniqueID,
|
||||||
})
|
})
|
||||||
.ToList(),
|
.ToList(),
|
||||||
TotalItemPrice = latestStationItemMovements.Where(m => m != null && m.ToStore > 0 && m.NextStore!.StoreName.Contains(u.DepartmentName)).Select(m => m!.Item!.ConvertPrice).Sum(),
|
TotalItemPrice = latestStoreItemMovements.Where(m => m != null && m.ToStore > 0 && m.NextStore!.StoreName.Contains(u.DepartmentName)).Select(m => m!.Item!.ConvertPrice).Sum(),
|
||||||
|
|
||||||
})
|
})
|
||||||
.Where(u => u.Items.Count > 0).FirstOrDefault();
|
.Where(u => u.Items.Count > 0).FirstOrDefault();
|
||||||
@ -386,7 +330,7 @@ namespace PSTW_CentralSystem.Controllers.API
|
|||||||
|
|
||||||
// 2. Fetch Movements DURING the range (Required for Stock Issue logic)
|
// 2. Fetch Movements DURING the range (Required for Stock Issue logic)
|
||||||
var movementsInRange = await _centralDbContext.ItemMovements
|
var movementsInRange = await _centralDbContext.ItemMovements
|
||||||
.Include(m => m.Item).ThenInclude(i => i.Product)
|
.Include(m => m.Item).ThenInclude(i => i!.Product)
|
||||||
.Include(m => m.FromStation).Include(m => m.FromUser)
|
.Include(m => m.FromStation).Include(m => m.FromUser)
|
||||||
.Include(m => m.NextStation).Include(m => m.NextUser)
|
.Include(m => m.NextStation).Include(m => m.NextUser)
|
||||||
.Include(m => m.NextStore)
|
.Include(m => m.NextStore)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user