|
|
@ -12,9 +12,9 @@ using SDKAdapter; |
|
|
|
using SDKAdapter.OperationPlatform.Models; |
|
|
|
using SDKAdapter.PurchasePlatform.Client; |
|
|
|
using SDKAdapter.PurchasePlatform.Models; |
|
|
|
using System.Net; |
|
|
|
using System.Text; |
|
|
|
using Yitter.IdGenerator; |
|
|
|
using BBWYB.Server.Business.Extensions; |
|
|
|
|
|
|
|
namespace BBWYB.Server.Business |
|
|
|
{ |
|
|
@ -96,6 +96,8 @@ namespace BBWYB.Server.Business |
|
|
|
var productAmount = 0M; |
|
|
|
var totalAmount = 0M; |
|
|
|
|
|
|
|
var previewAmountGroupByPurchaserList = new List<PreviewAmountGroupByPurchaser>(); |
|
|
|
|
|
|
|
foreach (var cargoParamGroup in request.CargoParamGroupList) |
|
|
|
{ |
|
|
|
var purchaseAccount = request.PurchaseAccountList.FirstOrDefault(pa => pa.PurchasePlatformId == cargoParamGroup.PurchasePlatform); |
|
|
@ -133,6 +135,16 @@ namespace BBWYB.Server.Business |
|
|
|
freightAmount += response.FreightAmount; |
|
|
|
productAmount += response.ProductAmount; |
|
|
|
totalAmount += response.TotalAmount; |
|
|
|
|
|
|
|
previewAmountGroupByPurchaserList.Add(new PreviewAmountGroupByPurchaser() |
|
|
|
{ |
|
|
|
FreightAmount = response.FreightAmount, |
|
|
|
ProductAmount = response.ProductAmount, |
|
|
|
TotalAmount = response.TotalAmount, |
|
|
|
PurchasePlatform = cargoParamGroup.PurchasePlatform, |
|
|
|
PurchaserId = cargoParamGroup.PurchaserId, |
|
|
|
PurchaserName = cargoParamGroup.PurchaserName |
|
|
|
}); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
@ -147,7 +159,8 @@ namespace BBWYB.Server.Business |
|
|
|
Extensions = JsonConvert.SerializeObject(extJArray), |
|
|
|
FreightAmount = freightAmount, |
|
|
|
ProductAmount = productAmount, |
|
|
|
TotalAmount = totalAmount |
|
|
|
TotalAmount = totalAmount, |
|
|
|
PreviewAmountGroupByPurchaserList = previewAmountGroupByPurchaserList |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
@ -176,13 +189,11 @@ namespace BBWYB.Server.Business |
|
|
|
if (request.CargoParamGroupList == null || request.CargoParamGroupList.Count() == 0) |
|
|
|
throw new BusinessException("缺少下单商品参数"); |
|
|
|
|
|
|
|
//var deleteOrderCostDetail = fsql.Delete<OrderCostDetail>().Where(ocd => ocd.OrderId == dbOrder.Id);
|
|
|
|
var isRepurchase = fsql.Select<OrderCost>(dbOrder.Id).Any(); |
|
|
|
var orderSkus = fsql.Select<OrderSku>().Where(osku => osku.Price != 0 && osku.OrderId == request.OrderId).ToList(); |
|
|
|
|
|
|
|
|
|
|
|
var extJArray = JsonConvert.DeserializeObject<JArray>(request.Extensions); |
|
|
|
//IList<PP_QueryOrderDetailResponse> purchaseOrderSimpleInfoList = new List<PP_QueryOrderDetailResponse>();
|
|
|
|
|
|
|
|
List<OrderCostDetail> insertOrderCostDetails = new List<OrderCostDetail>(); |
|
|
|
List<OrderPurchaseInfo> insertOrderPurchaseInfos = new List<OrderPurchaseInfo>(); |
|
|
@ -201,7 +212,8 @@ namespace BBWYB.Server.Business |
|
|
|
|
|
|
|
IInsert<OrderCost> insertOrderCost = null; |
|
|
|
IUpdate<OrderCost> updateOrderCost = null; |
|
|
|
var totalPurchaseAmount = 0M; |
|
|
|
var totalPurchaseProductAmount = 0M; |
|
|
|
var totalPurchaseFreight = 0M; |
|
|
|
|
|
|
|
foreach (var cargoParamGroup in request.CargoParamGroupList) |
|
|
|
{ |
|
|
@ -235,12 +247,10 @@ namespace BBWYB.Server.Business |
|
|
|
Platform = (AdapterEnums.PlatformType)purchaseAccount.PurchasePlatformId, |
|
|
|
PurchaseMode = (AdapterEnums.PurchaseMode)request.PurchaseOrderMode, |
|
|
|
Extensions = extJson.Value<string>("OrderTradeTypeCode"), |
|
|
|
Remark = request.Remark, |
|
|
|
Remark = !string.IsNullOrEmpty(cargoParamGroup.Remark) ? cargoParamGroup.Remark : request.Remark, |
|
|
|
OrderProductParamList = orderProductParamList |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var purchaseOrderSimpleInfo = client.QueryOrderDetail(new PP_QueryOrderDetailRequest() |
|
|
|
{ |
|
|
|
AppKey = purchaseAccount.AppKey, |
|
|
@ -249,6 +259,9 @@ namespace BBWYB.Server.Business |
|
|
|
OrderId = createOrderResponse.OrderId |
|
|
|
}); |
|
|
|
|
|
|
|
totalPurchaseProductAmount += purchaseOrderSimpleInfo.ProductAmount; |
|
|
|
totalPurchaseFreight += purchaseOrderSimpleInfo.FreightAmount; |
|
|
|
|
|
|
|
foreach (var cargoParam in cargoParamGroup.CargoParamList) |
|
|
|
{ |
|
|
|
var orderSku = orderSkus.FirstOrDefault(osku => osku.SkuId == cargoParam.BelongSkuId); |
|
|
@ -269,49 +282,50 @@ namespace BBWYB.Server.Business |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
totalPurchaseAmount += purchaseOrderSimpleInfo.TotalAmount; |
|
|
|
|
|
|
|
//采购单总件数,由于1688运费只存在订单层,sku层没有运费,所以需要以订单总件数为基础等比计算采购运费
|
|
|
|
var purchaseSkuTotalQuantity = purchaseOrderSimpleInfo.ItemList.Sum(x => x.Quantity); |
|
|
|
var belongSkuGroups = cargoParamGroup.CargoParamList.GroupBy(p => p.BelongSkuId); |
|
|
|
foreach (var belongSkuGroup in belongSkuGroups) |
|
|
|
{ |
|
|
|
var belongSkuId = belongSkuGroup.Key; |
|
|
|
var currentOrderSkuProductAmount = 0M; //采购成本
|
|
|
|
var currentOrderSkuCargoParamList = cargoParamGroup.CargoParamList.Where(p => p.BelongSkuId == belongSkuId); //找当前skuId的采购skuId
|
|
|
|
|
|
|
|
var currentSkuAmount = 0M; //采购成本
|
|
|
|
var currentSkuTotalPurchaseQuantity = currentOrderSkuCargoParamList.Sum(x => x.Quantity); //当前skuId的采购数量总和
|
|
|
|
var currentPurchaseFreight = purchaseOrderSimpleInfo.FreightAmount * |
|
|
|
(1.0M * currentSkuTotalPurchaseQuantity / purchaseSkuTotalQuantity); //采购运费
|
|
|
|
|
|
|
|
foreach (var currentOrderSkuCargo in currentOrderSkuCargoParamList) |
|
|
|
{ |
|
|
|
var purchaseSkuProductAmount = purchaseOrderSimpleInfo.ItemList.Where(p => p.SkuId == currentOrderSkuCargo.SkuId) |
|
|
|
var currentPurchaseSkuProductAmount = purchaseOrderSimpleInfo.ItemList.Where(p => p.SkuId == currentOrderSkuCargo.SkuId) |
|
|
|
.Sum(p => p.ProductAmount); |
|
|
|
var purchaseSkuTotalQuantity = purchaseOrderSimpleInfo.ItemList.Where(p => p.SkuId == currentOrderSkuCargo.SkuId) |
|
|
|
var currentPurchaseSkuTotalQuantity = purchaseOrderSimpleInfo.ItemList.Where(p => p.SkuId == currentOrderSkuCargo.SkuId) |
|
|
|
.Sum(p => p.Quantity); |
|
|
|
currentOrderSkuProductAmount += purchaseSkuProductAmount * (1.0M * currentOrderSkuCargo.Quantity / purchaseSkuTotalQuantity); |
|
|
|
} |
|
|
|
|
|
|
|
var currentOrderSkuFreightAmount = purchaseOrderSimpleInfo.FreightAmount / belongSkuGroups.Count(); //采购运费(按sku数均分)
|
|
|
|
currentSkuAmount += currentPurchaseSkuProductAmount * (1.0M * currentOrderSkuCargo.Quantity / currentPurchaseSkuTotalQuantity); |
|
|
|
//currentPurchaseFreight += purchaseOrderSimpleInfo.FreightAmount * (1.0M * currentOrderSkuCargo.Quantity / purchaseSkuTotalQuantity);
|
|
|
|
} |
|
|
|
|
|
|
|
#region 成本明细
|
|
|
|
var orderSku = orderSkus.FirstOrDefault(osku => osku.SkuId == belongSkuId); |
|
|
|
var orderCostDetail = new OrderCostDetail() |
|
|
|
{ |
|
|
|
Id = idGenerator.NewLong(), |
|
|
|
ConsumableAmount = 0, |
|
|
|
CreateTime = DateTime.Now, |
|
|
|
DeductionQuantity = orderSku.ItemTotal.Value, |
|
|
|
DeliveryExpressFreight = 0, |
|
|
|
FirstFreight = 0, |
|
|
|
//OperationAmount = 0,
|
|
|
|
InStorageAmount = 0, |
|
|
|
OutStorageAmount = 0, |
|
|
|
OrderId = request.OrderId, |
|
|
|
ProductId = orderSku.ProductId, |
|
|
|
PurchaseFreight = currentOrderSkuFreightAmount, |
|
|
|
PurchaseOrderId = purchaseOrderSimpleInfo.OrderId, |
|
|
|
SkuAmount = currentOrderSkuProductAmount, |
|
|
|
TotalCost = currentOrderSkuProductAmount + currentOrderSkuFreightAmount, |
|
|
|
SkuId = belongSkuId, |
|
|
|
StorageAmount = 0, |
|
|
|
IsEnabled = true |
|
|
|
}; |
|
|
|
orderCostDetail.CalculationOrderCostDetailCostAndProfit(orderSku.Price.Value * orderSku.ItemTotal.Value, |
|
|
|
orderSku.BuyerPayFreight ?? 0M, |
|
|
|
orderSku.InPackAmount ?? 0M, |
|
|
|
currentSkuAmount, |
|
|
|
currentPurchaseFreight, |
|
|
|
0M, |
|
|
|
0M); |
|
|
|
insertOrderCostDetails.Add(orderCostDetail); |
|
|
|
#endregion
|
|
|
|
} |
|
|
@ -331,7 +345,8 @@ namespace BBWYB.Server.Business |
|
|
|
PurchaserName = cargoParamGroup.PurchaserName, |
|
|
|
ShopId = request.ShopId, |
|
|
|
BelongSkuIds = string.Join(",", belongSkuGroups.Select(bsg => bsg.Key)), |
|
|
|
IsEnabled = true |
|
|
|
IsEnabled = true, |
|
|
|
Remark = !string.IsNullOrEmpty(cargoParamGroup.Remark) ? cargoParamGroup.Remark : request.Remark |
|
|
|
}; |
|
|
|
insertOrderPurchaseInfos.Add(orderPurchaserInfo); |
|
|
|
#endregion
|
|
|
@ -359,17 +374,14 @@ namespace BBWYB.Server.Business |
|
|
|
{ |
|
|
|
OrderId = request.OrderId, |
|
|
|
CreateTime = DateTime.Now, |
|
|
|
DeliveryExpressFreight = 0, |
|
|
|
IsManualEdited = false, |
|
|
|
PlatformCommissionRatio = 0, |
|
|
|
PreferentialAmount = 0, |
|
|
|
PurchaseAmount = totalPurchaseAmount, |
|
|
|
TotalCost = totalPurchaseAmount |
|
|
|
IsManualEdited = false |
|
|
|
}; |
|
|
|
//orderCost.PlatformCommissionAmount = dbOrder.OrderSellerPrice * orderCost.PlatformCommissionRatio;
|
|
|
|
orderCost.Profit = dbOrder.OrderTotalPrice - |
|
|
|
orderCost.PurchaseAmount - |
|
|
|
orderCost.DeliveryExpressFreight; // -orderCost.PlatformCommissionAmount
|
|
|
|
orderCost.CalculationOrderCostCostAndProfit(dbOrder.OrderTotalPrice.Value, |
|
|
|
totalPurchaseProductAmount, |
|
|
|
totalPurchaseFreight, |
|
|
|
0M, |
|
|
|
0M); |
|
|
|
|
|
|
|
if (!isRepurchase) |
|
|
|
{ |
|
|
|
insertOrderCost = fsql.Insert(orderCost); |
|
|
@ -382,12 +394,14 @@ namespace BBWYB.Server.Business |
|
|
|
|
|
|
|
fsql.Transaction(() => |
|
|
|
{ |
|
|
|
//deleteOrderCostDetail.ExecuteAffrows();
|
|
|
|
if (updatePurchaseOrderIdList.Count() > 0) |
|
|
|
fsql.Update<OrderPurchaseInfo>(updatePurchaseOrderIdList).Set(opi => opi.IsEnabled, false).ExecuteAffrows(); |
|
|
|
if (updateOrderCostDetailIdList.Count() > 0) |
|
|
|
fsql.Update<OrderCostDetail>(updateOrderCostDetailIdList).Set(ocd => ocd.IsEnabled, false).ExecuteAffrows(); |
|
|
|
}); |
|
|
|
|
|
|
|
fsql.Transaction(() => |
|
|
|
{ |
|
|
|
fsql.Insert(insertOrderCostDetails).ExecuteAffrows(); |
|
|
|
fsql.Insert(insertOrderPurchaseInfos).ExecuteAffrows(); |
|
|
|
fsql.Insert(insertOrderPurchaseSkuInfos).ExecuteAffrows(); |
|
|
@ -425,7 +439,7 @@ namespace BBWYB.Server.Business |
|
|
|
PurchasePlatform = purchaseOrder.PurchasePlatform.Value, |
|
|
|
PurchaserId = purchaseOrder.PurchaserId, |
|
|
|
PurchaserName = purchaseOrder.PurchaserName, |
|
|
|
PurchaserOrderId = purchaseOrder.PurchaseOrderId, |
|
|
|
PurchaseOrderId = purchaseOrder.PurchaseOrderId, |
|
|
|
IsEnabled = purchaseOrder.IsEnabled, |
|
|
|
PurchaseMethod = purchaseOrder.PurchaseMethod.Value |
|
|
|
}; |
|
|
@ -470,6 +484,9 @@ namespace BBWYB.Server.Business |
|
|
|
var dbPurchaseOrderList = fsql.Select<OrderPurchaseInfo>().Where(opi => opi.OrderId == request.OrderId).ToList(); |
|
|
|
var dbInvalidPurchaseOrderIdList = dbPurchaseOrderList.Where(opi => !opi.IsEnabled).Select(opi => opi.PurchaseOrderId).ToList(); |
|
|
|
|
|
|
|
if (request.AssociationPurchaseOrderList.Any(x => dbInvalidPurchaseOrderIdList.Contains(x.PurchaseOrderId))) |
|
|
|
throw new BusinessException("关联采购单时不能包含历史采购单"); |
|
|
|
|
|
|
|
var dbOrderSkuList = fsql.Select<OrderSku>().Where(osku => osku.OrderId == request.OrderId).ToList(); |
|
|
|
var dbOrderCostDetailList = fsql.Select<OrderCostDetail>().Where(ocd => ocd.OrderId == request.OrderId && ocd.IsEnabled == true).ToList(); |
|
|
|
var dbOrderCost = fsql.Select<OrderCost>(request.OrderId).ToOne(); |
|
|
@ -484,11 +501,13 @@ namespace BBWYB.Server.Business |
|
|
|
|
|
|
|
foreach (var purchaseOrder in request.AssociationPurchaseOrderList) |
|
|
|
{ |
|
|
|
if (dbInvalidPurchaseOrderIdList.Contains(purchaseOrder.PurchaserOrderId)) |
|
|
|
if (dbInvalidPurchaseOrderIdList.Contains(purchaseOrder.PurchaseOrderId)) |
|
|
|
continue; |
|
|
|
|
|
|
|
var avgFreight = purchaseOrder.PurchaseFreight / purchaseOrder.AssocationOrderCostDetailList.Count(); |
|
|
|
var dbPurchaserOrder = dbPurchaseOrderList.FirstOrDefault(x => x.PurchaseOrderId == purchaseOrder.PurchaserOrderId); |
|
|
|
var totalQuantity = purchaseOrder.AssocationOrderCostDetailList.Sum(x => x.PurchaseQuantity); |
|
|
|
|
|
|
|
|
|
|
|
var dbPurchaserOrder = dbPurchaseOrderList.FirstOrDefault(x => x.PurchaseOrderId == purchaseOrder.PurchaseOrderId); |
|
|
|
if (dbPurchaserOrder == null) |
|
|
|
{ |
|
|
|
dbPurchaserOrder = new OrderPurchaseInfo() |
|
|
@ -501,7 +520,7 @@ namespace BBWYB.Server.Business |
|
|
|
BelongSkuIds = string.Join(",", purchaseOrder.AssocationOrderCostDetailList.Select(x => x.SkuId)), |
|
|
|
CreateTime = DateTime.Now, |
|
|
|
PurchaseMethod = Enums.PurchaseMethod.关联外部单, |
|
|
|
PurchaseOrderId = purchaseOrder.PurchaserOrderId, |
|
|
|
PurchaseOrderId = purchaseOrder.PurchaseOrderId, |
|
|
|
PurchasePlatform = purchaseOrder.PurchasePlatform, |
|
|
|
PurchaserId = purchaseOrder.PurchaserId, |
|
|
|
PurchaserName = purchaseOrder.PurchaserName, |
|
|
@ -512,6 +531,7 @@ namespace BBWYB.Server.Business |
|
|
|
foreach (var assOrderCostDetail in purchaseOrder.AssocationOrderCostDetailList) |
|
|
|
{ |
|
|
|
var dbOrderSku = dbOrderSkuList.FirstOrDefault(osku => osku.SkuId == assOrderCostDetail.SkuId); |
|
|
|
var purchaseFreight = purchaseOrder.PurchaseFreight * (1.0M * assOrderCostDetail.PurchaseQuantity / totalQuantity); |
|
|
|
var orderCostDetail = new OrderCostDetail() |
|
|
|
{ |
|
|
|
Id = idGenerator.NewLong(), |
|
|
@ -521,10 +541,15 @@ namespace BBWYB.Server.Business |
|
|
|
OrderId = request.OrderId, |
|
|
|
ProductId = dbOrderSku.ProductId, |
|
|
|
SkuId = assOrderCostDetail.SkuId, |
|
|
|
PurchaseFreight = avgFreight, |
|
|
|
TotalCost = assOrderCostDetail.SkuAmount + avgFreight, |
|
|
|
PurchaseOrderId = purchaseOrder.PurchaserOrderId |
|
|
|
PurchaseOrderId = purchaseOrder.PurchaseOrderId |
|
|
|
}; |
|
|
|
orderCostDetail.CalculationOrderCostDetailCostAndProfit(dbOrderSku.Price.Value * dbOrderSku.ItemTotal.Value, |
|
|
|
dbOrderSku.BuyerPayFreight ?? 0M, |
|
|
|
dbOrderSku.InPackAmount ?? 0M, |
|
|
|
assOrderCostDetail.SkuAmount, |
|
|
|
purchaseFreight, |
|
|
|
0M, |
|
|
|
0M); |
|
|
|
insertOrderCostDetailList.Add(orderCostDetail); |
|
|
|
} |
|
|
|
} |
|
|
@ -540,45 +565,72 @@ namespace BBWYB.Server.Business |
|
|
|
|
|
|
|
foreach (var assOrderCostDetail in purchaseOrder.AssocationOrderCostDetailList) |
|
|
|
{ |
|
|
|
var dbOrderCostDetail = dbOrderCostDetailList.FirstOrDefault(ocd => ocd.SkuId == assOrderCostDetail.SkuId); |
|
|
|
dbOrderCostDetail.SkuAmount = assOrderCostDetail.SkuAmount; |
|
|
|
dbOrderCostDetail.DeductionQuantity = assOrderCostDetail.PurchaseQuantity; |
|
|
|
dbOrderCostDetail.PurchaseFreight = avgFreight; |
|
|
|
dbOrderCostDetail.TotalCost = assOrderCostDetail.SkuAmount + avgFreight; |
|
|
|
updateOrderCostDetailList.Add(fsql.Update<OrderCostDetail>().SetSource(dbOrderCostDetail)); |
|
|
|
var dbOrderSku = dbOrderSkuList.FirstOrDefault(osku => osku.SkuId == assOrderCostDetail.SkuId); |
|
|
|
|
|
|
|
var orderCostDetail = dbOrderCostDetailList.FirstOrDefault(ocd => ocd.SkuId == assOrderCostDetail.SkuId && |
|
|
|
ocd.PurchaseOrderId == purchaseOrder.PurchaseOrderId); |
|
|
|
|
|
|
|
var purchaseFreight = purchaseOrder.PurchaseFreight * (1.0M * assOrderCostDetail.PurchaseQuantity / totalQuantity); |
|
|
|
|
|
|
|
|
|
|
|
if (orderCostDetail == null) |
|
|
|
{ |
|
|
|
orderCostDetail = new OrderCostDetail() |
|
|
|
{ |
|
|
|
Id = idGenerator.NewLong(), |
|
|
|
CreateTime = DateTime.Now, |
|
|
|
DeductionQuantity = assOrderCostDetail.PurchaseQuantity, |
|
|
|
IsEnabled = true, |
|
|
|
OrderId = request.OrderId, |
|
|
|
ProductId = dbOrderSku.ProductId, |
|
|
|
SkuId = assOrderCostDetail.SkuId, |
|
|
|
PurchaseOrderId = purchaseOrder.PurchaseOrderId |
|
|
|
}; |
|
|
|
} |
|
|
|
orderCostDetail.DeductionQuantity = assOrderCostDetail.PurchaseQuantity; |
|
|
|
orderCostDetail.CalculationOrderCostDetailCostAndProfit(dbOrderSku.Price.Value * dbOrderSku.ItemTotal.Value, |
|
|
|
dbOrderSku.BuyerPayFreight ?? 0M, |
|
|
|
dbOrderSku.InPackAmount ?? 0M, |
|
|
|
assOrderCostDetail.SkuAmount, |
|
|
|
purchaseFreight, |
|
|
|
orderCostDetail.OutPackAmount ?? 0M, |
|
|
|
orderCostDetail.DeliveryExpressFreight ?? 0M); |
|
|
|
|
|
|
|
if (orderCostDetail == null) |
|
|
|
insertOrderCostDetailList.Add(orderCostDetail); |
|
|
|
else |
|
|
|
updateOrderCostDetailList.Add(fsql.Update<OrderCostDetail>().SetSource(orderCostDetail)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
var totalPurchaseAmount = request.AssociationPurchaseOrderList.Where(x => !dbInvalidPurchaseOrderIdList.Contains(x.PurchaserOrderId)) |
|
|
|
.Sum(p => p.PurchaseAmount + p.PurchaseFreight); |
|
|
|
var profit = dbOrder.OrderTotalPrice - totalPurchaseAmount - (dbOrderCost?.DeliveryExpressFreight ?? 0); // -orderCost.PlatformCommissionAmount
|
|
|
|
//var totalPurchaseAmount = request.AssociationPurchaseOrderList.Where(x => !dbInvalidPurchaseOrderIdList.Contains(x.PurchaseOrderId))
|
|
|
|
// .Sum(p => p.PurchaseAmount + p.PurchaseFreight);
|
|
|
|
//var profit = dbOrder.OrderTotalPrice - totalPurchaseAmount - (dbOrderCost?.DeliveryExpressFreight ?? 0); // -orderCost.PlatformCommissionAmount
|
|
|
|
|
|
|
|
if (dbOrderCost == null) |
|
|
|
{ |
|
|
|
dbOrderCost = new OrderCost() |
|
|
|
{ |
|
|
|
OrderId = request.OrderId, |
|
|
|
DeliveryExpressFreight = 0, |
|
|
|
IsManualEdited = false, |
|
|
|
PlatformCommissionAmount = 0, |
|
|
|
PlatformCommissionRatio = 0, |
|
|
|
PreferentialAmount = 0, |
|
|
|
CreateTime = DateTime.Now, |
|
|
|
PurchaseAmount = totalPurchaseAmount, |
|
|
|
TotalCost = totalPurchaseAmount, |
|
|
|
Profit = profit |
|
|
|
}; |
|
|
|
insertOrderCost = fsql.Insert(dbOrderCost); |
|
|
|
} |
|
|
|
|
|
|
|
var totalPurchaseProductAmount = request.AssociationPurchaseOrderList.Sum(x => x.PurchaseAmount); |
|
|
|
var totalPurchaseFreight = request.AssociationPurchaseOrderList.Sum(x => x.PurchaseFreight); |
|
|
|
var totalOutPackAmount = dbOrderCostDetailList.Sum(ocd => ocd.OutPackAmount); |
|
|
|
dbOrderCost.CalculationOrderCostCostAndProfit(dbOrder.OrderTotalPrice.Value, |
|
|
|
totalPurchaseProductAmount, |
|
|
|
totalPurchaseFreight, |
|
|
|
totalOutPackAmount ?? 0M, |
|
|
|
0M); |
|
|
|
|
|
|
|
if (dbOrderCost == null) |
|
|
|
insertOrderCost = fsql.Insert(dbOrderCost); |
|
|
|
else |
|
|
|
{ |
|
|
|
dbOrderCost.PurchaseAmount = totalPurchaseAmount; |
|
|
|
dbOrderCost.Profit = profit; |
|
|
|
updateOrderCost = fsql.Update<OrderCost>(request.OrderId).Set(oc => oc.PurchaseAmount, totalPurchaseAmount) |
|
|
|
.Set(oc => oc.TotalCost, totalPurchaseAmount) |
|
|
|
.Set(oc => oc.Profit, profit); |
|
|
|
} |
|
|
|
updateOrderCost = fsql.Update<OrderCost>().SetSource(dbOrderCost); |
|
|
|
|
|
|
|
fsql.Transaction(() => |
|
|
|
{ |
|
|
@ -967,12 +1019,15 @@ namespace BBWYB.Server.Business |
|
|
|
if (dbOrderPurchaseRelationInfos.Count() == 0) |
|
|
|
throw new Exception($"采购单{purchaseOrderId}-未查询到采购单关联明细,手动关联的采购单不支持改价"); |
|
|
|
|
|
|
|
var dbOrderSkus = fsql.Select<OrderSku>().Where(osku => osku.OrderId == orderPurchaseInfo.OrderId).ToList(); |
|
|
|
|
|
|
|
List<IUpdate<OrderCostDetail>> updateOrderCostDetailList = new List<IUpdate<OrderCostDetail>>(); |
|
|
|
IUpdate<OrderCost> updateOrderCost = null; |
|
|
|
|
|
|
|
var client = ppPlatformClientFactory.GetClient(AdapterEnums.PlatformType.阿里巴巴); |
|
|
|
|
|
|
|
var totalPurchaseAmount = 0M; |
|
|
|
var totalPurchaseProductAmount = 0M; |
|
|
|
var totalPurchaseFreight = 0M; |
|
|
|
foreach (var opi in dbOrderPurchaseInfoList) |
|
|
|
{ |
|
|
|
var purchaseOrderSimpleInfo = client.QueryOrderDetail(new PP_QueryOrderDetailRequest() |
|
|
@ -982,43 +1037,60 @@ namespace BBWYB.Server.Business |
|
|
|
AppToken = purchaseAccount.AppToken, |
|
|
|
OrderId = opi.PurchaseOrderId |
|
|
|
}); |
|
|
|
totalPurchaseAmount += purchaseOrderSimpleInfo.TotalAmount; |
|
|
|
|
|
|
|
totalPurchaseProductAmount += purchaseOrderSimpleInfo.ProductAmount; |
|
|
|
totalPurchaseFreight += purchaseOrderSimpleInfo.FreightAmount; |
|
|
|
|
|
|
|
var purchaseSkuTotalQuantity = purchaseOrderSimpleInfo.ItemList.Sum(x => x.Quantity); |
|
|
|
var currentPurchaseOrderRelationInfos = dbOrderPurchaseRelationInfos.Where(x => x.PurchaseOrderId == opi.PurchaseOrderId).ToList(); |
|
|
|
|
|
|
|
var belongSkuGroups = currentPurchaseOrderRelationInfos.GroupBy(p => p.BelongSkuId); |
|
|
|
foreach (var belongSkuGroup in belongSkuGroups) |
|
|
|
{ |
|
|
|
var belongSkuId = belongSkuGroup.Key; |
|
|
|
var currentOrderSkuProductAmount = 0M; //采购成本
|
|
|
|
var currentOrderSkuCargoParamList = belongSkuGroup.ToList(); //找当前skuId的采购skuId
|
|
|
|
var currentOrderSkuProductAmount = 0M; //采购成本
|
|
|
|
var currentSkuTotalPurchaseQuantity = currentOrderSkuCargoParamList.Sum(x => x.Quantity); //当前skuId的采购数量总和
|
|
|
|
var currentPurchaseFreight = purchaseOrderSimpleInfo.FreightAmount * |
|
|
|
(1.0M * currentSkuTotalPurchaseQuantity / purchaseSkuTotalQuantity); //采购运费
|
|
|
|
|
|
|
|
foreach (var currentOrderSkuCargo in currentOrderSkuCargoParamList) |
|
|
|
{ |
|
|
|
var purchaseSkuProductAmount = purchaseOrderSimpleInfo.ItemList.Where(p => p.SkuId == currentOrderSkuCargo.PurchaseSkuId) |
|
|
|
var currentPurchaseSkuProductAmount = purchaseOrderSimpleInfo.ItemList.Where(p => p.SkuId == currentOrderSkuCargo.PurchaseSkuId) |
|
|
|
.Sum(p => p.ProductAmount); |
|
|
|
var purchaseSkuTotalQuantity = purchaseOrderSimpleInfo.ItemList.Where(p => p.SkuId == currentOrderSkuCargo.PurchaseSkuId) |
|
|
|
var currentPurchaseSkuTotalQuantity = purchaseOrderSimpleInfo.ItemList.Where(p => p.SkuId == currentOrderSkuCargo.PurchaseSkuId) |
|
|
|
.Sum(p => p.Quantity); |
|
|
|
currentOrderSkuProductAmount += purchaseSkuProductAmount * (1.0M * currentOrderSkuCargo.Quantity.Value / purchaseSkuTotalQuantity); |
|
|
|
currentOrderSkuProductAmount += currentPurchaseSkuProductAmount * (1.0M * currentOrderSkuCargo.Quantity.Value / currentPurchaseSkuTotalQuantity); |
|
|
|
} |
|
|
|
|
|
|
|
var currentOrderSkuFreightAmount = purchaseOrderSimpleInfo.FreightAmount / belongSkuGroups.Count(); //采购运费(按sku数均分)
|
|
|
|
|
|
|
|
var dbOrderSkuDetail = dbOrderCostDetails.FirstOrDefault(ocd => ocd.SkuId == belongSkuId); |
|
|
|
dbOrderSkuDetail.SkuAmount = currentOrderSkuProductAmount; |
|
|
|
dbOrderSkuDetail.PurchaseFreight = currentOrderSkuFreightAmount; |
|
|
|
dbOrderSkuDetail.TotalCost = currentOrderSkuProductAmount + currentOrderSkuFreightAmount; |
|
|
|
var orderSku = dbOrderSkus.FirstOrDefault(osku => osku.SkuId == belongSkuId); |
|
|
|
//dbOrderSkuDetail.SkuAmount = currentOrderSkuProductAmount;
|
|
|
|
//dbOrderSkuDetail.PurchaseFreight = currentOrderSkuFreightAmount;
|
|
|
|
//dbOrderSkuDetail.TotalCost = currentOrderSkuProductAmount + currentOrderSkuFreightAmount;
|
|
|
|
|
|
|
|
dbOrderSkuDetail.CalculationOrderCostDetailCostAndProfit(orderSku.Price.Value * orderSku.ItemTotal.Value, |
|
|
|
orderSku.BuyerPayFreight ?? 0M, |
|
|
|
orderSku.InPackAmount ?? 0M, |
|
|
|
currentOrderSkuProductAmount, |
|
|
|
currentPurchaseFreight ?? 0M, |
|
|
|
dbOrderSkuDetail.OutPackAmount ?? 0M, |
|
|
|
dbOrderSkuDetail.DeductionQuantity ?? 0M); |
|
|
|
updateOrderCostDetailList.Add(fsql.Update<OrderCostDetail>().SetSource(dbOrderSkuDetail)); |
|
|
|
} |
|
|
|
|
|
|
|
dbOrderCost.TotalCost = dbOrderCost.PurchaseAmount = totalPurchaseAmount; |
|
|
|
dbOrderCost.Profit = dbOrder.OrderTotalPrice - |
|
|
|
dbOrderCost.PurchaseAmount - |
|
|
|
dbOrderCost.DeliveryExpressFreight; // -orderCost.PlatformCommissionAmount
|
|
|
|
updateOrderCost = fsql.Update<OrderCost>(dbOrderCost.OrderId) |
|
|
|
.Set(oc => oc.PurchaseAmount, dbOrderCost.PurchaseAmount) |
|
|
|
.Set(oc => oc.TotalCost, dbOrderCost.TotalCost) |
|
|
|
.Set(oc => oc.Profit, dbOrderCost.Profit); |
|
|
|
//dbOrderCost.TotalCost = dbOrderCost.PurchaseAmount = totalPurchaseAmount;
|
|
|
|
//dbOrderCost.Profit = dbOrder.OrderTotalPrice -
|
|
|
|
// dbOrderCost.PurchaseAmount -
|
|
|
|
// dbOrderCost.DeliveryExpressFreight;
|
|
|
|
|
|
|
|
|
|
|
|
dbOrderCost.CalculationOrderCostCostAndProfit(dbOrder.OrderTotalPrice ?? 0M, |
|
|
|
totalPurchaseProductAmount, |
|
|
|
totalPurchaseFreight, |
|
|
|
dbOrderCostDetails.Sum(ocd => ocd.OutPackAmount ?? 0M), |
|
|
|
dbOrderCostDetails.Sum(ocd => ocd.DeliveryExpressFreight ?? 0M)); |
|
|
|
updateOrderCost = fsql.Update<OrderCost>().SetSource(dbOrderCost); |
|
|
|
|
|
|
|
fsql.Transaction(() => |
|
|
|
{ |
|
|
|