From f880a035d404d9ae197f7fda700352a248b183d5 Mon Sep 17 00:00:00 2001 From: mkm <727897186@qq.com> Date: Mon, 1 Jul 2024 11:34:44 +0800 Subject: [PATCH] =?UTF-8?q?feat(order):=20=E6=B7=BB=E5=8A=A0=E8=AE=A2?= =?UTF-8?q?=E5=8D=95=E9=80=BB=E8=BE=91=EF=BC=8C=E5=8C=85=E6=8B=AC=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E8=AE=A2=E5=8D=95=E3=80=81=E6=9F=A5=E8=AF=A2=E8=AE=A2?= =?UTF-8?q?=E5=8D=95=E3=80=81=E6=A0=B8=E9=94=80=E8=AE=A2=E5=8D=95=E7=AD=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/logic/order/OrderLogic.php | 205 +++++++++++++++-------------- 1 file changed, 103 insertions(+), 102 deletions(-) diff --git a/app/api/logic/order/OrderLogic.php b/app/api/logic/order/OrderLogic.php index f3ced1881..b00605f3a 100644 --- a/app/api/logic/order/OrderLogic.php +++ b/app/api/logic/order/OrderLogic.php @@ -50,8 +50,9 @@ class OrderLogic extends BaseLogic public static $pay_price; public static $cost; public static $profit; - public static $store_price;//门店零售价 + public static $store_price; //门店零售价 public static $activity_price; + public static $deduction_price; /** * @notes 获取购物车商品信息 @@ -74,41 +75,45 @@ class OrderLogic extends BaseLogic self::$profit = 0; //利润 self::$activity_price = 0; //活动减少 self::$store_price = 0; //门店零售价 + $deduction_price = 0; //抵扣金额 /** 计算价格 */ - $off_activity=Config::where('name','off_activity')->value('value'); - $field='id branch_product_id,store_name,image,unit,price,vip_price,cost,purchase,product_id'; + $off_activity = Config::where('name', 'off_activity')->value('value'); + $field = 'id branch_product_id,store_name,image,unit,price,vip_price,cost,purchase,product_id'; foreach ($cart_select as $k => $v) { $find = StoreBranchProduct::where(['product_id' => $v['product_id'], 'store_id' => $params['store_id']])->field($field)->withTrashed()->find(); if (!$find) { continue; } unset($cart_select[$k]['id']); - if($off_activity==1){ - $price=$find['cost']; - }else{ - $price=$find['price']; + $cart_select[$k]['total_price'] = bcmul($v['cart_num'], $find['price'], 2); //订单总价 + if ($off_activity == 1) { + $price = $find['cost']; + } else { + $price = $find['price']; } $cart_select[$k]['price'] = $price; $cart_select[$k]['cost'] = $find['cost']; - $cart_select[$k]['total_price'] = bcmul($v['cart_num'], $find['price'], 2); //订单总价 - $cart_select[$k]['deduction_price'] =self::$activity_price;//抵扣金额 $cart_select[$k]['vip'] = 0; //利润 // $cart_select[$k]['profit'] = bcmul($v['cart_num'], $onePrice, 2); //利润 $cart_select[$k]['purchase'] = bcmul($v['cart_num'], $find['purchase'], 2) ?? 0; //成本 $cart_select[$k]['pay_price'] = bcmul($v['cart_num'], $price, 2); //订单支付金额 - $cart_select[$k]['store_price'] = bcmul($v['cart_num'], $find['cost'], 2)??0; //门店零售价 - $cart_select[$k]['vip_price'] = bcmul($v['cart_num'], $find['vip_price'], 2)??0; //vip售价 + $cart_select[$k]['store_price'] = bcmul($v['cart_num'], $find['cost'], 2) ?? 0; //门店零售价 + $cart_select[$k]['vip_price'] = bcmul($v['cart_num'], $find['vip_price'], 2) ?? 0; //vip售价 + if($cart_select[$k]['total_price']>$cart_select[$k]['pay_price']){ + $deduction_price=bcsub($cart_select[$k]['total_price'],$find['deduction_price'],2); + $cart_select[$k]['deduction_price'] =$deduction_price;//抵扣金额 + } $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'] ?? ''; //vip1待返回金额 - $cart_select[$k]['vip_frozen_price'] = bcsub($cart_select[$k]['pay_price'],$cart_select[$k]['vip_price'],2); + $cart_select[$k]['vip_frozen_price'] = bcsub($cart_select[$k]['pay_price'], $cart_select[$k]['vip_price'], 2); -// d($cart_select[$k]['pay_price'],$cart_select[$k]['store_price'],$cart_select[$k]['vip_price'] ); + // d($cart_select[$k]['pay_price'],$cart_select[$k]['store_price'],$cart_select[$k]['vip_price'] ); $cartInfo = $cart_select[$k]; $cartInfo['name'] = $find['store_name']; $cartInfo['image'] = $find['image']; @@ -122,20 +127,21 @@ class OrderLogic extends BaseLogic self::$total_price = bcadd(self::$total_price, $cart_select[$k]['total_price'], 2); self::$pay_price = bcadd(self::$pay_price, $cart_select[$k]['pay_price'], 2); self::$cost = bcadd(self::$cost, $cart_select[$k]['purchase'], 2); - self::$store_price = bcadd(self::$store_price, $cart_select[$k]['store_price'], 2);//门店零售价格 + self::$store_price = bcadd(self::$store_price, $cart_select[$k]['store_price'], 2); //门店零售价格 + self::$deduction_price=bcadd(self::$deduction_price,$deduction_price,2);//抵扣金额 // self::$profit = bcadd(self::$profit, $cart_select[$k]['profit'], 2); } //加支付方式限制 - $pay_type = isset($params['pay_type'])?$params['pay_type']:0; - if ($user && $user['user_ship'] == 1 && $pay_type !=17) { + $pay_type = isset($params['pay_type']) ? $params['pay_type'] : 0; + if ($user && $user['user_ship'] == 1 && $pay_type != 17) { $pay_price = self::$pay_price; - }else{ - $pay_price =bcsub(self::$pay_price, self::$activity_price, 2); //减去活动优惠金额 + } else { + $pay_price = bcsub(self::$pay_price, self::$activity_price, 2); //减去活动优惠金额 } -// if($pay_price < 500){ -// throw new Exception('金额低于500'); -// } + // if($pay_price < 500){ + // throw new Exception('金额低于500'); + // } //成本价 收益 $order = [ @@ -153,43 +159,40 @@ class OrderLogic extends BaseLogic 'shipping_type' => $params['shipping_type'] ?? 2, //配送方式 1=快递 ,2=门店自提 'activity' => '减免', 'activity_price' => self::$activity_price, - 'activities' => self::$activity_price>0?1:0, - 'deduction_price' => self::$activity_price, + 'activities' => self::$activity_price > 0 ? 1 : 0, + 'deduction_price' => self::$deduction_price, 'is_vip' => 0, - 'is_storage' => $params['is_storage']??0, + 'is_storage' => $params['is_storage'] ?? 0, ]; $order['default_delivery'] = 0; if ($params['store_id']) { $order['default_delivery'] = SystemStore::where('id', $params['store_id'])->value('is_send'); } - if($user && $user['user_ship']>=1 &&$user['user_ship']<=3){ + if ($user && $user['user_ship'] >= 1 && $user['user_ship'] <= 3) { $order['is_vip'] = 1; - } //处理返回最近的店铺 $store['near_store'] = []; - if((isset($params['lat'])&&$params['lat']!='')&&(isset($params['long']) &&$params['lat']!='')){ + if ((isset($params['lat']) && $params['lat'] != '') && (isset($params['long']) && $params['lat'] != '')) { $storeAll = SystemStore::field('id,name,phone,address,detailed_address,latitude,longitude')->select()->toArray(); $nearestStore = null; $minDistance = PHP_FLOAT_MAX; - foreach ($storeAll as $value){ - $value['distance'] = haversineDistance($value['latitude'],$value['longitude'],$params['lat'] ,$params['long']); + foreach ($storeAll as $value) { + $value['distance'] = haversineDistance($value['latitude'], $value['longitude'], $params['lat'], $params['long']); if ($value['distance'] < $minDistance) { $minDistance = $value['distance']; $nearestStore = $value; } } if ($nearestStore) { - $store['near_store'] =$nearestStore; + $store['near_store'] = $nearestStore; } - } - } catch (\Exception $e) { self::setError($e->getMessage()); return false; } - return ['order' => $order, 'cart_list' => $cart_select,'shopInfo'=>$store['near_store']]; + return ['order' => $order, 'cart_list' => $cart_select, 'shopInfo' => $store['near_store']]; } /** @@ -199,29 +202,29 @@ class OrderLogic extends BaseLogic static public function createOrder($cartId, $addressId, $user = null, $params = []) { $order_id = getNewOrderId('PF'); - $code = rand(1,10).'-'.substr($order_id, -5); + $code = rand(1, 10) . '-' . substr($order_id, -5); $verify_code = createCode($code); $params['order_id'] = $order_id; - $params['verify_code'] =$verify_code; + $params['verify_code'] = $verify_code; $orderInfo = self::cartIdByOrderInfo($cartId, $addressId, $user, $params); if (!$orderInfo) { return false; } - $uid=$user['id']??0; + $uid = $user['id'] ?? 0; $_order = $orderInfo['order']; $_order['uid'] = $uid; - $_order['spread_uid'] =$params['spread_uid']??0; - $_order['real_name'] = $user['real_name']??''; - $_order['mobile'] = $user['mobile']??''; + $_order['spread_uid'] = $params['spread_uid'] ?? 0; + $_order['real_name'] = $user['real_name'] ?? ''; + $_order['mobile'] = $user['mobile'] ?? ''; $_order['pay_type'] = $orderInfo['order']['pay_type']; $_order['verify_code'] = $verify_code; $_order['reservation_time'] = null; - $_order['reservation'] = $params['reservation']??0;//是否需要预约 + $_order['reservation'] = $params['reservation'] ?? 0; //是否需要预约 if (isset($params['reservation_time']) && $params['reservation_time']) { $_order['reservation_time'] = $params['reservation_time']; $_order['reservation'] = YesNoEnum::YES; } - if ($addressId > 0 &&$uid>0 ) { + if ($addressId > 0 && $uid > 0) { $address = UserAddress::where(['id' => $addressId, 'uid' => $uid])->find(); if ($address) { $_order['real_name'] = $address['real_name']; @@ -235,8 +238,8 @@ class OrderLogic extends BaseLogic //生成核销码 $generator = new BarcodeGeneratorPNG(); $barcode = $generator->getBarcode($verify_code, $generator::TYPE_CODE_128); - $findPath = '/image/barcode/'.time().'.png'; - $savePath = public_path().$findPath; + $findPath = '/image/barcode/' . time() . '.png'; + $savePath = public_path() . $findPath; file_put_contents($savePath, $barcode); $_order['verify_img'] = $findPath; Db::startTrans(); @@ -248,9 +251,9 @@ class OrderLogic extends BaseLogic $goods_list[$k]['uid'] = $uid; $goods_list[$k]['cart_id'] = implode(',', $cartId); $goods_list[$k]['delivery_id'] = $params['store_id']; //商家id - $StoreBranchProduct = StoreBranchProduct::where('id',$v['branch_product_id'])->withTrashed()->find(); - if($StoreBranchProduct['stock']-$v['cart_num']<=0){ - Db::name('store_product_cate')->where(['cate_id'=>$StoreBranchProduct['cate_id'],'store_id'=>$params['store_id']])->update(['count'=>0]); + $StoreBranchProduct = StoreBranchProduct::where('id', $v['branch_product_id'])->withTrashed()->find(); + if ($StoreBranchProduct['stock'] - $v['cart_num'] <= 0) { + Db::name('store_product_cate')->where(['cate_id' => $StoreBranchProduct['cate_id'], 'store_id' => $params['store_id']])->update(['count' => 0]); } } (new StoreOrderCartInfo())->saveAll($goods_list); @@ -273,10 +276,10 @@ class OrderLogic extends BaseLogic * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public static function checkLeft($params,$uid,$type =0) + public static function checkLeft($params, $uid, $type = 0) { $where = []; - if(empty($type)){ + if (empty($type)) { $where = ['is_pay' => 0]; } $cart_select = Cart::whereIn('id', $params['cart_id']) @@ -289,32 +292,32 @@ class OrderLogic extends BaseLogic //检查购物车对比店铺得商品数量差异 foreach ($cart_select as $v) { $store = StoreBranchProduct::where([ - 'store_id'=>$params['store_id'], - 'product_id'=>$v['product_id'], + 'store_id' => $params['store_id'], + 'product_id' => $v['product_id'], ])->field('id,store_name,stock')->withTrashed()->find(); - if(empty($store)){ - $store['stock'] =0; + if (empty($store)) { + $store['stock'] = 0; } - if($store['stock'] < $v['cart_num']){ + if ($store['stock'] < $v['cart_num']) { //缺失 $newArr[] = [ - 'uid'=>$uid, - 'store_id'=>$params['store_id'], - 'product_id'=>$v['product_id'], - 'missing_quantity'=> $v['cart_num'] - $store['stock'] + 'uid' => $uid, + 'store_id' => $params['store_id'], + 'product_id' => $v['product_id'], + 'missing_quantity' => $v['cart_num'] - $store['stock'] ]; } } - if($newArr){ + if ($newArr) { return [ - 'detail'=>$newArr, - 'reservation'=>1 + 'detail' => $newArr, + 'reservation' => 1 ]; } return [ - 'detail'=>[], - 'reservation'=>0 + 'detail' => [], + 'reservation' => 0 ]; } @@ -385,7 +388,7 @@ class OrderLogic extends BaseLogic $data = []; foreach ($arr as $k => $v) { $data[$k]['product_id'] = $v['product_id']; -// $unique = StoreProductAttrValue::where('product_id', $v['product_id'])->value('v'); + // $unique = StoreProductAttrValue::where('product_id', $v['product_id'])->value('v'); $data[$k]['product_attr_unique'] = ''; $data[$k]['cart_num'] = $v['cart_num']; $data[$k]['type'] = ''; @@ -410,7 +413,7 @@ class OrderLogic extends BaseLogic } - public static function detail($params,$url='',$param=[]): array + public static function detail($params, $url = '', $param = []): array { $find = StoreOrder::where($params)->findOrEmpty()->toArray(); if ($find) { @@ -429,26 +432,24 @@ class OrderLogic extends BaseLogic $store = SystemStore::where('id', $find['store_id'])->field('id,name,phone,address,detailed_address')->find(); $find['store_info'] = $store; - if($find['verify_img']){ - $find['verify_img'] = $url.$find['verify_img']; + if ($find['verify_img']) { + $find['verify_img'] = $url . $find['verify_img']; } //处理返回最近的店铺 - if($param['lat'] && $param['long']){ + if ($param['lat'] && $param['long']) { $storeAll = SystemStore::field('id,name,phone,address,detailed_address,latitude,longitude')->select()->toArray(); $nearestStore = null; $minDistance = PHP_FLOAT_MAX; - foreach ($storeAll as $value){ - $value['distance'] = haversineDistance($value['latitude'],$value['longitude'],$param['lat'] ,$param['long']); + foreach ($storeAll as $value) { + $value['distance'] = haversineDistance($value['latitude'], $value['longitude'], $param['lat'], $param['long']); if ($value['distance'] < $minDistance) { $minDistance = $value['distance']; $nearestStore = $value; } } if ($nearestStore) { - $find['near_store'] =$nearestStore; - + $find['near_store'] = $nearestStore; } - } } return $find; @@ -488,52 +489,52 @@ class OrderLogic extends BaseLogic } Db::startTrans(); try { - StoreOrder::update([ - 'verify_code'=>$params['verify_code'].'-1', + StoreOrder::update([ + 'verify_code' => $params['verify_code'] . '-1', 'status' => OrderEnum::RECEIVED_GOODS, 'is_writeoff' => OrderEnum::IS_OK, 'update_time' => time(), 'store_id' => $params['store_id'], - 'staff_id' => $params['staff_id']??0, + 'staff_id' => $params['staff_id'] ?? 0, ], ['id' => $order['id']]); (new StoreOrderCartInfo())->update([ - 'verify_code'=>$params['verify_code'].'-1', + 'verify_code' => $params['verify_code'] . '-1', 'writeoff_time' => time(), 'is_writeoff' => YesNoEnum::YES, 'store_id' => $params['store_id'], - 'staff_id' => $params['staff_id']??0, + 'staff_id' => $params['staff_id'] ?? 0, 'update_time' => time(), ], ['oid' => $order['id']]); PayNotifyLogic::descStock($order['id']); - $financeFlow=new StoreFinanceFlow(); - $financeFlowLogic=new StoreFinanceFlowLogic(); - $select_1=$financeFlow->where(['order_id'=>$order['id'],'financial_pm'=>1,'financial_type'=>['in'=>14,15,16]])->select(); - foreach($select_1 as $k=>$v){ - if($v['other_uid']>0){ - $financeFlowLogic->updateStatusUser($v['id'],$v['other_uid'],$v['number'],$v['order_id']); + $financeFlow = new StoreFinanceFlow(); + $financeFlowLogic = new StoreFinanceFlowLogic(); + $select_1 = $financeFlow->where(['order_id' => $order['id'], 'financial_pm' => 1, 'financial_type' => ['in' => 14, 15, 16]])->select(); + foreach ($select_1 as $k => $v) { + if ($v['other_uid'] > 0) { + $financeFlowLogic->updateStatusUser($v['id'], $v['other_uid'], $v['number'], $v['order_id']); } } - if($order['uid'] && $order['pay_price'] >= 500){ - $level = User::where('id',$order['uid'])->value('user_ship'); + if ($order['uid'] && $order['pay_price'] >= 500) { + $level = User::where('id', $order['uid'])->value('user_ship'); $discount = PayNotifyLogic::getDiscount($level); $user_number = bcmul($order['pay_price'], $discount, 2); User::where('id', $order['uid'])->inc('integral', $user_number)->update(); // 核销加冻结礼品券 解冻礼品券 - self::addUserSing($order,2,$user_number);//冻结 + self::addUserSing($order, 2, $user_number); //冻结 - self::addUserSing($order,4,$user_number,1,1);//解冻 + self::addUserSing($order, 4, $user_number, 1, 1); //解冻 - UserSign::where(['uid' => $order['uid'],'order_id' => $order['order_id'],'title'=>1])->update(['status'=>1]); + UserSign::where(['uid' => $order['uid'], 'order_id' => $order['order_id'], 'title' => 1])->update(['status' => 1]); } if ($order['spread_uid'] > 0) { - $spread_find=$financeFlow->where(['order_id'=>$order['id'],'financial_pm'=>1,'financial_type'=>12,'other_uid'=>$order['spread_uid']])->find(); - if($spread_find){ - $financeFlowLogic->updateStatusUser($spread_find['id'],$order['spread_uid'],$spread_find['number'],$order['id']); + $spread_find = $financeFlow->where(['order_id' => $order['id'], 'financial_pm' => 1, 'financial_type' => 12, 'other_uid' => $order['spread_uid']])->find(); + if ($spread_find) { + $financeFlowLogic->updateStatusUser($spread_find['id'], $order['spread_uid'], $spread_find['number'], $order['id']); } } - $deposit=$financeFlow->where(['order_id'=>$order['id'],'financial_pm'=>1,'financial_type'=>11])->value('number')??0; - $money=$financeFlow->where(['order_id'=>$order['id'],'financial_pm'=>1,'financial_type'=>2])->value('number')??0; - $financeFlowLogic->updateStatusStore($order['id'],$order['store_id'],$money,$deposit); + $deposit = $financeFlow->where(['order_id' => $order['id'], 'financial_pm' => 1, 'financial_type' => 11])->value('number') ?? 0; + $money = $financeFlow->where(['order_id' => $order['id'], 'financial_pm' => 1, 'financial_type' => 2])->value('number') ?? 0; + $financeFlowLogic->updateStatusStore($order['id'], $order['store_id'], $money, $deposit); Db::commit(); return true; } catch (\Exception $e) { @@ -552,14 +553,14 @@ class OrderLogic extends BaseLogic * @type $type //类型 0冻结 1解冻 * @return true */ - public static function addUserSing($order, $category, $number, int $pm=0, $type=0) + public static function addUserSing($order, $category, $number, int $pm = 0, $type = 0) { $user_sing = new UserSign(); $sing = [ 'uid' => $order['uid'], 'order_id' => $order['order_id'], -// 'title' => '购买商品获得兑换券', -// 'title' => PayNotifyLogic::getTitle($category,$number), + // 'title' => '购买商品获得兑换券', + // 'title' => PayNotifyLogic::getTitle($category,$number), 'title' => $category, 'financial_pm' => $pm, 'store_id' => $order['store_id'], @@ -588,19 +589,19 @@ class OrderLogic extends BaseLogic Db::startTrans(); try { StoreOrder::update([ - 'verify_code'=>$params['verify_code'].'-1', + 'verify_code' => $params['verify_code'] . '-1', 'status' => OrderEnum::RECEIVED_GOODS, 'is_writeoff' => OrderEnum::IS_OK, 'update_time' => time(), 'store_id' => $params['store_id'], - 'staff_id' => $params['staff_id']??0, + 'staff_id' => $params['staff_id'] ?? 0, ], ['id' => $data['id']]); (new StoreOrderCartInfo())->update([ - 'verify_code'=>$params['verify_code'].'-1', + 'verify_code' => $params['verify_code'] . '-1', 'writeoff_time' => time(), 'is_writeoff' => YesNoEnum::YES, 'store_id' => $params['store_id'], - 'staff_id' => $params['staff_id']??0, + 'staff_id' => $params['staff_id'] ?? 0, 'update_time' => time(), ], ['oid' => $data['id']]); // $financeFlow = (new StoreFinanceFlowLogic)->getStoreOrder($data['id'], $data['store_id']); @@ -608,7 +609,7 @@ class OrderLogic extends BaseLogic // $capitalFlowLogic = new CapitalFlowLogic($data->store, 'store'); // $capitalFlowLogic->storeIncome('store_order_income', 'order', $data['id'], $financeFlow['number']); // } - $order=StoreOrder::where('id',$data['id'])->find(); + $order = StoreOrder::where('id', $data['id'])->find(); PayNotifyLogic::descSwap($order['id']); Db::commit(); return true;