AgentFrontForm.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\AgentFrontErpCloudGoods;
  9. use app\models\AgentFrontErpInventory;
  10. use app\models\AgentFrontErpInventoryLog;
  11. use app\models\DeliveryInfo;
  12. use app\models\District;
  13. use app\models\Express;
  14. use app\models\FrontDeliveryOrder;
  15. use app\models\FrontExpressOrder;
  16. use app\models\LocalDeliveryCourier;
  17. use app\models\Md;
  18. use app\models\Order;
  19. use app\models\User;
  20. use app\utils\ExportList;
  21. use \yii\db\Query;
  22. use yii\base\Model;
  23. use app\models\Store;
  24. use app\models\Admin;
  25. use app\models\SaasUser;
  26. use app\models\AgentFrontBind;
  27. use app\models\Supplier;
  28. use app\models\AgentBind;
  29. class AgentFrontForm extends Model
  30. {
  31. public $page = 1;
  32. public $limit = 10;
  33. public $id;
  34. public $line_name;
  35. public $name;
  36. public $tel;
  37. public $car_no;
  38. public $admin;
  39. public $admin_id;
  40. public $front_agent_admin_name;
  41. public $front_agent_admin_id;
  42. public $is_cloud_delivery;
  43. public $status;
  44. public $store_id;
  45. public $md_id;
  46. public $store_name;
  47. public $store_phone;
  48. public $supplier_id;
  49. public $supplier_name;
  50. public $supplier_phone;
  51. public $order_id;
  52. public $express_name;
  53. public $express_no;
  54. public $send_type;
  55. public $words;
  56. public $zip_code;
  57. public $order_no;
  58. public $front_agent_name;
  59. public $type;
  60. public $md_name;
  61. public function rules()
  62. {
  63. return [
  64. [['md_id', 'store_id', 'id', 'type', 'order_id', 'send_type', 'is_cloud_delivery'], 'integer'],
  65. [['order_no', 'front_agent_name', 'store_name', 'supplier_name', 'md_name', 'express_name', 'express_no', 'words', 'zip_code'], 'string']
  66. ];
  67. }
  68. //前置仓列表
  69. public function list($getNameList = 0) {
  70. $query = Admin::find()->where(['type' => Admin::ADMIN_TYPE_FRONT_AGENT, 'is_delete' => 0]);
  71. if($this->admin['username'] != 'admin' && $this->admin['type'] != Admin::ADMIN_TYPE_SUPPLIER && $this->admin['type'] != Admin::ADMIN_TYPE_SAAS_STAFF){
  72. $query1 = (new Query())->select('admin_id')->from(AgentBind::tableName())->where(['type' => 2, 'parent_admin_id' => $this->admin_id]);
  73. $query->andWhere(['id' => $query1]);
  74. unset($query1);
  75. }
  76. if($this->name){
  77. $query->andWhere(['like', 'name', $this->name]);
  78. }
  79. $query->orderBy('id desc');
  80. if($getNameList){
  81. return [
  82. 'code' => 0,
  83. 'data' => $query->select(['id', 'name'])->all(),
  84. // 'sql' => $query->createCommand()->getRawSql(),
  85. ];
  86. }
  87. $res = pagination_make($query);
  88. foreach ($res['list'] as &$item) {
  89. $district = District::find()->where(['id' => [$item['province_id'], $item['city_id'], $item['district_id']]])->select('name')->column();
  90. $district = implode('', $district);
  91. $item['address'] = $district . $item['address'];
  92. unset($item['password']);
  93. $item['supplier_count'] = AgentFrontBind::find()->where(['is_delete' => 0, 'type' => 0, 'status' => 1, 'front_agent_admin_id' => $item['id']])->count();
  94. }
  95. return [
  96. 'code' => 0,
  97. 'msg' => 'success',
  98. 'data' => $res,
  99. // 'sql' => $query->createCommand()->getRawSql(),
  100. ];
  101. }
  102. //前置仓绑定关系(商家)列表
  103. public function bindStoreList() {
  104. $agentId = $this->admin['id'];
  105. if($this->admin['username'] == 'admin'){
  106. $agentId = null;
  107. }
  108. if($this->admin['type'] == Admin::ADMIN_TYPE_FRONT_AGENT){
  109. $agentId = null;
  110. $this->front_agent_admin_id = $this->admin['id'];
  111. }
  112. $query = AgentFrontBind::find()->where(['type' => 1, 'is_delete' => 0]);
  113. if($this->status >= 0){
  114. $query->andWhere(['status' => $this->status]);
  115. }
  116. if($this->store_id > 0){
  117. $query->andWhere(['type_id' => $this->store_id]);
  118. }
  119. if($this->admin['type'] == Admin::ADMIN_TYPE_STORE){
  120. $query->andWhere(['type_id' => $this->admin['type_id']]);
  121. }
  122. if($this->type == 1){
  123. $query->andWhere(['>', 'md_id', 0]);
  124. }
  125. if($this->front_agent_admin_id){
  126. $front_agent_admin_id = intval($this->front_agent_admin_id);
  127. }else{
  128. if($this->admin['username'] != 'admin' && $this->admin['type'] != Admin::ADMIN_TYPE_STORE){
  129. $front_agent_admin_id = (new Query())->select('admin_id')->from(AgentBind::tableName())->where(['type' => 2, 'parent_admin_id' => $this->admin_id]);
  130. }
  131. }
  132. isset($front_agent_admin_id) && $query->andWhere(['front_agent_admin_id' => $front_agent_admin_id]);
  133. if($this->front_agent_admin_name){
  134. $front_agent_admin_name = (new Query())->select('id')->from(Admin::tableName())
  135. ->where(['type' => Admin::ADMIN_TYPE_FRONT_AGENT])
  136. ->andWhere(['like', 'name', $this->front_agent_admin_name]);
  137. $query->andWhere(['front_agent_admin_id' => $front_agent_admin_name]);
  138. }
  139. $query1 = (new Query())->select('id')->from(Store::tableName());
  140. $query1->andWhere(['is_delete' => 0]);
  141. if($this->store_name){
  142. $query1->andWhere(['like', 'name', $this->store_name]);
  143. }
  144. if($query1->where){
  145. $query->andWhere(['type_id' => $query1]);
  146. }
  147. unset($query1);
  148. // $query1 = (new Query())->select('id')->from(Md::tableName());
  149. // $query1->andWhere(['is_delete' => 0]);
  150. // if($this->md_name){
  151. // $query1->andWhere(['like', 'name', $this->md_name]);
  152. // }
  153. // if($query1->where){
  154. // $query->andWhere(['md_id' => $query1]);
  155. // }
  156. // unset($query1);
  157. $query->orderBy('id desc');
  158. $res = pagination_make($query);
  159. foreach ($res['list'] as &$item) {
  160. $item['store_info'] = Store::find()->where(['id' => $item['type_id']])->asArray()->one();
  161. $item['front_agent'] = Admin::findOne($item['front_agent_admin_id']);
  162. $item['md_info'] = Md::find()->where(['id' => $item['md_id']])->select('id, name, cover_url, mobile')->asArray()->one();
  163. // if($this->type == 1){
  164. // //做兼容前端 没法区分修改还是添加
  165. // $item['front_agent_admin_id'] = $item['id'];
  166. // }
  167. $item['is_cloud_delivery'] = intval($item['is_cloud_delivery']);
  168. }
  169. return [
  170. 'code' => 0,
  171. 'msg' => 'success',
  172. 'data' => $res,
  173. // 'sql' => $query->createCommand()->getRawSql(),
  174. ];
  175. }
  176. //前置仓绑定关系(供货商)列表
  177. public function bindList() {
  178. if($this->admin['type'] == Admin::ADMIN_TYPE_FRONT_AGENT){
  179. $this->front_agent_admin_id = $this->admin['id'];
  180. }
  181. $query = AgentFrontBind::find()->where(['type' => 0, 'is_delete' => 0]);
  182. if($this->status >= 0){
  183. $query->andWhere(['status' => $this->status]);
  184. }
  185. if($this->supplier_id > 0){
  186. $query->andWhere(['type_id' => $this->supplier_id]);
  187. }
  188. if($this->store_id > 0){
  189. $query->andWhere(['type_id' => $this->store_id]);
  190. }
  191. if($this->admin['type'] == Admin::ADMIN_TYPE_SUPPLIER){
  192. $query->andWhere(['type_id' => $this->admin['type_id']]);
  193. }
  194. if($this->front_agent_admin_id){
  195. $front_agent_admin_id = intval($this->front_agent_admin_id);
  196. }else{
  197. if($this->admin['username'] != 'admin' && $this->admin['type'] != Admin::ADMIN_TYPE_SUPPLIER){
  198. $front_agent_admin_id = (new Query())->select('admin_id')->from(AgentBind::tableName())->where(['type' => 2, 'parent_admin_id' => $this->admin_id]);
  199. }
  200. }
  201. isset($front_agent_admin_id) && $query->andWhere(['front_agent_admin_id' => $front_agent_admin_id]);
  202. if($this->front_agent_admin_name){
  203. $front_agent_admin_name = (new Query())->select('id')->from(Admin::tableName())
  204. ->where(['type' => Admin::ADMIN_TYPE_FRONT_AGENT])
  205. ->andWhere(['like', 'name', $this->front_agent_admin_name]);
  206. $query->andWhere(['front_agent_admin_id' => $front_agent_admin_name]);
  207. }
  208. $query1 = (new Query())->select('id')->from(Supplier::tableName());
  209. if($this->supplier_name){
  210. $query1->andWhere(['like', 'supplier_name', $this->supplier_name]);
  211. }
  212. if($this->supplier_phone){
  213. $query1->andWhere(['like', 'phone', $this->supplier_phone]);
  214. }
  215. if($query1->where){
  216. $query->andWhere(['type_id' => $query1]);
  217. }
  218. unset($query1);
  219. $query->orderBy('id desc');
  220. $res = pagination_make($query);
  221. foreach ($res['list'] as &$item) {
  222. $item['supplier_info'] = Supplier::findOne($item['type_id']);
  223. $item['front_agent'] = Admin::findOne($item['front_agent_admin_id']);
  224. }
  225. return [
  226. 'code' => 0,
  227. 'msg' => 'success',
  228. 'data' => $res,
  229. // 'sql' => $query->createCommand()->getRawSql(),
  230. ];
  231. }
  232. //前置仓绑定关系(供货商/商家) type = 1店铺 0供货商
  233. public function bind($id = 0, $status = 0, $type = AgentFrontBind::TYPE_SUPPLIER) {
  234. try{
  235. if(empty($this->front_agent_admin_id)){
  236. throw new \Exception('前置仓不能为空');
  237. }
  238. $is_cloud_delivery = $this->is_cloud_delivery;
  239. $type_id = 0;
  240. if($type == AgentFrontBind::TYPE_SUPPLIER){
  241. //供货商后台操作
  242. if(empty($this->supplier_id)){
  243. if($this->admin['type'] == Admin::ADMIN_TYPE_SUPPLIER){
  244. $this->supplier_id = $this->admin['type_id'];
  245. }
  246. }
  247. if(empty($this->supplier_id)){
  248. throw new \Exception('供货商不能为空');
  249. }
  250. $type_id = intval($this->supplier_id);
  251. }
  252. if($type == AgentFrontBind::TYPE_STORE){
  253. if(empty($this->store_id)){
  254. throw new \Exception('店铺不能为空');
  255. }
  256. $type_id = intval($this->store_id);
  257. }
  258. $hasWhere = [
  259. 'front_agent_admin_id' => $this->front_agent_admin_id,
  260. 'type_id' => $type_id,
  261. 'type' => $type,
  262. 'is_delete' => 0,
  263. // 'status' => 1,
  264. ];
  265. //需求需要一个店铺绑定多个仓库
  266. // if($type == 1){
  267. // unset($hasWhere['front_agent_admin_id']);
  268. // }
  269. if($id){
  270. $hasWhere = ['and', $hasWhere, ['!=', 'id', $id]];
  271. }
  272. $has = AgentFrontBind::find()->where($hasWhere)->one();
  273. if($has){
  274. //需求需要一个店铺绑定多个仓库
  275. // if($type == 1){
  276. // $has->front_agent_admin_id = $this->front_agent_admin_id;
  277. // $save = $has->save();
  278. // if(!$save){
  279. // throw new \Exception('操作失败,' . array_shift($has->getFirstErrors()));
  280. // }
  281. // return [
  282. // 'code' => 0,
  283. // 'msg' => '操作成功',
  284. // ];
  285. // }
  286. return [
  287. 'code' => 0,
  288. 'msg' => '操作成功,无需重复绑定,之前已经绑定过',
  289. ];
  290. }
  291. $model = $id ? AgentFrontBind::findOne($id) : new AgentFrontBind();
  292. $model->front_agent_admin_id = $this->front_agent_admin_id;
  293. $model->type_id = $type_id;
  294. $model->type = $type;
  295. $model->status = intval($status);
  296. $model->is_cloud_delivery = intval($is_cloud_delivery);
  297. if($type == 1){
  298. $model->status = 1;
  299. }
  300. $save = $model->save();
  301. if(!$save){
  302. throw new \Exception('操作失败,' . array_shift($model->getFirstErrors()));
  303. }
  304. return [
  305. 'code' => 0,
  306. 'msg' => '操作成功',
  307. ];
  308. }catch(\Exception $e){
  309. \Yii::error([__METHOD__, $e]);
  310. return [
  311. 'code' => 1,
  312. 'msg' => $e->getMessage(),
  313. ];
  314. }
  315. }
  316. //前置仓绑定关系店铺门店 一个仓库可以绑定多个门店
  317. public function agentFrontBindStoreMd() {
  318. try{
  319. $id = $this->id;
  320. $store_id = $this->store_id;
  321. $front_agent_admin_id = $this->front_agent_admin_id;
  322. $md_id = $this->md_id;
  323. if(empty($front_agent_admin_id)){
  324. throw new \Exception('未选择绑定仓库');
  325. }
  326. if(empty($this->md_id)){
  327. throw new \Exception('门店不能为空');
  328. }
  329. $hasWhere = [
  330. 'type' => AgentFrontBind::TYPE_STORE,
  331. 'is_delete' => 0,
  332. 'md_id' => $md_id
  333. ];
  334. if($id){
  335. $hasWhere = ['and', $hasWhere, ['!=', 'id', $id]];
  336. }
  337. $has = AgentFrontBind::find()->where($hasWhere)->one();
  338. if($has){
  339. return [
  340. 'code' => 1,
  341. 'msg' => '无需重复绑定,之前已经绑定过',
  342. ];
  343. }
  344. $store_bind_model = AgentFrontBind::findOne([
  345. 'type' => AgentFrontBind::TYPE_STORE,
  346. 'type_id' => $store_id,
  347. 'status' => 1,
  348. 'front_agent_admin_id' => $front_agent_admin_id,
  349. 'is_delete' => 0
  350. ]);
  351. if (!$store_bind_model) {
  352. throw new \Exception('所选店铺未与所选仓库绑定');
  353. }
  354. $mdInfo = Md::findOne(['id' => $md_id, 'is_delete' => 0, 'store_id' => $store_bind_model->type_id]);
  355. if (!$mdInfo) {
  356. throw new \Exception('门店信息查询失败');
  357. }
  358. $model = AgentFrontBind::findOne([
  359. 'type' => AgentFrontBind::TYPE_STORE,
  360. 'type_id' => $store_id,
  361. 'status' => 1,
  362. 'front_agent_admin_id' => $front_agent_admin_id,
  363. 'md_id' => $md_id,
  364. ]);
  365. if (!$model) {
  366. $model = new AgentFrontBind();
  367. $model->md_id = $md_id;
  368. $model->type = AgentFrontBind::TYPE_STORE;
  369. $model->type_id = $store_id;
  370. $model->front_agent_admin_id = $front_agent_admin_id;
  371. }
  372. $model->status = 1;
  373. $model->is_delete = 0;
  374. if(!$model->save()){
  375. throw new \Exception('操作失败,' . json_encode($model->errors, JSON_UNESCAPED_UNICODE));
  376. }
  377. return [
  378. 'code' => 0,
  379. 'msg' => '操作成功',
  380. ];
  381. }catch(\Exception $e){
  382. return [
  383. 'code' => 1,
  384. 'msg' => $e->getMessage(),
  385. ];
  386. }
  387. }
  388. public function bindStatus ($ids, $status)
  389. {
  390. try {
  391. if ($ids) {
  392. is_string($ids) && $ids = explode(',', $ids);
  393. if (in_array($status, [0, 1, 2])) {
  394. AgentFrontBind::updateAll(['status' => $status], ['and', ['in', 'id', $ids], ['is_delete' => 0]]);
  395. }
  396. }
  397. return [
  398. 'code' => 0,
  399. 'msg' => '操作成功!'
  400. ];
  401. } catch (\Exception $e) {
  402. return [
  403. 'code' => 1,
  404. 'msg' => $e->getMessage()
  405. ];
  406. }
  407. }
  408. public function bindDel ($ids)
  409. {
  410. try {
  411. if ($ids) {
  412. is_string($ids) && $ids = explode(',', $ids);
  413. AgentFrontBind::updateAll(['is_delete' => 1], ['and', ['in', 'id', $ids], ['is_delete' => 0]]);
  414. }
  415. return [
  416. 'code' => 0,
  417. 'msg' => '操作成功!'
  418. ];
  419. } catch (\Exception $e) {
  420. return [
  421. 'code' => 1,
  422. 'msg' => $e->getMessage()
  423. ];
  424. }
  425. }
  426. //仓库对应的仓库订单
  427. public function agentFrontDeliveryOrder() {
  428. $front_agent_admin_id = $this->front_agent_admin_id;
  429. $supplier_id = $this->supplier_id;
  430. $order_no = $this->order_no;
  431. $supplier_name = $this->supplier_name;
  432. $store_name = $this->store_name;
  433. $front_agent_name = $this->front_agent_name;
  434. $cloud_supplier_id = 0;
  435. if (isset($supplier_id)) {
  436. $supplier = Supplier::findOne($supplier_id);
  437. if (!$supplier) {
  438. return [
  439. 'code' => 1,
  440. 'msg' => '供货商信息不存在'
  441. ];
  442. }
  443. $cloud_supplier_id = $supplier->cloud_supplier_id;
  444. }
  445. $query = FrontDeliveryOrder::find()->alias('fdo')
  446. ->leftJoin(['di' => DeliveryInfo::tableName()], 'fdo.delivery_info_id = di.id')
  447. ->leftJoin(['o' => Order::tableName()], 'di.order_no = o.order_no')
  448. ->leftJoin(['sp' => Supplier::tableName()], 'fdo.cloud_supplier_id = sp.cloud_supplier_id AND sp.is_delete = 0')
  449. ->leftJoin(['s' => Store::tableName()], 'fdo.store_id = s.id')
  450. ->leftJoin(['a' => Admin::tableName()], 'fdo.front_agent_admin_id = a.id')
  451. ->where(['di.is_delete' => 0, 'di.is_store_delivery_type' => 0, 'di.is_front_delivery' => 1]);
  452. if ($cloud_supplier_id) {
  453. $query->andWhere(['fdo.cloud_supplier_id' => $cloud_supplier_id]);
  454. }
  455. if (isset($front_agent_admin_id)) {
  456. $query->andWhere(['fdo.front_agent_admin_id' => $front_agent_admin_id]);
  457. }
  458. if (trim($order_no)) {
  459. $query->andWhere(['LIKE', 'o.order_no', $order_no]);
  460. }
  461. if (trim($supplier_name)) {
  462. $query->andWhere(['LIKE', 'sp.supplier_name', $supplier_name]);
  463. }
  464. if (trim($store_name)) {
  465. $query->andWhere(['LIKE', 's.name', $store_name]);
  466. }
  467. if (trim($front_agent_name)) {
  468. $query->andWhere(['LIKE', 'a.name', $front_agent_name]);
  469. }
  470. $query->select('fdo.id, di.order_no, fdo.front_agent_admin_id, fdo.cloud_supplier_id, di.local_status,
  471. 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');
  472. $query->orderBy('fdo.id DESC');
  473. // var_dump($query->createCommand()->getRawSql());die;
  474. //分页
  475. $pagination = pagination_make($query);
  476. $list = $pagination['list'];
  477. foreach ($list as $index => &$item) {
  478. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  479. $item['local_status'] = intval($item['local_status']);
  480. $item['local_status_name'] = DeliveryInfo::LOCAL_STATUS_ARR[$item['local_status']];
  481. //仓库信息
  482. $admin = Admin::findOne($item['front_agent_admin_id']);
  483. if ($admin) {
  484. [$province, $city, $district] = District::find()->where(['id' => [$admin->province_id, $admin->city_id, $admin->district_id]])
  485. ->select('name')->column();
  486. }
  487. $item['front_agent_info'] = [
  488. 'name' => $admin->name ?? '',
  489. 'address' => ($province ?? '') . ($city ?? '') . ($district ?? '') . $admin->address ?? '',
  490. 'mobile' => $admin->mobile ?? '',
  491. 'latitude' => $admin->lat ?? 0,
  492. 'longitude' => $admin->lng ?? 0,
  493. ];
  494. //商城信息
  495. $store = Store::findOne($item['store_id']);
  496. $item['store_info'] = [
  497. 'name' => $store->name ?? '',
  498. 'logo' => $store->logo ?? ''
  499. ];
  500. //派送地址信息
  501. $user = User::findOne($item['user_id']);
  502. $item['user_address'] = [
  503. 'name' => $item['name'] ?? '',
  504. 'mobile' => $item['mobile'] ?: $user->binding,
  505. 'address' => $item['address']
  506. ];
  507. if (intval($item['is_offline'])) {
  508. $md = Md::findOne($item['md_id']);
  509. $item['user_address']['address'] = $md->address ?? '';
  510. }
  511. //供货商信息
  512. $supplier = Supplier::findOne(['cloud_supplier_id' => $item['cloud_supplier_id'], 'is_delete' => 0]);
  513. $item['supplier_info'] = [
  514. 'name' => $supplier->supplier_name ?? '',
  515. 'logo' => $supplier->logo ?? '',
  516. 'mobile' => $supplier->phone ?? '',
  517. ];
  518. //骑手信息
  519. $courier = LocalDeliveryCourier::findOne(['id' => $item['rider_id'], 'is_delete' => 0]);
  520. $item['rider_info'] = [
  521. 'name' => $courier->real_name ?? '',
  522. 'logo' => $courier->avatar ?? '',
  523. 'mobile' => $courier->mobile ?? '',
  524. ];
  525. //商品信息
  526. $item['goods_info'] = AgentFrontErpInventoryLog::find()->alias('ael')
  527. ->leftJoin(['ei' => AgentFrontErpInventory::tableName()], 'ael.inventory_id = ei.id')
  528. ->leftJoin(['g' => AgentFrontErpCloudGoods::tableName()], 'g.cloud_goods_id = ei.goods_id')
  529. ->where(['ael.type' => AgentFrontErpInventoryLog::LOG_TYPE_ORDER, 'ael.order_id' => $item['cloud_order_id']])
  530. ->groupBy('ael.id')->select('ael.num, g.goods_name, g.cover_pic, ei.attr_info')
  531. ->asArray()->all();
  532. foreach ($item['goods_info'] as &$goods_item) {
  533. $goods_item['num'] = abs($goods_item['num']);
  534. $goods_item['attr_info'] = json_decode($goods_item['attr_info'] , true);
  535. }
  536. unset($list[$index]['name'], $list[$index]['mobile'], $list[$index]['address']);
  537. }
  538. return [
  539. 'code' => 0,
  540. 'msg' => '',
  541. 'data' => [
  542. 'data' => $list,
  543. 'pageNo' => $pagination['pageNo'],
  544. 'totalCount' => $pagination['totalCount'],
  545. ]
  546. ];
  547. }
  548. /**
  549. * 仓库对应的仓库快递订单
  550. */
  551. public function agentFrontExpressOrder() {
  552. try {
  553. $front_agent_admin_id = $this->front_agent_admin_id;
  554. $supplier_id = $this->supplier_id;
  555. $order_no = $this->order_no;
  556. $supplier_name = $this->supplier_name;
  557. $store_name = $this->store_name;
  558. $front_agent_name = $this->front_agent_name;
  559. $cloud_supplier_id = 0;
  560. if (isset($supplier_id)) {
  561. $supplier = Supplier::findOne($supplier_id);
  562. if (!$supplier) {
  563. return [
  564. 'code' => 1,
  565. 'msg' => '供货商信息不存在'
  566. ];
  567. }
  568. $cloud_supplier_id = $supplier->cloud_supplier_id;
  569. }
  570. $query = FrontExpressOrder::find()->alias('feo')
  571. ->leftJoin(['o' => Order::tableName()], 'feo.order_id = o.id')
  572. ->leftJoin(['sp' => Supplier::tableName()], 'feo.cloud_supplier_id = sp.cloud_supplier_id AND sp.is_delete = 0')
  573. ->leftJoin(['s' => Store::tableName()], 'feo.store_id = s.id')
  574. ->leftJoin(['a' => Admin::tableName()], 'feo.front_agent_admin_id = a.id');
  575. if ($cloud_supplier_id) {
  576. $query->andWhere(['feo.cloud_supplier_id' => $cloud_supplier_id]);
  577. }
  578. if (isset($front_agent_admin_id)) {
  579. $query->andWhere(['feo.front_agent_admin_id' => $front_agent_admin_id]);
  580. }
  581. if (trim($order_no)) {
  582. $query->andWhere(['LIKE', 'o.order_no', $order_no]);
  583. }
  584. if (trim($supplier_name)) {
  585. $query->andWhere(['LIKE', 'sp.supplier_name', $supplier_name]);
  586. }
  587. if (trim($store_name)) {
  588. $query->andWhere(['LIKE', 's.name', $store_name]);
  589. }
  590. if (trim($front_agent_name)) {
  591. $query->andWhere(['LIKE', 'a.name', $front_agent_name]);
  592. }
  593. $query->select('feo.id, o.order_no, feo.front_agent_admin_id, feo.cloud_supplier_id, feo.store_id,
  594. 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');
  595. $query->orderBy('feo.id DESC');
  596. //分页
  597. $pagination = pagination_make($query);
  598. $list = $pagination['list'];
  599. foreach ($list as $index => &$item) {
  600. $item['trade_status'] = intval($item['trade_status']);
  601. $item['trade_status_text'] = Order::TRADE_STATUS_TEXT[$item['trade_status']];
  602. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  603. //仓库信息
  604. $admin = Admin::findOne($item['front_agent_admin_id']);
  605. if ($admin) {
  606. [$province, $city, $district] = District::find()->where(['id' => [$admin->province_id, $admin->city_id, $admin->district_id]])
  607. ->select('name')->column();
  608. }
  609. $item['front_agent_info'] = [
  610. 'name' => $admin->name ?? '',
  611. 'address' => ($province ?? '') . ($city ?? '') . ($district ?? '') . $admin->address ?? '',
  612. 'mobile' => $admin->mobile ?? '',
  613. 'latitude' => $admin->lat ?? 0,
  614. 'longitude' => $admin->lng ?? 0,
  615. ];
  616. //商城信息
  617. $store = Store::findOne($item['store_id']);
  618. $item['store_info'] = [
  619. 'name' => $store->name ?? '',
  620. 'logo' => $store->logo ?? ''
  621. ];
  622. //派送地址信息
  623. $user = User::findOne($item['user_id']);
  624. $item['user_address'] = [
  625. 'name' => $item['name'] ?? '',
  626. 'mobile' => $item['mobile'] ?: $user->binding,
  627. 'address' => $item['address']
  628. ];
  629. if (intval($item['is_offline'])) {
  630. $md = Md::findOne($item['md_id']);
  631. $item['user_address']['address'] = $md->address ?? '';
  632. }
  633. //供货商信息
  634. $supplier = Supplier::findOne(['cloud_supplier_id' => $item['cloud_supplier_id'], 'is_delete' => 0]);
  635. $item['supplier_info'] = [
  636. 'name' => $supplier->supplier_name ?? '',
  637. 'logo' => $supplier->logo ?? '',
  638. 'mobile' => $supplier->phone ?? '',
  639. ];
  640. //商品信息
  641. $item['goods_info'] = AgentFrontErpInventoryLog::find()->alias('ael')
  642. ->leftJoin(['ei' => AgentFrontErpInventory::tableName()], 'ael.inventory_id = ei.id')
  643. ->leftJoin(['g' => AgentFrontErpCloudGoods::tableName()], 'g.cloud_goods_id = ei.goods_id')
  644. ->where(['ael.type' => AgentFrontErpInventoryLog::LOG_TYPE_ORDER, 'ael.order_id' => $item['cloud_order_id']])->andWhere(['<=', 'ael.num', 0])
  645. ->groupBy('ael.id')->select('ael.num, g.goods_name, g.cover_pic, ei.attr_info')
  646. ->asArray()->all();
  647. foreach ($item['goods_info'] as &$goods_item) {
  648. $goods_item['num'] = abs($goods_item['num']);
  649. $goods_item['attr_info'] = json_decode($goods_item['attr_info'] , true);
  650. }
  651. unset($list[$index]['name'], $list[$index]['mobile'], $list[$index]['address']);
  652. }
  653. $f = new ExportList();
  654. $exportList = $f->getList();
  655. return [
  656. 'code' => 0,
  657. 'msg' => '',
  658. 'data' => [
  659. 'data' => $list,
  660. 'pageNo' => $pagination['pageNo'],
  661. 'totalCount' => $pagination['totalCount'],
  662. 'express_list' => Express::getExpressList(),
  663. ]
  664. ];
  665. } catch (\Exception $e) {
  666. return [
  667. 'code' => 1,
  668. 'msg' => $e->getMessage()
  669. ];
  670. }
  671. }
  672. /**
  673. * 仓库发货仓库快递订单
  674. */
  675. public function agentFrontExpressSendOrder() {
  676. try {
  677. $front_agent_admin_id = $this->front_agent_admin_id;
  678. $frontExpressOrder = FrontExpressOrder::findOne([
  679. 'cloud_order_id' => $this->order_id,
  680. 'front_agent_admin_id' => $front_agent_admin_id
  681. ]);
  682. if (!$frontExpressOrder) {
  683. throw new \Exception('订单不存在');
  684. }
  685. $order = Order::findOne($frontExpressOrder->order_id);
  686. if (!$order) {
  687. throw new \Exception('商城订单不存在');
  688. }
  689. if (intval($order->trade_status)) {
  690. throw new \Exception('订单已发货');
  691. }
  692. // $supplier_token = get_supplier_token($frontExpressOrder->cloud_supplier_id);
  693. $cloud_order_id = (string)$frontExpressOrder->cloud_order_id;
  694. $express_name = $this->express_name;
  695. $express_no = $this->express_no;
  696. $zip_code = $this->zip_code;
  697. $words = $this->words;
  698. if (empty(trim($express_name))) {
  699. throw new \Exception('快递名称不能为空');
  700. }
  701. if (empty(trim($express_no))) {
  702. throw new \Exception('快递单号不能为空');
  703. }
  704. //云仓订单发货 云仓自动调用商城发货接口
  705. $supplierForm = new SupplierForm();
  706. $supplierForm->order_id = $cloud_order_id;
  707. $supplierForm->express_name = $express_name;
  708. $supplierForm->express_no = $express_no;
  709. $supplierForm->send_type = 1;
  710. $supplierForm->words = $words;
  711. $supplierForm->zip_code = $zip_code;
  712. return $supplierForm->supplierPurchaseSend($frontExpressOrder->cloud_supplier_id);
  713. } catch (\Exception $e) {
  714. return [
  715. 'code' => 1,
  716. 'msg' => $e->getMessage()
  717. ];
  718. }
  719. }
  720. }