Browse Source

改价回调

qianyi
shanji 3 years ago
parent
commit
696f479234
  1. 103
      BBWY.Server.Business/PurchaseOrder/PurchaseOrderBusiness.cs

103
BBWY.Server.Business/PurchaseOrder/PurchaseOrderBusiness.cs

@ -343,7 +343,7 @@ namespace BBWY.Server.Business
insertOrderCost = fsql.Insert(orderCost);
#endregion
#region 采购信息
#region 代发信息
var orderDropShipping = new OrderDropShipping()
{
OrderId = createOnlinePurchaseOrderRequest.OrderId,
@ -532,15 +532,6 @@ namespace BBWY.Server.Business
throw new Exception("未查询到代发信息");
#endregion
#region 查询采购账号
currentProgress = "查询采购账号";
var purchaseAccount = fsql.Select<PurchaseAccount>().WhereIf(orderDropshipping.PurchaseAccountId != 0, pa => pa.Id == orderDropshipping.PurchaseAccountId)
.WhereIf(orderDropshipping.PurchaseAccountId == 0, pa => pa.AccountName == orderDropshipping.BuyerAccount)
.Where(pa => pa.PurchasePlatformId == callbackPlatform).ToOne();
if (purchaseAccount == null)
throw new Exception($"未查询到采购账号{orderDropshipping.BuyerAccount}");
#endregion
#region 查询订单Sku
currentProgress = "查询订单Sku";
var orderSkuList = fsql.Select<OrderSku>().Where(osku => osku.OrderId == orderDropshipping.OrderId).ToList();
@ -548,19 +539,20 @@ namespace BBWY.Server.Business
throw new BusinessException("订单Sku不存在");
#endregion
#region 查询订单
currentProgress = "查询订单";
var dbOrder = fsql.Select<Order>(orderDropshipping.OrderId).ToOne();
if (dbOrder == null)
throw new BusinessException("订单不存在");
#endregion
#region 查询采购单
currentProgress = "查询采购单";
var purchaseOrderList = fsql.Select<PurchaseOrder>().Where(po => po.PurchaseOrderId == purchaseOrderId &&
po.PurchaseMethod == Enums.PurchaseMethod.线).ToList();
var purchaseOrderList = fsql.Select<PurchaseOrder>().Where(po => po.PurchaseOrderId == purchaseOrderId).ToList();
if (purchaseOrderList == null || purchaseOrderList.Count() == 0)
throw new BusinessException("采购单不存在");
if (orderSkuList.Count() > 1 && purchaseOrderList.Any(p => p.PurchaseMethod == Enums.PurchaseMethod.线))
throw new Exception("多sku订单关联采购单不支持改价");
#endregion
#region 查询成本
currentProgress = "查询成本";
var orderCost = fsql.Select<OrderCost>(orderDropshipping.OrderId).ToOne();
if (orderCost == null)
throw new BusinessException("订单成本不存在");
#endregion
#region 查询成本明细
@ -570,11 +562,13 @@ namespace BBWY.Server.Business
throw new BusinessException("订单成本明细不存在");
#endregion
#region 查询订单成本
currentProgress = "查询订单成本";
var orderCost = fsql.Select<OrderCost>(orderDropshipping.OrderId).ToOne();
if (orderCost == null)
throw new BusinessException("订单成本不存在");
#region 查询采购账号
currentProgress = "查询采购账号";
var purchaseAccount = fsql.Select<PurchaseAccount>().WhereIf(orderDropshipping.PurchaseAccountId != 0, pa => pa.Id == orderDropshipping.PurchaseAccountId)
.WhereIf(orderDropshipping.PurchaseAccountId == 0, pa => pa.AccountName == orderDropshipping.BuyerAccount)
.Where(pa => pa.PurchasePlatformId == callbackPlatform).ToOne();
if (purchaseAccount == null)
throw new Exception($"未查询到采购账号{orderDropshipping.BuyerAccount}");
#endregion
#region 查询改价后的订单金额
@ -589,19 +583,70 @@ namespace BBWY.Server.Business
});
#endregion
#region 查询采购单明细
currentProgress = "查询采购单明细";
var purchaseOrderDetails = fsql.Select<PurchaseOrderDetail>().Where(p => p.OrderId == orderDropshipping.OrderId);
#endregion
#region 查询订单
currentProgress = "查询订单";
var dbOrder = fsql.Select<Order>(orderDropshipping.OrderId).ToOne();
if (dbOrder == null)
throw new BusinessException("订单不存在");
#endregion
IList<IUpdate<PurchaseOrder>> updatePurchaseOrders = new List<IUpdate<PurchaseOrder>>();
IList<IUpdate<OrderCostDetail>> updateOrderCostDetails = new List<IUpdate<OrderCostDetail>>();
foreach (var orderSku in orderSkuList)
{
var currentOrderSkuProductAmount = 0M; //采购成本
if (orderSkuList.Count() != 1)
{
var currentOrderSkuPurchaseOrderDetails = purchaseOrderDetails.Where(p => p.SkuId == orderSku.SkuId); //找当前skuId的采购skuId
currentOrderSkuProductAmount = purchaseOrderSimpleInfo.ItemList.Where(p => currentOrderSkuPurchaseOrderDetails.Any(p1 => p1.PurchaseSkuId == p.SkuId))
?.Sum(p => p.ProductAmount) ?? 0M;
}
else
{
currentOrderSkuProductAmount = purchaseOrderSimpleInfo.ProductAmount;
}
var currentOrderSkuFreightAmount = purchaseOrderSimpleInfo.FreightAmount / orderSkuList.Count(); //采购运费(按sku数均分)
var purchaseOrder = purchaseOrderList.FirstOrDefault(po => po.SkuId == orderSku.SkuId);
var orderCostDetail = orderCostDetailList.FirstOrDefault(oc => oc.PurchaseOrderPKId == purchaseOrder.Id);
purchaseOrder.SingleSkuAmount = currentOrderSkuProductAmount / orderSku.ItemTotal.Value;
purchaseOrder.SingleFreight = currentOrderSkuFreightAmount / orderSku.ItemTotal.Value;
orderCostDetail.SkuAmount = currentOrderSkuProductAmount;
orderCostDetail.PurchaseFreight = currentOrderSkuFreightAmount;
orderCostDetail.UnitCost = purchaseOrder.UnitCost;
orderCostDetail.TotalCost = currentOrderSkuProductAmount + currentOrderSkuFreightAmount;
updatePurchaseOrders.Add(fsql.Update<PurchaseOrder>().SetSource(purchaseOrder));
updateOrderCostDetails.Add(fsql.Update<OrderCostDetail>().SetSource(orderCostDetail));
}
orderCost.PurchaseAmount = purchaseOrderSimpleInfo.TotalAmount;
orderCost.Profit = dbOrder.OrderSellerPrice +
dbOrder.FreightPrice -
orderCost.PurchaseAmount -
orderCost.DeliveryExpressFreight -
orderCost.PlatformCommissionAmount;
orderDropshipping.PurchaseAmount = purchaseOrderSimpleInfo.TotalAmount;
fsql.Transaction(() =>
{
//fsql.Update<PurchaseOrder>(purchaseOrderId).Set(po => po.SingleSkuAmount, orderAmountResponse.ProductAmount / orderSku.ItemTotal.Value)
// .Set(po => po.SingleFreight, orderAmountResponse.FreightAmount / orderSku.ItemTotal.Value)
// .ExecuteAffrows();
//fsql.Update<OrderCostDetail>().Where(ocd=>)
foreach (var update in updatePurchaseOrders)
update.ExecuteAffrows();
foreach (var update in updateOrderCostDetails)
update.ExecuteAffrows();
fsql.Update<OrderCost>(orderCost.OrderId).SetSource(orderCost).ExecuteAffrows();
fsql.Update<OrderDropShipping>(orderDropshipping.OrderId).SetSource(orderDropshipping).ExecuteAffrows();
});
}
catch (Exception ex)

Loading…
Cancel
Save