public RedirectToRouteResult MsgClicked(string ReqFormId) { using (SSISdbEntities e = new SSISdbEntities()) { DAL.StaffRequisitionRepositoryImpl dal = new DAL.StaffRequisitionRepositoryImpl(e); StaffRequisitionHeader srh = e.StaffRequisitionHeaders.Where(x => x.FormID == ReqFormId).FirstOrDefault(); if (srh.NotificationStatus != "Read") { if (User.IsInRole("Department Head")) { int noUnreadRequests = (int)Session["NoUnreadRequests"]; noUnreadRequests--; Session["NoUnreadRequests"] = noUnreadRequests; } else { int noUnreadRequests = (int)Session["NoUnreadRequestsEmployee"]; noUnreadRequests--; Session["NoUnreadRequestsEmployee"] = noUnreadRequests; } } srh.NotificationStatus = "Read"; dal.UpdateStaffRequisitionHeader(srh); e.SaveChanges(); } return(RedirectToAction("RequisitionHistory", "Dept")); }
public RedirectToRouteResult Reject(String data) { //update staff req using (SSISdbEntities e = new SSISdbEntities()) { DAL.StaffRequisitionRepositoryImpl dal = new DAL.StaffRequisitionRepositoryImpl(e); StaffRequisitionHeader srh = dal.GetStaffRequisitionHeaderById(Session["ReviewNewRequisitionId"].ToString()); srh.ApprovalStatus = "Rejected"; srh.Approver = Session["UserId"].ToString(); srh.DateProcessed = System.DateTime.Now; srh.Status = "Cancelled"; srh.Remarks = data; if (srh.NotificationStatus == "Unread") { int noUnreadRequests = (int)Session["NoUnreadRequests"]; noUnreadRequests--; Session["NoUnreadRequests"] = noUnreadRequests; } srh.NotificationStatus = "Unread"; dal.UpdateStaffRequisitionHeader(srh); e.SaveChanges(); Employee req = e.Employees.Where(x => x.EmployeeID == srh.EmployeeID).FirstOrDefault(); string title = "[LogicUniversity] Requisition Rejected: " + srh.FormID; string message = "Your requisition has rejected due to: " + data; CommonLogic.Email.sendEmail("*****@*****.**", req.EmployeeEmail, title, message); } return(RedirectToAction("Approval", "Dept")); }
public void ApproveTest() { HttpContext.Current = SA46Team1_Web_ADProjTests.MockSession.FakeHttpContext(); var wrapper = new HttpContextWrapper(HttpContext.Current); DeptApprovalController controller = new DeptApprovalController(); controller.ControllerContext = new ControllerContext(wrapper, new RouteData(), controller); HttpContext.Current.Session["ReviewNewRequisitionId"] = "SR-077"; HttpContext.Current.Session["UserId"] = "E1"; HttpContext.Current.Session["NoUnreadRequests"] = 10; var results = controller.Approve("remarks") as RedirectToRouteResult; using (SSISdbEntities m = new SSISdbEntities()) { StaffRequisitionHeader srh = m.StaffRequisitionHeaders.Where(x => x.FormID == "SR-077").FirstOrDefault(); Assert.AreEqual("Approved", srh.ApprovalStatus); } Assert.IsTrue(results.RouteValues.ContainsKey("action")); Assert.IsTrue(results.RouteValues.ContainsKey("controller")); Assert.AreEqual("Approval", results.RouteValues["action"].ToString()); Assert.AreEqual("Dept", results.RouteValues["controller"].ToString()); }
public ActionResult DisplayReqHistoryDetails2(string formId) { TempData["goBackToDelivery"] = true; Session["DeptReqTabIndex"] = "3"; string deptCode = Session["DepartmentCode"].ToString(); ReqHistoryModel model; using (SSISdbEntities e = new SSISdbEntities()) { StaffRequisitionHeader srh = e.StaffRequisitionHeaders.Where(x => x.FormID == formId).FirstOrDefault(); string repId = e.DepartmentDetails.Where(x => x.DepartmentCode == deptCode).Select(x => x.RepresentativeID).FirstOrDefault(); string repName = e.Employees.Where(x => x.EmployeeID == repId).Select(x => x.EmployeeName).FirstOrDefault(); string requestor = e.Employees.Where(x => x.EmployeeID == srh.EmployeeID).Select(x => x.EmployeeName).FirstOrDefault(); string approverName = e.Employees.Where(x => x.EmployeeID == srh.Approver).Select(x => x.EmployeeName).FirstOrDefault(); string approvalStatus = srh.ApprovalStatus; DateTime requestDate = srh.DateRequested; model = new ReqHistoryModel(); model.ApprovalStatus = approvalStatus; model.ApproverName = approverName; model.RepName = repName; model.RequestDate = requestDate; model.Requestor = requestor; Session["CurrentReqHistory"] = model; } Session["ReqHistoryPage"] = "2"; Session["id"] = formId; return(RedirectToAction("RequisitionHistory", "Dept")); }
public RedirectToRouteResult DiscardSelBackorders(string[] itemCodes, string[] formIds) { using (SSISdbEntities e = new SSISdbEntities()) { DAL.StaffRequisitionDetailsRepositoryImpl dal = new DAL.StaffRequisitionDetailsRepositoryImpl(e); DAL.StaffRequisitionRepositoryImpl dalHeader = new DAL.StaffRequisitionRepositoryImpl(e); int index = 0; foreach (string i in itemCodes) { string formId = formIds[index]; //update SRD StaffRequisitionDetail srd = new StaffRequisitionDetail(); srd = dal.GetStaffRequisitionDetailById(formId, itemCodes[index]); srd.CancelledBackOrdered = srd.QuantityBackOrdered; srd.QuantityBackOrdered = 0; dal.UpdateStaffRequisitionDetail(srd); //update SRH StaffRequisitionHeader srh = new StaffRequisitionHeader(); srh = dalHeader.GetStaffRequisitionHeaderById(formId); string stockRetrievalId = e.StockRetrievalReqForms.Where(x => x.ReqFormID == formId).Select(x => x.StockRetrievalID).FirstOrDefault(); byte? disbursedStatus = e.StockRetrievalHeaders.Where(x => x.ID == stockRetrievalId).Select(x => x.Disbursed).FirstOrDefault(); bool backOrderStatus = false; List <StaffRequisitionDetail> reqDetailsList = e.StaffRequisitionDetails.Where(x => x.FormID == formId).ToList(); foreach (StaffRequisitionDetail detail in reqDetailsList) { if (detail.QuantityBackOrdered > 0) { backOrderStatus = true; } } switch (backOrderStatus) { case true: //backorder exists for current SR break; case false: srh.Status = "Cancelled"; break; } dalHeader.UpdateStaffRequisitionHeader(srh); index++; e.SaveChanges(); } } Session["DeptReqTabIndex"] = "2"; return(RedirectToAction("Requisition", "Dept")); }
//[Route("NewReq/SubmitNewRequestForm")] public RedirectToRouteResult SubmitNewRequestForm() { string deptCode = Session["DepartmentCode"].ToString(); string employeeCode = Session["LoginEmployeeID"].ToString(); using (SSISdbEntities e = new SSISdbEntities()) { DAL.StaffRequisitionDetailsRepositoryImpl dalDetails = new DAL.StaffRequisitionDetailsRepositoryImpl(e); DAL.StaffRequisitionRepositoryImpl dalHeader = new DAL.StaffRequisitionRepositoryImpl(e); StaffRequisitionHeader srh = new StaffRequisitionHeader(); srh.FormID = Session["currentFormId"].ToString(); srh.DepartmentCode = deptCode; srh.EmployeeID = employeeCode; srh.DateRequested = System.DateTime.Now; srh.Status = "Open"; srh.ApprovalStatus = "Pending"; srh.DateProcessed = null; srh.Approver = e.Employees.Where(x => x.EmployeeID == srh.EmployeeID).Select(x => x.ReportsTo).FirstOrDefault(); srh.NotificationStatus = "Unread"; dalHeader.InsertStaffRequisitionHeader(srh); foreach (StaffRequisitionDetail srd in (List <StaffRequisitionDetail>)Session["newReqList"]) { srd.Item = null; dalDetails.InsertStaffRequisitionDetail(srd); } e.SaveChanges(); Session["newReqList"] = new List <StaffRequisitionDetail>(); int noUnreadRequests = (int)Session["NoUnreadRequests"]; noUnreadRequests++; Session["NoUnreadRequests"] = noUnreadRequests; Employee current = e.Employees.Where(x => x.EmployeeID == employeeCode).FirstOrDefault(); Employee head = e.Employees.Where(x => x.EmployeeID == current.ReportsTo).FirstOrDefault(); string title = "[LogicUniversity] New Requisition: " + srh.FormID; string message = Session["EmpName"].ToString() + " has raised a new requisition."; CommonLogic.Email.sendEmail("*****@*****.**", head.EmployeeEmail, title, message); } Session["tempList"] = new List <String>(); return(RedirectToAction("Requisition", "Dept")); }
public RedirectToRouteResult DisplayApprovalDetails(string ReqFormId) { Session["ReqApprovalPage"] = "2"; Session["ReviewNewRequisitionId"] = ReqFormId; using (SSISdbEntities e = new SSISdbEntities()) { DAL.StaffRequisitionRepositoryImpl dal = new DAL.StaffRequisitionRepositoryImpl(e); StaffRequisitionHeader srh = dal.GetStaffRequisitionHeaderById(ReqFormId); TempData["RequisitionRequstor"] = e.Employees.Where(x => x.EmployeeID == srh.EmployeeID).Select(x => x.EmployeeName).First(); TempData["RequisitionDateReq"] = srh.DateRequested; } return(RedirectToAction("Approval", "Dept")); }
public RedirectToRouteResult DiscardSelReqItems(string data, int index) { string formId = Session["id"].ToString(); Session["existingReqEditMode"] = false; //update details of current req history using (SSISdbEntities m = new SSISdbEntities()) { m.Configuration.ProxyCreationEnabled = false; DAL.StaffRequisitionDetailsRepositoryImpl dal = new DAL.StaffRequisitionDetailsRepositoryImpl(m); string itemCode = m.Items.Where(x => x.Description == data).Select(x => x.ItemCode).FirstOrDefault(); dal.DeleteStaffRequisitionDetail(formId, itemCode); int noOfItemsInRequest = m.StaffRequisitionDetails.Where(x => x.FormID == formId).ToList().Count(); if (noOfItemsInRequest == 1) { DAL.StaffRequisitionRepositoryImpl dalHeader = new DAL.StaffRequisitionRepositoryImpl(m); StaffRequisitionHeader srh = m.StaffRequisitionHeaders.Where(x => x.FormID == formId).FirstOrDefault(); srh.Status = "Withdrawn"; //to add in list of constants srh.ApprovalStatus = "Withdrawn"; dalHeader.UpdateStaffRequisitionHeader(srh); if (srh.NotificationStatus == "Unread") { int noUnreadRequests = (int)Session["NoUnreadRequests"]; noUnreadRequests--; Session["NoUnreadRequests"] = noUnreadRequests; } } m.SaveChanges(); } Session["ReqHistoryPage"] = "2"; return(RedirectToAction("RequisitionHistory", "Dept")); }
public void UpdateStaffRequisitionHeader(StaffRequisitionHeader staffRequisitionHeader) { context.Entry(staffRequisitionHeader).State = EntityState.Modified; }
public void DeleteStaffRequisitionHeader(string formId) { StaffRequisitionHeader staffRequisitionHeader = context.StaffRequisitionHeaders.Find(formId); context.StaffRequisitionHeaders.Remove(staffRequisitionHeader); }
public void InsertStaffRequisitionHeader(StaffRequisitionHeader staffRequisitionHeader) { context.StaffRequisitionHeaders.Add(staffRequisitionHeader); }
public Task Execute(IJobExecutionContext context) { using (SSISdbEntities m = new SSISdbEntities()) { #region Variables // List of StaffReq with Open & Approved List <StaffRequisitionHeader> staffReqHeadList = m.StaffRequisitionHeaders .Where(x => x.Status == "Open" && x.ApprovalStatus == "Approved").ToList(); // List of StaffReq with Outstanding List <StaffRequisitionHeader> srhWithBO = m.StaffRequisitionHeaders.Where(x => x.Status == "Outstanding").ToList(); Dictionary <Item, int> itemAndQty = new Dictionary <Item, int>(); List <StaffRequisitionDetail> srdList = new List <StaffRequisitionDetail>(); List <StaffRequisitionDetail> srdWithBackOrders = new List <StaffRequisitionDetail>(); Dictionary <Item, List <string> > listOfItemsAndColAdded = new Dictionary <Item, List <string> >(); List <string> colpt = new List <string>(); List <StaffRequisitionDetail> allsrd = new List <StaffRequisitionDetail>(); #endregion #region Prepare new StaffRequisitionDetails to add // Search for all Outstanding StaffReqDetails foreach (StaffRequisitionHeader srhBO in srhWithBO) { List <StaffRequisitionDetail> SRD = m.StaffRequisitionDetails .Where(x => x.FormID == srhBO.FormID && x.QuantityBackOrdered > 0).ToList(); if (SRD != null) { foreach (StaffRequisitionDetail sr in SRD) { srdWithBackOrders.Add(sr); } } } // Search for all Open && Approved StaffReqDetails foreach (StaffRequisitionHeader srhInWeek in staffReqHeadList) { srhInWeek.Status = "Outstanding"; m.SaveChanges(); // Find List of StaffReqDetails based on Header ID List <StaffRequisitionDetail> SRD = m.StaffRequisitionDetails .Where(x => x.FormID == srhInWeek.FormID).ToList(); if (SRD != null) { foreach (StaffRequisitionDetail sr in SRD) { srdList.Add(sr); } } } #endregion // Check if there's any entries to collate if (srdList.Count() > 0 || srdWithBackOrders.Count() > 0) { #region Create StockRetrievalHeader // Create StockRetrievalHeader StockRetrievalHeader newsrh = new StockRetrievalHeader(); int newSRH = m.StockRetrievalHeaders.Count() + 1; string srhId = CommonLogic.SerialNo(newSRH, "StoR"); newsrh.ID = srhId; newsrh.Date = DateTime.Now; newsrh.Disbursed = 0; newsrh.AllItemsRetrieved = 0; m.StockRetrievalHeaders.Add(newsrh); //m.SaveChanges(); #endregion #region Confirming how many Items & quantity to collate // Collate items based on Backorder SRDs (Total up items and its quantity to collect) if (srdWithBackOrders.Count() > 0) { foreach (StaffRequisitionDetail srdlist in srdWithBackOrders) { Item item = m.Items.Where(x => x.ItemCode == srdlist.ItemCode).FirstOrDefault(); if (!itemAndQty.ContainsKey(item)) { itemAndQty.Add(item, srdlist.QuantityBackOrdered); } else { itemAndQty[item] += srdlist.QuantityBackOrdered; } } } // Collate items based on new SRD (Total up items and its quantity to collect) if (srdList.Count() > 0) { foreach (StaffRequisitionDetail srdlist in srdList) { Item item = m.Items.Where(x => x.ItemCode == srdlist.ItemCode).FirstOrDefault(); if (!itemAndQty.ContainsKey(item)) { itemAndQty.Add(item, srdlist.QuantityOrdered); } else { itemAndQty[item] += srdlist.QuantityOrdered; } } } // Check if quantity is sufficient (If insufficient, can only collect based on Quantity on Hand) foreach (Item itemToCollate in itemAndQty.Keys.ToList()) { if (itemToCollate.Quantity < itemAndQty[itemToCollate]) { itemAndQty[itemToCollate] = itemToCollate.Quantity; } } #endregion #region Fulfill Backorders // Fulfill all backorders first if (srdWithBackOrders.Count() > 0) { foreach (StaffRequisitionDetail retrievalListWithBO in srdWithBackOrders) { Item itemRetrieved = m.Items.Where(x => x.ItemCode == retrievalListWithBO.ItemCode).FirstOrDefault(); Bin bin = m.Bins.Where(x => x.ItemCode == retrievalListWithBO.ItemCode).FirstOrDefault(); StaffRequisitionHeader srhDeptCode = m.StaffRequisitionHeaders.Where(x => x.FormID == retrievalListWithBO.FormID).FirstOrDefault(); DepartmentDetail dd = m.DepartmentDetails.Where(x => x.DepartmentCode == srhDeptCode.DepartmentCode).FirstOrDefault(); StockRetrievalDetail newsrd = new StockRetrievalDetail(); int qtyAvailable = itemAndQty[itemRetrieved]; // Only create entries with qty available if (qtyAvailable > 0) { // Create quantity requested that is really available for that item int qtyToAdd = 0; if (qtyAvailable > retrievalListWithBO.QuantityBackOrdered) { qtyToAdd = retrievalListWithBO.QuantityBackOrdered; } else { qtyToAdd = qtyAvailable; } if (listOfItemsAndColAdded.ContainsKey(itemRetrieved)) { if (!listOfItemsAndColAdded[itemRetrieved].Contains(dd.CollectionPointID)) { // If its an entirely new Item for the collection point of the SRD, create a new entry newsrd.Id = srhId; newsrd.Bin = bin.Number; newsrd.ItemCode = retrievalListWithBO.ItemCode; newsrd.QuantityRetrieved = qtyToAdd; newsrd.CollectionPointID = dd.CollectionPointID; newsrd.QuantityAdjusted = 0; newsrd.Remarks = ""; newsrd.Collected = 0; m.StockRetrievalDetails.Add(newsrd); allsrd.Add(retrievalListWithBO); // Prepare list for iteration of repeated entries (Indicate that there's exisiting record of Item & Collection point) List <string> existingColpt = listOfItemsAndColAdded[itemRetrieved]; existingColpt.Add(dd.CollectionPointID); listOfItemsAndColAdded[itemRetrieved] = existingColpt; } else { // If there's existing entry (Based on previous collection point / back orders created), add to entry StockRetrievalDetail existingSRD = m.StockRetrievalDetails .Where(x => x.Id == srhId && x.ItemCode == itemRetrieved.ItemCode && x.CollectionPointID == dd.CollectionPointID).FirstOrDefault(); existingSRD.QuantityRetrieved += qtyToAdd; allsrd.Add(retrievalListWithBO); } } else { // If its an entirely new Item for the collection point of the SRD, create a new entry newsrd.Id = srhId; newsrd.Bin = bin.Number; newsrd.ItemCode = retrievalListWithBO.ItemCode; newsrd.QuantityRetrieved = qtyToAdd; newsrd.CollectionPointID = dd.CollectionPointID; newsrd.QuantityAdjusted = 0; newsrd.Remarks = ""; newsrd.Collected = 0; m.StockRetrievalDetails.Add(newsrd); allsrd.Add(retrievalListWithBO); colpt = new List <string>(); colpt.Add(dd.CollectionPointID); listOfItemsAndColAdded.Add(itemRetrieved, colpt); } //Remove quantity added from dictionary itemAndQty[itemRetrieved] -= qtyToAdd; m.SaveChanges(); } } } #endregion #region Fulfill new entries // Create StockRetrievalDetails if (srdList.Count() > 0) { foreach (StaffRequisitionDetail retrievalList in srdList) { Item itemRetrieved = m.Items.Where(x => x.ItemCode == retrievalList.ItemCode).FirstOrDefault(); Bin bin = m.Bins.Where(x => x.ItemCode == retrievalList.ItemCode).FirstOrDefault(); StaffRequisitionHeader srhDeptCode = m.StaffRequisitionHeaders .Where(x => x.FormID == retrievalList.FormID).FirstOrDefault(); DepartmentDetail dd = m.DepartmentDetails .Where(x => x.DepartmentCode == srhDeptCode.DepartmentCode).FirstOrDefault(); StockRetrievalDetail newsrd = new StockRetrievalDetail(); int qtyAvailable = itemAndQty[itemRetrieved]; // Only create entries with qty available if (qtyAvailable > 0) { // Create quantity requested that is really available for that item int qtyToAdd = 0; if (qtyAvailable > retrievalList.QuantityOrdered) { qtyToAdd = retrievalList.QuantityOrdered; } else { qtyToAdd = qtyAvailable; } if (listOfItemsAndColAdded.ContainsKey(itemRetrieved)) { if (!listOfItemsAndColAdded[itemRetrieved].Contains(dd.CollectionPointID)) { // If its an entirely new Item for the collection point of the SRD, create a new entry newsrd.Id = srhId; newsrd.Bin = bin.Number; newsrd.ItemCode = retrievalList.ItemCode; newsrd.QuantityRetrieved = qtyToAdd; newsrd.CollectionPointID = dd.CollectionPointID; newsrd.QuantityAdjusted = 0; newsrd.Remarks = ""; newsrd.Collected = 0; m.StockRetrievalDetails.Add(newsrd); allsrd.Add(retrievalList); // Prepare list for iteration of repeated entries (Indicate that there's exisiting record of Item & Collection point) List <string> existingColpt = listOfItemsAndColAdded[itemRetrieved]; existingColpt.Add(dd.CollectionPointID); listOfItemsAndColAdded[itemRetrieved] = existingColpt; } else { // If there's existing entry (Based on previous collection point / back orders created), add to entry StockRetrievalDetail existingSRD = m.StockRetrievalDetails .Where(x => x.Id == srhId && x.ItemCode == itemRetrieved.ItemCode && x.CollectionPointID == dd.CollectionPointID).FirstOrDefault(); existingSRD.QuantityRetrieved += qtyToAdd; allsrd.Add(retrievalList); } } else { // If its an entirely new Item for the collection point of the SRD, create a new entry newsrd.Id = srhId; newsrd.Bin = bin.Number; newsrd.ItemCode = retrievalList.ItemCode; newsrd.QuantityRetrieved = qtyToAdd; newsrd.CollectionPointID = dd.CollectionPointID; newsrd.QuantityAdjusted = 0; newsrd.Remarks = ""; newsrd.Collected = 0; m.StockRetrievalDetails.Add(newsrd); allsrd.Add(retrievalList); colpt = new List <string>(); colpt.Add(dd.CollectionPointID); listOfItemsAndColAdded.Add(itemRetrieved, colpt); } // Remove qty that is added itemAndQty[itemRetrieved] -= qtyToAdd; m.SaveChanges(); } } } #endregion #region Create StockRetrievalReqForm List <String> allNewlyCreatedReqFormId = new List <String>(); foreach (StaffRequisitionDetail allNewlyCreated in allsrd) { allNewlyCreatedReqFormId.Add(allNewlyCreated.FormID); } //To remove duplicate req form id allNewlyCreatedReqFormId = allNewlyCreatedReqFormId.Distinct().ToList(); foreach (String newReqFormId in allNewlyCreatedReqFormId) { // New StockRetrievalReqForm StockRetrievalReqForm srrf = new StockRetrievalReqForm(); //srrf.ReqFormID = allNewlyCreated.FormID; srrf.ReqFormID = newReqFormId; srrf.StockRetrievalID = srhId; m.StockRetrievalReqForms.Add(srrf); m.SaveChanges(); } #endregion } } return(null); }
public RedirectToRouteResult DisburseItems() { using (SSISdbEntities m = new SSISdbEntities()) { m.Configuration.ProxyCreationEnabled = false; string id = (string)Session["RetrievalId"]; StockRetrievalHeader srh = m.StockRetrievalHeaders.Where(x => x.ID == id).First(); srh.Disbursed = 1; List <StockRetrievalDetail> itemsRetrieved = m.StockRetrievalDetails.Where(x => x.Id == id).ToList(); bool stockAdjustmentHeaderCreated = false; foreach (StockRetrievalDetail srd in itemsRetrieved) { DateTime localDate = DateTime.Now; if (srd.QuantityAdjusted > 0) { //To check whether there is stock adjustment header for the item if (!stockAdjustmentHeaderCreated) { //To Create Stock Adjustment Header stockAdjustmentHeaderCreated = true; StockAdjustmentHeader sah = new StockAdjustmentHeader(); int stockAdjustmentHeaderId = m.StockAdjustmentHeaders.Count() + 1; sah.RequestId = CommonLogic.SerialNo(stockAdjustmentHeaderId, "SA"); sah.DateRequested = localDate; sah.Requestor = (string)Session["LoginEmployeeID"]; sah.TransactionType = "Stock Adjustment"; m.StockAdjustmentHeaders.Add(sah); m.SaveChanges(); } //To Create Stock Adjustment Details int stockAdjustmentDetailId = m.StockAdjustmentHeaders.Count(); StockAdjustmentDetail sad = new StockAdjustmentDetail(); sad.RequestId = CommonLogic.SerialNo(stockAdjustmentDetailId, "SA"); sad.ItemCode = srd.ItemCode; sad.ItemQuantity = srd.QuantityAdjusted; float itemUnitCost = m.Items.Where(x => x.ItemCode == sad.ItemCode).Select(x => x.AvgUnitCost).FirstOrDefault(); sad.Amount = itemUnitCost * sad.ItemQuantity; sad.Remarks = srd.Remarks; sad.Status = "Pending"; m.StockAdjustmentDetails.Add(sad); //To add the item transactions ItemTransaction itemTransaction = new ItemTransaction(); itemTransaction.TransDateTime = localDate; itemTransaction.DocumentRefNo = sad.RequestId; itemTransaction.ItemCode = sad.ItemCode; itemTransaction.TransactionType = "Stock Adjustment"; itemTransaction.Quantity = sad.ItemQuantity; itemTransaction.UnitCost = itemUnitCost; itemTransaction.Amount = sad.Amount; m.ItemTransactions.Add(itemTransaction); //To update the quantity of the item table Item itemAdjusted = m.Items.Where(x => x.ItemCode == itemTransaction.ItemCode).FirstOrDefault(); itemAdjusted.Quantity -= itemTransaction.Quantity; m.SaveChanges(); } } //Creating list of new disbursements //To order by id so the earlier id will mean that the req form was submitted earlier List <String> reqFormIDList = m.StockRetrievalReqForms.OrderBy(x => x.Id).Where(x => x.StockRetrievalID == id).Select(x => x.ReqFormID).ToList <String>(); //Create distinct disbursement headers List <String> disbHeaderDeptCodes = new List <String>(); //Take from stock retrieval details List <StockRetrievalDetail> stockRetrievalDetails = m.StockRetrievalDetails.Where(x => x.Id == id).ToList <StockRetrievalDetail>(); foreach (StockRetrievalDetail srd in stockRetrievalDetails) { //To take care that in case stock adjustment makes the quantity retrieved to be 0 if (srd.QuantityRetrieved > 0) { String deptCode = m.DepartmentDetails.Where(x => x.CollectionPointID == srd.CollectionPointID).FirstOrDefault().DepartmentCode; disbHeaderDeptCodes.Add(deptCode); } } //Make it distinct so that only one disbursement header is created disbHeaderDeptCodes = disbHeaderDeptCodes.Distinct().ToList(); foreach (String deptCode in disbHeaderDeptCodes) { DisbursementHeader newDH = new DisbursementHeader(); int count = m.DisbursementHeaders.Count() + 1; string disId = CommonLogic.SerialNo(count, "DH"); newDH.Id = disId; newDH.Status = "Open"; newDH.Date = DateTime.Now; newDH.Amount = 0; //Put 0 first and then to be calculated after all the disbursement details is done newDH.StockRetrievalId = id; newDH.DepartmentCode = deptCode; newDH.CollectionPointID = m.DepartmentDetails.Where(x => x.DepartmentCode == deptCode).FirstOrDefault().CollectionPointID; newDH.RepresentativeID = m.DepartmentDetails.Where(x => x.DepartmentCode == newDH.DepartmentCode).FirstOrDefault().RepresentativeID; float totalAmount = 0f; //Create disbursement details, since one collection point is for one dept, then the entire stock retrieved would be assigned to that dept foreach (StockRetrievalDetail srd in stockRetrievalDetails) { String deptCodeDH = m.DepartmentDetails.Where(x => x.CollectionPointID == srd.CollectionPointID).Select(x => x.DepartmentCode).FirstOrDefault(); //Only disbursed if quantity retrieved is more than 0 if (srd.QuantityRetrieved > 0 && deptCodeDH.Equals(newDH.DepartmentCode)) { DisbursementDetail newDD = new DisbursementDetail(); newDD.Id = disId; newDD.ItemCode = srd.ItemCode; int quantityOrdered = 0; foreach (String reqF in reqFormIDList) { StaffRequisitionDetail staffReqDet = m.StaffRequisitionDetails.Where(x => x.FormID == reqF && x.ItemCode == newDD.ItemCode).FirstOrDefault(); StaffRequisitionHeader staffReqHeader = m.StaffRequisitionHeaders.Where(x => x.FormID == reqF).FirstOrDefault(); int reqQtyOrdered = 0; if (staffReqDet != null && staffReqHeader != null) { if (staffReqHeader.DepartmentCode.Equals(newDH.DepartmentCode)) { reqQtyOrdered = staffReqDet.QuantityOrdered; } } quantityOrdered = quantityOrdered + reqQtyOrdered; } newDD.QuantityOrdered = quantityOrdered; newDD.QuantityReceived = srd.QuantityRetrieved; float itemUnitCost = m.Items.Where(x => x.ItemCode == newDD.ItemCode).Select(x => x.AvgUnitCost).FirstOrDefault(); newDD.UnitCost = itemUnitCost; newDD.UoM = m.Items.Where(x => x.ItemCode == newDD.ItemCode).Select(x => x.UoM).FirstOrDefault(); newDD.QuantityAdjusted = 0; newDD.TransactionType = "Disbursement"; float amount = itemUnitCost * newDD.QuantityReceived; totalAmount += amount; m.DisbursementDetails.Add(newDD); //To add the item transactions DateTime localDate = DateTime.Now; ItemTransaction itemTransaction = new ItemTransaction(); itemTransaction.TransDateTime = localDate; itemTransaction.DocumentRefNo = newDD.Id; itemTransaction.ItemCode = newDD.ItemCode; itemTransaction.TransactionType = "Disbursement"; itemTransaction.Quantity = newDD.QuantityReceived; itemTransaction.UnitCost = itemUnitCost; itemTransaction.Amount = newDD.QuantityReceived * itemUnitCost; m.ItemTransactions.Add(itemTransaction); //To update the quantity of the item table Item itemDisbursed = m.Items.Where(x => x.ItemCode == itemTransaction.ItemCode).FirstOrDefault(); itemDisbursed.Quantity -= itemTransaction.Quantity; } } newDH.Amount = totalAmount; m.DisbursementHeaders.Add(newDH); m.SaveChanges(); } } return(RedirectToAction("Disbursements", "Store")); }