MonthlyInventory report
This commit is contained in:
@@ -7,6 +7,7 @@ using AutoMapper.QueryableExtensions;
|
||||
using InventoryTraker.Web.Core;
|
||||
using InventoryTraker.Web.Data;
|
||||
using InventoryTraker.Web.Models;
|
||||
using InventoryTraker.Web.Utilities;
|
||||
|
||||
namespace InventoryTraker.Web.Controllers
|
||||
{
|
||||
@@ -40,8 +41,6 @@ namespace InventoryTraker.Web.Controllers
|
||||
Date = g.Key.TransactionDate,
|
||||
Destination = g.Key.Destination,
|
||||
Transactions = g.ToList()
|
||||
//(from transaction in g
|
||||
//select Mapper.Map<TransactionViewModel>(transaction)).ToArray()
|
||||
};
|
||||
|
||||
var report =
|
||||
@@ -66,7 +65,143 @@ namespace InventoryTraker.Web.Controllers
|
||||
|
||||
public ActionResult MonthlyInventory(DateTime month)
|
||||
{
|
||||
return BetterJson(true);
|
||||
var startDate = month;
|
||||
var endDate = startDate.AddMonths(1);
|
||||
|
||||
var inventoryTypeReport
|
||||
= new InventoryTypeReport
|
||||
{
|
||||
Items = GetInventoryTypeReportItems(startDate, endDate),
|
||||
Month = month
|
||||
};
|
||||
|
||||
return BetterJson(inventoryTypeReport);
|
||||
}
|
||||
|
||||
private IEnumerable<InventoryTypeReportItem> GetInventoryTypeReportItems(DateTime startDate, DateTime endDate)
|
||||
{
|
||||
var transactionsMostRecentBefore =
|
||||
(from transaction in _context.Transactions
|
||||
where
|
||||
transaction.TransactionDate < startDate
|
||||
group transaction by transaction.Inventory
|
||||
into g
|
||||
let mostRecent =
|
||||
g.OrderByDescending(t => t.TransactionDate)
|
||||
.ThenBy(t => t.CurrentQuantity) // for days with multiple, assume it's the smallest
|
||||
.FirstOrDefault()
|
||||
where mostRecent.CurrentQuantity > 0
|
||||
select mostRecent).ToList();
|
||||
|
||||
var transactionSums =
|
||||
(from transaction in _context.Transactions
|
||||
where
|
||||
transaction.TransactionDate >= startDate
|
||||
&& transaction.TransactionDate < endDate
|
||||
group transaction by transaction.Inventory
|
||||
into g
|
||||
let addedQty = g.Sum(t => t.AddedQuantity)
|
||||
let distributed = g.Where(t => t.TransactionType == TransactionType.Distributed)
|
||||
let distributedQty = distributed.Any() ? distributed.Sum(t => t.RemovedQuantity) : 0
|
||||
let adjustment = g.Where(t =>
|
||||
t.TransactionType == TransactionType.Expired
|
||||
|| t.TransactionType == TransactionType.Loss)
|
||||
let adjustmentQty = adjustment.Any() ? adjustment.Sum(t => t.RemovedQuantity) : 0
|
||||
let endingQty =
|
||||
g
|
||||
.OrderByDescending(t => t.TransactionDate)
|
||||
.ThenBy(t => t.CurrentQuantity)
|
||||
.FirstOrDefault().CurrentQuantity
|
||||
select new
|
||||
{
|
||||
Inventory = g.Key,
|
||||
addedQty,
|
||||
adjustmentQty,
|
||||
distributedQty,
|
||||
endingQty
|
||||
}).ToList();
|
||||
|
||||
var inventoryReportItems =
|
||||
transactionsMostRecentBefore.FullOuterJoin( // source
|
||||
transactionSums, // inner
|
||||
before => before.Inventory.Id, // fk
|
||||
sums => sums.Inventory.Id, // pk
|
||||
(before, sums, r) =>
|
||||
{
|
||||
var item = new InventoryReportItem();
|
||||
|
||||
if (before != null)
|
||||
{
|
||||
item.Inventory = before.Inventory;
|
||||
item.BeginningQuantity = before.CurrentQuantity;
|
||||
if (sums != null)
|
||||
{
|
||||
item.AddedQuantity = sums.addedQty;
|
||||
item.DistributedQuantity = sums.distributedQty;
|
||||
item.AdjustmentQuantity = sums.adjustmentQty;
|
||||
item.EndingQuantity = sums.endingQty;
|
||||
}
|
||||
else // no change
|
||||
{
|
||||
item.EndingQuantity = item.BeginningQuantity;
|
||||
}
|
||||
}
|
||||
else if (sums != null) // item was added in this time period
|
||||
{
|
||||
item.Inventory = sums.Inventory;
|
||||
item.AddedQuantity = sums.addedQty;
|
||||
item.DistributedQuantity = sums.distributedQty;
|
||||
item.AdjustmentQuantity = sums.adjustmentQty;
|
||||
item.EndingQuantity = sums.endingQty;
|
||||
}
|
||||
item.TotalAvailableQuantity = item.BeginningQuantity + item.AddedQuantity;
|
||||
return item;
|
||||
}).ToArray();
|
||||
|
||||
// group by inventory type
|
||||
var inventoryTypeReportItems =
|
||||
from item in inventoryReportItems
|
||||
group item by item.Inventory.InventoryType
|
||||
into grp
|
||||
select new InventoryTypeReportItem
|
||||
{
|
||||
InventoryType = Mapper.Map<InventoryTypeViewModel>(grp.Key),
|
||||
BeginningQuantity = grp.Sum(g => g.BeginningQuantity),
|
||||
AddedQuantity = grp.Sum(g => g.AddedQuantity),
|
||||
TotalAvailableQuantity = grp.Sum(g => g.TotalAvailableQuantity),
|
||||
DistributedQuantity = grp.Sum(g => g.DistributedQuantity),
|
||||
AdjustmentQuantity = grp.Sum(g => g.AdjustmentQuantity),
|
||||
EndingQuantity = grp.Sum(g => g.EndingQuantity)
|
||||
};
|
||||
return inventoryTypeReportItems;
|
||||
}
|
||||
|
||||
public class InventoryReportItem
|
||||
{
|
||||
public Inventory Inventory { get; set; }
|
||||
public int BeginningQuantity { get; set; }
|
||||
public int AddedQuantity { get; set; }
|
||||
public int TotalAvailableQuantity { get; set; }
|
||||
public int DistributedQuantity { get; set; }
|
||||
public int AdjustmentQuantity { get; set; }
|
||||
public int EndingQuantity { get; set; }
|
||||
}
|
||||
|
||||
public class InventoryTypeReport
|
||||
{
|
||||
public DateTime Month { get; set; }
|
||||
public IEnumerable<InventoryTypeReportItem> Items { get; set; }
|
||||
}
|
||||
|
||||
public class InventoryTypeReportItem
|
||||
{
|
||||
public InventoryTypeViewModel InventoryType { get; set; }
|
||||
public int BeginningQuantity { get; set; }
|
||||
public int AddedQuantity { get; set; }
|
||||
public int TotalAvailableQuantity { get; set; }
|
||||
public int DistributedQuantity { get; set; }
|
||||
public int AdjustmentQuantity { get; set; }
|
||||
public int EndingQuantity { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user