public ActionResult ReceiveDO(int poId)
 {
     if (Session["existinguser"] != null)
     {
         LoginDTO currentUser = (LoginDTO)Session["existinguser"];
         if (currentUser.RoleId != (int)Enums.Roles.StoreClerk)
         {
             return(RedirectToAction("RedirectToClerkOrDepartmentView", "Login"));
         }
         ReceiveDoDTO receiveDO = new ReceiveDoDTO();
         receiveDO.purchaseOrder = PurchaseOrderService.Instance.getPurchaseOrderById(poId);
         if (receiveDO.purchaseOrder.PurchaseOrderDetails.Count > 0)
         {
             receiveDO.DOReceivedList = new int[receiveDO.purchaseOrder.PurchaseOrderDetails.Count];
             int i = 0;
             foreach (var item in receiveDO.purchaseOrder.PurchaseOrderDetails)
             {
                 receiveDO.DOReceivedList[i] = item.QuantityDelivered == null ? 0 : (int)item.QuantityDelivered;
                 i++;
             }
         }
         return(View(receiveDO));
     }
     return(RedirectToAction("Index", "Login"));
 }
        public ActionResult ReceiveDO(ReceiveDoDTO receiveDO)
        {
            if (Session["existinguser"] != null)
            {
                LoginDTO currentUser = (LoginDTO)Session["existinguser"];
                if (currentUser.RoleId != (int)Enums.Roles.StoreClerk)
                {
                    return(RedirectToAction("RedirectToClerkOrDepartmentView", "Login"));
                }
                if (ModelState.IsValid)
                {
                    if (receiveDO.DO.ContentLength > 10 * 1024 * 1024 || receiveDO.Invoice.ContentLength > 10 * 1024 * 1024)
                    {
                        receiveDO.Error.HasError = true;
                        receiveDO.Error.Message  = "Each attachment cannot be bigger than 10MB.";
                    }
                    else
                    {
                        PurchaseOrder receivedQtyDTO = new PurchaseOrder();
                        if (TempData["DOReceivedQty"] != null)
                        {
                            receivedQtyDTO = (PurchaseOrder)TempData["DOReceivedQty"];
                            TempData.Keep("DOReceivedQty");
                        }
                        else
                        {
                            receivedQtyDTO = PurchaseOrderService.Instance.getPurchaseOrderById(receiveDO.purchaseOrder.Id);
                        }
                        //save attachments
                        List <string> attachments = new List <string>();
                        var           filename    = "DO_" + receiveDO.purchaseOrder.Id + "_" + DateTime.Now.ToString("ddMMyyyy_hhmm") + Path.GetExtension(receiveDO.DO.FileName);
                        var           path        = Path.Combine(Server.MapPath("~/Images/DeliveryOrders/"), filename);

                        receiveDO.DO.SaveAs(path);
                        receiveDO.purchaseOrder.DO = filename;
                        receivedQtyDTO.DO          = filename;
                        attachments.Add(path);
                        filename = "Invoice_" + receiveDO.purchaseOrder.Id + "_" + DateTime.Now.ToString("ddMMyyyy_hhmm") + Path.GetExtension(receiveDO.Invoice.FileName);
                        path     = Path.Combine(Server.MapPath("~/Images/DeliveryOrders/"), filename);
                        receiveDO.Invoice.SaveAs(path);
                        receiveDO.purchaseOrder.Invoice = filename;
                        receivedQtyDTO.Invoice          = filename;
                        attachments.Add(path);
                        receivedQtyDTO.Status = Enum.GetName(typeof(Enums.POStatus), Enums.POStatus.CLOSED);

                        //save updatedPO
                        receivedQtyDTO.DeliveryDateTime = DateTime.Now;
                        receivedQtyDTO.DORemark         = receiveDO.purchaseOrder.DORemark;
                        receivedQtyDTO.DeliveryOrderNo  = receiveDO.purchaseOrder.DeliveryOrderNo;
                        PurchaseOrderService.Instance.UpdatePO(receivedQtyDTO);

                        foreach (var detail in receivedQtyDTO.PurchaseOrderDetails)
                        {
                            //update stationery qty
                            Stationery s = StationeryService.Instance.GetStationeryById(detail.StationeryId);
                            s.Quantity += detail.QuantityDelivered == null ? 0 : (int)detail.QuantityDelivered;
                            StationeryService.Instance.UpdateStationery(s);
                        }
                        //Check to move waitlistApproved to Preparing
                        RequisitionCatalogueService.Instance.CheckStockAndUpdateStatusForWaitlistApprovedRequisitionDetails(receivedQtyDTO.Id);
                        EmailNotificationService.Instance.SendNotificationEmail(receipient: "*****@*****.**", subject: "(Stationery Store) Delivery Order and Invoice for " + DateTime.Now.ToString("dd/MM/yyyy"), body: "Dear Bursar Department,\n\nDelivery Order and Invoices for Delivery Orders of Stationery Store are attached.", attachments: attachments.AsEnumerable());
                        TempData["DOReceivedQty"] = null;
                        return(RedirectToAction("Index"));
                    }
                }
                PurchaseOrder po = PurchaseOrderService.Instance.getPurchaseOrderById(receiveDO.purchaseOrder.Id);
                po.Remark = receiveDO.purchaseOrder.Remark;
                po.DO     = receiveDO.purchaseOrder.DO;
                receiveDO.purchaseOrder = po;
                return(View(receiveDO));
            }
            return(RedirectToAction("Index", "Login"));
        }