Files
xk-api-yii/service/modules/v1/controllers/PrescriptionController.php
2024-12-19 19:27:00 +08:00

2329 lines
99 KiB
PHP

<?php
namespace service\modules\v1\controllers;
use Carbon\Carbon;
use common\core\BaseAppController;
use common\enums\DrugEnum;
use common\enums\ProductOrderEnum;
use common\events\PrescriptionEvent;
use common\events\ProductOrderEvent;
use common\helpers\FuncHelper;
use common\jobs\Sms\WaitApprovalMessageJob;
use common\jobs\templateMessage\PrescriptionCreated;
use common\models\oldDb\Address;
use common\models\oldDb\ChineseRepice;
use common\models\oldDb\DiseaseCommon;
use common\models\oldDb\DoctorCommon;
use common\models\oldDb\DoctorInfo;
use common\models\oldDb\Drug;
use common\models\oldDb\DrugDosage;
use common\models\oldDb\DrugUseFrequency;
use common\models\oldDb\DrugUseNum;
use common\models\oldDb\DrugUseTime;
use common\models\oldDb\DrugUseType;
use common\models\oldDb\DrugUseWay;
use common\models\oldDb\GranularRepice;
use common\models\oldDb\Prescription;
use common\models\oldDb\PrescriptionChinese;
use common\models\oldDb\PrescriptionGranular;
use common\models\oldDb\PrescriptionWest;
use common\models\oldDb\ProcessRule;
use common\models\oldDb\ProcessRuleNote;
use common\models\oldDb\ProductOrder;
use common\models\oldDb\ProductOrderItems;
use common\models\oldDb\Region;
use common\models\oldDb\Register;
use common\models\oldDb\SystemConfig;
use common\models\oldDb\SystemNotice;
use common\models\oldDb\UserPatient;
use common\models\oldDb\WestRepice;
use common\models\oldDb\WestUnit;
use common\modelsgii\oldDb\ChineseMedicine;
use common\modelsgii\oldDb\Disease;
use common\modelsgii\oldDb\GranularMedicine;
use common\modelsgii\oldDb\PayConfig;
use common\services\PrescriptionService;
use yii\base\Exception;
use yii\helpers\Json;
/**
* @doc-module 开处方
*/
class PrescriptionController extends BaseAppController
{
// public $optional = ['process-rule-list', 'process-note-list'];
public function beforeAction($action){
$ret = parent::beforeAction($action);
$doctorInfo = DoctorInfo::findOne(['su_id' => \YII::$app->user->id]);
if(!$doctorInfo){
throw new Exception('无权限进行该操作');
}
$westActions = ['actionProcessRuleList','actionProcessNoteList','actionWestList', 'actionWestCommon','actionServicePackageList', 'actionServicePackageCommon', '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('您当前并未拥有开具此方的权限');
}
return $ret;
}
/**
* @doc-name 添加西药-全部西药列表
* @doc-param name string 药名
*/
public function actionWestList()
{
$name = \Yii::$app->request->post('name');
$store_id = \Yii::$app->request->post('store_id');
if (!$name) {
$common = DoctorCommon::find()->alias('dc')->where([
'dc.su_id' => \Yii::$app->user->identity->id,
'dc.store_id' => $store_id
])->joinWith([
'drug d' => function($q){
$q->andWhere([
'in','d.type',[2,4]
]);
}
])->joinWith([
'drugStoreDrug','drugStoreRelation'
])->asArray()->all();
//有收藏药品
if ($common) {
foreach ($common as $v) {
$drug = $v['drug'];
$ids[] = $drug['id'];
$drug['buy_price'] = $v['drugStoreRelation']['buy_price'];
$drug['price'] = $v['drugStoreRelation']['price'];
$drug['stock'] = $v['drugStoreDrug']['stock'];
$collect[] = $drug;
}
$unCollect = Drug::find()->alias('d')->select('d.*')->where([
'in','d.type',[2,4]
])->andWhere([
'not in', 'd.id', $ids
])->with('drugStoreDrug')->joinWith(['drugStoreRelation dsr' => function ($q) use ($store_id){
$q->andWhere(['dsr.store_id' => $store_id]);
$q->andWhere(['dsr.status' => 2]);
}])->asArray()->all();
foreach($unCollect as $k=>$v){
$unCollect[$k]['buy_price'] = $v['drugStoreRelation']['buy_price'];
$unCollect[$k]['price'] = $v['drugStoreRelation']['price'];
$unCollect[$k]['stock'] = $v['drugStoreDrug']['stock'];
unset($unCollect[$k]['drugStoreRelation']);
unset($unCollect[$k]['drugStoreDrug']);
}
return ['collect' => $collect, 'uncollect' => $unCollect];
}
$unCollect = Drug::find()->alias('d')->select('d.*')->where([
'in','d.type',[2,4]
])->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();
foreach($unCollect as $k=>$v){
$unCollect[$k]['buy_price'] = $v['drugStoreRelation']['buy_price'];
$unCollect[$k]['price'] = $v['drugStoreRelation']['price'];
$unCollect[$k]['stock'] = $v['drugStoreDrug']['stock'];
unset($unCollect[$k]['drugStoreRelation']);
unset($unCollect[$k]['drugStoreDrug']);
}
return ['uncollect' => $unCollect];
}
//搜索
$allDrug = Drug::find()->alias('d')->select('d.*')->where([
'in','d.type',[2,4]
])->andWhere([
'or',
['like', 'd.drug_name', '%' . $name . "%", false],
['like', 'd.pinyin_simple', '%' . $name . "%", false],
['like', 'd.drug_alias', '%' . $name . "%", false]
])->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 (!$allDrug) {
throw new Exception('没有搜到');
}
foreach($allDrug as $k=>$v){
$allDrug[$k]['buy_price'] = $v['drugStoreRelation']['buy_price'];
$allDrug[$k]['price'] = $v['drugStoreRelation']['price'];
$allDrug[$k]['stock'] = $v['drugStoreDrug']['stock'];
unset($allDrug[$k]['drugStoreRelation']);
unset($allDrug[$k]['drugStoreDrug']);
$ids[] = $v['id'];
}
$is_collect = DoctorCommon::find()->alias('dc')->select(['dc.drug_id'])->where([
'in', 'dc.drug_id', $ids
])->andWhere([
'dc.su_id'=>\Yii::$app->user->identity->id,
'dc.store_id' => $store_id
])->joinWith([
'drug d' => function ($q){
$q->andWhere([
'in','d.type',[2,4]
]);
}
])->joinWith([
'drugStoreDrug','drugStoreRelation'
])->column();
//没有收藏的药品
if (!$is_collect) {
return ['uncollect' => $allDrug];
}
foreach ($ids as $k => $v) {
foreach ($is_collect as $val) {
if ($v == $val) {
unset($ids[$k]);
}
}
}
$no_collect = Drug::find()->alias('d')->select('d.*')->where([
'in', 'd.id', $ids
])->andWhere([
'in','d.type',[2,4]
])->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();
foreach($no_collect as $k=>$v){
$no_collect[$k]['buy_price'] = $v['drugStoreRelation']['buy_price'];
$no_collect[$k]['price'] = $v['drugStoreRelation']['price'];
$no_collect[$k]['stock'] = $v['drugStoreDrug']['stock'];
unset($no_collect[$k]['drugStoreRelation']);
unset($no_collect[$k]['drugStoreDrug']);
}
$collected = Drug::find()->alias('d')->select('d.*')->where([
'in', 'd.id', $is_collect
])->andWhere([
'in','d.type',[2,4]
])->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();
foreach($collected as $k=>$v){
$collected[$k]['buy_price'] = $v['drugStoreRelation']['buy_price'];
$collected[$k]['price'] = $v['drugStoreRelation']['price'];
$collected[$k]['stock'] = $v['drugStoreDrug']['stock'];
unset($collected[$k]['drugStoreRelation']);
unset($collected[$k]['drugStoreDrug']);
}
if (!$no_collect) {
return ['collected' => $collected];
}
return [
'uncollect' => $no_collect,
'collected' => $collected
];
}
/**
* @doc-name 添加西药-常用西药列表
* @doc-param name string 药名
*/
public function actionWestCommon()
{
$name = \Yii::$app->request->post('name');
$store_id = \Yii::$app->request->post('store_id');
$common = DoctorCommon::find()->alias('dc')->where([
'dc.su_id' => \Yii::$app->user->identity->id,
'dc.store_id' => $store_id
])->joinWith([
'drug d' => function ($q) use ($name){
$q->andWhere([
'in','d.type',[2,4]
]);
if($name){
$q->andWhere([
'like', 'd.drug_name', '%' . $name . "%", false
]);
}
}
])->joinWith([
'drugStoreDrug','drugStoreRelation'
])->asArray()->all();
if (!$common) throw new Exception('你还没有添加常用药');
foreach($common as $k=>$v){
$drug = $v['drug'];
$data[$k] = $drug;
$data[$k]['buy_price'] = $v['drugStoreRelation']['buy_price'];
$data[$k]['price'] = $v['drugStoreRelation']['price'];
$data[$k]['stock'] = $v['drugStoreDrug']['stock'];
unset($v[$k]['store']);
unset($v[$k]['drugStoreRelation']);
unset($v[$k]['drugStoreDrug']);
}
return $data;
}
/**
* @doc-name 西药收藏
* @doc-param int id 西药id
*/
public function actionWestCollection()
{
$id = \Yii::$app->request->get('id');
$store_id = \Yii::$app->request->get('store_id');
if (empty($id)||empty($store_id)) throw new Exception('参数缺失');
$drug = Drug::find()->alias('d')->where(['d.id' => $id])->andWhere([
'in','d.type',[2,4]
])->joinWith(['drugStoreRelation dsr' => function ($q) use ($store_id){
$q->andWhere(['dsr.store_id' => $store_id]);
$q->andWhere(['dsr.status' => 2]);
}])->one();
if (!$drug) {
throw new Exception('该西药不存在');
}
$isCollected = DoctorCommon::findOne(['su_id' =>\Yii::$app->user->identity->id,'drug_id' => $id,'store_id'=>$store_id]);
if($isCollected){
throw new Exception('该西药已收藏');
}
$DoctorCommon = new DoctorCommon();
$DoctorCommon->store_id = $store_id;
$DoctorCommon->su_id = \Yii::$app->user->identity->id;
$DoctorCommon->drug_id = $id;
$DoctorCommon->saveOrFail();
return ['收藏成功'];
}
/**
* @doc-name 西药取消收藏
* @doc-param int id 西药id
*/
public function actionWestUncollect()
{
$id = \Yii::$app->request->get('id');
if (empty($id)) throw new Exception('id不能为空');
$drug = DoctorCommon::find()->where([
'su_id' => \Yii::$app->user->identity->id,
'drug_id' => $id
])->one();
if (!$drug) {
throw new Exception('该收藏不存在');
}
$drug->delete();
return ['取消收藏成功'];
// \Yii::$app->db->createCommand()->delete('yii_doctor_common', ['su_id' => \Yii::$app->user->identity->id, 'drug_id' => $id])->execute();
}
/**
* 西药单位列表
*/
public function actionWestUnit()
{
return WestUnit::find()->all();
}
/**
* 药的使用时间列表
*/
public function actionUseTime()
{
return DrugUseTime::find()->all();
}
/**
* 药的使用方式
*/
public function actionUseType()
{
return DrugUseType::find()->all();
}
/**
* 药的使用频率
*/
public function actionUseFre()
{
return DrugUseFrequency::find()->all();
}
/**
* @doc-name 添加西药药方
* @doc-param int number 数量
* @doc-param string instruction 说明书
* @doc-param int time_id 使用时间
* @doc-param int type_id 使用类型
* @doc-param int grain_number 粒数
* @doc-param int f_id 频率
* @doc-param int wu_id 西药单位id
* @doc-param int content 药品id
* @doc-param int available_days 可用天数
* @doc-return string aa bb
*/
public function actionAddWest()
{
$param = \Yii::$app->request->post();
$this->requestValidate($param, [
['store_id', 'required'],
['content', 'required'],
['number', 'required'],
// ['instruction', 'required'],
['time_id', 'required'],
['type_id', 'required'],
['grain_number', 'required'],
['wu_id', 'required'],
['available_days', 'required'],
['f_id', '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',[2,4]
])->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['drugStoreDrug']['stock'] < $param['number']){
throw new Exception('药品库存数量不足');
}
$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']);
$usage_dosage = Json::encode($drug);
unset($param['content']);
$WestRecipe = new WestRepice();
$WestRecipe->content = $usage_dosage;
$WestRecipe->total_price = bcmul($param['number'], $price,2);
$WestRecipe->attributes = $param;
$WestRecipe->saveOrFail();
$transaction->commit();
return [$WestRecipe->id];
} catch (Exception $e) {
$transaction->commit();
throw $e;
}
}
/**
* @doc-name 开西药处方
* @doc-param string doctor_order 医嘱
* @doc-param string clinical_diagnose 临床诊断
* @doc-param int up_id 就诊人id
* @doc-param int category 类别1自费2医保
* @doc-param string wr_ids 关联西药药方表
* @doc-return string aa bb
*/
public function actionAddPrescriptionWest()
{
$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'],
['wr_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 = 'XY' . rand(111111, 999999) . time();
$marketPrice = '0';
$priceTotal = 0;
$wr_ids = explode(',', $param['wr_ids']);
$drugStoreDrugs = [];
for ($i = 0; $i < sizeof($wr_ids); $i++) {
$recipe = WestRepice::find()->where(['id' => $wr_ids[$i]])->asArray()->one();
if(!$recipe){
throw new Exception('药方数据错误');
}
$priceTotal += $recipe['total_price'];
$content = Json::decode($recipe['content']);
$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); //对药品总价进行四舍五入
//组装处方快照
$wr_ids = explode(',', $param['wr_ids']);
$repice = WestRepice::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());
$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('name,depart_id,title_id')->where(['su_id' => $id])->with(['depart','title'])->asArray()->one();
$prescription_content['total_pay_price'] = $priceTotal;
$prescriptionWest = new PrescriptionWest();
$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 = 0;
$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;
$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 = 2;//西药
$prescription->category = $param['category'];
$over_time = \Yii::$app->params['prescription']['over_time'];
$doctor_order = [];
if(date('H')>=16){
$doctor_order[] = '该处方有效期延长为三天内有效';
$over_time = 72*3600;
}
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->wr_ids = $param['wr_ids'];
$prescription->total_pay_price = $priceTotal;
$prescription->saveOrFail();
//获取支付方式配置
$payConfig = PayConfig::findOne(['status' => 1, 'current_use' => 1]);
// 生成商品订单
$productOrder = new ProductOrder();
$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 = 0;
$productOrder->prescription_type = 2; // 西(中成)药处方
$productOrder->order_type = 1;
$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->status = ProductOrderEnum::UNPAY;
$treatementPrice = 0;
if($param['treatement_price']){
$treatementPrice = $param['treatement_price'];
}
$productOrder->treatement_price = $treatementPrice;
$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'];
$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'];
//判断是否满足包邮条件
$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 + $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->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;
\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();
//处方生成短信通知药师审方
\Yii::$app->queue->push(new WaitApprovalMessageJob([
'orderId' => $prescription->id,
]));
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;
}
}
/**
* 中药剂数列表
*/
public function actionDosage()
{
return DrugDosage::find()->all();
}
/**
* 中药用量列表
*/
public function actionUseNum()
{
return DrugUseNum::find()->all();
}
/**
* @doc-name 中药的使用煎熬方式
* @doc-return mixed @drug-use-way{*}
*/
public function actionUseWay()
{
return DrugUseWay::find()->all();
}
public function actionProcessRuleList(){
$pid = \Yii::$app->request->get('pid') ?? 0;
return ProcessRule::find()->where(['pid' => $pid])->asArray()->all();
}
public function actionProcessNoteList(){
$rule_id = \Yii::$app->request->get('rule_id') ?? 0;
if(!$rule_id){
throw new Exception("请先选择加工方式");
}
return ProcessRuleNote::find()->where(['rule_id' => $rule_id])->asArray()->all();
}
/**
* @doc-name 中药列表
* @doc-param string name 药名
*/
public function actionChineseList()
{
$name = \Yii::$app->request->post('name');
$store_id =\Yii::$app->request->post('store_id');
if(!$name){
throw new Exception('请输入药品名称关键字');
}
$drug = Drug::find()->alias('d')->select('d.*')->where([
'd.type' => 1
])->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]['drugStoreDrug']);
unset($drug[$k]['drugStoreRelation']);
}
return $drug;
}
/**
* @doc-name 一次添加一个中药
* @doc-param int drug_id 药id
* @doc-param int number 数量
*/
public function actionAddOneChinese()
{
$request = \Yii::$app->request;
$param = $request->post();
$this->requestValidate($param, [
['store_id', 'required'],
['drug_id', 'required'],
['number', 'required']
]);
$store_id = $param['store_id'];
try {
$transaction = \Yii::$app->db->beginTransaction();
$drug = Drug::find()->alias('d')->select('d.*')->where([
'd.id' => $param['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('药品暂不售卖');
}
if ($param['number'] > $drug['drugStoreDrug']['stock']) throw new Exception('库存不足');
$ChineseMedicine = new ChineseMedicine();
$ChineseMedicine->su_id = \Yii::$app->user->id;
$ChineseMedicine->drug_id = $param['drug_id'];
$ChineseMedicine->drug_number = $drug['drug_number'];
$ChineseMedicine->name = $drug['drug_name'];
$ChineseMedicine->number = $param['number'];
$ChineseMedicine->order = $param['order']??0;
$ChineseMedicine->unit = $drug['unit_id']??0;
$ChineseMedicine->price = $drug['drugStoreRelation']['price'];
$ChineseMedicine->buy_price = $drug['drugStoreRelation']['buy_price'];
$ChineseMedicine->saveOrFail();
$transaction->commit();
return [$ChineseMedicine->id];
} catch (Exception $e) {
$transaction->rollBack();
throw $e;
}
}
/**
* @doc-name 添加中药药方
* @doc-param int deployment 调配1煎煮2外配
* @doc-param int dosage 剂数id
* @doc-param int consumption 用量id
* @doc-param int usage 用法
* @doc-param int fufa_id 服法id
* @doc-param string remark 备注
* @doc-param int is_deepfry 是否浓煎0否1是
* @doc-param string cm_id 中药处方的中药表id
* @doc-return array aa bb
*/
public function actionAddRecipe()
{
$param = \Yii::$app->request->post();
$this->requestValidate($param, [
['store_id', 'required'],
['deployment', 'required'],
['dosage', 'required'],
['consumption', 'required'],
['cm_id', 'required'],
['process_rule_id','integer'],
['process_rule_note','string']
]);
$store_id = $param['store_id'];
try {
$transaction = \Yii::$app->db->beginTransaction();
$ids = explode(',', $param['cm_id']);
$price = 0;
$medicine = ChineseMedicine::find()->select('id,drug_id,name,order,unit,number,price,drug_number,buy_price')->with(['unit','useWay'])->where(['in', 'id', $ids])->asArray()->all();
$drug_ids= ChineseMedicine::find()->select('drug_id')->where(['in', 'id', $ids])->column();
$drug_ids_str=implode(',',$drug_ids);
$totalNum = 0;
foreach ($medicine as $value){
$drug = Drug::find()->alias('d')->select('d.*')->where([
'd.id' => $value['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) $value['number'] * (int) $param['dosage'];
if($drug['drugStoreDrug']['stock'] < $drugNumber){
throw new Exception($value['name'].'库存数量不足');
}
$totalNum += $drugNumber;
$price+=$value['number'] * $value['price'] * $param['dosage'];
}
$process_price = 0;
$processRuleContent = '';
if($param['process_rule_id']){
$processRule = ProcessRule::find()->where(['id' => $param['process_rule_id']])->one();
if(empty($processRule)){
throw new Exception("加工方式错误");
}
$parentProcessRule = ProcessRule::find()->where(['id' => $processRule->pid])->one();
if(empty($parentProcessRule)){
throw new Exception("加工方式错误");
}
if($processRule->calc_method == 1){
$process_price = $processRule->price;
$processRuleContent = '加工方式:'.$parentProcessRule->name.'-'.$processRule->name.'-'.$param['process_rule_note'].",固定收费".$process_price."";
}elseif($processRule->calc_method == 2){
$process_price = $processRule->price * $param['dosage'];
$processRuleContent = '加工方式:'.$parentProcessRule->name.'-'.$processRule->name.'-'.$param['process_rule_note'].",".$processRule->price."元/".$processRule->unit.',共'.$param['dosage'].'贴';
}else{
$process_price = $processRule->price * $totalNum;
$processRuleContent = '加工方式:'.$parentProcessRule->name.'-'.$processRule->name.'-'.$param['process_rule_note'].",".$processRule->price."元/".$processRule->unit.',共'.$totalNum.'g';
}
}
$encode = Json::encode($medicine);
$chineseRecipe = new ChineseRepice();
$chineseRecipe->su_id = \Yii::$app->user->id;
$chineseRecipe->content = $encode;
$chineseRecipe->deployment = $param['deployment'];
$chineseRecipe->dosage = $param['dosage'];
$chineseRecipe->consumption = $param['consumption'];
$chineseRecipe->is_deepfry = $param['is_deepfry']??0;
$chineseRecipe->volume = $param['volume']??0;
$chineseRecipe->cm_id = $param['cm_id'];
$chineseRecipe->process_rule_id = $param['process_rule_id'] ?? 0;
$chineseRecipe->process_rule = $processRuleContent;
$chineseRecipe->process_rule_note = $param['process_rule_note'];
$chineseRecipe->process_price = $process_price;
$chineseRecipe->total_price = round($price, 4);
$chineseRecipe->drug_ids = $drug_ids_str;
$chineseRecipe->saveOrFail();
$transaction->commit();
return [$chineseRecipe->id];
} catch (Exception $e) {
$transaction->rollBack();
throw $e;
}
}
/**
* @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 actionAddPrescription()
{
$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'],
['cr_ids', 'required'],
['treatement_price', 'number']
]);
$transaction = \Yii::$app->db->beginTransaction();
try {
$store_id = $param['store_id'];
/* @var \common\models\oldDb\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 = 'ZY' . rand(111111, 999999) . time();
$marketPrice = '0';
$priceTotal = 0;
$processTotal = 0;
$ids = explode(',', $param['cr_ids']);
for ($i = 0; $i < sizeof($ids); $i++) {
$recipe = ChineseRepice::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 = ChineseRepice::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;
$prescriptionChinese = new PrescriptionChinese();
$prescriptionChinese->store_id = $store_id;
$prescriptionChinese->prescription_no = $prescription_no;
$prescriptionChinese->su_id = $id;
$prescriptionChinese->user_id = $userPatient->user_id;
$prescriptionChinese->up_id = $param['up_id'];
$prescriptionChinese->status = 0;
$prescriptionChinese->type = 1;//普通方
$prescriptionChinese->content = Json::encode($prescription_content);
$prescriptionChinese->category = $param['category'];
$prescriptionChinese->doctor_order = $param['doctor_order'];
$prescriptionChinese->clinical_diagnose = $param['clinical_diagnose'];
$prescriptionChinese->cr_ids = $param['cr_ids'];
$prescriptionChinese->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 = 1;// 中药
$prescription->category = $param['category'];
$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[] = '患者需长期使用此药,开具超七天用量';
}
if($param['doctor_second_sign']?? 0 == 1){
$prescription->doctor_second_sign = 1;
}
$doctor_order[] = $param['doctor_order'];
$prescription->valid_hours = $over_time/3600;
$prescription->doctor_order = implode('|',$doctor_order);
$prescription->clinical_diagnose = $param['clinical_diagnose'];
$prescription->cr_ids = $param['cr_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 = 1;//中药处方
$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;
$treatementPrice = 0;
if($param['treatement_price']){
$treatementPrice = $param['treatement_price'];
}
$productOrder->treatement_price = $treatementPrice;
$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->status = ProductOrderEnum::UNPAY;
$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();
//处方生成短信通知药师审方
\Yii::$app->queue->push(new WaitApprovalMessageJob([
'orderId' => $prescription->id,
]));
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;
}
}
/*******************************************************颗粒药******************************************************************************** */
/**
* @doc-name 颗粒药列表
* @doc-param string name 药名
*/
public function actionGranularList()
{
$name = \Yii::$app->request->post('name');
$store_id =\Yii::$app->request->post('store_id');
if(!$name){
throw new Exception('请输入药品名称关键字');
}
$drug = Drug::find()->alias('d')->select('d.*')->where([
'd.type' => 3
])->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 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
* @doc-param int number 数量
*/
public function actionAddOneGranular()
{
$request = \Yii::$app->request;
$param = $request->post();
$this->requestValidate($param, [
['store_id', 'required'],
['drug_id', 'required'],
['number', 'required']
]);
$store_id = $param['store_id'];
try {
$transaction = \Yii::$app->db->beginTransaction();
$drug = Drug::find()->alias('d')->select('d.*')->where([
'd.id' => $param['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('药品不存在');
}
if ($param['number'] > $drug['drugStoreDrug']['stock']) throw new Exception('库存不够');
$GranularMedicine = new GranularMedicine();
$GranularMedicine->drug_id = $param['drug_id'];
$GranularMedicine->drug_number = $drug['drug_number'];
$GranularMedicine->name = $drug['drug_name'];
$GranularMedicine->number = $param['number'];
$GranularMedicine->order = $param['order']??0;
$GranularMedicine->order = $drug['unit_id']??0;
$GranularMedicine->price = $drug['drugStoreRelation']['price'];
$GranularMedicine->buy_price = $drug['drugStoreRelation']['buy_price'];
$GranularMedicine->saveOrFail();
$transaction->commit();
return [$GranularMedicine->id];
} catch (Exception $e) {
$transaction->rollBack();
throw $e;
}
}
/**
* @doc-name 添加颗粒药药方
* @doc-param int deployment 调配1煎煮2外配
* @doc-param int dosage 剂数id
* @doc-param int consumption 用量id
* @doc-param int usage 用法
* @doc-param int fufa_id 服法id
* @doc-param string remark 备注
* @doc-param int is_deepfry 是否浓煎0否1是
* @doc-param string cm_id 颗粒药处方的颗粒药表id
* @doc-return array aa bb
*/
public function actionAddGranularRecipe()
{
$param = \Yii::$app->request->post();
$this->requestValidate($param, [
['store_id', 'required'],
['deployment', 'required'],
['dosage', 'required'],
['consumption', 'required'],
['gm_id', 'required'],
['process_rule_id','integer'],
['process_rule_note','string']
]);
$store_id = $param['store_id'];
try {
$transaction = \Yii::$app->db->beginTransaction();
$ids = explode(',', $param['gm_id']);
$price = 0;
$totalNum = 0;
$medicine = GranularMedicine::find()->select('id,drug_id,name,order,unit,number,price,buy_price,drug_number')->with(['unit','useWay'])->where(['in', 'id', $ids])->asArray()->all();
foreach ($medicine as $value){
$drug = Drug::find()->alias('d')->select('d.*')->where([
'd.id' => $value['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) $value['number'] * (int) $param['dosage'];
if($drug['drugStoreDrug']['stock'] < $drugNumber){
throw new Exception($value['name'].'库存数量不足');
}
$totalNum += $drugNumber;
$price+=$value['number'] * $value['price'] * $param['dosage'];
}
$encode = Json::encode($medicine);
if($param['process_rule_id']){
$processRule = ProcessRule::find()->where(['id' => $param['process_rule_id']])->one();
if(empty($processRule)){
throw new Exception("加工方式错误");
}
if($processRule->calc_method == 1){
$process_price = $processRule->price;
$processRuleContent = $processRule->name.",固定收费".$process_price."";
}elseif($processRule->calc_method == 2){
$process_price = $processRule->price * $param['dosage'];
$processRuleContent = $processRule->name.",".$processRule->price."元/".$processRule->unit;
}else{
$process_price = $processRule->price * $totalNum;
$processRuleContent = $processRule->name.",".$processRule->price."元/".$processRule->unit;
}
}
$GranularRecipe = new GranularRepice();
$GranularRecipe->content = $encode;
$GranularRecipe->total_price = round($price, 4);
$GranularRecipe->deployment = $param['deployment'];
$GranularRecipe->dosage = $param['dosage'];
$GranularRecipe->consumption = $param['consumption'];
$GranularRecipe->is_deepfry = $param['is_deepfry']??0;
$GranularRecipe->volume = $param['volume']??0;
$GranularRecipe->gm_id = $param['gm_id'];
$GranularRecipe->process_rule_id = $param['process_rule_id'];
$GranularRecipe->process_rule = $processRuleContent;
$GranularRecipe->process_rule_note = $param['process_rule_note'];
$GranularRecipe->process_price = $process_price;
$GranularRecipe->saveOrFail();
$transaction->commit();
return [$GranularRecipe->id];
} catch (Exception $e) {
$transaction->rollBack();
throw $e;
}
}
/**
* @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 actionAddPrescriptionGranular()
{
$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 = 0;
$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();
//处方生成短信通知药师审方
\Yii::$app->queue->push(new WaitApprovalMessageJob([
'orderId' => $prescription->id,
]));
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;
}
}
/**
* @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;
}
}
/**
* 常用诊断
*/
public function actionDiseaseCommon(){
$diseaseCommon = DiseaseCommon::find()->select('id,disease_id')->where([
'su_id' => \Yii::$app->user->identity->id
])->with('disease')->orderBy('created_at DESC')->asArray()->all();
return $diseaseCommon;
}
/**
* 添加常用诊断
*/
public function actionAddDiseaseCommon(){
$post = \YII::$app->request->post();
$this->requestValidate($post,[
['disease_id','required']
]);
$diseaseCommon = DiseaseCommon::findOne([
'su_id' => \YII::$app->user->identity->id,
'disease_id' => $post['disease_id']
]);
if($diseaseCommon){
throw new Exception('已是您的常用诊断');
}
$diseaseCommon = new DiseaseCommon();
$diseaseCommon->su_id = \YII::$app->user->identity->id;
$diseaseCommon->disease_id = $post['disease_id'];
$diseaseCommon->saveOrFail();
return ['添加常用诊断成功'];
}
/**
* 删除常用诊断
*/
public function actionDelDiseaseCommon(){
$post = \YII::$app->request->post();
$this->requestValidate($post,[
['id','required']
]);
$diseaseCommon = DiseaseCommon::findOne([
'su_id' => \YII::$app->user->identity->id,
'id' => $post['id']
]);
if(!$diseaseCommon){
throw new Exception('参数错误');
}
$diseaseCommon->delete();
return ['删除常用诊断成功'];
}
/**
* @doc-name 搜索疾病
* @doc-param string name 疾病关键字
*/
public function actionSearchDisease()
{
$name = \Yii::$app->request->get('name');
if (!$name) throw new Exception('输入需要搜索的诊断');
$query = Disease::find()->where([
'or',
['like', 'name', '%' . $name . "%", false],
['like', 'pinyin', '%' . $name . "%", false]
]);
$this->field = [
Disease::class => [
'id', 'name'
]
];
return $this->create($query, ['page_size' => 50]);
}
/**
* @doc-name 处方详情
* @doc-param string prescription_no 处方编号
* @doc-return mixed @List{id-int-处方id,prescription_no-int-处方编号,type-int-类型1普通方2常用方,created_at-int-开具时间,clinical_diagnose-string-临床诊断,category-int-类别1自费2医保,status-int-状态0待审核1已通过2未通过3待使用4已使用5未使用6已失效7已初审,doctor_order-string-医嘱,patient_name-string-患者,sex-int-0默认1男2女,age-int-年龄,depart-string-科室,mobile-string-手机号,doctor_name-string-医生,first_view-string-初审药师,again_view-string-复审药师,patient_name-string-患者,@Rp{@Granular{content-string-药品有关信息,dosage-int-剂数,useNum-string-用量,usage-int-用法,fufa-string-服法,is_deepfry-int-是否浓煎0否1是,total_price-float-价格},@West{content-string-药品有关信息,number-int-数量,available_days-int-可用天数,total_price-float-价格}}} 处方记录
*/
public function actionPrescriptionDetail()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
['prescription_no', 'required'],
['store_id', 'required']
]);
return (new PrescriptionService())->detail($post['prescription_no']);
}
/**
* 中药相冲相畏有毒检查
*/
public function actionDrugCheck(){
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
['cr_ids', 'required'],
['store_id', 'required']
]);
$ids = explode(',',$post['cr_ids']);
$repice = ChineseRepice::find()->where(['in', 'id', $ids])->asArray()->all();
if(!$repice){
throw new Exception('药品不存在');
}
// $prescription = Prescription::find()->select('content')->where([
// 'prescription_no' => $post['prescription_no'],
// 'prescription_type' => 1
// ])->asArray()->one();
// if(!$prescription){
// throw new Exception('中药处方不存在');
// }
// $drugData = $prescription['repice'][0]['content'];
$drugArr = json_decode($repice[0]['content'],true);
$drugs = array_column($drugArr,'name');
if(array_intersect($drugs,DrugEnum::POISONOUS)){
return [
'is_exist' => true,
'message'=>'处方中存在有毒药品'
];
}
foreach(DrugEnum::OPPOSITION as $key=>$value){
if(in_array($key,$drugs)){
if(array_intersect($value,$drugs)){
return [
'is_exist' => true,
'message' => '处方中存在十八反药品'
];
}
}
}
foreach(DrugEnum::CONFLCT as $v){
$conflictArr = array_intersect($v,$drugs);
if($conflictArr && !array_diff($conflictArr,$v) && !array_diff($v,$conflictArr)){
return [
'is_exist' => true,
'message' => '处方中存在十九畏药品'
];
}
}
return [
'is_exist' => false
];
}
/**
* @doc-name 添加产品服务包-全部产品服务包列表
* @doc-param name string 药名
*/
public function actionServicePackageList()
{
$name = \Yii::$app->request->post('name');
$store_id = \Yii::$app->request->post('store_id');
if (!$name) {
$common = DoctorCommon::find()->alias('dc')->where([
'dc.su_id' => \Yii::$app->user->identity->id,
'dc.store_id' => $store_id
])->joinWith([
'drug d' => function($q){
$q->andWhere([
'in','d.type',[5]
]);
}
])->joinWith([
'drugStoreDrug','drugStoreRelation'
])->asArray()->all();
//有收藏药品
if ($common) {
foreach ($common as $v) {
$drug = $v['drug'];
$ids[] = $drug['id'];
$drug['buy_price'] = $v['drugStoreRelation']['buy_price'];
$drug['price'] = $v['drugStoreRelation']['price'];
$drug['stock'] = $v['drugStoreDrug']['stock'];
$collect[] = $drug;
}
$unCollect = Drug::find()->alias('d')->select('d.*')->where([
'in','d.type',[2,4]
])->andWhere([
'not in', 'd.id', $ids
])->with('drugStoreDrug')->joinWith(['drugStoreRelation dsr' => function ($q) use ($store_id){
$q->andWhere(['dsr.store_id' => $store_id]);
$q->andWhere(['dsr.status' => 2]);
}])->asArray()->all();
foreach($unCollect as $k=>$v){
$unCollect[$k]['buy_price'] = $v['drugStoreRelation']['buy_price'];
$unCollect[$k]['price'] = $v['drugStoreRelation']['price'];
$unCollect[$k]['stock'] = $v['drugStoreDrug']['stock'];
unset($unCollect[$k]['drugStoreRelation']);
unset($unCollect[$k]['drugStoreDrug']);
}
return ['collect' => $collect, 'uncollect' => $unCollect];
}
$unCollect = Drug::find()->alias('d')->select('d.*')->where([
'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()->all();
foreach($unCollect as $k=>$v){
$unCollect[$k]['buy_price'] = $v['drugStoreRelation']['buy_price'];
$unCollect[$k]['price'] = $v['drugStoreRelation']['price'];
$unCollect[$k]['stock'] = $v['drugStoreDrug']['stock'];
unset($unCollect[$k]['drugStoreRelation']);
unset($unCollect[$k]['drugStoreDrug']);
}
return ['uncollect' => $unCollect];
}
//搜索
$allDrug = Drug::find()->alias('d')->select('d.*')->where([
'in','d.type', [5]
])->andWhere([
'or',
['like', 'd.drug_name', '%' . $name . "%", false],
['like', 'd.pinyin_simple', '%' . $name . "%", false],
['like', 'd.drug_alias', '%' . $name . "%", false]
])->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 (!$allDrug) {
throw new Exception('没有搜到');
}
foreach($allDrug as $k=>$v){
$allDrug[$k]['buy_price'] = $v['drugStoreRelation']['buy_price'];
$allDrug[$k]['price'] = $v['drugStoreRelation']['price'];
$allDrug[$k]['stock'] = $v['drugStoreDrug']['stock'];
unset($allDrug[$k]['drugStoreRelation']);
unset($allDrug[$k]['drugStoreDrug']);
$ids[] = $v['id'];
}
$is_collect = DoctorCommon::find()->alias('dc')->select(['dc.drug_id'])->where([
'in', 'dc.drug_id', $ids
])->andWhere([
'dc.su_id'=>\Yii::$app->user->identity->id,
'dc.store_id' => $store_id
])->joinWith([
'drug d' => function ($q){
$q->andWhere([
'in','d.type', [5]
]);
}
])->joinWith([
'drugStoreDrug','drugStoreRelation'
])->column();
//没有收藏的药品
if (!$is_collect) {
return ['uncollect' => $allDrug];
}
foreach ($ids as $k => $v) {
foreach ($is_collect as $val) {
if ($v == $val) {
unset($ids[$k]);
}
}
}
$no_collect = Drug::find()->alias('d')->select('d.*')->where([
'in', 'd.id', $ids
])->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()->all();
foreach($no_collect as $k=>$v){
$no_collect[$k]['buy_price'] = $v['drugStoreRelation']['buy_price'];
$no_collect[$k]['price'] = $v['drugStoreRelation']['price'];
$no_collect[$k]['stock'] = $v['drugStoreDrug']['stock'];
unset($no_collect[$k]['drugStoreRelation']);
unset($no_collect[$k]['drugStoreDrug']);
}
$collected = Drug::find()->alias('d')->select('d.*')->where([
'in', 'd.id', $is_collect
])->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()->all();
foreach($collected as $k=>$v){
$collected[$k]['buy_price'] = $v['drugStoreRelation']['buy_price'];
$collected[$k]['price'] = $v['drugStoreRelation']['price'];
$collected[$k]['stock'] = $v['drugStoreDrug']['stock'];
unset($collected[$k]['drugStoreRelation']);
unset($collected[$k]['drugStoreDrug']);
}
if (!$no_collect) {
return ['collected' => $collected];
}
return [
'uncollect' => $no_collect,
'collected' => $collected
];
}
/**
* @doc-name 添加产品服务包-常用产品服务包列表
* @doc-param name string 药名
*/
public function actionServicePackageCommon()
{
$name = \Yii::$app->request->post('name');
$store_id = \Yii::$app->request->post('store_id');
$common = DoctorCommon::find()->alias('dc')->where([
'dc.su_id' => \Yii::$app->user->identity->id,
'dc.store_id' => $store_id
])->joinWith([
'drug d' => function ($q) use ($name){
$q->andWhere([
'in','d.type',[5]
]);
if($name){
$q->andWhere([
'like', 'd.drug_name', '%' . $name . "%", false
]);
}
}
])->joinWith([
'drugStoreDrug','drugStoreRelation'
])->asArray()->all();
if (!$common) throw new Exception('你还没有添加常用药');
foreach($common as $k=>$v){
$drug = $v['drug'];
$data[$k] = $drug;
$data[$k]['buy_price'] = $v['drugStoreRelation']['buy_price'];
$data[$k]['price'] = $v['drugStoreRelation']['price'];
$data[$k]['stock'] = $v['drugStoreDrug']['stock'];
unset($v[$k]['store']);
unset($v[$k]['drugStoreRelation']);
unset($v[$k]['drugStoreDrug']);
}
return $data;
}
}