feat(OrderLogic): 重构订单逻辑,支持新活动状态与库存计算,优化利润计算方式

This commit is contained in:
mkm 2024-06-09 18:35:54 +08:00
parent a329c7ef5b
commit fa055acdea

View File

@ -59,14 +59,14 @@ class OrderLogic extends BaseLogic
}
try {
self::$total = 0;
self::$cost = 0;//成本
self::$profit = 0;//利润
self::$activity_price = 0;//活动减少
self::$cost = 0; //成本
self::$profit = 0; //利润
self::$activity_price = 0; //活动减少
/** 计算价格 */
$check = DictType::where('type', 'activities')->find();
foreach ($cart_select as $k => $v) {
$find = StoreBranchProduct::where(['product_id' => $v['product_id'],'store_id'=>$params['store_id']])->field('store_name,image,unit,price,cost,product_id')->find();
$find = StoreBranchProduct::where(['product_id' => $v['product_id'], 'store_id' => $params['store_id']])->field('store_name,image,unit,price,cost,product_id')->find();
if (!$find) {
continue;
}
@ -75,16 +75,16 @@ class OrderLogic extends BaseLogic
$cart_select[$k]['price'] = $find['price'];
if (isset($check) && $check['status'] == 1) {
//零售价*折扣率
$activity_price=bcmul($find['price'],0.9,2);
self::$activity_price = bcadd(self::$activity_price, bcsub($find['price'],$activity_price,2), 2);
$activity_price = bcmul($find['price'], 0.9, 2);
self::$activity_price = bcadd(self::$activity_price, bcsub($find['price'], $activity_price, 2), 2);
$find['price'] = $activity_price;
}
//利润
$onePrice = bcsub($find['price'], $find['cost'], 2);
$cart_select[$k]['profit'] = bcmul($v['cart_num'], $onePrice, 2);//利润
$cart_select[$k]['profit'] = bcmul($v['cart_num'], $onePrice, 2); //利润
$cart_select[$k]['cost'] = bcmul($v['cart_num'], $find['cost'], 2) ?? 0;
$cart_select[$k]['total'] = bcmul($v['cart_num'], $find['price'], 2);//钱
$cart_select[$k]['total_price'] = bcmul($v['cart_num'], $find['price'], 2);//钱
$cart_select[$k]['total'] = bcmul($v['cart_num'], $find['price'], 2); //钱
$cart_select[$k]['total_price'] = bcmul($v['cart_num'], $find['price'], 2); //钱
$cart_select[$k]['product_id'] = $find['product_id'];
$cart_select[$k]['old_cart_id'] = $v['id'];
$cart_select[$k]['cart_num'] = $v['cart_num'];
@ -97,7 +97,7 @@ class OrderLogic extends BaseLogic
// if ($vipPrice) {
// $cartInfo['price'] = $vipPrice;
// }
$cartInfo['vip_price'] = 0;//$cart_select[$k]['total'] - $vipPrice ?? 0;
$cartInfo['vip_price'] = 0; //$cart_select[$k]['total'] - $vipPrice ?? 0;
$cart_select[$k]['cart_info'] = json_encode($cartInfo);
//理论上每笔都是拆分了
$cart_select[$k]['name'] = $find['store_name'];
@ -109,16 +109,16 @@ class OrderLogic extends BaseLogic
}
//TODO 收单打9.9折 会员按照比例打折 等级按照充值去升级
$pay_price = self::$total;
// $check = StoreOrder::where('uid',\request()->userId)->count();//首单逻辑
// $check = StoreOrder::where('uid',\request()->userId)->count();//首单逻辑
$vipPrice = 0;
// if (isset($check) && $check['status'] == 1) {
// $discountRate = '0.99';//首单逻辑
// $pay_price 是价格
// $discountRate = $check['remark'];//折扣
// $discountRate = bcdiv($discountRate, '10', 2);
// $pay_price = bcdiv(bcmul($pay_price, $discountRate, 4), '1', 2);
// $discountRate = '0.99';//首单逻辑
// $pay_price 是价格
// $discountRate = $check['remark'];//折扣
// $discountRate = bcdiv($discountRate, '10', 2);
// $pay_price = bcdiv(bcmul($pay_price, $discountRate, 4), '1', 2);
// } else {
/* $userVip = User::where('id', \request()->userId)->value('user_ship');
/* $userVip = User::where('id', \request()->userId)->value('user_ship');
if ($userVip) {
switch ($userVip) {
case UserShipEnum::VIP1:
@ -152,24 +152,24 @@ class OrderLogic extends BaseLogic
$order = [
'create_time' => time(),
'order_id' => getNewOrderId('PF'),
'total_price' => self::$total,//总价
'cost' => self::$cost,//成本价
'profit' => self::$profit,//利润
'pay_price' => $pay_price,//后期可能有降价抵扣
'total_price' => self::$total, //总价
'cost' => self::$cost, //成本价
'profit' => self::$profit, //利润
'pay_price' => $pay_price, //后期可能有降价抵扣
'vip_price' => $vipPrice,
'total_num' => count($cart_select),//总数
'total_num' => count($cart_select), //总数
'pay_type' => $params['pay_type'] ?? 0,
'reservation_time' => $params['reservation_time'] ?? null,
'cart_id' => implode(',', $cartId),
'store_id' => $params['store_id'] ?? 0,
'shipping_type' => $params['shipping_type'] ?? 1,//配送方式 1=快递 2=门店自提
'activity' =>'减免',
'activity_price' =>self::$activity_price,
'activities'=>$check['status'],
'shipping_type' => $params['shipping_type'] ?? 1, //配送方式 1=快递 2=门店自提
'activity' => '减免',
'activity_price' => self::$activity_price,
'activities' => $check['status'],
];
$order['default_delivery'] = 0;
if($params['store_id']){
$order['default_delivery'] = SystemStore::where('id',$params['store_id'])->value('is_store');
if ($params['store_id']) {
$order['default_delivery'] = SystemStore::where('id', $params['store_id'])->value('is_store');
}
} catch (\Exception $e) {
self::setError($e->getMessage());
@ -210,9 +210,9 @@ class OrderLogic extends BaseLogic
$_order['user_phone'] = $address['phone'];
$_order['user_address'] = $address['detail'];
}
}else{
} else {
//没有地址时,默认为门店自提
$_order['status']=1;
$_order['status'] = 1;
}
Db::startTrans();
@ -223,7 +223,7 @@ class OrderLogic extends BaseLogic
$goods_list[$k]['oid'] = $order->id;
$goods_list[$k]['uid'] = request()->userId;
$goods_list[$k]['cart_id'] = implode(',', $cartId);
$goods_list[$k]['delivery_id'] = $params['store_id'];//商家id
$goods_list[$k]['delivery_id'] = $params['store_id']; //商家id
}
(new StoreOrderCartInfo())->saveAll($goods_list);
$where = ['is_pay' => 0];
@ -367,15 +367,18 @@ class OrderLogic extends BaseLogic
if ($find) {
$find['goods_list'] = StoreOrderCartInfo::where('oid', $find['id'])
->with('goodsName')
->field('product_id,cart_num nums')->select()->each(function ($item) {
->field('product_id,cart_num nums,store_id')->select()->each(function ($item) use($find) {
$find = StoreBranchProduct::where('product_id', $item['product_id'])->where('store_id', $find['store_id'])->find();
$item['store_name'] = $find['store_name'];
$item['image'] = $find['image'];
$item['price'] = $find['price'];
$item['unit_name'] = StoreProductUnit::where('id', $find['unit'])->value('name') ?? '';
$item['msg'] = '预计48小时发货';
$item['unit_name'] = StoreProductUnit::where('id', $item['unit'])->value('name');
return $item;
});
$store = SystemStore::where('id', $find['store_id'])->field('id,name,phone,address,detailed_address')->find();
$find['store_info'] = $store;
}
return $find;
}
@ -390,8 +393,6 @@ class OrderLogic extends BaseLogic
'refund_cancle_time' => date('Y-m-d H:i:s', time())
]
);
}
@ -438,7 +439,6 @@ class OrderLogic extends BaseLogic
self::setError($e->getMessage());
return false;
}
}
@ -478,7 +478,6 @@ class OrderLogic extends BaseLogic
'no_send' => $no_send,
'send' => $send
];
}
public static function getOne($code)
@ -490,7 +489,6 @@ class OrderLogic extends BaseLogic
return $item; //返回处理后的数据。
})
->toArray();
}
public static function write_list($info, $status, $params)
@ -548,7 +546,7 @@ class OrderLogic extends BaseLogic
//todo 单子不是完成的不允许退款
//单笔不拆单子直接修改状态
$order = StoreOrder::where('id', $params['id'])->withTrashed()->findOrEmpty();
$params['refund_num'] = 1;//todo 拿实际数量
$params['refund_num'] = 1; //todo 拿实际数量
if (count($params['old_cart_id']) == 1) {
$order->refund_status = OrderEnum::REFUND_STATUS_YES;
$order->status = OrderEnum::ALREADY_REFUND;
@ -588,14 +586,13 @@ class OrderLogic extends BaseLogic
}
$refundOrder = self::dealCreateRefundOrder($order, $params);
self::dealChangeCartInfo($refundOrder);
// d($leftOrder,$refundOrder);
// d($leftOrder,$refundOrder);
Db::commit();
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
return 1;
@ -666,7 +663,7 @@ class OrderLogic extends BaseLogic
$order['refund_status'] = OrderEnum::REFUND_STATUS_YES;
$order['status'] = OrderEnum::ALREADY_REFUND;
$order['refund_num'] = $params['refund_num'];//按数量整单退剩余的
$order['refund_num'] = $params['refund_num']; //按数量整单退剩余的
$order['refund_reason_wap_explain'] = $params['refund_message'] ?? '';
$order['mark'] = $params['mark'] ?? '';
$order['total_num'] = count($params['old_cart_id']);
@ -697,7 +694,6 @@ class OrderLogic extends BaseLogic
$order['update_time'] = null;
$order['delete_time'] = null;
return StoreOrder::create($order);
}
@ -705,7 +701,7 @@ class OrderLogic extends BaseLogic
{
$check = DictType::where('type', 'activities')->find();
if (isset($check) && $check['status'] == 1) {
// $discountRate = '0.99';//首单逻辑
// $discountRate = '0.99';//首单逻辑
$discountRate = $check['remark'];
$discountRate = bcdiv($discountRate, '100', 2);
$pay_price = bcdiv(bcmul($pay_price, $discountRate, 4), '1', 2);
@ -736,8 +732,5 @@ class OrderLogic extends BaseLogic
}
}
return $pay_price;
}
}