request->get(); $this->requestValidate($get, [ ['store_id', 'required'] ]); $query = DrugStoreRelations::find()->alias('ds')->where(['ds.store_id' => $get['store_id']]); $query->joinWith(['drug' => function ($d) { $d->andWhere(['type' =>[ 2,4]]); }]); if (!empty($get['status'])) { $query->andWhere(['ds.status' => $get['status']]); } $this->field = [ DrugStoreRelations::class => [ 'id', 'drug_id', 'type' => 'drug.type', 'name' => 'drug.drug_name', 'pinyin_simple' => 'drug.pinyin_simple', 'is_otc' => 'drug.is_otc', 'store_price' => 'price', 'drugstore_price' => 'drugStoreDrug.price', 'market_price' => 'drugStoreDrug.market_price', 'stock' => 'drugStoreDrug.stock', 'buy_price', 'store_status' => 'status', 'drugstore_status' => 'drugStoreDrug.status', 'image' => 'drug.image', 'is_forbid' ] ]; return $this->create($query, $get); } /** * @doc-name 门店设置西药价格 * @doc-param int store_id 门店ID * @doc-param int drug_id 药品ID * @doc-param float price 销售价 / optional */ public function actionUpdatePrice() { $post = \Yii::$app->request->post(); $this->requestValidate($post, [ [['store_id', 'drug_id'], 'required'] ]); $DrugStoreDrug = DrugStoreDrug::find()->where([ 'drug_id' => $post['drug_id'] ])->one(); if (!$DrugStoreDrug) throw new Exception('仓库中没有该药品'); // if ($DrugStoreDrug->type == 1 || $DrugStoreDrug->type == 3) throw new Exception('您不能修改中药或配方颗粒的销售价格'); $DrugStoreRelations = DrugStoreRelations::find()->where([ 'store_id' => $post['store_id'], 'drug_id' => $post['drug_id'], ])->one(); if (!$DrugStoreRelations) throw new Exception('门店没有该药品'); $DrugStoreRelations->price = $post['price'] ?? $DrugStoreRelations->price; if (!$DrugStoreRelations->saveOrFail()) throw new Exception('修改失败'); return ['修改成功']; } /** * @doc-name 门店设置上下架 * @doc-param int store_id 门店ID * @doc-param int drug_id 药品ID * @doc-param int status 1下架2上架 */ public function actionUpdateStatus() { $post = \Yii::$app->request->post(); $this->requestValidate($post, [ [['store_id', 'drug_id', 'status'], 'required'] ]); $DrugStoreRelations = DrugStoreRelations::find()->where([ 'store_id' => $post['store_id'], 'drug_id' => $post['drug_id'], ])->one(); if (!$DrugStoreRelations) throw new Exception('门店中药品不存在'); $DrugStoreRelations->status = $post['status']; if (!$DrugStoreRelations->saveOrFail()) { throw new Exception('修改失败'); } return ['修改成功']; } /** * @doc-name 添加子账号 * @doc-param string mobile 手机号 * @doc-param string role 角色 * @doc-param string password 密码 * @doc-param string username 用户名 * @doc-param int store_id 门店 */ public function actionAddAccount() { $post = \Yii::$app->request->post(); $this->requestValidate($post, [ [['mobile', 'password', 'username','store_id','role'], 'required'] ]); $admin = \Yii::$app->user->identity; $t = \Yii::$app->db->beginTransaction(); try { $is_admin = Admin::find()->where([ 'mobile' => $post['mobile'], 'is_delete' => 0 ])->one(); if ($is_admin) throw new Exception('该账号已注册过'); if (! Store::find()->where(['id'=>$post['store_id']])->one()){ throw new Exception('门店不存在'); } if (! Role::find()->where(['id'=>$post['role']])->one()){ throw new Exception('角色不存在'); } $model = new Admin(); $model->is_sub = 1;//子账号 $model->mobile = $post['mobile']; $model->role = $post['role']; $model->username = $post['username']; $model->store_id = $post['store_id']; $model->setPassword($post['password']); $model->role = $admin->role; $model->store_id = $admin->store_id ?? ''; $model->saveOrFail(); $token = AdminAccessToken::createToken($model->id, 'admin'); $t->commit(); return [ 'token' => $token, 'user' => Admin::findOne($model->uid), ]; } catch (\yii\db\Exception $e) { $t->rollBack(); throw new Exception($e->getMessage()); } } /** * @doc-name 门店设置自己的销售比例 * @doc-param float store_set_sale_z 门店自己设置中药销售比例 * @doc-param float store_set_sale_g 门店自己设置配方颗粒的销售比例 */ public function actionStoreSetPercent() { $post = \Yii::$app->request->post(); $admin = \Yii::$app->user->identity; if ($admin->role != UserRoleEnum::STORE_ADMIN) { throw new Exception('你不是门店管理员'); } $Store = Store::find()->where(['id' => $admin->store_id])->one(); $t = \Yii::$app->db->beginTransaction(); try { $Store->z_sale_percent = $post['store_set_sale_z'] ?? $Store->z_sale_percent; $Store->g_sale_percent = $post['store_set_sale_g'] ?? $Store->g_sale_percent; $Store->store_set_sale_z = $post['store_set_sale_z'] ?? $Store->store_set_sale_z; $Store->store_set_sale_g = $post['store_set_sale_g'] ?? $Store->store_set_sale_g; $Store->saveOrFail(); if (!empty($post['store_set_sale_z'])) { $chinese = DrugStoreDrug::find()->select(['drug_id'])->where(['type' => 1])->column(); $chinese_price = DrugStoreDrug::find()->select(['drug_id', 'market_price','price'])->where(['type' => 1])->all(); $DrugStoreRelations = DrugStoreRelations::find()->select(['id', 'drug_id'])->where([ 'store_id' => $admin->store_id, ])->andWhere(['in','drug_id',$chinese])->all(); foreach ($DrugStoreRelations as $val) { foreach ($chinese_price as $v) { if ($val['drug_id'] == $v['drug_id']) { \Yii::$app->db->createCommand()->update('yii_drug_store_relations', ['price' =>bcdiv(bcmul($post['store_set_sale_z'],$v['price'],4) ,100,4)], ['id' => $val['id']])->execute(); } } } } if (!empty($post['store_set_sale_g'])) { $granular= DrugStoreDrug::find()->select(['drug_id'])->where(['type' => 3])->column(); $granular_price = DrugStoreDrug::find()->select(['drug_id', 'market_price','price'])->where(['type' => 3])->all(); $DrugStoreRelations = DrugStoreRelations::find()->select(['id', 'drug_id'])->where([ 'store_id' => $admin->store_id, ])->andWhere(['in','drug_id',$granular])->all(); foreach ($DrugStoreRelations as $val) { foreach ($granular_price as $v) { if ($val['drug_id'] == $v['drug_id']) { \Yii::$app->db->createCommand()->update('yii_drug_store_relations', ['price' =>bcdiv(bcmul( $post['store_set_sale_g'],$v['price'],4) ,100,4) ], ['id' => $val['id']])->execute(); } } } } $t->commit(); return ['设置成功']; } catch (\Exception $e) { $t->rollBack(); throw new Exception($e->getMessage()); } } /** * @doc-name 设置是否开启查看毛利率 * @doc-param int store_id 门店id */ public function actionSeeRate() { $get = \Yii::$app->request->get(); $this->requestValidate($get,[ ['store_id','required'] ]); $admin = \Yii::$app->user->identity; if ($admin->role != UserRoleEnum::STORE_ADMIN) { throw new Exception('你不是门店管理员'); } $Store = Store::find()->where(['id' => $get['store_id']])->one(); if (!$Store) throw new Exception('诊所不存在'); $t = \Yii::$app->db->beginTransaction(); try { $Store->see_rate=$Store->see_rate==0? 1:0; $Store->saveOrFail(); $t->commit(); return [$Store->see_rate==0?'关闭':'开启']; }catch (\Exception $exception){ $t->rollBack(); throw new $exception; } } /** * @doc-name 门店查看是否开启毛利 * @doc-param int store_id 门店id */ public function actionIsSeeRate() { $get = \Yii::$app->request->get(); $this->requestValidate($get,[ ['store_id','required'] ]); $admin = \Yii::$app->user->identity; if ($admin->role != UserRoleEnum::STORE_ADMIN) { throw new \yii\db\Exception('你不是门店管理员'); } $Store = Store::find()->where(['id' => $get['store_id']])->one(); if (!$Store) throw new \yii\base\Exception('诊所不存在'); return $Store; } /** * @doc-name 平台控制门店西药是否禁售 * @doc-param int store_id 门店id * @doc-param int drug_id 药品id */ public function actionSetDrugStatus() { $get = \Yii::$app->request->get(); $this->requestValidate($get,[ [['store_id','drug_id'],'required'] ]); $Drug=Drug::find()->where(['id'=>$get['drug_id']])->one(); if (!$Drug) throw new Exception('药品不存在'); $DrugStoreRelations=DrugStoreRelations::find()->where(['store_id'=>$get['store_id'],'drug_id'=>$get['drug_id']])->one(); if (!$DrugStoreRelations) throw new Exception('门店没有该药品'); $DrugStoreRelations->is_forbid=$DrugStoreRelations->is_forbid==0?1 :0; if ($DrugStoreRelations->is_forbid==1){//禁售 $DrugStoreRelations->status=1;//下架 }else{//在售 $DrugStoreRelations->status=2;//上架 } $DrugStoreRelations->saveOrFail(); return [ $DrugStoreRelations->is_forbid==1?'禁售':'在售']; } /** * @doc-name 查看门店信息 */ public function actionSeeStorePercent() { $get = \Yii::$app->request->get(); $this->requestValidate($get,[ ['store_id','required'] ]); $Store = Store::find()->select(['z_buy_percent','z_sale_percent','g_bug_percent','g_sale_percent'])->where(['id' => $get['store_id'],'is_delete'=>0])->one(); if (!$Store) throw new Exception('门店不存在'); return $Store; } }