protected void cmdSave_click(object sender, EventArgs e) { if (facade == null) { facade = new WarehouseFacade(base.DataProvider); } if (_Invenfacade == null) { _Invenfacade = new InventoryFacade(base.DataProvider); } DBDateTime dbTime = FormatHelper.GetNowDBDateTime(this.DataProvider); int sum = 0; int insum = 0; object[] objs_asnd = facade.GetASNDetailByStNoAndDQMCode(poStno, DQMCode); if (objs_asnd != null) { #region 检查维护的数量是否等于导入数量 for (int i = 0; i < this.gridWebGrid.Rows.Count; i++) { if (!string.IsNullOrEmpty(this.gridWebGrid.Rows[i].Items.FindItemByKey("INQTY").Value.ToString())) { try { insum += int.Parse(this.gridWebGrid.Rows[i].Items.FindItemByKey("INQTY").Value.ToString()); } catch (Exception ex) { WebInfoPublish.Publish(this, "来料数量必须是数字格式" + ex.Message, this.languageComponent1); return; } } } foreach (Asndetail asnd in objs_asnd) { sum += asnd.Qty; } if (insum != sum) { WebInfoPublish.Publish(this, "分配数量不等于导入数量", this.languageComponent1); return; } #endregion this.DataProvider.BeginTransaction(); #region 除asndetailitem表中原有的数据(stno,dqmcode) foreach (Asndetail asnd in objs_asnd) { object[] objs_asnditem = facade.GetASNDetailItembyStnoAndStline(asnd.Stno, asnd.Stline); foreach (Asndetailitem asnditem_old in objs_asnditem) { facade.DeleteAsndetailitem(asnditem_old); } } #endregion //#region 根据grid表重新写asndetailitem //int PlanQty = 0; //for (int i = 0; i < this.gridWebGrid.Rows.Count; i++) //{ // if (!string.IsNullOrEmpty(this.gridWebGrid.Rows[i].Items.FindItemByKey("POQTY").Value.ToString()) || int.Parse(this.gridWebGrid.Rows[i].Items.FindItemByKey("POQTY").Value.ToString())!=0) // { // PlanQty = int.Parse(this.gridWebGrid.Rows[i].Items.FindItemByKey("POQTY").Value.ToString()); // Asndetailitem asnditem_new = facade.CreateNewAsndetailitem(); // foreach (Asndetail asnd in objs_asnd) // { // if(asnd.Qty) // } // } //} #region 插入tblasndetailITEM foreach (Asndetail asnd in objs_asnd) { Asndetailitem detailitem = facade.CreateNewAsndetailitem(); detailitem.CDate = dbTime.DBDate; detailitem.CTime = dbTime.DBTime; detailitem.CUser = this.GetUserCode(); detailitem.MaintainDate = dbTime.DBDate; detailitem.MaintainTime = dbTime.DBTime; detailitem.MaintainUser = this.GetUserCode(); detailitem.Stline = asnd.Stline.ToString(); detailitem.Stno = asnd.Stno; detailitem.MCode = asnd.MCode; detailitem.DqmCode = asnd.DqmCode; //detailitem.ActQty = 0; //detailitem.QcpassQty = 0; //detailitem.ReceiveQty = 0; //查找对应的SAP单 decimal sub = asnd.Qty; for (int i = 0; i < this.gridWebGrid.Rows.Count; i++) { if (!string.IsNullOrEmpty(this.gridWebGrid.Rows[i].Items.FindItemByKey("INQTY").Value.ToString()) || int.Parse(this.gridWebGrid.Rows[i].Items.FindItemByKey("INQTY").Value.ToString()) != 0) { //InvoicesDetail invdetail = qtyobjs[i] as InvoicesDetail; decimal subNeed = decimal.Parse(this.gridWebGrid.Rows[i].Items.FindItemByKey("POQTY").Value.ToString()) - decimal.Parse(this.gridWebGrid.Rows[i].Items.FindItemByKey("IN1QTY").Value.ToString()); if (subNeed < decimal.Parse(this.gridWebGrid.Rows[i].Items.FindItemByKey("INQTY").Value.ToString())) { this.DataProvider.RollbackTransaction(); WebInfoPublish.Publish(this, "分配数量大于所需数量", this.languageComponent1); return; } object findNeedQTY_now = facade.GetNeedImportQtyNow(this.gridWebGrid.Rows[i].Items.FindItemByKey("invno").Value.ToString(), Int32.Parse(this.gridWebGrid.Rows[i].Items.FindItemByKey("POITEMNO").Value.ToString()), asnd.Stno); //找这个invoice行已经导入了多少,和判退多少 Asndetailitem subItemNow = findNeedQTY_now as Asndetailitem; subNeed = decimal.Parse(this.gridWebGrid.Rows[i].Items.FindItemByKey("INQTY").Value.ToString()) - subItemNow.Qty; if (subNeed == 0) { continue; } //如果箱数量大于需求数量差---进行拆分 if (sub > subNeed) { sub = sub - subNeed; // sub是剩余的 detailitem.Qty = subNeed; detailitem.Invline = this.gridWebGrid.Rows[i].Items.FindItemByKey("POITEMNO").Value.ToString(); detailitem.Invno = this.gridWebGrid.Rows[i].Items.FindItemByKey("invno").Value.ToString(); detailitem.ActQty = detailitem.Qty; detailitem.QcpassQty = detailitem.Qty; detailitem.ReceiveQty = detailitem.Qty; facade.AddAsndetailitem(detailitem); } //如果箱单数量小于等于需求数量差--直接填入 else { detailitem.Qty = sub; detailitem.Invline = this.gridWebGrid.Rows[i].Items.FindItemByKey("POITEMNO").Value.ToString(); detailitem.Invno = this.gridWebGrid.Rows[i].Items.FindItemByKey("invno").Value.ToString(); detailitem.ActQty = detailitem.Qty; detailitem.QcpassQty = detailitem.Qty; detailitem.ReceiveQty = detailitem.Qty; facade.AddAsndetailitem(detailitem); sub = 0; } if (sub == 0) { break; } } } //如果sub>0,说明导入数量过多,报错 if (sub > 0) { this.DataProvider.RollbackTransaction(); WebInfoPublish.Publish(this, "$Error_SAP_NEED_DATA_ERROR", this.languageComponent1); // BenQGuru.eMES.Common.ExceptionManager.Raise(this.GetType().BaseType, "$Error_SAP_NEED_DATA_ERROR"); return; } } #endregion } else { this.DataProvider.RollbackTransaction(); WebInfoPublish.Publish(this, "AsnDetail中没有数据", this.languageComponent1); return; } this.DataProvider.CommitTransaction(); WebInfoPublish.Publish(this, "保存成功", this.languageComponent1); this.gridHelper.RequestData(); return; }