/// <summary>
        /// 计算退税额,
        /// </summary>
        /// <param name="soLine">销售订单行</param>
        /// <param name="orderBom">随单BOM</param>
        /// <returns></returns>
        public static decimal GetTaxCredits(SM.SO.SOLine soLine, OrderBomBE.OrderBomHead.EntityList orderBom)
        {
            decimal taxCredits = 0;//计算的单行退税额
            //退税率:料品私有字段9
            // 成本的减项,根据中类退税率自动算出
            //公式:订单行中类工具子件单位成本*子件用量*订单行中类数量*中类退税率
            //取价退税时,子件最近一次的采购价为美金时,采购费用不退税。
            //订单行中类非进口的工具子件单位成本的计算方式:
            //料品的商品类型:工具子件、包材子件、工具成品(就是指中类)
            //销售订单行的包装工厂如果为外厂包装(委外业务)、内厂包装(自制),展开BOM,卷积子件成本,没BOM报错
            //销售订单行的外厂成品直接取成品采购价格
            decimal taxRefundRate = 0;//退税率

            if (soLine.ItemInfo.ItemIDKey.GetEntity().DescFlexField.PrivateDescSeg9 != "")
            {
                taxRefundRate = Convert.ToDecimal(soLine.ItemInfo.ItemIDKey.GetEntity().DescFlexField.PrivateDescSeg9);
            }
            else
            {
                throw new Exception("请维护销售订单" + soLine.SO.DocNo + "行" + soLine.DocLineNo + "对应料品退税率");
            }
            decimal purchasePrice = 0; //采购价
            int     currencyType  = 0; //采购币种标记

            //if (soLine.ItemInfo.ItemIDKey.GetEntity().StockCategory == null)
            //{
            //    throw new Exception("销售订单" + soLine.SO.DocNo + "行" + soLine.DocLineNo + "对应的料品没有设置料品分类");
            //}
            //if (soLine.DescFlexField.PubDescSeg18 == "03") ////如果是成品采购,根据包装工厂为外厂成品区分
            //if (soLine.ItemInfo.ItemIDKey.GetEntity().StockCategory != null && soLine.ItemInfo.ItemIDKey.GetEntity().StockCategory.Code == "01") //中类直接取最近采购价格
            if (orderBom == null) //随单BOM为空,即计算成品采购;
            {
                DataSet ds  = new DataSet();
                string  sql = "select top 1 B.OrderPriceTC,A.TC from PM_POLine b left join PM_PurchaseOrder a on b.PurchaseOrder=a.id where b.ItemInfo_ItemID=" + soLine.ItemInfo.ItemID.ID + " and Org=" + UFIDA.U9.Base.Context.LoginOrg.ID + " order by a.BusinessDate desc";
                UFSoft.UBF.Util.DataAccess.DataAccessor.RunSQL(UFSoft.UBF.Util.DataAccess.DataAccessor.GetConn(), sql, null, out ds);
                purchasePrice = CalculatePurchasePrice(0, soLine, null, ds, ref currencyType);
                return(purchasePrice * soLine.OrderByQtyTU * taxRefundRate);
            }
            else //计算子件退税额
            {
                //获取子项
                DataSet ds  = new DataSet();
                string  sql = "";
                foreach (OrderBomHead line in orderBom)
                {
                    if (line.SubKey != null && line.SubKey.Code == "FDBQ") //防盗标签子件不退税
                    {
                        continue;
                    }
                    sql = "select top 1 B.OrderPriceTC,A.TC from PM_POLine b left join PM_PurchaseOrder a on b.PurchaseOrder=a.id where b.ItemInfo_ItemID=" + line.SubKey.ID + " and Org=" + UFIDA.U9.Base.Context.LoginOrg.ID + " order by a.BusinessDate desc";
                    UFSoft.UBF.Util.DataAccess.DataAccessor.RunSQL(UFSoft.UBF.Util.DataAccess.DataAccessor.GetConn(), sql, null, out ds);
                    purchasePrice = CalculatePurchasePrice(0, soLine, line, ds, ref currencyType);
                    //taxCredits += purchasePrice * line.OrderBomHead.Dosage * soLine.OrderByQtyTU * taxRefundRate;
                    //子件成本=子件的采购单价*需求数量,需求数量=子件的用量比例*中类的销售数量(即为随单BOM行的需要数量)
                    taxCredits += purchasePrice * line.NeedNumber * taxRefundRate;
                }
            }
            return(taxCredits);
        }
        /// <summary>
        /// 计算防盗标签费
        /// </summary>
        /// <param name="soLine"></param>
        /// <returns></returns>
        public static decimal GetLabelling(SM.SO.SOLine soLine, OrderBomBE.OrderBomHead.EntityList orderBom)
        {
            decimal labelling = 0;
            //BOM中防盗标签子件,则根据【防盗标签】最近一次采购价,币种,不退税。
            //BOM中没有防盗标签子件,则手工输入。
            //中类需要一个字段来标识需要防盗标签,子件通过物料分类来区分防盗标签
            //销售行防盗标签费扩展字段
            //if (soLine.DescFlexField.PrivateDescSeg7 == "")
            //{
            //    //throw new Exception("请维护销售单" + soLine.SO.DocNo + "行" + soLine.DocLineNo + "对应的防盗标签费");
            //}
            decimal purchasePrice = 0;//采购价
            DataSet ds            = new DataSet();
            string  sql           = "";

            if (orderBom != null)
            {
                foreach (OrderBomHead line in orderBom)
                {
                    ds  = new DataSet();
                    sql = "";
                    if (line.SubKey != null && line.SubKey.Code == "FDBQ")
                    {
                        //取最近采购价
                        sql = "select top 1 B.OrderPriceTC,A.TC from PM_POLine b left join PM_PurchaseOrder a on b.PurchaseOrder=a.id where b.ItemInfo_ItemID=" + line.SubKey.ID + " and Org=" + UFIDA.U9.Base.Context.LoginOrg.ID + " order by a.BusinessDate desc";
                        UFSoft.UBF.Util.DataAccess.DataAccessor.RunSQL(UFSoft.UBF.Util.DataAccess.DataAccessor.GetConn(), sql, null, out ds);
                        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0]["OrderPriceTC"].ToString() != "")
                        {
                            purchasePrice = Convert.ToDecimal(ds.Tables[0].Rows[0]["OrderPriceTC"].ToString());
                            if (Convert.ToInt64(ds.Tables[0].Rows[0]["TC"].ToString()) != soLine.SO.TC.ID)//币种不同,汇率转换为销售订单币种
                            {
                                //purchasePrice =
                                UFIDA.U9.Cust.CBO.RateBE.OperationalRateLine rateLine = UFIDA.U9.Cust.CBO.RateBE.OperationalRateLine.Finder.Find("StartDate<='" + DateTime.Now + "' and EndDate>='" + DateTime.Now + "'" +
                                                                                                                                                 "and OperationalRateHead.IsCurrency=" + Convert.ToInt64(ds.Tables[0].Rows[0]["TC"].ToString()) + " and OperationalRateHead.NotCurrency=" + soLine.SO.TC.ID + " and OperationalRateHead.Org=" + UFIDA.U9.Base.Context.LoginOrg.ID);
                                if (rateLine == null)
                                {
                                    UFIDA.U9.Base.Currency.Currency isCurenty = UFIDA.U9.Base.Currency.Currency.Finder.FindByID(Convert.ToInt64(ds.Tables[0].Rows[0]["TC"].ToString()));
                                    throw new Exception("原币" + isCurenty.Name + "对目标币" + soLine.SO.TC.Name + "的汇率未设置,请在基础设置-业务汇率表中设置");
                                }
                                purchasePrice = purchasePrice * rateLine.Parities;
                            }
                        }
                        labelling += purchasePrice * line.NeedNumber;
                    }
                }
            }
            if (labelling == 0)//未取到防盗标签费,取销售订单行
            {
                labelling = decimal.Parse(soLine.DescFlexField.PrivateDescSeg7);
            }

            return(labelling);
        }
        /// <summary>
        /// 计算防盗标签费,已包含进包材采购成本
        /// </summary>
        /// <param name="soLine"></param>
        /// <returns></returns>
        public static decimal GetLabelling(GS.FT.ShipPlanDetailBE.ShipPlanDetailLine shipLine, OrderBomBE.OrderBomHead.EntityList orderBom)
        {
            decimal labelling = 0;
            //一段时间内,防盗标签子件对应的已出运确认的明细单中对应的中类实际使用的防盗标签子件数量报表,
            //如果生产的即生产领用消耗数量,如果是委外则是委外发料消耗数量。

            decimal purchasePrice = 0;//采购价
            DataSet ds            = new DataSet();
            string  sql           = "";

            if (orderBom != null)
            {
                foreach (OrderBomHead line in orderBom)
                {
                    ds  = new DataSet();
                    sql = "";
                    if (line.SubKey != null && line.SubKey.Code == "FDBQ")
                    {
                        //取最近采购价
                        sql = "select top 1 B.OrderPriceTC,A.TC from PM_POLine b left join PM_PurchaseOrder a on b.PurchaseOrder=a.id where b.ItemInfo_ItemID=" + line.SubKey.ID + " and Org=" + UFIDA.U9.Base.Context.LoginOrg.ID + " order by a.BusinessDate desc";
                        UFSoft.UBF.Util.DataAccess.DataAccessor.RunSQL(UFSoft.UBF.Util.DataAccess.DataAccessor.GetConn(), sql, null, out ds);
                        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0]["OrderPriceTC"].ToString() != "")
                        {
                            purchasePrice = Convert.ToDecimal(ds.Tables[0].Rows[0]["OrderPriceTC"].ToString());
                            if (Convert.ToInt64(ds.Tables[0].Rows[0]["TC"].ToString()) != shipLine.ShipPlanDetailHead.Currency.ID)//币种不同,汇率转换为销售订单币种
                            {
                                //purchasePrice =
                                UFIDA.U9.Cust.CBO.RateBE.OperationalRateLine rateLine = UFIDA.U9.Cust.CBO.RateBE.OperationalRateLine.Finder.Find("StartDate<='" + DateTime.Now + "' and EndDate>='" + DateTime.Now + "'" +
                                                                                                                                                 "and OperationalRateHead.IsCurrency=" + Convert.ToInt64(ds.Tables[0].Rows[0]["TC"].ToString()) + " and OperationalRateHead.NotCurrency=" + shipLine.ShipPlanDetailHead.Currency.ID + " and OperationalRateHead.Org=" + UFIDA.U9.Base.Context.LoginOrg.ID);
                                if (rateLine == null)
                                {
                                    UFIDA.U9.Base.Currency.Currency isCurenty = UFIDA.U9.Base.Currency.Currency.Finder.FindByID(Convert.ToInt64(ds.Tables[0].Rows[0]["TC"].ToString()));
                                    throw new Exception("原币" + isCurenty.Name + "对目标币" + shipLine.ShipPlanDetailHead.Currency.Name + "的汇率未设置,请在基础设置-业务汇率表中设置");
                                }
                                purchasePrice = purchasePrice * rateLine.Parities;
                            }
                        }
                        labelling += purchasePrice * line.NeedNumber;
                    }
                }
            }
            return(labelling);
        }
        /// <summary>
        /// 计算产品采购成本
        /// </summary>
        /// <param name="shipLine">销售行</param>
        /// <param name="rmbCost">人民币采购成本</param>
        /// <param name="dollorCost">美元采购成本</param>
        /// <returns></returns>
        public static decimal GetProductCost(GS.FT.ShipPlanDetailBE.ShipPlanDetailLine shipLine, OrderBomBE.OrderBomHead.EntityList orderBom, ref decimal rmbCost, ref decimal dollorCost)
        {
            decimal productCost = 0;
            // 取【批号装配关系表】中的中类批号对应的成品或子件批号价格。

            //全部纯成品采购:取中类采购合同价格。包装工厂判断
            //明细单行中类对应的中类成品采购合同价*明细单行中类数量

            //全部成品委外:
            //公式:明细单行中类对应的子件采购合价*子件数量
            //1)先算子件数量(含放的余量)
            //2)子件单位成本*子件数量
            //	先算明细单行成本金额,再汇总到明细单头。
            decimal purchasePrice = 0;

            SM.SO.SOLine soLine = SM.SO.SOLine.Finder.FindByID(shipLine.SrcLineID);
            if (orderBom == null || orderBom.Count == 0) //随单BOM为空,即计算成品采购;
            {
                purchasePrice = GetPurchasePrice(0, shipLine, null);
                productCost   = purchasePrice * shipLine.Qty;
            }
            else //子件退税额
            {
                foreach (OrderBomBE.OrderBomHead bomLine in orderBom)
                {
                    if (bomLine.SubKey.StockCategory.Code != "02")//不为工具小类,不计算成本
                    {
                        continue;
                    }
                    purchasePrice = GetPurchasePrice(1, shipLine, bomLine);
                    //先算子件数量
                    //2)再算子件成本=子件合同单价*子件数量
                    decimal sumQty = bomLine.NeedNumber / soLine.OrderByQtyTU * shipLine.Qty;//子件数量:
                }
            }

            return(productCost);
        }
        /// <summary>
        /// 计算包装采购成本,
        /// </summary>
        /// <param name="shipLine"></param>
        /// <returns></returns>
        public static decimal GetProcurementCost(GS.FT.ShipPlanDetailBE.ShipPlanDetailLine shipLine, OrderBomBE.OrderBomHead.EntityList orderBom)
        {
            decimal procurementCost = 0;
            //全部成品采购,无包装BOM:取中类上包装采购价格。
            //明细单行中类对应的中类包装价*订单行中类数量
            //全部成品委外。
            //公式:明细单行中类对应的子件采购合价*子件数量
            //1)先算子件数量(含放的余量)
            //2)子件单位成本*子件数量
            //先算明细单行成本金额,再汇总到明细单头。
            decimal purchasePrice = 0;

            SM.SO.SOLine soLine = SM.SO.SOLine.Finder.FindByID(shipLine.SrcLineID);
            if (orderBom == null) //随单BOM为空,即计算成品采购;
            {
                //purchasePrice = GetPurchasePrice(0, shipLine, null);
                purchasePrice = 0;
            }
            else //子件退税额
            {
                foreach (OrderBomBE.OrderBomHead bomLine in orderBom)
                {
                    if (bomLine.SubKey.StockCategory.Code != "03")//不为包材小类,不计算包装成本
                    {
                        continue;
                    }
                    purchasePrice = GetPurchasePrice(1, shipLine, bomLine);
                    //先算子件数量
                    //2)再算子件成本=子件合同单价*子件数量
                    decimal sumQty = bomLine.NeedNumber / soLine.OrderByQtyTU * shipLine.Qty;//子件数量:
                }
            }

            return(procurementCost);
        }
        /// <summary>
        /// 计算退税额,
        /// </summary>
        /// <param name="shipLine"></param>
        /// <param name="orderBom"></param>
        /// <returns></returns>
        public static decimal GetTaxCredits(GS.FT.ShipPlanDetailBE.ShipPlanDetailLine shipLine, OrderBomBE.OrderBomHead.EntityList orderBom)
        {
            decimal taxCredits = 0;
            //1、全部成品委外公式:明细单行中类工具子件采购合同成本(不含税)*中类退税率。
            //   1)先算子件数量
            //   2)再算子件成本=子件合同单价*子件数量。
            //如果是全部成品采购时,退税公式:
            //明细单行中类采购合同成本(不含税)*中类退税率
            //是否退税的判断:
            //1)进口件采购不退税。
            //2)在KJ组织标记,子件和供应商绑定时(货源表)设置是否为进口件,则子件合同存在该组合的采购时,不退税。(如果
            //GJ存在,也可以使用该原则)
            //3)所有HK组织的采购不退税。
            decimal purchasePrice = 0;

            if (shipLine.SrcLineID <= 0)
            {
                throw new Exception("出运明细单" + shipLine.ShipPlanDetailHead.DocNo + "行" + shipLine.RowNo + "来源为空");
            }
            SM.SO.SOLine soLine = SM.SO.SOLine.Finder.FindByID(shipLine.SrcLineID);
            if (soLine == null)
            {
                throw new Exception("出运明细单" + shipLine.ShipPlanDetailHead.DocNo + "行" + shipLine.RowNo + "来源为空");
            }
            decimal taxRefundRate = 0;//退税率

            if (soLine.ItemInfo.ItemIDKey.GetEntity().DescFlexField.PrivateDescSeg9 != "")
            {
                taxRefundRate = Convert.ToDecimal(soLine.ItemInfo.ItemIDKey.GetEntity().DescFlexField.PrivateDescSeg9);
            }
            else
            {
                throw new Exception("请维护销售订单" + soLine.SO.DocNo + "行" + soLine.DocLineNo + "对应料品退税率");
            }
            //if (soLine.DescFlexField.PubDescSeg18 == "03") ////如果是成品采购,根据包装工厂为外厂成品区分
            if (orderBom == null) //随单BOM为空,即计算成品采购;
            {
                ////先根据销售订单号和行号查询采购订单行
                //PM.PO.POLine.EntityList poLineList = PM.PO.POLine.Finder.FindAll("DescFlexSegments.PrivateDescSeg1='" + shipLine.SrcDocNo + "' and DescFlexSegments.PrivateDescSeg2='" + shipLine.SrcLineNo + "' and ItemInfo.ItemCode='"+shipLine.Item.Code+"");
                //// var max = poLineList.Max<PM.PO.POLine, DateTime>(p => p.PurchaseOrder.BusinessDate);
                ////取出最近销售订单行对应的最近采购行,可能只会有一个
                //var lasterPM = poLineList.OrderByDescending<PM.PO.POLine, DateTime>(p => p.PurchaseOrder.BusinessDate).FirstOrDefault<PM.PO.POLine>();
                //if (lasterPM != null)
                //{
                //    if (lasterPM.PurchaseOrder.DescFlexField.PrivateDescSeg2 != "J003") //HK组织不退税
                //    {
                //        return 0;
                //    }
                //    //中类采购合同成本(不含税)*中类退税率
                //    if (taxRefundRate == 0)
                //        throw new Exception("出运明细单" + shipLine.ShipPlanDetailHead.DocNo + "行" + shipLine.RowNo + "对应的料品退税率不能为0");
                //    taxCredits = lasterPM.NetMnyTC / lasterPM.PurQtyPU * taxRefundRate;
                //}
                purchasePrice = GetPurchasePrice(0, shipLine, null);
                taxCredits    = purchasePrice * shipLine.Qty * taxRefundRate;
            }
            else //子件退税额
            {
                foreach (OrderBomBE.OrderBomHead bomLine in orderBom)
                {
                    ////取出最近销售订单行对应的最近采购行,可能只会有一个
                    //var lineByPoList = poLineList.Where<PM.PO.POLine>(p => p.ItemInfo.ItemCode == line.SubKey.Code);
                    ////取最近一次采购价
                    //var lasterPM = lineByPoList.OrderByDescending<PM.PO.POLine, DateTime>(p => p.PurchaseOrder.BusinessDate).FirstOrDefault<PM.PO.POLine>();
                    //if (lasterPM != null)
                    //{
                    //    if (lasterPM.PurchaseOrder.DescFlexField.PrivateDescSeg2 != "J003")
                    //    {
                    //        //HK组织不退税
                    //    }
                    //    else
                    //    {
                    //        //在KJ组织标记,子件和供应商绑定时(货源表)设置是否为进口件,则子件合同存在该组合的采购时,不退税。
                    //        //(如果GJ存在,也可以使用该原则)

                    //        //暂时无法判断进口件,

                    //        //明细单行中类工具子件采购合同成本(不含税)*中类退税率。
                    //        //先算子件数量
                    //        //2)再算子件成本=子件合同单价*子件数量
                    //        decimal price = line.NeedNumber / soLine.OrderByQtyTU * shipLine.Qty ;//子件数量:
                    //        taxCredits += lasterPM.NetMnyTC / lasterPM.PurQtyPU * price * taxRefundRate;
                    //    }
                    //}
                    purchasePrice = GetPurchasePrice(0, shipLine, bomLine);
                    //先算子件数量
                    //2)再算子件成本=子件合同单价*子件数量
                    decimal sumQty = bomLine.NeedNumber / soLine.OrderByQtyTU * shipLine.Qty;//子件数量:
                    taxCredits += purchasePrice * sumQty * taxRefundRate;
                }
            }
            return(taxCredits);
        }
        /// <summary>
        /// 计算工具采购成本
        /// </summary>
        /// <param name="soLine">销售行</param>
        /// <param name="rmbCost">人民币采购成本</param>
        /// <param name="dollorCost">美元采购成本</param>
        /// <returns></returns>
        public static decimal GetProductCost(SM.SO.SOLine soLine, ref decimal rmbCost, ref decimal dollorCost, OrderBomBE.OrderBomHead.EntityList orderBom)
        {
            decimal productCost = 0;
            //如果成品采购:
            //取成品采购最近一次采购价,如果读取不到,则手工输入;
            //如果是子件采购:
            //根据小类最近一次采购价、币别取价。
            //公式:订单行中类对应的子件采购单价*子件数量
            //1)先算子件数量
            //2)子件单位成本*子件数量
            //先算订单行成本金额,再汇总到订单头
            //委外/生产,工具子件/包装子件采购成本取价:
            //1)无BOM时,手工填写工具子件\包材子件采购成本。
            //2)有BOM时,老物料优先取最新实际成本(最新价表),如果取不到,手工填写工具子件\包材子件采购成本。
            decimal purchaseCost  = 0;
            decimal purchasePrice = 0; //采购价
            int     currencyType  = 0; //采购币种标记

            //if (soLine.ItemInfo.ItemIDKey.GetEntity().StockCategory.Code == "01") //中类直接取最近采购价格
            //if (soLine.DescFlexField.PubDescSeg18 == "03") ////如果是成品采购,
            if (orderBom == null || orderBom.Count == 0) //随单BOM为空,取中类采购价
            {
                DataSet ds  = new DataSet();
                string  sql = "select top 1 B.OrderPriceTC,A.TC from PM_POLine b left join PM_PurchaseOrder a on b.PurchaseOrder=a.id where b.ItemInfo_ItemID=" + soLine.ItemInfo.ItemID.ID + " and Org=" + UFIDA.U9.Base.Context.LoginOrg.ID + " order by a.BusinessDate desc";
                UFSoft.UBF.Util.DataAccess.DataAccessor.RunSQL(UFSoft.UBF.Util.DataAccess.DataAccessor.GetConn(), sql, null, out ds);
                purchasePrice = CalculatePurchasePrice(2, soLine, null, ds, ref currencyType);
                //取成品采购最近一次采购价,如果读取不到,则取销售订单行成品成本字段值
                if (purchasePrice == 0 && soLine.DescFlexField.PrivateDescSeg23 != "")
                {
                    purchasePrice = decimal.Parse(soLine.DescFlexField.PrivateDescSeg23);
                    if (soLine.SO.TC.ID == 1)
                    {
                        currencyType = 1;
                    }
                    else if (soLine.SO.TC.ID == 9)
                    {
                        currencyType = 9;
                    }
                }
                if (currencyType == 1)
                {
                    rmbCost = purchasePrice;
                }
                else if (currencyType == 9)
                {
                    dollorCost = purchasePrice;
                }
                purchaseCost = rmbCost + dollorCost;
                //return purchaseCost;
            }
            else
            {
                //获取子项
                DataSet ds  = new DataSet();
                string  sql = "";
                foreach (OrderBomHead line in orderBom)
                {
                    if (line.SubKey != null && line.SubKey.StockCategory != null && line.SubKey.StockCategory.Code == "02")//工具子件
                    {
                        ds  = new DataSet();
                        sql = "select top 1 B.OrderPriceTC,A.TC from PM_POLine b left join PM_PurchaseOrder a on b.PurchaseOrder=a.id where b.ItemInfo_ItemID=" + line.SubKey.ID + " and Org=" + UFIDA.U9.Base.Context.LoginOrg.ID + " order by a.BusinessDate desc";
                        UFSoft.UBF.Util.DataAccess.DataAccessor.RunSQL(UFSoft.UBF.Util.DataAccess.DataAccessor.GetConn(), sql, null, out ds);
                        purchasePrice = CalculatePurchasePrice(2, soLine, line, ds, ref currencyType);
                        if (currencyType == 1)
                        {
                            rmbCost += purchasePrice;
                        }
                        else if (currencyType == 9)
                        {
                            dollorCost += purchasePrice;
                        }
                    }
                }
            }

            //币种转换
            if (dollorCost != 0 && soLine.SO.TC.ID == 1)
            {
                UFIDA.U9.Cust.CBO.RateBE.OperationalRateLine rateLine = UFIDA.U9.Cust.CBO.RateBE.OperationalRateLine.Finder.Find("StartDate<='" + DateTime.Now + "' and EndDate>='" + DateTime.Now + "'" +
                                                                                                                                 "and OperationalRateHead.IsCurrency=9 and OperationalRateHead.NotCurrency=1 and OperationalRateHead.Org=" + UFIDA.U9.Base.Context.LoginOrg.ID);
                if (rateLine == null)
                {
                    throw new Exception("美元对人民币的汇率未设置,请在基础设置-业务汇率表中设置");
                }
                productCost = dollorCost * rateLine.Parities + rmbCost;
            }
            else if (rmbCost != 0 && soLine.SO.TC.ID == 9)
            {
                UFIDA.U9.Cust.CBO.RateBE.OperationalRateLine rateLine = UFIDA.U9.Cust.CBO.RateBE.OperationalRateLine.Finder.Find("StartDate<='" + DateTime.Now + "' and EndDate>='" + DateTime.Now + "'" +
                                                                                                                                 "and OperationalRateHead.IsCurrency=1 and OperationalRateHead.NotCurrency=9 and OperationalRateHead.Org=" + UFIDA.U9.Base.Context.LoginOrg.ID);
                if (rateLine == null)
                {
                    throw new Exception("人民币对美元的汇率未设置,请在基础设置-业务汇率表中设置");
                }
                productCost = dollorCost + rmbCost * rateLine.Parities;
            }

            return(productCost);
        }
        /// <summary>
        /// 计算包装采购成本
        /// </summary>
        /// <param name="soLine"></param>
        /// <returns></returns>
        public static decimal GetProcurementCost(SM.SO.SOLine soLine, OrderBomBE.OrderBomHead.EntityList orderBom)
        {
            decimal procurementCost = 0;//包装采购成本
            //委外/生产,包装子件采购成本取价:
            //1)无BOM时,手工填写【包材汇总成本】。--2014725销售订单维护
            //2)有BOM时,老物料优先取最新实际成本(最新价表),多个价格,按照熟高,如果取不到,方式2种:
            //—在【包材汇总成本】处输入金额,则成本预测按照汇总成本预测。
            //—按照成本预测子件取价逻辑。
            //解释:包材分类:泡壳、纸材,按照包材的分类维护余量比例及上限量档案,即某一类包材的余量比例是多少
            //只有人民币采购
            decimal sumCost = 0;//包材汇总成本

            if (soLine.DescFlexField.PrivateDescSeg22 != "")
            {
                sumCost = decimal.Parse(soLine.DescFlexField.PrivateDescSeg22);
            }
            decimal purchasePrice = 0;                   //采购价
            int     currencyType  = 0;                   //采购币种标记

            if (orderBom == null || orderBom.Count == 0) //随单BOM为空,从销售订单行扩展字段【包材汇总成本】中取
            {
                return(sumCost);
            }
            else
            {
                //获取子项
                DataSet ds  = new DataSet();
                string  sql = "";
                foreach (OrderBomHead line in orderBom)
                {
                    if (line.SubKey != null && line.SubKey.StockCategory != null)
                    {
                        if (line.SubKey.StockCategory.Code != "03")//不为包材小类,不计算包装成本
                        {
                            continue;
                        }
                        sql = "select top 1 B.OrderPriceTC,A.TC from PM_POLine b left join PM_PurchaseOrder a on b.PurchaseOrder=a.id where b.ItemInfo_ItemID=" + line.SubKey.ID + " and Org=" + UFIDA.U9.Base.Context.LoginOrg.ID + " order by a.BusinessDate desc";
                        UFSoft.UBF.Util.DataAccess.DataAccessor.RunSQL(UFSoft.UBF.Util.DataAccess.DataAccessor.GetConn(), sql, null, out ds);
                        purchasePrice    = CalculatePurchasePrice(1, soLine, line, ds, ref currencyType);
                        procurementCost += purchasePrice * line.NeedNumber;
                    }
                }
            }
            #region 废弃代码
            //if (soLine.DescFlexField.PubDescSeg18 == "03") ////如果是成品采购,
            ////if (soLine.ItemInfo.ItemIDKey.GetEntity().StockCategory.Code == "01") //成品直接取最近采购价格
            //{
            //    DataSet ds = new DataSet();
            //    string sql = "select top 1 B.OrderPriceTC,A.TC from PM_POLine b left join PM_PurchaseOrder a on b.PurchaseOrder=a.id where b.ItemInfo_ItemID=" + soLine.ItemInfo.ItemID.ID + " and Org=" + UFIDA.U9.Base.Context.LoginOrg.ID + " order by a.BusinessDate desc";
            //    UFSoft.UBF.Util.DataAccess.DataAccessor.RunSQL(UFSoft.UBF.Util.DataAccess.DataAccessor.GetConn(), sql, null, out ds);
            //    if (ds.Tables[0].Rows.Count > 0)
            //    {
            //        procurementCost = Convert.ToDecimal(ds.Tables[0].Rows[0]["OrderPriceTC"].ToString());
            //        return procurementCost;//应该为采购价*料品扩展字段包装单位成本*销售订单数量
            //    }
            //    else
            //    {
            //        //throw new Exception("请设置销售订单" + soLine.SO.DocNo + "行" + soLine.DocLineNo + "包材汇总成本");
            //    }
            //}
            //else if(orderBom != null)
            //{
            //    decimal price = 0;
            //    if(soLine.DescFlexField.PrivateDescSeg7 != "")
            //    {
            //        //throw new Exception("请设置销售订单" + soLine.SO.DocNo + "行" + soLine.DocLineNo + "包装成本费用");
            //        price = decimal.Parse(soLine.DescFlexField.PrivateDescSeg7);
            //        foreach (OrderBomLine line in orderBom.OrderBomLine)
            //        {
            //            procurementCost += soLine.OrderByQtyTU * price * line.ProcurementQty;
            //        }
            //    }

            //}
            #endregion
            return(procurementCost);
        }
        /// <summary>
        /// 为实体Dto赋值
        /// </summary>
        /// <param name="SO">销售订单集合</param>
        /// <param name="SoLine">销售订单行集合与销售订单集合互斥</param>
        /// <param name="dotList">成本预测实体DTo</param>
        private List <CostForecastBE.CostForecastDTO> SetCostDto(List <SM.SO.SO.EntityKey> SO, List <SM.SO.SOLine.EntityKey> SoLine)
        {
            List <CostForecastBE.CostForecastDTO> dtoList = new List <CostForecastBE.CostForecastDTO>();//订单成本预测实体集合

            #region 参数声明
            CostForecastBE.CostForecastDTO dto    = null; //订单成本预测实体dto
            CostForecastBE.CostForecastDTO dtoOne = null; //成品成本预测实体dto
            //获取所需预置参数
            CalculateDtoFieldExtend.GetPresetParameter();
            #endregion

            //整单成本预测
            if (SO != null && SO.Count > 0)
            {
                //从UI得到的销售订单应该是已经按照销售订单过滤了的
                SM.SO.SO soEntity = null;
                using (ISession session = Session.Open())
                {
                    foreach (SM.SO.SO.EntityKey soKey in SO)                        //遍历销售订单集合
                    {
                        //得到对应销售订单
                        soEntity = soKey.GetEntity();
                        if (soEntity == null)
                        {
                            throw new Exception("请确认查询方式是否设置为整单成本预测");
                            //continue;
                        }
                        dto              = new CostForecastBE.CostForecastDTO();
                        dto.SONo         = soEntity;                                       //赋值销售订单
                        dto.ForecastType = AllEnumBE.CostForecastTypeEnum.AllCostForecast; //预测类型整单成本预测
                        if (soEntity.DescFlexField.PrivateDescSeg6 != "")
                        {
                            dto.OceanFreight = decimal.Parse(soEntity.DescFlexField.PrivateDescSeg6);//国际费用
                        }
                        if (soEntity.DescFlexField.PrivateDescSeg8 != "")
                        {
                            dto.OtherFee = decimal.Parse(soEntity.DescFlexField.PrivateDescSeg8);//其他国外费用
                        }
                        if (soEntity.DescFlexField.PrivateDescSeg5 != "")
                        {
                            dto.DomesticFee = decimal.Parse(soEntity.DescFlexField.PrivateDescSeg5);//国内运费
                        }
                        if (soEntity.DescFlexField.PrivateDescSeg7 != "")
                        {
                            dto.OtherDomesticFee = decimal.Parse(soEntity.DescFlexField.PrivateDescSeg7);//其他国内费用
                        }
                        #region 计算整单体积
                        decimal allVolume = 0;
                        foreach (SM.SO.SOLine soLineEntity in soEntity.SOLines)    //遍历销售订单行集合计算整单体积
                        {
                            //bool isCalculateFreight = 0;//
                            //单行的体积=单行的箱数*料品.体积
                            //单行的箱数=订单数量/料品.装箱数量
                            //判断料品的装箱数量与装箱体积是否为空
                            UFIDA.U9.Cust.GS.FT.ItemSubTableBE.ItemSubTable itemSub = UFIDA.U9.Cust.GS.FT.ItemSubTableBE.ItemSubTable.Finder.Find("ItemMaster=" + soLineEntity.ItemInfo.ItemID.ID + "");
                            //if (itemSub == null)
                            //{
                            //    throw new Exception("请录入" + soLineEntity.SO.DocNo + "行" + soLineEntity.DocLineNo + "对应料品的装箱数量");
                            //}
                            //if(itemSub.BoxNumber ==0)
                            //{
                            //    throw new Exception("请录入" + soLineEntity.SO.DocNo + "行" + soLineEntity.DocLineNo + "对应料品的装箱数量");
                            //}
                            //if (itemSub.GrossWeight == 0)
                            //{
                            //    throw new Exception("请录入" + soLineEntity.SO.DocNo + "行" + soLineEntity.DocLineNo + "对应料品的毛重");
                            //}
                            //if (itemSub.BoxLong == 0)
                            //{
                            //    throw new Exception("请录入" + soLineEntity.SO.DocNo + "行" + soLineEntity.DocLineNo + "对应料品的装箱长度");
                            //}
                            //if (itemSub.BoxWidth == 0)
                            //{
                            //    throw new Exception("请录入" + soLineEntity.SO.DocNo + "行" + soLineEntity.DocLineNo + "对应料品的装箱宽度");
                            //}
                            //if (itemSub.BoxHight == 0)
                            //{
                            //    throw new Exception("请录入" + soLineEntity.SO.DocNo + "行" + soLineEntity.DocLineNo + "对应料品的装箱高度");
                            //}
                            //decimal itemQty = decimal.Parse(soLineEntity.ItemInfo.ItemIDKey.GetEntity().DescFlexField.PrivateDescSeg3);//料品装箱数量
                            //decimal itemRough = decimal.Parse(soLineEntity.ItemInfo.ItemIDKey.GetEntity().DescFlexField.PrivateDescSeg2);//料品毛重
                            if (itemSub != null)
                            {
                                decimal itemVolume = itemSub.BoxLong * itemSub.BoxWidth * itemSub.BoxHight;//料品体积
                                if (itemSub.BoxNumber == 0)
                                {
                                    allVolume += soLineEntity.OrderByQtyTU * itemVolume;//整单体积
                                }
                                else
                                {
                                    allVolume += (soLineEntity.OrderByQtyTU / itemSub.BoxNumber) * itemVolume;//整单体积
                                }
                            }
                        }
                        #endregion

                        #region 遍历得到销售订单行
                        string dtoDemo = "";
                        foreach (SM.SO.SOLine soLineEntity in soEntity.SOLines)    //遍历销售订单行集合
                        {
                            string dtoOneDemo = "";
                            UFIDA.U9.Cust.GS.FT.ItemSubTableBE.ItemSubTable itemSub = UFIDA.U9.Cust.GS.FT.ItemSubTableBE.ItemSubTable.Finder.Find("ItemMaster=" + soLineEntity.ItemInfo.ItemID.ID + "");
                            //计算共用值
                            decimal itemQty    = 0; //料品装箱数量
                            decimal itemRough  = 0; //料品毛重
                            decimal itemVolume = 0; //料品体积
                            if (itemSub != null)
                            {
                                itemQty    = itemSub.BoxNumber;                                     //料品装箱数量
                                itemRough  = itemSub.GrossWeight;                                   //料品毛重
                                itemVolume = itemSub.BoxLong * itemSub.BoxWidth * itemSub.BoxHight; //料品体积
                            }
                            decimal rmbCost    = 0;                                                 //工具人民币采购成本
                            decimal dollorCost = 0;                                                 //工具美元采购成本
                            decimal allFee     = 0;                                                 //成品各项费用

                            dtoOne = new CostForecastBE.CostForecastDTO();
                            dtoOne.ForecastType = AllEnumBE.CostForecastTypeEnum.OneCostForecast;     //预测类型
                            dtoOne.SONo         = soEntity;                                           //销售订单
                            dtoOne.SOLine       = soLineEntity;                                       //销售订单行
                            dtoOne.Brokerage    = CalculateDtoFieldExtend.GetBrokerage(soLineEntity); //佣金
                            dtoOne.Discount     = CalculateDtoFieldExtend.GetDiscount(soLineEntity);  //折扣
                            if (itemQty == 0 || itemRough == 0 || itemVolume == 0)
                            {
                                dtoOne.OceanFreight = 0;
                                dtoOne.DomesticFee  = 0;
                            }
                            else
                            {
                                dtoOne.OceanFreight = CalculateDtoFieldExtend.GetOceanFreight(itemQty, itemRough, itemVolume, soLineEntity);           //海运费
                                dtoOne.DomesticFee  = CalculateDtoFieldExtend.GetDomesticFee(itemQty, itemRough, itemVolume, allVolume, soLineEntity); //国内运费
                            }
                            dtoOne.OtherFee         = CalculateDtoFieldExtend.GetOtherOceanFreight(soLineEntity);                                      //其他国外费
                            dtoOne.OtherDomesticFee = CalculateDtoFieldExtend.GetOtherOceanFreight(soLineEntity);                                      //其他国内费

                            //随单BOM
                            OrderBomBE.OrderBomHead.EntityList orderBom = OrderBomBE.OrderBomHead.Finder.FindAll("OrderLine=@SOLine", new OqlParam(soLineEntity.ID));
                            dtoOne.TaxCredits        = CalculateDtoFieldExtend.GetTaxCredits(soLineEntity, orderBom);                               //退税额
                            dtoOne.ProcurementCost   = CalculateDtoFieldExtend.GetProcurementCost(soLineEntity, orderBom);                          //包装采购成本
                            dtoOne.LabourCharges     = CalculateDtoFieldExtend.GetLabourCharges(soLineEntity);                                      //包装人工费用
                            dtoOne.ProductCost       = CalculateDtoFieldExtend.GetProductCost(soLineEntity, ref rmbCost, ref dollorCost, orderBom); //产品采购成本
                            dtoOne.RMBProductCost    = rmbCost;                                                                                     //工具人民币采购成本
                            dtoOne.DollorProductCost = dollorCost;                                                                                  //工具美元采购成本
                            dtoOne.AdministrativeFee = CalculateDtoFieldExtend.GetAdministrativeFee(soLineEntity);                                  //管理费
                            dtoOne.Interest          = CalculateDtoFieldExtend.GetInterest(soLineEntity);                                           //利息
                            dtoOne.MouldFee          = CalculateDtoFieldExtend.GetMouldFee(soLineEntity);                                           //模具费‘
                            dtoOne.Labelling         = CalculateDtoFieldExtend.GetLabelling(soLineEntity, orderBom);                                //防盗标签费
                            dtoOne.CreditInsurance   = CalculateDtoFieldExtend.GetCreditInsurance(soLineEntity);                                    //信用保险费
                            dtoOne.BankCharge        = CalculateDtoFieldExtend.GetBankCharge(soLineEntity);                                         //银行费用

                            //dtoOne.OrderBomHead = orderBom;//随单Bom
                            dtoOne.ForecastTime = DateTime.Now;
                            dtoOne.ForecastMan  = UFIDA.U9.Base.Context.LoginUser;

                            //成品预测利润:各中类成品外销额-成品各项费用+成品退税额
                            allFee = dtoOne.Brokerage + dtoOne.Discount + dtoOne.OceanFreight + dtoOne.OtherFee + dtoOne.DomesticFee + dtoOne.OtherDomesticFee +
                                     dtoOne.ProcurementCost + dtoOne.LabourCharges + dtoOne.ProductCost + dtoOne.AdministrativeFee + dtoOne.Interest + dtoOne.MouldFee +
                                     dto.Labelling + dtoOne.CreditInsurance + dtoOne.BankCharge;
                            dtoOne.ProductForecastRate = soLineEntity.TotalMoneyTC - allFee + dto.TaxCredits;
                            dtoOne.ProductRate         = dto.ProductForecastRate / soLineEntity.TotalMoneyTC;
                            //备注
                            dtoOne.Demo = dtoOneDemo;
                            dtoList.Add(dtoOne);

                            #region 汇总到整单成本预测
                            dto.Brokerage         += dtoOne.Brokerage;
                            dto.Discount          += dtoOne.Discount;
                            dto.OceanFreight      += dtoOne.OceanFreight;//海运费
                            dto.TaxCredits        += dtoOne.TaxCredits;
                            dto.ProcurementCost   += dtoOne.ProcurementCost;
                            dto.LabourCharges     += dtoOne.LabourCharges;
                            dto.ProductCost       += dtoOne.ProductCost;
                            dto.RMBProductCost    += dtoOne.RMBProductCost;
                            dto.DollorProductCost += dtoOne.DollorProductCost;
                            dto.AdministrativeFee += dtoOne.AdministrativeFee;
                            dto.Interest          += dtoOne.Interest;
                            dto.MouldFee          += dtoOne.MouldFee;
                            dto.Labelling         += dtoOne.Labelling;
                            dto.CreditInsurance   += dtoOne.CreditInsurance;
                            dto.BankCharge        += dtoOne.BankCharge;
                            dto.ListForecastRate  += dto.ProductForecastRate;
                            //备注
                            dto.Demo = "";
                            //dto.SOLine = soLineEntity;
                            //随单BOM


                            #endregion
                            //得到销售订单行soLineEntity
                            #region 计算成本预测字段值
                            //根据销售订单行得到随单Bom表
                            OrderBomHead bomHead = OrderBomHead.Finder.Find("");
                            #endregion
                        }
                        dto.ListRate     = dto.ProductForecastRate / soEntity.TotalMoneyAC; //整单利润率
                        dto.ForecastTime = DateTime.Now;                                    //预测时间
                        dto.ForecastMan  = UFIDA.U9.Base.Context.LoginUser;                 //预测人

                        dtoList.Add(dto);

                        //soEntity.DescFlexField.PrivateDescSeg7 = dto.OceanFreight.ToString();
                        #endregion
                    }
                    session.Commit();
                }
            }
            else if (SoLine != null && SoLine.Count > 0)
            {
                using (Session seesion = Session.Open())
                {
                    //成品成本预测
                    //从UI得到的销售订单行应该是已经按照销售订单行过滤了的
                    foreach (SM.SO.SOLine.EntityKey soLineKey in SoLine)          //遍历销售订单行集合
                    {
                        //得到销售订单行
                        //SM.SO.SOLine soLineEntity = soLineKey.GetEntity();
                        SM.SO.SOLine line = SM.SO.SOLine.Finder.FindByID(soLineKey.ID);
                        if (line == null)
                        {
                            throw new Exception("请确认查询方案设置为成品成本预测");
                        }
                        SM.SO.SOLine soLineEntity = line;

                        #region 计算整单体积
                        decimal allVolume = 0;
                        foreach (SM.SO.SOLine soLine in soLineEntity.SO.SOLines)    //遍历销售订单行集合计算整单体积
                        {
                            //单行的体积=单行的箱数*料品.体积
                            //单行的箱数=订单数量/料品.装箱数量
                            //判断料品的装箱数量与装箱体积是否为空

                            ItemSubTable itemsub = ItemSubTable.Finder.Find("ItemMaster=" + soLine.ItemInfo.ItemID.ID);
                            //if(itemsub == null)
                            //    throw new Exception("请维护" + soLine.SO.DocNo + "行" + soLine.DocLineNo + "对应料品子表装箱长度、装箱宽度、装箱高度、装箱数量、毛重字段");
                            //if(itemsub.BoxNumber == 0)
                            //{
                            //    throw new Exception("请录入" + soLine.SO.DocNo + "行" + soLine.DocLineNo + "对应料品的装箱数量");
                            //}
                            //if (itemsub.GrossWeight ==0 )
                            //{
                            //    throw new Exception("请录入" + soLine.SO.DocNo + "行" + soLine.DocLineNo + "对应料品的毛重");
                            //}
                            //if (itemsub.BoxLong == 0)
                            //{
                            //    throw new Exception("请录入" + soLine.SO.DocNo + "行" + soLine.DocLineNo + "对应料品的装箱长度");
                            //}
                            //if (itemsub.BoxWidth == 0)
                            //{
                            //    throw new Exception("请录入" + soLine.SO.DocNo + "行" + soLine.DocLineNo + "对应料品的装箱宽度");
                            //}
                            //if (itemsub.BoxHight == 0)
                            //{
                            //    throw new Exception("请录入" + soLine.SO.DocNo + "行" + soLine.DocLineNo + "对应料品的装箱高度");
                            //}
                            if (itemsub != null)
                            {
                                decimal itemQty    = itemsub.BoxNumber;                                     //料品装箱数量
                                decimal itemRough  = itemsub.GrossWeight;                                   //料品毛重
                                decimal itemVolume = itemsub.BoxLong * itemsub.BoxWidth * itemsub.BoxHight; //料品体积
                                allVolume += (soLine.OrderByQtyTU / itemQty) * itemVolume;                  //整单体积
                            }
                        }
                        #endregion

                        #region 计算成本预测字段值
                        //根据销售订单行得到随单Bom表
                        //计算共用值
                        ItemSubTable itemsub1    = ItemSubTable.Finder.Find("ItemMaster=" + soLineEntity.ItemInfo.ItemID.ID);
                        int          itemQty1    = 0; //料品装箱数量
                        decimal      itemRough1  = 0; //料品毛重
                        decimal      itemVolume1 = 0; //料品体积
                        if (itemsub1 != null)
                        {
                            itemQty1    = itemsub1.OutBoxNumber;                             //料品装箱数量
                            itemRough1  = itemsub1.GrossWeight;                              //料品毛重
                            itemVolume1 = itemsub1.Length * itemsub1.Width * itemsub1.Hight; //料品体积
                        }
                        decimal rmbCost    = 0;                                              //工具人民币采购成本
                        decimal dollorCost = 0;                                              //工具美元采购成本
                        decimal allFee     = 0;                                              //成品各项费用

                        dtoOne = new CostForecastBE.CostForecastDTO();
                        dtoOne.ForecastType = AllEnumBE.CostForecastTypeEnum.OneCostForecast;     //预测类型
                        dtoOne.SONo         = soLineEntity.SO;                                    //销售订单
                        dtoOne.SOLine       = soLineEntity;                                       //销售订单行
                        dtoOne.Brokerage    = CalculateDtoFieldExtend.GetBrokerage(soLineEntity); //佣金
                        dtoOne.Discount     = CalculateDtoFieldExtend.GetDiscount(soLineEntity);  //折扣
                        if (itemQty1 == 0 || itemRough1 == 0 || itemVolume1 == 0)
                        {
                            dtoOne.OceanFreight = 0;
                            dtoOne.DomesticFee  = 0;
                        }
                        else
                        {
                            dtoOne.OceanFreight = CalculateDtoFieldExtend.GetOceanFreight(itemQty1, itemRough1, itemVolume1, soLineEntity);           //海运费
                            dtoOne.DomesticFee  = CalculateDtoFieldExtend.GetDomesticFee(itemQty1, itemRough1, itemVolume1, allVolume, soLineEntity); //国内运费
                        }
                        dtoOne.OtherFee         = CalculateDtoFieldExtend.GetOtherOceanFreight(soLineEntity);                                         //其他国外费
                        dtoOne.OtherDomesticFee = CalculateDtoFieldExtend.GetOtherOceanFreight(soLineEntity);                                         //其他国内费
                        //随单BOM
                        OrderBomBE.OrderBomHead.EntityList orderBom = OrderBomBE.OrderBomHead.Finder.FindAll("OrderLine=@BomMaster", new OqlParam(soLineEntity.ID));
                        dtoOne.TaxCredits        = CalculateDtoFieldExtend.GetTaxCredits(soLineEntity, orderBom);                               //退税额
                        dtoOne.ProcurementCost   = CalculateDtoFieldExtend.GetProcurementCost(soLineEntity, orderBom);                          //包装采购成本
                        dtoOne.LabourCharges     = CalculateDtoFieldExtend.GetLabourCharges(soLineEntity);                                      //包装人工费用
                        dtoOne.ProductCost       = CalculateDtoFieldExtend.GetProductCost(soLineEntity, ref rmbCost, ref dollorCost, orderBom); //产品采购成本
                        dtoOne.RMBProductCost    = rmbCost;                                                                                     //工具人民币采购成本
                        dtoOne.DollorProductCost = dollorCost;                                                                                  //工具美元采购成本
                        dtoOne.AdministrativeFee = CalculateDtoFieldExtend.GetAdministrativeFee(soLineEntity);                                  //管理费
                        dtoOne.Interest          = CalculateDtoFieldExtend.GetInterest(soLineEntity);                                           //利息
                        dtoOne.MouldFee          = CalculateDtoFieldExtend.GetMouldFee(soLineEntity);                                           //模具费‘
                        dtoOne.Labelling         = CalculateDtoFieldExtend.GetLabelling(soLineEntity, orderBom);                                //防盗标签费
                        dtoOne.CreditInsurance   = CalculateDtoFieldExtend.GetCreditInsurance(soLineEntity);                                    //信用保险费
                        dtoOne.BankCharge        = CalculateDtoFieldExtend.GetBankCharge(soLineEntity);                                         //银行费用

                        //dtoOne.OrderBomHead = orderBom;//随单Bom
                        dtoOne.ForecastTime = DateTime.Now;
                        dtoOne.ForecastMan  = UFIDA.U9.Base.Context.LoginUser;

                        //成品预测利润:各中类成品外销额-成品各项费用+成品退税额
                        allFee = dtoOne.Brokerage + dtoOne.Discount + dtoOne.OceanFreight + dtoOne.OtherFee + dtoOne.DomesticFee + dtoOne.OtherDomesticFee +
                                 dtoOne.ProcurementCost + dtoOne.LabourCharges + dtoOne.ProductCost + dtoOne.AdministrativeFee + dtoOne.Interest + dtoOne.MouldFee +
                                 dtoOne.Labelling + dtoOne.CreditInsurance + dtoOne.BankCharge;
                        dtoOne.ProductForecastRate = soLineEntity.TotalMoneyTC - allFee + dtoOne.TaxCredits;
                        dtoOne.ProductRate         = dtoOne.ProductForecastRate / soLineEntity.TotalMoneyTC;

                        //反写销售订单行预测状态
                        if (soLineEntity.DescFlexField.PrivateDescSeg18 == "")
                        {
                            soLineEntity.DescFlexField.PrivateDescSeg18 = "Y";
                        }
                        //else if (soLineEntity.DescFlexField.PrivateDescSeg18 == "N")
                        //{
                        //    soLineEntity.DescFlexField.PrivateDescSeg18 = "M";
                        //}
                        dtoList.Add(dtoOne);
                        #endregion
                    }
                    seesion.Commit();
                }
            }
            else
            {
                throw new Exception("未选择任何需要预测的订单");
            }
            return(dtoList);
            //CreatEntity(dtoList);
        }
        /// <summary>
        /// 为实体Dto赋值
        /// </summary>
        /// <param name="SO">销售订单集合</param>
        /// <param name="SoLine">销售订单行集合与销售订单集合互斥</param>
        /// <param name="dotList">成本预测实体DTo</param>
        private List <ShipCostForecastBE.ShipCostForecastDTO> SetCostDto(List <GS.FT.ShipPlanDetailBE.ShipPlanDetailHead.EntityKey> ShiPlan, List <GS.FT.ShipPlanDetailBE.ShipPlanDetailLine.EntityKey> ShipLine)
        {
            List <ShipCostForecastBE.ShipCostForecastDTO> dtoList = new List <ShipCostForecastBE.ShipCostForecastDTO>();//订单成本预测实体集合

            #region 参数声明
            ShipCostForecastBE.ShipCostForecastDTO dto    = null; //明细单成本预测实体dto
            ShipCostForecastBE.ShipCostForecastDTO dtoOne = null; //明细单成品成本预测实体dto
            //获取所需预置参数
            CalculateDtoFieldShipExtend.GetPresetParameter();
            #endregion

            //整单成本预测
            if (ShiPlan != null && ShiPlan.Count > 0)
            {
                //从UI得到的销售订单应该是已经按照销售订单过滤了的
                GS.FT.ShipPlanDetailBE.ShipPlanDetailHead shipEntity = null;
                using (ISession session = Session.Open())
                {
                    foreach (GS.FT.ShipPlanDetailBE.ShipPlanDetailHead.EntityKey shipKey in ShiPlan)                        //遍历销售订单集合
                    {
                        //得到对应明细订单
                        shipEntity = shipKey.GetEntity();
                        if (shipEntity == null)
                        {
                            continue;
                        }
                        dto                  = new ShipCostForecastBE.ShipCostForecastDTO();
                        dto.ShipPlan         = shipEntity;                                     //赋值出运明细单
                        dto.ForecastType     = AllEnumBE.CostForecastTypeEnum.AllCostForecast; //预测类型整单成本预测
                        dto.OceanFreight     = shipEntity.OceanFreight;
                        dto.OtherFee         = shipEntity.OherFee;
                        dto.DomesticFee      = shipEntity.InlandFreight;
                        dto.OtherDomesticFee = shipEntity.OtherInland;
                        #region 遍历得到明细订单行
                        foreach (GS.FT.ShipPlanDetailBE.ShipPlanDetailLine shipLineEntity in shipEntity.ShipPlanDetailLine)    //遍历出运明细单行集合
                        {
                            //计算共用值
                            decimal rmbCost    = 0; //工具人民币采购成本
                            decimal dollorCost = 0; //工具美元采购成本
                            decimal allFee     = 0; //成品各项费用

                            dtoOne = new ShipCostForecastBE.ShipCostForecastDTO();
                            dtoOne.ForecastType     = AllEnumBE.CostForecastTypeEnum.OneCostForecast;                   //预测类型
                            dtoOne.ShipPlan         = shipEntity;                                                       //销售订单
                            dtoOne.ShipLine         = shipLineEntity;                                                   //销售订单行
                            dtoOne.Brokerage        = CalculateDtoFieldShipExtend.GetBrokerage(shipLineEntity);         //佣金
                            dtoOne.Discount         = CalculateDtoFieldShipExtend.GetDiscount(shipLineEntity);          //折扣
                            dtoOne.OceanFreight     = CalculateDtoFieldShipExtend.GetOceanFreight(shipLineEntity);      //海运费
                            dtoOne.OtherFee         = CalculateDtoFieldShipExtend.GetOtherOceanFreight(shipLineEntity); //其他国外费
                            dtoOne.DomesticFee      = CalculateDtoFieldShipExtend.GetDomesticFee(shipLineEntity);       //国内运费
                            dtoOne.OtherDomesticFee = CalculateDtoFieldShipExtend.GetOtherOceanFreight(shipLineEntity); //其他国内费
                            //随单BOM
                            OrderBomBE.OrderBomHead.EntityList orderBom = OrderBomBE.OrderBomHead.Finder.FindAll("OrderLine=@SOLine", new OqlParam(shipLineEntity.SrcLineID));
                            dtoOne.TaxCredits        = CalculateDtoFieldShipExtend.GetTaxCredits(shipLineEntity, orderBom);                               //退税额
                            dtoOne.ProcurementCost   = CalculateDtoFieldShipExtend.GetProcurementCost(shipLineEntity, orderBom);                          //包装采购成本
                            dtoOne.LabourCharges     = CalculateDtoFieldShipExtend.GetLabourCharges(shipLineEntity);                                      //包装人工费用
                            dtoOne.ProductCost       = CalculateDtoFieldShipExtend.GetProductCost(shipLineEntity, orderBom, ref rmbCost, ref dollorCost); //产品采购成本
                            dtoOne.RMBProductCost    = rmbCost;                                                                                           //工具人民币采购成本
                            dtoOne.DollorProductCost = dollorCost;                                                                                        //工具美元采购成本
                            dtoOne.AdministrativeFee = CalculateDtoFieldShipExtend.GetAdministrativeFee(shipLineEntity);                                  //管理费
                            dtoOne.Interest          = CalculateDtoFieldShipExtend.GetInterest(shipLineEntity);                                           //利息
                            dtoOne.MouldFee          = CalculateDtoFieldShipExtend.GetMouldFee(shipLineEntity);                                           //模具费‘
                            dtoOne.Labelling         = CalculateDtoFieldShipExtend.GetLabelling(shipLineEntity, orderBom);                                //防盗标签费
                            dtoOne.CreditInsurance   = CalculateDtoFieldShipExtend.GetCreditInsurance(shipLineEntity);                                    //信用保险费
                            dtoOne.BankCharge        = CalculateDtoFieldShipExtend.GetBankCharge(shipLineEntity);                                         //银行费用

                            dtoOne.ForecastTime = DateTime.Now;
                            dtoOne.ForecastMan  = UFIDA.U9.Base.Context.LoginUser;

                            //成品预测利润:各中类成品外销额-成品各项费用+成品退税额
                            allFee = dtoOne.Brokerage + dtoOne.Discount + dtoOne.OceanFreight + dtoOne.OtherFee + dtoOne.DomesticFee + dtoOne.OtherDomesticFee +
                                     dtoOne.ProcurementCost + dtoOne.LabourCharges + dtoOne.ProductCost + dtoOne.AdministrativeFee + dtoOne.Interest + dtoOne.MouldFee +
                                     dto.Labelling + dtoOne.CreditInsurance + dtoOne.BankCharge;
                            dtoOne.ProductForecastRate = shipLineEntity.TotalTax - allFee + dto.TaxCredits;
                            dtoOne.ProductRate         = dto.ProductForecastRate / shipLineEntity.TotalTax;
                            //备注
                            dtoOne.Demo = "";
                            dtoList.Add(dtoOne);

                            #region 汇总到整单成本预测
                            dto.Brokerage         += dtoOne.Brokerage;
                            dto.Discount          += dtoOne.Discount;
                            dto.OceanFreight      += dtoOne.OceanFreight;//海运费
                            dto.TaxCredits        += dtoOne.TaxCredits;
                            dto.ProcurementCost   += dtoOne.ProcurementCost;
                            dto.LabourCharges     += dtoOne.LabourCharges;
                            dto.ProductCost       += dtoOne.ProductCost;
                            dto.RMBProductCost    += dtoOne.RMBProductCost;
                            dto.DollorProductCost += dtoOne.DollorProductCost;
                            dto.AdministrativeFee += dtoOne.AdministrativeFee;
                            dto.Interest          += dtoOne.Interest;
                            dto.MouldFee          += dtoOne.MouldFee;
                            dto.Labelling         += dtoOne.Labelling;
                            dto.CreditInsurance   += dtoOne.CreditInsurance;
                            dto.BankCharge        += dtoOne.BankCharge;
                            dto.ListForecastRate  += dto.ProductForecastRate;
                            //备注
                            dto.Demo = ",";
                            //dto.SOLine = soLineEntity;
                            //随单BOM


                            #endregion
                            //得到销售订单行soLineEntity
                            #region 计算成本预测字段值
                            //根据销售订单行得到随单Bom表
                            OrderBomHead bomHead = OrderBomHead.Finder.Find("");
                            #endregion
                        }
                        //dto.ListRate = dto.ProductForecastRate / soEntity.TotalMoneyAC;//整单利润率
                        dto.ForecastTime = DateTime.Now;                    //预测时间
                        dto.ForecastMan  = UFIDA.U9.Base.Context.LoginUser; //预测人

                        dtoList.Add(dto);

                        shipEntity.DescFlexField.PrivateDescSeg7 = dto.OceanFreight.ToString();
                        #endregion
                    }
                    session.Commit();
                }
            }
            else if (ShipLine != null && ShipLine.Count > 0)
            {
                using (Session seesion = Session.Open())
                {
                    //成品成本预测
                    //从UI得到的销售订单行应该是已经按照销售订单行过滤了的
                    foreach (GS.FT.ShipPlanDetailBE.ShipPlanDetailLine.EntityKey shipLineKey in ShipLine)          //遍历出运明细单行集合
                    {
                        //得到出运明细行
                        //if (shipLineKey.GetEntity() == null)
                        //    throw new Exception("请在列表选择出运明细行信息");
                        ShipPlanDetailBE.ShipPlanDetailLine line = ShipPlanDetailBE.ShipPlanDetailLine.Finder.FindByID(shipLineKey.ID);
                        if (line == null)
                        {
                            throw new Exception("请在列表选择出运明细行信息");
                        }
                        GS.FT.ShipPlanDetailBE.ShipPlanDetailLine shipLineEntity = line;

                        #region 计算成本预测字段值

                        decimal rmbCost    = 0; //工具人民币采购成本
                        decimal dollorCost = 0; //工具美元采购成本
                        decimal allFee     = 0; //成品各项费用

                        dtoOne = new ShipCostForecastBE.ShipCostForecastDTO();
                        dtoOne.ForecastType     = AllEnumBE.CostForecastTypeEnum.OneCostForecast;                   //预测类型
                        dtoOne.ShipPlan         = shipLineEntity.ShipPlanDetailHead;                                //销售订单
                        dtoOne.ShipLine         = shipLineEntity;                                                   //销售订单行
                        dtoOne.Brokerage        = CalculateDtoFieldShipExtend.GetBrokerage(shipLineEntity);         //佣金
                        dtoOne.Discount         = CalculateDtoFieldShipExtend.GetDiscount(shipLineEntity);          //折扣
                        dtoOne.OceanFreight     = CalculateDtoFieldShipExtend.GetOceanFreight(shipLineEntity);      //海运费
                        dtoOne.OtherFee         = CalculateDtoFieldShipExtend.GetOtherOceanFreight(shipLineEntity); //其他国外费
                        dtoOne.DomesticFee      = CalculateDtoFieldShipExtend.GetDomesticFee(shipLineEntity);       //国内运费
                        dtoOne.OtherDomesticFee = CalculateDtoFieldShipExtend.GetOtherOceanFreight(shipLineEntity); //其他国内费

                        //随单BOM
                        OrderBomBE.OrderBomHead.EntityList orderBom = OrderBomBE.OrderBomHead.Finder.FindAll("OrderLine=@SOLine", new OqlParam(shipLineEntity.SrcLineID));
                        dtoOne.TaxCredits        = CalculateDtoFieldShipExtend.GetTaxCredits(shipLineEntity, orderBom);                               //退税额
                        dtoOne.ProcurementCost   = CalculateDtoFieldShipExtend.GetProcurementCost(shipLineEntity, orderBom);                          //包装采购成本
                        dtoOne.LabourCharges     = CalculateDtoFieldShipExtend.GetLabourCharges(shipLineEntity);                                      //包装人工费用
                        dtoOne.ProductCost       = CalculateDtoFieldShipExtend.GetProductCost(shipLineEntity, orderBom, ref rmbCost, ref dollorCost); //产品采购成本
                        dtoOne.RMBProductCost    = rmbCost;                                                                                           //工具人民币采购成本
                        dtoOne.DollorProductCost = dollorCost;                                                                                        //工具美元采购成本
                        dtoOne.AdministrativeFee = CalculateDtoFieldShipExtend.GetAdministrativeFee(shipLineEntity);                                  //管理费
                        dtoOne.Interest          = CalculateDtoFieldShipExtend.GetInterest(shipLineEntity);                                           //利息
                        dtoOne.MouldFee          = CalculateDtoFieldShipExtend.GetMouldFee(shipLineEntity);                                           //模具费‘
                        dtoOne.Labelling         = CalculateDtoFieldShipExtend.GetLabelling(shipLineEntity, orderBom);                                //防盗标签费
                        dtoOne.CreditInsurance   = CalculateDtoFieldShipExtend.GetCreditInsurance(shipLineEntity);                                    //信用保险费
                        dtoOne.BankCharge        = CalculateDtoFieldShipExtend.GetBankCharge(shipLineEntity);                                         //银行费用

                        //dtoOne.OrderBomHead = orderBom;//随单Bom
                        dtoOne.ForecastTime = DateTime.Now;
                        dtoOne.ForecastMan  = UFIDA.U9.Base.Context.LoginUser;

                        //成品预测利润:各中类成品外销额-成品各项费用+成品退税额
                        allFee = dtoOne.Brokerage + dtoOne.Discount + dtoOne.OceanFreight + dtoOne.OtherFee + dtoOne.DomesticFee + dtoOne.OtherDomesticFee +
                                 dtoOne.ProcurementCost + dtoOne.LabourCharges + dtoOne.ProductCost + dtoOne.AdministrativeFee + dtoOne.Interest + dtoOne.MouldFee +
                                 dtoOne.Labelling + dtoOne.CreditInsurance + dtoOne.BankCharge;

                        //dtoOne.ProductForecastRate = shipLineEntity.TotalTax - allFee + dto.TaxCredits;
                        //dtoOne.ProductRate = dto.ProductForecastRate / shipLineEntity.TotalTax;

                        //反写出运明细单预测状态

                        dtoList.Add(dtoOne);
                        #endregion
                    }
                    seesion.Commit();
                }
            }
            else
            {
                throw new Exception("未选择任何需要预测的订单");
            }
            return(dtoList);
            //CreatEntity(dtoList);
        }