| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\AgentFrontErpCloudGoods;
- use app\models\AgentFrontErpInventory;
- use app\models\AgentFrontErpInventoryLog;
- use app\models\DeliveryInfo;
- use app\models\District;
- use app\models\Express;
- use app\models\FrontDeliveryOrder;
- use app\models\FrontExpressOrder;
- use app\models\LocalDeliveryCourier;
- use app\models\Md;
- use app\models\Order;
- use app\models\User;
- use app\utils\ExportList;
- use \yii\db\Query;
- use yii\base\Model;
- use app\models\Store;
- use app\models\Admin;
- use app\models\SaasUser;
- use app\models\AgentFrontBind;
- use app\models\Supplier;
- use app\models\AgentBind;
- class AgentFrontForm extends Model
- {
- public $page = 1;
- public $limit = 10;
- public $id;
- public $line_name;
- public $name;
- public $tel;
- public $car_no;
- public $admin;
- public $admin_id;
- public $front_agent_admin_name;
- public $front_agent_admin_id;
- public $is_cloud_delivery;
- public $status;
- public $store_id;
- public $md_id;
- public $store_name;
- public $store_phone;
- public $supplier_id;
- public $supplier_name;
- public $supplier_phone;
- public $order_id;
- public $express_name;
- public $express_no;
- public $send_type;
- public $words;
- public $zip_code;
- public $order_no;
- public $front_agent_name;
- public $type;
- public $md_name;
- public function rules()
- {
- return [
- [['md_id', 'store_id', 'id', 'type', 'order_id', 'send_type', 'is_cloud_delivery'], 'integer'],
- [['order_no', 'front_agent_name', 'store_name', 'supplier_name', 'md_name', 'express_name', 'express_no', 'words', 'zip_code'], 'string']
- ];
- }
- //前置仓列表
- public function list($getNameList = 0) {
- $query = Admin::find()->where(['type' => Admin::ADMIN_TYPE_FRONT_AGENT, 'is_delete' => 0]);
- if($this->admin['username'] != 'admin' && $this->admin['type'] != Admin::ADMIN_TYPE_SUPPLIER && $this->admin['type'] != Admin::ADMIN_TYPE_SAAS_STAFF){
- $query1 = (new Query())->select('admin_id')->from(AgentBind::tableName())->where(['type' => 2, 'parent_admin_id' => $this->admin_id]);
- $query->andWhere(['id' => $query1]);
- unset($query1);
- }
- if($this->name){
- $query->andWhere(['like', 'name', $this->name]);
- }
- $query->orderBy('id desc');
- if($getNameList){
- return [
- 'code' => 0,
- 'data' => $query->select(['id', 'name'])->all(),
- // 'sql' => $query->createCommand()->getRawSql(),
- ];
- }
- $res = pagination_make($query);
- foreach ($res['list'] as &$item) {
- $district = District::find()->where(['id' => [$item['province_id'], $item['city_id'], $item['district_id']]])->select('name')->column();
- $district = implode('', $district);
- $item['address'] = $district . $item['address'];
- unset($item['password']);
- $item['supplier_count'] = AgentFrontBind::find()->where(['is_delete' => 0, 'type' => 0, 'status' => 1, 'front_agent_admin_id' => $item['id']])->count();
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $res,
- // 'sql' => $query->createCommand()->getRawSql(),
- ];
- }
- //前置仓绑定关系(商家)列表
- public function bindStoreList() {
- $agentId = $this->admin['id'];
- if($this->admin['username'] == 'admin'){
- $agentId = null;
- }
- if($this->admin['type'] == Admin::ADMIN_TYPE_FRONT_AGENT){
- $agentId = null;
- $this->front_agent_admin_id = $this->admin['id'];
- }
- $query = AgentFrontBind::find()->where(['type' => 1, 'is_delete' => 0]);
- if($this->status >= 0){
- $query->andWhere(['status' => $this->status]);
- }
- if($this->store_id > 0){
- $query->andWhere(['type_id' => $this->store_id]);
- }
- if($this->admin['type'] == Admin::ADMIN_TYPE_STORE){
- $query->andWhere(['type_id' => $this->admin['type_id']]);
- }
- if($this->type == 1){
- $query->andWhere(['>', 'md_id', 0]);
- }
- if($this->front_agent_admin_id){
- $front_agent_admin_id = intval($this->front_agent_admin_id);
- }else{
- if($this->admin['username'] != 'admin' && $this->admin['type'] != Admin::ADMIN_TYPE_STORE){
- $front_agent_admin_id = (new Query())->select('admin_id')->from(AgentBind::tableName())->where(['type' => 2, 'parent_admin_id' => $this->admin_id]);
- }
- }
- isset($front_agent_admin_id) && $query->andWhere(['front_agent_admin_id' => $front_agent_admin_id]);
- if($this->front_agent_admin_name){
- $front_agent_admin_name = (new Query())->select('id')->from(Admin::tableName())
- ->where(['type' => Admin::ADMIN_TYPE_FRONT_AGENT])
- ->andWhere(['like', 'name', $this->front_agent_admin_name]);
- $query->andWhere(['front_agent_admin_id' => $front_agent_admin_name]);
- }
- $query1 = (new Query())->select('id')->from(Store::tableName());
- $query1->andWhere(['is_delete' => 0]);
- if($this->store_name){
- $query1->andWhere(['like', 'name', $this->store_name]);
- }
- if($query1->where){
- $query->andWhere(['type_id' => $query1]);
- }
- unset($query1);
- // $query1 = (new Query())->select('id')->from(Md::tableName());
- // $query1->andWhere(['is_delete' => 0]);
- // if($this->md_name){
- // $query1->andWhere(['like', 'name', $this->md_name]);
- // }
- // if($query1->where){
- // $query->andWhere(['md_id' => $query1]);
- // }
- // unset($query1);
- $query->orderBy('id desc');
- $res = pagination_make($query);
- foreach ($res['list'] as &$item) {
- $item['store_info'] = Store::find()->where(['id' => $item['type_id']])->asArray()->one();
- $item['front_agent'] = Admin::findOne($item['front_agent_admin_id']);
- $item['md_info'] = Md::find()->where(['id' => $item['md_id']])->select('id, name, cover_url, mobile')->asArray()->one();
- // if($this->type == 1){
- // //做兼容前端 没法区分修改还是添加
- // $item['front_agent_admin_id'] = $item['id'];
- // }
- $item['is_cloud_delivery'] = intval($item['is_cloud_delivery']);
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $res,
- // 'sql' => $query->createCommand()->getRawSql(),
- ];
- }
- //前置仓绑定关系(供货商)列表
- public function bindList() {
- if($this->admin['type'] == Admin::ADMIN_TYPE_FRONT_AGENT){
- $this->front_agent_admin_id = $this->admin['id'];
- }
- $query = AgentFrontBind::find()->where(['type' => 0, 'is_delete' => 0]);
- if($this->status >= 0){
- $query->andWhere(['status' => $this->status]);
- }
- if($this->supplier_id > 0){
- $query->andWhere(['type_id' => $this->supplier_id]);
- }
- if($this->store_id > 0){
- $query->andWhere(['type_id' => $this->store_id]);
- }
- if($this->admin['type'] == Admin::ADMIN_TYPE_SUPPLIER){
- $query->andWhere(['type_id' => $this->admin['type_id']]);
- }
- if($this->front_agent_admin_id){
- $front_agent_admin_id = intval($this->front_agent_admin_id);
- }else{
- if($this->admin['username'] != 'admin' && $this->admin['type'] != Admin::ADMIN_TYPE_SUPPLIER){
- $front_agent_admin_id = (new Query())->select('admin_id')->from(AgentBind::tableName())->where(['type' => 2, 'parent_admin_id' => $this->admin_id]);
- }
- }
- isset($front_agent_admin_id) && $query->andWhere(['front_agent_admin_id' => $front_agent_admin_id]);
- if($this->front_agent_admin_name){
- $front_agent_admin_name = (new Query())->select('id')->from(Admin::tableName())
- ->where(['type' => Admin::ADMIN_TYPE_FRONT_AGENT])
- ->andWhere(['like', 'name', $this->front_agent_admin_name]);
- $query->andWhere(['front_agent_admin_id' => $front_agent_admin_name]);
- }
- $query1 = (new Query())->select('id')->from(Supplier::tableName());
- if($this->supplier_name){
- $query1->andWhere(['like', 'supplier_name', $this->supplier_name]);
- }
- if($this->supplier_phone){
- $query1->andWhere(['like', 'phone', $this->supplier_phone]);
- }
- if($query1->where){
- $query->andWhere(['type_id' => $query1]);
- }
- unset($query1);
- $query->orderBy('id desc');
- $res = pagination_make($query);
- foreach ($res['list'] as &$item) {
- $item['supplier_info'] = Supplier::findOne($item['type_id']);
- $item['front_agent'] = Admin::findOne($item['front_agent_admin_id']);
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $res,
- // 'sql' => $query->createCommand()->getRawSql(),
- ];
- }
- //前置仓绑定关系(供货商/商家) type = 1店铺 0供货商
- public function bind($id = 0, $status = 0, $type = AgentFrontBind::TYPE_SUPPLIER) {
- try{
- if(empty($this->front_agent_admin_id)){
- throw new \Exception('前置仓不能为空');
- }
- $is_cloud_delivery = $this->is_cloud_delivery;
- $type_id = 0;
- if($type == AgentFrontBind::TYPE_SUPPLIER){
- //供货商后台操作
- if(empty($this->supplier_id)){
- if($this->admin['type'] == Admin::ADMIN_TYPE_SUPPLIER){
- $this->supplier_id = $this->admin['type_id'];
- }
- }
- if(empty($this->supplier_id)){
- throw new \Exception('供货商不能为空');
- }
- $type_id = intval($this->supplier_id);
- }
- if($type == AgentFrontBind::TYPE_STORE){
- if(empty($this->store_id)){
- throw new \Exception('店铺不能为空');
- }
- $type_id = intval($this->store_id);
- }
- $hasWhere = [
- 'front_agent_admin_id' => $this->front_agent_admin_id,
- 'type_id' => $type_id,
- 'type' => $type,
- 'is_delete' => 0,
- // 'status' => 1,
- ];
- //需求需要一个店铺绑定多个仓库
- // if($type == 1){
- // unset($hasWhere['front_agent_admin_id']);
- // }
- if($id){
- $hasWhere = ['and', $hasWhere, ['!=', 'id', $id]];
- }
- $has = AgentFrontBind::find()->where($hasWhere)->one();
- if($has){
- //需求需要一个店铺绑定多个仓库
- // if($type == 1){
- // $has->front_agent_admin_id = $this->front_agent_admin_id;
- // $save = $has->save();
- // if(!$save){
- // throw new \Exception('操作失败,' . array_shift($has->getFirstErrors()));
- // }
- // return [
- // 'code' => 0,
- // 'msg' => '操作成功',
- // ];
- // }
- return [
- 'code' => 0,
- 'msg' => '操作成功,无需重复绑定,之前已经绑定过',
- ];
- }
- $model = $id ? AgentFrontBind::findOne($id) : new AgentFrontBind();
- $model->front_agent_admin_id = $this->front_agent_admin_id;
- $model->type_id = $type_id;
- $model->type = $type;
- $model->status = intval($status);
- $model->is_cloud_delivery = intval($is_cloud_delivery);
- if($type == 1){
- $model->status = 1;
- }
- $save = $model->save();
- if(!$save){
- throw new \Exception('操作失败,' . array_shift($model->getFirstErrors()));
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功',
- ];
- }catch(\Exception $e){
- \Yii::error([__METHOD__, $e]);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- //前置仓绑定关系店铺门店 一个仓库可以绑定多个门店
- public function agentFrontBindStoreMd() {
- try{
- $id = $this->id;
- $store_id = $this->store_id;
- $front_agent_admin_id = $this->front_agent_admin_id;
- $md_id = $this->md_id;
- if(empty($front_agent_admin_id)){
- throw new \Exception('未选择绑定仓库');
- }
- if(empty($this->md_id)){
- throw new \Exception('门店不能为空');
- }
- $hasWhere = [
- 'type' => AgentFrontBind::TYPE_STORE,
- 'is_delete' => 0,
- 'md_id' => $md_id
- ];
- if($id){
- $hasWhere = ['and', $hasWhere, ['!=', 'id', $id]];
- }
- $has = AgentFrontBind::find()->where($hasWhere)->one();
- if($has){
- return [
- 'code' => 1,
- 'msg' => '无需重复绑定,之前已经绑定过',
- ];
- }
- $store_bind_model = AgentFrontBind::findOne([
- 'type' => AgentFrontBind::TYPE_STORE,
- 'type_id' => $store_id,
- 'status' => 1,
- 'front_agent_admin_id' => $front_agent_admin_id,
- 'is_delete' => 0
- ]);
- if (!$store_bind_model) {
- throw new \Exception('所选店铺未与所选仓库绑定');
- }
- $mdInfo = Md::findOne(['id' => $md_id, 'is_delete' => 0, 'store_id' => $store_bind_model->type_id]);
- if (!$mdInfo) {
- throw new \Exception('门店信息查询失败');
- }
- $model = AgentFrontBind::findOne([
- 'type' => AgentFrontBind::TYPE_STORE,
- 'type_id' => $store_id,
- 'status' => 1,
- 'front_agent_admin_id' => $front_agent_admin_id,
- 'md_id' => $md_id,
- ]);
- if (!$model) {
- $model = new AgentFrontBind();
- $model->md_id = $md_id;
- $model->type = AgentFrontBind::TYPE_STORE;
- $model->type_id = $store_id;
- $model->front_agent_admin_id = $front_agent_admin_id;
- }
- $model->status = 1;
- $model->is_delete = 0;
- if(!$model->save()){
- throw new \Exception('操作失败,' . json_encode($model->errors, JSON_UNESCAPED_UNICODE));
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功',
- ];
- }catch(\Exception $e){
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- public function bindStatus ($ids, $status)
- {
- try {
- if ($ids) {
- is_string($ids) && $ids = explode(',', $ids);
- if (in_array($status, [0, 1, 2])) {
- AgentFrontBind::updateAll(['status' => $status], ['and', ['in', 'id', $ids], ['is_delete' => 0]]);
- }
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功!'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function bindDel ($ids)
- {
- try {
- if ($ids) {
- is_string($ids) && $ids = explode(',', $ids);
- AgentFrontBind::updateAll(['is_delete' => 1], ['and', ['in', 'id', $ids], ['is_delete' => 0]]);
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功!'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //仓库对应的仓库订单
- public function agentFrontDeliveryOrder() {
- $front_agent_admin_id = $this->front_agent_admin_id;
- $supplier_id = $this->supplier_id;
- $order_no = $this->order_no;
- $supplier_name = $this->supplier_name;
- $store_name = $this->store_name;
- $front_agent_name = $this->front_agent_name;
- $cloud_supplier_id = 0;
- if (isset($supplier_id)) {
- $supplier = Supplier::findOne($supplier_id);
- if (!$supplier) {
- return [
- 'code' => 1,
- 'msg' => '供货商信息不存在'
- ];
- }
- $cloud_supplier_id = $supplier->cloud_supplier_id;
- }
- $query = FrontDeliveryOrder::find()->alias('fdo')
- ->leftJoin(['di' => DeliveryInfo::tableName()], 'fdo.delivery_info_id = di.id')
- ->leftJoin(['o' => Order::tableName()], 'di.order_no = o.order_no')
- ->leftJoin(['sp' => Supplier::tableName()], 'fdo.cloud_supplier_id = sp.cloud_supplier_id AND sp.is_delete = 0')
- ->leftJoin(['s' => Store::tableName()], 'fdo.store_id = s.id')
- ->leftJoin(['a' => Admin::tableName()], 'fdo.front_agent_admin_id = a.id')
- ->where(['di.is_delete' => 0, 'di.is_store_delivery_type' => 0, 'di.is_front_delivery' => 1]);
- if ($cloud_supplier_id) {
- $query->andWhere(['fdo.cloud_supplier_id' => $cloud_supplier_id]);
- }
- if (isset($front_agent_admin_id)) {
- $query->andWhere(['fdo.front_agent_admin_id' => $front_agent_admin_id]);
- }
- if (trim($order_no)) {
- $query->andWhere(['LIKE', 'o.order_no', $order_no]);
- }
- if (trim($supplier_name)) {
- $query->andWhere(['LIKE', 'sp.supplier_name', $supplier_name]);
- }
- if (trim($store_name)) {
- $query->andWhere(['LIKE', 's.name', $store_name]);
- }
- if (trim($front_agent_name)) {
- $query->andWhere(['LIKE', 'a.name', $front_agent_name]);
- }
- $query->select('fdo.id, di.order_no, fdo.front_agent_admin_id, fdo.cloud_supplier_id, di.local_status,
- di.rider_id, fdo.store_id, o.created_at, o.is_offline, o.user_id, o.name, o.mobile, o.address, o.md_id, fdo.cloud_order_id');
- $query->orderBy('fdo.id DESC');
- // var_dump($query->createCommand()->getRawSql());die;
- //分页
- $pagination = pagination_make($query);
- $list = $pagination['list'];
- foreach ($list as $index => &$item) {
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- $item['local_status'] = intval($item['local_status']);
- $item['local_status_name'] = DeliveryInfo::LOCAL_STATUS_ARR[$item['local_status']];
- //仓库信息
- $admin = Admin::findOne($item['front_agent_admin_id']);
- if ($admin) {
- [$province, $city, $district] = District::find()->where(['id' => [$admin->province_id, $admin->city_id, $admin->district_id]])
- ->select('name')->column();
- }
- $item['front_agent_info'] = [
- 'name' => $admin->name ?? '',
- 'address' => ($province ?? '') . ($city ?? '') . ($district ?? '') . $admin->address ?? '',
- 'mobile' => $admin->mobile ?? '',
- 'latitude' => $admin->lat ?? 0,
- 'longitude' => $admin->lng ?? 0,
- ];
- //商城信息
- $store = Store::findOne($item['store_id']);
- $item['store_info'] = [
- 'name' => $store->name ?? '',
- 'logo' => $store->logo ?? ''
- ];
- //派送地址信息
- $user = User::findOne($item['user_id']);
- $item['user_address'] = [
- 'name' => $item['name'] ?? '',
- 'mobile' => $item['mobile'] ?: $user->binding,
- 'address' => $item['address']
- ];
- if (intval($item['is_offline'])) {
- $md = Md::findOne($item['md_id']);
- $item['user_address']['address'] = $md->address ?? '';
- }
- //供货商信息
- $supplier = Supplier::findOne(['cloud_supplier_id' => $item['cloud_supplier_id'], 'is_delete' => 0]);
- $item['supplier_info'] = [
- 'name' => $supplier->supplier_name ?? '',
- 'logo' => $supplier->logo ?? '',
- 'mobile' => $supplier->phone ?? '',
- ];
- //骑手信息
- $courier = LocalDeliveryCourier::findOne(['id' => $item['rider_id'], 'is_delete' => 0]);
- $item['rider_info'] = [
- 'name' => $courier->real_name ?? '',
- 'logo' => $courier->avatar ?? '',
- 'mobile' => $courier->mobile ?? '',
- ];
- //商品信息
- $item['goods_info'] = AgentFrontErpInventoryLog::find()->alias('ael')
- ->leftJoin(['ei' => AgentFrontErpInventory::tableName()], 'ael.inventory_id = ei.id')
- ->leftJoin(['g' => AgentFrontErpCloudGoods::tableName()], 'g.cloud_goods_id = ei.goods_id')
- ->where(['ael.type' => AgentFrontErpInventoryLog::LOG_TYPE_ORDER, 'ael.order_id' => $item['cloud_order_id']])
- ->groupBy('ael.id')->select('ael.num, g.goods_name, g.cover_pic, ei.attr_info')
- ->asArray()->all();
- foreach ($item['goods_info'] as &$goods_item) {
- $goods_item['num'] = abs($goods_item['num']);
- $goods_item['attr_info'] = json_decode($goods_item['attr_info'] , true);
- }
- unset($list[$index]['name'], $list[$index]['mobile'], $list[$index]['address']);
- }
- return [
- 'code' => 0,
- 'msg' => '',
- 'data' => [
- 'data' => $list,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- ]
- ];
- }
- /**
- * 仓库对应的仓库快递订单
- */
- public function agentFrontExpressOrder() {
- try {
- $front_agent_admin_id = $this->front_agent_admin_id;
- $supplier_id = $this->supplier_id;
- $order_no = $this->order_no;
- $supplier_name = $this->supplier_name;
- $store_name = $this->store_name;
- $front_agent_name = $this->front_agent_name;
- $cloud_supplier_id = 0;
- if (isset($supplier_id)) {
- $supplier = Supplier::findOne($supplier_id);
- if (!$supplier) {
- return [
- 'code' => 1,
- 'msg' => '供货商信息不存在'
- ];
- }
- $cloud_supplier_id = $supplier->cloud_supplier_id;
- }
- $query = FrontExpressOrder::find()->alias('feo')
- ->leftJoin(['o' => Order::tableName()], 'feo.order_id = o.id')
- ->leftJoin(['sp' => Supplier::tableName()], 'feo.cloud_supplier_id = sp.cloud_supplier_id AND sp.is_delete = 0')
- ->leftJoin(['s' => Store::tableName()], 'feo.store_id = s.id')
- ->leftJoin(['a' => Admin::tableName()], 'feo.front_agent_admin_id = a.id');
- if ($cloud_supplier_id) {
- $query->andWhere(['feo.cloud_supplier_id' => $cloud_supplier_id]);
- }
- if (isset($front_agent_admin_id)) {
- $query->andWhere(['feo.front_agent_admin_id' => $front_agent_admin_id]);
- }
- if (trim($order_no)) {
- $query->andWhere(['LIKE', 'o.order_no', $order_no]);
- }
- if (trim($supplier_name)) {
- $query->andWhere(['LIKE', 'sp.supplier_name', $supplier_name]);
- }
- if (trim($store_name)) {
- $query->andWhere(['LIKE', 's.name', $store_name]);
- }
- if (trim($front_agent_name)) {
- $query->andWhere(['LIKE', 'a.name', $front_agent_name]);
- }
- $query->select('feo.id, o.order_no, feo.front_agent_admin_id, feo.cloud_supplier_id, feo.store_id,
- o.created_at, o.is_offline, o.user_id, o.name, o.mobile, o.address, o.md_id, feo.cloud_order_id, o.trade_status');
- $query->orderBy('feo.id DESC');
- //分页
- $pagination = pagination_make($query);
- $list = $pagination['list'];
- foreach ($list as $index => &$item) {
- $item['trade_status'] = intval($item['trade_status']);
- $item['trade_status_text'] = Order::TRADE_STATUS_TEXT[$item['trade_status']];
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- //仓库信息
- $admin = Admin::findOne($item['front_agent_admin_id']);
- if ($admin) {
- [$province, $city, $district] = District::find()->where(['id' => [$admin->province_id, $admin->city_id, $admin->district_id]])
- ->select('name')->column();
- }
- $item['front_agent_info'] = [
- 'name' => $admin->name ?? '',
- 'address' => ($province ?? '') . ($city ?? '') . ($district ?? '') . $admin->address ?? '',
- 'mobile' => $admin->mobile ?? '',
- 'latitude' => $admin->lat ?? 0,
- 'longitude' => $admin->lng ?? 0,
- ];
- //商城信息
- $store = Store::findOne($item['store_id']);
- $item['store_info'] = [
- 'name' => $store->name ?? '',
- 'logo' => $store->logo ?? ''
- ];
- //派送地址信息
- $user = User::findOne($item['user_id']);
- $item['user_address'] = [
- 'name' => $item['name'] ?? '',
- 'mobile' => $item['mobile'] ?: $user->binding,
- 'address' => $item['address']
- ];
- if (intval($item['is_offline'])) {
- $md = Md::findOne($item['md_id']);
- $item['user_address']['address'] = $md->address ?? '';
- }
- //供货商信息
- $supplier = Supplier::findOne(['cloud_supplier_id' => $item['cloud_supplier_id'], 'is_delete' => 0]);
- $item['supplier_info'] = [
- 'name' => $supplier->supplier_name ?? '',
- 'logo' => $supplier->logo ?? '',
- 'mobile' => $supplier->phone ?? '',
- ];
- //商品信息
- $item['goods_info'] = AgentFrontErpInventoryLog::find()->alias('ael')
- ->leftJoin(['ei' => AgentFrontErpInventory::tableName()], 'ael.inventory_id = ei.id')
- ->leftJoin(['g' => AgentFrontErpCloudGoods::tableName()], 'g.cloud_goods_id = ei.goods_id')
- ->where(['ael.type' => AgentFrontErpInventoryLog::LOG_TYPE_ORDER, 'ael.order_id' => $item['cloud_order_id']])->andWhere(['<=', 'ael.num', 0])
- ->groupBy('ael.id')->select('ael.num, g.goods_name, g.cover_pic, ei.attr_info')
- ->asArray()->all();
- foreach ($item['goods_info'] as &$goods_item) {
- $goods_item['num'] = abs($goods_item['num']);
- $goods_item['attr_info'] = json_decode($goods_item['attr_info'] , true);
- }
- unset($list[$index]['name'], $list[$index]['mobile'], $list[$index]['address']);
- }
- $f = new ExportList();
- $exportList = $f->getList();
- return [
- 'code' => 0,
- 'msg' => '',
- 'data' => [
- 'data' => $list,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- 'express_list' => Express::getExpressList(),
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 仓库发货仓库快递订单
- */
- public function agentFrontExpressSendOrder() {
- try {
- $front_agent_admin_id = $this->front_agent_admin_id;
- $frontExpressOrder = FrontExpressOrder::findOne([
- 'cloud_order_id' => $this->order_id,
- 'front_agent_admin_id' => $front_agent_admin_id
- ]);
- if (!$frontExpressOrder) {
- throw new \Exception('订单不存在');
- }
- $order = Order::findOne($frontExpressOrder->order_id);
- if (!$order) {
- throw new \Exception('商城订单不存在');
- }
- if (intval($order->trade_status)) {
- throw new \Exception('订单已发货');
- }
- // $supplier_token = get_supplier_token($frontExpressOrder->cloud_supplier_id);
- $cloud_order_id = (string)$frontExpressOrder->cloud_order_id;
- $express_name = $this->express_name;
- $express_no = $this->express_no;
- $zip_code = $this->zip_code;
- $words = $this->words;
- if (empty(trim($express_name))) {
- throw new \Exception('快递名称不能为空');
- }
- if (empty(trim($express_no))) {
- throw new \Exception('快递单号不能为空');
- }
- //云仓订单发货 云仓自动调用商城发货接口
- $supplierForm = new SupplierForm();
- $supplierForm->order_id = $cloud_order_id;
- $supplierForm->express_name = $express_name;
- $supplierForm->express_no = $express_no;
- $supplierForm->send_type = 1;
- $supplierForm->words = $words;
- $supplierForm->zip_code = $zip_code;
- return $supplierForm->supplierPurchaseSend($frontExpressOrder->cloud_supplier_id);
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|