コード例 #1
0
        public ActionResult SetDisbursementList(int id, string status, string deptId)
        {
            string[] productIds    = Request.Form.GetValues("productId");
            string[] requestedQtys = Request.Form.GetValues("requestedQty");
            string[] receivedQtys  = Request.Form.GetValues("receivedQty");
            string[] remarks       = Request.Form.GetValues("remarks");
            int      storeStaffId  = Convert.ToInt32(Request.Form.Get("creatorId"));

            //Update the item quantities && Set Disburesment status to delivered
            disbursementDAO.SaveDisbursement(id, status, receivedQtys, remarks, storeStaffId);
            DepartmentStaff rep = departmentStaffDAO.getDeptRep(deptId);

            //Set all the orders status to Delivered. Not DAO-ed
            using (db = new LogicEntities())
            {
                var resultSet = from r in db.Request
                                where r.DeptId == deptId
                                select r;
                List <Request> requests = resultSet.ToList();
                foreach (Request req in requests)
                {
                    req.Status          = "Delivered";
                    db.Entry(req).State = EntityState.Modified;
                    db.SaveChanges();
                }
                //if Qty received < Qty Requested, Generate outstanding order in requests

                Request outstanding = new Request
                {
                    DeptId  = deptId,
                    ReqDate = DateTime.Now,
                    Remark  = "Outstanding Order, " + DateTime.Now.Date.ToString("d"),
                    Status  = "Outstanding",
                    StaffId = rep.StaffId
                };
                List <RequestDetail> outstandingDetails = new List <RequestDetail>();
                for (int x = 0; x < requestedQtys.Length; x++)
                {
                    if (requestedQtys[x] != receivedQtys[x])
                    {
                        RequestDetail rd = new RequestDetail
                        {
                            ProductId   = productIds[x],
                            RequestId   = outstanding.RequestId,
                            ReqQty      = Convert.ToInt32(requestedQtys[x]) - Convert.ToInt32(receivedQtys[x]),
                            ReceivedQty = 0, //Shouldn't be needed...
                        };
                        outstandingDetails.Add(rd);
                    }
                }
                if (outstandingDetails.Count != 0)
                {
                    db.Request.Add(outstanding);
                    db.RequestDetail.AddRange(outstandingDetails);
                    db.SaveChanges();
                }
            }
            return(RedirectToAction("FindDisbursementList"));
        }