diff --git a/service/modules/v1/controllers/PrescriptionController.php b/service/modules/v1/controllers/PrescriptionController.php index aaad791..ed5fb88 100644 --- a/service/modules/v1/controllers/PrescriptionController.php +++ b/service/modules/v1/controllers/PrescriptionController.php @@ -1084,7 +1084,7 @@ class PrescriptionController extends BaseAppController if($repice[0]['dosage'] > 7){ $doctor_order[] = '患者需长期使用此药,开具超七天用量'; } - if($param['doctor_second_sign'] == 1){ + if($param['doctor_second_sign']?? 0 == 1){ $prescription->doctor_second_sign = 1; } $doctor_order[] = $param['doctor_order']; @@ -1250,6 +1250,48 @@ class PrescriptionController extends BaseAppController return $drug; } + /** + * @doc-name 产品服务包列表 + * @doc-param string name 药名 + */ + public function actionServerList() + { + // TODO 正在搞这个 + $name = \Yii::$app->request->post('name'); + $store_id =\Yii::$app->request->post('store_id'); + + if(!$name){ + $name = ''; + } + $drug = Drug::find()->alias('d')->select('d.*')->where([ + 'd.type' => 5 + ])->andWhere([ + 'or', + ['like', 'd.drug_name', '%' . $name . "%", false], + ['like', 'd.pinyin_simple', '%' . $name . "%", false], + ['like', 'd.drug_alias', '%' . $name . "%", false] + ])->with('unit')->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()->all(); + + if (!$drug) { + throw new Exception('没有找到您要搜索的药品'); + } + + foreach($drug as $k=>$v){ + $drug[$k]['buy_price'] = $v['drugStoreRelation']['buy_price']; + $drug[$k]['price'] = $v['drugStoreRelation']['price']; + $drug[$k]['stock'] = $v['drugStoreDrug']['stock']; + unset($drug[$k]['drugStoreRelation']); + unset($drug[$k]['drugStoreDrug']); + } + + return $drug; + } + /** * @doc-name 一次添加一个中药 * @doc-param int drug_id 药id @@ -1667,6 +1709,268 @@ class PrescriptionController extends BaseAppController } } + /** + * @doc-name 产品服务包开方 + * @doc-param string clinical_diagnose 临床诊断 + * @doc-param int up_id 就诊人id + * @doc-param string doctor_order 医嘱 + * @doc-param int category 类别1自费2医保 + * @doc-param string cr_ids 拼接药方id + */ + public function actionAddPrescriptionProductServicePackage() + { + // TODO 该功能未完成 + $id = \Yii::$app->user->identity->id; + + $param = \Yii::$app->request->post(); + $this->requestValidate($param, [ + ['store_id', 'required'], + ['register_id', 'required'], + ['clinical_diagnose', 'required'], + ['up_id', 'required'], + ['category', 'required'], + ['doctor_order', 'required'], + ['gr_ids', 'required'], + ['treatement_price', 'number'] + ]); + + $transaction = \Yii::$app->db->beginTransaction(); + try { + $store_id = $param['store_id']; + + /* @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('就诊人不存在'); + } + + $register = Register::find()->where([ + 'id' => $param['register_id'], + 'user_patient_id' => $param['up_id'], + 'store_id' => $store_id + ])->with('healthInquery')->one(); + if(!$register){ + throw new Exception('挂号订单不存在'); + } + + $prescription_no = 'GY' . 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(); + 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); + } + } + + $priceTotal = round($priceTotal, 2); //对药品总价进行四舍五入 + + + //组装处方快照 + $repice = GranularRepice::find()->where(['in', 'id', $ids])->asArray()->all(); + $prescription_content['prescription_no'] = $prescription_no; + $prescription_content['repice'] = $repice; + $prescription_content['created_at'] = date('Y-m-d',time()); + $prescription_content['doctor_order'] = $param['doctor_order']; + $prescription_content['clinical_diagnose'] = $param['clinical_diagnose']; + $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['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(); + + + + $prescription = new Prescription(); + $prescription->store_id = $store_id; + $prescription->register_id = $param['register_id']; + $prescription->prescription_no = $prescription_no; + $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->content = Json::encode($prescription_content); + $prescription->prescription_type = 3; // 颗粒药 + $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){ + $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->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->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->order_type = 1; + $productOrder->prescription_type = 3;//颗粒药处方 + $productOrder->type = $payConfig->pay_type??2;//1微信 2易票联 + $productOrder->is_pay = 0; + $productOrder->trans_expenses = 0; + $productOrder->items_price = $priceTotal; + $productOrder->market_price = $marketPrice; + $productOrder->process_price = $processTotal; + $treatementPrice = 0; + if($param['treatement_price']){ + $treatementPrice = $param['treatement_price']; + } + $productOrder->treatement_price = $treatementPrice; + + $productOrder->status = ProductOrderEnum::UNPAY; + $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']; + $productOrder->address = Json::encode($userAddress[0]); + $productOrder->express_name = $userAddress[0]['name']; + $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; + } + $productOrder->total_pay_price = $priceTotal + $processTotal + $treatementPrice; + $productOrder->pay_method = 1; + $productOrder->free_ship = 1; + $productOrder->sync_order_no = $param['sync_order_no']??''; + $productOrder->saveOrFail(); + + $productOrderItem = new ProductOrderItems(); + foreach ($drugStoreDrugs as $v) { + $item = clone $productOrderItem; + $item->product_order_id = $productOrder->id; + $item->drug_id = $v['id']; + $item->drug_image = $v['image']; + $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->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; + \Yii::$app->trigger(ProductOrder::EVENT_CREATED, $event); + + //处方开具待支付订阅消息通知 + \Yii::$app->queue->push(new PrescriptionCreated([ + 'orderId' => $prescription->id, + ])); + + //发送处方系统通知 + $systemNotice = new SystemNotice(); + $systemNotice->data = $prescription_no; + $systemNotice->store_id = $store_id; + $systemNotice->content = '医生已为您开具处方,请及时查看!'; + $systemNotice->base_type = 4;//处方通知 + $systemNotice->scene_type = 1; + $systemNotice->user_id = $userPatient->user_id; + $systemNotice->notice_at = date('Y-m-d H:i:s',time()); + $systemNotice->saveOrFail(); + + $transaction->commit(); + + return [ + 'id' => $prescription->id, + 'product_order_id' => $productOrder->id, + 'prescription_no' => $prescription_no, + 'issue_time' => time(), + 'clinical_diagnose' => $param['clinical_diagnose'] + ]; + } catch (Exception $e) { + $transaction->rollBack(); + throw $e; + } + } + /** * 常用诊断 */