/// <summary>
        /// 创建包装业务员奖金
        /// </summary>
        /// <param name="bpObj">BP入口参数:会计期间集合</param>
        /// <param name="dsPayment">付款通知单数据集</param>
        /// <param name="bonusCoefficient">奖金系数</param>
        /// <param name="degradationCoefficient">降价系数</param>
        /// <param name="DeliveryExpectDay">基准交期</param>
        private string CreatePackagingOperatorBonus(CalculateOperatorBonus bpObj, DataTable dsPayment, decimal packaingBonusCoefficient)
        {
            if (dsPayment == null || dsPayment.Rows.Count == 0)
            {
                return("");
            }
            var groupOperators             = dsPayment.Rows.Cast <DataRow>().GroupBy(t => t["Operators_Code"].ToString());
            BonusOperatorDocType bonusType = BonusOperatorDocType.Finder.Find("BuzType=1 and IsCreatedByPush=1");

            if (bonusType == null)
            {
                throw new Exception("请设置产品包装业务员单据类型并设置为上游单据推出");
            }
            using (ISession session = Session.Open())
            {
                BonusPackagingDoc bonusProduct = BonusPackagingDoc.Create();
                bonusProduct.DocumentTypeKey = bonusType.Key; //单据类型
                bonusProduct.BusinessDate    = DateTime.Now;  //业务日期
                bonusProduct.Status          = EnumBE.FICommonStatusEnum.Opened;
                if (bpObj.BonusCalcuteList[0].SOBAccountingPeriod != null && bpObj.BonusCalcuteList[0].SOBAccountingPeriod.AccountPeriod != null)
                {
                    bonusProduct.AccountPeriodKey = bpObj.BonusCalcuteList[0].SOBAccountingPeriod.AccountPeriod.AccountingCalendarKey; //会计日历
                }
                bonusProduct.CurrencyKey = Base.Currency.Currency.Finder.Find("Code='C001'").Key;                                      //币种只会为人民币,
                bonusProduct.Oprator     = Base.Context.LoginUser;
                bonusProduct.OprateDate  = DateTime.Now;

                //设置默认行号
                GetProfileValueProxy lineNo = new GetProfileValueProxy();
                lineNo.ProfileCode = "SysLineNo";
                PVDTOData pVTDOData = lineNo.Do();
                string    row       = pVTDOData.ProfileValue;
                //根据业务员分组后的付款通知单数据
                foreach (var groupOperator in groupOperators)
                {
                    decimal sumPayment           = 0; //付款单总额,
                    decimal sumPerformance       = 0; //业绩单总额
                    decimal productBonusTotal    = 0; //新产品奖金合计
                    decimal depreciateBonusTotal = 0; //降价奖金合计
                    decimal bonusTotal           = 0; //奖金合计
                    long    operatorsKey         = Convert.ToInt64(groupOperator.First <DataRow>()["Operators"].ToString());
                    long    dept = Convert.ToInt64(groupOperator.First <DataRow>()["Department"].ToString());
                    BonusPackagingDocLine productLine = BonusPackagingDocLine.Create(bonusProduct); //业务员奖金明细行
                    productLine.DocLineNo = Convert.ToInt32(row);                                   //行号
                    if (operatorsKey > 0)
                    {
                        productLine.OperatorsKey = CBO.HR.Operator.Operators.Finder.FindByID(operatorsKey).Key;//业务员
                    }
                    if (dept > 0)
                    {
                        productLine.DepartmentKey = CBO.HR.Department.Department.Finder.FindByID(dept).Key;//部门
                    }
                    //设置默认行号
                    string subRow = pVTDOData.ProfileValue;
                    foreach (DataRow Operator in groupOperator)
                    {
                        if (Convert.ToInt32(Operator["SrcDocType"].ToString()) != EnumBE.PaySrcDocTypeEnum.Rcv.Value)
                        {
                            continue;
                        }
                        BonusPackagingDocSubLine subLine = BonusPackagingDocSubLine.Create(productLine);
                        subLine.DocLineNo     = Convert.ToInt32(subRow);
                        subLine.OperatorsKey  = CBO.HR.Operator.Operators.Finder.FindByID(operatorsKey).Key; //业务员
                        subLine.DepartmentKey = CBO.HR.Department.Department.Finder.FindByID(dept).Key;      //部门
                        decimal payment = decimal.Parse(Operator["Payment"].ToString());                     //付款单金额,转换成人民币
                        subLine.PaymentMey = payment;                                                        //付款单小类金额
                        sumPayment        += payment;
                        //获得产品系数档案
                        ProductCoefficient coefficient = ProductCoefficient.Finder.Find("ItemMaster.Code='" + Operator["Item_Code"].ToString() + "'");
                        if (coefficient == null)
                        {
                            throw new Exception("料品" + Operator["Item_Code"].ToString() + " " + Operator["Item_Name"].ToString() + "没有维护类别系数档案");
                        }
                        subLine.CategoryCoefficient = coefficient;                                                                                                         //类别系数
                        subLine.PaymentRowCount     = groupOperator.Where <DataRow>(p => p["PaymentID"].ToString() == Operator["PaymentID"].ToString()).Count <DataRow>(); //付款单行数

                        decimal customerCoefficient = 1;
                        //1、如果来源采购订单有接单客户则算取出客户的客户难易系数,否则默认1
                        if (Operator["Customer"].ToString() != "")
                        {
                            CBO.SCM.Customer.Customer customer = CBO.SCM.Customer.Customer.Finder.FindByID(Operator["Customer"].ToString());
                            if (customer != null && customer.DescFlexField.PubDescSeg24 != "")
                            {
                                customerCoefficient = decimal.Parse(customer.DescFlexField.PubDescSeg24);
                            }
                        }
                        subLine.CustomerCoefficient = customerCoefficient;      //客户难易系数
                        subLine.ItemCoefficient     = packaingBonusCoefficient; //小类奖金系数
                        //计算奖金:付款通知单行数*类别系数*客户难易系数 + 付款单小类金额*千分之4*客户难易系数
                        subLine.Bonus = subLine.PaymentRowCount * coefficient.Coefficien * customerCoefficient * +payment * packaingBonusCoefficient * customerCoefficient;
                        //subLine.ExpectMey = //预期奖金
                        subLine.PaymentDocKey = PaymentBE.Payment.Finder.FindByID(Operator["PaymentID"].ToString()).Key;
                        subLine.PaymentDocNo  = Operator["Payment_DocNo"].ToString();
                        subLine.ItemKey       = CBO.SCM.Item.ItemMaster.Finder.FindByID(Operator["Item"].ToString()).Key;
                        subLine.SrcDocLineNo  = Convert.ToInt32(Operator["SrcDocLineNo"].ToString());

                        //降价奖金
                        subRow = (int.Parse(subRow) + int.Parse(pVTDOData.ProfileValue)).ToString();//自增行号
                    }
                    productLine.SumPayment = sumPayment;

                    productLine.BonusTotal = bonusTotal;
                    row = (int.Parse(row) + int.Parse(pVTDOData.ProfileValue)).ToString();//自增行号
                }
                session.Commit();
                return(bonusProduct.DocNo);
            }
        }
        /// <summary>
        /// 创建采购业务员奖金
        /// </summary>
        /// <param name="bpObj">BP入口参数:会计期间集合</param>
        /// <param name="dsPayment">付款通知单数据集</param>
        /// <param name="bonusCoefficient">奖金系数</param>
        /// <param name="degradationCoefficient">降价系数</param>
        /// <param name="DeliveryExpectDay">基准交期</param>
        private string CreateProductOperatorBonus(CalculateOperatorBonus bpObj, DataTable dsPayment, decimal bonusCoefficient, decimal degradationCoefficient, int DeliveryExpectDay)
        {
            if (dsPayment == null || dsPayment.Rows.Count == 0)
            {
                return("");
            }
            var groupOperators             = dsPayment.Rows.Cast <DataRow>().GroupBy(t => t["Operators_Code"].ToString());
            BonusOperatorDocType bonusType = BonusOperatorDocType.Finder.Find("BuzType=0 and IsCreatedByPush=1");

            if (bonusType == null)
            {
                throw new Exception("请设置产品采购业务员单据类型并设置为上游单据推出");
            }
            using (ISession session = Session.Open())
            {
                BonusProductDoc bonusProduct = BonusProductDoc.Create();
                bonusProduct.DocumentTypeKey = bonusType.Key; //单据类型
                bonusProduct.BusinessDate    = DateTime.Now;  //业务日期
                bonusProduct.Status          = EnumBE.FICommonStatusEnum.Opened;
                if (bpObj.BonusCalcuteList[0].SOBAccountingPeriod != null && bpObj.BonusCalcuteList[0].SOBAccountingPeriod.AccountPeriod != null)
                {
                    bonusProduct.AccountPeriodKey = bpObj.BonusCalcuteList[0].SOBAccountingPeriod.AccountPeriod.AccountingCalendarKey; //会计日历
                }
                bonusProduct.CurrencyKey = Base.Currency.Currency.Finder.Find("Code='C001'").Key;                                      //币种只会为人民币,写死
                bonusProduct.Oprator     = Base.Context.LoginUser;
                bonusProduct.OprateDate  = DateTime.Now;

                //设置默认行号
                GetProfileValueProxy lineNo = new GetProfileValueProxy();
                lineNo.ProfileCode = "SysLineNo";
                PVDTOData pVTDOData = lineNo.Do();
                string    row       = pVTDOData.ProfileValue;
                //根据业务员分组后的付款通知单数据
                foreach (var groupOperator in groupOperators)
                {
                    decimal             sumPayment           = 0; //付款单总额,
                    decimal             sumPerformance       = 0; //业绩单总额
                    decimal             productBonusTotal    = 0; //新产品奖金合计
                    decimal             depreciateBonusTotal = 0; //降价奖金合计
                    decimal             bonusTotal           = 0; //奖金合计
                    long                operatorsKey         = Convert.ToInt64(groupOperator.First <DataRow>()["Operators"].ToString());
                    long                dept        = Convert.ToInt64(groupOperator.First <DataRow>()["Department"].ToString());
                    BonusProductDocLine productLine = BonusProductDocLine.Create(bonusProduct); //业务员奖金明细行
                    productLine.DocLineNo = Convert.ToInt32(row);                               //行号
                    if (operatorsKey > 0)
                    {
                        productLine.OperatorsKey = CBO.HR.Operator.Operators.Finder.FindByID(operatorsKey).Key;//业务员
                    }
                    if (dept > 0)
                    {
                        productLine.DepartmentKey = CBO.HR.Department.Department.Finder.FindByID(dept).Key;//部门
                    }
                    //设置默认行号
                    string subRow = pVTDOData.ProfileValue;
                    foreach (DataRow Operator in groupOperator)
                    {
                        if (Convert.ToInt32(Operator["SrcDocType"].ToString()) != EnumBE.PaySrcDocTypeEnum.Rcv.Value)
                        {
                            continue;
                        }
                        BonusProductDocSubLine subLine = BonusProductDocSubLine.Create(productLine);
                        subLine.DocLineNo     = Convert.ToInt32(subRow);
                        subLine.OperatorsKey  = CBO.HR.Operator.Operators.Finder.FindByID(operatorsKey).Key; //业务员
                        subLine.DepartmentKey = CBO.HR.Department.Department.Finder.FindByID(dept).Key;      //部门
                        decimal payment = decimal.Parse(Operator["Payment"].ToString());                     //付款单金额,转换成人民币
                        subLine.Payment = payment;
                        sumPayment     += payment;
                        //获得产品系数档案
                        ProductCoefficient coefficient = ProductCoefficient.Finder.Find("ItemMaster.Code='" + Operator["Item_Code"].ToString() + "'");
                        if (coefficient == null)
                        {
                            throw new Exception("料品" + Operator["Item_Code"].ToString() + " " + Operator["Item_Name"].ToString() + "没有维护产品系数");
                        }
                        subLine.ProductCoefficient    = coefficient; //产品系数
                        subLine.NewProductCoefficient = coefficient; //新产品系数
                        //来源类型入库单取实际入库时间
                        if (Convert.ToInt32(Operator["Item_Code"].ToString()) == 3)
                        {
                            if (Operator["Item_Code"].ToString() != "")
                            {
                                long rcvId = Convert.ToInt64(Operator["Item_Code"].ToString());
                                if (rcvId > 0)
                                {
                                    PM.Rcv.RcvLine rcvLine = PM.Rcv.RcvLine.Finder.FindByID(rcvId);
                                    if (rcvLine != null)
                                    {
                                        subLine.PracticalRcvTime = rcvLine.ConfirmDate;//实际入库时间
                                    }
                                    else
                                    {
                                        throw new Exception("付款通知单" + Operator["Payment_DocNo"].ToString() + "行" + Operator["SrcDocLineNo"].ToString() + "找不到来源入库单");
                                    }
                                }
                            }
                        }

                        //应交货日期取收购单上的应交货日期。
                        //PM.PO.POShipLine shipLine = PM.PO.POShipLine.Finder.FindByID(Operator["POShipLineID"].ToString());
                        //if (shipLine != null)
                        //{
                        //    subLine.DeliveryTime = shipLine.DeliveryDate;//应交货日期
                        //}
                        if (Operator["SPDelivery"].ToString() != "")
                        {
                            subLine.DeliveryTime = DateTime.Parse(Operator["SPDelivery"].ToString());
                        }
                        subLine.DegradationCoefficient = degradationCoefficient; //降价系数,预置参数

                        //计算交付预期与计算奖金相关
                        //1、如果来源采购
                        #region 计算交付预期和奖金,默认1
                        decimal deliveryExpect = 1;
                        //库存采购不考虑逾期,用采购单据类型编号判断;
                        //判断如果逾期,则置为-1
                        if (Operator["DocumentType"].ToString() != "PO03")  ///库存采购单据类型编码目前默认PO03,后期基础数据准备完成后相应改动
                        {
                            //在奖金计算时,如果该批号对应的数量大于在预留表里面的对应批号的数量,说明多余的数量已转自由库存,
                            //需同时计算不考虑逾期的奖金和预留数量对应的受预期影响的奖金两部分
                            if (Operator["Lot"].ToString() != "")
                            {
                                long lot = Convert.ToInt64(Operator["Lot"].ToString());
                                //获取预留表批号
                                UFIDA.U9.ATP.Reserve.Reserve reserve = ATP.Reserve.Reserve.Finder.Find("SupplyLot=" + lot + "");
                                if (reserve != null && reserve.ReserveQtyIU < decimal.Parse(Operator["PayQty"].ToString()))
                                {
                                    //比预留表多余的数量表示已经转自由库存,不考虑预期,计算奖金
                                    subLine.Bonus = decimal.Parse(Operator["FinallyPrice"].ToString()) * (decimal.Parse(Operator["PayQty"].ToString()) - reserve.ReserveQtyIU) * bonusCoefficient * coefficient.Coefficien * coefficient.ProductCoeffien * deliveryExpect;
                                    //实际入库时间-收购单(供应计划)下单日>基准交期,并且大于交货日期,则逾期系数为-1,负奖金,需要扣奖金;
                                    if (subLine.DeliveryTime != null && (subLine.PracticalRcvTime - Convert.ToDateTime(Operator["CreatedOn"].ToString())).Days > DeliveryExpectDay && subLine.PracticalRcvTime > subLine.DeliveryTime)//逾期,扣奖金
                                    {
                                        deliveryExpect         = -1;
                                        subLine.DeliveryExpect = deliveryExpect;
                                        //计算奖金
                                        subLine.Bonus += decimal.Parse(Operator["FinallyPrice"].ToString()) * reserve.ReserveQtyIU * bonusCoefficient * coefficient.Coefficien * coefficient.ProductCoeffien * deliveryExpect;
                                    }
                                    else
                                    {
                                        deliveryExpect         = 1;
                                        subLine.DeliveryExpect = deliveryExpect;
                                        //计算奖金
                                        subLine.Bonus += decimal.Parse(Operator["FinallyPrice"].ToString()) * reserve.ReserveQtyIU * bonusCoefficient * coefficient.Coefficien * coefficient.ProductCoeffien * deliveryExpect;
                                    }
                                }
                                else
                                {
                                    //throw new Exception("付款通知单" + Operator["Payment_DocNo"].ToString() + "行" + Operator["SrcDocLineNo"].ToString() + "入库批号找不到预留表对应批号");
                                    //实际入库时间-收购单(供应计划)下单日>基准交期,并且大于交货日期,则逾期系数为-1,负奖金,需要扣奖金;
                                    if (subLine.DeliveryTime != null && (subLine.PracticalRcvTime - Convert.ToDateTime(Operator["CreatedOn"].ToString())).Days > DeliveryExpectDay && subLine.PracticalRcvTime > subLine.DeliveryTime)//逾期,扣奖金
                                    {
                                        deliveryExpect         = -1;
                                        subLine.DeliveryExpect = deliveryExpect;
                                        //计算奖金
                                        subLine.Bonus = payment * bonusCoefficient * coefficient.Coefficien * coefficient.ProductCoeffien * deliveryExpect;
                                    }
                                    else
                                    {
                                        deliveryExpect         = 1;
                                        subLine.DeliveryExpect = deliveryExpect;
                                        //计算奖金
                                        subLine.Bonus = payment * bonusCoefficient * coefficient.Coefficien * coefficient.ProductCoeffien * deliveryExpect;
                                    }
                                }
                            }
                            else //找不到对应批号,直接判断是否逾期,正常流程基本不存在的
                            {
                                //实际入库时间-收购单(供应计划)下单日>基准交期,并且大于交货日期,则逾期系数为-1,负奖金,需要扣奖金;
                                if (subLine.DeliveryTime != null && (subLine.PracticalRcvTime - Convert.ToDateTime(Operator["CreatedOn"].ToString())).Days > DeliveryExpectDay && subLine.PracticalRcvTime > subLine.DeliveryTime)//逾期,扣奖金
                                {
                                    deliveryExpect         = -1;
                                    subLine.DeliveryExpect = deliveryExpect;
                                    //计算奖金
                                    subLine.Bonus = payment * bonusCoefficient * coefficient.Coefficien * coefficient.ProductCoeffien * deliveryExpect;
                                }
                                else
                                {
                                    deliveryExpect         = 1;
                                    subLine.DeliveryExpect = deliveryExpect;
                                    //计算奖金
                                    subLine.Bonus = payment * bonusCoefficient * coefficient.Coefficien * coefficient.ProductCoeffien * deliveryExpect;
                                }
                            }
                        }
                        else  //库存采购
                        {
                            //库存采购不考虑逾期
                            deliveryExpect         = 1;
                            subLine.DeliveryExpect = deliveryExpect;
                            //计算奖金
                            subLine.Bonus = payment * bonusCoefficient * coefficient.Coefficien * coefficient.ProductCoeffien * deliveryExpect;
                        }

                        #endregion


                        subLine.PaymentDocKey = PaymentBE.Payment.Finder.FindByID(Operator["PaymentID"].ToString()).Key;
                        subLine.PaymentDocNo  = Operator["Payment_DocNo"].ToString();
                        subLine.ItemKey       = CBO.SCM.Item.ItemMaster.Finder.FindByID(Operator["Item"].ToString()).Key;
                        subLine.SrcDocLineNo  = Convert.ToInt32(Operator["SrcDocLineNo"].ToString());

                        //降价奖金
                        ProductDepreciate productDepreciate = ProductDepreciate.Finder.Find("ItemMaster.Code='" + Operator["Item_Code"].ToString() + "'");
                        if (productDepreciate != null)
                        {
                            depreciateBonusTotal += productDepreciate.CanPrice * Decimal.Parse(Operator["PayQty"].ToString()) * degradationCoefficient;
                        }

                        subRow = (int.Parse(subRow) + int.Parse(pVTDOData.ProfileValue)).ToString();//自增行号
                    }
                    productLine.SumPayment           = sumPayment;
                    productLine.SumPerformance       = sumPerformance;
                    productLine.ProductBonusTotal    = productBonusTotal;
                    productLine.DepreciateBonusTotal = depreciateBonusTotal;
                    productLine.BonusTotal           = bonusTotal;
                    row = (int.Parse(row) + int.Parse(pVTDOData.ProfileValue)).ToString();//自增行号
                }
                session.Commit();
                return(bonusProduct.DocNo);
            }
        }