request->param(); $admin = Admin::where('id', $this->admin_id)->findOrFail(); if (!password_verify($params['safe_word'], $admin->payment_password)) throw new Exception('资金密码错误'); $orderList = OrderModel::whereIn('id', $params['order_id'])->get(); foreach ($orderList as $order) { if ($order->return_status != 0 || $order->pay_status != 1) { continue; } $order->status = 3; $order->return_status = 2; $order->return_operation_time = time(); $order->save(); $user = User::where('user_id', $order->user_id)->findOrFail(); if (!$user) continue; if ($user->type == 1) { FundsRecord::addData([ 'transaction_type' => 'return_order', 'amount_change' => $order->amount, 'balance_before' => $user->money, 'balance_after' => bcsub($user->money, $order->amount, 2), 'user_id' => $user->user_id, ]); $user->money = bcsub($user->money, $order->amount, 2); $user->save(); } } DB::commit(); } catch (Exception $e) { DB::rollBack(); return $this->error($e->getMessage(), $errors); } return $this->success(); } /** * @api {post} /order/refund 同意退款 * @apiGroup 订单管理 */ public function refund() { $errors = []; DB::beginTransaction(); try { $params = $this->request->param(); $orderId = $params['order_id']; $safeWord = $params['safe_word']; $admin = Admin::where('id', $this->admin_id)->findOrFail(); if (!password_verify($safeWord, $admin->payment_password)) throw new Exception('资金密码错误'); $order = OrderModel::where('id', $orderId)->findOrFail(); if (!$order) throw new Exception('订单不存在'); if ($order->return_status != 1 || $order->pay_status != 1) { $errors = ['id' => $orderId]; throw new Exception("该订单状态无法操作"); } $order->status = 3; $order->return_status = 2; $order->return_operation_time = time(); $order->save(); $user = User::where('user_id', $order->user_id)->findOrFail(); $balanceAfter = bcadd($user->money, $order->amount, 2); FundsRecord::addData([ 'transaction_type' => 'return_order', 'amount_change' => $order->amount, 'balance_before' => $user->money, 'balance_after' => $balanceAfter, 'user_id' => $user->user_id, ]); $user->money = $balanceAfter; $user->save(); DB::commit(); } catch (Exception $e) { DB::rollBack(); return $this->error($e->getMessage(), $errors); } return $this->success(); } /** * 驳回退款申请 * @apiGroup 订单管理 */ public function rejection() { $errors = []; DB::beginTransaction(); try { $params = $this->request->param(); if (empty($params['failure_msg'])) $params['failure_msg'] = ''; $order = OrderModel::where('id', $params['order_id'])->first(); if (!$order) throw new Exception('订单不存在'); if ($order->status != 1 || $order->return_status != 1) { $errors = ['id' => $params['order_id']]; throw new Exception("该订单状态无法操作"); } $order->return_operation_time = time(); $order->failure_msg = $params['failure_msg']; $order->save(); DB::commit(); } catch (Exception $e) { DB::rollBack(); return $this->error($e->getMessage(), $errors); } return $this->success(); } /** * @api {get} /order/list 订单列表 * @apiGroup 订单管理 * @apiVersion 1.0.0 * @apiUse header * @apiUse lang * * @apiParam {int} [page=1] * @apiParam {int} [limit=15] * @apiParam {String} [id] 订单编号 * @apiParam {String} [user_id] 买家ID * @apiParam {String} [store_id] 卖家ID * @apiParam {String} [phone] 手机号 * @apiParam {String} [contacts] 收货人 * @apiParam {int=0,1} [pay_status] 支付状态 0待支付 1已支付 * @apiParam {int=0,1} [profit_status] 利润发放 0未发放 1已发放 * @apiParam {String} [start_time] 开始时间 * - 格式:`yyyy-mm-dd` * @apiParam {String} [end_time] 开始时间 * - 格式:`yyyy-mm-dd` * @apiParam {int=-1,0,1,2,3,4,5,6,7} [type=7] 订单类别 * - `7 全部订单`,`0 待付款`,`1 已确认`,`2 待发货`,`3 待收货`,`4 已收货`,`5 已评价`,`6 已退款`,`-1 已取消` * @apiSuccess {String} id 订单编号 * @apiSuccess {Object} store 店铺信息 * @apiSuccess {String} store.user_id 店铺ID * @apiSuccess {String} store.seller_name 店铺名 * @apiSuccess {String} store.seller_img 店铺logo * @apiSuccess {String} store.seller_address 店铺地址 * @apiSuccess {String} contacts 收货人姓名 * @apiSuccess {String} user_id 买家ID * @apiSuccess {String} store_id 卖家ID * @apiSuccess {String} total_cost 采购价格 * @apiSuccess {String} price_count 订单金额 * @apiSuccess {String} profit 利润 * @apiSuccess {int} pay_status 支付状态 0待支付 1已支付 * @apiSuccess {int} status 订单状态 * - -1 已取消 * - 0 待付款 * - 1 待发货 * - 2 待发货 * - 3 待收货 * - 4 已确认 * - 5 已评价 * */ public function getList(Request $request) { try { $params = request()->validate([ 'page' => ['nullable', 'integer', 'min:1'], 'limit' => ['nullable', 'integer', 'min:1'], 'id' => ['nullable', 'string'], 'user_id' => ['nullable', 'string'], 'store_id' => ['nullable', 'string'], 'phone' => ['nullable', 'string'], 'contacts' => ['nullable', 'string'], 'pay_status' => ['nullable', 'integer', 'in:0,1'], 'return_status' => ['nullable', 'integer', 'in:0,1,2,3'], 'purchase_status' => ['nullable', 'integer', 'in:0,1,2'], 'purchase_amount_status' => ['nullable', 'integer', 'in:0,1'], 'profit_status' => ['nullable', 'integer', 'in:0,1'], 'status' => ['nullable', 'integer', 'in:0,1,2,3,4,5,6,7,-1'], 'start_time' => ['nullable', 'date', 'date_format:Y-m-d', 'required_with:end_time'], 'end_time' => ['nullable', 'date', 'date_format:Y-m-d', 'required_with:start_time'], 'order_task_id' => ['nullable', 'string'], 'type' => ['nullable', 'integer', 'in:1,2'], ]); $page = request()->input('page', 1); $limit = request()->input('limit', 15); $admin_id = $request->user->id; if ($admin_id != 1 && Config::adminOpenAllData($admin_id) === false) { $user_code = $request->user->user_code; $query = OrderModel::leftJoin('users', 'orders.user_id', '=', 'users.user_id') ->leftJoin('users as stores', 'orders.store_id', '=', 'stores.user_id') ->where(function ($query) use ($user_code){ $query->where('users.user_code', $user_code) ->orWhere('stores.user_code', $user_code); }); } else { $query = OrderModel::query(); } if (!empty($params['start_time'])) { $query = $query->where('orders.created_at', '>=', $params['start_time'] . " 00:00:00") ->where('orders.created_at', '<=', $params['end_time'] . " 23:59:59"); } if (!empty($params['id'])) { $query = $query->where('orders.id', $params['id']); } if (!empty($params['user_id'])) { $query = $query->where('orders.user_id', $params['user_id']); } if (!empty($params['store_id'])) { $query = $query->where('orders.store_id', $params['store_id']); } if (!empty($params['phone'])) { $query = $query->where('orders.phone', $params['phone']); } if (!empty($params['contacts'])) { $query = $query->where('orders.contacts', 'like', '%' . $params['contacts'] . '%'); } if (isset($params['status'])) { $query = $query->where('orders.status', $params['status']); } if (isset($params['return_status'])) { $query = $query->where('orders.return_status', $params['return_status']); } if (isset($params['pay_status'])) { $query = $query->where('orders.pay_status', $params['pay_status']); } if (isset($params['profit_status'])) { $query = $query->where('orders.profit_status', $params['profit_status']); } if (isset($params['purchase_status'])) { $query = $query->where('orders.purchase_status', $params['purchase_status']); } if (isset($params['purchase_amount_status'])) { $query = $query->where('orders.purchase_amount_status', $params['purchase_amount_status']); } if (!empty($params['order_task_id'])) { $query = $query->where('orders.order_task_id', $params['order_task_id']); } if (!empty($params['type'])) { $query = $query->where('orders.type', $params['type']); } $count = $query->count(); $list = $query->with(['store', 'orderLog']) ->select('orders.*') ->forPage($page, $limit) ->orderByDesc('orders.created_at') ->get(); } catch (ValidationException $e) { return $this->error($e->validator->errors()->first()); } catch (Exception $e) { return $this->error($e->getMessage()); } return $this->success(['count' => $count, 'stateList' => OrderShip::getStateList(), 'list' => $list]); } //订单详情 function info() { try { request()->validate([ 'order_id' => ['required', 'string', 'min:19', 'max:19'], ]); $orderId = request()->input('order_id'); $order = OrderModel::with(['store'])->where('id', $orderId)->first(); if (!$order) throw new Exception('订单不存在'); $order = $order->toArray(); ksort($order); //订单的商品列表 $list = OrderItem::where('order_id', $orderId) ->get()->makeHidden(['total_cost', 'system_price', 'profit']) ->toArray(); foreach ($list as &$item) { $goodsLanguages = GoodsLanguages::where('goods_id', $item['goods_id']) ->get()->toArray(); $goodsLanguages = Util::getDataByLanguageCode($goodsLanguages, $this->lang); $item['goods_name'] = $goodsLanguages['name']; $sku = GoodsSku::where('sku_id', $item['sku_id'])->first(); if (empty($sku)) { $item['cover_img'] = ''; $item['attributes'] = []; } else { $sku = $sku->toArray(); $item['cover_img'] = $sku['cover_img']; $item['attributes'] = AttrValue::getAttr($sku['attr_value_ids'], $this->lang); } } $order['goods'] = $list; //退款驳回的原因 $order['failure_msg'] = ''; if ($order['return_status'] == 3) { $order['failure_msg'] = OrderRefund::where('order_id', $orderId)->pluck('failure_msg')->first(); } } catch (ValidationException $e) { return $this->error($e->validator->errors()->first()); } catch (Exception $e) { return $this->error($e->getMessage()); } $order['stateList'] = OrderShip::getStateList(); $order['ship_time'] = OrderShip::where(['order_id' => $orderId, 'state' => 3])->value('created_at'); return $this->success($order); } /** * @api {post} /order/ship 发货 * @apiGroup 订单管理 * @apiVersion 1.0.0 * @apiUse header * @apiUse lang * * @apiParam {String[]} order_id 订单id */ public function ship() { $errors = []; DB::beginTransaction(); try { $params = request()->validate([ 'order_id' => ['required', 'array', 'min:1'], 'order_id.*' => ['string', 'min:19', 'max:19'], ]); $orderIds = $params['order_id']; foreach ($orderIds as $orderId) { $order = OrderModel::where('id', $orderId)->first(); if (!$order) continue; if ($order->pay_status != 1 ) { continue; // $errors = ['id' => $orderId]; // throw new Exception("当前订单未支付"); } if ($order->purchase_status != 1 ) { continue; // $errors = ['id' => $orderId]; // throw new Exception("当前订单未采购"); } if ($order->status != 2 ) { continue; // $errors = ['id' => $orderId]; // throw new Exception("当前订单未到待发货状态"); } if ($order->return_status != 0 ) { continue; // $errors = ['id' => $orderId]; // throw new Exception("当前订单已申请退款"); } $order->status = 3; $order->ship_state = 3; $order->save(); OrderLog::addData([ 'order_id' => $order->id, 'state' => 7 ]); //记录物流 OrderShip::addData($order->id, 3); } DB::commit(); } catch (ValidationException $e) { DB::rollBack(); return $this->error($e->validator->errors()->first()); } catch (Exception $e) { DB::rollBack(); return $this->error($e->getMessage(), $errors); } return $this->success(); } public function updateOrderShip() { try { $params = request()->validate([ 'order_id' => ['required', 'string', 'min:19', 'max:19'], 'state' => ['required', 'integer', 'in:4,5,6,7,8,9'], ]); $orderId = $params['order_id']; $state = $params['state']; $order = OrderModel::where('id', $orderId)->first(); if (!$order) throw new Exception('订单不存在'); if ($order->status < 3 ) { throw new Exception("当前订单未到已发货状态"); } Db::beginTransaction(); $order->ship_state = $state; $order->save(); OrderShip::addData($order->id, $state); Db::commit(); } catch (ValidationException $e) { return $this->error($e->validator->errors()->first()); } catch (Exception $e) { Db::rollBack(); return $this->error($e->getMessage()); } return $this->success(); } //查看订单物流 public function orderShip() { try { $params = request()->validate([ 'order_id' => ['required', 'string', 'min:19', 'max:19'], ]); $orderId = $params['order_id']; $ship = OrderModel::getOrderShip($orderId); } catch (ValidationException $e) { return $this->error($e->validator->errors()->first()); } catch (Exception $e) { return $this->error($e->getMessage()); } return $this->success($ship); } }