From 3254cb68290db6b3297aa1f86d72c3ba7e9934ff Mon Sep 17 00:00:00 2001 From: liqi Date: Sat, 21 Dec 2024 16:34:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=A7=E5=93=81=E6=9C=8D=E5=8A=A1=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/services/UploadService.php | 1 - .../v1/controllers/PrescriptionController.php | 250 +++++++++++------- 2 files changed, 152 insertions(+), 99 deletions(-) diff --git a/common/services/UploadService.php b/common/services/UploadService.php index c489ac5..1eafdcb 100644 --- a/common/services/UploadService.php +++ b/common/services/UploadService.php @@ -7,7 +7,6 @@ use OSS\Core\OssException; use OSS\OssClient; use yii\base\Exception; use yii\web\UploadedFile; -use Yii; class UploadService{ diff --git a/service/modules/v1/controllers/PrescriptionController.php b/service/modules/v1/controllers/PrescriptionController.php index 670a312..9c40d3b 100644 --- a/service/modules/v1/controllers/PrescriptionController.php +++ b/service/modules/v1/controllers/PrescriptionController.php @@ -27,6 +27,7 @@ use common\models\oldDb\GranularRepice; use common\models\oldDb\Prescription; use common\models\oldDb\PrescriptionChinese; use common\models\oldDb\PrescriptionGranular; +use common\models\oldDb\PrescriptionServer; use common\models\oldDb\PrescriptionWest; use common\models\oldDb\ProcessRule; use common\models\oldDb\ProcessRuleNote; @@ -34,6 +35,7 @@ use common\models\oldDb\ProductOrder; use common\models\oldDb\ProductOrderItems; use common\models\oldDb\Region; use common\models\oldDb\Register; +use common\models\oldDb\ServicePackageRepice; use common\models\oldDb\SystemConfig; use common\models\oldDb\SystemNotice; use common\models\oldDb\UserPatient; @@ -63,7 +65,7 @@ class PrescriptionController extends BaseAppController throw new Exception('无权限进行该操作'); } - $westActions = ['actionProcessRuleList','actionProcessNoteList','actionWestList', 'actionWestCommon','actionServicePackageList', 'actionServicePackageCommon', 'actionWestCollection', 'actionWestUncollect', 'actionAddWest', 'actionAddPrescriptionWest','actionUseTime','actionUseType','actionUseFre','actionSearchDisease','actionDiseaseCommon','actionAddDiseaseCommon','actionDelDiseaseCommon','actionWestUnit','actionPrescriptionDetail']; + $westActions = ['actionProcessRuleList','actionProcessNoteList','actionWestList', 'actionWestCommon','actionServicePackageList', 'actionServicePackageCommon', 'actionUseNum', 'actionDosage', 'actionWestCollection', 'actionWestUncollect', 'actionAddWest', 'actionAddPrescriptionWest','actionUseTime','actionUseType','actionUseFre','actionSearchDisease','actionDiseaseCommon','actionAddDiseaseCommon','actionDelDiseaseCommon','actionWestUnit','actionPrescriptionDetail']; if($doctorInfo->identity == 2 && !in_array($action->actionMethod, $westActions)){ throw new Exception('您当前并未拥有开具此方的权限'); } @@ -1277,7 +1279,7 @@ class PrescriptionController extends BaseAppController }])->asArray()->all(); if (!$drug) { - throw new Exception('没有找到您要搜索的药品'); + throw new Exception('没有找到您要搜索的服务包'); } foreach($drug as $k=>$v){ @@ -1291,6 +1293,65 @@ class PrescriptionController extends BaseAppController return $drug; } + + /** + * @doc-name 添加产品服务包 + */ + public function actionAddServicePackageRecipe() + { + $param = \Yii::$app->request->post(); + $this->requestValidate($param, [ + ['store_id', 'required'], + ['content', 'required'], + ['number', 'required'], + ]); + try { + $store_id = $param['store_id']; + $transaction = \Yii::$app->db->beginTransaction(); + + $drug = Drug::find()->alias('d')->select('d.*')->where([ + 'd.id' => $param['content'] + ])->andWhere([ + 'in','d.type', [5] + ])->joinWith(['drugStoreDrug dsd' => function($q){ + $q->andWhere(['dsd.status' => 2]); + }])->joinWith(['drugStoreRelation dsr' => function ($q) use ($store_id){ + $q->andWhere(['dsr.store_id' => $store_id]); + $q->andWhere(['dsr.status' => 2]); + }])->asArray()->one(); + + $price = $drug['drugStoreRelation']['price']; + $drug['buy_price'] = $drug['drugStoreRelation']['buy_price']; + $drug['price'] = $price; + $drug['stock'] = $drug['drugStoreDrug']['stock']; + unset($drug['drugStoreRelation']); + unset($drug['drugStoreDrug']); + + foreach ($param as $k => $v) { + if (empty($v)) { + $param[$k] = 0; + } + } + $usage_dosage = Json::encode($drug); + unset($param['content']); + $WestRecipe = new ServicePackageRepice(); + $WestRecipe->content = $usage_dosage; + $WestRecipe->total_price = bcmul($param['number'], $price,2); + $WestRecipe->attributes = $param; +// ds([ +// $WestRecipe->attributes, +// $param +// ]); + $WestRecipe->saveOrFail(); + + $transaction->commit(); + return [$WestRecipe->id]; + } catch (Exception $e) { + $transaction->commit(); + throw $e; + } + } + /** * @doc-name 一次添加一个中药 * @doc-param int drug_id 药id @@ -1322,7 +1383,7 @@ class PrescriptionController extends BaseAppController if(!$drug){ throw new Exception('药品不存在'); } - if ($param['number'] > $drug['drugStoreDrug']['stock']) throw new Exception('库存不够'); +// if ($param['number'] > $drug['drugStoreDrug']['stock']) throw new Exception('库存不够'); $GranularMedicine = new GranularMedicine(); $GranularMedicine->drug_id = $param['drug_id']; @@ -1442,6 +1503,7 @@ class PrescriptionController extends BaseAppController } } + /** * @doc-name 开颗粒药处方 * @doc-param string clinical_diagnose 临床诊断 @@ -1716,20 +1778,21 @@ class PrescriptionController extends BaseAppController * @doc-param int category 类别1自费2医保 * @doc-param string cr_ids 拼接药方id */ - public function actionAddPrescriptionProductServicePackage() + public function actionAddPrescriptionServicePackage() { // TODO 该功能未完成 + $id = \Yii::$app->user->identity->id; $param = \Yii::$app->request->post(); $this->requestValidate($param, [ ['store_id', 'required'], ['register_id', 'required'], + ['doctor_order', 'required'], ['clinical_diagnose', 'required'], ['up_id', 'required'], ['category', 'required'], - ['doctor_order', 'required'], - ['gr_ids', 'required'], + ['wr_ids', 'required'], ['treatement_price', 'number'] ]); @@ -1737,7 +1800,7 @@ class PrescriptionController extends BaseAppController try { $store_id = $param['store_id']; - /* @var UserPatient $userPatient */ + /* @var UserPatient $userPatient */ $userPatient = UserPatient::find()->select('id,name,sex,id_card,user_id,mobile')->where(['id' => $param['up_id']])->one(); if(!$userPatient){ throw new Exception('就诊人不存在'); @@ -1752,56 +1815,53 @@ class PrescriptionController extends BaseAppController throw new Exception('挂号订单不存在'); } - $prescription_no = 'GY' . rand(111111, 999999) . time(); + $prescription_no = 'XY' . rand(111111, 999999) . time(); $marketPrice = '0'; $priceTotal = 0; - $processTotal = 0; - $ids = explode(',', $param['gr_ids']); - for ($i = 0; $i < sizeof($ids); $i++) { - $recipe = GranularRepice::find()->where(['id' => $ids[$i]])->asArray()->one(); + $wr_ids = explode(',', $param['wr_ids']); + $drugStoreDrugs = []; + for ($i = 0; $i < sizeof($wr_ids); $i++) { + $recipe = ServicePackageRepice::find()->where(['id' => $wr_ids[$i]])->asArray()->one(); if(!$recipe){ throw new Exception('药方数据错误'); } $priceTotal += $recipe['total_price']; - $processTotal += $recipe['process_price']; $content = Json::decode($recipe['content']); - for ($j = 0; $j < sizeof($content); $j++) { - $drug = Drug::find()->alias('d')->select('d.*')->where([ - 'd.id' => $content[$j]['drug_id'] - ])->joinWith(['drugStoreDrug dsd' => function($q){ - $q->andWhere(['dsd.status' => 2]); - }])->joinWith(['drugStoreRelation dsr' => function ($q) use ($store_id){ - $q->andWhere(['dsr.store_id' => $store_id]); - $q->andWhere(['dsr.status' => 2]); - }])->asArray()->one(); - if(!$drug){ - throw new Exception('药品库存数据错误'); - } - $drugNumber = (int) $content[$j]['number'] * (int) $recipe['dosage']; - if($drug['drugStoreDrug']['stock']<$drugNumber){ - throw new Exception($content['name'].'库存数量不足'); - } - \Yii::$app->db->createCommand()->update('yii_drug_store_relations', [ - 'sale_number' => $drug['drugStoreRelation']['sale_number'] + $drugNumber - ], ['id' => $drug['drugStoreRelation']['id']])->execute(); - \Yii::$app->db->createCommand()->update('yii_drugstore_drug', [ - 'stock' => $drug['drugStoreDrug']['stock'] - $drugNumber, - 'frozen_number' => $drug['drugStoreDrug']['frozen_number'] + $drugNumber - ], ['id' => $drug['drugStoreDrug']['id']])->execute(); - $drug['number'] = $drugNumber; - $drugStoreDrugs[] = $drug; - $itemMarketPrice = bcmul($drugNumber,$drug['drugStoreRelation']['buy_price'],2); - $marketPrice = bcadd($marketPrice,$itemMarketPrice,2); + $drug = Drug::find()->alias('d')->select('d.*')->where([ + 'd.id' => $content['id'] + ])->joinWith(['drugStoreDrug dsd' => function($q){ + $q->andWhere(['dsd.status' => 2]); + }])->joinWith(['drugStoreRelation dsr' => function ($q) use ($store_id){ + $q->andWhere(['dsr.store_id' => $store_id]); + $q->andWhere(['dsr.status' => 2]); + }])->asArray()->one(); + if(!$drug){ + throw new Exception('药品库存数据错误'); } + + if($drug['drugStoreDrug']['stock']<$recipe['number']){ + throw new Exception($content['drug_name'].'库存数量不足'); + } + \Yii::$app->db->createCommand()->update('yii_drug_store_relations', [ + 'sale_number' => $drug['drugStoreRelation']['sale_number'] + $recipe['number'] + ], ['id' => $drug['drugStoreRelation']['id']])->execute(); + \Yii::$app->db->createCommand()->update('yii_drugstore_drug', [ + 'stock' => $drug['drugStoreDrug']['stock'] - $recipe['number'], + 'frozen_number' => $drug['drugStoreDrug']['frozen_number'] + $recipe['number'] + ], ['id' => $drug['id']])->execute(); + $drug['number'] = $recipe['number']; + $drugStoreDrugs[] = $drug; + $itemMarketPrice = bcmul($recipe['number'],$drug['drugStoreRelation']['buy_price'],2); + $marketPrice = bcadd($marketPrice,$itemMarketPrice,2); } $priceTotal = round($priceTotal, 2); //对药品总价进行四舍五入 - //组装处方快照 - $repice = GranularRepice::find()->where(['in', 'id', $ids])->asArray()->all(); + $wr_ids = explode(',', $param['wr_ids']); + $repice = ServicePackageRepice::find()->where(['in', 'id', $wr_ids])->with(['usetime', 'usetype', 'frequency', 'westUnit'])->asArray()->all(); $prescription_content['prescription_no'] = $prescription_no; $prescription_content['repice'] = $repice; $prescription_content['created_at'] = date('Y-m-d',time()); @@ -1810,27 +1870,23 @@ class PrescriptionController extends BaseAppController $prescription_content['category'] = $param['category']==1?'自费':'医保'; $prescription_content['patient'] = $userPatient; $prescription_content['patient']['age'] = FuncHelper::getAgeFromIdNo($userPatient->id_card); - $prescription_content['doctor'] = DoctorInfo::find()->select('su_id,name,depart_id,title_id')->where(['su_id' => $id])->with(['depart','title'])->asArray()->one(); + $prescription_content['doctor'] = DoctorInfo::find()->select('name,depart_id,title_id')->where(['su_id' => $id])->with(['depart','title'])->asArray()->one(); $prescription_content['total_pay_price'] = $priceTotal; - - - $prescriptionGranular = new PrescriptionGranular(); - $prescriptionGranular->store_id = $store_id; - $prescriptionGranular->prescription_no = $prescription_no; - $prescriptionGranular->su_id = $id; - $prescriptionGranular->user_id = $userPatient->user_id; - $prescriptionGranular->up_id = $param['up_id']; - $prescriptionGranular->status = 1; - $prescriptionGranular->type = 1;//普通方 - $prescriptionGranular->content = Json::encode($prescription_content); - $prescriptionGranular->category = $param['category']; - $prescriptionGranular->doctor_order = $param['doctor_order']; - $prescriptionGranular->clinical_diagnose = $param['clinical_diagnose']; - $prescriptionGranular->gr_ids = $param['gr_ids']; - $prescriptionGranular->saveOrFail(); - - + $prescriptionWest = new PrescriptionServer(); + $prescriptionWest->store_id = $store_id; + $prescriptionWest->prescription_no = $prescription_no; + $prescriptionWest->su_id = $id; + $prescriptionWest->user_id = $userPatient->user_id; + $prescriptionWest->up_id = $param['up_id']; + $prescriptionWest->status = 1; + $prescriptionWest->type = 1;//普通方 + $prescriptionWest->content = Json::encode($prescription_content); + $prescriptionWest->category = $param['category']; + $prescriptionWest->doctor_order = $param['doctor_order']; + $prescriptionWest->clinical_diagnose = $param['clinical_diagnose']; + $prescriptionWest->wr_ids = $param['wr_ids']; + $prescriptionWest->saveOrFail(); $prescription = new Prescription(); $prescription->store_id = $store_id; @@ -1839,60 +1895,57 @@ class PrescriptionController extends BaseAppController $prescription->su_id = $id; $prescription->user_id = $userPatient->user_id; $prescription->up_id = $param['up_id']; - $prescription->status = 0; $prescription->type = 1;//普通方 $prescription->is_online = 0; + $prescription->status = 3; $prescription->content = Json::encode($prescription_content); - $prescription->prescription_type = 3; // 颗粒药 + $prescription->prescription_type = 5;//服务包 $prescription->category = $param['category']; - $prescription->process_price = $processTotal; - $prescription->process_rule_id = $repice[0]['process_rule_id']; - $prescription->process_rule = $repice[0]['process_rule']; - $prescription->process_rule_note = $repice[0]['process_rule_note']; $over_time = \Yii::$app->params['prescription']['over_time']; $doctor_order = []; if(date('H')>=16){ $doctor_order[] = '该处方有效期延长为三天内有效'; $over_time = 72*3600; } - if($repice[0]['dosage'] > 7){ + if($repice[0]['dosage']?? 0 > 7){ $doctor_order[] = '患者需长期使用此药,开具超七天用量'; } $doctor_order[] = $param['doctor_order']; $prescription->valid_hours = $over_time/3600; $prescription->doctor_order = implode('|',$doctor_order); $prescription->clinical_diagnose = $param['clinical_diagnose']; - $prescription->gr_ids = $param['gr_ids']; + $prescription->wr_ids = $param['wr_ids']; $prescription->total_pay_price = $priceTotal; $prescription->saveOrFail(); //获取支付方式配置 $payConfig = PayConfig::findOne(['status' => 1, 'current_use' => 1]); - // 生成商品订单 $productOrder = new ProductOrder(); - $productOrder->store_id = $store_id; $productOrder->su_id = $id; + $productOrder->store_id = $store_id; $productOrder->user_id = $userPatient->user_id; $productOrder->up_id = $userPatient->id; $productOrder->order_no = Carbon::now()->format('YmdHis') . rand(10000, 99999) . rand(10000, 99999); $productOrder->p_id = $prescription->id; - $productOrder->dosage = $repice[0]['dosage']; + $productOrder->dosage = 0; + $productOrder->prescription_type = 5; // 服务包 $productOrder->order_type = 1; - $productOrder->prescription_type = 3;//颗粒药处方 - $productOrder->type = $payConfig->pay_type??2;//1微信 2易票联 + $productOrder->type = $payConfig->pay_type??0;//0未知 1微信 2易票联 $productOrder->is_pay = 0; $productOrder->trans_expenses = 0; $productOrder->items_price = $priceTotal; $productOrder->market_price = $marketPrice; - $productOrder->process_price = $processTotal; + $productOrder->status = ProductOrderEnum::UNPAY; $treatementPrice = 0; if($param['treatement_price']){ $treatementPrice = $param['treatement_price']; } $productOrder->treatement_price = $treatementPrice; - $productOrder->status = ProductOrderEnum::UNPAY; + + $productOrder->is_free_shipping = 0;//是否包邮 0不包邮 1包邮 + $userAddress = Address::find()->select('id,name,mobile,province,region,detail_address')->where(['user_id' => $prescription->user_id])->orderBy('is_default DESC')->asArray()->all(); if(count($userAddress)>0){ $productOrder->address_id = $userAddress[0]['id']; @@ -1901,11 +1954,18 @@ class PrescriptionController extends BaseAppController $productOrder->express_mobile = $userAddress[0]['mobile']; $productOrder->express_region = $userAddress[0]['region']; $productOrder->express_address = $userAddress[0]['detail_address']; - $region = Region::find()->where(['name' => $userAddress[0]['province']])->one(); - $productOrder->trans_expenses = $region->express_fee; - $priceTotal = $priceTotal + $region->express_fee; + //判断是否满足包邮条件 + $systemConfig = SystemConfig::find()->where(['config_type' => 2, 'type' => 2])->one();//西药包邮条件 + if($priceTotal < $systemConfig->value){//不满足条件计算快递费 + $region = Region::find()->where(['name' => $userAddress[0]['province']])->one(); + $productOrder->trans_expenses = $region->express_fee; + $priceTotal = $priceTotal + $region->express_fee; + } else { + $productOrder->is_free_shipping = 1; //1包邮 + } } - $productOrder->total_pay_price = $priceTotal + $processTotal + $treatementPrice; + + $productOrder->total_pay_price = $priceTotal + $treatementPrice; $productOrder->pay_method = 1; $productOrder->free_ship = 1; $productOrder->sync_order_no = $param['sync_order_no']??''; @@ -1920,20 +1980,14 @@ class PrescriptionController extends BaseAppController $item->drug_no = $v['drug_number']; $item->number = $v['number']; $item->type = $v['type']; - $item->price = $v['drugStoreRelation']['price']; $item->buy_price = $v['drugStoreRelation']['buy_price']; + $item->price = $v['drugStoreRelation']['price']; $item->drug_name = $v['drug_name']; $item->small_info = $v['small_info']; $item->saveOrFail(); } - //触发处方自动失效事件 - $prescriptionEvent = new PrescriptionEvent(); - $prescriptionEvent->prescription = $prescription; - $prescriptionEvent->sender = $this; - \Yii::$app->trigger(Prescription::AUTO_EXPIRE, $prescriptionEvent); - - //触发订单自动取消事件 + //触发订单创建事件 $event = new ProductOrderEvent(); $event->order = $productOrder; $event->sender = $this; @@ -1964,9 +2018,9 @@ class PrescriptionController extends BaseAppController 'issue_time' => time(), 'clinical_diagnose' => $param['clinical_diagnose'] ]; - } catch (Exception $e) { + } catch (Exception $e) { $transaction->rollBack(); - throw $e; + throw $e; } } @@ -2135,7 +2189,7 @@ class PrescriptionController extends BaseAppController ])->joinWith([ 'drug d' => function($q){ $q->andWhere([ - 'in','d.type', [2,4] + 'in','d.type',[5] ]); } ])->joinWith([ @@ -2152,7 +2206,7 @@ class PrescriptionController extends BaseAppController $collect[] = $drug; } $unCollect = Drug::find()->alias('d')->select('d.*')->where([ - 'in','d.type',[2,4] + 'in','d.type', [5] ])->andWhere([ 'not in', 'd.id', $ids ])->with('drugStoreDrug')->joinWith(['drugStoreRelation dsr' => function ($q) use ($store_id){ @@ -2170,7 +2224,7 @@ class PrescriptionController extends BaseAppController } $unCollect = Drug::find()->alias('d')->select('d.*')->where([ - 'in','d.type', [2,4] + 'in','d.type', [5] ])->joinWith(['drugStoreDrug dsd' => function($q){ $q->andWhere(['dsd.status' => 2]); }])->joinWith(['drugStoreRelation dsr' => function ($q) use ($store_id){ @@ -2189,7 +2243,7 @@ class PrescriptionController extends BaseAppController } //搜索 $allDrug = Drug::find()->alias('d')->select('d.*')->where([ - 'in','d.type', [2,4] + 'in','d.type', [5] ])->andWhere([ 'or', ['like', 'd.drug_name', '%' . $name . "%", false], @@ -2222,7 +2276,7 @@ class PrescriptionController extends BaseAppController ])->joinWith([ 'drug d' => function ($q){ $q->andWhere([ - 'in','d.type', [2,4] + 'in','d.type', [5] ]); } ])->joinWith([ @@ -2244,7 +2298,7 @@ class PrescriptionController extends BaseAppController $no_collect = Drug::find()->alias('d')->select('d.*')->where([ 'in', 'd.id', $ids ])->andWhere([ - 'in','d.type', [2,4] + 'in','d.type', [5] ])->joinWith(['drugStoreDrug dsd' => function($q){ $q->andWhere(['dsd.status' => 2]); }])->joinWith(['drugStoreRelation dsr' => function ($q) use ($store_id){ @@ -2262,7 +2316,7 @@ class PrescriptionController extends BaseAppController $collected = Drug::find()->alias('d')->select('d.*')->where([ 'in', 'd.id', $is_collect ])->andWhere([ - 'in','d.type', [2,4] + 'in','d.type', [5] ])->joinWith(['drugStoreDrug dsd' => function($q){ $q->andWhere(['dsd.status' => 2]); }])->joinWith(['drugStoreRelation dsr' => function ($q) use ($store_id){ @@ -2301,7 +2355,7 @@ class PrescriptionController extends BaseAppController ])->joinWith([ 'drug d' => function ($q) use ($name){ $q->andWhere([ - 'in','d.type', [2,4] + 'in','d.type',[5] ]); if($name){ $q->andWhere([