InventoryForm.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models\erp;
  8. use app\models\ErpInventory;
  9. use app\models\ErpInventoryProp;
  10. use app\models\ErpInventoryPropLog;
  11. use app\models\ErpInventoryLog;
  12. use app\models\ErpPurchaseinOrder;
  13. use app\models\ErpPurchasein;
  14. use app\models\ErpPurchaseoutOrder;
  15. use app\models\ErpPurchaseout;
  16. use app\models\ErpSupplier;
  17. use app\models\Goods;
  18. use app\models\GoodsCat;
  19. use app\models\Cat;
  20. use app\models\Option;
  21. use app\constants\OptionSetting;
  22. use app\models\Order;
  23. use app\models\OrderDetail;
  24. use app\models\User;
  25. use app\models\SaasUser;
  26. use app\models\Warehouse;
  27. use app\models\WarehouseZone;
  28. class InventoryForm extends Model
  29. {
  30. public $store_id;
  31. public $id;
  32. public $ids;
  33. public $goods_name;
  34. public $supplier_name;
  35. public $goods_id;
  36. public $cat_id;
  37. public $supplier_id;
  38. public $warehouse_id;
  39. public $warehouse_zone_id;
  40. public $is_delete;
  41. public $warning_num;
  42. public $eiId;
  43. public $eipId;
  44. public $nickname;
  45. public $phone;
  46. public $order_no;
  47. public $type;
  48. public $purchase_in_price;
  49. public function rules()
  50. {
  51. return [
  52. [['id', 'store_id', 'is_delete'], 'integer'],
  53. [['ids', 'goods_name', 'supplier_name'], 'string'],
  54. [['is_delete', 'goods_id', 'supplier_id', 'warehouse_id', 'warehouse_zone_id', 'warning_num', 'eiId', 'nickname', 'phone', 'order_no', 'type'], 'safe'],
  55. [['cat_id', 'purchase_in_price', 'eipId'], 'safe'],
  56. ];
  57. }
  58. public function init() {
  59. parent::init();
  60. if(empty($this->store_id)){
  61. $this->store_id = get_store_id();
  62. }
  63. }
  64. public static function isOpen($store_id) {
  65. $isOpen = Option::get(OptionSetting::ERP_OPEN, $store_id, 'store', 0)['value'];
  66. return $isOpen;
  67. }
  68. public static function initStoreGoods($store_id, $start_id = 0, $count = 10) {
  69. if(!self::isOpen($store_id)){
  70. return [
  71. 'code' => 1,
  72. 'msg' => '操作失败!功能未开启'
  73. ];
  74. }
  75. if($start_id == 0 && cache_lock(__METHOD__ . $store_id, 86400 - time()%86400)){
  76. return [
  77. 'code' => 1,
  78. 'msg' => '操作失败!提交太频繁,明天再试'
  79. ];
  80. }
  81. $query = Goods::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'mch_id' => 0, 'product_type' => 0]);
  82. $query->andWhere(['md_food_id' => 0]);
  83. $query->andWhere(['cloud_goods_id' => 0]);
  84. $query->andWhere(['jst_goods_id' => '']);
  85. $query->andWhere(['>=', 'id', $start_id]);
  86. $goodsList = $query->select('id')->limit($count)->asArray()->column();
  87. $errIds = [];
  88. foreach($goodsList as $gid){
  89. $start_id = $gid;
  90. $res = self::initGoods($store_id, $gid);
  91. if($res['code'] != 0){
  92. $errIds[] = $gid . $res['msg'];
  93. // return $res;
  94. }
  95. }
  96. $queue = 0;
  97. if(count($goodsList) >= $count){
  98. $queue = queue_push(new \app\jobs\erp\ErpInitStoreGoodsJob([
  99. 'store_id' => $store_id,
  100. 'id' => $start_id + 1,
  101. 'count' => 100,
  102. ]), 30, 1);
  103. }
  104. return [
  105. 'code' => 0,
  106. 'msg' => '操作成功!' . ($queue ? '数据较多,后台继续处理中' : ''),
  107. 'errIds' => $errIds,
  108. ];
  109. }
  110. public static function filterGoods($goods) {
  111. if($goods['jst_goods_id']){
  112. return false;
  113. }
  114. if($goods['cloud_goods_id'] && !intval($goods['is_wholesale'])){
  115. return false;
  116. }
  117. if($goods['md_food_id']){
  118. return false;
  119. }
  120. if($goods['mch_id']){
  121. return false;
  122. }
  123. if($goods['product_type']){
  124. return false;
  125. }
  126. return true;
  127. }
  128. public static function initGoods($store_id, $goods) {
  129. $t = \Yii::$app->db->beginTransaction();
  130. try {
  131. if(!($goods instanceof Goods)){
  132. $goods = Goods::find()->where(['id' => $goods, 'store_id' => $store_id])->limit(1)->one();
  133. }
  134. if(!self::filterGoods($goods)){
  135. throw new \Exception('非本地上传商品或无需进销存管理');
  136. }
  137. Goods::skuAttr($goods);
  138. $attr = json_decode($goods['attr'], true);
  139. if(empty($attr)){
  140. throw new \Exception('attr是空的');
  141. }
  142. foreach($attr as $item){
  143. if(!$item['attr_ids']){
  144. throw new \Exception('商品规格数据错误' . $goods['id'] . json_encode($attr));
  145. }
  146. $model = ErpInventory::findOne(['goods_id' => $goods['id'], 'sku_id' => $item['cyy_skuId'], 'store_id' => $goods['store_id']]);
  147. if (empty($model)) {
  148. $num = 0;
  149. $erp_init_goods_set_num = Option::get(OptionSetting::ERP_INIT_GOODS_SET_NUM, $store_id)['value'];
  150. if($erp_init_goods_set_num){
  151. $num = $item['num'];
  152. }
  153. $model = new ErpInventory();
  154. $model->store_id = $goods['store_id'];
  155. $model->store_id = $goods['store_id'];
  156. $model->goods_id = $goods['id'];
  157. $model->sku_id = $item['cyy_skuId'];
  158. $model->num = $num;
  159. $model->attr_ids = $item['attr_ids'];
  160. $model->attr_info = json_encode($item);
  161. $model->goods_no = $item['no'];
  162. $model->sync2Goods = 0;
  163. if (!$model->save()) {
  164. \Yii::error([__METHOD__, $model->attributes]);
  165. throw new \Exception(array_shift($model->getFirstErrors()));
  166. }
  167. }else{
  168. $model->is_delete = 0;
  169. $up = 0;
  170. $attr_info = json_decode($model->attr_info, true);
  171. if($model->goods_no != $item['no']){
  172. $up++;
  173. $model->goods_no = $item['no'];
  174. }
  175. \Yii::error([__METHOD__, $attr_info, $item]);
  176. if($attr_info['pic'] != $item['pic'] || $attr_info['price'] != $item['price']){
  177. $up++;
  178. $model->attr_info = json_encode($item);
  179. }
  180. $model->sync2Goods = 0;
  181. if ($up && !$model->save()) {
  182. \Yii::error([__METHOD__, $model->attributes]);
  183. throw new \Exception(array_shift($model->getFirstErrors()));
  184. }
  185. }
  186. }
  187. $t->commit();
  188. return [
  189. 'code' => 0,
  190. 'msg' => '操作成功!'
  191. ];
  192. } catch (\Exception $e) {
  193. $t->rollBack();
  194. \Yii::error($e);
  195. return [
  196. 'code' => 1,
  197. 'msg' => $e->getMessage()
  198. ];
  199. }
  200. }
  201. public static function afterGoodsSave($goods, $insert, $changedAttributes) {
  202. if(!self::isOpen($goods['store_id'])){
  203. return;
  204. }
  205. if($insert || array_key_exists('attr', $changedAttributes)){
  206. self::initGoods($goods['store_id'], $goods);
  207. }
  208. }
  209. public static function erp2Goods($store_id, $goods) {
  210. try{
  211. if(!self::isOpen($store_id)){
  212. return;
  213. }
  214. $list = ErpInventory::findAll(['store_id' => $store_id, 'goods_id' => $goods['id'], 'is_delete' => 0]);
  215. foreach($list as $item){
  216. ErpInventory::num2Goods($item['id']);
  217. }
  218. $list && $goods->refresh();
  219. } catch (\Exception $ex) {
  220. \Yii::error($ex->getMessage());
  221. }
  222. }
  223. public static function afterOrderDetailSave($od) {
  224. try {
  225. $order = $od->order;
  226. if(!self::isOpen($order['store_id'])){
  227. return;
  228. }
  229. $skuId = Goods::skuid1($od['goods_id'], json_decode($od['attr'], true));
  230. $ei = ErpInventory::findOne(['is_delete' => 0, 'goods_id' => $od['goods_id'], 'sku_id' => $skuId]);
  231. if($ei){
  232. ErpInventory::logSave($ei, -$od['num'], ErpInventoryLog::LOG_TYPE_ORDER, $order->id, $order->order_no);
  233. }
  234. } catch (\Exception $e) {
  235. \Yii::error($e);
  236. return [
  237. 'code' => 1,
  238. 'msg' => $e->getMessage()
  239. ];
  240. }
  241. }
  242. public static function afterOrderSave($order, $insert, $changedAttributes) {
  243. try {
  244. if(!self::isOpen($order['store_id'])){
  245. return;
  246. }
  247. //恢复库存
  248. if (!$insert && isset($changedAttributes['trade_status']) && $order->trade_status == Order::ORDER_FLOW_CANCEL) {
  249. if(!cache_lock(__METHOD__ . $order->id, 60)){
  250. $od = $order->detail;
  251. foreach($od as $item){
  252. $skuId = Goods::skuid1($item['goods_id'], json_decode($item['attr'], true));
  253. $ei = ErpInventory::findOne(['is_delete' => 0, 'goods_id' => $item['goods_id'], 'sku_id' => $skuId]);
  254. if($ei){
  255. ErpInventory::logSave($ei, $item['num'], ErpInventoryLog::LOG_TYPE_ORDER, $order->id, $order->order_no);
  256. }
  257. }
  258. }
  259. }
  260. } catch (\Exception $e) {
  261. \Yii::error($e);
  262. return [
  263. 'code' => 1,
  264. 'msg' => $e->getMessage()
  265. ];
  266. }
  267. }
  268. public static function getAllByGid($goods_id = 0){
  269. return ErpInventory::find()->where(['goods_id' => $goods_id, 'is_delete' => 0])->asArray()->all();
  270. }
  271. public function warningList ($query = [])
  272. {
  273. $this->warning_num = ERP::getWarningNum($this->store_id);
  274. // if (empty($this->supplier_id)) {
  275. // return [
  276. // 'code' => 1,
  277. // 'msg' => '请选择供货商'
  278. // ];
  279. // }
  280. // $_GET['pageSize'] = 300;
  281. return $this->search(0, $query);
  282. }
  283. public function search($eip_list = 0, $params = [])
  284. {
  285. try {
  286. $query = ErpInventory::find()->alias('ei')->where(['ei.is_delete' => 0, 'ei.store_id' => $this->store_id]);
  287. $query->leftJoin(['g' => Goods::tableName()], 'g.id = ei.goods_id')->andWhere(['g.is_delete' => 0]);
  288. $query->leftJoin(['es' => ErpSupplier::tableName()], 'es.id = ei.supplier_id');
  289. $query->leftJoin(['wh' => Warehouse::tableName()], 'wh.id = g.warehouse_id');
  290. $query->leftJoin(['whz' => WarehouseZone::tableName()], 'whz.id = g.warehouse_zone_id');
  291. if (!empty($this->warning_num)) {
  292. $query->andWhere(['<', 'ei.num', $this->warning_num]);
  293. }
  294. if (!empty($params['prop'])) {
  295. if($params['prop'] == 1){
  296. $query->andWhere(['ei.id' => ErpInventoryProp::find()->select('inventory_id')->where(['is_delete' => 0])]);
  297. }
  298. if($params['prop'] == 2){
  299. $query->andWhere(['not in', 'ei.id', ErpInventoryProp::find()->select('inventory_id')->where(['is_delete' => 0])]);
  300. }
  301. }
  302. if (!empty($this->goods_name)) {
  303. $query->andWhere(['like', 'g.name', trim($this->goods_name)]);
  304. }
  305. if (!empty($this->supplier_name)) {
  306. $query->andWhere(['ei.supplier_id' => ErpSupplier::find()->select('id')->where(['like', 'name', $this->supplier_name])]);
  307. }
  308. if (!empty($this->supplier_id)) {
  309. $query->andWhere(['ei.supplier_id' => $this->supplier_id]);
  310. }
  311. if (!empty($this->goods_id)) {
  312. $query->andWhere(['ei.goods_id' => $this->goods_id]);
  313. }
  314. if (!empty($params['goods_no'])) {
  315. $query->andWhere(['ei.goods_no' => $params['goods_no']]);
  316. }
  317. if (!empty($this->cat_id)) {
  318. $query->andWhere(['ei.goods_id' => GoodsCat::find()->select('goods_id')->where(['cat_id' => Cat::getCatId($this->cat_id)])]);
  319. }
  320. if (!empty($params['warehouse_id'])) {
  321. $query->andWhere(['g.warehouse_id' => $params['warehouse_id']]);
  322. }
  323. if (!empty($params['warehouse_zone_id'])) {
  324. $query->andWhere(['g.warehouse_zone_id' => $params['warehouse_zone_id']]);
  325. }
  326. $query->select('ei.*, g.name goods_name, g.cover_pic, g.warehouse_id, g.warehouse_zone_id, es.name supplier_name,wh.name as wh_name,whz.name as whz_name');
  327. if($eip_list){
  328. $query->leftJoin(['eip' => ErpInventoryProp::tableName()], 'eip.inventory_id = ei.id AND eip.is_delete = 0');
  329. $query->leftJoin(['eipl' => ErpInventoryPropLog::find()->alias('eipl')->leftJoin(['eil' => ErpInventoryLog::tableName()], 'eipl.inventory_log_id=eil.id')->where('eil.type=1')->select('MAX(eipl.id) max_eipl_id,eipl.inventory_prop_id')->groupBy('eipl.inventory_prop_id')], 'eipl.inventory_prop_id = eip.id');
  330. $query->addSelect('eip.id eip_id');
  331. $query->indexBy(function($row){
  332. return implode('_', [$row['id'], $row['eip_id']]);
  333. });
  334. $query->addSelect('eipl.max_eipl_id');
  335. $query->addOrderBy('eipl.max_eipl_id DESC');
  336. $query_in = ErpPurchaseinOrder::find()->alias('epo')->leftJoin(['epi' => ErpPurchasein::tableName()], 'epo.id=epi.purchase_order_id')->select('epi.inventory_id');
  337. if (!empty($params['in_start_time'])) {
  338. $query_in->andWhere(['>=' , 'epo.created_at', strtotime($params['in_start_time'])]);
  339. }
  340. if (!empty($params['in_end_time'])) {
  341. $query_in->andWhere(['<=' , 'epo.created_at', strtotime($params['in_end_time']) + 86400]);
  342. }
  343. if (!empty($params['timeout_start_time'])) {
  344. $query_in->andWhere(['>=' , 'eip.timeout', strtotime($params['timeout_start_time'])]);
  345. }
  346. if (!empty($params['timeout_end_time'])) {
  347. $query_in->andWhere(['<=' , 'eip.timeout', strtotime($params['timeout_end_time']) + 86400]);
  348. }
  349. if($query_in->where){
  350. $query->andWhere(['ei.id' => $query_in]);
  351. }
  352. }
  353. $query->addOrderBy('ei.goods_id DESC');
  354. $pagination = pagination_make($query);
  355. $pagination['list'] = array_values($pagination['list']);
  356. foreach ($pagination['list'] as &$item) {
  357. $item['warehouse_name'] = $item['wh_name'].'-'.$item['whz_name'];
  358. // $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  359. $attr_info = json_decode($item['attr_info'], true);
  360. $item['attr_names'] = implode(',', array_column($attr_info['attr_list'], 'attr_name'));
  361. $item['price'] = $attr_info['price'];
  362. if($attr_info['pic']){
  363. $item['cover_pic'] = $attr_info['pic'];
  364. }
  365. $eip = null;
  366. if($item['eip_id']){
  367. $eip = ErpInventoryProp::findOne($item['eip_id'])->toArray();
  368. $item['num_total'] = $item['num'];
  369. $item['num'] = $eip['num'];
  370. }
  371. $item['num_total_goods'] = (int)ErpInventory::find()->where(['is_delete' => 0, 'goods_id' => $item['goods_id']])->sum('num');
  372. $item['eip'] = $eip;
  373. $erpPurchasein = ErpPurchasein::find()->where(['goods_id' => $item['goods_id']])->orderBy('created_at DESC')->one();
  374. if ($erpPurchasein){
  375. $item['purchase_in_price'] = $erpPurchasein['price'];
  376. $item['num'] = $erpPurchasein['num'];
  377. $item['supplier_id'] = $erpPurchasein['supplier_id'];
  378. $item['timeout'] = $erpPurchasein['timeout'] ? date('Y-m-d', $erpPurchasein['timeout']) : '';
  379. $item['batch_no'] = $erpPurchasein['batch_no'];
  380. }
  381. }
  382. return [
  383. 'code' => 0,
  384. 'msg' => 'success',
  385. 'data' => $pagination,
  386. 'q' => $query->createCommand()->getRawSql(),
  387. ];
  388. } catch (\Exception $e) {
  389. \Yii::error($e);
  390. return [
  391. 'code' => 1,
  392. 'msg' => $e->getMessage()
  393. ];
  394. }
  395. }
  396. public function saveSupplier ()
  397. {
  398. $t = \Yii::$app->db->beginTransaction();
  399. try {
  400. $ids = explode(',', $this->id);
  401. foreach($ids as $id){
  402. $up = 0;
  403. $model = ErpInventory::findOne(['is_delete' => 0, 'id' => $id, 'store_id' => $this->store_id]);
  404. if($this->supplier_id && $model->supplier_id != $this->supplier_id){
  405. $up++;
  406. $model->supplier_id = $this->supplier_id;
  407. }
  408. if($this->purchase_in_price && $model->purchase_in_price != $this->purchase_in_price){
  409. $up++;
  410. $model->purchase_in_price = $this->purchase_in_price;
  411. }
  412. if ($up && !$model->save()) {
  413. \Yii::error([__METHOD__, $model->attributes]);
  414. throw new \Exception(array_shift($model->getFirstErrors()));
  415. }
  416. if($this->warehouse_id){
  417. $goods = Goods::findOne($model->goods_id);
  418. $goods->warehouse_id = $this->warehouse_id;
  419. $goods->warehouse_zone_id = $this->warehouse_zone_id;
  420. if (!$goods->save()) {
  421. \Yii::error([__METHOD__, $goods->attributes]);
  422. throw new \Exception(array_shift($goods->getFirstErrors()));
  423. }
  424. }
  425. }
  426. $t->commit();
  427. $warehouseZone = WarehouseZone::findOne($this->warehouse_zone_id);
  428. $warehouse_zone_name = $warehouseZone->name;
  429. $warehouse = Warehouse::findOne($this->warehouse_id);
  430. $warehouse_name = $warehouse->name;
  431. $erpSupplier = ErpSupplier::findOne($this->supplier_id);
  432. $erp_supplier_name = $erpSupplier->name;
  433. return [
  434. 'code' => 0,
  435. 'msg' => '操作成功!',
  436. 'data' => [
  437. 'supplier_id' => $this->supplier_id,
  438. 'warehouse_id' => $this->warehouse_id,
  439. 'warehouse_zone_id' => $this->warehouse_zone_id,
  440. 'warehouse_zone_name' => $warehouse_zone_name,
  441. 'warehouse_name' => $warehouse_name,
  442. 'erp_supplier_name' => $erp_supplier_name,
  443. ]
  444. ];
  445. } catch (\Exception $e) {
  446. \Yii::error($e);
  447. $t->rollBack();
  448. return [
  449. 'code' => 1,
  450. 'msg' => $e->getMessage()
  451. ];
  452. }
  453. }
  454. public function saveKey ()
  455. {
  456. $t = \Yii::$app->db->beginTransaction();
  457. try {
  458. $model = ErpInventory::findOne(['is_delete' => 0, 'id' => $this->id, 'store_id' => $this->store_id]);
  459. if (empty($model)) {
  460. throw new \Exception('参数错误');
  461. }
  462. $key = $this->key;
  463. $model->$key = $this->val;
  464. if (!$model->save()) {
  465. \Yii::error([__METHOD__, $model->attributes]);
  466. throw new \Exception(array_shift($model->getFirstErrors()));
  467. }
  468. $t->commit();
  469. return [
  470. 'code' => 0,
  471. 'msg' => '操作成功!'
  472. ];
  473. } catch (\Exception $e) {
  474. \Yii::error($e);
  475. $t->rollBack();
  476. return [
  477. 'code' => 1,
  478. 'msg' => $e->getMessage()
  479. ];
  480. }
  481. }
  482. public function del ($params = [])
  483. {
  484. $t = \Yii::$app->db->beginTransaction();
  485. try {
  486. $eip_id = $params['eip_id'] ?: 0;
  487. $delSave = (new PurchaseoutForm())->delSave($eip_id);
  488. if ($delSave['code']) {
  489. \Yii::error([__METHOD__, $eip_id, $delSave]);
  490. throw new \Exception($delSave['msg']);
  491. }
  492. if ($eip_id) {
  493. $up = ErpInventoryProp::updateAll(['is_delete' => 1], ['id' => $eip_id]);
  494. if (!$up) {
  495. \Yii::error([__METHOD__, $eip_id, $up]);
  496. throw new \Exception('eip删除错误');
  497. }
  498. }
  499. $t->commit();
  500. return [
  501. 'code' => 0,
  502. 'msg' => '操作成功!',
  503. ];
  504. } catch (\Exception $e) {
  505. \Yii::error($e);
  506. $t->rollBack();
  507. return [
  508. 'code' => 1,
  509. 'msg' => $e->getMessage()
  510. ];
  511. }
  512. }
  513. public function logList() {
  514. try {
  515. $query = ErpInventoryLog::find()->alias('eil')
  516. ->leftJoin(['ei' => ErpInventory::tableName()], 'ei.id = eil.inventory_id')
  517. ->where(['eil.is_delete' => 0, 'ei.store_id' => $this->store_id]);
  518. $query->leftJoin(['o' => Order::tableName()], 'o.id = eil.order_id');
  519. $query->leftJoin(['u' => User::tableName()], 'o.user_id = u.id');
  520. $query->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile');
  521. $query->leftJoin(['g' => Goods::tableName()], 'g.id = ei.goods_id');
  522. if($this->eiId){
  523. $query->andWhere(['ei.id' => $this->eiId]);
  524. }
  525. if($this->eipId){
  526. $query->andWhere(['eil.id' => ErpInventoryPropLog::find()->select('inventory_log_id')->where(['inventory_prop_id' => $this->eipId])]);
  527. }
  528. if(isset($this->type) && $this->type > -1){
  529. $query->andWhere(['eil.type' => $this->type]);
  530. }
  531. if (!empty($this->goods_name)) {
  532. $query->andWhere(['like', 'g.name', trim($this->goods_name)]);
  533. }
  534. if($this->nickname || $this->phone){
  535. $query->andWhere(['eil.type' => ErpInventoryLog::LOG_TYPE_ORDER]);
  536. $query->andWhere(['like', 'su.mobile', $this->phone]);
  537. $query->andWhere(['like', 'su.name', $this->nickname]);
  538. }
  539. if($this->order_no){
  540. $query->andWhere(['like', 'eil.order_no', $this->order_no]);
  541. }
  542. if (!empty($this->supplier_id)) {
  543. $query->andWhere(['ei.supplier_id' => $this->supplier_id]);
  544. }
  545. $query->orderBy('eil.id DESC');
  546. $query->select('eil.*, su.name, su.mobile, su.avatar, g.name goods_name, g.cover_pic');
  547. $pagination = pagination_make($query);
  548. foreach ($pagination['list'] as &$item) {
  549. $item['ei'] = ErpInventory::findOne($item['inventory_id']);
  550. $attr_info = json_decode($item['ei']['attr_info'], true);
  551. $item['attr_names'] = implode(',', array_column($attr_info['attr_list'], 'attr_name'));
  552. if($attr_info['pic']){
  553. $item['cover_pic'] = $attr_info['pic'];
  554. }
  555. if($item['type'] == ErpInventoryLog::LOG_TYPE_PURCHASEIN){
  556. $item['epo'] = ErpPurchaseinOrder::findOne($item['order_id']);
  557. $item['ep'] = ErpPurchasein::findOne(['purchase_order_id' => $item['order_id'], 'inventory_id' => $item['inventory_id']]);
  558. }
  559. $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  560. if($item['type'] == ErpInventoryLog::LOG_TYPE_PURCHASEIN){
  561. $item['name'] = '入库';
  562. }
  563. if($item['type'] == ErpInventoryLog::LOG_TYPE_PURCHASEOUT){
  564. $item['name'] = '出库';
  565. }
  566. if($this->eipId){
  567. $propLog = ErpInventoryPropLog::find()->where(['inventory_prop_id' => $this->eipId, 'inventory_log_id' => $item['id']])->one();
  568. $item['propLog'] = $propLog;
  569. $item['num'] = $propLog['num'];
  570. $item['before'] = $propLog['before'];
  571. $item['after'] = $propLog['after'];
  572. }
  573. }
  574. return [
  575. 'code' => 0,
  576. 'msg' => 'success',
  577. 'data' => $pagination,
  578. 'q' => $query->createCommand()->getRawSql(),
  579. ];
  580. } catch (\Exception $e) {
  581. \Yii::error($e);
  582. return [
  583. 'code' => 1,
  584. 'msg' => $e->getMessage()
  585. ];
  586. }
  587. }
  588. }