AgentForm.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models\agent;
  8. use app\constants\OptionSetting;
  9. use app\models\Admin;
  10. use app\models\AgentBind;
  11. use app\models\AgentFrontBind;
  12. use app\models\AgentGoodsBindGoods;
  13. use app\models\AgentGoodsInstallLog;
  14. use app\models\District;
  15. use app\models\Goods;
  16. use app\models\Order;
  17. use app\models\OrderDetail;
  18. use app\models\OrderTransit;
  19. use app\models\PurchaseOrder;
  20. use app\models\PurchaseOrderDetail;
  21. use app\models\SaasUser;
  22. use app\models\Store;
  23. use app\models\StoreShareMoney;
  24. use app\models\Supplier;
  25. use app\modules\admin\models\PlatformForm;
  26. use yii\base\Model;
  27. class AgentForm extends Model
  28. {
  29. public $id;
  30. public $type;
  31. public $admin_id;
  32. public $parent_admin_id;
  33. public $page;
  34. public $limit;
  35. public $cloud_goods_id;
  36. public $status;
  37. public $admin_name;
  38. public $goods_name;
  39. public $supplier_id;
  40. public $supplier_name;
  41. public $order_no;
  42. public $store_name;
  43. public $cat_id;
  44. public $start_time;
  45. public $end_time;
  46. public $min_money;
  47. public $max_money;
  48. public function rules()
  49. {
  50. return [
  51. [['id', 'type', 'admin_id', 'parent_admin_id', 'page', 'limit', 'cloud_goods_id', 'status', 'supplier_id', 'cat_id'], 'integer'],
  52. [['admin_name', 'goods_name', 'supplier_name', 'store_name', 'start_time', 'end_time', 'order_no'], 'string'],
  53. [['min_money', 'max_money'], 'number']
  54. ];
  55. }
  56. /**
  57. * 获取前置仓绑定的供货商商品
  58. */
  59. public function frontAgentGoodsList() {
  60. try {
  61. $admin = get_admin();
  62. $goods_name = $this->goods_name;
  63. $cat_id = $this->cat_id;
  64. $supplier_id = $this->supplier_id;
  65. $min_money = $this->min_money;
  66. $max_money = $this->max_money;
  67. if (!in_array($admin->type, ['goods_agent', 'admin'])) {
  68. throw new \Exception('权限错误');
  69. }
  70. //获取商品
  71. $form = new PlatformForm();
  72. $form->page = $this->page;
  73. $form->limit = $this->limit;
  74. $form->send_type = 3;
  75. //商品名称
  76. if (!empty($goods_name)) {
  77. $form->name = $goods_name;
  78. }
  79. //供货商id
  80. if (!empty($supplier_id)) {
  81. $form->supplier_id = $supplier_id;
  82. }
  83. //分类
  84. // if (!empty($cat_id)) {
  85. // $form->cat_id = $cat_id;
  86. // }
  87. //最大金额
  88. if (!empty($min_money)) {
  89. $form->min_money = $min_money;
  90. }
  91. //最大金额
  92. if (!empty($max_money)) {
  93. $form->max_money = $max_money;
  94. }
  95. //如果为区域代理
  96. if ($admin->type === 'admin') {
  97. $result = $this->checkGoodsBind($admin->id);
  98. if ($result['code'] === 0) {
  99. $filter_cloud_goods_id = $result['data'];
  100. } else {
  101. throw new \Exception($result['msg']);
  102. }
  103. //剔除被代理的产品
  104. $form->filter_goods_id = implode(',', $filter_cloud_goods_id);
  105. return $form->goodsList();
  106. } else {
  107. $agent_bind = AgentBind::findOne(['admin_id' => $admin->id]);
  108. $parent_admin_id = $agent_bind->parent_admin_id;
  109. //产品代理
  110. // $result = $this->getSupplier($parent_admin_id);
  111. //
  112. // if ($result['code'] === 0) {
  113. // if (!empty($supplier_id)) {
  114. // $supplier_id = array_intersect($result['data'], [$supplier_id]);
  115. // } else {
  116. // $supplier_id = $result['data'];
  117. // }
  118. //// $supplier_id = $result['data'];
  119. // } else {
  120. // throw new \Exception($result['msg']);
  121. // }
  122. //找到同级产品代理,检测某个商品是否已经被代理
  123. $result = $this->checkGoodsBind($parent_admin_id);
  124. if ($result['code'] === 0) {
  125. $filter_cloud_goods_id = $result['data'];
  126. } else {
  127. throw new \Exception($result['msg']);
  128. }
  129. //获取商品
  130. $form->type = 1;
  131. //剔除被代理的产品
  132. $form->filter_goods_id = implode(',', $filter_cloud_goods_id);
  133. return $form->goodsList();
  134. }
  135. } catch (\Exception $e) {
  136. return [
  137. 'code' => 1,
  138. 'msg' => $e->getMessage()
  139. ];
  140. }
  141. }
  142. /**
  143. * 绑定商品
  144. */
  145. public function bindFrontAgentGoods() {
  146. try {
  147. $admin = get_admin();
  148. //获取商品
  149. $form = new PlatformForm();
  150. if (!$this->cloud_goods_id) {
  151. throw new \Exception('未选择产品');
  152. }
  153. if ($admin->type === 'admin') {
  154. $admin_id = $this->admin_id;
  155. $result = $this->checkGoodsBind($admin->id, $admin_id);
  156. if ($result['code'] === 0) {
  157. if (in_array($this->cloud_goods_id, $result['data'])) {
  158. throw new \Exception('该商品已经绑定其他产品代理');
  159. }
  160. } else {
  161. throw new \Exception($result['msg']);
  162. }
  163. } else {
  164. $admin_id = $admin->id;
  165. $agent_bind = AgentBind::findOne(['admin_id' => $admin->id]);
  166. $parent_admin_id = $agent_bind->parent_admin_id;
  167. // $result = $this->getSupplier($parent_admin_id);
  168. // if ($result['code'] === 0) {
  169. // $supplier_id = $result['data'];
  170. // } else {
  171. // throw new \Exception($result['msg']);
  172. // }
  173. $result = $this->checkGoodsBind($parent_admin_id, $admin_id);
  174. if ($result['code'] === 0) {
  175. if (in_array($this->cloud_goods_id, $result['data'])) {
  176. throw new \Exception('该商品已经绑定其他产品代理');
  177. }
  178. } else {
  179. throw new \Exception($result['msg']);
  180. }
  181. $form->type = 1;
  182. // $form->supplier_id = implode(',', $supplier_id);
  183. }
  184. $form->send_type = 3;
  185. $form->id = $this->cloud_goods_id;
  186. $result = $form->goodsList();
  187. if ($result['code'] !== 0 || empty($result['data']['list'])) {
  188. throw new \Exception('商品查询失败');
  189. }
  190. //绑定产品
  191. if (empty($admin_id)) {
  192. throw new \Exception('未选择产品代理');
  193. }
  194. $agent_goods_bind = AgentGoodsBindGoods::findOne(['goods_agent_admin_id' => $admin_id, 'is_delete' => 0, 'status' => [0, 1], 'cloud_goods_id' => $this->cloud_goods_id]);
  195. if (!$agent_goods_bind) {
  196. $agent_goods_bind = new AgentGoodsBindGoods();
  197. $agent_goods_bind->status = 0;
  198. }
  199. if ($admin->type === 'admin') {
  200. $agent_goods_bind->status = 1;
  201. }
  202. $agent_goods_bind->goods_agent_admin_id = $admin_id;
  203. $agent_goods_bind->cloud_goods_id = $this->cloud_goods_id;
  204. $agent_goods_bind->cloud_supplier_id = $result['data']['list'][0]['supplier_id'];
  205. if (!$agent_goods_bind->save()) {
  206. throw new \Exception(json_encode($agent_goods_bind->errors));
  207. }
  208. return [
  209. 'code' => 0,
  210. 'msg' => '申请成功,等待审核'
  211. ];
  212. } catch (\Exception $e) {
  213. return [
  214. 'code' => 1,
  215. 'msg' => $e->getMessage()
  216. ];
  217. }
  218. }
  219. /**
  220. * 绑定商品
  221. */
  222. public function applyBindFrontAgentGoods() {
  223. try {
  224. $id = $this->id;
  225. $status = $this->status;
  226. $agent_goods_bind = AgentGoodsBindGoods::findOne(['id' => $id, 'is_delete' => 0]);
  227. if (!$agent_goods_bind) {
  228. throw new \Exception('绑定信息未找到');
  229. }
  230. if (!in_array($status, [1, 2]) || (int)$agent_goods_bind->status !== 0) {
  231. throw new \Exception('状态错误');
  232. }
  233. $agent_bind = AgentBind::findOne(['admin_id' => $agent_goods_bind->goods_agent_admin_id, 'type' => 1, 'is_delete' => 0]);
  234. if ((int)$agent_bind->parent_admin_id !== (int)get_admin()->id) {
  235. throw new \Exception('权限验证错误');
  236. }
  237. $agent_goods_bind->status = $status;
  238. if (!$agent_goods_bind->save()) {
  239. throw new \Exception(json_encode($agent_goods_bind->errors));
  240. }
  241. return [
  242. 'code' => 0,
  243. 'msg' => '操作成功'
  244. ];
  245. } catch (\Exception $e) {
  246. return [
  247. 'code' => 1,
  248. 'msg' => $e->getMessage()
  249. ];
  250. }
  251. }
  252. /**
  253. * 删除绑定商品
  254. */
  255. public function delBindFrontAgentGoods() {
  256. try {
  257. $id = $this->id;
  258. $agent_goods_bind_goods = AgentGoodsBindGoods::findOne(['id' => $id, 'is_delete' => 0]);
  259. if (!$agent_goods_bind_goods) {
  260. throw new \Exception('未查找到信息');
  261. }
  262. $agent_goods_bind_goods->is_delete = 1;
  263. if (!$agent_goods_bind_goods->save()) {
  264. throw new \Exception(json_encode($agent_goods_bind_goods->errors));
  265. }
  266. return [
  267. 'code' => 0,
  268. 'msg' => '删除成功'
  269. ];
  270. } catch (\Exception $e) {
  271. return [
  272. 'code' => 1,
  273. 'msg' => $e->getMessage()
  274. ];
  275. }
  276. }
  277. /**
  278. * 获取绑定产品列表
  279. */
  280. public function getBindGoodsList() {
  281. //查询条件 商品名称 代理名称 供货商
  282. try {
  283. $admin = get_admin();
  284. $status = $this->status;
  285. $goods_name = $this->goods_name;
  286. $admin_name = $this->admin_name;
  287. $supplier_name = $this->supplier_name;
  288. if (!in_array($admin->type, ['goods_agent', 'admin'])) {
  289. throw new \Exception('权限错误');
  290. }
  291. if ($admin->type === 'admin' && $admin->username !== 'admin') {
  292. //获取子产品代理
  293. $goods_agent_admin_id = AgentBind::find()->where(['parent_admin_id' => $admin->id, 'is_delete' => 0, 'type' => 1])
  294. ->select('admin_id')->column();
  295. $goods_agent_admin_id = array_merge($goods_agent_admin_id, [0]);
  296. } else {
  297. //获取当前的产品代理
  298. $goods_agent_admin_id = $admin->id;
  299. }
  300. //查询绑定的商品信息
  301. $query = AgentGoodsBindGoods::find()->alias('agbg')
  302. ->where(['agbg.is_delete' => 0])
  303. ->leftJoin(['a' => Admin::tableName()], 'agbg.goods_agent_admin_id = a.id')
  304. ->leftJoin(['su' => SaasUser::tableName()], 'a.saas_user_id = su.id')
  305. ->select('agbg.id, agbg.cloud_goods_id, agbg.goods_agent_admin_id, a.saas_user_id, agbg.status, a.name user_name, su.avatar, agbg.cloud_supplier_id, agbg.status bind_status');//->asArray()->all();
  306. //搜索状态
  307. if ($goods_agent_admin_id) {
  308. $query->andWhere(['agbg.goods_agent_admin_id' => $goods_agent_admin_id]);
  309. }
  310. if ($status != -1 && $status !== null) {
  311. $query->andWhere(['agbg.status' => $status]);
  312. }
  313. //搜索代理名称
  314. if ($admin_name) {
  315. $query->andWhere(['LIKE', 'su.name', $admin_name]);
  316. }
  317. //搜索供货商
  318. if ($supplier_name) {
  319. $supplier_id = Supplier::find()->where(['like', 'supplier_name', $supplier_name])->select('cloud_supplier_id')->column();
  320. $query->andWhere(['agbg.cloud_supplier_id' => $supplier_id]);
  321. }
  322. $sql = (clone $query)->createCommand()->getRawSql();
  323. $list = $query->asArray()->all();
  324. // $pagination = pagination_make($query);
  325. // $list = $pagination['list'];
  326. if (!empty($list)) {
  327. $form = new PlatformForm();
  328. $form->type = 1;
  329. $goods_ids = array_column($list, 'cloud_goods_id');
  330. $goods_ids = implode(',', $goods_ids);
  331. // foreach ($pagination['list'] as &$goods) {
  332. // $saas_user = SaasUser::findOne($goods['saas_user_id']);
  333. // $goods['saas_user'] = [
  334. // 'name' => $saas_user->name,
  335. // 'avatar' => $saas_user->avatar,
  336. // ];
  337. $form->ids = $goods_ids;
  338. $form->limit = $this->limit;
  339. $form->page = $this->page;
  340. $form->send_type = 3;
  341. if ($goods_name) {
  342. $form->name = $goods_name;
  343. }
  344. $result = $form->goodsList();
  345. if ($result['code'] !== 0) {
  346. throw new \Exception($result['msg']);
  347. }
  348. foreach ($result['data']['list'] as &$goods) {
  349. foreach ($list as $value) {
  350. $value['bind_status'] *= 1;
  351. if ($value['cloud_supplier_id']) {
  352. $supplier = Supplier::findOne(['cloud_supplier_id' => $value['cloud_supplier_id']]);
  353. $value['supplier_name'] = $supplier->supplier_name;
  354. } else {
  355. $value['supplier_name'] = '-';
  356. }
  357. if ((int)$goods['id'] === (int)$value['cloud_goods_id']) {
  358. $goods = array_merge($goods, $value);
  359. }
  360. }
  361. }
  362. // $goods = array_merge($goods, $result['data']['list'][0]);
  363. // }
  364. return [
  365. 'code' => 0,
  366. 'msg' => '获取成功',
  367. 'data' => [
  368. 'list' => $result['data']['list'],
  369. 'pageNo' => $this->page,
  370. 'totalCount' => $result['data']['count'],
  371. 'sql' =>$sql
  372. ]
  373. ];
  374. } else {
  375. return [
  376. 'code' => 0,
  377. 'msg' => '获取成功',
  378. 'data' => [
  379. 'list' => [],
  380. 'pageNo' => 0,
  381. 'totalCount' => 0,
  382. ]
  383. ];
  384. }
  385. } catch (\Exception $e) {
  386. return [
  387. 'code' => 1,
  388. 'msg' => $e->getMessage()
  389. ];
  390. }
  391. }
  392. /**
  393. * 获取安装记录
  394. */
  395. public function getInstallLog() {
  396. try {
  397. $status = $this->status;
  398. $order_no = $this->order_no;
  399. $admin_name = $this->admin_name;
  400. $store_name = $this->store_name;
  401. $start_time = $this->start_time;
  402. $end_time = $this->end_time;
  403. $min_money = $this->min_money;
  404. $max_money = $this->max_money;
  405. $admin = get_admin();
  406. $order_query = AgentGoodsInstallLog::find()->alias('agil')
  407. ->leftJoin(['a' => Admin::tableName()], 'agil.goods_agent_admin_id = a.id')
  408. ->where(['is_delete' => 0]);
  409. if (get_supplier_id()) {
  410. $supplier = Supplier::findOne(get_supplier_id());
  411. $cloud_goods_id = AgentGoodsBindGoods::find()
  412. ->where(['cloud_supplier_id' => $supplier->cloud_supplier_id, 'status' => 1, 'is_delete' => 0])
  413. ->select('cloud_goods_id')->column();
  414. $supplier_order_id = OrderDetail::find()->alias('od')->leftJoin(['g' => Goods::tableName()], 'od.goods_id = g.id')
  415. ->where(['g.cloud_goods_id' => $cloud_goods_id])->select('order_id')->column();
  416. $supplier_purchase_order_id = PurchaseOrderDetail::find()->alias('od')
  417. ->where(['goods_id' => $cloud_goods_id])->select('order_id')->column() ?: [0];
  418. $order_query->andWhere(['OR', ['order_type' => 0, 'order_id' => $supplier_order_id], ['order_type' => 1, 'order_id' => $supplier_order_id]]);
  419. } else {
  420. if ($admin->type === Admin::ADMIN_TYPE_GOODS_AGENT) {
  421. $order_query->andWhere(['goods_agent_admin_id' => $admin->id]);
  422. }
  423. }
  424. ;//->select('order_id')->column();
  425. if ($status !== null && (int)$status !== -1) {
  426. $order_query->andWhere(['agil.status' => $status]);
  427. }
  428. if ($admin_name) {
  429. $order_query->andWhere(['LIKE', 'a.name', $admin_name]);
  430. }
  431. $order_arr = $order_query->groupBy('order_id')->select('agil.order_id, agil.order_type')->asArray()->all();
  432. $order_id = [0];
  433. $purchase_order_id = [0];
  434. if (!empty($order_arr)) {
  435. foreach ($order_arr as $install_order_item) {
  436. if (intval($install_order_item['order_type']) === AgentGoodsInstallLog::ORDER_TYPE_NORMAL) {
  437. array_push($order_id, $install_order_item['order_id']);
  438. }
  439. if (intval($install_order_item['order_type']) === AgentGoodsInstallLog::ORDER_TYPE_PURCHASE) {
  440. array_push($purchase_order_id, $install_order_item['order_id']);
  441. }
  442. }
  443. if (!empty($supplier_order_id)) {
  444. array_push($supplier_order_id, 0);
  445. $order_id = array_intersect($supplier_order_id, $order_id);
  446. }
  447. if (!empty($supplier_purchase_order_id)) {
  448. $purchase_order_id = array_intersect($supplier_purchase_order_id, $purchase_order_id);
  449. }
  450. }
  451. $order_table_name = Order::tableName();
  452. $purchase_order_table_name = PurchaseOrder::tableName();
  453. $store_table_name = Store::tableName();
  454. if (empty($order_id)) {
  455. $order_id = [0];
  456. }
  457. if (empty($purchase_order_id)) {
  458. $purchase_order_id = [0];
  459. }
  460. $order_id = implode(',', $order_id);
  461. $purchase_order_id = implode(',', $purchase_order_id);
  462. $fields = " id, order_no, created_at, confirm_time, pay_price, `name`, mobile, address_data, store_id ";
  463. $order_type_normal = AgentGoodsInstallLog::ORDER_TYPE_NORMAL;
  464. $order_type_purchase = AgentGoodsInstallLog::ORDER_TYPE_PURCHASE;
  465. $sql = "SELECT o.* FROM (SELECT {$fields}, {$order_type_normal} AS order_type FROM {$order_table_name} WHERE id IN ({$order_id}) AND is_delete = 0 AND trade_status IN (0, 2, 3)
  466. UNION ALL SELECT {$fields}, {$order_type_purchase} AS order_type FROM {$purchase_order_table_name} WHERE id IN ({$purchase_order_id}) AND is_delete = 0 AND trade_status IN (0, 2, 3))
  467. AS o LEFT JOIN {$store_table_name} s ON o.store_id = s.id WHERE o.id > 0 ";
  468. $query = Order::find()->alias('o')
  469. ->where(['o.id' => $order_id, 'o.is_delete' => 0, 'o.trade_status' => [0, 2, 3]])
  470. ->leftJoin(['s' => Store::tableName()], 'o.store_id = s.id')
  471. ->select('o.id, o.order_no, o.name, o.created_at, o.confirm_time, o.pay_price, o.name, o.mobile, o.address_data, o.store_id');
  472. if ($order_no) {
  473. $sql .= " AND o.order_no LIKE '%{$order_no}%'";
  474. }
  475. if ($store_name) {
  476. $sql .= " AND s.name LIKE '%{$store_name}%'";
  477. }
  478. if ($start_time) {
  479. $start_time = strtotime($start_time);
  480. $sql .= " AND o.created_at >= {$start_time}";
  481. }
  482. if ($end_time) {
  483. $end_time = strtotime($end_time);
  484. $sql .= " AND o.created_at <= {$end_time}";
  485. }
  486. if ($min_money) {
  487. $sql .= " AND o.pay_price >= {$min_money}";
  488. }
  489. if ($max_money) {
  490. $sql .= " AND o.pay_price <= {$max_money}";
  491. }
  492. $pageNo = get_params('pageNo', get_params('page', 1));
  493. $pageSize = get_params('pageSize', \Yii::$app->params['pageSize']);
  494. $pageInitNum = ($pageNo - 1) * $pageSize;
  495. $count = \Yii::$app->db->createCommand($sql)->query()->count();
  496. $sql .= " ORDER BY created_at desc LIMIT {$pageSize} OFFSET {$pageInitNum}";
  497. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  498. foreach ($list as &$item) {
  499. $item['status'] = 1;
  500. $item['created_at'] = !empty($item['created_at']) ? date('Y-m-d H:i:s', $item['created_at']) : '-';
  501. $item['confirm_time'] = !empty($item['confirm_time']) ? date('Y-m-d H:i:s', $item['confirm_time']) : '-';
  502. $order_detail_table_name = $item['order_type'] ? PurchaseOrderDetail::tableName() : OrderDetail::tableName();
  503. $item['goods_list'] = AgentGoodsInstallLog::find()->alias('agil')->where(['agil.order_id' => $item['id']])
  504. ->leftJoin(['od' => $order_detail_table_name], 'agil.order_detail_id = od.id')
  505. ->leftJoin(['a' => Admin::tableName()], 'agil.goods_agent_admin_id = a.id')
  506. ->select('od.goods_name, od.attr, od.pic, od.num, od.total_price, agil.status, agil.is_need_install, a.name admin_name')->asArray()->all();
  507. foreach ($item['goods_list'] as &$value) {
  508. $value['status'] *= 1;
  509. $value['is_need_install'] *= 1;
  510. $value['attr'] = json_decode($value['attr'], true);
  511. if ($value['status'] === 0) {
  512. $item['status'] = 0;
  513. }
  514. if ($value['is_need_install'] === 1) {
  515. if ($item['address_data']) {
  516. $address_data = json_decode($item['address_data'], true);
  517. $address_data = $address_data['province'] . $address_data['city'] . $address_data['district'] . $address_data['detail'] ;
  518. } else {
  519. $address_data = '';
  520. }
  521. $value['address'] = $address_data;
  522. $value['mobile'] = $item['mobile'];
  523. $value['name'] = $item['name'];
  524. } else {
  525. $store = Store::findOne($item['store_id']);
  526. $province_id = $store->province_id;
  527. $city_id = $store->city_id;
  528. $district_id = $store->district_id;
  529. $district = District::find()->where(['id' => [$province_id, $city_id, $district_id]])->select('name')->column();
  530. $district = implode('', $district);
  531. $value['address'] = $district . $store->address;
  532. $value['mobile'] = $store->contact_tel;
  533. $value['name'] = $store->name;
  534. }
  535. }
  536. // $item['status'] *= 1;
  537. // $item['is_need_install'] *= 1;
  538. // $item['attr'] = json_decode($item['attr'], true);
  539. }
  540. return [
  541. 'code' => 0,
  542. 'msg' => 'success',
  543. 'data' => [
  544. 'data' => $list,
  545. 'pageNo' => $pageNo,
  546. 'totalCount' => $count,
  547. ],
  548. ];
  549. } catch (\Exception $e) {
  550. return [
  551. 'code' => 1,
  552. 'msg' => $e->getMessage()
  553. ];
  554. }
  555. }
  556. /**
  557. * 点击送达
  558. */
  559. public function setInstallStatus($order_type = 0) {
  560. $t = \Yii::$app->db->beginTransaction();
  561. try {
  562. $id = $this->id;
  563. $admin = get_admin();
  564. $agent_order_type = AgentGoodsInstallLog::ORDER_TYPE_NORMAL;
  565. if (intval($order_type) !== 0) {
  566. $agent_order_type = AgentGoodsInstallLog::ORDER_TYPE_PURCHASE;
  567. }
  568. $install_log = AgentGoodsInstallLog::find()->where(['order_id' => $id, 'status' => 0, 'order_type' => $agent_order_type])->asArray()->all();
  569. if (empty($install_log)) {
  570. throw new \Exception('未查找到数据');
  571. }
  572. $is_finish = true;
  573. foreach ($install_log as $value) {
  574. if ((int)$value['goods_agent_admin_id'] !== (int)$admin->id) {
  575. throw new \Exception('权限错误');
  576. }
  577. if ((int)$value['status'] !== 0) {
  578. throw new \Exception('订单已经送达');
  579. }
  580. $item = AgentGoodsInstallLog::findOne($value['id']);
  581. $item->status = 1;
  582. if (!$item->save()) {
  583. throw new \Exception(json_encode($item->errors));
  584. }
  585. if (!$item->is_need_install) {
  586. $is_finish = false;
  587. }
  588. }
  589. //如果不需要上门安装 则发货操作由商家完成
  590. //模拟供货商发货即可(转单)
  591. if (intval($order_type)) {
  592. $purchase_order = PurchaseOrder::findOne(['id' => $id, 'is_delete' => 0]);
  593. $cloud_supplier_id = $purchase_order->cloud_supplier_id;
  594. $cloud_order_id = $purchase_order->cloud_order_id;
  595. $goods_url = "/cloud/supplier/supplierOrderSend";
  596. } else {
  597. $order_transit = OrderTransit::findOne(['order_id' => $id, 'is_delete' => 0]);
  598. $cloud_supplier_id = $order_transit->cloud_supplier_id;
  599. $cloud_order_id = $order_transit->cloud_order_id;
  600. $goods_url = "/cloud/supplier/supplierPurchaseSend";
  601. }
  602. $param = [
  603. 'order_ids' => (string)$cloud_order_id,
  604. 'access_token' => get_supplier_token($cloud_supplier_id),
  605. 'express_name' => '',
  606. 'express_no' => '',
  607. 'send_type' => 3,
  608. 'seller_desc' => '',
  609. ];
  610. //请求接口
  611. $domain = (new OptionSetting)->getCloudDomainName();
  612. $goodsInfo = cloud_post($domain . $goods_url, $param);
  613. $goodsInfo = json_decode($goodsInfo, true);
  614. if ((int)$goodsInfo['code'] !== 0) {
  615. throw new \Exception($goodsInfo['msg']);
  616. }
  617. $t->commit();
  618. return [
  619. 'code' => 0,
  620. 'msg' => '操作成功'
  621. ];
  622. } catch (\Exception $e) {
  623. $t->rollBack();
  624. return [
  625. 'code' => 1,
  626. 'msg' => $e->getMessage()
  627. ];
  628. }
  629. }
  630. /**
  631. * 获取前置仓绑定的供货商
  632. */
  633. public function getSupplier($parent_admin_id = 0) {
  634. try {
  635. //查询前置仓 以及绑定的供货商
  636. $front_agent = AgentBind::find()->where(['parent_admin_id' => $parent_admin_id, 'is_delete' => 0, 'type' => 2])
  637. ->select('admin_id')->column();
  638. if ($front_agent) {
  639. $supplier_id = AgentFrontBind::find()->where(['type' => 0, 'is_delete' => 0, 'front_agent_admin_id' => $front_agent, 'status' => 1])
  640. ->select('type_id')->column();
  641. }
  642. if (!empty($supplier_id)) {
  643. $supplier_id = Supplier::find()->where(['id' => $supplier_id])->select('cloud_supplier_id')->column();
  644. }
  645. return [
  646. 'code' => 0,
  647. 'msg' => '获取成功',
  648. 'data' => $supplier_id ?? []
  649. ];
  650. } catch (\Exception $e) {
  651. return [
  652. 'code' => 1,
  653. 'msg' => $e->getMessage()
  654. ];
  655. }
  656. }
  657. /**
  658. * 找到同级产品代理,检测某个商品是否已经被代理并剔除
  659. */
  660. public function checkGoodsBind($parent_admin_id = 0, $admin_id = 0) {
  661. try {
  662. //找到同级产品代理,检测被代理的产品
  663. $front_agent = AgentBind::find()->where(['parent_admin_id' => $parent_admin_id, 'is_delete' => 0, 'type' => 1])
  664. ->select('admin_id')->column();
  665. if ($admin_id) {
  666. $front_agent = array_diff($front_agent, [$admin_id]);
  667. }
  668. $bind_goods = AgentGoodsBindGoods::find()->where(['goods_agent_admin_id' => $front_agent, 'is_delete' => 0, 'status' => [0, 1]])
  669. ->select('cloud_goods_id')->column();
  670. return [
  671. 'code' => 0,
  672. 'msg' => '获取成功',
  673. 'data' => $bind_goods
  674. ];
  675. } catch (\Exception $e) {
  676. return [
  677. 'code' => 1,
  678. 'msg' => $e->getMessage()
  679. ];
  680. }
  681. }
  682. }