Files
xk-api-yii/service/modules/v1/controllers/ExamineController.php

205 lines
8.4 KiB
PHP
Raw Normal View History

2024-09-19 11:32:39 +08:00
<?php
namespace service\modules\v1\controllers;
use common\core\BasePharmacistController;
use common\core\sms\PrescriptionPassMessage;
use common\events\ProductOrderEvent;
use common\forms\ProductCancelForm;
use common\forms\ProductRefundForm;
use common\jobs\Sms\PrescriptionPassMessageJob;
use common\jobs\Sms\PrescriptionRefuseMessageJob;
use common\models\Department;
use common\models\ExamineLog;
use common\models\Store;
use common\models\DoctorInfo;
use common\models\DrugUseWay;
use common\models\PharmacistrInfo;
use common\models\Prescription;
use common\models\ProductOrder;
use common\models\SystemNotice;
use common\services\PrescriptionService;
use yii\base\Exception;
/**
* @doc-module 药师审核处方
*/
class ExamineController extends BasePharmacistController
{
/**
* @doc-name 中药的使用煎熬方式
* @doc-return mixed @drug-use-way{*}
*/
public function actionUseWay()
{
return DrugUseWay::find()->all();
}
/**
* @doc-name 查看处方
* @doc-param int prescription_id 处方id
* @doc-return string issue_date 开方时间
* @doc-return mixed @PrescriptionWest{*,@UserPatient{*},@DoctorInfo{*,@Department{*}},@PharmacistInfo{name-string-药师}} 西药处方
* @doc-return mixed @PrescriptionChinese{*,@UserPatient{*},@DoctorInfo{*,@Department{*}},@PharmacistInfo{name-string-药师}} 中药处方
* @doc-return mixed @WestRepice{*,@DrugUseTime{*},@DrugUseType{*},@DrugUseFrequency{*},@WestUnit{*}} 西药方
* @doc-return mixed @ChineseRepice{*,@DrugUseTime{*},@DrugUseNum{*}} 中药方
* @doc-return float total_price 金额
*/
public function actionDetail()
{
$pharmacistrInfo = PharmacistrInfo::find()->where(['su_id' => \Yii::$app->user->identity->getId()])->one();
if (!$pharmacistrInfo) {
throw new Exception('药师数据错误');
}
$prescription_no = \Yii::$app->request->get('prescription_no');
if (!$prescription_no) {
throw new Exception('处方no不能为空');
}
return (new PrescriptionService())->detail(\Yii::$app->request->get('prescription_no'));
}
/**
* @doc-name 处方列表
* @doc-param int status 状态0待审核 1已审核通过 2未通过
* @doc-return mixed @PrescriptionChinese{*,@UserPatient{*}} 中药处方
* @doc-return mixed @PrescriptionWest{*,@UserPatient{*}} 中药处方
*/
public function actionList()
{
$pharmacistrInfo = PharmacistrInfo::find()->where(['su_id' => \Yii::$app->user->identity->getId()])->one();
if (!$pharmacistrInfo) {
throw new Exception('药师数据错误');
}
$status = \Yii::$app->request->get('status');
if($status > 0){ //已审核或未通过审核
$prescription = Prescription::find()->where(['status' => $status, 'pharmacist_id' => \Yii::$app->user->identity->id])->with('patients')->orderBy('created_at DESC')->asArray()->all();
} else {
$prescription = Prescription::find()->where(['status' => $status])->with('patients')->orderBy('created_at DESC')->asArray()->all();
}
if (!$prescription) {
throw new Exception('暂无数据');
}
return [
'prescription' => $prescription
];
}
/**
* @doc-name 处方审核
* @doc-param int prescription_id 处方id
* @doc-param int status 状态 1已通过 2拒绝
*/
public function actionVerify()
{
$pharmacistrInfo = PharmacistrInfo::find()->where(['su_id' => \Yii::$app->user->identity->getId()])->one();
if (!$pharmacistrInfo) {
throw new Exception('药师数据错误');
}
//处方号
$prescription_no = \Yii::$app->request->post('prescription_no');
if (!$prescription_no) {
throw new Exception('处方号不能为空');
}
//处方状态
$status = \Yii::$app->request->post('status');
if (!in_array($status, [1, 2])) {
throw new Exception('审核状态错误');
}
$prescription = Prescription::find()->where(['prescription_no' => $prescription_no])->one();
if(!$prescription) throw new Exception('处方不存在');
$t = \Yii::$app->db->beginTransaction();
try {
$prescription->pharmacist_id = \Yii::$app->user->identity->id;
$prescription->status = $status;
$prescription->reject_view = $status == 2 ? \Yii::$app->user->identity->id:0;
$prescription->reject_reason = $status == 2 ? \Yii::$app->request->post('reject_reason'):'';
$prescription->reject_time = $status == 2 ? time():0;
$prescription->pharmacist_view_time = time();
$prescription->saveOrFail();
if($status == 2){ //审批不通过,已支付订单需要退款
$productOrder = ProductOrder::find()->where(['p_id' => $prescription->id])->one();
if($productOrder->status == 0){//待支付订单需要取消
$form = new ProductCancelForm();
$form->cancel(['user_id' =>$productOrder->user_id, 'order_id' => $productOrder->id]);
} elseif ($productOrder->status == 1){//已支付订单需要退款
$form = new ProductRefundForm();
$form->refund(['user_id' =>$productOrder->user_id, 'order_id' => $productOrder->id], 'auto');
}
}else{
//中药处方订单同步江奥川erp
if($prescription->prescription_type == 1){
$productOrder = ProductOrder::find()->where(['p_id' => $prescription->id, 'status' => 1])->one();
if($productOrder){
//触发订单支付事件同步江奥川ERP
$event = new ProductOrderEvent();
$event->order = $productOrder;
$event->sender = $this;
\Yii::$app->trigger(ProductOrder::EVENT_PAYED, $event);
}
}
}
$systemNotice = new SystemNotice();
$systemNotice->store_id = $prescription->store_id;
$systemNotice->user_id = $prescription->su_id;
$systemNotice->data = $prescription->id;
$systemNotice->content = $status==1?'您开具的处方已通过审核!':'您开具的处方未通过审核,原因是:'.\Yii::$app->request->post('reject_reason');
$systemNotice->base_type = 5;
$systemNotice->scene_type = 2;// 医生
$systemNotice->notice_at = date('Y-m-d H:i:s');
$systemNotice->saveOrFail();
$t->commit();
//处方生成短信通知药师审方
if($status == 2){
\Yii::$app->queue->push(new PrescriptionRefuseMessageJob([
'orderId' => $prescription->id,
]));
} else {
\Yii::$app->queue->push(new PrescriptionPassMessageJob([
'orderId' => $prescription->id,
]));
}
} catch (\Exception $e) {
$t->rollBack();
throw new Exception($e->getMessage());
}
return ['审核成功'];
}
/**
* @doc-name 处方溯源
* @doc-param int prescription_id 处方id
* @doc-param string prescription_no 处方编号
*/
public function actionSource()
{
$prescription_no = \Yii::$app->request->get('prescription_no');
if (!$prescription_no) throw new Exception('处方号不能为空');
$store_id = \Yii::$app->request->get('store_id');
// 先查询是否西药处方
$prescription = Prescription::find()->where(['prescription_no' => $prescription_no])->with(['pharmacistInfo', 'patients'])->asArray()->one();
$doctor = [];
$doctorInfo = DoctorInfo::find()->where(['su_id' => $prescription['su_id']])->one();
$depart = Department::findOne(['id' => $doctorInfo->depart_id]);
$store = Store::findOne(['id' => $store_id]);
$doctor['store'] = $store->name;
$doctor['depart'] = $depart->name;
$doctor['name'] = $doctorInfo->name;
return [
'type' => $prescription->prescription_type == 1 ? '中药处方' : ($prescription->prescription_type == 2 ? '西药处方' : '颗粒药处方'),
'prescription' => $prescription,
'doctor' => $doctor
];
}
}