model = Order::find()->where(['store_id'=>get_store_id(), 'is_delete' => 0, 'is_recycle' => 0]); $whereMch = []; if($this->mch_id){ $whereMch = ['mch_id' => $this->mch_id]; $this->model->andWhere($whereMch); } } public function rules() { return [ [['id', 'status', 'type', 'is_default'], 'integer'], [['date_range'], 'array'], [['price','express_price'], 'number'], [['mch_id'], 'safe'], [['remark', 'mch_name', 'mch_mobile', 'mch_address'], 'string'] ]; } //订单管理 public function orderList(){ $status = $this->status; $date_range = $this->date_range; $model = $this->model; $order_query = $model->select('id, order_no, total_price, pay_price, is_pay, trade_status, created_at, is_sale, trade_status, is_trans, md_id, pay_type')->with(['orderDetail'])->orderBy('created_at desc'); //筛选状态 switch ($status) { case 1: // $time = time() - 60 * 15; $order_query->andWhere(['OR', ['is_pay' => Order::IS_PAY_FALSE, 'trade_status' => Order::ORDER_FLOW_DEFAULT], ['AND', ['is_pay' => Order::IS_PAY_FALSE, 'pay_type' => Order::PAY_TYPE_COD], ['<>', 'trade_status', Order::ORDER_FLOW_CANCEL]]]); //未支付 break; case 2: $order_query->andWhere(['OR', ['trade_status' => Order::ORDER_FLOW_NO_SEND], ['is_pay' => Order::IS_PAY_FALSE, 'pay_type' => Order::PAY_TYPE_COD, 'trade_status' => Order::ORDER_FLOW_DEFAULT]]); //待发货 break; case 3: $order_query->andWhere(['trade_status' => 2]); //待收获 break; } if (!empty($date_range)) { //获取开始结束时间 $begin_time = strtotime($date_range['begin_time']); $end_time = strtotime($date_range['end_time']) + (60 * 60 * 24); $order_query->andWhere(['and', ['>', 'created_at', $begin_time], ['<', 'created_at', $end_time]]); } $page = pagination_make($order_query); $list = $page['list']; foreach ($list as &$item) { foreach ($item['orderDetail'] as &$od) { if (!empty($od['attr'])) { $od['attr'] = json_decode($od['attr'],true); } unset($od['goods_info']); unset($od['goods_attr']); } $item['status'] = (int)$item['trade_status']; if ($item['is_sale'] == 1) { $item['status'] = 4; } // if ($item['status'] === -1 && ($item['created_at'] + 60 * 15 < time()) && $item['is_pay'] == 0 && intval($item['pay_type']) !== 2) { // $item['status'] = 1; // } $item['pay_type'] = intval($item['pay_type']); //获取退款的总商品金额 / 获取最新一条的状态 $cancel_info = []; $order_cancel = OrderGoodsCancel::find()->where(['order_id' => $item['id']]) ->orderBy('id desc')->select('status')->asArray()->one(); if ($order_cancel) { $cancel_info['refund_price'] = OrderGoodsCancel::find()->where(['order_id' => $item['id']])->sum('refund_price'); $cancel_info['status_text'] = OrderGoodsCancel::$status_desc[$order_cancel['status']]; } if ($cancel_info) { $cancel_info['cancel_list'] = OrderGoodsCancel::find()->alias('og') ->leftJoin(['od' => OrderDetail::tableName()], 'og.order_detail_id = od.id') ->where([ 'og.order_id' => $item['id'] ]) ->orderBy('og.id desc') ->select('od.goods_name, og.status, og.num, og.refund_price, og.created_at, og.updated_at') ->asArray() ->all(); foreach ($cancel_info['cancel_list'] as &$cancel_item) { $cancel_item['status_text'] = OrderGoodsCancel::$status_text[$cancel_item['status']]; $cancel_item['status'] = intval($cancel_item['status']); if ($cancel_item['status'] === OrderGoodsCancel::STATUS_PAY) { $cancel_item['status'] = 1; } if ($cancel_item['status'] === OrderGoodsCancel::STATUS_PAY_FAIL) { $cancel_item['status'] = 0; } if ($cancel_item['status'] === OrderGoodsCancel::STATUS_CANCEL) { $cancel_item['status'] = 2; } $cancel_item['created_at'] = date('Y-m-d H:i:s', $cancel_item['created_at']); if (in_array($cancel_item['status'], [OrderGoodsCancel::STATUS_PASS, OrderGoodsCancel::STATUS_REFUSE])) { $cancel_item['created_at'] = date('Y-m-d H:i:s', $cancel_item['updated_at']); } } } $item['cancel_info'] = $cancel_info ?: null; //获取退款的总商品金额 / 获取最新一条的状态 $handle_cancel = OrderGoodsCancel::find()->where(['order_id' => $item['id'], 'status' => [ OrderGoodsCancel::STATUS_APPLY, OrderGoodsCancel::STATUS_PAY_FAIL ]])->select('SUM(num) as num, SUM(refund_price) as refund_price')->groupBy('order_id') ->asArray()->one() ?: null; $item['handle_cancel'] = $handle_cancel ?: null; unset($item['trade_status']); } //查询快递公司 $express_list = Express::find()->where(['is_delete' => 0])->select('id, name')->all(); return [ 'code' => 0, 'msg' => "获取成功", 'data' => [ 'list' => $list, 'express_list'=>$express_list, 'pageNo' => $page['pageNo'], 'totalCount' => $page['totalCount'] ] ]; } //取消订单 public function orderCancel(){ try { checkReplay($this->id); $id = $this->id; $model = $this->model; $remark = $this->remark; $order = $model->andWhere(['id' => $id])->select('id, remark, mch_id, user_id, mobile, order_no, pay_price, store_id')->one(); if (!$order || $order->mch_id > 0) { throw new \Exception('订单不存在,请刷新页面后重试'); } // 发送备注消息 $order->remark = $remark??''; $order->first_price = 0; $order->second_price = 0; $order->third_price = 0; $form = new OrderRevokeForm(); $form->order_id = $order->id; $form->delete_pass = true; $form->user_id = $order->user_id; $form->store_id = $order->store_id; $res = $form->save(); if ($res['code'] == 0) { if (!$order->save()) { throw new \Exception(json_encode($order->errors)); } $goods = Goods::findOne(OrderDetail::findOne(['order_id' => $order->id])->goods_id); //通知用户 NoticeSend::OrderCancel($order->user_id, $order->mobile, $order->order_no, $order->pay_price, $goods->name); // 订单打印 if ((int)$order->md_id === -1 || (int)$order->md_id === 0 || !isset($order->md_id)) { $order->md_id = 0; } //打印 $printer_order = new PrintOrder($order->store_id, $order->id, 'confirm', 0, $order->md_id, 0, $order['mch_id']); $printer_order->is_refund = true; $res = $printer_order->print_order(); if ($res['code'] != 0) { throw new \Exception($res['msg']); } return [ 'code' => 0, 'msg' => '操作成功', ]; } else { throw new \Exception($res['msg']); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage().$e->getLine() ]; } } //售后订单 public function orderRefund(){ try { $whereMch = []; if($this->mch_id){ $whereMch = ['mch_id' => $this->mch_id]; } $status = $this->status; $date_range = $this->date_range; $query = OrderRefund::find()->alias('or')->where(['or.user_delete' => 0, 'or.store_id' => get_store_id(), 'or.md_id' => 0]); //商家信息 $query->leftJoin(['s' => Store::tableName()], 's.id = or.store_id'); //订单总金额信息 $query->leftJoin(['o' => Order::tableName()], 'o.id = or.order_id')->andWhere($whereMch); $query->select('or.id, or.store_id, or.order_id, or.order_detail_id, or.status, or.is_agree, or.updated_at, s.name, o.pay_price all_price, o.mobile, or.created_at')->orderBy('created_at desc'); switch ($status) { case 1: //处理中 $query->andWhere(['or.status' => 0]); break; case 2: //已完成 $query->andWhere(['or', ['or.status' => 1], ['or.status' => 2], ['or.status' => 3]]); break; } if (!empty($date_range)) { //获取开始结束时间 $begin_time = strtotime($date_range['begin_time']); $end_time = strtotime($date_range['end_time']) + (60 * 60 * 24); $query->andWhere(['and', ['>', 'or.created_at', $begin_time], ['<', 'or.created_at', $end_time]]); } $page = pagination_make($query); $list = $page['list']; foreach($list as &$item){ $order_detail_id = json_decode($item['order_detail_id'],true); $item['orderDetail'] = OrderDetail::find()->where(['id' => $order_detail_id])->select('id, goods_name name, num, total_price, attr, pic')->asArray()->all(); foreach($item['orderDetail'] as &$od){ $od['attr'] = json_decode($od['attr'], true); } if ($item['is_agree'] == 1 && $item['status'] == 0) { //已经同意,需要用户发货 $item['status'] = 4; } if ($item['is_user_send'] == 1) { //用户已经发货 商家等待收货 $item['status'] = 5; } } return [ 'code' => 0, 'msg' => "获取成功", 'data' => [ 'list' => $list, 'pageNo' => $page['pageNo'], 'totalCount' => $page['totalCount'] ] ]; } catch (\Exception $e) { return [ 'code' => 0, 'msg' => $e->getMessage() ]; } } //订单详情 public function orderDetail(){ try { $id = $this->id; $type = $this->type; // 判断是否为普通订单 or 售后订单 if ($type == 0) { $model = Order::find()->where(['o.store_id'=>get_store_id(), 'o.is_delete' => 0])->alias('o'); $model = $model->andWhere(['o.id' => $id])->with(['detail'=>function($query){ $query->select('id, goods_name, pic, total_price, delivery_type, attr, num, order_id, goods_id, goods_info')->asArray(); }])->leftJoin(['s' => Store::tableName()], 's.id = o.store_id') ->select('o.id, o.is_pay, o.md_id, o.is_trans, o.trade_status, o.order_type, o.name, o.mobile, o.address, o.store_id, o.order_no, o.created_at, o.pay_type, o.pay_price, o.express_price, o.send_time, o.express, o.express_no, s.name store_name, s.logo, o.is_offline, o.is_delivery, o.user_id,o.before_update_price') ->asArray()->one(); if (empty($model)) { throw new \Exception("订单信息错误"); } $model['md_name'] = ''; if ($model['md_id'] > 0) { $model['md_name'] = Md::find()->where(['id' => $model['md_id']])->select('name')->scalar(); } foreach ($model['detail'] as &$item) { $item['cancel_info'] = OrderGoodsCancel::find()->where(['order_detail_id' => $item['id'], 'status' => [ OrderGoodsCancel::STATUS_APPLY, OrderGoodsCancel::STATUS_PAY_FAIL ]])->select('id, status, num, refund_price, created_at')->asArray()->one(); if ($item['cancel_info']) { $item['cancel_info']['created_at'] = date('Y-m-d H:i:s', $item['cancel_info']['created_at']); $item['cancel_info']['status_text'] = OrderGoodsCancel::$status_text[$item['cancel_info']['status']]; } $item['cancel_info'] = $item['cancel_info'] ?: null; $item['attr'] = json_decode($item['attr'], true); $goods = HelpersJson::decode($item['goods_info']); $item['order_form']['is_form'] = $goods['is_form']; $item['order_form']['name'] = $goods['form_name']; $item['order_form']['list'] = OrderForm::find()->where(['order_id' => $model['id'], 'goods_id' => $item['goods_id']])->asArray()->all(); foreach ($item['order_form']['list'] as &$form_item) { $form_item['default'] = $form_item['value']; if ($form_item['type'] == "checkbox" || $form_item['type'] == "radio") { $list = explode(',', $form_item['value']); $list_arr = []; foreach ($list as &$list_item) { $arr = []; $arr['name'] = $list_item; $arr['checked'] = true; array_push($list_arr, $arr); } $form_item['default_list'] = $list_arr; } $form_item['name'] = $form_item['key']; unset($form_item['key']); } } $model['order_type'] = $model['order_type'] * 1; $model['status'] = $model['trade_status'] * 1; $model['goods_price'] = sprintf("%.2f", $model['pay_price'] - $model['express_price']); $model['delivery_type'] = $model['detail'][0]['delivery_type']; $model['created_at'] = date("Y-m-d H:i:s", $model['created_at']); //判断何时会自动收货 $time = time(); $delivery_time = Option::get(OptionSetting::STORE_DELIVERY_TIME)['value']; $delivery_time = $time - ($delivery_time * 86400); // 查询物流信息 $express_detail = new ExpressDetail(); $express_detail->express = $model['express']; $express_detail->express_no = $model['express_no']; $express_detail->receive_mobile = $model['mobile']; $express_detail->store_id = get_store_id(); $res = $express_detail->search(); $model['express_detail'] = null; if ($res['code'] != 0) { $model['express_detail']['list'] = []; $model['express_detail']['status'] = 0; $model['express_detail']['status_text'] = '未知'; } else { $model['express_detail'] = $res['data']; } if ($model['status'] == 2) { $model['pay_time'] = $delivery_time; } $diy_shop_name = Option::get(OptionSetting::DIY_SHOP_NAME, get_store_id(), 'store', '自提配送')['value'] ?: '自提配送'; $diy_shop_name = Option::get(OptionSetting::DIY_SHOP_NAME, get_store_id(), 'pay', $diy_shop_name)['value']; $diy_express_name = Option::get(OptionSetting::DIY_EXPRESS_NAME, get_store_id(), 'store', '快递配送')['value'] ?: '快递配送'; $diy_express_name = Option::get(OptionSetting::DIY_EXPRESS_NAME, get_store_id(), 'pay', $diy_express_name)['value']; $diy_delivery_name = Option::get(OptionSetting::DIY_DELIVERY_NAME, get_store_id(), 'store', '同城配送')['value'] ?: '同城配送'; $diy_delivery_name = Option::get(OptionSetting::DIY_DELIVERY_NAME, get_store_id(), 'pay', $diy_delivery_name)['value']; $model['diy_express_name'] = intval($model['is_offline']) === 1 ? $diy_shop_name : (intval($model['is_delivery']) === 1 ? $diy_delivery_name : $diy_express_name); $address = []; if ((int)$model['is_offline'] === 1) { if ($model['md_id'] > 0) { $md = Md::find()->where(['id' => $model['md_id']])->select('name, mobile, address')->asArray()->one(); $model['name'] = $md['name']; $model['mobile'] = $md['mobile']; $model['address'] = $md['address']; } } $model['order_form'] = OrderForm::findAll(['order_id' => $model['id']]); //获取退款的总商品金额 / 获取最新一条的状态 $handle_cancel = OrderGoodsCancel::find()->where(['order_id' => $model['id'], 'status' => [ OrderGoodsCancel::STATUS_APPLY, OrderGoodsCancel::STATUS_PAY_FAIL ]])->select('SUM(num) as num, SUM(refund_price) as refund_price')->groupBy('order_id')->asArray()->one() ?: null; $model['handle_cancel'] = $handle_cancel ?: null; } else { $model = OrderRefund::find()->where(['store_id' => get_store_id(), 'is_delete' => 0, 'user_delete' => 0, 'id' => $id]) ->select('id, store_id, user_id, order_detail_id, status, is_agree, order_refund_no, is_user_send, created_at, refuse_desc, address_id, pic_list, refund_price, desc, type, user_send_express, user_send_express_no')->asArray()->one(); if (empty($model)) { throw new \Exception("订单信息错误"); } $model['pic_list'] = json_decode($model['pic_list'], true); $order_detail_id = json_decode($model['order_detail_id'], true); $model['detail'] = OrderDetail::find()->where(['id' => $order_detail_id])->select('id, goods_name, num, total_price, attr, pic')->asArray()->all(); foreach ($model['detail'] as &$item) { $item['attr'] = json_decode($item['attr'], true); $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']); } $model['store'] = RefundAddress::find()->where(['id' => $model['address_id']])->select('id, name, address, mobile')->asArray()->one(); \Yii::error($model['status'],"这是一个status"); $model['status'] = $model['status'] * 1; if ($model['is_agree'] == 1 && $model['status'] == 0) { //已经同意,需要用户发货 $model['status'] = 4; } if ($model['is_user_send'] == 1 && $model['status'] == 0) { //用户已经发货 商家等待收货 $model['status'] = 5; } // 查询物流信息 $express_detail = new ExpressDetail(); $express_detail->express = $model['user_send_express']; $express_detail->express_no = $model['user_send_express_no']; $express_detail->store_id = get_store_id(); $res = $express_detail->search(); $model['express_detail'] = null; if ($res['code'] != 0) { $model['express_detail']['list'] = []; $model['express_detail']['status'] = 0; $model['express_detail']['status_text'] = '未知'; } else { $model['express_detail'] = $res['data']; } //如果地址存在则用订单地址 如果不存在则用默认地址 $where = ['id' => $model['address_id']]; if (empty($model['address_id'])) { $where = ['is_default' => 1]; } $address_one = RefundAddress::find()->where(['store_id' => get_store_id(), 'is_delete' => 0])->andWhere($where)->select('id, name, address, mobile')->asArray()->one(); if (!empty($address_one)) {//'is_default' => 1 $model['name'] = $address_one["name"]; $model['mobile'] = $address_one["mobile"]; $model['address'] = $address_one["address"]; } $address = RefundAddress::find()->where(['store_id' => get_store_id(), 'is_delete' => 0])->select('id, name, address, mobile, is_default')->asArray()->all(); } $model['user_mobile'] = ''; $user = \app\models\User::findOne($model['user_id']); if ($user) { $model['user_mobile'] = $user->binding; } //查询快递公司 $express_list = Express::find()->where(['is_delete' => 0])->select('id, name')->all(); return [ 'code' => 0, 'msg' => "获取成功", 'data' => [ 'order' => $model, 'express' => $express_list, 'address' => $address ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //获取快递公司数据 public function express(){ $storeExpressList = Order::find() ->select('express') ->where([ 'and', ['store_id' => get_store_id()], ['is_pay' => Order::IS_PAY_TRUE], ['in', 'trade_status', [Order::ORDER_FLOW_SEND, Order::ORDER_FLOW_CONFIRM]], ['!=', 'express', ''], ])->groupBy('express, send_time')->orderBy('send_time DESC')->limit(5)->asArray()->all(); $expressLst = Express::getExpressList(); $newStoreExpressList = []; foreach ($storeExpressList as $i => $item) { foreach ($expressLst as $value) { if ($value['name'] == $item['express']) { $newStoreExpressList[] = $item['express']; break; } } } $newPublicExpressList = []; foreach ($expressLst as $i => $item) { $newPublicExpressList[] = $item['name']; } $express_list = array_merge($newStoreExpressList, $newPublicExpressList); return [ 'code' => 0, 'msg' => "获取成功", 'data' => [ 'express' => $express_list, ] ]; } //售后订单申请 public function orderRefundApply(){ try { $id = $this->id; $status = $this->status; $model = OrderRefund::find()->where(['id' => $id])->select('id, is_agree, status')->one(); if (empty($model)) { throw new \Exception("订单信息错误"); } //已同意 if ($status == 1) { $model->status = 1; } elseif ($status == 2) { //商户拒绝 $model->status = 3; } if ($model->save()) { throw new \Exception("取消申请失败"); } return [ 'code' => 0, 'msg' => "操作成功" ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //售后订单删除 public function orderRefundDelete(){ try { $id = $this->id; $model = OrderRefund::find()->where(['id' => $id])->select('id, is_delete')->one(); if (empty($model)) { throw new \Exception("订单信息错误"); } $model->is_delete = 1; if ($model->save()) { throw new \Exception("删除失败"); } return [ 'code' => 0, 'msg' => "删除成功" ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //商户收货地址保存 public function refundAddressSave(){ try { $id = $this->id; $model = RefundAddress::findOne($id)?:new RefundAddress(); $model->store_id = get_store_id(); $model->name = $this->mch_name; $model->mobile = $this->mch_mobile; $model->address = $this->mch_address; if (!$model->save()) { throw new \Exception("保存失败"); } //是否默认 if ($this->is_default) { $this->id = $model->id; $this->refundAddressDefault(); } return [ 'code' => 0, 'msg' => "保存成功", 'data' => [ 'address_id' => $model->id ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //获取地址列表 public function refundAddress(){ try { $list = RefundAddress::find()->where(['store_id' => get_store_id(), 'mch_id' => 0, 'is_delete' => 0])->select('id, name, address, mobile, is_default')->asArray()->all(); foreach ($list as &$item) { $item['is_default'] *= 1; } return [ 'code' => 0, 'msg' => "获取成功", 'data' => $list ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //获取地址详情 public function getAddressInfo(){ try { $id = $this->id; $model = RefundAddress::findOne($id); return [ 'code' => 0, 'msg' => "获取成功", 'data' => $model ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //设置默认地址 public function refundAddressDefault(){ try { $id = $this->id; RefundAddress::updateAll(['is_default' => 0],['store_id' => get_store_id()]); $model = RefundAddress::findOne($id); if (empty($model)) { throw new \Exception("查询不到地址信息"); } $model->is_default = 1; if (!$model->save()) { throw new \Exception("设置失败"); } return [ 'code' => 0, 'msg' => "设置成功", ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //商户地址删除 public function refundAddressDel(){ try { $id = $this->id; $model = RefundAddress::findOne($id); if (empty($model)) { throw new \Exception("查询不到地址信息"); } $model->is_delete = 1; if (!$model->save()) { throw new \Exception("删除失败"); } return [ 'code' => 0, 'msg' => "删除成功", ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //设置订单收货地址 public function setRefundAddress(){ try { $id = $this->id; $address_id = $this->mch_address; $model = OrderRefund::findOne($id); $model->address_id = $address_id; if (!$model->save()) { throw new \Exception("设置失败"); } return [ 'code' => 0, 'msg' => "设置成功", ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //修改订单价格 public function ModifyPrice() { try { $order = Order::findOne([ 'id' => $this->id, 'is_delete' => Order::IS_DELETE_FALSE, 'is_pay' => Order::IS_PAY_FALSE ]); if (!$order) { return [ 'code' => 1, 'msg' => '订单错误' ]; } $money = doubleval($order->pay_price); if ($this->price || $this->price == 0) { if ($this->price < 0) { return [ 'code' => 1, 'msg' => '支付价格不能小于0' ]; } if ($order->before_update_price) { } else { $order->before_update_price = $money; } $order->pay_price = $this->price + $order->express_price; } if ($order->save()) { $orderDetailList = OrderDetail::findAll(['order_id' => $order->id, 'is_delete' => 0]); $goodsTotalPrice = 0.00; $goodsTotalPayPrice = $order->pay_price - $order->express_price; foreach ($orderDetailList as $goods) { $goodsTotalPrice += $goods->total_price; } foreach ($orderDetailList as $goods) { if (in_array(get_plugin_type(), [0, 2])) { $goods->total_price = doubleval(sprintf('%.2f', $goodsTotalPayPrice * $goods->total_price / $goodsTotalPrice)); } $goods->save(); } return [ 'code' => 0, 'msg' => '成功' ]; } else { throw new \Exception($this->getErrorSummary(false)[0]); } }catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage().$e->getLine(), 'trea'=>$e->getTraceAsString() ]; } } //修改运费价格 public function ExpressPrice() { try { $order = Order::findOne([ 'id' => $this->id, 'is_delete' => Order::IS_DELETE_FALSE, 'is_pay' => Order::IS_PAY_FALSE ]); if (!$order) { return [ 'code' => 1, 'msg' => '订单错误' ]; } $money = doubleval($order->pay_price);//30 $express = doubleval($order->express_price);//10 if ($express != $this->express_price) { if ($this->express_price < 0) { return [ 'code' => 1, 'msg' => '运费不能小于0' ]; } $order->before_update_express = $express; // 计算新的支付总价 = 原支付总价 - 原运费 + 新运费 $order->pay_price = $money - $express + $this->express_price; $order->express_price = $this->express_price; // 设置新运费 } if ($order->save()) { $orderDetailList = OrderDetail::findAll(['order_id' => $order->id, 'is_delete' => 0]); $goodsTotalPrice = 0.00; $goodsTotalPayPrice = $order->pay_price - $order->express_price; foreach ($orderDetailList as $goods) { $goodsTotalPrice += $goods->total_price; } foreach ($orderDetailList as $goods) { if (in_array(get_plugin_type(), [0, 2])) { $goods->total_price = doubleval(sprintf('%.2f', $goodsTotalPayPrice * $goods->total_price / $goodsTotalPrice)); } $goods->save(); } return [ 'code' => 0, 'msg' => '成功' ]; } else { throw new \Exception($this->getErrorSummary(false)[0]); } }catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage().$e->getLine(), 'trea'=>$e->getTraceAsString() ]; } } //单品退款 public function orderGoodsCancelList() { try { $status = $this->status; $query = OrderGoodsCancel::find()->where(['store_id' => get_store_id()]); if (isset($status) && in_array($status, [ OrderGoodsCancel::STATUS_APPLY, OrderGoodsCancel::STATUS_PASS, OrderGoodsCancel::STATUS_REFUSE ])) { if (intval($status) === OrderGoodsCancel::STATUS_PASS) { $query->andWhere(['status' => [ OrderGoodsCancel::STATUS_PASS, OrderGoodsCancel::STATUS_PAY, OrderGoodsCancel::STATUS_PAY_FAIL ]]); } else { $query->andWhere(['status' => $status]); } } $query = $query->select('id, status, order_id, order_detail_id, refund_price, created_at, num') ->orderBy('created_at DESC'); $list = pagination_make($query); foreach ($list['list'] as &$item) { $order = Order::findOne($item['order_id']); $item['order_no'] = $order->order_no; $item['status'] = intval($item['status']); $item['status_text'] = OrderGoodsCancel::$status_text[$item['status']]; $goods_list = OrderDetail::find()->where(['id' => $item['order_detail_id']]) ->select('goods_name, pic, attr')->asArray()->all(); foreach ($goods_list as &$goods_item) { $goods_item['attr'] = json_decode($goods_item['attr'], true); $goods_item['refund_price'] = $item['refund_price']; $goods_item['num'] = $item['num']; } $item['goods_list'] = $goods_list; } return [ 'code' => 0, 'msg' => '', 'data' => $list ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() . $e->getLine() . $e->getFile() ]; } } }