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() ]; } } }