0]; $cart_select = Cart::whereIn('id', $cartId)->where($where)->field('id,product_id,cart_num')->select()->toArray(); if (empty($cart_select)) { self::setError('购物车为空'); return false; } try { self::$total_price = 0; self::$pay_price = 0; self::$cost = 0; //成本由采购价替代原成本为门店零售价 self::$profit = 0; //利润 self::$activity_price = 0; //活动减少 self::$store_price = 0; //门店零售价 self::$deduction_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'; 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) { // unset($cart_select[$k]); // continue; $field = 'id branch_product_id,store_name,image,unit,price,vip_price,cost,purchase, id product_id'; $find = StoreProduct::where(['id' => $v['product_id']])->field($field)->withTrashed()->find(); $cart_select[$k]['status'] = 1;//缺货标识 } unset($cart_select[$k]['id']); $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]['vip'] = 0; $cart_select[$k]['unit_name'] = StoreProductUnit::where(['id' => $find['unit']])->value('name')??''; //单位名称 //利润 // $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售价 if($cart_select[$k]['total_price']>$cart_select[$k]['pay_price']){ $deduction_price=bcsub($cart_select[$k]['total_price'],$cart_select[$k]['pay_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'] ?? ''; $cart_select[$k]['vip_frozen_price']=0; //会员待返回金额 if ($user && $off_activity==0){ if($user['user_ship']==4){ //商户 $cart_select[$k]['vip_frozen_price'] = bcsub($cart_select[$k]['pay_price'], $cart_select[$k]['purchase'], 2); }else{ //其他会员 $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'] ); $cartInfo = $cart_select[$k]; $cartInfo['name'] = $find['store_name']; $cartInfo['image'] = $find['image']; $cart_select[$k]['cart_info'] = json_encode($cartInfo); $cart_select[$k]['branch_product_id'] = $find['branch_product_id']; //理论上每笔都是拆分了 $cart_select[$k]['name'] = $find['store_name']; $cart_select[$k]['imgs'] = $find['image']; $cart_select[$k]['store_id'] = $params['store_id'] ?? 0; $cart_select[$k]['unit_name'] = StoreProductUnit::where(['id' => $find['unit']])->value('name'); 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::$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_price = self::$pay_price; // } else { $pay_price = bcsub(self::$pay_price, self::$activity_price, 2); //减去活动优惠金额 // } //成本价 收益 $order = [ 'create_time' => time(), 'order_id' => $params['order_id'] ?? getNewOrderId('PF'), 'total_price' => self::$total_price, //总价 'cost' => self::$cost, //成本价1- 'pay_price' => $pay_price, //后期可能有降价抵扣 'vip_price' => 0, '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'] ?? 2, //配送方式 1=快递 ,2=门店自提 'activity' => '减免', 'activity_price' => self::$activity_price, 'activities' => self::$activity_price > 0 ? 1 : 0, 'deduction_price' => self::$deduction_price, 'source' => 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 (isset($params['source']) && $params['source'] > 0) { $order['source'] = $params['source']; } //处理返回最近的店铺 $store_check = 0; if(empty($user)){ $store_id = getenv('STORE_ID') ?? 1; $store['near_store'] =SystemStore::where('id', $store_id)->field('id,name,phone,address,detailed_address,latitude,longitude')->find()??[]; }else{ $checkOrderStore = StoreOrder::where('uid',$user['id'])->field('id,store_id') ->order('id','desc')->find(); if($checkOrderStore){ $store['near_store'] =SystemStore::where('id', $checkOrderStore['store_id'])->field('id,name,phone,address,detailed_address,latitude,longitude')->find()??[]; $store_check = 1; }else{ $store_id = getenv('STORE_ID') ?? 1; $store['near_store'] =SystemStore::where('id', $store_id)->field('id,name,phone,address,detailed_address,latitude,longitude')->find()??[]; } } if (empty($store_check)) { if ((isset($params['lat']) && $params['lat'] != '') && (isset($params['long']) && $params['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'], $params['lat'], $params['long']); if ($value['distance'] < $minDistance) { $minDistance = $value['distance']; $nearestStore = $value; } } $store['near_store'] =[]; if ($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 Object|bool|array */ static public function createOrder($cartId, $addressId, $user = null, $params = []) { $order_id = getNewOrderId('PF'); $code = rand(1, 10) . '-' . substr($order_id, -5); $verify_code = createCode($code); $params['order_id'] = $order_id; $params['verify_code'] = $verify_code; $orderInfo = self::cartIdByOrderInfo($cartId, $addressId, $user, $params); if (!$orderInfo) { return false; } $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['pay_type'] = $orderInfo['order']['pay_type']; $_order['verify_code'] = $verify_code; $_order['reservation_time'] = null; $_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) { $address = UserAddress::where(['id' => $addressId, 'uid' => $uid])->find(); if ($address) { $_order['real_name'] = $address['real_name']; $_order['user_phone'] = $address['phone']; $_order['user_address'] = $address['detail']; } } if ($params['shipping_type'] == 2) { $_order['status'] = 1; } //生成核销码 $generator = new BarcodeGeneratorPNG(); $barcode = $generator->getBarcode($verify_code, $generator::TYPE_CODE_128); $findPath = '/image/barcode/' . time() . '.png'; $savePath = public_path() . $findPath; file_put_contents($savePath, $barcode); $_order['verify_img'] = $findPath; Db::startTrans(); try { $order = StoreOrder::create($_order); $goods_list = $orderInfo['cart_list']; foreach ($goods_list as $k => $v) { $goods_list[$k]['oid'] = $order->id; $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]); // } } (new StoreOrderCartInfo())->saveAll($goods_list); $where = ['is_pay' => 0]; Cart::whereIn('id', $cartId)->where($where)->update(['is_pay' => 1]); Db::commit(); return $order; } catch (\Exception $e) { Db::rollback(); self::setError($e->getMessage()); return false; } } /** * 检验丢失 * @param $params * @return array|false * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function checkLeft($params, $uid, $type = 0) { $where = []; if (empty($type)) { $where = ['is_pay' => 0]; } $cart_select = Cart::whereIn('id', $params['cart_id']) ->where($where)->field('id,product_id,cart_num,store_id')->select()->toArray(); if (empty($cart_select)) { self::setError('购物车为空'); return false; } $newArr = []; //检查购物车对比店铺得商品数量差异 foreach ($cart_select as $v) { $store = StoreBranchProduct::where([ 'store_id' => $params['store_id'], 'product_id' => $v['product_id'], ])->field('id,store_name,stock')->withTrashed()->find(); if (empty($store)) { $store['stock'] = 0; } 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'] ]; } } if ($newArr) { return [ 'detail' => $newArr, 'reservation' => 1 ]; } return [ 'detail' => [], 'reservation' => 0 ]; } /** * @notes 获取订单号 * @param $type * @return string * @author likeadmin * @date 2021/7/28 17:05 */ static public function getNewOrderId($type) { list($msec, $sec) = explode(' ', microtime()); $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', ''); $orderId = $type . $msectime . mt_rand(10000, max(intval($msec * 10000) + 10000, 98369)); return $orderId; } /** * @notes 订单支付成功 * @param $order 订单 * @param $CallbackData 回调数据 * @date 2021/7/8 00:40 */ static function paySuccess($order, $CallbackData = []) { Db::startTrans(); try { $order->money = $CallbackData['money']; $order->paid = 1; $order->save(); Log::info('支付成功'); // 提交事务 Db::commit(); return true; } catch (\Exception $e) { // 回滚事务 Db::rollback(); Log::error('支付失败' . $e->getMessage() . '。like:' . $e->getLine()); self::setError('支付失败' . $e->getMessage()); return false; } } /** * 获取用户常用购买记录 */ public static function frequentlyPurchase($params) { try { $goods_id = StoreOrderCartInfo::where('uid', Request()->userId)->page($params['page_no'])->limit(50)->column('product_id'); if (!$goods_id) { return []; } $goods_arr = array_unique($goods_id); $select = StoreBranchProduct::where('product_id', 'in', $goods_arr)->with('unitName')->field('id,store_name,price,image,unit')->select(); return $select->toArray(); } catch (\Exception $e) { self::setError($e->getMessage()); return false; } } public static function purchaseAgain($order_id) { $arr = StoreOrderCartInfo::where('oid', $order_id)->field('product_id,cart_num,staff_id,store_id')->select(); $data = []; foreach ($arr as $k => $v) { $data[$k]['product_id'] = $v['product_id']; // $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'] = ''; $data[$k]['uid'] = Request()->userId; $data[$k]['store_id'] = $v['store_id']; $data[$k]['staff_id'] = $v['staff_id']; $data[$k]['combination_id'] = 0; $data[$k]['seckill_id'] = 0; $data[$k]['bargain_id'] = 0; $data[$k]['discount_id'] = 0; $data[$k]['status'] = 1; $data[$k]['staff_id'] = 0; $data[$k]['is_new'] = 0; $data[$k]['is_del'] = 0; $data[$k]['is_pay'] = 0; } if ($data) { (new Cart())->saveAll($data); return true; } return false; } public static function detail($params, $url = '', $param = []): array { $find = StoreOrder::where($params)->findOrEmpty()->toArray(); if ($find) { $find['goods_list'] = StoreOrderCartInfo::where('oid', $find['id']) ->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(); if(empty($find)){ $find = StoreProduct::where('id', $item['product_id'])->withTrashed()->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小时发货'; return $item; }); $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 ($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']); if ($value['distance'] < $minDistance) { $minDistance = $value['distance']; $nearestStore = $value; } } if ($nearestStore) { $find['near_store'] = $nearestStore; } } } return $find; } //取消售后 public static function cancelSell($where) { return StoreOrder::where($where)->update( [ 'refund_status' => OrderEnum::CANCEL_SALE, 'status' => OrderEnum::CANCEL_ORDER, 'refund_cancle_time' => date('Y-m-d H:i:s', time()) ] ); } //核销 /** * @param $params * @return bool * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @author: codeliu * @Time: 2024/6/3 22:42 */ public static function writeOff($params): bool { $order = StoreOrder::with('store')->where([ 'verify_code' => $params['verify_code'] ])->find(); if (empty($order)) { self::setError('订单不存在'); return false; } Db::startTrans(); try { 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, ], ['id' => $order['id']]); (new StoreOrderCartInfo())->update([ 'verify_code' => $params['verify_code'] . '-1', 'writeoff_time' => time(), 'is_writeoff' => YesNoEnum::YES, 'store_id' => $params['store_id'], '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']); } } 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']); } } $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); //积分结算 $user_ship=-1; if($order['uid']){ $user_ship=User::where('id',$order['uid'])->value('user_ship'); } if($order['is_storage']==0&&$order['source']==0&&in_array($user_ship,[0,4])){ UserSignLogic::WriteOff($order); } Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); self::setError($e->getMessage()); return false; } } //不走二次分钱的核销 public static function lessWriteOff($params): bool { $data = StoreOrder::with('store')->where([ 'verify_code' => $params['verify_code'] ])->find(); if (empty($data)) { return false; } Db::startTrans(); try { 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, ], ['id' => $data['id']]); (new StoreOrderCartInfo())->update([ 'verify_code' => $params['verify_code'] . '-1', 'writeoff_time' => time(), 'is_writeoff' => YesNoEnum::YES, 'store_id' => $params['store_id'], 'staff_id' => $params['staff_id'] ?? 0, 'update_time' => time(), ], ['oid' => $data['id']]); // $financeFlow = (new StoreFinanceFlowLogic)->getStoreOrder($data['id'], $data['store_id']); // if (!empty($financeFlow)) { // $capitalFlowLogic = new CapitalFlowLogic($data->store, 'store'); // $capitalFlowLogic->storeIncome('store_order_income', 'order', $data['id'], $financeFlow['number']); // } $order = StoreOrder::where('id', $data['id'])->find(); PayNotifyLogic::descSwap($order['id']); Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); self::setError($e->getMessage()); return false; } } public static function write_count($info, $params) { $store_id = SystemStoreStaff::where('phone', $info['mobile'])->value('store_id'); if (empty($store_id)) { throw new \Exception('该用户未绑定店铺请查看'); } $query = StoreOrderCartInfo::alias('o') ->leftJoin('store_branch_product p', 'p.id = o.product_id') ->leftJoin('store_order s', 's.id = o.oid') ->field('o.oid,p.store_name,s.order_id') ->where('o.store_id', $store_id); if (isset($params['name']) && $params['name']) { if ($params['name'] && preg_match('/[\x{4e00}-\x{9fff}]+/u', $params['name']) == 1) { $query->where('p.store_name', 'like', '%' . $params['name'] . '%'); } else { $query->where('s.order_id', $params['name']); } } $product = $query->select(); if (empty($product)) { return [ 'no_send' => 0, 'send' => 0 ]; } $oids = array_column($product->toArray(), 'oid'); $uniqueOids = array_unique($oids); $no_send = StoreOrder::whereIn('id', $uniqueOids) ->where('status', 1)->count(); $send = StoreOrder::whereIn('id', $uniqueOids) ->where('status', 2)->count(); return [ 'no_send' => $no_send, 'send' => $send ]; } public static function getOne($code) { return StoreOrder::with(['store'])->where('verify_code', $code) ->select()->each(function ($item) { $item['goods_list'] = StoreOrderCartInfo::where('oid', $item['id'])->with('goodsName')->field('product_id,cart_num,verify_code,is_writeoff,writeoff_time')->limit(3)->select(); $item['goods_count'] = count(explode(',', $item['cart_id'])); return $item; //返回处理后的数据。 }) ->toArray(); } public static function write_list($info, $status, $params) { $store_id = SystemStoreStaff::where('phone', $info['mobile'])->value('store_id'); if (empty($store_id)) { throw new \Exception('该用户未绑定店铺请查看'); } //先查商品相似 $query = StoreOrderCartInfo::alias('o') ->leftJoin('store_branch_product p', 'p.id = o.product_id') ->leftJoin('store_order s', 's.id = o.oid') ->field('o.oid,p.store_name,s.order_id') ->where('o.store_id', $store_id); if (isset($params['name']) && $params['name']) { if ($params['name'] && preg_match('/[\x{4e00}-\x{9fff}]+/u', $params['name']) == 1) { $query->where('p.store_name', 'like', '%' . $params['name'] . '%'); } else { $query->where('s.order_id', $params['name']); } } $product = $query->select(); if (empty($product)) { return [ 'list' => [], 'count' => 0 ]; } $oids = array_column($product->toArray(), 'oid'); $uniqueOids = array_unique($oids); $query = StoreOrder::with(['store']) ->whereIn('id', $uniqueOids) ->where('status', $status); $count = $query->count(); $list = $query ->page($params['page_no'], $params['page_size']) ->order(['id' => 'desc']) ->select() ->each(function ($item) { $item['goods_list'] = StoreOrderCartInfo::where('oid', $item['id'])->with('goodsName')->field('product_id,cart_num,verify_code,is_writeoff,writeoff_time')->limit(3)->select(); $item['goods_count'] = count(explode(',', $item['cart_id'])); }) ->toArray(); return [ 'list' => $list, 'count' => $count ]; } public static function dealRefund($uid, $params) { //todo 单子不是完成的不允许退款 //单笔不拆单子直接修改状态 $order = StoreOrder::where('id', $params['id'])->withTrashed()->findOrEmpty(); $params['refund_num'] = 1; //todo 拿实际数量 if (count($params['old_cart_id']) == 1) { $order->refund_status = OrderEnum::REFUND_STATUS_YES; $order->status = OrderEnum::ALREADY_REFUND; $order->refund_reason_wap_explain = $params['refund_message'] ?? ''; $order->mark = $params['mark'] ?? ''; $order->refund_num = $params['refund_num'] ?? 1; $refund_price_cart = StoreOrderCartInfo::where('oid', $params['id']) ->field('id,oid,cart_info') ->find()->toArray(); $vipPrice = $order['vip_price']; $price = $refund_price_cart['cart_info']['price'] * $params['refund_num']; $onePrice = 0; bcscale(2); if ($vipPrice) { //每单的vip价格 $onePrice = bcdiv($vipPrice, $refund_price_cart['cart_info']['cart_num']); } if ($price > $onePrice) { $price = bcsub($price, $onePrice); } $order->refund_price = $price; $order->refund_type = $params['refund_type']; $order->save(); } else { // 多单的情况 拆主订单为新的2单 修改新的2单的核销码 修改cart_info的核销码 和订单id 退款直接退一单的钱 $order->delete_time = time(); $order->save(); Db::startTrans(); try { $order = $order->toArray(); $cart_info = StoreOrderCartInfo::where('oid', $params['id']) ->whereNotIn('old_cart_id', $params['old_cart_id']) ->select()->toArray(); if ($cart_info) { $leftOrder = self::dealCreateLeftOrder($order, $cart_info, $params); self::dealChangeCartInfo($leftOrder); } $refundOrder = self::dealCreateRefundOrder($order, $params); self::dealChangeCartInfo($refundOrder); // d($leftOrder,$refundOrder); Db::commit(); } catch (\Exception $e) { // 回滚事务 Db::rollback(); throw new BusinessException($e->getMessage()); } } return 1; } public static function dealChangeCartInfo($leftOrder) { $code = $leftOrder->verify_code; $new_oid = $leftOrder->id; $old_id = $leftOrder->pid; $car_id = explode(',', $leftOrder->cart_id); return StoreOrderCartInfo::where('oid', $old_id) ->whereIn('old_cart_id', $car_id) ->update([ 'oid' => $new_oid, 'verify_code' => $code ]); } public static function dealCreateLeftOrder($order, $cart_info, $params) { //查出不在这个退货中的数据 $order['order_id'] = $order['order_id'] . '-2'; $order['pid'] = $order['id']; unset($order['id']); $allOldCartIds = []; $totalTotal = 0; $totalPrice = 0; $totalVipPrice = 0; foreach ($cart_info as $value) { if (isset($value['old_cart_id'])) { $allOldCartIds[] = $value['old_cart_id']; } if (isset($value['cart_info'])) { if (isset($value['cart_info']['total'])) { $totalTotal += floatval($value['cart_info']['total']); } if (isset($value['cart_info']['price'])) { $totalPrice += floatval($value['cart_info']['price']); } if (isset($value['cart_info']['vip_price'])) { $totalVipPrice += floatval($value['cart_info']['vip_price']); } } } $order['cart_id'] = implode(',', $allOldCartIds); $order['total_num'] = count($cart_info); $order['refund_type'] = $params['refund_type']; $order['total_price'] = number_format($totalTotal, 2); $order['pay_price'] = number_format($totalPrice, 2); $order['vip_price'] = number_format($totalVipPrice, 2); $order['verify_code'] = verificationCode(); $order['create_time'] = time(); $order['update_time'] = null; $order['delete_time'] = null; return StoreOrder::create($order); } public static function dealCreateRefundOrder($order, $params) { $order['order_id'] = $order['order_id'] . '-1'; $order['pid'] = $order['id']; unset($order['id']); $order['cart_id'] = implode(',', $params['old_cart_id']); $order['refund_status'] = OrderEnum::REFUND_STATUS_YES; $order['status'] = OrderEnum::ALREADY_REFUND; $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']); $refund_price_cart = StoreOrderCartInfo::whereIn('old_cart_id', $params['old_cart_id']) ->field('id,oid,cart_info') ->select()->toArray(); $totalTotals = array_column(array_column($refund_price_cart, 'cart_info'), 'total'); $totalTotal = array_reduce($totalTotals, function ($carry, $item) { return $carry + $item; }, 0); $totalPrices = array_column(array_column($refund_price_cart, 'cart_info'), 'price'); $totalPrice = array_reduce($totalPrices, function ($carry, $item) { return $carry + $item; }, 0); $totalVipPrices = array_column(array_column($refund_price_cart, 'cart_info'), 'vip_price'); $totalVipPrices = array_reduce($totalVipPrices, function ($carry, $item) { return $carry + $item; }, 0); $order['total_price'] = number_format($totalTotal, 2); $order['pay_price'] = number_format($totalPrice, 2); $order['vip_price'] = number_format($totalVipPrices, 2); $order['refund_price'] = number_format($totalPrice, 2); $order['verify_code'] = verificationCode(); $order['refund_reason_time'] = time(); $order['create_time'] = time() + 1; $order['update_time'] = null; $order['delete_time'] = null; return StoreOrder::create($order); } public static function dealVip($pay_price) { $check = DictType::where('type', 'activities')->find(); 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 { $userVip = User::where('id', \request()->userId)->value('user_ship'); if ($userVip) { switch ($userVip) { case UserShipEnum::VIP1: $discountRate = UserShip::where('id', UserShipEnum::VIP1)->value('discount'); break; case UserShipEnum::VIP2: $discountRate = UserShip::where('id', UserShipEnum::VIP2)->value('discount'); break; case UserShipEnum::VIP3: $discountRate = UserShip::where('id', UserShipEnum::VIP3)->value('discount'); break; case UserShipEnum::VIP4: $discountRate = UserShip::where('id', UserShipEnum::VIP4)->value('discount'); break; case UserShipEnum::VIP5: $discountRate = UserShip::where('id', UserShipEnum::VIP5)->value('discount'); break; default: $discountRate = 1; } $discountRate = bcdiv($discountRate, '100', 2); $pay_price = bcdiv(bcmul($pay_price, $discountRate, 4), '1', 2); } } return $pay_price; } }