model = Order::find()->where(['store_id'=>$this->store_id, 'is_delete' => 0]); } public function rules() { return [ [['id', 'status', 'type', 'is_default'], 'integer'], [['date_range'], 'array'], [['remark', 'mch_name', 'mch_mobile', 'mch_address'], 'string'] ]; } //订单管理 public function orderList(){ $status = $this->status; $date_range = $this->date_range; $this->initOrder(); $model = $this->model; $order_query = $model->select('id, order_no, pay_price, total_price, is_pay, trade_status, created_at, is_sale, trade_status, apply_delete, 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']]; } $item['cancel_info'] = $cancel_info ?: 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 { $id = $this->id; $this->initOrder(); $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, saas_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; $form->saas_id = $order->saas_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, 0); //打印 $printer_order = new PrintOrder($order->store_id, $order->id, 'confirm', 0, 0, 0, $order['mch_id']); $printer_order->is_refund = true; $res = $printer_order->print_order(); // if ($res['code'] != 0) { // throw new \Exception($res['msg']); // } //如果已经转单 就调用取消接口 $orderTransit = OrderTransit::findOne(['order_id' => $order->id, 'is_delete' => 0]); if ($orderTransit) { $merchant = new \app\modules\admin\models\MerchantForm(); $merchant->order_id = $orderTransit->cloud_order_id; $merchant->store_id = $order->store_id; $purchaseOrderCancel = $merchant->purchaseOrderCancel(); } return [ 'code' => 0, 'msg' => '操作成功', ]; } else { throw new \Exception($res['msg']); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage().$e->getLine() ]; } } //拒绝取消订单 public function orderRejectCancel(){ $where = [ 'id' => $this->id, 'is_delete' => Order::IS_DELETE_FALSE, 'store_id' => $this->store_id, 'mch_id' => 0, ]; $order = Order::findOne($where); if (!$order || $order->mch_id > 0) { return [ 'code' => 1, 'msg' => '订单不存在,请刷新页面后重试' ]; } $order->apply_delete = Order::ORDER_APPLY_DELETE_DEFAULT; if(!$order->save()){ return [ 'code' => 1, 'msg' => '操作失败,' . array_shift($order->getFirstErrors()), ]; } return [ 'code' => 0, 'msg' => '操作成功', ]; } //售后订单 public function orderRefund(){ try { $status = $this->status; $date_range = $this->date_range; $store_id = $this->store_id; $query = OrderRefund::find()->alias('or')->where(['or.user_delete' => 0, 'or.store_id' => $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'); $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; $store_id = $this->store_id; // 判断是否为普通订单 or 售后订单 if ($type == 0) { $model = Order::find()->where(['o.store_id'=>$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, order_transit_id, goods_id')->asArray(); }])->leftJoin(['s' => Store::tableName()], 's.id = o.store_id') ->select('o.id, o.is_pay, o.trade_status, 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.province_id, o.city_id, o.district_id, o.md_id') ->asArray()->one(); if (empty($model)) { throw new \Exception("订单信息错误"); } $model['is_trans'] = false; 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); //当前商品绑定的云仓商品 $item['goods_bind'] = (bool)CloudGoodsBind::findOne(['store_id' => $this->store_id, 'goods_id' => $item['goods_id'], 'is_delete' => 0]); if ($item['goods_bind']) { //存在订单转单id if ($item['order_transit_id']) { $order_transit = OrderTransit::findOne($item['order_transit_id']); if ($order_transit) { $item['status'] = (int)$order_transit->status; } else { $model['is_trans'] = true; $item['status'] = 3; } } else { //可转单 $item['status'] = 3; $model['is_trans'] = true; } } } $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 = $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; } $address = []; //获取退款的总商品金额 / 获取最新一条的状态 $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' => $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 = $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' => $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' => $store_id, 'is_delete' => 0])->select('id, name, address, mobile, is_default')->asArray()->all(); } //查询快递公司 $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(){ $store_id = $this->store_id; $storeExpressList = Order::find() ->select('express') ->where([ 'and', ['store_id' => $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; $store_id = $this->store_id; $model = RefundAddress::findOne($id)?:new RefundAddress(); $model->store_id = $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 { $store_id = $this->store_id; $list = RefundAddress::find()->where(['store_id' => $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; $store_id = $this->store_id; RefundAddress::updateAll(['is_default' => 0],['store_id' => $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() ]; } } }