public static List <WCFDisburse> getAllPossibleSignOffsForUserForDept(string currentUser, string currentDeptCode) { List <WCFDisburse> wcfList = new List <WCFDisburse>(); using (SSISEntities context = new SSISEntities()) { 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(); foreach (var itemGroup in itemGroups) { //chnage dept code into dept name // 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); WCFDisburse wcfItem = new WCFDisburse(); wcfItem.ItemName = itemGroup.Key.Description; wcfItem.RetrievedQty = itemQty.ToString(); wcfItem.ItemCode = itemGroup.Key.ItemCode; wcfList.Add(wcfItem); } } return(wcfList); }
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); }
protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { return; } using (SSISEntities context = new SSISEntities()) { List <Collection_Point> collectionPts = context.Collection_Point.Where(w => w.deleted != "Y").ToList(); List <Department> departmentList = context.Departments.Where(w => w.deleted != "Y").ToList(); Session[SESSION_DEPARTMENT_LIST] = departmentList; if (collectionPts.Count == 0) { return; } // If the user is tagged to a collection point, add it into the location name string currentUser = User.Identity.Name; int currentCollectionPtId = collectionPts.First().collection_pt_id; // Get the department codes that are related to this value List <Department> departments = departmentList.Where(w => w.collection_point == currentCollectionPtId).ToList(); // Get all that can be disbursed given the current user DisbursementModelCollection disbursingList = FacadeFactory.getDisbursementService(context).getAllThatCanBeSignedOff(currentUser); //Session[SESSION_DISBURSING_LIST] = disbursingList; string currentDepartmentCode = departments.First().dept_code; 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, sm.CollectionPtId }) ).GroupBy(k => new { k.ItemCode, k.Description, DeptCode = k.dept_code }, v => v).ToList(); /* Filter active collection points */ List <int> activeCollectionPoints = itemGroups.SelectMany(sm => sm.Select(s => s.CollectionPtId)).Distinct().ToList(); foreach (var collectionPt in collectionPts) { if (collectionPt.username == currentUser) { collectionPt.location += " [Assigned to you]"; } if (!activeCollectionPoints.Contains(collectionPt.collection_pt_id)) { collectionPt.location += " (Empty)"; } } ddlCollectionPoints.DataSource = collectionPts; ddlCollectionPoints.DataValueField = "collection_pt_id"; ddlCollectionPoints.DataTextField = "location"; ddlCollectionPoints.DataBind(); List <ConfirmDisbursementViewModel> list = new List <ConfirmDisbursementViewModel>(); 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(); ConfirmDisbursementViewModel model = new ConfirmDisbursementViewModel(); model.ItemCode = itemGroup.Key.ItemCode; model.ItemDescription = itemGroup.Key.Description; model.DeptCode = itemGroup.Key.DeptCode; //model.DeptName = deptGroup.First().name; model.QuantityExpected = itemQty; model.QuantityActual = itemQty; model.RequestIds = reqIds; //model.Include = true; list.Add(model); } Session[SESSION_DISBURSING_LIST] = list; list = list.Where(w => w.DeptCode == currentDepartmentCode).ToList(); //_refreshGrid(list); lblNoDepartments.Visible = false; _refrehDepartmentsDropDown(departments); } }
protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { return; } panelNoItems.Visible = false; panelNormal.Visible = false; string forwardedDeptCode = Request.QueryString["dept"]; using (SSISEntities context = new SSISEntities()) { //List<Collection_Point> collectionPts = context.Collection_Point.Where(w => w.deleted != "Y").ToList(); List <Department> departmentList = context.Departments.Where(w => w.deleted != "Y").ToList(); List <Collection_Point> collectionPtList = context.Collection_Point.Where(w => w.deleted != "Y").ToList(); List <Dept_Registry> usersList = context.Dept_Registry.Where(w => w.deleted != "Y").ToList(); Session[SESSION_DEPARTMENT_LIST] = departmentList; Session[SESSION_COLLECTION_PT_LIST] = collectionPtList; Session[SESSION_USER_LIST] = usersList; string currentUser = User.Identity.Name; // Get all that can be disbursed given the current user DisbursementModelCollection disbursingList = FacadeFactory.getDisbursementService(context).getAllThatCanBeSignedOff(currentUser); //Session[SESSION_DISBURSING_LIST] = disbursingList; 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> filteredDisbursingList = new List <ConfirmDisbursementViewModel>(); foreach (var itemGroup in itemGroups) { int itemQty = itemGroup.Select(s => s.Quantity).Where(w => w > 0).Aggregate((a, b) => a + b); List <int> reqIds = itemGroup.Select(s => s.RequestId).ToList(); ConfirmDisbursementViewModel model = new ConfirmDisbursementViewModel(); model.ItemCode = itemGroup.Key.ItemCode; model.ItemDescription = itemGroup.Key.Description; model.DeptCode = itemGroup.Key.DeptCode; //model.DeptName = deptGroup.First().name; model.QuantityExpected = itemQty; model.QuantityActual = itemQty; model.RequestIds = reqIds; //model.Include = true; filteredDisbursingList.Add(model); } string currentDepartmentCode = departmentList.First().dept_code; if (forwardedDeptCode != null) { // Forwarded from View Generated forms currentDepartmentCode = forwardedDeptCode; } // Mark empty departments List <string> deptCodes = filteredDisbursingList.Select(s => s.DeptCode).ToList(); foreach (var dept in departmentList) { // If the dept does not have disbursements: if (!deptCodes.Contains(dept.dept_code)) { dept.name += " (empty)"; } } Session[SESSION_DISBURSING_LIST] = filteredDisbursingList; Session[SESSION_CURRENT_DEPT_CODE] = currentDepartmentCode; //filteredDisbursingList = filteredDisbursingList.Where(w => w.DeptCode == currentDepartmentCode).ToList(); _refreshDepartmentsDropDown(departmentList); _refreshGrid(filteredDisbursingList); ddlDepartments.SelectedValue = currentDepartmentCode; if (departmentList.Count == 0 || disbursingList.Count == 0) { panelNoItems.Visible = true; } else { panelNormal.Visible = true; } } }