public ActionResult ImportInfo() { //需要盤點程序結束的狀態且是ImportInventory有資料的情況下,才能進行盤盈匯入 if (db.SysPara.Where(r => r.ParaName == "Inventory").FirstOrDefault().ParaValue.Equals("false") && db.ImportInventory.Count() > 0) { //先查詢要匯入的資料 foreach (var result in db.ImportInventory) { //先將資料記錄到HistoryInventory var HistoryInfo = new HistoryInventory { Type = result.Type, Prod = result.Prod, Thickness = result.Thickness, Widt = result.Widt, Leng = result.Leng, Splice = result.Splice, Ptno = result.Ptno, ProductDate = Convert.ToDateTime(result.ProductDate), CustomerNO = result.CustomerNO, TransactionDate = Convert.ToDateTime(result.TransactionDate), SrnmType = "4", NewWeight = result.NewWeight, ZoneSN = result.ZoneSN, ZoneID = result.ZoneID, PackNo = result.PackNo, Thickness2 = result.Thickness2 }; db.HistoryInventory.Add(HistoryInfo); db.SaveChanges(); //再寫入庫存table var ImportInfo = new Inventory { Type = result.Type, Prod = result.Prod, Thickness = result.Thickness, Widt = result.Widt, Leng = result.Leng, Splice = result.Splice, Ptno = result.Ptno, ProductDate = Convert.ToDateTime(result.ProductDate), CustomerNO = result.CustomerNO, TransactionDate = Convert.ToDateTime(result.TransactionDate), SrnmType = "4", NewWeight = result.NewWeight, ZoneSN = result.ZoneSN, ZoneID = result.ZoneID, PackNo = result.PackNo, Thickness2 = result.Thickness2 }; db.Inventory.Add(ImportInfo); db.SaveChanges(); //刪除每一筆ImportInventory的資料 db.ImportInventory.Remove(result); if (TryUpdateModel(result)) { db.SaveChanges(); } } //如果前此盤點時,完全都沒有需要盤虧刪除的資料,仍要把tempInventory的資料刪除 if (db.ClearInventory.Count() == 0) { var TempResult = db.TempInventory.Select(s => s).ToList(); //刪除tempinventory的所有資料 db.TempInventory.RemoveRange(TempResult); db.SaveChanges(); } } //當ClearInventory與ImportInventory資料都為0, //表示此盤點程序與庫存資料已經核對完畢,則將db的參數改為true if (db.ClearInventory.Count() == 0 && db.ImportInventory.Count() == 0) { try { if (ModelState.IsValid) { var SysPara = db.SysPara.Where(r => r.ParaName == "Inventory"); if (SysPara != null) { SysPara.FirstOrDefault().ParaValue = "true"; db.SaveChanges(); } } } catch (Exception ex) { }; } //return View(); return RedirectToAction("Index", "BarCodeWebDiffInventory"); }
//入庫 public String ReceiptIntoDB(String sStaffID, String sZoneNo, String sZoneSN, String sFuncCode, List<String> InfoList, String sCopper, String sCustomer, String sCtlNo1, String sCtlNo2) { String sResult = ""; //取得ZoneID String sZoneID = GetZoneID(sZoneNo); //取得松電工的支號 String sProd = (sCopper.Equals("4")) ? InfoList[3].ToString() : InfoList[0].ToString(); var Info = new Inventory(); try { if (db.Inventory.Where(i => i.Prod == sProd).Count() > 0) { sResult = sProd + ",此筆資料,已入庫!!"; } else { if (InfoList.Count == 12 && sCopper.Equals("1")) { //設定日期格式 DateTime dt = SetDateTime(int.Parse(InfoList[7].Substring(0, 4)), int.Parse(InfoList[7].Substring(4, 2)), int.Parse(InfoList[7].Substring(6, 2))); Info = new Inventory { Thickness = InfoList[2], Widt = InfoList[3], Type = InfoList[10], Prod = InfoList[0], Leng = int.Parse((InfoList[4])), NewWeight = float.Parse(InfoList[5]), SrnmType = sFuncCode, ProductDate = dt, TransactionDate = DateTime.Now, StaffID = sStaffID, ZoneID = sZoneID, ZoneSN = sZoneSN, Splice = InfoList[6], Ptno = InfoList[11], CustomerNO = sCustomer, PackNo = InfoList[9], Thickness2 = InfoList[1], CtlNo1 = sCtlNo1, CtlNo2 = sCtlNo2 }; } //松電工 else if (InfoList.Count == 12 && sCopper.Equals("4")) { //設定日期格式 DateTime dt = SetDateTime(int.Parse(InfoList[9].Substring(0, 4)), int.Parse(InfoList[9].Substring(4, 2)), int.Parse(InfoList[9].Substring(6, 2))); Info = new Inventory { Thickness = InfoList[5], Widt = InfoList[6], Type = InfoList[11], Prod = InfoList[3], Leng = int.Parse((InfoList[7])), NewWeight = float.Parse(InfoList[2]), SrnmType = sFuncCode, ProductDate = dt, TransactionDate = DateTime.Now, StaffID = sStaffID, ZoneID = sZoneID, ZoneSN = sZoneSN, Splice = InfoList[8], Ptno = InfoList[0], CustomerNO = sCustomer, PackNo = InfoList[10], Thickness2 = InfoList[4], CtlNo1 = sCtlNo1, CtlNo2 = sCtlNo2 }; } //日系一般 else if (InfoList.Count == 9 && sCopper.Equals("5")) { //設定日期格式 DateTime dt = SetDateTime(int.Parse(InfoList[5].Substring(0, 2)) + 2000, int.Parse(InfoList[5].Substring(2, 2)), int.Parse(InfoList[5].Substring(4, 2))); Info = new Inventory { //Thickness = InfoList[1],日系一般沒有此項資料 Widt = InfoList[2], Type = InfoList[6], Prod = InfoList[0], Leng = int.Parse((InfoList[3])), NewWeight = float.Parse(InfoList[4]), SrnmType = sFuncCode, ProductDate = dt, TransactionDate = DateTime.Now, StaffID = sStaffID, ZoneID = sZoneID, ZoneSN = sZoneSN, //Splice = InfoList[5],沒有接頭數 CustomerNO = sCustomer, CtlNo1 = sCtlNo1, CtlNo2 = sCtlNo2, Thickness2 = InfoList[1], Ptno = InfoList[7] //特殊記項為,InfoList[8],尚無用到 }; } else if (InfoList.Count == 9) //else { //設定日期格式 DateTime dt = SetDateTime(int.Parse(InfoList[6].Substring(0, 2)) + 2000, int.Parse(InfoList[6].Substring(2, 2)), int.Parse(InfoList[6].Substring(4, 2))); Info = new Inventory { //Thickness = InfoList[2], //Widt = InfoList[3], //Type = InfoList[9], //Prod = InfoList[1], //Leng = int.Parse((InfoList[4])), //NewWeight = float.Parse(InfoList[5]), //SrnmType = sFuncCode, //ProductDate = dt, //TransactionDate = DateTime.Now, //StaffID = sStaffID, //ZoneID = sZoneID, //ZoneSN = sZoneSN, //Splice = InfoList[6], ////Ptno = InfoList[11] Thickness = InfoList[1], Widt = InfoList[2], Type = InfoList[8], Prod = InfoList[0], Leng = int.Parse((InfoList[3])), NewWeight = float.Parse(InfoList[4]), SrnmType = sFuncCode, ProductDate = dt, TransactionDate = DateTime.Now, StaffID = sStaffID, ZoneID = sZoneID, ZoneSN = sZoneSN, Splice = InfoList[5], CustomerNO = sCustomer, CtlNo1 = sCtlNo1, CtlNo2 = sCtlNo2 //Ptno = InfoList[11] }; } db.Inventory.Add(Info); db.SaveChanges(); //將異動資料寫入歷史記錄資料表 var HistoryInfo = new HistoryInventory { Type = Info.Type, Prod = Info.Prod, Thickness = Info.Thickness, Widt = Info.Widt, Leng = Info.Leng, Splice = Info.Splice, Ptno = Info.Ptno, ProductDate = Convert.ToDateTime(Info.ProductDate), CustomerNO = Info.CustomerNO, StaffID = sStaffID, TransactionDate = Convert.ToDateTime(Info.TransactionDate), SrnmType = sFuncCode, NewWeight = Info.NewWeight, ZoneSN = Info.ZoneSN, ZoneID = Info.ZoneID, PackNo = Info.PackNo, Thickness2 = Info.Thickness2 }; db.HistoryInventory.Add(HistoryInfo); db.SaveChanges(); sResult = "true"; } } catch (System.Data.Entity.Validation.DbEntityValidationException ex) { sResult = ex.ToString(); }; return sResult; }
public ActionResult CreateReceiptInfo(IEnumerable<InventoryView> receiptInfo) { try { if (ModelState.IsValid) { //前端使用者輸入庫位名稱,透過此方法取得ZoneID //String sZoneID = oDB.GetZoneID(receiptInfo.FirstOrDefault().ZoneNo); //寫入庫存table var Inventory = new Inventory { Type = receiptInfo.FirstOrDefault().Type, Prod = receiptInfo.FirstOrDefault().Prod, Thickness = receiptInfo.FirstOrDefault().Thickness, Widt = receiptInfo.FirstOrDefault().Widt, Leng = receiptInfo.FirstOrDefault().Leng, Splice = receiptInfo.FirstOrDefault().Splice, Ptno = receiptInfo.FirstOrDefault().Ptno, ProductDate = Convert.ToDateTime(receiptInfo.FirstOrDefault().ProductDate), CustomerNO = receiptInfo.FirstOrDefault().CustomerName,//前端客戶名稱欄位用來存放使用者輸入的客戶代碼 TransactionDate = Convert.ToDateTime(receiptInfo.FirstOrDefault().TransactionDate), SrnmType = "2", StaffID = receiptInfo.FirstOrDefault().StaffID, NewWeight = receiptInfo.FirstOrDefault().NewWeight, ZoneSN = oDB.MakeSN(), ZoneID = oDB.GetZoneID(receiptInfo.FirstOrDefault().ZoneNo), PackNo = receiptInfo.FirstOrDefault().PackNo, Thickness2 = receiptInfo.FirstOrDefault().Thickness2, CtlNo1 = receiptInfo.FirstOrDefault().CtlNo1, CtlNo2 = receiptInfo.FirstOrDefault().CtlNo2, }; if (TryUpdateModel(Inventory)) { db.Inventory.Add(Inventory); db.SaveChanges(); } //將資料寫入歷史資料庫 //oDB.InsertHistoryInfo(Inventory, Session["Account"].ToString(), "2"); //將資料寫入歷史資料庫 var HistoryInfo = new HistoryInventory { Type = Inventory.Type, Prod = Inventory.Prod, Thickness = Inventory.Thickness, Widt = Inventory.Widt, Leng = Inventory.Leng, Splice = Inventory.Splice, Ptno = Inventory.Ptno, ProductDate = Convert.ToDateTime(Inventory.ProductDate), CustomerNO = Inventory.CustomerNO, TransactionDate = Convert.ToDateTime(Inventory.TransactionDate), SrnmType = "2", StaffID = Inventory.StaffID, NewWeight = Inventory.NewWeight, ZoneSN = Inventory.ZoneSN, ZoneID = Inventory.ZoneID, PackNo = Inventory.PackNo, Thickness2 = Inventory.Thickness2, CtlNo1 = Inventory.CtlNo1, CtlNo2 = Inventory.CtlNo2, }; if (TryUpdateModel(HistoryInfo)) { db.HistoryInventory.Add(HistoryInfo); db.SaveChanges(); } } } catch (System.Data.Entity.Validation.DbEntityValidationException ex) { }; return Json(null); }
//新增異動資料method,使用IEnumerable public void InsertHistoryInfo(Inventory oInfo, String sAccount, String sType) { using(SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["BarCode"].ConnectionString)) { string sCommStr = "INSERT INTO HistoryInventory ( Type," + "Prod," + "Thickness," + "Widt," + "Leng," + "Splice," + "Ptno," + "ProductDate," + "StaffID," + "TransactionDate," + "SrnmType," + "NewWeight," + "ZoneSN," + "ZoneID," + "Thickness2 ) " + "VALUES ( @param1, @param2, @param3, @param4, @param5, @param6, @param7, @param8, @param11, @param12, @param13, @param18, @param20, @param21 , @param22) "; conn.Open(); SqlCommand scmd = new SqlCommand(sCommStr, conn); scmd.Parameters.Add(new SqlParameter("@param1", oInfo.Type)); scmd.Parameters.Add(new SqlParameter("@param2", oInfo.Prod)); scmd.Parameters.Add(new SqlParameter("@param3", oInfo.Thickness)); scmd.Parameters.Add(new SqlParameter("@param4", oInfo.Widt)); scmd.Parameters.Add(new SqlParameter("@param5", oInfo.Leng)); scmd.Parameters.Add(new SqlParameter("@param6", oInfo.Splice)); scmd.Parameters.Add(new SqlParameter("@param7", oInfo.Ptno)); scmd.Parameters.Add(new SqlParameter("@param8", oInfo.ProductDate)); scmd.Parameters.Add(new SqlParameter("@param11", sAccount)); scmd.Parameters.Add(new SqlParameter("@param12", DateTime.Now)); scmd.Parameters.Add(new SqlParameter("@param13", sType)); scmd.Parameters.Add(new SqlParameter("@param18", oInfo.NewWeight)); scmd.Parameters.Add(new SqlParameter("@param20", oInfo.ZoneSN)); scmd.Parameters.Add(new SqlParameter("@param21", oInfo.ZoneID)); scmd.Parameters.Add(new SqlParameter("@param22", oInfo.Thickness2)); scmd.ExecuteNonQuery(); conn.Close(); } }
public ActionResult UpdateReceiptInfo(IEnumerable<HistoryInventoryView> receiptInfo) { try { //先修改庫存資料 if (ModelState.IsValid) { var Inventory = new Inventory { Thickness = receiptInfo.FirstOrDefault().Thickness, Widt = receiptInfo.FirstOrDefault().Widt, Type = receiptInfo.FirstOrDefault().Type, Prod = receiptInfo.FirstOrDefault().Prod, Leng = receiptInfo.FirstOrDefault().Leng, NewWeight = receiptInfo.FirstOrDefault().NewWeight, ProductDate = receiptInfo.FirstOrDefault().ProductDate, TransactionDate = receiptInfo.FirstOrDefault().TransactionDate, CtlNo1 = receiptInfo.FirstOrDefault().CtlNo1, CtlNo2 = receiptInfo.FirstOrDefault().CtlNo2, //CtlName1 = receiptInfo.FirstOrDefault().CtlName1, //CtlName2 = receiptInfo.FirstOrDefault().CtlName2 }; if (TryUpdateModel(Inventory)) { db.Inventory.Attach(Inventory); db.Entry(Inventory).Property(r => r.Thickness).IsModified = true; db.Entry(Inventory).Property(r => r.Widt).IsModified = true; db.Entry(Inventory).Property(r => r.Type).IsModified = true; db.Entry(Inventory).Property(r => r.Leng).IsModified = true; db.Entry(Inventory).Property(r => r.NewWeight).IsModified = true; db.Entry(Inventory).Property(r => r.ProductDate).IsModified = true; db.Entry(Inventory).Property(r => r.TransactionDate).IsModified = true; db.Entry(Inventory).Property(r => r.CtlNo1).IsModified = true; db.Entry(Inventory).Property(r => r.CtlNo2).IsModified = true; //db.Entry(Inventory).Property(r => r.CtlName1).IsModified = true; //db.Entry(Inventory).Property(r => r.CtlName2).IsModified = true; db.SaveChanges(); } //將異動資料寫入資料庫 oDB.InsertHistoryInfo(Inventory, Session["Account"].ToString(),"3"); } } catch (Exception ex) { }; return Json(null); }