public IHttpActionResult GetRetrieval([FromBody] ConfirmRetrievalViewModel model) { var retrieval = new RetrievalService(context).FindRetrievalById(model.RetrievalId); if (retrieval == null) { return(NotFound()); } return(Ok(retrieval.Disbursements .SelectMany(d => d.DisbursementDetails .Select(dd => new RetrievalDetailByDeptViewModel() { Department = dd.Disbursement.Department.Name, DepartmentCode = dd.Disbursement.Department.DepartmentCode, ItemCode = dd.ItemCode, ItemName = dd.Item.Name, Bin = dd.Bin, PlanQuantity = dd.PlanQuantity, ActualQuantity = dd.ActualQuantity, Status = dd.Status.Name, RetrievalStatus = retrieval.Status.Name, Uom = dd.Item.Uom, }) ))); }
public IHttpActionResult ConfirmRetrieval([FromBody] ConfirmRetrievalViewModel model) { try { new RetrievalService(Context).ConfirmRetrieval(model.RetrievalId, model.Email); } catch (ArgumentException e) { return(BadRequest(e.Message)); } return(Ok(new MessageViewModel() { Message = "Successfully confirmed" })); }
public static bool ConfirmRetrievalFromWarehouse(string currentUser, Dictionary <string, int> itemCodeAndQuantities) { using (SSISEntities context = new SSISEntities()) { var allocated = FacadeFactory.getRetrievalService(context).getAllRetrievingByClerk(currentUser); var itemGroups = allocated.SelectMany(sm => sm.Items .Select(s => new { s.Key.ItemCode, s.Key.Description, Quantity = s.Value, sm.Department.dept_code, sm.RequestId, sm.Department.name }) ).GroupBy(k => k.ItemCode, v => v).ToList(); List <ConfirmRetrievalViewModel> list = new List <ConfirmRetrievalViewModel>(); foreach (var itemGroup in itemGroups) { int itemQty = itemGroup.Select(s => s.Quantity).Aggregate((a, b) => a + b); List <int> reqIds = itemGroup.Select(s => s.RequestId).ToList(); int actualQty = itemCodeAndQuantities[itemGroup.Key]; ConfirmRetrievalViewModel model = new ConfirmRetrievalViewModel(); model.ItemCode = itemGroup.Key; model.ItemDescription = itemGroup.First().Description; model.QuantityExpected = itemQty; model.QuantityActual = actualQty; model.RequestIds = reqIds; list.Add(model); } // Check if all item codes are present List <string> itemCodes = allocated.SelectMany(sm => sm.Items.Select(s => s.Key.ItemCode)).ToList(); if (false == itemCodes.TrueForAll(code => itemCodeAndQuantities.ContainsKey(code))) { // Some item codes are missing return(false); } // Do sorting List <ConfirmRetrievalViewModel> okayItems = new List <ConfirmRetrievalViewModel>(); Dictionary <ConfirmRetrievalViewModel, int> notOkayItems = new Dictionary <ConfirmRetrievalViewModel, int>(); // For each gvr, check if actual == expected foreach (ConfirmRetrievalViewModel model in list) { int expectedQty = model.QuantityExpected; int actualQty = model.QuantityActual; if (actualQty > expectedQty) { throw new ArgumentOutOfRangeException("Actual quantity cannot be more than expected quantity! For item: " + model.ItemCode); } if (expectedQty == actualQty) { okayItems.Add(model); } else { int difference = expectedQty - actualQty; notOkayItems.Add(model, difference); } } // Save the Okay Items // Get a list of all request ids and item code (de-normalized) okayItems.SelectMany(sm => sm.RequestIds .Select(s => new { RequestId = s, sm.ItemCode }) ) // Normalise and group by requestId .GroupBy(k => k.RequestId, v => v.ItemCode) .ToList() // For each, get the Request object, match the items in the request, and save to DB .ForEach(idAndItemCode => { Request request = context.Requests.Find(idAndItemCode.Key); Dictionary <string, int> itemCodeAndQty = request.Request_Details .Where(w => w.deleted != "Y" && idAndItemCode.Contains(w.item_code) ) .ToDictionary( k => k.item_code, v => v.Request_Event.First().allocated.Value ); FacadeFactory.getRequestMovementService(context).moveFromRetrievingToRetrieved(idAndItemCode.Key, itemCodeAndQty, currentUser); }); Dictionary < string, Dictionary <int, int> > itemCodeAndIdAndQty = new Dictionary <string, Dictionary <int, int> >(); // Go through not okay items and shift quantities around foreach (var item in notOkayItems) { // Get list of all requestIds List <Request> requestsByDateDesc = item.Key.RequestIds.Select(s => context.Requests.Find(s)).Where(w => w.deleted != "Y").OrderByDescending(o => o.date_time).ToList(); int shortfall = item.Value; Dictionary <int, int> idAndQty = new Dictionary <int, int>(); // First is the latest made request foreach (var request in requestsByDateDesc) { Request_Details detail = request.Request_Details.Where(w => w.deleted != "Y" && w.item_code == item.Key.ItemCode).DefaultIfEmpty(null).FirstOrDefault(); if (detail == null) { continue; } int origQty = detail.Request_Event.First().allocated.Value; int retrievedQty = origQty; if (shortfall > 0) { if (shortfall > origQty) { shortfall -= origQty; retrievedQty = 0; } else if (shortfall == origQty) { shortfall = 0; retrievedQty = 0; } else { // shortfall < origQty retrievedQty = origQty - shortfall; shortfall = 0; } } idAndQty.Add(request.request_id, retrievedQty); } itemCodeAndIdAndQty.Add(item.Key.ItemCode, idAndQty); // Minus the expected quantity for each request's item origQty, while expected quantity is > 0, // and don't minus if origQty is > expectedQty // Once you've hit zero or cannot minus, assign the leftover into the next request // Then the rest of the requests are gonna be zero // Then save all these requests using the MovementService } var notOkayGroupingsByRequestId = itemCodeAndIdAndQty .SelectMany(sm => sm.Value.Select(s => new { requestId = s.Key, itemCode = sm.Key, retrievedQty = s.Value }) ) .GroupBy(k => k.requestId, v => new { v.itemCode, v.retrievedQty }); foreach (var grouping in notOkayGroupingsByRequestId) { var items = grouping.ToDictionary(k => k.itemCode, v => v.retrievedQty); FacadeFactory.getRequestMovementService(context).moveFromRetrievingToRetrieved(grouping.Key, items, currentUser); } context.SaveChanges(); } // Disposal of context return(true); }