/// <summary>
        /// 更新预售订单
        /// </summary>
        /// <param name="User"></param>
        /// <param name="OrderInfo"></param>
        /// <param name="ErrorInfo"></param>
        /// <returns></returns>
        public static int Order_Update(UserInfo User, Order OrderInfo, out string ErrorInfo)
        {
            ErrorInfo = "";
            LogWriter.WriteLog("PBMIFService.Order_Update:UserName="******",OrderInfo=" + JsonConvert.SerializeObject(OrderInfo));

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

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

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

            //默认业务人员为当前员工
            if (OrderInfo.SalesMan == 0) OrderInfo.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 (OrderInfo.Items == null || OrderInfo.Items.Count == 0) { ErrorInfo = "无订货产品明细!"; return -10; }
            #endregion

            #region 保存订货单头信息
            bll.Model.SalesMan = OrderInfo.SalesMan;
            bll.Model.WipeAmount = OrderInfo.WipeAmount;
            bll.Model.ArriveTime = OrderInfo.ArriveTime < DateTime.Today ? DateTime.Today.AddDays(1) : OrderInfo.ArriveTime;
            bll.Model.WorkList = OrderInfo.WorkList;
            bll.Model.Remark = OrderInfo.Remark;
            #endregion

            #region 循环处理每个订单明细
            foreach (Order.OrderDetail item in OrderInfo.Items)
            {
                if (item.Product == 0) continue;
                if (item.BookQuantity <= 0 && item.ConfirmQuantity <= 0)
                {
                    if (item.DetailID == 0)
                        continue;
                    else
                        bll.DeleteDetail(item.DetailID);
                }

                int quantity = item.BookQuantity == 0 ? item.ConfirmQuantity : item.BookQuantity;

                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 (item.DetailID > 0)
                {
                    PBM_OrderDetail d = bll.GetDetailModel(item.DetailID);

                    d.ConvertFactor = productbll.Model.ConvertFactor == 0 ? 1 : productbll.Model.ConvertFactor;
                    d.BookQuantity = quantity;
                    d.ConfirmQuantity = quantity;
                    d.Remark = item.Remark;
                    bll.UpdateDetail(d);
                }
                else
                {
                    #region 新增商品明细品项
                    PBM_OrderDetail d = new PBM_OrderDetail();

                    d.Product = item.Product;
                    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.BookQuantity = quantity;
                    d.ConfirmQuantity = d.BookQuantity;
                    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.ConfirmQuantity / 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.ConfirmQuantity / p.ConvertFactor)
                - bll.Model.WipeAmount, 2);

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

            return 0;
        }
        /// <summary>
        /// 提交订货单,并输出提交后的订单信息
        /// </summary>
        /// <param name="User"></param>
        /// <param name="OrderID">订货单ID</param>
        /// <param name="OrderInfo">输出:订货单结构</param>
        /// <param name="ErrorInfo">输出:出错信息</param>
        /// <returns>0:成功 小于0:失败</returns>
        public static int Order_Submit(UserInfo User, int OrderID, decimal WipeAmount, List<Order.OrderPayInfo> PayInfoList, out Order OrderInfo, out string ErrorInfo)
        {
            ErrorInfo = "";
            OrderInfo = null;
            LogWriter.WriteLog("PBMIFService.Order_Submit:UserName="******",OrderID=" + OrderID.ToString());

            if (OrderID <= 0) { ErrorInfo = "订货单ID无效"; return -1; }

            PBM_OrderBLL bll = new PBM_OrderBLL(OrderID);
            if (bll.Model == null) { ErrorInfo = "订货单ID无效"; return -1; }
            if (bll.Model.State > 1 || bll.Model.ApproveFlag == 1) { 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.ConfirmQuantity / p.ConvertFactor)
                - bll.Model.WipeAmount, 2);
            bll.Update();

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

            foreach (Order.OrderPayInfo item in PayInfoList)
            {
                PBM_DeliveryPayInfoBLL paybll = new PBM_DeliveryPayInfoBLL();
                paybll.Model.DeliveryID = OrderID;
                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.Submit(User.StaffID);
            if (ret < 0) { ErrorInfo = "订货单提交失败!"; return -1; }

            OrderInfo = new Order(bll.Model.ID);
            return 0;
        }