//此方法為幫助INDEX的DATATABLE查訂單資料 public JsonResult GetPurchaseOrderList(string PurchaseOrderStatus) { //////////////////////////////////////////////// //取得供應商帳號資料 SupplierAccount supplier = User.Identity.GetSupplierAccount(); supplierAccount = supplier.SupplierAccountID; supplierCode = supplier.SupplierCode; //////////////////////////////////////////////////// ShipNoticesUtilities utilities = new ShipNoticesUtilities(); string status = PurchaseOrderStatus; var query = from po in db.PurchaseOrder.AsEnumerable() where (po.PurchaseOrderStatus == status && po.SupplierCode == supplierCode) select new shipOrderViewModel { PurchaseOrderStatus = po.PurchaseOrderStatus, PurchaseOrderID = po.PurchaseOrderID, ReceiverName = po.ReceiverName, ReceiverTel = po.ReceiverTel, ReceiverMobile = po.ReceiverMobile, ReceiptAddress = po.ReceiptAddress }; List <shipOrderViewModel> qlist = query.ToList(); for (int i = 0; i < qlist.Count(); i++) { //ShipNoticesUtilities utilities = new ShipNoticesUtilities(); string d = utilities.GetStatus(qlist[i].PurchaseOrderStatus); qlist[i].PurchaseOrderStatusDisplay = d; } var s = query.ToList(); return(Json(new { data = qlist }, JsonRequestBehavior.AllowGet)); }
public Task <SupplierAccountDto> Update(SupplierAccountDto supplierAccountDto) { SupplierAccount supplierAccount = ObjectMapper.Map <SupplierAccount>(supplierAccountDto); supplierAccount = _supplierAccountRepository.Update(supplierAccount); return(Task.FromResult <SupplierAccountDto>(supplierAccountDto)); }
public ActionResult GetStockData() { //取得供應商帳號資料 SupplierAccount supplier = User.Identity.GetSupplierAccount(); supplierAccount = supplier.SupplierAccountID; supplierCode = supplier.SupplierCode; //////////////////////////////////////////////////// var q = from sl in db.SourceList join pt in db.Part on sl.PartNumber equals pt.PartNumber where sl.SupplierCode == supplierCode /*&& (sl.UnitsInStock <= sl.SafetyQty)*/ select new { sl.PartNumber, sl.SafetyQty, sl.UnitsInStock, sl.UnitsOnOrder, pt.PartName }; var s = q.ToList(); return(Json(s, JsonRequestBehavior.AllowGet)); }
public ActionResult ShipQtyByStatus() { //取得供應商帳號資料 SupplierAccount supplier = User.Identity.GetSupplierAccount(); supplierAccount = supplier.SupplierAccountID; supplierCode = supplier.SupplierCode; //////////////////////////////////////////////////// using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString)) { connection.Open(); using (SqlCommand cmd = new SqlCommand(@"SELECT pod.PartName, pod.PartNumber, pod.Qty, sl.UnitsOnOrder, snd.ShipQty FROM PurchaseOrderDtl AS pod LEFT OUTER JOIN PurchaseOrder AS po ON pod.PurchaseOrderID = po.PurchaseOrderID LEFT OUTER JOIN SourceList AS sl ON pod.SourceListID = sl.SourceListID LEFT OUTER JOIN ShipNoticeDtl AS snd ON pod.PurchaseOrderDtlCode = snd.PurchaseOrderDtlCode WHERE po.SupplierCode = @supplier", connection)) { SqlParameter supParameter = new SqlParameter("@supplier", SqlDbType.VarChar); supParameter.Value = supplierCode; cmd.Parameters.Add(supParameter); SqlDataReader Reader = cmd.ExecuteReader(); var qpod = Reader.Cast <IDataRecord>().Select(x => new { PartName = x["PartName"], PartNumber = x["PartNumber"], Qty = x["Qty"], UnitsOnOrder = x["UnitsOnOrder"], ShipQty = x["ShipQty"] }).ToList(); return(Json(qpod, JsonRequestBehavior.AllowGet)); } } }
// 添加供应商 internal void InsertSupplierAccount(SupplierAccount supplierAccount) { using (var db = new InventoryDB()) { db.Insert(supplierAccount); } }
// 更新供应商 internal void UpdateSupplierAccount(SupplierAccount supplierAccount) { using (var db = new InventoryDB()) { db.Update(supplierAccount); } }
public async Task <SupplierAccountDto> GetById(int id) { SupplierAccount result = await _supplierAccountRepository.GetAsync(id); SupplierAccountDto dto = ObjectMapper.Map <SupplierAccountDto>(result); return(dto); }
public AddSupplierAccountWindow(SupplierAccount supplierAccount) { InitializeComponent(); this.DataContext = new AddSupplierAccountWindowViewModel() { SupplierAccount = supplierAccount, Window = this }; }
public Supplier(Region homeRegion, Payer payer) : this() { Payer = payer; HomeRegion = homeRegion; RegionMask = homeRegion.Id; Account = new SupplierAccount(this); }
public ActionResult DeleteAccount(int accountid, int supplierid) { SupplierAccount supplierAccount = Db.SupplierAccounts.Find(accountid); Db.SupplierAccounts.Remove(supplierAccount); Db.SaveChanges(); return(RedirectToAction("Details", new { id = supplierid })); }
public Task <SupplierAccountDto> Create(SupplierAccountDto supplierAccountDto) { supplierAccountDto.CreatorUserId = AbpSession.UserId.Value; supplierAccountDto.CreationTime = DateTime.Now; SupplierAccount supplierAccount = ObjectMapper.Map <SupplierAccount>(supplierAccountDto); supplierAccountDto.Id = _supplierAccountRepository.InsertAndGetId(supplierAccount); return(Task.FromResult <SupplierAccountDto>(supplierAccountDto)); }
//4.存到資料庫 //更新此 user table private async Task updateTable(ApplicationUser user, SupplierAccount sa) { //更新此 user table await UserManager.UpdateAsync(user); //sa table db.Entry(sa).State = EntityState.Modified; int u2 = await db.SaveChangesAsync(); }
void ExecuteEditSupplierAccount(SupplierAccount parameter) { AddSupplierAccountWindow addSupplierAccountWindow = new AddSupplierAccountWindow(parameter); // 如果添加成功就更新数据 if (addSupplierAccountWindow.ShowDialog() == true) { ExecuteUpdateSupplierAccounts(CurrentSupplier); } }
//[Authorize(Roles = "ExternalSupplierAccount:12, ExternalSupplierAccount:13, ExternalSupplierAccount:15, ExternalSupplierAccount:16")] public IActionResult Create([FromBody] SupplierAccount supplierAccount) { var res = _supplierAccount.addSupplierAccount(supplierAccount); if (res == true) { return(CreatedAtRoute("SupplierAccountById", new { Controller = "ExternalSupplierAccount", id = supplierAccount.SupplierAccountId }, supplierAccount)); } return(StatusCode(500)); }
public ActionResult AddAccount(int supplierId) { Supplier supplier = Db.Suppliers.Find(supplierId); SupplierAccount supplierAccount = new SupplierAccount(); supplierAccount.Supplier = supplier; supplierAccount.SupplierId = supplier.Id; return(View(supplierAccount)); }
public ActionResult GetPartQtyByShipStatus() { //取得供應商帳號資料 SupplierAccount supplier = User.Identity.GetSupplierAccount(); supplierAccount = supplier.SupplierAccountID; supplierCode = supplier.SupplierCode; //////////////////////////////////////////////////// //未出貨料件的出貨數量 var qpodUnship = from pod in db.PurchaseOrderDtl.AsEnumerable() join po in db.PurchaseOrder on pod.PurchaseOrderID equals po.PurchaseOrderID where po.SupplierCode == supplierCode && pod.ShipDate == null select new OrderPartQty { name = pod.PartName, value = pod.Qty, }; //已出貨料件的出貨數量 var qpodShipped = from po in db.PurchaseOrder.AsEnumerable() join pod in db.PurchaseOrderDtl on po.PurchaseOrderID equals pod.PurchaseOrderID where po.SupplierCode == supplierCode && pod.ShipDate != null select new OrderPartQty { name = pod.PartName, value = pod.Qty, }; //未答交料件的出貨數量 var qpodNoApplied = from po in db.PurchaseOrder.AsEnumerable() join pod in db.PurchaseOrderDtl on po.PurchaseOrderID equals pod.PurchaseOrderID where po.SupplierCode == supplierCode && po.PurchaseOrderStatus == "P" select new OrderPartQty { name = pod.PartName, value = pod.Qty, }; IList <OrderPart> orderParts = new List <OrderPart>(); orderParts.Add(new OrderPart { name = "未出貨", data = qpodUnship.ToList() }); orderParts.Add(new OrderPart { name = "已出貨", data = qpodShipped.ToList() }); orderParts.Add(new OrderPart { name = "未答交", data = qpodNoApplied.ToList() }); var json = orderParts; return(Json(json, JsonRequestBehavior.AllowGet)); }
/// /////////////////////////// // GET: Orders public ActionResult Index() { //取得供應商帳號資料 SupplierAccount supplier = User.Identity.GetSupplierAccount(); supplierAccount = supplier.SupplierAccountID; supplierCode = supplier.SupplierCode; //////////////////////////////////////////////////// //var q = (db.PurchaseOrder.Where(x=>x.SupplierCode==supplierCode).Select(x=>new PurchaseOrder { // SupplierCode= x.SupplierCode, // PurchaseOrderID = x.PurchaseOrderID, // PurchaseOrderOID = x.PurchaseOrderOID //})).AsEnumerable(); var qpoP = from po in db.PurchaseOrder where po.PurchaseOrderStatus == "P" && po.SupplierCode == supplierCode select new { po.PurchaseOrderID }; var s = qpoP.ToList(); List <SelectListItem> orderList = new List <SelectListItem>(); foreach (var orderID in qpoP) { SelectListItem order = new SelectListItem() { Text = orderID.PurchaseOrderID, Value = orderID.PurchaseOrderID, Selected = false, }; orderList.Add(order); } OrderSendedToSupplierViewModel orderModel; if (orderList.Count() != 0) { orderModel = new OrderSendedToSupplierViewModel() { SupplierCode = supplierCode, orderID = orderList[0].Value, orderList = orderList }; return(View(orderModel)); } orderModel = new OrderSendedToSupplierViewModel() { SupplierCode = supplierCode, orderID = "orderID", orderList = orderList }; return(View(orderModel)); }
//SendEmail public async Task SendMailToBuyer(List <OrderDtlForMail> shipNotice) { /////////////////////////////////////////////////// //取得供應商帳號資料 SupplierAccount supplier = User.Identity.GetSupplierAccount(); string supplierAccount = supplier.SupplierAccountID; string supplierCode = supplier.SupplierCode; //////////////////////////////////////////////////// string supAccID = supplierAccount; //string borderColor = "border-color:black"; //string borderLine = "1"; //string shipDtlMail = $"<h2>出貨單號:{shipNotice.FirstOrDefault().ShipNoticeID}</h2>"; //shipDtlMail += $"<table style={borderColor} border={borderLine}><thead><tr><th>出貨商品明細流水號</th><th>料件編號</th><th>料件名稱</th><th>出貨數量</th><th>出貨日期</th></tr><thead><tbody>"; //foreach (var snd in shipNotice) //{ // DateTime shipDate = (DateTime)snd.ShipDate; // shipDtlMail += "<tr>"; // shipDtlMail += $"<td>{snd.ShipNoticeOID}</td><td>{snd.PartNumber}</td><td>{snd.PartName}</td><td>{snd.ShipQty}</td><td>{shipDate.ToString("yyyy/MM/dd")}</td>"; // shipDtlMail += "</tr>"; //} //shipDtlMail += "</tbody></table>"; string snId = shipNotice[0].ShipNoticeID; PurchaseOrder order = db.PurchaseOrder.Find(db.ShipNotice.Find(snId).PurchaseOrderID); string OrderID = order.PurchaseOrderID; string OrderApply = "已出貨"; string borderSize = "1"; // borderColor = "border:'1'"; //borderColor += "sans-serif;margin: 20px auto; background-color: #eee; padding-bottom: 20px;"; string OrderDtl = $"<tr><td colspan='5'><h2>出貨明細</h2></br><h2>出貨單號:{snId}</h2></td></tr>"; OrderDtl += "<tr><td>出貨明細編號</td><td>料件編號</td><td>料件名稱</td><td>出貨數量</td><td>出貨日期</td></tr>"; foreach (var snd in shipNotice) { DateTime shipDate = (DateTime)snd.ShipDate; OrderDtl += $"<tr>"; OrderDtl += $"<td>{snd.ShipNoticeOID}</td><td>{snd.PartNumber}</td><td>{snd.PartName}</td><td>{snd.ShipQty}</td><td>{shipDate.ToString("yyyy/MM/dd")}</td>"; OrderDtl += "</tr>"; } string SupplierName = db.SupplierInfo.Where(x => x.SupplierCode == supplierCode).SingleOrDefault().SupplierName; string BuyerID = db.Employee.Where(x => x.EmployeeID == order.EmployeeID).SingleOrDefault().EmployeeID; string emp = db.PurchaseOrder.Find(db.ShipNotice.Find(snId).PurchaseOrderID).EmployeeID; string EmployeeName = db.Employee.Find(emp).Name; var user = UserManager.Users.Where(x => x.UserName == emp).SingleOrDefault(); var userId = user.Id; string tempMail = System.IO.File.ReadAllText(Server.MapPath(@"~\Areas\SupplierArea\Views\Shared\SendMailToBuyer.html")); string MailBody = MembersDBService.getMailBody(tempMail, OrderID, OrderApply, EmployeeName, OrderDtl, SupplierName); //寄信 await UserManager.SendEmailAsync(userId, "商品出貨通知", MailBody); // bool a = UserManager.SendEmailAsync(userId, "商品出貨通知", shipDtlMail).IsCompleted; }
public ActionResult AddAccount(SupplierAccount supplierAccount) { try { Db.SupplierAccounts.Add(supplierAccount); Db.SaveChanges(); return(RedirectToAction("Details", new { id = supplierAccount.SupplierId })); } catch { ViewBag.ErrorMessage = "Error occured while adding account number."; return(View("Error")); } }
//進入庫存管理頁面方法 public ActionResult Index() { //取得供應商帳號資料 SupplierAccount supplier = User.Identity.GetSupplierAccount(); supplierAccount = supplier.SupplierAccountID; supplierCode = supplier.SupplierCode; //////////////////////////////////////////////////// SupplierInfo supplierInfo = db.SupplierInfo.Find(supplierCode); ViewBag.supplierName = supplierInfo.SupplierName; ViewBag.supplierCode = supplierCode; return(View()); }
public bool addSupplierAccount(SupplierAccount supplier) { try { _context.ExternalSupplierAccounts.Add(supplier); _context.SaveChanges(); } catch (Exception e) { Console.WriteLine(e.StackTrace); return(false); } return(true); }
public bool updateSupplierAccount(int id, SupplierAccount supplier) { supplier.SupplierId = id; try { _context.ExternalSupplierAccounts.Update(supplier); _context.SaveChanges(); } catch (Exception e) { Console.WriteLine(e.StackTrace); return(false); } return(true); }
//pieChart public ActionResult GetPartTotalPricePercentage(string dateStart, string dateEnd) { //取得供應商帳號資料 SupplierAccount supplier = User.Identity.GetSupplierAccount(); supplierAccount = supplier.SupplierAccountID; supplierCode = supplier.SupplierCode; //////////////////////////////////////////////////// DateTime dateStartD = Convert.ToDateTime(dateStart); DateTime dateEndD = Convert.ToDateTime(dateEnd); if (dateStart == null || dateEnd == null) { dateStartD = DateTime.Now.AddMonths(-3); dateEndD = DateTime.Now; } //計算計算選取區間已出貨的商品金額,如無選取預設為今日以前三個月 var qpo = from po in db.PurchaseOrder join pod in db.PurchaseOrderDtl on po.PurchaseOrderID equals pod.PurchaseOrderID where po.CreateDate > dateStartD && po.CreateDate < dateEndD //&& po.PurchaseOrderStatus == "E" || po.PurchaseOrderStatus == "S" && po.SupplierCode == supplierCode select new { pod.PartName, pod.PartNumber, pod.QtyPerUnit, pod.Qty, pod.PurchaseUnitPrice, pod.Discount, pod.SourceListID }; //計算百分比//搞錯了highChart會自動幫你計算百分比 List <partTotalPriceViewModel> list = new List <partTotalPriceViewModel>(); foreach (var data in qpo) { partTotalPriceViewModel temp = new partTotalPriceViewModel(); temp.ToalPrice = data.Qty * data.PurchaseUnitPrice * data.QtyPerUnit * (1 - data.Discount); temp.PartNumber = data.PartNumber; temp.PartName = data.PartName; temp.SourceListID = data.SourceListID; list.Add(temp); } return(Json(list, JsonRequestBehavior.AllowGet)); }
public static SupplierAccount GetSupplierAccount(this IIdentity identity) { if (identity == null) { return(null); } string id = identity.GetUserName(); //供應商帳號SE開頭 if (!id.StartsWith("SE")) { return(null); } using (PMSAEntities db = new PMSAEntities()) { SupplierAccount sa = db.SupplierAccount.Find(id); return(sa); } }
/// 出貨管理首頁////////////////////////////////////////////////// public ActionResult Index() { //////////////////////////////////////////////// //取得供應商帳號資料 SupplierAccount supplier = User.Identity.GetSupplierAccount(); supplierAccount = supplier.SupplierAccountID; supplierCode = supplier.SupplierCode; //////////////////////////////////////////////////// if (TempData["message"] != null) { ViewBag.message = TempData["message"]; } else { ViewBag.message = "你好"; } return(View()); }
//TODO BuyerSup Admin 學這邊把update結果變回傳0 1 //判斷一起成敗 改成用交易 //4.存到資料庫 //更新此 user table private async Task <int> updateTable(ApplicationUser user, SupplierAccount sa) { //更新此 user table var store = new UserStore <ApplicationUser>(new ApplicationDbContext()); var u1 = await UserManager.UpdateAsync(user); var ctx = store.Context; await ctx.SaveChangesAsync(); //sa table db.Entry(sa).State = EntityState.Modified; var u2 = await db.SaveChangesAsync(); //成功 if (u1.Succeeded && u2 > 0) { return(1); } //失敗 return(0); }
//寄信 public async Task SendMailToBuyer(PurchaseOrder order, string Reply, string OrderDtl) { /////////////////////////////////////////////////// //取得供應商帳號資料 SupplierAccount supplier = User.Identity.GetSupplierAccount(); string supplierAccount = supplier.SupplierAccountID; string supplierCode = supplier.SupplierCode; //////////////////////////////////////////////////// //string borderColor = "border-color:black"; //string borderLine = "1"; //string BuyerName = db.Employee.Find(order.EmployeeID).Name; //string shipDtlMail = $"<table style='{borderColor}' border='{borderLine}'>"; //shipDtlMail += $"<thead><tr><th>{BuyerName},你好</th></tr></thead>"; //shipDtlMail += $"<tr><td>訂單編號:{order.PurchaseOrderID}{Reply}</td></tr>"; //shipDtlMail += "</table>"; string OrderID = order.PurchaseOrderID; string OrderApply = Reply; if (OrderDtl == null) { OrderDtl = ""; } string SupplierName = db.SupplierInfo.Where(x => x.SupplierCode == supplierCode).SingleOrDefault().SupplierName; string BuyerID = db.Employee.Where(x => x.EmployeeID == order.EmployeeID).SingleOrDefault().EmployeeID; string EmployeeName = db.Employee.Find(BuyerID).Name; var user = UserManager.Users.Where(x => x.UserName == BuyerID).SingleOrDefault(); var userId = user.Id; // 如需如何進行帳戶確認及密碼重設的詳細資訊,請前往 https://go.microsoft.com/fwlink/?LinkID=320771 // 傳送包含此連結的電子郵件 //var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("PMSAWebMVC"); //更改密碼要在code之前不然他是拿UpdateSecurityStampAsync 來生code的 //string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); string tempMail = System.IO.File.ReadAllText(Server.MapPath(@"~\Areas\SupplierArea\Views\Shared\SendMailToBuyer.html")); // 經測試 gmail 不支援 uri data image 所以用網址傳圖比較保險 //string img = "https://ci5.googleusercontent.com/proxy/4OJ0k4udeu09Coqzi7ZQRlKXsHTtpTKlg0ungn0aWQAQs2j1tTS6Q6e8E0dZVW2qsbzD1tod84Zbsx62gMgHLFGWigDzFOPv1qBrzhyFIlRYJWSMWH8=s0-d-e1-ft#https://app.flashimail.com/rest/images/5d8108c8e4b0f9c17e91fab7.jpg"; string MailBody = MembersDBService.getMailBody(tempMail, OrderID, OrderApply, EmployeeName, OrderDtl, SupplierName); //寄信 await UserManager.SendEmailAsync(userId, "供應商訂單答交通知", MailBody); }
public Server2Client addTrans(SupplierAccount ca) { sc = new Server2Client(); MySqlCommand cmd = new MySqlCommand("INSERT INTO SupplierAccount (SupplierID, TransDate, Description, Debit, Credit, Balance) VALUES (@CID, @TDT, @DSC, @CDR, @CCR, @BAL)", cm); cmd.Parameters.AddWithValue("@CID", ca.SupplierID); cmd.Parameters.AddWithValue("@TDT", ca.TransDate); cmd.Parameters.AddWithValue("@DSC", ca.Description); cmd.Parameters.AddWithValue("@CDR", ca.Debit); cmd.Parameters.AddWithValue("@CCR", ca.Credit); cmd.Parameters.AddWithValue("@BAL", ca.Balance); try { cm.Open(); cmd.ExecuteNonQuery(); } catch (Exception ex) { sc.Message = ex.Message; } finally { cm.Close(); } return(sc); }
//拒絕按鈕 public async Task <JsonResult> OrderRefuse(string orderID) { /////////////////////////////////////////////////// //取得供應商帳號資料 SupplierAccount supplier = User.Identity.GetSupplierAccount(); string supplierAccount = supplier.SupplierAccountID; string supplierCode = supplier.SupplierCode; //////////////////////////////////////////////////// ShipNoticesUtilities utilities = new ShipNoticesUtilities(); PurchaseOrder purchaseOrder = db.PurchaseOrder.Find(orderID); if (purchaseOrder == null) { return(Json("fail", JsonRequestBehavior.AllowGet)); } //狀態異動中=C purchaseOrder.PurchaseOrderStatus = "C"; utilities.AddAPOChanged(purchaseOrder, supplierAccount, supplierCode); db.Entry(purchaseOrder).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); await SendMailToBuyer(purchaseOrder, "已拒絕", null); return(Json("success", JsonRequestBehavior.AllowGet)); }
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 })); }