private string autoCreateASNFromCheckoutInfo(string invNo, Pick pick)
        {
            try
            {
                InvoicesDetail   invD   = (InvoicesDetail)_InventoryFacade.GetInvoicesDetail(invNo);
                CARTONINVOICES[] cartos = _warehouseFacade.GetGrossAndWeight(pick.PickNo);
                string           stno   = CreateStNO();
                Asn asn = new Asn();
                asn.Stno    = stno;
                asn.StType  = "UB";
                asn.CDate   = FormatHelper.TODateInt(DateTime.Now);
                asn.CTime   = FormatHelper.TOTimeInt(DateTime.Now);
                asn.CUser   = GetUserCode();
                asn.Status  = "Release";
                asn.Invno   = invNo;
                asn.FacCode = pick.FacCode;
                if (cartos.Length > 0)
                {
                    asn.Gross_weight = (decimal)cartos[0].GROSS_WEIGHT;
                    asn.Volume       = cartos[0].VOLUME;
                }
                asn.StorageCode  = invD.StorageCode ?? " ";
                asn.MaintainUser = GetUserCode();
                asn.MaintainTime = FormatHelper.TOTimeInt(DateTime.Now);
                asn.MaintainDate = FormatHelper.TODateInt(DateTime.Now);
                CartonInvDetailMaterial[] cartonMs    = _warehouseFacade.GetCartonInvDetailMaterial(pick.PickNo);
                MOModel.ItemFacade        _itemfacade = new BenQGuru.eMES.MOModel.ItemFacade(this.DataProvider);

                int i = 1;

                this.DataProvider.BeginTransaction();
                foreach (CartonInvDetailMaterial m in cartonMs)
                {
                    object materobj = _itemfacade.GetMaterial(m.MCODE);
                    Domain.MOModel.Material mater = materobj as Domain.MOModel.Material;
                    if (materobj == null)
                    {
                        this.DataProvider.RollbackTransaction();
                        throw new Exception("物料表没有物料:" + m.MCODE);
                    }
                    string custMCode = _warehouseFacade.GetCustMCodeForUB(pick.PickNo, m.DQMCODE);
                    if (mater.MCONTROLTYPE == SAP_CONTROLTYPE.SAP_ITEM_CONTROL_KEYPARTS)
                    {
                        ASNDetail asnd = new ASNDetail();
                        asnd.ActQty = 0;
                        //asnd.CartonNo = " ";
                        asnd.DQMCode      = m.DQMCODE;
                        asnd.Qty          = (int)m.QTY;
                        asnd.StLine       = i.ToString();
                        asnd.CustMCode    = custMCode;
                        asnd.MCode        = m.MCODE;
                        asnd.Status       = "Release";
                        asnd.StNo         = stno;
                        asnd.MDesc        = mater.MchshortDesc;
                        asnd.Unit         = m.UNIT;
                        asnd.CDate        = FormatHelper.TODateInt(DateTime.Now);
                        asnd.CTime        = FormatHelper.TOTimeInt(DateTime.Now);
                        asnd.CUser        = GetUserCode();
                        asnd.MaintainDate = FormatHelper.TODateInt(DateTime.Now);
                        asnd.MaintainTime = FormatHelper.TOTimeInt(DateTime.Now);
                        asnd.MaintainUser = GetUserCode();
                        asnd.LotNo        = " ";
                        CARTONINVDETAILSN[] sns    = _warehouseFacade.GetCartonInvDetailSn(m.CARTONNO, pick.PickNo);
                        List <string>       snList = new List <string>();
                        if (sns.Length > 0)
                        {
                            foreach (CARTONINVDETAILSN sn in sns)
                            {
                                snList.Add(sn.SN);
                            }
                            Asndetail detail = _warehouseFacade.GetFirstCheckInAsnDetail(snList);
                            if (detail != null)
                            {
                                asnd.ProductionDate = detail.Production_Date;
                                asnd.SupplierLotNo  = detail.Supplier_lotno;
                                asnd.LotNo          = detail.Lotno;
                                asnd.StorageAgeDate = detail.StorageageDate;
                            }
                        }
                        DBDateTime    dbTime     = FormatHelper.GetNowDBDateTime(this.DataProvider);
                        Asndetailitem detailitem = _warehouseFacade.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       = i.ToString();
                        detailitem.Stno         = asn.Stno;
                        detailitem.MCode        = asnd.MCode;
                        detailitem.DqmCode      = asnd.DQMCode;

                        //查找对应的SAP单
                        object[] qtyobjs = _warehouseFacade.GetSAPNOandLinebyMCODE(asn.Invno, asnd.MCode, asnd.DQMCode);
                        if (qtyobjs == null)
                        {
                            this.DataProvider.RollbackTransaction();
                            throw new Exception("入库需求" + asn.Invno + " 没有相关物料" + asnd.MCode + "," + asnd.DQMCode + "或者此行已被取消!");
                        }
                        decimal sub = asnd.Qty;
                        for (int k = 0; k < qtyobjs.Length; k++)
                        {
                            InvoicesDetail invdetail       = qtyobjs[k] as InvoicesDetail;
                            decimal        subNeed         = 0;
                            object         findNeedQTY_old = _warehouseFacade.GetNeedImportQtyOLD(invdetail.InvNo, invdetail.InvLine, asnd.StNo); //找这个invoice行已经导入了多少,和判退多少
                            Asndetailitem  subItemOld      = findNeedQTY_old as Asndetailitem;
                            object         findNeedQTY_now = _warehouseFacade.GetNeedImportQtyNow(invdetail.InvNo, invdetail.InvLine, asnd.StNo); //找这个invoice行已经导入了多少,和判退多少
                            Asndetailitem  subItemNow      = findNeedQTY_now as Asndetailitem;

                            subNeed = invdetail.PlanQty - subItemOld.Qty + (subItemOld.Qty - subItemOld.ReceiveQty) + (subItemOld.ReceiveQty - subItemOld.QcpassQty);
                            subNeed = subNeed - subItemNow.Qty;

                            if (subNeed == 0)
                            {
                                continue;
                            }

                            //如果箱数量大于需求数量差---进行拆分
                            if (sub > subNeed)
                            {
                                sub                   = sub - subNeed; //  sub是剩余的
                                detailitem.Qty        = subNeed;
                                detailitem.Invline    = invdetail.InvLine.ToString();
                                detailitem.Invno      = invdetail.InvNo;
                                detailitem.ActQty     = detailitem.Qty;
                                detailitem.QcpassQty  = detailitem.Qty;
                                detailitem.ReceiveQty = detailitem.Qty;
                                _warehouseFacade.AddAsndetailitem(detailitem);
                            }

                            //如果箱单数量小于等于需求数量差--直接填入
                            else
                            {
                                detailitem.Qty        = sub;
                                detailitem.Invline    = invdetail.InvLine.ToString();
                                detailitem.Invno      = invdetail.InvNo;
                                detailitem.ActQty     = detailitem.Qty;
                                detailitem.QcpassQty  = detailitem.Qty;
                                detailitem.ReceiveQty = detailitem.Qty;
                                _warehouseFacade.AddAsndetailitem(detailitem);
                                sub = 0;
                            }
                            if (sub == 0)
                            {
                                break;
                            }
                        }
                        //如果sub>0,说明导入数量过多,报错
                        if (sub > 0)
                        {
                            this.DataProvider.RollbackTransaction();

                            throw new Exception("箱单导入数量与需求数量不符!");
                        }

                        foreach (CARTONINVDETAILSN sn in sns)
                        {
                            Asndetailsn asnSN = new Asndetailsn();

                            asnSN.CDate        = FormatHelper.TODateInt(DateTime.Now);
                            asnSN.CTime        = FormatHelper.TOTimeInt(DateTime.Now);
                            asnSN.CUser        = GetUserCode();
                            asnSN.MaintainDate = FormatHelper.TODateInt(DateTime.Now);
                            asnSN.MaintainTime = FormatHelper.TOTimeInt(DateTime.Now);
                            asnSN.MaintainUser = GetUserCode();
                            asnSN.Sn           = sn.SN;
                            asnSN.Stline       = i.ToString();
                            asnSN.Stno         = stno;
                            _warehouseFacade.AddAsndetailsn(asnSN);
                        }
                        _InventoryFacade.AddASNDetail(asnd);
                    }
                    else if (mater.MCONTROLTYPE == SAP_CONTROLTYPE.SAP_ITEM_CONTROL_LOT || mater.MCONTROLTYPE == SAP_CONTROLTYPE.SAP_ITEM_CONTROL_NOCONTROL)
                    {
                        ASNDetail asnd = new ASNDetail();
                        asnd.ActQty       = 0;
                        asnd.CustMCode    = custMCode;
                        asnd.DQMCode      = m.DQMCODE;
                        asnd.Qty          = (int)m.QTY;
                        asnd.StLine       = i.ToString();
                        asnd.MCode        = m.MCODE;
                        asnd.StNo         = stno;
                        asnd.Status       = "Release";
                        asnd.MDesc        = mater.MchshortDesc;
                        asnd.Unit         = m.UNIT;
                        asnd.CDate        = FormatHelper.TODateInt(DateTime.Now);
                        asnd.CTime        = FormatHelper.TOTimeInt(DateTime.Now);
                        asnd.CUser        = GetUserCode();
                        asnd.MaintainDate = FormatHelper.TODateInt(DateTime.Now);
                        asnd.MaintainTime = FormatHelper.TOTimeInt(DateTime.Now);
                        asnd.MaintainUser = GetUserCode();
                        asnd.LotNo        = " ";
                        Pickdetailmaterial pickMaterial = (Pickdetailmaterial)_warehouseFacade.GetLotNOInformationFromDQMCODE(m.DQMCODE, pick.PickNo);

                        if (pickMaterial != null)
                        {
                            asnd.ProductionDate = pickMaterial.Production_Date;
                            asnd.SupplierLotNo  = pickMaterial.Supplier_lotno;
                            asnd.LotNo          = pickMaterial.Lotno;
                            asnd.StorageAgeDate = pickMaterial.StorageageDate;
                        }
                        else
                        {
                            throw new Exception(pick.PickNo + "没有找到检料信息!");
                        }
                        _InventoryFacade.AddASNDetail(asnd);

                        DBDateTime    dbTime     = FormatHelper.GetNowDBDateTime(this.DataProvider);
                        Asndetailitem detailitem = _warehouseFacade.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       = i.ToString();
                        detailitem.Stno         = asn.Stno;
                        detailitem.MCode        = asnd.MCode;
                        detailitem.DqmCode      = asnd.DQMCode;

                        //查找对应的SAP单
                        object[] qtyobjs = _warehouseFacade.GetSAPNOandLinebyMCODE(asn.Invno, asnd.MCode, asnd.DQMCode);
                        if (qtyobjs == null)
                        {
                            this.DataProvider.RollbackTransaction();
                            throw new Exception("箱单导入数量与需求数量不符!");
                        }
                        decimal sub = asnd.Qty;
                        for (int k = 0; k < qtyobjs.Length; k++)
                        {
                            InvoicesDetail invdetail       = qtyobjs[k] as InvoicesDetail;
                            decimal        subNeed         = 0;
                            object         findNeedQTY_old = _warehouseFacade.GetNeedImportQtyOLD(invdetail.InvNo, invdetail.InvLine, asnd.StNo); //找这个invoice行已经导入了多少,和判退多少
                            Asndetailitem  subItemOld      = findNeedQTY_old as Asndetailitem;
                            object         findNeedQTY_now = _warehouseFacade.GetNeedImportQtyNow(invdetail.InvNo, invdetail.InvLine, asnd.StNo); //找这个invoice行已经导入了多少,和判退多少
                            Asndetailitem  subItemNow      = findNeedQTY_now as Asndetailitem;

                            subNeed = invdetail.PlanQty - subItemOld.Qty + (subItemOld.Qty - subItemOld.ReceiveQty) + (subItemOld.ReceiveQty - subItemOld.QcpassQty);
                            subNeed = subNeed - subItemNow.Qty;

                            if (subNeed == 0)
                            {
                                continue;
                            }

                            //如果箱数量大于需求数量差---进行拆分
                            if (sub > subNeed)
                            {
                                sub                   = sub - subNeed; //  sub是剩余的
                                detailitem.Qty        = subNeed;
                                detailitem.Invline    = invdetail.InvLine.ToString();
                                detailitem.Invno      = invdetail.InvNo;
                                detailitem.ActQty     = detailitem.Qty;
                                detailitem.QcpassQty  = detailitem.Qty;
                                detailitem.ReceiveQty = detailitem.Qty;
                                _warehouseFacade.AddAsndetailitem(detailitem);
                            }

                            //如果箱单数量小于等于需求数量差--直接填入
                            else
                            {
                                detailitem.Qty        = sub;
                                detailitem.Invline    = invdetail.InvLine.ToString();
                                detailitem.Invno      = invdetail.InvNo;
                                detailitem.ActQty     = detailitem.Qty;
                                detailitem.QcpassQty  = detailitem.Qty;
                                detailitem.ReceiveQty = detailitem.Qty;
                                _warehouseFacade.AddAsndetailitem(detailitem);
                                sub = 0;
                            }
                            if (sub == 0)
                            {
                                break;
                            }
                        }
                        //如果sub>0,说明导入数量过多,报错
                        if (sub > 0)
                        {
                            this.DataProvider.RollbackTransaction();

                            throw new Exception("箱单导入数量与需求数量不符!");
                        }
                    }
                    i++;
                }
                _warehouseFacade.AddAsn(asn);
                WarehouseFacade facade = new WarehouseFacade(base.DataProvider);
                #region 在invinouttrans表中增加一条数据
                //ASN asn = (ASN)domainObject;
                DBDateTime    dbTime1 = FormatHelper.GetNowDBDateTime(this.DataProvider);
                InvInOutTrans trans   = facade.CreateNewInvInOutTrans();
                trans.CartonNO        = string.Empty;
                trans.DqMCode         = string.Empty;
                trans.FacCode         = asn.FacCode;
                trans.FromFacCode     = asn.FromfacCode;
                trans.FromStorageCode = asn.FromstorageCode;
                trans.InvNO           = asn.Invno;
                trans.InvType         = asn.StType;
                trans.LotNo           = string.Empty;
                trans.MaintainDate    = dbTime1.DBDate;
                trans.MaintainTime    = dbTime1.DBTime;
                trans.MaintainUser    = this.GetUserCode();
                trans.MCode           = string.Empty;
                trans.ProductionDate  = 0;
                trans.Qty             = 0;
                trans.Serial          = 0;
                trans.StorageAgeDate  = 0;
                trans.StorageCode     = asn.StorageCode;
                trans.SupplierLotNo   = string.Empty;
                trans.TransNO         = asn.Stno;
                trans.TransType       = "IN";
                trans.Unit            = string.Empty;
                trans.ProcessType     = "UBCreateASN";
                facade.AddInvInOutTrans(trans);
                #endregion
                this.DataProvider.CommitTransaction();
                return(stno);
            }
            catch (Exception ex)
            {
                this.DataProvider.RollbackTransaction();
                throw ex;
            }
        }
        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;
        }