public void CreateRequestDetail(WCFItemTotalQty req) { Request r = new Work().GetRequest(); Request_Details rdetail = new Request_Details(); rdetail.deleted = "N"; rdetail.request_id = r.request_id; Stock_Inventory item = new Work().GetStockInventory(req.ItemDes); rdetail.item_code = item.item_code; rdetail.orig_quantity = Convert.ToInt32(req.TotalQty); new Work().CreateRequestDetail(rdetail); Request_Details newreq = new Work().GetLastRequestDetail(); Request_Event revent = new Request_Event(); revent.request_detail_id = newreq.request_detail_id; revent.status = RequestStatus.PENDING; revent.quantity = Convert.ToInt32(newreq.orig_quantity); revent.date_time = Convert.ToDateTime(r.date_time); revent.deleted = "N"; revent.username = r.username; new Work().CreateRequestEvent(revent); }
public bool saveNewRequest(RequestModel request) { DateTime timestamp = DateTime.Now; using (var transaction = context.Database.BeginTransaction()) { try { Request newRequest = new Request(); newRequest.current_status = RequestStatus.PENDING; newRequest.deleted = "N"; newRequest.dept_code = request.Department.dept_code; newRequest.reason = request.Reason; newRequest.rejected = "N"; newRequest.rejected_reason = ""; newRequest.username = request.UserModel.Username; newRequest.date_time = timestamp; List <Request_Details> newDetails = new List <Request_Details>(); foreach (KeyValuePair <ItemModel, int> itemAndQty in request.Items) { Request_Details newDetail = new Request_Details(); newDetail.deleted = "N"; newDetail.item_code = itemAndQty.Key.ItemCode; newDetail.orig_quantity = itemAndQty.Value; Request_Event newEvent = new Request_Event(); newEvent.deleted = "N"; newEvent.date_time = timestamp; newEvent.quantity = itemAndQty.Value; newEvent.allocated = 0; newEvent.not_allocated = 0; newEvent.status = RequestStatus.PENDING; newEvent.username = request.UserModel.Username; // Establish relationships newDetail.Request_Event.Add(newEvent); newRequest.Request_Details.Add(newDetail); } // Add to DB context.Requests.Add(newRequest); } catch (Exception exec) { transaction.Rollback(); throw exec; } transaction.Commit(); } return(true); }
public int markRequestAsRetrieved(RequestModel toAllocate, string currentUser) { int added = 0; // For each item provided, find the related item in DB, and add an event to it if (RequestStatus.requestHasHadStatus(toAllocate, RequestStatus.DISBURSED)) { throw new RequestAlreadyApprovedException("Already fully disbursed."); //return false; } DateTime timestamp = DateTime.Now; using (var transaction = context.Database.BeginTransaction()) { try { Request targetRequest = context.Requests.Find(toAllocate.RequestId); List <Request_Details> targetDetails = targetRequest.Request_Details.ToList(); foreach (KeyValuePair <ItemModel, int> itemAndQty in toAllocate.Items) { if (itemAndQty.Value == 0) { // No items to retrieve continue; } Request_Event newEvent = new Request_Event(); newEvent.deleted = "N"; newEvent.date_time = timestamp; newEvent.quantity = itemAndQty.Value; newEvent.status = EventStatus.RETRIEVED; newEvent.username = toAllocate.UserModel.Username; // Establish relationships Request_Details targetDetail = targetDetails.Where(x => x.item_code == itemAndQty.Key.ItemCode).First(); targetDetail.Request_Event.Add(newEvent); added++; } } catch (Exception exec) { transaction.Rollback(); throw exec; } transaction.Commit(); } return(added); }
public void DeleteRequestDetail(string id) { int req_id = Convert.ToInt32(id); Request_Details req = context.Request_Details.Where(x => x.request_detail_id == req_id).First(); req.deleted = "Y"; context.SaveChanges(); Request_Event revent = context.Request_Event.Where(x => x.request_detail_id == req.request_detail_id).First(); revent.deleted = "Y"; context.SaveChanges(); }
public void UpdateRequestDetail(string id, string qty) { int req_id = Convert.ToInt32(id); Request_Details req = context.Request_Details.Where(x => x.request_detail_id == req_id).First(); req.orig_quantity = Convert.ToInt32(qty); context.SaveChanges(); Request_Event revent = context.Request_Event.Where(x => x.request_detail_id == req.request_detail_id).First(); revent.quantity = Convert.ToInt32(qty); context.SaveChanges(); }
protected void btnSubmit_Click(object sender, EventArgs e) { // Get all the models List <ConfirmRetrievalViewModel> list = Session[SESSION_RETRIEVING_LIST] as List <ConfirmRetrievalViewModel>; 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)//gvToRetrieve.Rows) { //string expected = (gvr.FindControl("lblQtyExpected") as Label).Text; //string actual = (gvr.FindControl("tbQtyActual") as Label).Text; //int expectedQty = int.Parse(expected); //int actualQty = int.Parse(actual); int expectedQty = model.QuantityExpected; int actualQty = model.QuantityActual; if (actualQty > expectedQty) { lblWarningInfo.Text = "Items cannot have a higher actual quantity than what was to be retrieved."; //string.Format("Row {0} cannot have a higher actual quantity than expected.", gvr.DataItemIndex + 1); } if (expectedQty == actualQty) { okayItems.Add(model); } else { int difference = expectedQty - actualQty; notOkayItems.Add(model, difference); } } using (SSISEntities context = new SSISEntities()) { // 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, User.Identity.Name); }); 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, User.Identity.Name); } context.SaveChanges(); } // Disposal of context // If there were not okay items, record into file discrepancy session variable if (notOkayItems.Count > 0) { DiscrepancyDictionary discrepancyDict = Session[PUBLIC_SESSION_DISCREPANCY_DICT] as DiscrepancyDictionary; if (discrepancyDict == null) { discrepancyDict = new DiscrepancyDictionary(); } foreach (var item in notOkayItems) { discrepancyDict.AddOrIncrease(item.Key.ItemCode, item.Value); } Session[PUBLIC_SESSION_DISCREPANCY_DICT] = discrepancyDict; // Enable button to continue to fileDiscrepancies // Change to redirect to heng tiong's thing Response.Redirect("FileDiscrepency.aspx", false); } else { Response.Redirect(Request.Url.ToString(), false); } }
public bool updateRequestChanges(RequestModel newRequest) { if (RequestStatus.requestHasHadStatus(newRequest, RequestStatus.APPROVED)) { throw new RequestAlreadyApprovedException("Already approved or rejected."); //return false; } DateTime timestamp = DateTime.Now; using (var transaction = context.Database.BeginTransaction()) { //try //{ Request targetRequest = context.Requests.Find(newRequest.RequestId); List <Request_Details> targetDetails = targetRequest.Request_Details.ToList(); RequestModel targetRequestModel = findRequestById(newRequest.RequestId); foreach (var item in targetRequestModel.Items) { if (!newRequest.Items.ContainsKey(item.Key)) { // New requestModel does not contain old key // So add it in, but set quantity to 0 newRequest.Items.Add(item.Key, 0); } } foreach (KeyValuePair <ItemModel, int> itemAndQty in newRequest.Items) { Request_Event newEvent = new Request_Event(); newEvent.deleted = "N"; newEvent.date_time = timestamp; newEvent.quantity = itemAndQty.Value; newEvent.status = EventStatus.UPDATED; newEvent.username = newRequest.UserModel.Username; // Establish relationships Request_Details targetDetail = targetDetails.Where(x => x.item_code == itemAndQty.Key.ItemCode).DefaultIfEmpty(null).FirstOrDefault(); if (targetDetail == null) { // Need to insert new detail Request_Details newDetail = new Request_Details(); newDetail.deleted = "N"; newDetail.item_code = itemAndQty.Key.ItemCode; newDetail.orig_quantity = itemAndQty.Value; newDetail.Request_Event.Add(newEvent); newDetail.request_id = newRequest.RequestId; context.Request_Details.Add(newDetail); } else { newEvent.request_detail_id = targetDetail.request_detail_id; // Add to DB context.Request_Event.Add(newEvent); } //targetDetail.Request_Event.Add(newEvent); } // Update Status context.Requests.Find(newRequest.RequestId).current_status = RequestStatus.UPDATED; context.Requests.Find(newRequest.RequestId).reason = newRequest.Reason; //} //catch (Exception exec) //{ //transaction.Rollback(); //throw exec; //} transaction.Commit(); } return(true); }
public static bool SignOffDisbursement(string currentUser, string currentDeptCode, Dictionary <string, int> itemCodeAndQuantities) { using (SSISEntities context = new SSISEntities()) { // Get all the models: DisbursementModelCollection disbursingList = FacadeFactory.getDisbursementService(context).getAllThatCanBeSignedOff(currentUser); var itemGroups = disbursingList.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 => new { k.ItemCode, k.Description, DeptCode = k.dept_code }, v => v) .ToList(); List <ConfirmDisbursementViewModel> modelDisbursingList = new List <ConfirmDisbursementViewModel>(); // For each item, create a view model foreach (var itemGroup in itemGroups) { // If the dept code is not correct, just SKIP if (itemGroup.Key.DeptCode != currentDeptCode) { continue; } 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.ItemCode]; ConfirmDisbursementViewModel model = new ConfirmDisbursementViewModel(); model.ItemCode = itemGroup.Key.ItemCode; model.ItemDescription = itemGroup.Key.Description; model.DeptCode = itemGroup.Key.DeptCode; model.QuantityExpected = itemQty; model.QuantityActual = actualQty; model.RequestIds = reqIds; modelDisbursingList.Add(model); } // Make sure all item codes were provided List <string> itemCodes = modelDisbursingList.Select(s => s.ItemCode).ToList(); if (false == itemCodes.TrueForAll(deptCode => itemCodeAndQuantities.ContainsKey(deptCode))) { // If provided dictionary does not have all the item codes, return false and stop return(false); } // Now do filtering: List <ConfirmDisbursementViewModel> okayItems = new List <ConfirmDisbursementViewModel>(); Dictionary <ConfirmDisbursementViewModel, int> notOkayItems = new Dictionary <ConfirmDisbursementViewModel, int>(); // Sort modelList into okay and not okay items foreach (ConfirmDisbursementViewModel model in modelDisbursingList) { 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).moveFromDisbursingToDisbursed(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).moveFromDisbursingToDisbursed(grouping.Key, items, currentUser); } context.SaveChanges(); } // Disposal of SSISEntities context return(true); }
public int allocateRequest(RequestModel toAllocate, string currentUser) { List <ItemModel> itemsAllocateable = new List <ItemModel>(); // First, make sure each item can be allocated in its entirety. If it can, allocate it. foreach (var item in toAllocate.Items) { if (item.Key.AvailableQuantity >= item.Value) { // Can be completely allocated itemsAllocateable.Add(item.Key); } } int added = 0; DateTime timestamp = DateTime.Now; using (var transaction = context.Database.BeginTransaction()) { try { Request efRequest = context.Requests.Find(toAllocate.RequestId); if (efRequest == null) { throw new ItemNotFoundException("Could not find Request object"); } foreach (var item in itemsAllocateable) { // get associated request_detail Request_Details targetDetail = efRequest.Request_Details.Where(r => r.item_code == item.ItemCode).First(); if (targetDetail == null) { throw new ItemNotFoundException("Could not find Request_Details object"); } // For each item, add a new event that is allocated Request_Event newEvent = new Request_Event(); newEvent.request_detail_id = targetDetail.request_detail_id; // get the quantity from the provided item quantity in the method arguments newEvent.quantity = toAllocate.Items.Where(i => i.Key.ItemCode == item.ItemCode).First().Value; newEvent.status = EventStatus.ALLOCATED; newEvent.username = currentUser; newEvent.date_time = timestamp; newEvent.deleted = "N"; context.Request_Event.Add(newEvent); added++; } } catch (Exception exec) { transaction.Rollback(); throw exec; } transaction.Commit(); } return(added); }
public void CreateRequestDetail(Request_Details r) { context.Entry(r).State = System.Data.Entity.EntityState.Added; context.SaveChanges(); }