WarehouseForm.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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\Goods;
  9. use app\models\Warehouse;
  10. use app\models\WarehouseZone;
  11. class WarehouseForm extends Model
  12. {
  13. public $store_id;
  14. public $id;
  15. public $name;
  16. public $sort;
  17. public $is_delete;
  18. public function rules()
  19. {
  20. return [
  21. [['id', 'store_id', 'sort'], 'integer'],
  22. [['name'], 'string'],
  23. [['name'], 'safe'],
  24. ];
  25. }
  26. public function search ()
  27. {
  28. try {
  29. $query = Warehouse::find()->where(['is_delete' => 0, 'store_id' => $this->store_id]);
  30. if (!empty($this->id)) {
  31. $query->andWhere(['id' => $this->id]);
  32. }
  33. if (!empty($this->name)) {
  34. $query->andWhere(['like', 'name', $this->name]);
  35. }
  36. $query->orderBy('sort DESC');
  37. $pagination = pagination_make($query);
  38. foreach ($pagination['list'] as &$item) {
  39. $item['warehouseZoneNum'] = WarehouseZone::find()->where(['is_delete' => 0, 'warehouse_id' => $item['id']])->count();
  40. $item['goods_num'] = Goods::find()->where(['is_delete' => 0, 'warehouse_id' => $item['id']])->count();
  41. }
  42. return [
  43. 'code' => 0,
  44. 'msg' => 'success',
  45. 'data' => $pagination,
  46. ];
  47. } catch (\Exception $e) {
  48. \Yii::error($e);
  49. return [
  50. 'code' => 1,
  51. 'msg' => $e->getMessage()
  52. ];
  53. }
  54. }
  55. public function selectList ()
  56. {
  57. try {
  58. $query = Warehouse::find()->where(['is_delete' => 0, 'store_id' => $this->store_id]);
  59. $query->orderBy('id DESC');
  60. $query->select('id, name');
  61. $data = $query->asArray()->all();
  62. return [
  63. 'code' => 0,
  64. 'msg' => 'success',
  65. 'data' => $data,
  66. ];
  67. } catch (\Exception $e) {
  68. \Yii::error($e);
  69. return [
  70. 'code' => 1,
  71. 'msg' => $e->getMessage()
  72. ];
  73. }
  74. }
  75. public function save ()
  76. {
  77. $t = \Yii::$app->db->beginTransaction();
  78. try {
  79. $model = Warehouse::findOne(['id' => $this->id, 'store_id' => $this->store_id]);
  80. if (empty($model)) {
  81. $model = new Warehouse();
  82. $model->store_id = $this->store_id;
  83. }
  84. $model->name = $this->name;
  85. $model->sort = $this->sort;
  86. if (!$model->save()) {
  87. \Yii::error([__METHOD__, $model->attributes]);
  88. throw new \Exception(array_shift($model->getFirstErrors()));
  89. }
  90. $t->commit();
  91. return [
  92. 'code' => 0,
  93. 'msg' => '操作成功!'
  94. ];
  95. } catch (\Exception $e) {
  96. \Yii::error($e);
  97. $t->rollBack();
  98. return [
  99. 'code' => 1,
  100. 'msg' => $e->getMessage()
  101. ];
  102. }
  103. }
  104. public function del ()
  105. {
  106. try {
  107. if ($this->id) {
  108. Warehouse::updateAll(['is_delete' => 1], ['id' => $this->id]);
  109. }
  110. return [
  111. 'code' => 0,
  112. 'msg' => '操作成功!'
  113. ];
  114. } catch (\Exception $e) {
  115. \Yii::error($e);
  116. return [
  117. 'code' => 1,
  118. 'msg' => $e->getMessage()
  119. ];
  120. }
  121. }
  122. }