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;
            }
        }