public TransportActingBill CreateTransportActingBill(string orderNo)
        {
            TransportOrderMaster order = genericMgr.FindById<TransportOrderMaster>(orderNo);
            TransportActingBill actBill = new TransportActingBill();
            IList<TransportActingBillDetail> transportActingBillDetailList = new List<TransportActingBillDetail>();
            if (!string.IsNullOrEmpty(order.Expense))
            {
                #region 费用单
                //actBill.BillAmount = order.Expense.Amount;
                //actBill.UnitPrice = order.Expense.Amount;
                //actBill.BillQty = 1;
                //actBill.Currency = order.Expense.Currency;
                //actBill.IsIncludeTax = order.Expense.IsIncludeTax;
                //actBill.Currency.Code = order.Expense.Currency.Code;
                //actBill.IsProvisionalEstimate = false;
                #endregion

            }
            else
            {
                TransportFlowMaster tFlow = genericMgr.FindById<TransportFlowMaster>(order.Flow);
                TransportPriceList tPriceList = genericMgr.FindById<TransportPriceList>(order.PriceList);
                string currency = tPriceList.Currency;
                IList<TransportPriceListDetail> tPriceListDetailList = genericMgr.FindAll<TransportPriceListDetail>("from TransportPriceListDetail t where t.PriceList = ?", order.PriceList);



                #region 包车
                if (order.PricingMethod == CodeMaster.TransportPricingMethod.Chartered)
                {
                    TransportPriceListDetail tPriceListDetail = tPriceListDetailList.Where(p => p.ShipFrom == order.ShipFrom && p.ShipTo == order.ShipTo && p.PricingMethod == order.PricingMethod && p.Tonnage == order.Tonnage && p.StartDate < order.StartDate && (p.EndDate == null || p.EndDate > order.StartDate)).FirstOrDefault();

                    if (tPriceListDetail == null)
                    {
                        throw new BusinessException("没有找到对应的价格单明细");
                    }

                    actBill.BillQty = 1;

                    actBill.UnitPrice = tPriceListDetail.UnitPrice;
                    actBill.BillAmount = actBill.UnitPrice * actBill.BillQty;

                }
                #endregion

                #region 体积
                if (order.PricingMethod == CodeMaster.TransportPricingMethod.Volume)
                {
                    decimal totalVolume = 0;
                    TransportPriceListDetail tPriceListDetail = null;
                    foreach (TransportOrderDetail d in order.TransportOrderDetailList)
                    {
                        TransportActingBillDetail transportActingBillDetail = new TransportActingBillDetail();
                        tPriceListDetail = tPriceListDetailList.Where(p => p.ShipFrom == d.ShipFrom && p.ShipTo == d.ShipTo && p.Tonnage == order.Tonnage && p.PricingMethod == order.PricingMethod && p.StartDate < order.StartDate && (p.EndDate == null || p.EndDate > order.StartDate)).FirstOrDefault();

                        if (tPriceListDetail == null)
                        {
                            throw new BusinessException("没有找到对应的价格单明细");
                        }

                        #region 最小起运量
                        if (d.Volume.Value < tPriceListDetail.MinVolume)
                        {
                            totalVolume += tPriceListDetail.MinVolume;
                        }
                        else
                        {
                            totalVolume += d.Volume.Value;
                        }
                        #endregion

                        transportActingBillDetail.BillQty = totalVolume;
                        transportActingBillDetail.BillAmount = totalVolume * tPriceListDetail.UnitPrice;
                        transportActingBillDetail.Currency = tPriceListDetail.Currency;
                        transportActingBillDetail.IpNo = d.IpNo;
                        transportActingBillDetail.IsIncludeTax = tPriceList.IsIncludeTax;
                        transportActingBillDetail.PriceListDetail = tPriceListDetail.Id;
                        transportActingBillDetail.ShipFrom = d.ShipFrom;
                        transportActingBillDetail.ShipFromAddress = d.ShipFromAddress;
                        transportActingBillDetail.ShipTo = d.ShipTo;
                        transportActingBillDetail.ShipToAddress = d.ShipToAddress;
                        transportActingBillDetail.TaxCode = tPriceList.Tax;
                        transportActingBillDetail.UnitPrice = tPriceListDetail.UnitPrice;
                        transportActingBillDetailList.Add(transportActingBillDetail);
                    }

                    actBill.BillQty = totalVolume;
                    if (tPriceListDetail != null && actBill.UnitPrice == 0)
                    {
                        actBill.UnitPrice = tPriceListDetail.UnitPrice;
                    }
                    actBill.BillAmount = actBill.UnitPrice * actBill.BillQty;

                }
                #endregion

                #region 阶梯体积
                if (order.PricingMethod == CodeMaster.TransportPricingMethod.LadderVolume)
                {

                    decimal totalVolume = 0;
                    decimal totalAmount = 0;
                    TransportPriceListDetail tPriceListDetail = null;
                    foreach (TransportOrderDetail d in order.TransportOrderDetailList)
                    {
                        TransportActingBillDetail transportActingBillDetail = new TransportActingBillDetail();
                        decimal detailVolume = d.Volume.Value;
                        #region 最小起运量
                        if (detailVolume < tPriceListDetail.MinVolume)
                        {
                            detailVolume = tPriceListDetail.MinVolume;
                        }
                        #endregion

                        tPriceListDetail = tPriceListDetailList.Where(p => p.ShipFrom == d.ShipFrom && p.ShipTo == d.ShipTo
                           && p.PricingMethod == order.PricingMethod && p.StartDate < order.StartDate && (p.EndDate == null || p.EndDate > order.StartDate)
                           && p.StartQty < detailVolume && (!p.EndQty.HasValue || p.EndQty.Value > detailVolume)).FirstOrDefault();
                        if (tPriceListDetail == null)
                        {
                            throw new BusinessException("没有找到对应的价格单明细");
                        }


                        #region 账单明细
                        decimal minPrice = tPriceListDetail.MinPrice.HasValue ? tPriceListDetail.MinPrice.Value : 0;
                        transportActingBillDetail.BillQty = detailVolume;
                        transportActingBillDetail.BillAmount = minPrice + detailVolume * tPriceListDetail.UnitPrice;
                        transportActingBillDetail.Currency = tPriceListDetail.Currency;
                        transportActingBillDetail.IpNo = d.IpNo;
                        transportActingBillDetail.IsIncludeTax = tPriceList.IsIncludeTax;
                        transportActingBillDetail.PriceListDetail = tPriceListDetail.Id;
                        transportActingBillDetail.ShipFrom = d.ShipFrom;
                        transportActingBillDetail.ShipFromAddress = d.ShipFromAddress;
                        transportActingBillDetail.ShipTo = d.ShipTo;
                        transportActingBillDetail.ShipToAddress = d.ShipToAddress;
                        transportActingBillDetail.TaxCode = tPriceList.Tax;
                        transportActingBillDetail.UnitPrice = tPriceListDetail.UnitPrice;
                        transportActingBillDetailList.Add(transportActingBillDetail);
                        #endregion

                        totalVolume += detailVolume;
                        totalAmount += transportActingBillDetail.BillAmount;
                    }

                    #region 头
                    actBill.BillQty = totalVolume;
                    actBill.BillAmount = totalAmount;
                    #endregion
                }
                #endregion

                #region 重量
                if (order.PricingMethod == CodeMaster.TransportPricingMethod.Weight)
                {
                    decimal totalWeight = 0;
                    TransportPriceListDetail tPriceListDetail = null;
                    foreach (TransportOrderDetail d in order.TransportOrderDetailList)
                    {
                        TransportActingBillDetail transportActingBillDetail = new TransportActingBillDetail();
                        tPriceListDetail = tPriceListDetailList.Where(p => p.ShipFrom == d.ShipFrom && p.ShipTo == d.ShipTo && p.PricingMethod == order.PricingMethod && p.Tonnage == order.Tonnage && p.StartDate < order.StartDate && (p.EndDate == null || p.EndDate > order.StartDate)).FirstOrDefault();

                        if (tPriceListDetail == null)
                        {
                            throw new BusinessException("没有找到对应的价格单明细");
                        }

                        #region 最小起运量
                        if (d.Weight.Value < tPriceListDetail.MinWeight)
                        {
                            totalWeight += tPriceListDetail.MinWeight;
                        }
                        else
                        {
                            totalWeight += d.Weight.Value;
                        }
                        #endregion

                        transportActingBillDetail.BillQty = totalWeight;
                        transportActingBillDetail.BillAmount = totalWeight * tPriceListDetail.UnitPrice;
                        transportActingBillDetail.Currency = tPriceListDetail.Currency;
                        transportActingBillDetail.IpNo = d.IpNo;
                        transportActingBillDetail.IsIncludeTax = tPriceList.IsIncludeTax;
                        transportActingBillDetail.PriceListDetail = tPriceListDetail.Id;
                        transportActingBillDetail.ShipFrom = d.ShipFrom;
                        transportActingBillDetail.ShipFromAddress = d.ShipFromAddress;
                        transportActingBillDetail.ShipTo = d.ShipTo;
                        transportActingBillDetail.ShipToAddress = d.ShipToAddress;
                        transportActingBillDetail.TaxCode = tPriceList.Tax;
                        transportActingBillDetail.UnitPrice = tPriceListDetail.UnitPrice;
                        transportActingBillDetailList.Add(transportActingBillDetail);
                    }

                    actBill.BillQty = totalWeight;
                    if (tPriceListDetail != null && actBill.UnitPrice == 0)
                    {
                        actBill.UnitPrice = tPriceListDetail.UnitPrice;
                    }
                    actBill.BillAmount = actBill.UnitPrice * actBill.BillQty;


                }
                #endregion

                #region 距离
                if (order.PricingMethod == CodeMaster.TransportPricingMethod.Distance)
                {
                    TransportPriceListDetail tPriceListDetail = tPriceListDetailList.Where(p => p.ShipFrom == order.ShipFrom && p.ShipTo == order.ShipTo && p.PricingMethod == order.PricingMethod && p.Tonnage == order.Tonnage && p.StartDate < order.StartDate && (p.EndDate == null || p.EndDate > order.StartDate)).FirstOrDefault();
                    if (tPriceListDetail == null)
                    {
                        throw new BusinessException("没有找到对应的价格单明细");
                    }
                    IList<TransportOrderRoute> tRouteList = genericMgr.FindAll<TransportOrderRoute>("from TransportOrderRoute t where t.OrderNo = ?", order.OrderNo);

                    decimal totalDistance = tRouteList.Sum(p => p.Distance.HasValue ? p.Distance.Value : 0);

                    actBill.BillQty = totalDistance;
                    actBill.UnitPrice = tPriceListDetail.UnitPrice;
                    actBill.BillAmount = actBill.UnitPrice * actBill.BillQty;
                }
                #endregion
            }

            actBill.OrderNo = order.OrderNo;
            actBill.BillAddress = order.BillAddress;
            actBill.EffectiveDate = DateTime.Parse(order.CreateDate.ToString("yyyy-MM-dd"));
            actBill.Type = CodeMaster.BillType.Transport;

            genericMgr.Create(actBill);

            #region 保存头
            if (transportActingBillDetailList != null && transportActingBillDetailList.Count > 0)
            {
                foreach (TransportActingBillDetail d in transportActingBillDetailList)
                {
                    d.ActBill = actBill.Id;
                    genericMgr.Create(d);
                }
            }
            #endregion
            return actBill;
        }
        private TransportBillDetail TransportActingBill2TransportBillDetail(TransportActingBill actingBill)
        {
            TransportBillDetail billDetail = Mapper.Map<TransportActingBill, TransportBillDetail>(actingBill);

            billDetail.BillAmount = actingBill.CurrentBillAmount;
            billDetail.BillQty = actingBill.CurrentBillQty;
            //  billDetail.PriceList = actingBill.PriceList;

            //本次开票数量大于剩余数量
            if (actingBill.CurrentBillQty > (actingBill.BillQty - actingBill.BillingQty))
            {
                throw new BusinessException("TransportActingBill.Error.CurrentBillQtyGeRemainQty");
            }

            //本次开票金额大于剩余金额
            if (actingBill.CurrentBillAmount > (actingBill.BillAmount - actingBill.BillingAmount))
            {
                throw new BusinessException("TransportActingBill.Error.CurrentBillAmountGeRemainAmount");
            }

            return billDetail;
        }