| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models\erp;
- use app\models\ErpSupplier;
- use app\models\District;
- use app\models\ErpInventory;
- class SupplierForm extends Model
- {
- public $store_id;
- public $id;
- public $ids;
- public $name;
- public $contact;
- public $phone;
- public $province_id;
- public $city_id;
- public $district_id;
- public $address;
- public $logo;
- public $remark;
- public $begin_time;
- public $end_time;
- public $is_delete;
- public $key;
- public $val;
- public $warning;
- public function rules()
- {
- return [
- [['id', 'store_id', 'is_delete', 'province_id', 'city_id', 'district_id'], 'integer'],
- [['ids', 'name', 'address', 'phone', 'contact', 'logo', 'remark'], 'string'],
- [['key', 'val', 'end_time', 'begin_time', 'warning'], 'safe'],
- ];
- }
- public function init() {
- parent::init();
- if(empty($this->store_id)){
- $this->store_id = get_store_id();
- }
- }
- public function selectList ()
- {
- try {
- $is_delete = 0;
- $query = ErpSupplier::find()->where(['is_delete' => $is_delete, 'store_id' => $this->store_id]);
- if($this->warning){
- $warning_num = ERP::getWarningNum($this->store_id);
- $query->andWhere(['id' => ErpInventory::find()->where(['<', 'num', $warning_num])->andWhere(['is_delete' => 0])->distinct(true)->select('supplier_id')]);
- }
- $query->orderBy('id DESC');
- $query->select('id, name');
- $data = $query->asArray()->all();
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $data,
- // 'q' => $query->createCommand()->getRawSql(),
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function search ()
- {
- try {
- $is_delete = 0;
- if ($this->is_delete == 1) {
- $is_delete = 1;
- }
- $query = ErpSupplier::find()->where(['is_delete' => $is_delete, 'store_id' => $this->store_id]);
- if (!empty($this->id)) {
- $query->andWhere(['id' => $this->id]);
- }
- if (!empty($this->supplier_id)) {
- $query->andWhere(['id' => $this->supplier_id]);
- }
- if (!empty($this->name)) {
- $query->andWhere(['like', 'name', $this->name]);
- }
- if (!empty($this->contact)) {
- $query->andWhere(['like', 'contact', $this->contact]);
- }
- if (!empty($this->phone)) {
- $query->andWhere(['like', 'phone', $this->phone]);
- }
- if (!empty($this->begin_time)) {
- $query->andWhere(['>=', 'created_at', strtotime($this->begin_time)]);
- }
- if (!empty($this->end_time)) {
- $query->andWhere(['<=', 'created_at', strtotime($this->end_time)]);
- }
- $query->orderBy('id DESC');
- $pagination = pagination_make($query);
- foreach ($pagination['list'] as &$item) {
- $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
- $item['district'] = implode('', District::find()->where(['id' => [$item['province_id'], $item['city_id'], $item['district_id']]])->select('name')->column());
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $pagination,
- // 'q' => $query->createCommand()->getRawSql(),
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function getInfo()
- {
- try {
- $model = ErpSupplier::find()->where(['id' => $this->id, 'store_id' => $this->store_id])->one();
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $model ?: [],
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage() . $e->getFile() . $e->getLine()
- ];
- }
- }
- public function save ()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $model = ErpSupplier::findOne(['id' => $this->id, 'store_id' => $this->store_id]);
- if (empty($model)) {
- $model = new ErpSupplier();
- $model->store_id = $this->store_id;
- }
- $model->name = $this->name;
- $model->contact = $this->contact;
- $model->logo = $this->logo ?? '';
- $model->phone = $this->phone;
- $model->province_id = (int)$this->province_id;
- $model->city_id = (int)$this->city_id;
- $model->district_id = (int)$this->district_id;
- $model->address = $this->address;
- $model->remark = $this->remark;
- if (!$model->save()) {
- \Yii::error([__METHOD__, $model->attributes]);
- throw new \Exception(array_shift($model->getFirstErrors()));
- }
- $t->commit();
- $province = District::findOne((int)$this->province_id)->name ?? '';
- $city = District::findOne((int)$this->city_id)->name ?? '';
- $district = District::findOne((int)$this->district_id)->name ?? '';
- return [
- 'code' => 0,
- 'msg' => '操作成功!',
- 'data' => [
- 'district' => $province . $city . $district
- ]
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function saveKey ()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $model = ErpSupplier::findOne(['id' => $this->id, 'store_id' => $this->store_id]);
- if (empty($model)) {
- throw new \Exception('参数错误');
- }
- $key = $this->key;
- $model->$key = $this->val;
-
- 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->ids) {
- $ids = explode(',', $this->ids);
- ErpSupplier::updateAll(['is_delete' => 1, 'deleted_at' => time()], ['and', ['in', 'id', $ids], ['store_id' => $this->store_id]]);
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功!'
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
-
- }
|