| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models\agent\front_erp;
- use app\models\Goods;
- use app\models\Warehouse;
- use app\models\WarehouseZone;
- class WarehouseForm extends Model
- {
- public $store_id;
- public $id;
- public $name;
- public $sort;
- public $is_delete;
- public function rules()
- {
- return [
- [['id', 'store_id', 'sort'], 'integer'],
- [['name'], 'string'],
- [['name'], 'safe'],
- ];
- }
- public function search ()
- {
- try {
- $query = Warehouse::find()->where(['is_delete' => 0, 'store_id' => $this->store_id]);
- if (!empty($this->id)) {
- $query->andWhere(['id' => $this->id]);
- }
- if (!empty($this->name)) {
- $query->andWhere(['like', 'name', $this->name]);
- }
- $query->orderBy('sort DESC');
- $pagination = pagination_make($query);
- foreach ($pagination['list'] as &$item) {
- $item['warehouseZoneNum'] = WarehouseZone::find()->where(['is_delete' => 0, 'warehouse_id' => $item['id']])->count();
- $item['goods_num'] = Goods::find()->where(['is_delete' => 0, 'warehouse_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 selectList ()
- {
- try {
- $query = Warehouse::find()->where(['is_delete' => 0, 'store_id' => $this->store_id]);
- $query->orderBy('id DESC');
- $query->select('id, name');
- $data = $query->asArray()->all();
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $data,
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function save ()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $model = Warehouse::findOne(['id' => $this->id, 'store_id' => $this->store_id]);
- if (empty($model)) {
- $model = new Warehouse();
- $model->store_id = $this->store_id;
- }
- $model->name = $this->name;
- $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) {
- Warehouse::updateAll(['is_delete' => 1], ['id' => $this->id]);
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功!'
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|