feat(OrderLogic, CartLogic): 重构订单与购物车逻辑,支持新活动状态与库存计算
This commit is contained in:
parent
909e66b1f3
commit
a329c7ef5b
@ -61,7 +61,8 @@ class CartList extends BaseAdminDataLists implements ListsSearchInterface, Lists
|
||||
// $check = DictType::where('type', 'activities')->find();
|
||||
|
||||
foreach ($list as $key => &$item) {
|
||||
$find = StoreBranchProduct::where(['id' => $item['product_id']])
|
||||
|
||||
$find = StoreBranchProduct::where(['product_id' => $item['product_id'],'store_id' => $item['store_id']])
|
||||
->field('product_id,image,price,cost,store_name,unit')
|
||||
->find();
|
||||
// if (isset($check) && $check['status'] == 1) {
|
||||
|
@ -26,6 +26,10 @@ class CartLogic extends BaseLogic
|
||||
*/
|
||||
public static function add(array $params): bool
|
||||
{
|
||||
if($params['store_id']<=0){
|
||||
self::setError('门店ID不能为空');
|
||||
return false;
|
||||
}
|
||||
Db::startTrans();
|
||||
try {
|
||||
Cart::create([
|
||||
|
@ -52,7 +52,7 @@ class OrderLogic extends BaseLogic
|
||||
{
|
||||
|
||||
$where = ['is_pay' => 0];
|
||||
$cart_select = Cart::whereIn('id', $cartId)->where($where)->field('id,product_id as goods,cart_num')->select()->toArray();
|
||||
$cart_select = Cart::whereIn('id', $cartId)->where($where)->field('id,product_id,cart_num')->select()->toArray();
|
||||
if (empty($cart_select)) {
|
||||
self::setError('购物车为空');
|
||||
return false;
|
||||
@ -66,7 +66,7 @@ class OrderLogic extends BaseLogic
|
||||
/** 计算价格 */
|
||||
$check = DictType::where('type', 'activities')->find();
|
||||
foreach ($cart_select as $k => $v) {
|
||||
$find = StoreBranchProduct::where(['id' => $v['goods']])->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;
|
||||
}
|
||||
@ -237,110 +237,6 @@ class OrderLogic extends BaseLogic
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 获取购货订单购物车商品信息
|
||||
* @param $params
|
||||
* @return array
|
||||
*/
|
||||
static public function cartIdByPurchaseOrderInfo($user, $params)
|
||||
{
|
||||
if (!$user) {
|
||||
self::setError('没有用户信息,请先登录');
|
||||
return false;
|
||||
}
|
||||
$mer_id = $user['merchant']['mer_id'];
|
||||
$where1 = ['paid' => 1, 'is_opurchase' => 0];
|
||||
$where1[] = ['pay_type', '<>', 9];
|
||||
|
||||
$arrs = Cashierclass::where('merchant', $mer_id)->whereDay('create_time')->where($where1)->column('cart_id,id,address_id');
|
||||
// $order_id = Cashierclass::where('merchant', $mer_id)->whereDay('create_time')->where($where1)->column('id');
|
||||
$cart_arr = [];
|
||||
$order_id = [];
|
||||
foreach ($arrs as $k => $v) {
|
||||
if (empty($v['cart_id'])) {
|
||||
self::setError('没有购物车信息');
|
||||
return false;
|
||||
}
|
||||
// if (empty($v['address_id'])) {
|
||||
// self::setError('请先设置配送地址');
|
||||
// return false;
|
||||
// }
|
||||
$arr = explode(',', $v['cart_id']);
|
||||
foreach ($arr as $kk => $vv) {
|
||||
$cart_arr[] = $vv;
|
||||
}
|
||||
$order_id[] = $v['id'];
|
||||
}
|
||||
$where = ['is_pay' => 1, 'is_fail' => 0];
|
||||
$cart_select = Cart::whereIn('cart_id', $cart_arr)->where($where)->field('goods_id as goods,cart_num')->select()->toArray();
|
||||
if (empty($cart_select)) {
|
||||
self::setError('购物车为空');
|
||||
return false;
|
||||
}
|
||||
/** 计算价格 */
|
||||
foreach ($cart_select as $k => $v) {
|
||||
$sell = Goods::where(['id' => $v['goods']])->value('sell');
|
||||
$cart_select[$k]['total'] = bcmul($v['cart_num'], $sell, 2);
|
||||
$cart_select[$k]['price'] = $sell;
|
||||
}
|
||||
$order = [
|
||||
'time' => time(),
|
||||
'number' => static::getNewOrderId('CG'),
|
||||
'total' => array_sum(array_column($cart_select, 'total')),
|
||||
'pay_type' => $params['pay_type'] ?? 0,
|
||||
'cart_id' => implode(',', $cart_arr),
|
||||
'order_arr' => implode(',', $order_id)
|
||||
];
|
||||
return ['order' => $order, 'cart_list' => $cart_select];
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建购货订单
|
||||
* @return Object|bool
|
||||
*/
|
||||
static public function createOpurchaseOrder($user = null, $params = [])
|
||||
{
|
||||
|
||||
if (!$user) {
|
||||
self::setError('没有用户信息,请先登录');
|
||||
return false;
|
||||
}
|
||||
$mer_id = $user['merchant']['mer_id'];
|
||||
// $merchant = Merchant::where('mer_id', $mer_id)->find();
|
||||
$orderInfo = self::cartIdByPurchaseOrderInfo($user, $params);
|
||||
if (!$orderInfo) {
|
||||
return false;
|
||||
}
|
||||
$_order = $orderInfo['order'];
|
||||
// if ($_order['total'] < $merchant['mer_money']) {
|
||||
// self::setError('商户余额不足');
|
||||
// return false;
|
||||
// }
|
||||
$_order['merchant'] = $mer_id;
|
||||
$_order['money'] = $_order['total'];
|
||||
$_order['actual'] = $_order['total'];
|
||||
$_order['paid'] = 1;
|
||||
Db::startTrans();
|
||||
try {
|
||||
$order = Opurchaseclass::create($_order);
|
||||
|
||||
$goods_list = $orderInfo['cart_list'];
|
||||
foreach ($goods_list as $k => $v) {
|
||||
$goods_list[$k]['nums'] = $v['cart_num'];
|
||||
$goods_list[$k]['pid'] = $order->id;
|
||||
}
|
||||
(new Opurchaseinfo())->saveAll($goods_list);
|
||||
// $merchant->mer_money = bcsub($merchant->mer_money, $_order['total'], 2);
|
||||
$order_arr = explode(',', $_order['order_arr']);
|
||||
Cashierclass::where('id', 'in', $order_arr)->update(['is_opurchase' => 1]);
|
||||
Db::commit();
|
||||
return $order;
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 获取订单号
|
||||
|
@ -20,7 +20,9 @@ use think\facade\Db;
|
||||
class StoreOrderLogic extends BaseLogic
|
||||
{
|
||||
public static $total;
|
||||
|
||||
public static $cost;
|
||||
public static $profit;
|
||||
public static $activity_price;
|
||||
/**
|
||||
* @notes 获取购物车商品信息
|
||||
* @param $params
|
||||
@ -37,68 +39,77 @@ class StoreOrderLogic extends BaseLogic
|
||||
}
|
||||
try {
|
||||
self::$total = 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' => Request()->adminInfo['store_id']])->field('store_name,image,unit,price')->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;
|
||||
}
|
||||
unset($cart_select[$k]['id']);
|
||||
$cart_select[$k]['total'] = bcmul($v['cart_num'], $find['price'], 2); //钱
|
||||
|
||||
$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);
|
||||
$find['price'] = $activity_price;
|
||||
}
|
||||
//利润
|
||||
$onePrice = bcsub($find['price'], $find['cost'], 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]['product_id'] = $v['product_id'];
|
||||
$cart_select[$k]['product_id'] = $find['product_id'];
|
||||
$cart_select[$k]['old_cart_id'] = $v['id'];
|
||||
$cart_select[$k]['cart_num'] = $v['cart_num'];
|
||||
$cart_select[$k]['verify_code'] = $params['verify_code'] ?? '';
|
||||
$cart_select[$k]['store_id'] = Request()->adminInfo['store_id'] ?? '';
|
||||
$cart_select[$k]['staff_id'] = Request()->adminInfo['id'] ?? '';
|
||||
$cartInfo = $cart_select[$k];
|
||||
$cartInfo['name'] = $find['store_name'];
|
||||
$cartInfo['image'] = $find['image'];
|
||||
//计算好vip价格
|
||||
$vipPrice = false;
|
||||
if ($vipPrice) {
|
||||
$cartInfo['price'] = $vipPrice;
|
||||
}
|
||||
$cartInfo['vip_price'] = $cart_select[$k]['total'] - $vipPrice ?? 0;
|
||||
// $vipPrice = self::dealVip($find['price']);
|
||||
// if ($vipPrice) {
|
||||
// $cartInfo['price'] = $vipPrice;
|
||||
// }
|
||||
$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'];
|
||||
$cart_select[$k]['imgs'] = $find['image'];
|
||||
$cart_select[$k]['unit_name'] = StoreProductUnit::where(['id' => $find['unit']])->value('name');
|
||||
self::$total = bcadd(self::$total, $cart_select[$k]['total'], 2);
|
||||
self::$cost = bcadd(self::$cost, $cart_select[$k]['cost'], 2);
|
||||
self::$profit = bcadd(self::$profit, $cart_select[$k]['profit'], 2);
|
||||
}
|
||||
//TODO 收单打9.9折 会员按照比例打折 等级按照充值去升级
|
||||
$pay_price = self::$total;
|
||||
// $check = StoreOrder::where('uid',\request()->userId)->count();//首单逻辑
|
||||
$check = DictType::where('type', 'activities')->find();
|
||||
$vipPrice = 0;
|
||||
if (isset($check) && $check['status'] == 1) {
|
||||
// $discountRate = '0.99';//首单逻辑
|
||||
$discountRate = $check['remark'];
|
||||
$discountRate = bcdiv($discountRate, '100', 2);
|
||||
$pay_price = bcdiv(bcmul($pay_price, $discountRate, 4), '1', 2);
|
||||
} else {
|
||||
|
||||
$discountRate = bcdiv(100, '100', 2);
|
||||
$pay_price = bcdiv(bcmul($pay_price, $discountRate, 4), '1', 2);
|
||||
}
|
||||
if (!empty(self::$total) && !empty($pay_price)) {
|
||||
bcscale(2);
|
||||
$vipPrice = bcsub(self::$total, $pay_price, 2);
|
||||
}
|
||||
//成本价 收益
|
||||
$order = [
|
||||
'create_time' => time(),
|
||||
'order_id' => getNewOrderId('PF'),
|
||||
'total_price' => self::$total, //总价
|
||||
'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' => 2 //配送方式 1=快递 ,2=门店自提
|
||||
'shipping_type' => $params['shipping_type'] ?? 1,//配送方式 1=快递 ,2=门店自提
|
||||
'activity' =>'减免',
|
||||
'activity_price' =>self::$activity_price,
|
||||
'activities'=>$check['status'],
|
||||
'default_delivery'=>1,
|
||||
'original_price'=>bcadd(self::$activity_price,self::$total,2),
|
||||
];
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
|
@ -112,6 +112,7 @@ class StoreOrderController extends BaseAdminController
|
||||
{
|
||||
$cartId = (array)$this->request->post('cart_id', []);
|
||||
$params = $this->request->post();
|
||||
$params['store_id']=$this->adminInfo['store_id'];
|
||||
$res = StoreOrderLogic::cartIdByOrderInfo($cartId, null, null, $params);
|
||||
if ($res == false) {
|
||||
$msg = StoreOrderLogic::getError();
|
||||
|
Loading…
x
Reference in New Issue
Block a user