diff --git a/BBWY.Server.Business/Extensions/OrderCostExtension.cs b/BBWY.Server.Business/Extensions/OrderCostExtension.cs index 56fb2b72..67ac590e 100644 --- a/BBWY.Server.Business/Extensions/OrderCostExtension.cs +++ b/BBWY.Server.Business/Extensions/OrderCostExtension.cs @@ -13,26 +13,45 @@ namespace BBWY.Server.Business.Extensions orderCost.RefundPurchaseAmount = afterSaleOrders == null || afterSaleOrders.Count == 0M ? 0M : afterSaleOrders.Sum(aso => aso.RefundPurchaseAmount ?? 0); //退款之后平台扣点 - orderCost.PlatformCommissionAmount = (order.OrderSellerPrice - orderCost.RefundAmount) * (orderCost.PlatformCommissionRatio); + orderCost.PlatformCommissionAmount = (order.OrderPayment - order.FreightPrice - orderCost.RefundAmount) * (orderCost.PlatformCommissionRatio); + + //orderCost.Profit = order.OrderSellerPrice + order.FreightPrice - orderCost.RefundAmount - + // orderCost.PurchaseAmount - + // orderCost.DeliveryExpressFreight - + // orderCost.PlatformCommissionAmount - + // orderCost.AfterTotalCost + + // orderCost.RefundPurchaseAmount; - orderCost.Profit = order.OrderSellerPrice + order.FreightPrice - orderCost.RefundAmount - + orderCost.Profit = order.OrderPayment + + order.PreferentialAmount - + orderCost.RefundAmount - orderCost.PurchaseAmount - orderCost.DeliveryExpressFreight - orderCost.PlatformCommissionAmount - orderCost.AfterTotalCost + orderCost.RefundPurchaseAmount; + } - public static void CalculationOrderProfitAndCost(this OrderCost orderCost, decimal orderSellerPrice,decimal freightPrice, IList afterSaleOrders) + public static void CalculationOrderProfitAndCost(this OrderCost orderCost, decimal orderPayment, decimal preferentialAmount, decimal freightPrice, IList afterSaleOrders) { orderCost.AfterTotalCost = afterSaleOrders == null || afterSaleOrders.Count == 0 ? 0M : afterSaleOrders.Sum(aso => aso.AfterTotalCost); orderCost.RefundAmount = afterSaleOrders == null || afterSaleOrders.Count == 0 ? 0M : afterSaleOrders.Sum(aso => aso.RefundAmount ?? 0M); orderCost.RefundPurchaseAmount = afterSaleOrders == null || afterSaleOrders.Count == 0M ? 0M : afterSaleOrders.Sum(aso => aso.RefundPurchaseAmount ?? 0); //退款之后平台扣点 - orderCost.PlatformCommissionAmount = (orderSellerPrice - orderCost.RefundAmount) * (orderCost.PlatformCommissionRatio); + orderCost.PlatformCommissionAmount = (orderPayment - freightPrice - orderCost.RefundAmount) * (orderCost.PlatformCommissionRatio); + + //orderCost.Profit = orderSellerPrice + freightPrice - orderCost.RefundAmount - + // orderCost.PurchaseAmount - + // orderCost.DeliveryExpressFreight - + // orderCost.PlatformCommissionAmount - + // orderCost.AfterTotalCost + + // orderCost.RefundPurchaseAmount; - orderCost.Profit = orderSellerPrice + freightPrice - orderCost.RefundAmount - + orderCost.Profit = orderPayment + + preferentialAmount - + orderCost.RefundAmount - orderCost.PurchaseAmount - orderCost.DeliveryExpressFreight - orderCost.PlatformCommissionAmount - @@ -52,5 +71,20 @@ namespace BBWY.Server.Business.Extensions orderCost.PlatformCommissionAmount = (order.OrderSellerPrice - orderCost.RefundAmount) * (orderCost.PlatformCommissionRatio); orderCost.Profit = (orderCost.SDCommissionAmount + orderCost.SDOrderAmount + orderCost.DeliveryExpressFreight + orderCost.PlatformCommissionAmount + orderCost.AfterTotalCost) * -1; } + + /// + /// 计算SKU毛利 + /// + /// + /// + /// + /// + /// + public static void CalculationSkuGrossProfit(this OrderCostDetail ocd, decimal skuShouldPay, decimal skuPingtaibutie, decimal skuVenderFee, decimal platformCommissionRatio) + { + var koudian = (skuShouldPay - skuVenderFee) * ocd.DeductionQuantity * platformCommissionRatio; + ocd.SkuGrossProfit = (skuShouldPay + skuPingtaibutie) * ocd.DeductionQuantity - + ocd.TotalCost - ocd.DeliveryExpressFreight - koudian; + } } } diff --git a/BBWY.Server.Business/Order/OrderBusiness.cs b/BBWY.Server.Business/Order/OrderBusiness.cs index edd66f35..ef2e1597 100644 --- a/BBWY.Server.Business/Order/OrderBusiness.cs +++ b/BBWY.Server.Business/Order/OrderBusiness.cs @@ -593,7 +593,21 @@ namespace BBWY.Server.Business StorageAmount = purchaseOrder.SingleStorageAmount * deductionQuantity }; //orderCostDetail.SkuGrossProfit = orderSku.Price.Value * deductionQuantity - avgPreferential - (orderCostDetail.TotalCost + orderCostDetail.DeliveryExpressFreight) - orderSku.Price.Value * deductionQuantity * autoCalculationCostRequest.PlatformCommissionRatio; - orderCostDetail.SkuGrossProfit = ((orderSku.ShouldPay ?? 0M) + (orderSku.Coupon ?? 0M)) * deductionQuantity - orderCostDetail.TotalCost - orderCostDetail.DeliveryExpressFreight; + + + //var shouldPay = orderSku.ShouldPay ?? 0M; + //var pingTaiCoupon = orderSku.PingTaiChengDanYouHuiQuan ?? 0M; + //var venderFee = orderSku.VenderFee ?? 0M; + + //var koudian = (shouldPay - venderFee) * deductionQuantity * autoCalculationCostRequest.PlatformCommissionRatio; + //orderCostDetail.SkuGrossProfit = (shouldPay + pingTaiCoupon) * deductionQuantity - + // orderCostDetail.TotalCost - orderCostDetail.DeliveryExpressFreight - + // koudian; + orderCostDetail.CalculationSkuGrossProfit(orderSku.ShouldPay ?? 0M, + orderSku.PingTaiChengDanYouHuiQuan ?? 0M, + orderSku.VenderFee ?? 0M, + autoCalculationCostRequest.PlatformCommissionRatio); + insertOrderCostDetailList.Add(orderCostDetail); } } @@ -663,7 +677,8 @@ namespace BBWY.Server.Business osku.Price, osku.ItemTotal, osku.ShouldPay, - osku.Coupon + osku.PingTaiChengDanYouHuiQuan, + osku.VenderFee }); var avgPreferential = dbOrder.PreferentialAmount / orderSkuList.Count(); @@ -688,7 +703,13 @@ namespace BBWY.Server.Business //orderCostDetail.SkuGrossProfit = osku.Price.Value * orderCostDetail.DeductionQuantity - avgPreferential - // (orderCostDetail.TotalCost + orderCostDetail.DeliveryExpressFreight) - // osku.Price.Value * orderCostDetail.DeductionQuantity * manualCalculationCostRequest.PlatformCommissionRatio; - orderCostDetail.SkuGrossProfit = ((osku.ShouldPay ?? 0M) + (osku.Coupon ?? 0M)) * orderCostDetail.DeductionQuantity - orderCostDetail.TotalCost - orderCostDetail.DeliveryExpressFreight; + + //orderCostDetail.SkuGrossProfit = ((osku.ShouldPay ?? 0M) + (osku.Coupon ?? 0M)) * orderCostDetail.DeductionQuantity - orderCostDetail.TotalCost - orderCostDetail.DeliveryExpressFreight; + + orderCostDetail.CalculationSkuGrossProfit(osku.ShouldPay ?? 0M, + osku.PingTaiChengDanYouHuiQuan ?? 0M, + osku.VenderFee ?? 0M, + manualCalculationCostRequest.PlatformCommissionRatio); } } @@ -947,7 +968,11 @@ namespace BBWY.Server.Business //orderCostDetail.SkuGrossProfit = dbOrderSku.Price.Value * orderCostDetail.DeductionQuantity - avgPreferential - // (orderCostDetail.TotalCost + orderCostDetail.DeliveryExpressFreight) - // dbOrderSku.Price.Value * orderCostDetail.DeductionQuantity * relationPurchaseOrderRequestV2.PlatformCommissionRatio; - orderCostDetail.SkuGrossProfit = ((dbOrderSku.ShouldPay ?? 0M) + dbOrderSku.Coupon ?? 0M) * orderCostDetail.DeductionQuantity - orderCostDetail.TotalCost - orderCostDetail.DeliveryExpressFreight; + //orderCostDetail.SkuGrossProfit = ((dbOrderSku.ShouldPay ?? 0M) + dbOrderSku.Coupon ?? 0M) * orderCostDetail.DeductionQuantity - orderCostDetail.TotalCost - orderCostDetail.DeliveryExpressFreight; + orderCostDetail.CalculationSkuGrossProfit(dbOrderSku.ShouldPay ?? 0M, + dbOrderSku.PingTaiChengDanYouHuiQuan ?? 0M, + dbOrderSku.VenderFee ?? 0M, + relationPurchaseOrderRequestV2.PlatformCommissionRatio); } #endregion } diff --git a/BBWY.Server.Business/PurchaseOrder/PurchaseOrderBusiness.cs b/BBWY.Server.Business/PurchaseOrder/PurchaseOrderBusiness.cs index dd62b483..42be525b 100644 --- a/BBWY.Server.Business/PurchaseOrder/PurchaseOrderBusiness.cs +++ b/BBWY.Server.Business/PurchaseOrder/PurchaseOrderBusiness.cs @@ -13,7 +13,7 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using Yitter.IdGenerator; - +using BBWY.Server.Business.Extensions; namespace BBWY.Server.Business { public class PurchaseOrderBusiness : BaseBusiness, IDenpendency @@ -265,8 +265,12 @@ namespace BBWY.Server.Business }; //orderCostDetail.SkuGrossProfit = orderSku.Price.Value * orderCostDetail.DeductionQuantity - avgPreferential - (orderCostDetail.TotalCost + orderCostDetail.DeliveryExpressFreight) - orderSku.Price.Value * orderCostDetail.DeductionQuantity * createOnlinePurchaseOrderRequest.PlatformCommissionRatio; - orderCostDetail.SkuGrossProfit = ((orderSku.ShouldPay ?? 0M) + orderSku.Coupon ?? 0M) * orderCostDetail.DeductionQuantity - orderCostDetail.TotalCost - orderCostDetail.DeliveryExpressFreight; + //orderCostDetail.SkuGrossProfit = ((orderSku.ShouldPay ?? 0M) + orderSku.Coupon ?? 0M) * orderCostDetail.DeductionQuantity - orderCostDetail.TotalCost - orderCostDetail.DeliveryExpressFreight; + orderCostDetail.CalculationSkuGrossProfit(orderSku.ShouldPay ?? 0M, + orderSku.PingTaiChengDanYouHuiQuan ?? 0M, + orderSku.VenderFee ?? 0M, + createOnlinePurchaseOrderRequest.PlatformCommissionRatio); insertOrderCostDetails.Add(orderCostDetail); #endregion } diff --git a/BBWY.Server.Business/Sync/OrderSyncBusiness.cs b/BBWY.Server.Business/Sync/OrderSyncBusiness.cs index 75132231..0b48138f 100644 --- a/BBWY.Server.Business/Sync/OrderSyncBusiness.cs +++ b/BBWY.Server.Business/Sync/OrderSyncBusiness.cs @@ -10,13 +10,10 @@ using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Options; using Newtonsoft.Json; using Newtonsoft.Json.Linq; -using NLog; using System; -using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Net.Http; -using System.Security.Cryptography; using System.Threading; using System.Threading.Tasks; using Yitter.IdGenerator; @@ -453,6 +450,7 @@ namespace BBWY.Server.Business var preferentialAmount = 0M; if (dbOrder == null) { + var orderTotalInfo = insertOrderTotalInfoList.FirstOrDefault(x => x.Id == orderId); dbOrder = new Order() { Id = orderId, @@ -462,7 +460,7 @@ namespace BBWY.Server.Business EndTime = endTime, StartTime = orderJToken.Value("orderStartTime"), ModifyTime = modifyTime, - OrderPayment = orderPayment, + OrderPayment = orderPayment, //(orderTotalInfo?.TotalShouldPay) ?? orderPayment, SellerPreferentialAmount = sellerPreferentialAmount, OrderSellerPrice = orderSellerPrice, OrderTotalPrice = orderTotalPrice, @@ -474,7 +472,8 @@ namespace BBWY.Server.Business StoreOrder = storeOrder, StoreId = storeId, IsGift = orderSellerPrice == 0M, - StorageType = storageType + StorageType = storageType, + PreferentialAmount = (orderTotalInfo?.TotalPingTaiChengDanYouHuiQuan) ?? 0M }; if (memoryCache.TryGetValue(orderId, out sDCalculationCostRequest)) @@ -635,31 +634,31 @@ namespace BBWY.Server.Business #region 订单优惠 - var orderCouponJArray = (JArray)orderJToken["couponDetailList"]; - if (orderCouponJArray.HasValues) - { - foreach (var orderCouponJToken in orderCouponJArray) - { - var couponType = orderCouponJToken.Value("couponType"); - if (string.IsNullOrEmpty(couponType)) - continue; - //dbOrder.PreferentialAmount += orderCouponJToken.Value("couponPrice"); - preferentialAmount += orderCouponJToken.Value("couponPrice"); - if (!dbOrderCouponList.Any(oc => oc.OrderId == orderId)) - { - insertOrderCouponList.Add(new OrderCoupon() - { - Id = idGenerator.NewLong(), - SkuId = orderCouponJToken.Value("skuId"), - OrderId = orderId, - CreateTime = DateTime.Now, - CouponType = couponType, - CouponPrice = orderCouponJToken.Value("couponPrice") - }); - } - } - } - #endregion + //var orderCouponJArray = (JArray)orderJToken["couponDetailList"]; + //if (orderCouponJArray.HasValues) + //{ + // foreach (var orderCouponJToken in orderCouponJArray) + // { + // var couponType = orderCouponJToken.Value("couponType"); + // if (string.IsNullOrEmpty(couponType)) + // continue; + // //dbOrder.PreferentialAmount += orderCouponJToken.Value("couponPrice"); + // preferentialAmount += orderCouponJToken.Value("couponPrice"); + // if (!dbOrderCouponList.Any(oc => oc.OrderId == orderId)) + // { + // insertOrderCouponList.Add(new OrderCoupon() + // { + // Id = idGenerator.NewLong(), + // SkuId = orderCouponJToken.Value("skuId"), + // OrderId = orderId, + // CreateTime = DateTime.Now, + // CouponType = couponType, + // CouponPrice = orderCouponJToken.Value("couponPrice") + // }); + // } + // } + //} + //#endregion #region 订单状态转换 var orderState = ConvertOrderState(orderJToken, dbOrder.StorageType); @@ -723,7 +722,7 @@ namespace BBWY.Server.Business var orderCostPurchaseAmount = 0M; var orderDeliveryExpressFreight = 0M; //发货总运费,sku购买数量第二个开始半价 - var avgPreferential = preferentialAmount / orderSkuJArray.Count(); + //var avgPreferential = preferentialAmount / orderSkuJArray.Count(); #region 扣减库存 foreach (var orderSkuJToken in orderSkuJArray) @@ -731,17 +730,28 @@ namespace BBWY.Server.Business var orderSkuId = orderSkuJToken.Value("skuId"); var itemTotal = orderSkuJToken.Value("itemTotal"); //sku购买数量 + #region sku拆分明细 decimal? skuShouldPay = orderSkuCFDetailList.FirstOrDefault(x => x.OrderId == orderId && x.SkuId == orderSkuId)?.ShouldPay; if (skuShouldPay == null) skuShouldPay = insertOrderSkuList.FirstOrDefault(x => x.OrderId == orderId && x.SkuId == orderSkuId)?.ShouldPay; if (skuShouldPay == null) skuShouldPay = dbOrderSkuList.FirstOrDefault(x => x.OrderId == orderId && x.SkuId == orderSkuId)?.ShouldPay; - decimal? coupon = orderSkuCFDetailList.FirstOrDefault(x => x.OrderId == orderId && x.SkuId == orderSkuId)?.Coupon; - if (coupon == null) - coupon = insertOrderSkuList.FirstOrDefault(x => x.OrderId == orderId && x.SkuId == orderSkuId)?.Coupon; - if (coupon == null) - coupon = dbOrderSkuList.FirstOrDefault(x => x.OrderId == orderId && x.SkuId == orderSkuId)?.Coupon; + + decimal? pingtaiCoupon = orderSkuCFDetailList.FirstOrDefault(x => x.OrderId == orderId && + x.SkuId == orderSkuId)?.PingTaiChengDanYouHuiQuan; + if (pingtaiCoupon == null) + pingtaiCoupon = insertOrderSkuList.FirstOrDefault(x => x.OrderId == orderId && x.SkuId == orderSkuId)?.PingTaiChengDanYouHuiQuan; + if (pingtaiCoupon == null) + pingtaiCoupon = dbOrderSkuList.FirstOrDefault(x => x.OrderId == orderId && x.SkuId == orderSkuId)?.PingTaiChengDanYouHuiQuan; + + decimal? venderFee = orderSkuCFDetailList.FirstOrDefault(x => x.OrderId == orderId && + x.SkuId == orderSkuId)?.VenderFee; + if (venderFee == null) + venderFee = insertOrderSkuList.FirstOrDefault(x => x.OrderId == orderId && x.SkuId == orderSkuId)?.VenderFee; + if (venderFee == null) + venderFee = dbOrderSkuList.FirstOrDefault(x => x.OrderId == orderId && x.SkuId == orderSkuId)?.VenderFee; + #endregion //var itemPrice = orderSkuJToken.Value("jdPrice"); //sku单价 var isReduceMultiTimes = false; //是否多次扣减库存 @@ -801,7 +811,13 @@ namespace BBWY.Server.Business //orderCostDetail.SkuGrossProfit = itemPrice * deductionQuantity - avgPreferential - // (orderCostDetail.TotalCost + orderCostDetail.DeliveryExpressFreight) - // itemPrice * deductionQuantity * platformCommissionRatio; - orderCostDetail.SkuGrossProfit = ((skuShouldPay ?? 0M) + (coupon ?? 0M)) * deductionQuantity - orderCostDetail.TotalCost - orderCostDetail.DeliveryExpressFreight; + + //var koudian = (skuShouldPay.Value - venderFee.Value) * deductionQuantity * platformCommissionRatio; + //orderCostDetail.SkuGrossProfit = (skuShouldPay.Value + pingtaiCoupon.Value) * deductionQuantity - + // orderCostDetail.TotalCost - orderCostDetail.DeliveryExpressFreight - + // koudian; + + orderCostDetail.CalculationSkuGrossProfit(skuShouldPay ?? 0M, pingtaiCoupon ?? 0M, venderFee ?? 0M, platformCommissionRatio); insertOrderCostDetailList.Add(orderCostDetail); } } @@ -821,7 +837,7 @@ namespace BBWY.Server.Business if (dbOrder.OrderTotalPrice != 0) orderCost.CalculationOrderProfitAndCost(dbOrder, null); else - orderCost.CalculationOrderProfitAndCost(orderSellerPrice, freightPrice, null); + orderCost.CalculationOrderProfitAndCost(dbOrder.OrderPayment, dbOrder.PreferentialAmount, dbOrder.FreightPrice, null); insertOrderCostList.Add(orderCost); #endregion @@ -835,7 +851,7 @@ namespace BBWY.Server.Business if (isNewOrder) { dbOrder.OrderState = orderState; - dbOrder.PreferentialAmount = preferentialAmount; + //dbOrder.PreferentialAmount = preferentialAmount; } else if ((orderState != null && orderState != dbOrder.OrderState) || buyerRemark != dbOrder.BuyerRemark ||