Remove inventory, delete transactions
This commit is contained in:
@@ -4,7 +4,6 @@ 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;
|
||||
@@ -57,6 +56,10 @@ namespace InventoryTraker.Web.Controllers
|
||||
|
||||
var inventory = Mapper.Map<Inventory>(form);
|
||||
inventory.InventoryType = _context.InventoryTypes.Find(form.InventoryTypeId);
|
||||
|
||||
if (inventory.InventoryType == null)
|
||||
return GetErrorListJson("Inventory Type not found");
|
||||
|
||||
_context.Inventories.Add(inventory);
|
||||
inventory.Transactions = new List<Transaction>
|
||||
{
|
||||
@@ -81,35 +84,105 @@ namespace InventoryTraker.Web.Controllers
|
||||
if (!ModelState.IsValid)
|
||||
return GetModelStateErrorListJson();
|
||||
|
||||
var errors = new List<string>();
|
||||
|
||||
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");
|
||||
|
||||
{
|
||||
errors.Add($"'{quantityForm.InventoryId}' not found");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (inventory.Quantity < quantityForm.Quantity)
|
||||
return BetterJsonResult.Error(
|
||||
$"Inventory {inventory.InventoryType.Name} has only {inventory.Quantity}, trying to remove {quantityForm.Quantity}");
|
||||
errors.Add(
|
||||
$"'{inventory.InventoryType.Name}' has only {inventory.Quantity} qty, " +
|
||||
$"cannot remove {quantityForm.Quantity}");
|
||||
|
||||
inventory.Quantity -= quantityForm.Quantity;
|
||||
|
||||
var mostRecentTransaction =
|
||||
inventory.Transactions.OrderByDescending(t => t.TransactionDate).First();
|
||||
if (form.DistributedDate < mostRecentTransaction.TransactionDate)
|
||||
errors.Add($"'{inventory.InventoryType.Name}' most recent transaction " +
|
||||
$"is {mostRecentTransaction.TransactionDate.ToShortDateString()}. " +
|
||||
$"Cannot add a transaction on {form.DistributedDate.ToShortDateString()}.");
|
||||
|
||||
inventory.Transactions.Add(new Transaction
|
||||
{
|
||||
TransactionType = TransactionType.Distributed,
|
||||
RemovedQuantity = quantityForm.Quantity,
|
||||
CurrentQuantity = inventory.Quantity,
|
||||
Memo = "Distributed to " + form.Destination,
|
||||
Memo =
|
||||
string.IsNullOrEmpty(form.Memo)
|
||||
? $"{form.Memo} | "
|
||||
: ""
|
||||
+ $"Distributed to {form.Destination}",
|
||||
Timestamp = DateTime.Now,
|
||||
TransactionDate = form.DistributedDate
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
if (errors.Any())
|
||||
return GetErrorListJson(errors.ToArray());
|
||||
|
||||
_context.SaveChanges();
|
||||
|
||||
return BetterJson(AllInventory()
|
||||
.ProjectTo<InventoryViewModel>()
|
||||
.ToArray());
|
||||
}
|
||||
|
||||
public JsonResult Remove(InventoryRemoveForm form)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
return GetModelStateErrorListJson();
|
||||
|
||||
var errors = new List<string>();
|
||||
|
||||
var inventory = _context.Inventories.Find(form.InventoryId);
|
||||
|
||||
// check if it's really there
|
||||
if (inventory == null)
|
||||
{
|
||||
errors.Add($"'{form.InventoryId}' not found");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (inventory.Quantity < form.Quantity)
|
||||
errors.Add(
|
||||
$"'{inventory.InventoryType.Name}' has only {inventory.Quantity} qty, " +
|
||||
$"cannot remove {form.Quantity}");
|
||||
|
||||
inventory.Quantity -= form.Quantity;
|
||||
|
||||
var mostRecentTransaction =
|
||||
inventory.Transactions.OrderByDescending(t => t.TransactionDate).First();
|
||||
if (form.RemovedDate < mostRecentTransaction.TransactionDate)
|
||||
errors.Add($"'{inventory.InventoryType.Name}' most recent transaction " +
|
||||
$"is {mostRecentTransaction.TransactionDate.ToShortDateString()}. " +
|
||||
$"Cannot add a transaction on {form.RemovedDate.ToShortDateString()}.");
|
||||
|
||||
inventory.Transactions.Add(new Transaction
|
||||
{
|
||||
TransactionType = form.TransactionType,
|
||||
RemovedQuantity = form.Quantity,
|
||||
CurrentQuantity = inventory.Quantity,
|
||||
Memo = form.Memo,
|
||||
Timestamp = DateTime.Now,
|
||||
TransactionDate = form.RemovedDate
|
||||
});
|
||||
}
|
||||
|
||||
if (errors.Any())
|
||||
return GetErrorListJson(errors.ToArray());
|
||||
|
||||
_context.SaveChanges();
|
||||
|
||||
return BetterJson(Mapper.Map<InventoryViewModel>(inventory));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user