public ActionResult Create([Bind(Include = "PurchaseOrderOID,PurchaseRequisitionID,CheckedResultSetVM")] PurchaseOrderCreateViewModel model)
        {
            if (model == null || model.CheckedResultSetVM.Count(s => s.Checked) == 0)
            {
                TempData["ErrorMessage"] = "採購細項請至少勾選一項";
                return(RedirectToAction("Create"));
            }

            //從暫存新增至正式資料表
            DateTime now = DateTime.Now;

            using (PMSAEntities db = new PMSAEntities())
            {
                //新增採購單
                PurchaseOrderTemp pot = db.PurchaseOrderTemp.Find(model.PurchaseOrderOID);
                db.Entry(pot).State = EntityState.Detached;
                string poId  = $"PO-{now:yyyyMMdd}-";
                int    count = db.PurchaseOrder.Where(i => i.PurchaseOrderID.StartsWith(poId)).Count();
                count++;
                poId = $"{poId}{count:000}";
                PurchaseOrder po = new PurchaseOrder
                {
                    PurchaseOrderID     = poId,
                    SupplierCode        = pot.SupplierCode,
                    EmployeeID          = pot.EmployeeID,
                    CreateDate          = now,
                    PurchaseOrderStatus = "N"
                };
                db.PurchaseOrder.Add(po);
                db.SaveChanges();
                //新增採購單明細
                int index = 0;
                foreach (var item in model.CheckedResultSetVM)
                {
                    if (!item.Checked)
                    {
                        continue;
                    }
                    var podt = db.PurchaseOrderDtlTemp.Find(item.PurchaseOrderDtlOID);
                    index++;
                    PurchaseOrderDtl pod = new PurchaseOrderDtl
                    {
                        PurchaseOrderDtlCode = $"{poId}-{index:000}",
                        PurchaseOrderID      = poId,
                        PartNumber           = podt.PartNumber,
                        PartName             = podt.PartName,
                        PartSpec             = podt.PartSpec,
                        QtyPerUnit           = podt.QtyPerUnit,
                        TotalPartQty         = podt.TotalPartQty,
                        OriginalUnitPrice    = podt.OriginalUnitPrice,
                        Discount             = podt.Discount,
                        PurchaseUnitPrice    = podt.PurchaseUnitPrice,
                        Qty                  = podt.Qty,
                        PurchasedQty         = podt.PurchasedQty,
                        GoodsInTransitQty    = podt.GoodsInTransitQty,
                        Total                = podt.Total,
                        DateRequired         = podt.DateRequired,
                        CommittedArrivalDate = podt.CommittedArrivalDate,
                        ShipDate             = podt.ShipDate,
                        ArrivedDate          = podt.ArrivedDate,
                        SourceListID         = podt.SourceListID
                    };
                    db.PurchaseOrderDtl.Add(pod);
                    db.SaveChanges();
                    //請購單與採購單關聯
                    //TODO: 應從暫存取出,目前暫以傳入方式處理
                    PRPORelation rel = new PRPORelation
                    {
                        PurchaseOrderID            = poId,
                        PurchaseOrderDtlCode       = pod.PurchaseOrderDtlCode,
                        PurchaseRequisitionID      = model.PurchaseRequisitionID,
                        PurchaseRequisitionDtlCode = item.PurchaseRequisitionDtlCode,
                    };
                    db.PRPORelation.Add(rel);
                    db.SaveChanges();
                    //採購單異動總表
                    POChanged poc = new POChanged
                    {
                        PurchaseOrderID       = poId,
                        POChangedCategoryCode = "N",
                        RequestDate           = now,
                        RequesterRole         = "P",
                        RequesterID           = pot.EmployeeID
                    };
                    db.POChanged.Add(poc);
                    db.SaveChanges();
                    //更新PurchaseOrderDtl.POChangedOID
                    pod.POChangedOID = poc.POChangedOID;
                    db.Entry(pod).Property(podp => podp.POChangedOID).IsModified = true;
                    db.SaveChanges();
                }
                //刪除暫存資料
                var PRPORelationTemps = db.PRPORelationTemp.Where(i => i.PurchaseOrderOID == model.PurchaseOrderOID);
                db.PRPORelationTemp.RemoveRange(PRPORelationTemps);
                db.SaveChanges();
                var PurchaseOrderDtlTemps = db.PurchaseOrderDtlTemp.Where(i => i.PurchaseOrderOID == model.PurchaseOrderOID);
                db.PurchaseOrderDtlTemp.RemoveRange(PurchaseOrderDtlTemps);
                var PurchaseOrderOld = db.PurchaseOrderTemp.Find(model.PurchaseOrderOID);
                db.PurchaseOrderTemp.Remove(PurchaseOrderOld);
            }

            return(RedirectToAction("Index"));
        }
示例#2
0
        public ActionResult shipCheckDtl(UnshipOrderDtlViewModel unshipOrderDtl)
        {
            //建立一個LIST用來接住所有的OrderDtlItemChecked
            IList <OrderDtlItemChecked> OrderDtlChecked = unshipOrderDtl.orderDtlItemCheckeds;
            //用來存放確定有要出貨的LIST(有勾選)
            List <PurchaseOrderDtl> orderDtls = new List <PurchaseOrderDtl>();

            //檢查是否有勾選出貨,true為有勾,有則放進orderDtls
            foreach (var dtl in OrderDtlChecked)
            {
                if (dtl.Checked)
                {
                    PurchaseOrderDtl purchaseOrderDtl = db.PurchaseOrderDtl.Find(dtl.PurchaseOrderDtlCode);
                    orderDtls.Add(purchaseOrderDtl);
                }
            }
            DateTime          now         = DateTime.Now;
            List <SourceList> sourceLists = new List <SourceList>();

            //檢查庫存是否足夠,不足則顯示庫存不足的訊息,足夠則扣掉該或源清單庫存
            //並新增該採購單明細實際出貨日期,新增出貨明細//
            foreach (var dtl in orderDtls)
            {
                SourceList sourceList = db.SourceList.Find(dtl.SourceListID);
                if (sourceList.UnitsInStock < dtl.Qty)
                {
                    //這裡要return 錯誤訊息,並且回到原頁面
                    TempData["message"] = "<script>Swal.fire({  icon: 'error',  title: 'Oops...',  text: '庫存不足!',  footer: '<a href>Why do I have this issue?</a>'})</script>";
                    return(RedirectToAction("UnshipOrderDtl", "ShipNotices", new { PurchaseOrderID = unshipOrderDtl.PurchaseOrderID }));
                }
                //扣除該料件貨源清單的庫存以及訂單數量
                sourceList.UnitsInStock = sourceList.UnitsInStock - dtl.Qty;
                if (sourceList.UnitsOnOrder < dtl.Qty)
                {
                    sourceList.UnitsOnOrder = 0;
                }
                else
                {
                    sourceList.UnitsOnOrder = sourceList.UnitsOnOrder - dtl.Qty;
                }
                sourceLists.Add(sourceList);
                dtl.ShipDate = now;
                //新增出貨通知 應該在這 先檢查是否有該筆出貨通知(因為有可能分開出貨,所以同筆訂單後出貨的就不用在增加出貨通知,只要增加出貨明細即可)
                if (db.ShipNotice.Where(x => x.PurchaseOrderID == unshipOrderDtl.PurchaseOrderID).FirstOrDefault() == null)
                {
                    //新增出貨通知//感覺應該要在外面再加一個迴圈做出貨通知以及出貨明細
                    ShipNotice shipNotice = new ShipNotice();
                    string     snId       = $"SN-{now:yyyyMMdd}-";
                    int        count      = db.ShipNotice.Where(x => x.ShipNoticeID.StartsWith(snId)).Count();
                    count++;
                    snId = $"{snId}{count:000}";
                    shipNotice.ShipNoticeID      = snId;
                    shipNotice.PurchaseOrderID   = unshipOrderDtl.PurchaseOrderID;
                    shipNotice.ShipDate          = now;
                    shipNotice.EmployeeID        = db.PurchaseOrder.Find(unshipOrderDtl.PurchaseOrderID).EmployeeID;
                    shipNotice.CompanyCode       = db.Employee.Find(shipNotice.EmployeeID).CompanyCode;
                    shipNotice.SupplierAccountID = supplierAccount;
                    db.ShipNotice.Add(shipNotice);
                    //先把新增的出貨通知資料存進資料庫
                    db.SaveChanges();
                }
                //新增出貨明細 保存期限先不填
                ShipNoticeDtl shipNoticeDtl = new ShipNoticeDtl();
                shipNoticeDtl.ShipNoticeID         = db.ShipNotice.Where(x => x.PurchaseOrderID == unshipOrderDtl.PurchaseOrderID).FirstOrDefault().ShipNoticeID;
                shipNoticeDtl.PurchaseOrderDtlCode = dtl.PurchaseOrderDtlCode;
                shipNoticeDtl.ShipQty = dtl.Qty;
                //金額為數量*單價*折扣*批量
                shipNoticeDtl.ShipAmount = Convert.ToInt32(dtl.Qty * dtl.PurchaseUnitPrice * (1 - dtl.Discount) * dtl.QtyPerUnit);
                //把新出貨明細資料加進資料庫
                db.ShipNoticeDtl.Add(shipNoticeDtl);
                //把資料庫中的每筆訂單以及貨源清單資料狀態改為追蹤
                db.Entry(dtl).State        = EntityState.Modified;
                db.Entry(sourceList).State = EntityState.Modified;
            }
            //存進資料庫
            db.SaveChanges();
            //檢查該筆訂單所有產品是否都已經出貨,如果是,將該筆採購單狀態改為已出貨"S"
            //預設先當作都已出貨
            bool poCheck = true;
            var  q       = from pod in db.PurchaseOrderDtl
                           where pod.PurchaseOrderID == unshipOrderDtl.PurchaseOrderID
                           select pod;

            foreach (var pod in q)
            {
                if (pod.ShipDate == null)
                {
                    //找到未出貨產品,代表尚未全部出貨
                    poCheck = false;
                }
            }
            //確認是否已全部出貨,如果是修改採購單狀態為已出貨(S)並新增一筆採購單異動資料
            //採購單明細的POChangedOID欄位也要更新
            if (poCheck)
            {
                //改採購單狀態
                db.PurchaseOrder.Find(unshipOrderDtl.PurchaseOrderID).PurchaseOrderStatus = POChangedCategoryCodeShipped;
                //新增採購單異動總表
                POChanged pOChanged = new POChanged();
                pOChanged.PurchaseOrderID       = unshipOrderDtl.PurchaseOrderID;
                pOChanged.POChangedCategoryCode = POChangedCategoryCodeShipped;
                pOChanged.RequestDate           = now;
                pOChanged.RequesterRole         = RequesterRoleSupplier;
                pOChanged.RequesterID           = supplierAccount;
                db.POChanged.Add(pOChanged);
                db.SaveChanges();
                //更新採購單明細POChangedOID欄位
                //2019 11/20 23:06 下方註解掉的程式碼不知道為甚麼會發生無法辨識此方法,無法放入站存區,所以只好分開來寫
                //int a = db.POChanged.Last(x => (x.RequesterRole == RequesterRoleSupplier) && (x.PurchaseOrderID == unshipOrderDtl.PurchaseOrderID)).POChangedOID;

                //先找出最新一筆採購單異動資料且是供應商的
                var      poc          = db.POChanged.Where(x => (x.RequesterRole == RequesterRoleSupplier) && (x.PurchaseOrderID == unshipOrderDtl.PurchaseOrderID));
                DateTime dt           = poc.FirstOrDefault().RequestDate;
                int      pOChangedOID = poc.FirstOrDefault().POChangedOID;
                foreach (var pocD in poc)
                {
                    if (pocD.RequestDate > dt)
                    {
                        dt           = pocD.RequestDate;
                        pOChangedOID = pocD.POChangedOID;
                    }
                }
                //然後把找出來的採購單異動總表最新的POChangedOID更新至採購單明細POChangedOID欄位中
                var podQueryForPOChangedOID = from pod in db.PurchaseOrderDtl
                                              where pod.PurchaseOrderID == unshipOrderDtl.PurchaseOrderID
                                              select pod;
                foreach (var pod in podQueryForPOChangedOID)
                {
                    pod.POChangedOID    = pOChangedOID;
                    db.Entry(pod).State = EntityState.Modified;
                }
                db.SaveChanges();
                TempData["message"] = "<script>Swal.fire({position: 'top-end',icon: 'success',title: '出貨處理成功,庫存已扣除',showConfirmButton: false,timer: 1500})</script>";
            }
            //成功回原頁面
            TempData["message"] = "<script>Swal.fire({position: 'top-end',icon: 'success',title: '已全部出貨',showConfirmButton: false,timer: 1500})</script>";
            return(RedirectToAction("UnshipOrderDtl", "ShipNotices", new { PurchaseOrderID = unshipOrderDtl.PurchaseOrderID }));
        }
示例#3
0
        public async Task <ActionResult> shipCheckDtl(shipOrderViewModel unshipOrderDtl)
        {
            string shipnoticesid = "";//為了進貨單而設立的變數
            ////////////////////////////////////////////////
            //取得供應商帳號資料
            SupplierAccount supplier = User.Identity.GetSupplierAccount();

            supplierAccount = supplier.SupplierAccountID;
            supplierCode    = supplier.SupplierCode;
            ////////////////////////////////////////////////////
            //ShipNoticesUtilities utilities = new ShipNoticesUtilities();
            string message = "";
            //此LIST要用來存放出貨明細ID 用來寄送電子郵件給公司採購員
            List <string> shipDtlList    = new List <string>();
            List <int>    shipDtlListQty = new List <int>();
            string        shipNoticeID   = "";
            //建立一個LIST用來接住所有的OrderDtlItemChecked
            IList <OrderDtlItemChecked> OrderDtlChecked = unshipOrderDtl.orderDtlItemCheckeds;
            //用來存放確定有要出貨的LIST(有勾選)
            List <PurchaseOrderDtl> orderDtls = new List <PurchaseOrderDtl>();

            //檢查是否有勾選出貨,true為有勾,有則放進orderDtls
            foreach (var dtl in OrderDtlChecked)
            {
                if (dtl.Checked)
                {
                    PurchaseOrderDtl purchaseOrderDtl = db.PurchaseOrderDtl.Find(dtl.PurchaseOrderDtlCode);
                    orderDtls.Add(purchaseOrderDtl);
                }
            }
            //檢查是否至少一個被勾選,如沒有則跳回去UnshipOrderDtl頁面
            if (orderDtls.Count() == 0)
            {
                TempData["message"] = "<script>toastr.error('請選擇欲出貨商品!','通知')</script>";
                message             = "請選擇欲出貨商品!";
                return(RedirectToAction("UnshipOrderDtl", "ShipNotices", new { PurchaseOrderID = unshipOrderDtl.PurchaseOrderID, message = message }));
            }
            DateTime now = DateTime.Now;

            //檢查庫存是否足夠,不足則顯示庫存不足的訊息,足夠則扣掉該或源清單庫存
            //並新增該採購單明細實際出貨日期,新增出貨明細//
            foreach (var dtl in orderDtls)
            {
                SourceList sourceList = db.SourceList.Find(dtl.SourceListID);
                if (sourceList.UnitsInStock < unshipOrderDtl.orderDtlItemCheckeds.Where(x => x.PurchaseOrderDtlCode == dtl.PurchaseOrderDtlCode).FirstOrDefault().Qty)
                {
                    //這裡要return 錯誤訊息,並且回到原頁面
                    TempData["message"] = "<script>Swal.fire({  icon: 'error',  title: 'Oops...',  text: '庫存不足!',  footer: '<a href>Why do I have this issue?</a>'})</script>";
                    message             = "庫存不足!";
                    // return Json(new { PurchaseOrderID = unshipOrderDtl.PurchaseOrderID, message = message }, JsonRequestBehavior.AllowGet);
                    return(RedirectToAction("UnshipOrderDtl", "ShipNotices", new { PurchaseOrderID = unshipOrderDtl.PurchaseOrderID, message = message }));
                }
                //扣除該料件貨源清單的庫存以及訂單數量
                //出貨數量要在這裡檢查,先檢查出貨明細裡面的shipQty比對是否小於同一個採購單明細的Qty,
                //是的話,扣除該料件貨源清單的庫存以及訂單數量並且更新shipQty
                if (db.ShipNoticeDtl.Where(x => x.PurchaseOrderDtlCode == dtl.PurchaseOrderDtlCode).FirstOrDefault() != null)
                {
                    ShipNoticeDtl snd      = db.ShipNoticeDtl.Where(x => x.PurchaseOrderDtlCode == dtl.PurchaseOrderDtlCode).FirstOrDefault();
                    int           orderQty = dtl.Qty;
                    if (orderQty > snd.ShipQty || (unshipOrderDtl.orderDtlItemCheckeds.Where(x => x.PurchaseOrderDtlCode == dtl.PurchaseOrderDtlCode).FirstOrDefault().Qty + snd.ShipQty) < orderQty)
                    {
                        sourceList.UnitsInStock = sourceList.UnitsInStock - unshipOrderDtl.orderDtlItemCheckeds.Where(x => x.PurchaseOrderDtlCode == dtl.PurchaseOrderDtlCode).FirstOrDefault().Qty;
                    }
                }
                else
                {
                    sourceList.UnitsInStock = sourceList.UnitsInStock - unshipOrderDtl.orderDtlItemCheckeds.Where(x => x.PurchaseOrderDtlCode == dtl.PurchaseOrderDtlCode).FirstOrDefault().Qty;
                    if (sourceList.UnitsOnOrder < dtl.Qty)
                    {
                        sourceList.UnitsOnOrder = 0;
                    }
                    else
                    {
                        sourceList.UnitsOnOrder = sourceList.UnitsOnOrder - dtl.Qty;
                    }
                }
                //新增出貨通知 應該在這 先檢查是否有該筆出貨通知(因為有可能分開出貨,所以同筆訂單後出貨的就不用在增加出貨通知,只要增加出貨明細即可)
                if (db.ShipNotice.Where(x => x.PurchaseOrderID == unshipOrderDtl.PurchaseOrderID).FirstOrDefault() == null)
                {
                    //新增出貨通知//感覺應該要在外面再加一個迴圈做出貨通知以及出貨明細
                    ShipNotice shipNotice = new ShipNotice();
                    string     snId       = $"SN-{now:yyyyMMdd}-";
                    int        count      = db.ShipNotice.Where(x => x.ShipNoticeID.StartsWith(snId)).Count();
                    count++;
                    snId = $"{snId}{count:000}";
                    shipNotice.ShipNoticeID      = snId;
                    shipnoticesid                = snId; //將出貨ID存入變數中
                    shipNotice.PurchaseOrderID   = unshipOrderDtl.PurchaseOrderID;
                    shipNotice.ShipDate          = now;
                    shipNotice.EmployeeID        = db.PurchaseOrder.Find(unshipOrderDtl.PurchaseOrderID).EmployeeID;
                    shipNotice.CompanyCode       = db.Employee.Find(shipNotice.EmployeeID).CompanyCode;
                    shipNotice.SupplierAccountID = supplierAccount;
                    db.ShipNotice.Add(shipNotice);
                    //先把新增的出貨通知資料存進資料庫
                    db.SaveChanges();
                }
                //檢查是否有該出貨明細,沒有則新增出貨明細
                if (db.ShipNoticeDtl.Where(x => x.PurchaseOrderDtlCode == dtl.PurchaseOrderDtlCode).FirstOrDefault() == null)
                {
                    //新增出貨明細 保存期限先不填
                    ShipNoticeDtl shipNoticeDtl = new ShipNoticeDtl();
                    shipNoticeDtl.ShipNoticeID = db.ShipNotice.Where(x => x.PurchaseOrderID == unshipOrderDtl.PurchaseOrderID).FirstOrDefault().ShipNoticeID;
                    shipnoticesid = shipNoticeDtl.ShipNoticeID; //將出貨ID存入變數中
                    shipNoticeDtl.PurchaseOrderDtlCode = dtl.PurchaseOrderDtlCode;
                    shipNoticeDtl.ShipQty = unshipOrderDtl.orderDtlItemCheckeds.Where(x => x.PurchaseOrderDtlCode == dtl.PurchaseOrderDtlCode).FirstOrDefault().Qty;
                    //金額為數量*單價*折扣*批量
                    shipNoticeDtl.ShipAmount = Convert.ToInt32(shipNoticeDtl.ShipQty * dtl.PurchaseUnitPrice * (1 - dtl.Discount) * dtl.QtyPerUnit);
                    //把新出貨明細資料加進資料庫
                    db.ShipNoticeDtl.Add(shipNoticeDtl);
                    //存進出貨明細OID給寄送電子郵件用,改成存採購單編號CODE,因為OID會有新增先後順序的問題
                    shipDtlList.Add(dtl.PurchaseOrderDtlCode);
                    shipDtlListQty.Add(unshipOrderDtl.orderDtlItemCheckeds.Where(x => x.PurchaseOrderDtlCode == dtl.PurchaseOrderDtlCode).FirstOrDefault().Qty);
                }
                //有的話,則去修改出貨明細表的出貨數量和出貨金額
                else
                {
                    ShipNoticeDtl snd = db.ShipNoticeDtl.Where(x => x.PurchaseOrderDtlCode == dtl.PurchaseOrderDtlCode).FirstOrDefault();
                    snd.ShipQty        += unshipOrderDtl.orderDtlItemCheckeds.Where(x => x.PurchaseOrderDtlCode == dtl.PurchaseOrderDtlCode).FirstOrDefault().Qty;
                    snd.ShipAmount      = Convert.ToInt32(snd.ShipQty * dtl.PurchaseUnitPrice * (1 - dtl.Discount) * dtl.QtyPerUnit);
                    db.Entry(snd).State = EntityState.Modified;
                    //存進出貨明細OID給寄送電子郵件用,改成存採購單編號CODE,因為OID會有新增先後順序的問題
                    shipDtlList.Add(dtl.PurchaseOrderDtlCode);
                    shipDtlListQty.Add(unshipOrderDtl.orderDtlItemCheckeds.Where(x => x.PurchaseOrderDtlCode == dtl.PurchaseOrderDtlCode).FirstOrDefault().Qty);
                }
                //不管是採購單明細或是採購單有異動都要新增採購單異動總表
                //新增採購單異動總表(明細)
                POChanged pOChanged = new POChanged();
                pOChanged.PurchaseOrderID       = unshipOrderDtl.PurchaseOrderID;
                pOChanged.POChangedCategoryCode = POChangedCategoryCodeShipped;
                pOChanged.RequestDate           = now;
                pOChanged.DateRequired          = dtl.DateRequired;
                pOChanged.RequesterRole         = RequesterRoleSupplier;
                pOChanged.RequesterID           = supplierAccount;
                pOChanged.PurchaseOrderDtlCode  = dtl.PurchaseOrderDtlCode;
                pOChanged.Qty = db.PurchaseOrderDtl.Find(dtl.PurchaseOrderDtlCode).Qty;
                db.POChanged.Add(pOChanged);
                db.SaveChanges();
                //新增採購單明細出貨日期欄位以及POchangedOID欄位
                dtl.ShipDate = now;
                //更新採購單明細POChangedOID欄位
                //找出最新一筆採購單異動資料且是供應商的
                dtl.POChangedOID = utilities.FindPOChangedOIDByDtlCode(RequesterRoleSupplier, dtl.PurchaseOrderDtlCode);
                //把資料庫中的每筆訂單明細以及貨源清單資料狀態改為追蹤
                db.Entry(dtl).State        = EntityState.Modified;
                db.Entry(sourceList).State = EntityState.Modified;
            }
            //存進資料庫
            db.SaveChanges();
            //檢查該筆訂單所有產品是否都已經出貨,如果是,將該筆採購單狀態改為已出貨"S"
            //預設先當作都已出貨
            bool poCheck = true;
            var  q       = from pod in db.PurchaseOrderDtl
                           where pod.PurchaseOrderID == unshipOrderDtl.PurchaseOrderID
                           select pod;

            foreach (var pod in q)
            {
                if (pod.ShipDate == null)
                {
                    //找到未出貨產品,代表尚未全部出貨
                    poCheck = false;
                }
                else
                {
                    //如果有出貨過,檢查出貨數量是否跟採購單採購數量一致
                    ShipNoticeDtl snd = db.ShipNoticeDtl.Where(x => x.PurchaseOrderDtlCode == pod.PurchaseOrderDtlCode).SingleOrDefault();
                    if (snd != null && pod.Qty > snd.ShipQty)
                    {
                        poCheck = false;
                    }
                }
            }
            //確認是否已全部出貨,如果是,修改採購單狀態為已出貨(S)並新增一筆採購單異動資料
            //採購單明細的POChangedOID欄位也要更新
            if (poCheck)
            {
                //改採購單狀態
                db.PurchaseOrder.Find(unshipOrderDtl.PurchaseOrderID).PurchaseOrderStatus = POChangedCategoryCodeShipped;
                //新增採購單異動總表
                POChanged pOChanged = new POChanged();
                pOChanged.PurchaseOrderID       = unshipOrderDtl.PurchaseOrderID;
                pOChanged.POChangedCategoryCode = POChangedCategoryCodeShipped;
                pOChanged.RequestDate           = now;
                pOChanged.RequesterRole         = RequesterRoleSupplier;
                pOChanged.RequesterID           = supplierAccount;
                db.POChanged.Add(pOChanged);
                db.SaveChanges();
                //然後把找出來的採購單異動總表最新的POChangedOID更新至採購單明細POChangedOID欄位中
                var podQueryForPOChangedOID = from pod in db.PurchaseOrderDtl
                                              where pod.PurchaseOrderID == unshipOrderDtl.PurchaseOrderID
                                              select pod;
                int pOChangedOID = utilities.FindPOChangedOID(RequesterRoleSupplier, unshipOrderDtl.PurchaseOrderID);
                foreach (var pod in podQueryForPOChangedOID)
                {
                    pod.POChangedOID    = pOChangedOID;
                    db.Entry(pod).State = EntityState.Modified;
                }
                db.SaveChanges();
                // TempData["message"] = "<script>Swal.fire({position: 'top-end',icon: 'success',title: ' 已全部出貨',showConfirmButton: false,timer: 1500})</script>";
                message = "已全部出貨";
            }
            //成功回原頁面
            //TempData["message"] = "<script>Swal.fire({position: 'top-end',icon: 'success',title: '出貨處理成功,庫存已扣除',showConfirmButton: false,timer: 1500})</script>";
            TempData["message"] = "出貨處理成功,庫存已扣除";
            if (message == "")
            {
                message = "出貨處理成功,庫存已扣除";
            }
            List <OrderDtlForMail> odm = orderDtlForMails(shipDtlList, shipDtlListQty);

            await SendMailToBuyer(odm);

            //呼叫新增進貨單方法
            PurchaseOrderReceivesController purchaseOrderReceivesController = new PurchaseOrderReceivesController();

            purchaseOrderReceivesController.Create(shipnoticesid);

            //return Json(new { PurchaseOrderID = unshipOrderDtl.PurchaseOrderID, message = message },JsonRequestBehavior.AllowGet);
            return(RedirectToAction("Index", "ShipNotices", new { PurchaseOrderID = unshipOrderDtl.PurchaseOrderID, message = message }));
        }
示例#4
0
        public ActionResult shipChecked(string id)
        {
            string purchaseOrderID = id;

            if (purchaseOrderID == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            //修改貨源清單庫存數量
            var podquery = from pod in db.PurchaseOrderDtl
                           join sl in db.SourceList on pod.SourceListID equals sl.SourceListID
                           where pod.PurchaseOrderID == purchaseOrderID
                           select new { pod.Qty, sl.UnitsInStock, pod.SourceListID, pod.PurchaseOrderID, pod.PurchaseOrderDtlCode, sl.UnitsOnOrder };

            List <string> sourceListsTemp      = new List <string>();
            List <string> purchaseOrderDtlTemp = new List <string>();

            foreach (var x in podquery)
            {
                if (x.UnitsInStock >= x.Qty)
                {
                    //將貨源清單庫存數量以及訂單數量扣除該採購單料件請購數量並存回資料庫
                    sourceListsTemp.Add(x.SourceListID);
                    purchaseOrderDtlTemp.Add(x.PurchaseOrderDtlCode);

                    //SourceList sourceList = db.SourceList.Find(x.SourceListID);
                    //PurchaseOrderDtl purchaseOrderDtl = db.PurchaseOrderDtl.Find(x.PurchaseOrderDtlCode);
                    //sourceList.UnitsInStock = sourceList.UnitsInStock - purchaseOrderDtl.Qty;
                    //sourceList.UnitsOnOrder = sourceList.UnitsOnOrder - purchaseOrderDtl.Qty;
                    //db.Entry(sourceList).State = EntityState.Modified;
                }
                else
                {
                    return(Json("<script>Swal.fire('庫存不足')</script>", JsonRequestBehavior.AllowGet));
                }
            }
            for (int i = 0; i < sourceListsTemp.Count(); i++)
            {
                SourceList       sourceList       = db.SourceList.Find(sourceListsTemp[i]);
                PurchaseOrderDtl purchaseOrderDtl = db.PurchaseOrderDtl.Find(purchaseOrderDtlTemp[i]);
                sourceList.UnitsInStock = sourceList.UnitsInStock - purchaseOrderDtl.Qty;
                sourceList.UnitsOnOrder = sourceList.UnitsOnOrder - purchaseOrderDtl.Qty;
                //如果訂單書量小於零,則讓他為零
                if (sourceList.UnitsOnOrder < 0)
                {
                    sourceList.UnitsOnOrder = 0;
                }
                db.Entry(sourceList).State = EntityState.Modified;
            }
            //修改採購單狀態
            PurchaseOrder purchaseOrder = db.PurchaseOrder.Find(purchaseOrderID);

            purchaseOrder.PurchaseOrderStatus = "S";
            db.Entry(purchaseOrder).State     = EntityState.Modified;
            //新增出貨通知
            DateTime   now        = DateTime.Now;
            ShipNotice shipNotice = new ShipNotice();
            string     snId       = $"SN-{now:yyyyMMdd}-";
            int        count      = db.ShipNotice.Where(x => x.ShipNoticeID.StartsWith(snId)).Count();

            count++;
            snId = $"{snId}{count:000}";
            shipNotice.ShipNoticeID    = snId;
            shipNotice.PurchaseOrderID = purchaseOrderID;
            shipNotice.ShipDate        = now;
            shipNotice.EmployeeID      = purchaseOrder.EmployeeID;
            var compCode = db.Employee.Where(x => x.EmployeeID == purchaseOrder.EmployeeID).First();

            shipNotice.CompanyCode       = compCode.CompanyCode;
            shipNotice.SupplierAccountID = supplierAccount;
            //檢查出貨通知表裡面是否有該訂單ID,如果有,顯示該筆訂單已出貨
            ShipNotice ship = db.ShipNotice.Where(x => x.PurchaseOrderID == purchaseOrderID).FirstOrDefault();

            if (ship != null)
            {
                return(Json("<script>Swal.fire('此筆訂單已出貨')</script>", JsonRequestBehavior.AllowGet));
            }
            //存進資料庫
            purchaseOrder.ShipNotice.Add(shipNotice);
            db.SaveChanges();
            return(Json("<script>Swal.fire('出貨成功')</script>", JsonRequestBehavior.AllowGet));
        }