/// <summary>
        /// 验证销售单,当在执行预售送货时,或在收款单变更了销售单明细时,需要调用此接口更新销售单
        /// </summary>
        /// <param name="User"></param>
        /// <param name="DeliveryInfo"></param>
        /// <param name="ErrorInfo"></param>
        /// <returns></returns>
        public static int SaleOut_Update(UserInfo User, Delivery DeliveryInfo, out string ErrorInfo)
        {
            ErrorInfo = "";
            LogWriter.WriteLog("PBMIFService.SaleOut_Update:UserName="******",DeliveryInfo=" + JsonConvert.SerializeObject(DeliveryInfo));

            if (DeliveryInfo.ID == 0) { ErrorInfo = "销售单不存在,请先新增销售单!"; return -1; }

            PBM_DeliveryBLL bll = new PBM_DeliveryBLL(DeliveryInfo.ID);
            if (bll.Model == null) { ErrorInfo = "销售单不存在,请先新增销售单!"; return -1; }
            if (bll.Model.State > 3 || bll.Model.ApproveFlag == 1) { ErrorInfo = "销售单状态不允许执行此操作!"; return -1; }

            if (bll.Model.Supplier == 0) bll.Model.Supplier = DeliveryInfo.Supplier;
            if (bll.Model.Client == 0) bll.Model.Client = DeliveryInfo.Client;

            //如果有送货车辆,默认出货仓库为该车辆所关联仓库
            if (DeliveryInfo.DeliveryVehicle != 0 && DeliveryInfo.SupplierWareHouse == 0)
            {
                CM_Vehicle v = new CM_VehicleBLL(DeliveryInfo.DeliveryVehicle).Model;
                if (v == null) { ErrorInfo = "送货车辆无效!"; return -1; }

                DeliveryInfo.SupplierWareHouse = v.RelateWareHouse;
            }

            //默认业务人员为当前员工
            if (DeliveryInfo.SalesMan == 0) DeliveryInfo.SalesMan = User.StaffID;

            #region 必填字段校验
            if (bll.Model.Supplier == 0) { ErrorInfo = "无效的供货客户!"; return -2; }
            if (User.OwnerType == 3 && bll.Model.Supplier != User.ClientID) { ErrorInfo = "无效的供货客户!"; return -2; }
            if (bll.Model.Client == 0) { ErrorInfo = "无效的购买客户!"; return -2; }

            if (DeliveryInfo.SupplierWareHouse == 0) { ErrorInfo = "无效的供货仓库!"; return -2; }
            if (DeliveryInfo.Items == null || DeliveryInfo.Items.Count == 0) { ErrorInfo = "无销售产品明细!"; return -10; }
            #endregion

            #region 保存销售单头信息
            bll.Model.SalesMan = DeliveryInfo.SalesMan;
            bll.Model.DeliveryVehicle = DeliveryInfo.DeliveryVehicle;
            bll.Model.SupplierWareHouse = DeliveryInfo.SupplierWareHouse;
            bll.Model.WipeAmount = DeliveryInfo.WipeAmount;

            bll.Model.WorkList = DeliveryInfo.WorkList;
            bll.Model.Remark = DeliveryInfo.Remark;
            #endregion

            #region 循环处理每个订单明细
            foreach (Delivery.DeliveryDetail item in DeliveryInfo.Items)
            {
                if (item.Product == 0) continue;
                if (item.DeliveryQuantity <= 0 && item.SignInQuantity <= 0)
                {
                    if (item.DetailID == 0)
                        continue;
                    else
                        bll.DeleteDetail(item.DetailID);
                }

                string lotnumber = item.LotNumber.Trim();

                int quantity = 0;
                if (bll.Model.State == 1)
                    quantity = item.DeliveryQuantity == 0 ? item.SignInQuantity : item.DeliveryQuantity;
                else
                    quantity = item.SignInQuantity;

                string remark = item.Remark;

                PDT_ProductBLL productbll = new PDT_ProductBLL(item.Product);
                if (productbll.Model == null) { ErrorInfo = "无效产品项,产品ID:" + item.Product; return -11; }
                PDT_ProductExtInfo extinfo = productbll.GetProductExtInfo(bll.Model.Supplier);
                if (productbll.Model == null) { ErrorInfo = "产品不在销售商的经营目录中," + productbll.Model.FullName; return -11; }

                if (bll.Model.Classify != 2)
                {
                    //不为退货单时,判断车库存是否够销售
                    int inv_quantity = INV_InventoryBLL.GetProductQuantity(bll.Model.SupplierWareHouse, item.Product, lotnumber);
                    if (quantity > inv_quantity)
                    {
                        ErrorInfo = "产品[" + productbll.Model.FullName + "]库存不足,当前库存:" + inv_quantity.ToString();
                        return -11;
                    }
                }

                if (item.DetailID > 0)
                {
                    PBM_DeliveryDetail d = bll.GetDetailModel(item.DetailID);

                    if (bll.Model.State == 1) d.DeliveryQuantity = quantity;
                    d.SignInQuantity = item.SignInQuantity;
                    d.LotNumber = lotnumber;
                    d.Remark = item.Remark;
                    bll.UpdateDetail(d);
                }
                else
                {
                    #region 新增商品明细品项
                    PBM_DeliveryDetail d = new PBM_DeliveryDetail();

                    d.Product = item.Product;
                    d.LotNumber = lotnumber;
                    d.SalesMode = item.SalesMode == 0 ? 1 : item.SalesMode;     //默认为“销售”

                    d.Price = PDT_StandardPriceBLL.GetSalePrice(bll.Model.Client, bll.Model.Supplier, d.Product);
                    if (d.SalesMode == 1)
                        d.DiscountRate = (item.DiscountRate <= 0 || item.DiscountRate > 1) ? 1 : item.DiscountRate;
                    else
                        d.DiscountRate = 0;

                    d.ConvertFactor = productbll.Model.ConvertFactor == 0 ? 1 : productbll.Model.ConvertFactor;
                    d.DeliveryQuantity = item.DeliveryQuantity;
                    d.SignInQuantity = d.DeliveryQuantity;
                    d.Remark = item.Remark;

                    bll.AddDetail(d);
                    #endregion
                }
            }
            #endregion

            //计算折扣金额
            bll.Model.DiscountAmount = bll.Items.Sum(p => (1 - p.DiscountRate) *
                Math.Round(p.Price * p.ConvertFactor, 2) * p.SignInQuantity / p.ConvertFactor);

            //计算实际销售金额
            bll.Model.ActAmount = Math.Round((bll.Model.Classify == 2 ? -1 : 1) *
                bll.Items.Sum(p => p.DiscountRate * Math.Round(p.Price * p.ConvertFactor, 2) * p.SignInQuantity / p.ConvertFactor)
                - bll.Model.WipeAmount, 2);

            int ret = bll.Update();
            if (ret < 0) { ErrorInfo = "销售单保存失败!"; return ret; }

            return 0;
        }
    private bool Save()
    {
        if (ViewState["Details"] == null)
        {
            return(false);
        }
        ListTable <PBM_DeliveryDetail> Details = (ListTable <PBM_DeliveryDetail>)ViewState["Details"];

        PBM_DeliveryBLL _bll = new PBM_DeliveryBLL((int)ViewState["ID"]);

        pl_detail.GetData(_bll.Model);

        #region 判断必填项
        if (_bll.Model.Supplier == 0)
        {
            MessageBox.Show(this, "请正确选择供货商!");
            return(false);
        }

        if (_bll.Model.SupplierWareHouse == 0)
        {
            MessageBox.Show(this, "请正确选择盘点仓库!");
            return(false);
        }
        #endregion

        #region 循环设置盘点调整数量
        foreach (GridViewRow row in gv_List.Rows)
        {
            int _id = (int)gv_List.DataKeys[row.RowIndex]["ID"];
            PBM_DeliveryDetail d = Details[_id.ToString()];

            if (d == null)
            {
                continue;
            }
            int     quantity       = 0;
            TextBox tbx_Quantity_T = (TextBox)row.FindControl("tbx_Quantity_T");
            TextBox tbx_Quantity_P = (TextBox)row.FindControl("tbx_Quantity_P");

            if (tbx_Quantity_T != null)
            {
                int.TryParse(tbx_Quantity_T.Text, out quantity);
                d.DeliveryQuantity = quantity * d.ConvertFactor;
            }

            if (tbx_Quantity_P != null)
            {
                int.TryParse(tbx_Quantity_P.Text, out quantity);
                d.DeliveryQuantity += quantity;
            }

            DropDownList ddl_Mode = (DropDownList)row.FindControl("ddl_Mode");
            if (ddl_Mode != null && ddl_Mode.SelectedValue == "D")
            {
                int inv_quantity = 0;
                int.TryParse(d["PreInventoryQuantity"], out inv_quantity);
                if (inv_quantity < d.DeliveryQuantity)
                {
                    MessageBox.Show(this, "盘亏数量不能大于当前库存!");
                    return(false);
                }

                d.DeliveryQuantity = -1 * d.DeliveryQuantity;
                d.SignInQuantity   = d.DeliveryQuantity;
            }

            Details.Update(d);
        }
        #endregion

        _bll.Model.DiscountAmount = 0;
        _bll.Model.WipeAmount     = 0;

        //实际成交价
        _bll.Model.ActAmount = Math.Round(Details.GetListItem().Sum(p => Math.Round(p.Price * p.ConvertFactor, 2) * p.DeliveryQuantity / p.ConvertFactor), 2);

        if ((int)ViewState["ID"] != 0)
        {
            //修改
            _bll.Model.UpdateStaff = (int)Session["UserID"];

            #region 保存明细
            foreach (PBM_DeliveryDetail d in Details.GetListItem(ItemState.Modified))
            {
                _bll.UpdateDetail(d);
            }
            #endregion

            if (_bll.Update() == 0)
            {
                return(true);
            }
        }

        return(false);
    }
        /// <summary>
        /// 确认销售并收款
        /// </summary>
        /// <param name="User"></param>
        /// <param name="WipeAmount">优惠金额</param>
        /// <param name="DeliveryID"></param>
        /// <returns></returns>
        public static int SaleOut_Confirm(UserInfo User, int DeliveryID, decimal WipeAmount, List<Delivery.DeliveryPayInfo> PayInfoList, out string ErrorInfo)
        {
            ErrorInfo = "";
            LogWriter.WriteLog("PBMIFService.SaleOut_Confirm:UserName="******",DeliveryID=" + DeliveryID.ToString() +
                ",PayInfoList=" + JsonConvert.SerializeObject(PayInfoList));

            if (DeliveryID <= 0) { ErrorInfo = "销售单ID无效"; return -1; }

            PBM_DeliveryBLL bll = new PBM_DeliveryBLL(DeliveryID);
            if (bll.Model == null) { ErrorInfo = "销售单ID无效"; return -1; }
            if (bll.Model.State > 3) { ErrorInfo = "销售单状态无效"; return -1; }

            if (User.OwnerType == 3 && bll.Model.Supplier != User.ClientID) { ErrorInfo = "不可确认该销售单"; return -2; }

            bll.Model.WipeAmount = WipeAmount;
            bll.Model.ActAmount = Math.Round((bll.Model.Classify == 2 ? -1 : 1) *
                bll.Items.Sum(p => p.DiscountRate * Math.Round(p.Price * p.ConvertFactor, 2) * p.SignInQuantity / p.ConvertFactor)
                - bll.Model.WipeAmount, 2);

            bll.Update();

            if (Math.Abs(PayInfoList.Sum(p => p.Amount) - bll.Model.ActAmount) >= 0.1m)
            {
                ErrorInfo = "收款额与销售金额不符!";
                return -20;
            }

            if (bll.Model.Classify == 1)
            {
                decimal d = PayInfoList.Sum(p => p.PayMode == 11 ? p.Amount : 0);   //余额支付
                if (d > 0)
                {
                    AC_CurrentAccount ac = AC_CurrentAccountBLL.GetByTradeClient(bll.Model.Supplier, bll.Model.Client);
                    if (ac == null || d > ac.PreReceivedAmount)
                    {
                        ErrorInfo = "预收款不够支付!当前预收款余额为:" + ac.PreReceivedAmount.ToString("0.##");
                        return -21;
                    }
                }
            }

            #region 写入收款明细
            //先清除之前的付款信息
            if (bll.GetPayInfoList().Count > 0) bll.ClearPayInfo();

            foreach (Delivery.DeliveryPayInfo item in PayInfoList)
            {
                PBM_DeliveryPayInfoBLL paybll = new PBM_DeliveryPayInfoBLL();
                paybll.Model.DeliveryID = DeliveryID;
                paybll.Model.PayMode = item.PayMode;
                paybll.Model.Amount = item.Amount;
                paybll.Model.Remark = item.Remark;
                paybll.Model.ApproveFlag = 2;
                paybll.Model.InsertStaff = User.StaffID;
                paybll.Add();
            }
            #endregion

            int ret = bll.Confirm(User.StaffID);

            if (ret < 0)
            {
                switch (ret)
                {
                    case -10:
                        ErrorInfo = "库存不足,确认失败!";
                        break;
                    case -20:
                        ErrorInfo = "收款额与销售金额不符!";
                        break;
                    case -21:
                        ErrorInfo = "预收款不够支付!";
                        break;
                    default:
                        ErrorInfo = "销售单确认失败!";
                        break;
                }
                return ret;
            }

            return 0;
        }
    private bool Save()
    {
        if (ViewState["Details"] == null) return false;
        ListTable<PBM_DeliveryDetail> Details = (ListTable<PBM_DeliveryDetail>)ViewState["Details"];

        PBM_DeliveryBLL _bll = new PBM_DeliveryBLL((int)ViewState["ID"]);

        pl_detail.GetData(_bll.Model);

        #region 判断必填项
        if (_bll.Model.Supplier == 0)
        {
            MessageBox.Show(this, "请正确选择供货商!");
            return false;
        }

        if (_bll.Model.SupplierWareHouse == 0)
        {
            MessageBox.Show(this, "请正确选择盘点仓库!");
            return false;
        }
        #endregion

        #region 循环设置盘点调整数量
        foreach (GridViewRow row in gv_List.Rows)
        {
            int _id = (int)gv_List.DataKeys[row.RowIndex]["ID"];
            PBM_DeliveryDetail d = Details[_id.ToString()];

            if (d == null) continue;
            int quantity = 0;
            TextBox tbx_Quantity_T = (TextBox)row.FindControl("tbx_Quantity_T");
            TextBox tbx_Quantity_P = (TextBox)row.FindControl("tbx_Quantity_P");

            if (tbx_Quantity_T != null)
            {
                int.TryParse(tbx_Quantity_T.Text, out quantity);
                d.DeliveryQuantity = quantity * d.ConvertFactor;
            }

            if (tbx_Quantity_P != null)
            {
                int.TryParse(tbx_Quantity_P.Text, out quantity);
                d.DeliveryQuantity += quantity;
            }

            DropDownList ddl_Mode = (DropDownList)row.FindControl("ddl_Mode");
            if (ddl_Mode != null && ddl_Mode.SelectedValue == "D")
            {
                int inv_quantity = 0;
                int.TryParse(d["PreInventoryQuantity"], out inv_quantity);
                if (inv_quantity < d.DeliveryQuantity)
                {
                    MessageBox.Show(this, "盘亏数量不能大于当前库存!");
                    return false;
                }

                d.DeliveryQuantity = -1 * d.DeliveryQuantity;
                d.SignInQuantity = d.DeliveryQuantity;
            }

            Details.Update(d);
        }
        #endregion

        _bll.Model.DiscountAmount = 0;
        _bll.Model.WipeAmount = 0;

        //实际成交价
        _bll.Model.ActAmount = Math.Round(Details.GetListItem().Sum(p => Math.Round(p.Price * p.ConvertFactor, 2) * p.DeliveryQuantity / p.ConvertFactor), 2);

        if ((int)ViewState["ID"] != 0)
        {
            //修改
            _bll.Model.UpdateStaff = (int)Session["UserID"];

            #region 保存明细
            foreach (PBM_DeliveryDetail d in Details.GetListItem(ItemState.Modified))
            {
                _bll.UpdateDetail(d);
            }
            #endregion

            if (_bll.Update() == 0)
            {
                return true;
            }
        }

        return false;
    }