Files
xk-api-yii/admin/controllers/base/OrderController.php

508 lines
22 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace admin\controllers\base;
use admin\components\BaseAdminController;
use common\enums\UserRoleEnum;
use common\forms\ProductCancelForm;
use common\forms\ProductRefundForm;
use common\helpers\FuncHelper;
use common\models\oldDb\DrugStore;
use common\models\oldDb\Prescription;
use common\models\oldDb\ProductOrder;
use common\models\oldDb\ProductOrderItems;
use common\models\oldDb\ProductOrderRefund;
use common\models\oldDb\Store;
use common\models\oldDb\SystemConfig;
use common\models\oldDb\UserPatient;
use common\services\ExportService;
use common\services\ExpressService;
use yii\db\Exception;
use yii\helpers\Json;
use yii\web\Response;
class OrderController extends BaseAdminController
{
/**
* @doc-name 产品订单列表
* @doc-param string order_no 订单编号 / optional
* @doc-param string good_name 商品名称 / optional
* @doc-param string accept_name 收件人姓名 / optional
* @doc-param string accept_tel 收件人电话 / optional
* @doc-param string doctor 医生搜索 / optional
* @doc-param string store 门店 / optional
* @doc-param int status 产品订单状态0未支付1待发货2待收货3待评价4已退款5退款中6已收货7确认收货9已取消 / optional
* @doc-param int after_status 售后状态1申请退款2同意退款3已退款9取消退款 / optional
* @doc-param string start_time 开始时间 / optional
* @doc-param string end_time 结束时间 / optional
* @doc-return mixed @List{id,store_id-int-门店ID,p_id,user_id-int-用户id,up_id-int-就诊人id,status-int-产品订单状态0未支付1待发货2待收货3待评价4已退款5退款中6已收货7确认收货,cancel_status-int-取消状态1已取消,cancel_time-string-取消时间,cancel_remark-string-取消备注(未支付超时取消,主动取消,拒绝取消),refund_status-int-退款状态1退款中2已退款,refund_type-int-退款类型0仅退款1退货退款,refund_time-string-退款时间,drugstore-string-发货仓库,total_pay_price-float-总价,pay_time-string-支付时间,order_no-string-订单编号,is_has-int-是否有处方doctor-string-开方医生,good-int-商品信息,trans_expenses-float-运费(默认包邮),free_ship-int-是否包邮,remark-string-留言备注,express_name-string-收货人,express_mobile-string-收货电话,express_region-string-收货省市区,@Prescription{id-int-id,platform_prescription_no-string-平台处方单号,prescription_no-string-处方单号,order_id-int-订单id,status-int-状态 0待审核,1已通过,2未通过,content-string-处方快照,prescription_type-int-1中药处方2西药处方3颗粒药处方,category-int-类别1自费2医保,doctor_order-string-医嘱,clinical_diagnose-string-临床诊断,医生签名-string-医生签名,type-int-类型1普通方2常用方,order_type-int-订单类型1处方2医技,total_pay_price-float-支付金额},decoct_price-float-代煎费用,is_decoct-int-1需要代煎} 商品订单列表
* @doc-return mixed @Pagination{total-int-总数量,totalPage-int-总页数,pageSize-int-每页条数} 分页数据
*/
public function actionProductList()
{
$get = \Yii::$app->request->get();
$good_name = $get['good_name'];
$store = $get['store'];
$doctor = $get['doctor'];
$type=$get['type'];
$admin = \Yii::$app->user->identity;
$start_time=strtotime($get['start_time'].' '.'00:00:00');
$end_time=strtotime($get['end_time'].' '.'23:59:59');
if ($admin->role == UserRoleEnum::STORE_ADMIN) {//门店
$query = ProductOrder::find()->alias('pd')->where(['pd.store_id' => $admin->store_id])->orderBy(['pd.id' => SORT_DESC]);
} elseif ($admin->role == UserRoleEnum::PROVINCE_DAI){//省代
$store_id = Store::find()->select(['id'])->where(['province_id' => $admin->province_id])->column();
$query = ProductOrder::find()->alias('pd')->where(['in','pd.store_id',$store_id])->orderBy(['pd.id' => SORT_DESC]);
} elseif ($admin->role == UserRoleEnum::CITY_DAI){//市代
$store_id = Store::find()->select(['id'])->where(['city_id' => $admin->city_id])->column();
$query = ProductOrder::find()->alias('pd')->where(['in','pd.store_id',$store_id])->orderBy(['pd.id' => SORT_DESC]);
} elseif ($admin->role == UserRoleEnum::SUPPLY){//业务员
$store_id = Store::find()->select(['id'])->where(['code' => $admin->code])->column();
$query = ProductOrder::find()->alias('pd')->where(['in','pd.store_id',$store_id])->orderBy(['pd.id' => SORT_DESC]);
}else {//总后台
$query = ProductOrder::find()->alias('pd')->orderBy(['pd.id' => SORT_DESC]);
}
if (!empty($get['order_no'])) {//订单编号
$query->andWhere(['pd.order_no' => $get['order_no']]);
}
$query->joinWith(['prescription' => function ($p) use ($good_name) {
$p->alias('p');
if (!empty($good_name)) {//商品名称
$p->where(['like', 'p.content', $good_name]);
}
}]);
if (!empty($get['accept_name'])) {//收件人姓名
$query->where(['like', 'pd.express_name', $get['accept_name']]);
}
if (!empty($get['accept_tel'])) {//收件人电话
$query->where(['like', 'pd.express_mobile', $get['accept_tel']]);
}
$query->joinWith(['doctor' => function ($doc) use ($doctor) {
$doc->alias('doc');
if (!empty($doctor)) {//医生搜索
$doc->andWhere(['like', 'doc.name', $doctor]);
}
}]);
if (isset($get['status']) && is_numeric($get['status'])) {//产品订单状态
if ($get['status'] == 3) {
$query->andWhere(['pd.status' => [3, 6, 7]]);
} else {
$query->andWhere(['pd.status' => $get['status']]);
}
}
if (!empty($store)) {//门店
$query->andWhere(['pd.store_id' => $store]);
}
if (!empty($get['after_status'])) {//售后状态
$query->andWhere(['pd.refund_status' => $get['after_status']]);
}
if (!empty($type)) {
$query->andWhere(['pd.prescription_type' => $type]);
}
if (!empty($start_time) && !empty($end_time)) {
if ($start_time==$end_time){//筛选某一天
$time=FuncHelper::getDayBE($get['start_time']);
$end_time=$time[1];
}
$query->andWhere(['between', 'pd.created_at', $start_time,$end_time]);
}
$this->field = [
ProductOrder::class => [
'id', 'store_id','prescription_type', 'user_id', 'up_id', 'p_id', 'status', 'cancel_status', 'order_type', 'order_no', 'total_pay_price', 'type', 'items_price','market_price', 'process_price', 'treatement_price','address_id', 'status', 'trans_expenses', 'free_ship',
'cancel_time' => function ($pd) {
if($pd->cancel_time){
return date('Y-m-d H:i:s', $pd->cancel_time);
}
}, 'cancel_remark', 'refund_status',
'refund_time' => function ($pd) {
if($pd->refund_time){
return date('Y-m-d H:i:s', $pd->refund_time);
}
},
'store' => function ($d) {
return Store::find()->select(['id','drugstore_id','erp_id','plate_id','name','shouzimu','contact','mobile','offical_seal','code','position'])->where([
'id' => $d->store_id
])->one();
},
'drugstore' => function ($d) {
$drugstore_id = Store::find()->select(['drugstore_id'])->where([
'id' => $d->store_id
])->column();
return DrugStore::find()->select(['name'])->where(['in', 'id', $drugstore_id])->all();
},
'pay_time' => function ($m) {
if($m->pay_time){
return date('Y-m-d H:i:s', $m->pay_time);
}
},
'is_has' => function ($i) {
return $i->p_id ? '有处方' : '无处方';
},
'doctor' => 'doctor.name',
// 'good' => function ($model) {
// return $this->actionProductOrderDetail($model->id);
// },
'prescription' => function ($p) {
if($p->p_id){
$prescription = Prescription::find()->where([
'id' => $p->p_id,
'is_deleted' => 0
])->with(['pharmacistInfo','doctorInfo.depart'])->asArray()->one();
$content = Json::decode($prescription['content']);
$repice = $content['repice'];
if($prescription['prescription_type'] == 2){ //西药
foreach($repice as $k => $v){
$repice[$k]['content'] = Json::decode($v['content']);
}
} else {
$repiceContent = Json::decode($repice[0]['content']);
// foreach($repiceContent as $k=>$v){
// $repiceContent[$k]['content'] = Json::decode($v['content']);
// }
$repice[0]['content'] = $repiceContent;
}
$content['repice'] = $repice;
$prescription['content'] = $content;
}
return $prescription;
},
'address' => function ($m) {
return json_decode($m->address);
},
'created_at' => function ($mo) {
return date('Y-m-d H:i:s', $mo->created_at);
},
'express_name'=>function($e){
return $this->actionReceiveInfo($e->id,1);
},
'express_mobile'=>function($e){
return $this->actionReceiveInfo($e->id,2);
},
'express_region'=>function($e){
return $this->actionReceiveInfo($e->id,3);
},
'express_address'=>function($e){
return $this->actionReceiveInfo($e->id,4);
},
'patient' => 'userPatient.name',
'user' => 'user.nickname',
'product_order_items' => function ($m) {
return $this->actionProductOrderItem($m->id);
},
'patient_sex' => 'userPatient.sex',
'patient_age' => 'userPatient.age',
'patient_mobile' => 'userPatient.mobile',
'category' => 'prescription.category',
'productOrderRefund_status' => 'productOrderRefund.status',
'productOrderRefund_id' => function($m){
return $this->actionProductOrderRefund($m->id);
},
'service_price'=>function($m){
return $this->actionDaijianFei($m->id);
},
'is_decoct','decoct_price','express_no_id',
'delivery_method',
]
];
return $this->create($query, $get);
}
/**
* @doc-name 收获信息
* @doc-author huangyuling
*/
public function actionReceiveInfo($id,$type){
$info=ProductOrder::find()->where(['id'=>$id])->asArray()->one();
switch ($type){
case 1:
if ($info['delivery_method']==0){
return $info['express_name'];
}else{
$user_patient=UserPatient::find()->where(['id'=>$info['up_id']])->asArray()->one();
if (!$user_patient){
throw new Exception('患者信息不存在');
} return $user_patient['name'];
}
break;
case 2:
if ($info['delivery_method']==0){
return $info['express_name'];
}else{
$user_patient=UserPatient::find()->where(['id'=>$info['up_id']])->asArray()->one();
if (!$user_patient){
throw new Exception('患者信息不存在');
}
return $user_patient['mobile'];
}
break;
case 3:
if ($info['delivery_method']==0){
return $info['express_region'];
}else{
$store=Store::find()->where(['id'=>$info['store_id']])->with(['province','city'])->asArray()->one();
if (!$store){
throw new Exception('门店信息不存在');
}
return $store['province']['name'].$store['city']['name'];
}
break;
case 4:
if ($info['delivery_method']==0){
return $info['express_address'];
}else{
$store=Store::find()->where(['id'=>$info['store_id']])->asArray()->one();
if (!$store){
throw new Exception('门店信息不存在');
}
return $store['position'];
}
break;
default:
throw new Exception('类型错误');
}
}
/**
* @doc-name 退款申请ID
* @doc-return int id id
*/
public function actionProductOrderRefund($id)
{
$ProductOrderRefund=ProductOrderRefund::find()->select(['id','reason'])->where(['order_id'=>$id])->orderBy(['id'=>SORT_DESC])->one();
return $ProductOrderRefund;
}
/**
* @doc-name 取消商品(或产品)订单
* @doc-param int order_id 商品(或产品)订单id
* @doc-param int user_id 用户id
*/
public function actionGoodCancel()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
[['order_id', 'user_id'], 'required']
]);
$form = new ProductCancelForm();
return $form->cancel($post);
}
/**
* @doc-name 商品订单退款
* @doc-param int order_id 商品(或产品)订单id
* @doc-param int user_id 用户id
* @doc-param string reason 退款原因
* @doc-param string refund_images 退款图片 / optional
*/
public function actionGoodRefund()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
[['order_id', 'user_id', 'reason'], 'required']
]);
$form = new ProductRefundForm();
return $form->refund($post);
}
/**
* @doc-name 发货
* @doc-param int order_id 商品(或产品)订单id
*/
public function actionSendOperate()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
['order_id', 'required']
]);
$user = \Yii::$app->user->identity;
if ($user->role != UserRoleEnum::STORE_ADMIN) {
throw new Exception('您没有该门店权限');
}
$ProductOrder = ProductOrder::find()->where([
'store_id' => $user->store_id,
'id' => $post['order_id']
])->one();
if (!$ProductOrder) throw new Exception('产品订单不存在');
$Prescription=Prescription::find()->where(['id'=>$ProductOrder['p_id'],'is_deleted'=>0])->one();
if ($Prescription['status']!=1){
throw new Exception('处方未通过,不能发货');
}
$ProductOrder->is_send = 1;
$ProductOrder->send_time = date('Y-m-d H:i:s', time());
if (!$ProductOrder->saveOrFail()) {
throw new Exception('发货失败');
}
//订单发货3天后分账结算
// if($ProductOrder->type == 2){ //易票联支付的产品订单
// $config = \Yii::$app->params;
// $orderAutoSettlementTime = isset($config['product_order']['settlement_time']) ? $config['product_order']['settlement_time'] : 72*3600;
// $TIME=180;
// \Yii::$app->queue->delay($TIME)->push(new ProductOrderSendJob([
// 'orderId' => $ProductOrder->id
// ]));
// }
return ['发货成功'];
}
/**
* @doc-name 产品订单详情
* @doc-param int product_id 产品订单id
*/
public function actionProductDetail()
{
$get = \Yii::$app->request->get();
$this->requestValidate($get, [
['product_id', 'required']
]);
$ProductOrder = ProductOrder::find()->where([
'id' => $get['product_id']
])->with(['expressNos', 'prescription', 'orderItems'])->asArray()->one();
if (!$ProductOrder) throw new Exception('产品订单不存在');
if ($ProductOrder['prescription']['prescription_type'] == 1) {
$SystemConfig = SystemConfig::findOne(['type' => 1]);
$ProductOrder['daijianfei'] = $SystemConfig['value'];
}
return $ProductOrder;
}
/**
* 订单物流详情
*/
public function actionExpress(){
$get = \Yii::$app->request->get();
$this->requestValidate($get, [
['order_id', 'required']
]);
$express = (new ExpressService())->detail($get['order_id']);
return $express;
}
/**
* @doc-name 处理处方快照
*/
public function actionProductOrderDetail($id = null)
{
$p_id = ProductOrder::find()->select(['p_id'])->where(['id' => $id])->column();
$prescription_type = Prescription::find()->select(['prescription_type'])->where(['id' => $p_id, 'is_deleted' => 0])->column();
if ($prescription_type[0] == 1 || $prescription_type[0] == 3) {//中药 配方颗粒
$content = Prescription::find()->where(['id' => $p_id, 'is_deleted' => 0])->one();
$recipe = json_decode($content['content'], true)['repice'];
$data['content'] = json_decode($recipe[0]['content']);
$data['deployment'] = $recipe[0]['deployment'];
$data['dosage'] = $recipe[0]['dosage'];
$data['consumption'] = $recipe[0]['consumption'];
$data['directions'] = $recipe[0]['directions'];
$data['usage'] = $recipe[0]['usage'];
$data['fufa_id'] = $recipe[0]['fufa_id'];
$data['volume'] = $recipe[0]['volume'];
$data['is_deepfry'] = $recipe[0]['is_deepfry'];
$data['cm_id'] = $recipe[0]['cm_id'];
$data['total_price'] = $recipe[0]['total_price'];
$data['remark'] = $recipe[0]['remark'];
return $data;
} else {//西药
$Prescription = Prescription::find()->where(['id' => $p_id, 'is_deleted' => 0])->one();
if (!$Prescription) throw new Exception('处方不存在');
$repice = json_decode($Prescription['content'], true)['repice'];
$data = [];
foreach ($repice as $v) {
$item = [
'id' => $v['id'],
'content' => json_decode($v['content']),
'drug_name' => json_decode($v['content'])->drug_name,
'source' => json_decode($v['content'])->source,
'function' => json_decode($v['content'])->function,
'usage' => json_decode($v['content'])->usage,
'specification' => json_decode($v['content'])->specification,
'instruction' => json_decode($v['content'])->instruction,
'image' => json_decode($v['content'])->image,
'number' => $v['number'],
'available_days' => $v['available_days'],
'total_price' => $v['total_price'],
'created_at' => $v['created_at'],
'type_id' => json_decode($v['content'])->type_id,
'time_id' => json_decode($v['content'])->time_id,
'frequency_id' => json_decode($v['content'])->frequency_id,
'unit_id' => json_decode($v['content'])->unit_id,
'usetime' => $v['usetime']['name'],
'frequency' => $v['frequency']['name'],
'grain_number' => $v['grain_number'],
'westUnit' => $v['westUnit']['name'],
'types' => $v['usetype']['name'], //药的使用方式
];
$data[] = $item;
}
return $data;
}
}
//商品订单的商品明细
public function actionProductOrderItem($id = null)
{
return ProductOrderItems::find()->with('drug')->where(['product_order_id' => $id])->asArray()->all();
}
/**
* @doc-name 导出产品订单
*/
public function actionExportProductOrder()
{
\Yii::$app->response->format = Response::FORMAT_RAW;
$params = \Yii::$app->request->get();
$parameter['header'] = ['易票联订单号','订单号','诊所名称', '商品名称', '规格', '厂家', '单价', '加工费', '诊疗费', '销售价', '采购价','总价', '运费', '医生', '订单状态', '售后状态', '下单时间', '收件人姓名', '电话', '地址', '支付时间'];
$parameter['data'] = ProductOrder::inventory($params,\Yii::$app->user->identity->store_id);
ExportService::ExportByCors($parameter);
}
/**
* @doc-name 代煎费
*/
public function actionDaijianFei($id=null)
{
$ProductOrder=ProductOrder::find()->where(['id'=>$id])->one();
if (!$ProductOrder)throw new Exception('产品订单不存在');
$Prescription=Prescription::findOne(['id'=>$ProductOrder['p_id'],'is_deleted'=>0]);
if ($Prescription['prescription_type'] == 1 || $Prescription['prescription_type']==3 ) {
$SystemConfig = SystemConfig::findOne(['type' => 1]);
return $SystemConfig['value'];
}else{
return '无';
}
}
}