InventoryType from XML

Transaction updates
This commit is contained in:
2016-08-25 13:00:09 -04:00
parent 53ed1b3af9
commit a5fcb46e04
28 changed files with 471 additions and 70 deletions
@@ -4,6 +4,7 @@ using System.Linq;
using System.Web.Mvc;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using InventoryTraker.Web.ActionResults;
using InventoryTraker.Web.Core;
using InventoryTraker.Web.Data;
using InventoryTraker.Web.Models;
@@ -26,14 +27,22 @@ namespace InventoryTraker.Web.Controllers
public JsonResult All()
{
var viewModels = _context.Inventories
.OrderBy(x => x.InventoryType.Name)
var viewModels =
AllInventory()
.ProjectTo<InventoryViewModel>()
.ToArray();
return BetterJson(viewModels);
}
private IQueryable<Inventory> AllInventory()
{
return _context
.Inventories
.Where(x => x.Quantity > 0)
.OrderBy(x => x.InventoryType.Name);
}
public JsonResult Add(InventoryAddForm form)
{
if (!ModelState.IsValid)
@@ -42,18 +51,56 @@ namespace InventoryTraker.Web.Controllers
var inventory = Mapper.Map<Inventory>(form);
inventory.InventoryType = _context.InventoryTypes.Find(form.InventoryTypeId);
_context.Inventories.Add(inventory);
inventory.Transactions = new List<Transaction>();
inventory.Transactions.Add(new Transaction
inventory.Transactions = new List<Transaction>
{
AddedQuantity = inventory.Quantity,
Memo = "Arrival",
Timestamp = DateTime.Now,
TransactionDate = inventory.AddedDate
});
new Transaction
{
AddedQuantity = inventory.Quantity,
CurrentQuantity = inventory.Quantity,
Memo = "Arrival",
Timestamp = DateTime.Now,
TransactionDate = inventory.AddedDate
}
};
_context.SaveChanges();
var model = Mapper.Map<InventoryViewModel>(inventory);
return BetterJson(model);
}
public JsonResult Distribute(InventoryDistributeForm form)
{
if (!ModelState.IsValid)
return PackageModelStateErrors();
foreach (var quantityForm in form.InventoryQuantities)
{
var inventory = _context.Inventories.Find(quantityForm.InventoryId);
// check if it's really there
if (inventory == null)
return BetterJsonResult.Error($"Inventory {quantityForm.InventoryId} not found");
if (inventory.Quantity < quantityForm.Quantity)
return BetterJsonResult.Error(
$"Inventory {inventory.InventoryType.Name} has only {inventory.Quantity}, trying to remove {quantityForm.Quantity}");
inventory.Quantity -= quantityForm.Quantity;
inventory.Transactions.Add(new Transaction
{
RemovedQuantity = quantityForm.Quantity,
CurrentQuantity = inventory.Quantity,
Memo = "Distributed to " + form.Destination,
Timestamp = DateTime.Now,
TransactionDate = form.DistributedDate
});
}
_context.SaveChanges();
return BetterJson(AllInventory()
.ProjectTo<InventoryViewModel>()
.ToArray());
}
}
}