/// <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="type">取采购单价类型,因为不同字段取值采购单价校验不同(主要区分币种),用type字段区分,0计算退税率,1计算包装采购成本,3计算工具采购成本</param>
        /// <param name="soLine"></param>
        /// <param name="bomLine"></param>
        /// <param name="ds"></param>
        /// <returns></returns>
        private static decimal CalculatePurchasePrice(int type, SM.SO.SOLine soLine, OrderBomLine bomLine, DataSet ds, ref int currencyType)
        {
            decimal purchasePrice = 0;

            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 (type == 0 && Convert.ToInt64(ds.Tables[0].Rows[0]["TC"].ToString()) == 9)//采购币种为美元,不退税
                {
                    return(0);
                }
                else if (type == 1 && Convert.ToInt64(ds.Tables[0].Rows[0]["TC"].ToString()) != 1)//采购币种不为人民币,不计算计算包装采购成本
                {
                    return(0);
                }
                else
                {
                    if (type == 2)                                                       //工具采购成本计算结果根据币种不同需要保存到不同字段,此处记录币种为人民币/美元
                    {
                        if (Convert.ToInt64(ds.Tables[0].Rows[0]["TC"].ToString()) == 1) //人民币
                        {
                            currencyType = 1;
                        }
                        else if (Convert.ToInt64(ds.Tables[0].Rows[0]["TC"].ToString()) == 9)//美元
                        {
                            currencyType = 9;
                        }
                    }
                    else 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;
                    }
                    return(purchasePrice);
                }
            }
            else //没有取到最近采购价,取厂商价目表
            {
                UFIDA.U9.PPR.PurPriceList.PurPriceLine purPriceLine = null;
                if (bomLine == null) //BOM为空,取成品采购价
                {
                    purPriceLine = UFIDA.U9.PPR.PurPriceList.PurPriceLine.Finder.Find("ItemInfo='" + soLine.ItemInfo.ItemID.ID + "' and FromDate<='" + DateTime.Now + "' and ToDate>='" + DateTime.Now + "'");
                    if (type == 2 && purPriceLine == null) //工具子件取不到价格,取销售订单行成品成本
                    {
                        return(0);
                    }
                    else if (purPriceLine == null)
                    {
                        throw new Exception("销售订单" + soLine.SO.DocNo + "行" + soLine.DocLineNo + "对应料品" + soLine.ItemInfo.ItemName + "没有取到价格,请手工录入价表");
                    }
                }
                else if (bomLine.SubKey != null)//子件
                {
                    purPriceLine = UFIDA.U9.PPR.PurPriceList.PurPriceLine.Finder.Find("ItemInfo='" + bomLine.SubKey.ID + "' and FromDate<='" + DateTime.Now + "' and ToDate>='" + DateTime.Now + "'");
                    if (type == 2 && purPriceLine == null) //工具子件取不到价格,取销售订单行成品成本
                    {
                        return(0);
                    }
                    else if (purPriceLine == null)
                    {
                        throw new Exception("销售订单" + soLine.SO.DocNo + "行" + soLine.DocLineNo + "对应料品" + soLine.ItemInfo.ItemName + "子件" + bomLine.SubKey.Name + "没有取到价格,请手工录入价表");
                    }
                }
                if (type == 0 && purPriceLine.PurPriceList.Currency.ID == 9)//币种为美元,不退税
                {
                    return(0);
                }
                else if (type == 1 && purPriceLine.PurPriceList.Currency.ID != 1)//币种不为人民币,不计算计算包装采购成本
                {
                    return(0);
                }
                else
                {
                    if (type == 2)                                      //工具采购成本计算结果根据币种不同需要保存到不同字段,此处记录币种为人民币/美元
                    {
                        if (purPriceLine.PurPriceList.Currency.ID == 1) //人民币
                        {
                            currencyType = 1;
                        }
                        else if (purPriceLine.PurPriceList.Currency.ID == 9)//美元
                        {
                            currencyType = 9;
                        }
                    }
                }
                purchasePrice = purPriceLine.Price;                                                                   //厂商价目表单价
                if (purPriceLine.PurPriceList.Currency != null && purPriceLine.PurPriceList.Currency != soLine.SO.TC) //币种不同,汇率转换为销售订单币种
                {
                    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=" + purPriceLine.PurPriceList.Currency.ID + " and OperationalRateHead.NotCurrency=" + soLine.SO.TC.ID + " and OperationalRateHead.Org=" + UFIDA.U9.Base.Context.LoginOrg.ID);
                    if (rateLine == null)
                    {
                        throw new Exception("原币" + purPriceLine.PurPriceList.Currency.Name + "对目标币" + soLine.SO.TC.Name + "的汇率未设置,请在基础设置-业务汇率表中设置");
                    }
                    purchasePrice = purchasePrice * rateLine.Parities;
                }
                return(purchasePrice);
            }
            //return purchasePrice;
        }
        /// <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);
        }