WarehouseZoneForm.php 3.3 KB

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