| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models\erp;
- use app\models\Warehouse;
- use app\models\WarehouseZone;
- use app\models\Goods;
- class WarehouseZoneForm extends Model
- {
- public $store_id;
- public $id;
- public $name;
- public $sort;
- public $is_delete;
- public $warehouse_id;
- public function rules()
- {
- return [
- [['id', 'store_id', 'sort', 'warehouse_id'], 'integer'],
- [['name'], 'string'],
- [['name'], 'safe'],
- ];
- }
- public function search ()
- {
- try {
- $query = WarehouseZone::find()->alias('wz')
- ->leftJoin(['w' => Warehouse::tableName()], 'w.id = wz.warehouse_id')
- ->where(['wz.is_delete' => 0, 'wz.store_id' => $this->store_id]);
- if (!empty($this->name)) {
- $query->andWhere(['like', 'wz.name', $this->name]);
- }
- if (!empty($this->warehouse_id)) {
- $query->andWhere(['wz.warehouse_id' => $this->warehouse_id]);
- }
- $query->orderBy('wz.sort DESC')->select('wz.*,w.name as warehouse_name');
- $pagination = pagination_make($query);
- foreach ($pagination['list'] as &$item) {
- $item['goods_num'] = Goods::find()->where(['is_delete' => 0, 'warehouse_zone_id' => $item['id']])->count();
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $pagination,
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function save ()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $model = WarehouseZone::findOne(['id' => $this->id, 'store_id' => $this->store_id]);
- if (empty($model)) {
- $model = new WarehouseZone();
- $model->store_id = $this->store_id;
- }
- $model->name = $this->name;
- $model->warehouse_id = $this->warehouse_id;
- $model->sort = $this->sort;
- if (!$model->save()) {
- \Yii::error([__METHOD__, $model->attributes]);
- throw new \Exception(array_shift($model->getFirstErrors()));
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '操作成功!'
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function del ()
- {
- try {
- if ($this->id) {
- WarehouseZone::updateAll(['is_delete' => 1], ['id' => $this->id]);
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功!'
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|