2024-09-19 11:32:39 +08:00
< ? php
namespace admin\controllers\doctor ;
use admin\components\BaseAdminController ;
use common\enums\UserRoleEnum ;
use common\enums\UserStatusEnum ;
use common\helpers\FuncHelper ;
2024-12-19 19:20:27 +08:00
use common\models\oldDb\Department ;
use common\models\oldDb\DoctorInfo ;
use common\models\oldDb\DoctorPracticing ;
use common\models\oldDb\DoctorService ;
use common\models\oldDb\DoctorTitle ;
use common\models\oldDb\Ledger ;
use common\models\oldDb\ProductOrder ;
use common\models\oldDb\Reconciliation ;
use common\models\oldDb\ServiceUser ;
use common\models\oldDb\Store ;
use common\models\oldDb\StoreDoctor ;
2024-09-19 11:32:39 +08:00
use GuzzleHttp\Client ;
use yii\base\Exception ;
class DoctorController extends BaseAdminController
{
/**
* @ doc - name 医生列表
* @ doc - param int status 状态0待激活1待审核2已认证3拒绝 ( 需要修改激活资料 ) 4 全部医生5停用
* @ doc - param string name 姓名 / optional
* @ doc - param string mobile 电话 / optional
* @ doc - param int depart_id 科室id / optional
* @ doc - param int title_id 职称id / optional
* @ doc - param int identity 身份1中医2西医 / optional
* @ doc - param int page 页码 1 optional
* @ doc - param int page_size 每页条数 10 optional
* @ doc - return mixed @ List { ServiceUser { id , is_vip - int - 是否平台搜索1否2是 , reason - string - 拒绝原因 , status - int - 状态0待激活1待审核2已认证3拒绝 ( 需要修改激活资料 ), created_at - string - 注册时间 , updated_at - string - 审核通过时间 , avator - string - 医生头像 , is_star - int - 是否是名医0否1是 , name - string - 医生姓名 , depart - string - 医生科室 , hospital_id - int - 医院ID , hospital - string - 医院 , yard - string - 院区 , title - string - 职称 , good_at - string - 擅长 , process - int - 用户同步状态0身份审核通过1证书签发2设置签章3用户注销4申请拒绝5用户停用6修改手机号7用户启用9用户删除 , note - string - 当拒绝时或证书签发时 : 审核拒绝原因 / 证书信息 , @ Service { DoctorService { register_status , register_price }, @ Practicing { qualification - string - 资格证书 , practicing - string - 执业证书 , title - 职称证书 }, @ store { store_id - int - 门店ID }}} 医生列表
* @ doc - return mixed @ Pagination { total - int - 总数量 , totalPage - int - 总页数 , pageSize - int - 每页条数 } 分页数据
*/
public function actionList ()
{
$get = \Yii :: $app -> request -> get ();
$this -> requestValidate ( $get , [
[ 'status' , 'required' ]
]);
$name = $get [ 'name' ];
$mobile = $get [ 'mobile' ];
2024-12-05 15:57:38 +08:00
$depart_id = $get [ 'depart_id' ] ? ? 0 ;
$title_id = $get [ 'title_id' ] ? ? 0 ;
$identity = $get [ 'identity' ] ? ? 0 ;
2024-09-19 11:32:39 +08:00
$admin = \Yii :: $app -> user -> identity ;
switch ( $get [ 'status' ]) {
case 0 :
$query = ServiceUser :: find () -> alias ( 'su' ) -> where ([
'su.role' => UserRoleEnum :: DOCTOR ,
'su.status' => UserStatusEnum :: WAIT_JH ,
'su.is_delete' => 0
]);
break ;
case 1 :
$query = ServiceUser :: find () -> alias ( 'su' ) -> where ([
'su.role' => UserRoleEnum :: DOCTOR ,
'su.status' => UserStatusEnum :: WAIT_SH ,
'su.is_delete' => 0
]);
break ;
case 2 :
$query = ServiceUser :: find () -> alias ( 'su' ) -> where ([
'su.role' => UserRoleEnum :: DOCTOR ,
'su.status' => UserStatusEnum :: OK ,
'su.is_delete' => 0
]);
break ;
case 3 :
$query = ServiceUser :: find () -> alias ( 'su' ) -> where ([
'su.role' => UserRoleEnum :: DOCTOR ,
'su.status' => UserStatusEnum :: REFUSED ,
'su.is_delete' => 0
]);
break ;
case 4 :// 全部医生
//门店
if ( $admin -> role == UserRoleEnum :: STORE_ADMIN ) {
$su_id = StoreDoctor :: find () -> select ( 'su_id' ) -> where ([
'store_id' => $admin -> store_id ,
'is_delete' => 0
]) -> column ();
return ServiceUser :: find () -> alias ( 'su' ) -> where ([
'su.role' => UserRoleEnum :: DOCTOR ,
'su.status' => [ UserStatusEnum :: REFUSED , UserStatusEnum :: OK , UserStatusEnum :: WAIT_SH , UserStatusEnum :: FORBID ],
'su.is_delete' => 0
]) -> andWhere ([ 'in' , 'su.id' , $su_id ]) -> with ([ 'docInfo' , 'docPracticing' , 'docService' ]) -> asArray () -> all ();
}
//超管
$query = ServiceUser :: find () -> alias ( 'su' ) -> where ([
'su.role' => UserRoleEnum :: DOCTOR ,
'su.status' => [ UserStatusEnum :: REFUSED , UserStatusEnum :: OK , UserStatusEnum :: WAIT_SH , UserStatusEnum :: FORBID ],
'su.is_delete' => 0
]);
break ;
case 5 :
$query = ServiceUser :: find () -> alias ( 'su' ) -> where ([
'su.role' => UserRoleEnum :: DOCTOR ,
'su.status' => UserStatusEnum :: FORBID ,
'su.is_delete' => 0
]);
break ;
default :
throw new \yii\db\Exception ( '参数错误' );
}
$departs = Department :: find () -> select ( 'id,name' ) -> where ([ '<>' , 'id' , 0 ]) -> asArray () -> all ();
$id = 0 ;
foreach ( $departs as $v ) {
if ( in_array ( $depart_id , $v ) == true ) {
$id = $v [ 'id' ];
}
}
$query -> joinWith ([ 'docInfo' => function ( $q ) use ( $name , $id , $mobile , $depart_id , $title_id , $identity ) {
$q -> alias ( 'i' );
if ( ! empty ( $name )) { //搜索姓名
$q -> andWhere ([ 'like' , 'i.name' , $name ]);
}
if ( ! empty ( $mobile )) { //手机号
$q -> andWhere ([ 'i.mobile' => $mobile ]);
}
if ( ! empty ( $depart_id )) { //科室
$q -> andWhere ([ 'i.depart_id' => $depart_id ]);
}
if ( ! empty ( $title_id )) { //职称搜索
$q -> andWhere ([ 'i.title_id' => $title_id ]);
}
if ( ! empty ( $identity )) { //身份1中医2西医
$q -> andWhere ([ 'i.identity' => $identity ]);
}
}]);
$this -> field = [
ServiceUser :: class => [
'id' ,
'reason' , 'status' , 'created_at' , 'updated_at' ,
'depart_id' => 'docInfo.depart_id' ,
'avator' => 'docInfo.avatar' ,
'is_star' => 'docInfo.star' ,
'name' => 'docInfo.name' ,
'mobile' ,
2024-11-01 11:15:14 +08:00
'is_sync' => 'docInfo.is_sync' ,
2024-09-19 11:32:39 +08:00
'depart' => 'docInfo.depart.name' ,
'title1' => 'docInfo.title.name' ,
'intro' => 'docInfo.intro' ,
'good_at' => 'docInfo.good_at' ,
'identity' => 'docInfo.identity' ,
'service' => 'docService' ,
'practicing' => 'docPracticing' ,
'idcard' => 'docInfo.idcard' ,
'store' => function ( $m ) {
return StoreDoctor :: find () -> select ([ 'store_id' ]) -> where ([
'su_id' => $m -> id ,
'is_delete' => 0
2024-11-15 13:35:35 +08:00
]) -> with ([ 'store' => function ( $query ) {
return $query -> select ([ 'name' , 'id' ]);
}]) -> asArray () -> all ();
2024-09-19 11:32:39 +08:00
}
],
DoctorService :: class => [
'su_id' , 'register_status' , 'register_price'
],
DoctorPracticing :: class => [
'su_id' , 'qualification' , 'practicing' , 'title' ,
]
];
return $this -> create ( $query , $get );
}
/**
* @ doc - name 审核通过操作 + 拒绝操作(入驻)
* @ doc - desc 当status为2时必填reason
* @ doc - param int status 状态1审核通过操作2拒绝操作
* @ doc - param string reason 拒绝的原因 / optional
* @ doc - param int doctor_id 医生id
*/
public function actionIntoCheck ()
{
$post = \Yii :: $app -> request -> post ();
$this -> requestValidate ( $post , [
[[ 'status' , 'doctor_id' ], 'required' ]
]);
$is_exist = ServiceUser :: find () -> alias ( 'su' ) -> where ([
'id' => $post [ 'doctor_id' ],
'su.role' => UserRoleEnum :: DOCTOR ,
'su.status' => UserStatusEnum :: WAIT_SH ,
'su.is_delete' => 0
]) -> one ();
switch ( $post [ 'status' ]) {
case 1 :
if ( ! $is_exist ) throw new Exception ( '待审核的医生不存在' );
$is_exist -> status = UserStatusEnum :: OK ;
$is_exist -> updated_at = time ();
$is_exist -> saveOrFail ();
return [ '审核通过' ];
case 2 :
if ( ! $is_exist ) throw new Exception ( '医生不存在' );
if ( empty ( $post [ 'reason' ])) throw new Exception ( '拒绝的原因不能为空' );
$is_exist -> status = UserStatusEnum :: REFUSED ;
$is_exist -> updated_at = time ();
$is_exist -> reason = $post [ 'reason' ];
$is_exist -> saveOrFail ();
return [ '拒绝审核' ];
default :
throw new \yii\db\Exception ( '参数错误' );
}
}
/**
* @ doc - name 医生详情
* @ doc - param int doctor_id 医生id
* @ doc - return mixed @ List { ServiceUser { id , is_vip - int - 是否平台搜索1否2是 , reason - string - 拒绝原因 , status - int - 状态0待激活1待审核2已认证3拒绝 ( 需要修改激活资料 ), created_at - string - 注册时间 , updated_at - string - 审核通过时间 , avator - string - 医生头像 , is_star - int - 是否是名医0否1是 , name - string - 医生姓名 , depart - string - 医生科室 , hospital_id - int - 医院ID , hospital - string - 医院 , yard - string - 院区 , title - string - 职称 , good_at - string - 擅长 , process - int - 用户同步状态0身份审核通过1证书签发2设置签章3用户注销4申请拒绝5用户停用6修改手机号7用户启用9用户删除 , note - string - 当拒绝时或证书签发时 : 审核拒绝原因 / 证书信息 , @ Service { DoctorService { register_status , register_price }, @ Practicing { qualification - string - 资格证书 , practicing - string - 执业证书 , title - 职称证书 }, @ store { store_id - int - 门店ID }}} 医生列表
*/
public function actionInfo ()
{
2024-12-05 16:13:51 +08:00
$post = \Yii :: $app -> request -> post ();
$this -> requestValidate ( $post , [
2024-09-19 11:32:39 +08:00
[ 'doctor_id' , 'required' ]
]);
2024-12-05 16:13:51 +08:00
$doctor = $post [ 'doctor_id' ];
2024-09-19 11:32:39 +08:00
$ServiceUser = ServiceUser :: find () -> alias ( 'su' ) -> where ([
2024-12-05 16:13:51 +08:00
'su.id' => $post [ 'doctor_id' ],
2024-09-19 11:32:39 +08:00
'su.role' => UserRoleEnum :: DOCTOR ,
'su.is_delete' => 0
]) -> with ([ 'docInfo' , 'docIdentity' , 'docPracticing' , 'docService' ])
-> asArray () -> one ();
if ( ! $ServiceUser ) throw new Exception ( '医生不存在' );
$ServiceUser [ 'store' ] = StoreDoctor :: find () -> select ([ 'store_id' ]) -> where ([
'su_id' => $doctor ,
'is_delete' => 0
]) -> with ([ 'store' ]) -> asArray () -> all ();
return $ServiceUser ;
}
/**
* 医生同步互医审核通过
* @ return string []
* @ throws Exception
* @ throws \GuzzleHttp\Exception\GuzzleException
*/
public function actionSyncDoctor ()
{
$serviceUserId = \Yii :: $app -> request -> post ( 'doctor_id' );
2024-11-01 11:15:14 +08:00
if ( empty ( $serviceUserId )) {
2024-09-19 11:32:39 +08:00
throw new Exception ( '医生id不能为空' );
}
$serviceUser = ServiceUser :: find () -> where ([
'id' => $serviceUserId
]) -> with ( 'docInfo' , 'docIdentity' , 'docPracticing' ) -> one ();
if ( empty ( $serviceUser ) || empty ( $serviceUser -> docInfo ) || empty ( $serviceUser -> docIdentity ) || empty ( $serviceUser -> docPracticing )) {
throw new Exception ( '医生信息错误' );
}
2024-11-01 11:15:14 +08:00
if ( $serviceUser -> docInfo -> is_sync == 2 ) {
2024-09-19 11:32:39 +08:00
throw new Exception ( '该医生已同步至互医' );
}
$params = [
'id' => $serviceUserId ,
'store_id' => implode ( ',' , json_decode ( $serviceUser -> docInfo -> store_id , true )),
'mobile' => $serviceUser -> mobile ,
'avatar' => $serviceUser -> docInfo -> avatar ,
'name' => $serviceUser -> docInfo -> name ,
'id_card' => $serviceUser -> docInfo -> idcard ,
'good_at' => $serviceUser -> docInfo -> good_at ,
'intro' => $serviceUser -> docInfo -> intro ,
'card_up' => $serviceUser -> docIdentity -> card_up ,
'card_down' => $serviceUser -> docIdentity -> card_down ,
'work_avatar' => $serviceUser -> docIdentity -> work_avator ,
'qualification' => $serviceUser -> docPracticing -> qualification ,
'practicing' => $serviceUser -> docPracticing -> practicing ,
'title' => $serviceUser -> docPracticing -> title
];
$response = ( new Client ([ 'http_errors' => false ])) -> post (
\Yii :: $app -> params [ 'platform' ][ 'url' ] . " /platform/v1/sync/doctor " ,
[
'headers' => [ 'Content-Type' => 'application/json' , 'Authorization' => " Bearer " . \Yii :: $app -> params [ 'platform' ][ 'token' ]],
\GuzzleHttp\RequestOptions :: JSON => $params
]
);
$result = json_decode ( $response -> getBody (), true );
if ( $result [ 'errcode' ]) {
throw new Exception ( $result [ 'msg' ]);
}
DoctorInfo :: updateAll ([ 'is_sync' => 1 ], [ 'su_id' => $serviceUserId ]);
return [ '同步互医完成' ];
}
/**
* @ doc - name 中西医
* @ doc - return array aa bb
*/
public function actionIdentity ()
{
return [
[ 'key' => 0 , 'value' => '中医' ],
[ 'key' => 1 , 'value' => '西医' ],
];
}
/**
* @ doc - name 职称列表
* @ doc - return mixed @ DoctorTitle { * } 职称
*/
public function actionTitle ()
{
return DoctorTitle :: find () -> all ();
}
/**
* @ doc - name 医生绑定门店
* @ doc - param int doctor_id 医生ID
* @ doc - param string store_id 门店
*/
public function actionDocTieStore ()
{
$post = \Yii :: $app -> request -> post ();
$this -> requestValidate ( $post , [
[[ 'doctor_id' , 'store_id' ], 'required' ]
]);
$store_id = StoreDoctor :: find () -> select ([ 'store_id' ]) -> where ([
'su_id' => $post [ 'doctor_id' ],
'is_delete' => 0
]) -> column ();
$ids = explode ( ',' , $post [ 'store_id' ]);
$tie_id = array_diff ( $ids , $store_id ); //要绑定的
$del_id = array_diff ( $store_id , $ids ); //解除绑定的
$t = \Yii :: $app -> db -> beginTransaction ();
try {
if ( empty ( $tie_id ) && ! empty ( $del_id )) {
foreach ( $del_id as $val ) {
\Yii :: $app -> db -> createCommand () -> update ( 'yii_store_doctor' , [ 'is_delete' => 1 ], [ 'store_id' => $val , 'su_id' => $post [ 'doctor_id' ]]) -> execute ();
}
} else {
foreach ( $tie_id as $v ) {
$store_doctor = new StoreDoctor ();
$store_doctor -> store_id = $v ;
$store_doctor -> su_id = $post [ 'doctor_id' ];
$store_doctor -> saveOrFail ();
}
}
$DoctorInfo = DoctorInfo :: find () -> where ([
'su_id' => $post [ 'doctor_id' ]
]) -> one ();
if ( ! $DoctorInfo ) throw new \Exception ( '医生基本信息未完善' );
$DoctorInfo -> store_id = '[' . $post [ 'store_id' ] . ']' ;
$DoctorInfo -> saveOrFail ();
$t -> commit ();
return [ '绑定成功' ];
} catch ( \Exception $exception ) {
$t -> rollBack ();
throw new \Exception ( $exception -> getMessage ());
}
}
/**
* @ doc - name 账号停用或恢复
* @ doc - param int doctor_id 医生
*/
public function actionAccountForbid ()
{
$post = \Yii :: $app -> request -> post ();
$this -> requestValidate ( $post , [
[ 'doctor_id' , 'required' ]
]);
$ServiceUser = ServiceUser :: find () -> alias ( 'su' ) -> where ([
'id' => $post [ 'doctor_id' ],
'su.role' => UserRoleEnum :: DOCTOR ,
'su.is_delete' => 0
]) -> andWhere ([
'or' ,
[ 'su.status' => UserStatusEnum :: OK ],
[ 'su.status' => UserStatusEnum :: FORBID ],
]) -> one ();
if ( ! $ServiceUser ) throw new \yii\db\Exception ( '医生不存在或账号非已认证状态!!' );
$ServiceUser -> status == UserStatusEnum :: FORBID ? $ServiceUser -> status = UserStatusEnum :: OK : $ServiceUser -> status = UserStatusEnum :: FORBID ;
$ServiceUser -> saveOrFail ();
return [ 'success' ];
}
/**
* @ doc - name 门店的医生明细
* @ doc - param int store_id 医生
*/
public function actionDoctorDetail ()
{
$get = \Yii :: $app -> request -> get ();
$this -> requestValidate ( $get , [
[ 'store_id' , 'required' ]
]);
$store_id = \Yii :: $app -> request -> get ();
$store = Store :: find () -> where ([ 'id' => $get [ 'store_id' ]]) -> one ();
if ( ! $store ) {
throw new \yii\db\Exception ( '门店不存在!!' );
}
if ( ! empty ( $get [ 'start_time' ]) && ! empty ( $get [ 'end_time' ])) {
$start_time = strtotime ( $get [ 'start_time' ] . ' ' . '00:00:00' );
$end_time = strtotime ( $get [ 'end_time' ] . ' ' . '23:59:59' );
}
if ( ! empty ( $start_time ) && ! empty ( $end_time )) {
if ( $start_time == $end_time ) { //筛选当天
$time = FuncHelper :: getDayBE ( $get [ 'start_time' ]);
$end_time = $time ;
}
}
$doctor = StoreDoctor :: find () -> select ( 'su_id' ) -> where ([ 'store_id' => $get [ 'store_id' ]]) -> column ();
$query = DoctorInfo :: find () -> where ([ 'in' , 'su_id' , $doctor ]);
$this -> field = [
DoctorInfo :: class => [
'su_id' ,
'name' ,
'register_price' => function ( $r ) use ( $start_time , $end_time , $store_id ) {
return $this -> actionRegisterPrice ( $r -> su_id , $store_id , $start_time , $end_time );
},
'drug_sale_price' => function ( $r ) use ( $start_time , $end_time , $store_id ) {
return $this -> actionDrugSale ( $r -> su_id , $store_id , $start_time , $end_time );
},
'drug_give_price' => function ( $r ) use ( $start_time , $end_time , $store_id ) {
return $this -> actionDrugGive ( $r -> su_id , $store_id , $start_time , $end_time );
},
'drug_profit_price' => function ( $r ) use ( $start_time , $end_time , $store_id ) {
return $this -> actionDrugProfit ( $r -> su_id , $store_id , $start_time , $end_time );
},
2024-11-01 11:15:14 +08:00
'treatement_price' => function ( $r ) use ( $start_time , $end_time , $store_id ) {
2024-09-19 11:32:39 +08:00
return $this -> actionTreatementPrice ( $r -> su_id , $store_id , $start_time , $end_time );
},
2024-11-01 11:15:14 +08:00
'process_price' => function ( $r ) use ( $start_time , $end_time , $store_id ) {
2024-09-19 11:32:39 +08:00
return $this -> actionProcessPrice ( $r -> su_id , $store_id , $start_time , $end_time );
},
]
];
return $this -> create ( $query , $get );
}
/**
* @ doc - name 加工费
* @ doc - param int su_id 医生
*/
public function actionProcessPrice ( $su_id = '' , $store_id = '' , $start_time = '' , $end_time = '' )
{
if ( ! empty ( $start_time ) && ! empty ( $end_time )) {
$get_start_time = date ( 'Y-m-d H:i:s' , $start_time );
if ( $start_time == $end_time ) { //筛选某一天
$time = FuncHelper :: getDayBE ( $get_start_time );
$start_time = strtotime ( $get_start_time );
$end_time = $time [ 1 ];
}
$data = [ 'between' , 'r.xd_time' , $start_time , $end_time ];
}
$price = Ledger :: find () -> alias ( 'r' ) -> where ([ 'r.su_id' => $su_id , 'r.user_id' => $store_id , 'r.status' => 1 , 'r.user_type' => 1 , 'r.order_type' => 1 , 'r.fee_type' => 5 ]) -> andWhere ( $data ? ? '' ) -> sum ( 'money' );
return $price ? ? 0 ;
}
2024-11-01 11:15:14 +08:00
/**
2024-09-19 11:32:39 +08:00
* @ doc - name 诊疗费
* @ doc - param int su_id 医生
*/
public function actionTreatementPrice ( $su_id = '' , $store_id = '' , $start_time = '' , $end_time = '' )
{
if ( ! empty ( $start_time ) && ! empty ( $end_time )) {
$get_start_time = date ( 'Y-m-d H:i:s' , $start_time );
if ( $start_time == $end_time ) { //筛选某一天
$time = FuncHelper :: getDayBE ( $get_start_time );
$start_time = strtotime ( $get_start_time );
$end_time = $time [ 1 ];
}
$data = [ 'between' , 'r.xd_time' , $start_time , $end_time ];
}
$price = Ledger :: find () -> alias ( 'r' ) -> where ([ 'r.su_id' => $su_id , 'r.user_id' => $store_id , 'r.status' => 1 , 'r.user_type' => 1 , 'r.order_type' => 1 , 'r.fee_type' => 6 ]) -> andWhere ( $data ? ? '' ) -> sum ( 'money' );
return $price ? ? 0 ;
}
/**
* @ doc - name 挂号费
* @ doc - param int su_id 医生
*/
public function actionRegisterPrice ( $su_id = '' , $store_id = '' , $start_time = '' , $end_time = '' )
{
if ( ! empty ( $start_time ) && ! empty ( $end_time )) {
$get_start_time = date ( 'Y-m-d H:i:s' , $start_time );
if ( $start_time == $end_time ) { //筛选某一天
$time = FuncHelper :: getDayBE ( $get_start_time );
$start_time = strtotime ( $get_start_time );
$end_time = $time [ 1 ];
}
$data = [ 'between' , 'r.xd_time' , $start_time , $end_time ];
}
$price = Ledger :: find () -> alias ( 'r' ) -> where ([ 'r.su_id' => $su_id , 'r.user_id' => $store_id , 'r.status' => 1 , 'r.user_type' => 1 , 'r.order_type' => 2 ]) -> andWhere ( $data ? ? '' ) -> sum ( 'money' );
return $price ? ? 0 ;
}
/**
* @ doc - name 药品总销售额
*/
public function actionDrugSale ( $su_id = null , $store_id = null , $start_time = '' , $end_time = '' )
{
if ( ! empty ( $start_time ) && ! empty ( $end_time )) {
$get_start_time = date ( 'Y-m-d H:i:s' , $start_time );
if ( $start_time == $end_time ) { //筛选某一天
$time = FuncHelper :: getDayBE ( $get_start_time );
$start_time = strtotime ( $get_start_time );
$end_time = $time [ 1 ];
}
$data = [ 'between' , 'r.xd_time' , $start_time , $end_time ];
}
$order_id = ProductOrder :: find () -> select ( 'id' ) -> where ([ 'su_id' => $su_id , 'is_pay' => 1 , 'store_id' => $store_id ]) -> column ();
$total_price = Reconciliation :: find () -> alias ( 'r' ) -> where ([
'in' , 'r.order_id' , $order_id ,
'r.status' => 1
]) -> andWhere ( $data ? ? '' ) -> sum ( 'total_price' );
return $total_price ? ? 0 ;
}
/**
* @ doc - name 药品总供货金额
*/
public function actionDrugGive ( $su_id = null , $store_id = null , $start_time = '' , $end_time = '' )
{
if ( ! empty ( $start_time ) && ! empty ( $end_time )) {
$get_start_time = date ( 'Y-m-d H:i:s' , $start_time );
if ( $start_time == $end_time ) { //筛选某一天
$time = FuncHelper :: getDayBE ( $get_start_time );
$start_time = strtotime ( $get_start_time );
$end_time = $time [ 1 ];
}
$data = [ 'between' , 'r.xd_time' , $start_time , $end_time ];
}
$order_id = ProductOrder :: find () -> select ( 'id' ) -> where ([ 'su_id' => $su_id , 'is_pay' => 1 , 'store_id' => $store_id ]) -> column ();
//供货金额
$total_buy_price = Reconciliation :: find () -> alias ( 'r' ) -> where ([
'in' , 'r.order_id' , $order_id ,
'r.status' => 1
]) -> andWhere ( $data ? ? '' ) -> sum ( 'total_buy_price' );
return $total_buy_price ? ? 0 ;
}
/**
* @ doc - name 药品总利润
*/
public function actionDrugProfit ( $su_id = null , $store_id = null , $start_time = '' , $end_time = '' )
{
if ( ! empty ( $start_time ) && ! empty ( $end_time )) {
$get_start_time = date ( 'Y-m-d H:i:s' , $start_time );
if ( $start_time == $end_time ) { //筛选某一天
$time = FuncHelper :: getDayBE ( $get_start_time );
$start_time = strtotime ( $get_start_time );
$end_time = $time [ 1 ];
}
$data = [ 'between' , 'r.xd_time' , $start_time , $end_time ];
}
$order_id = ProductOrder :: find () -> select ( 'id' ) -> where ([ 'su_id' => $su_id , 'is_pay' => 1 , 'store_id' => $store_id ]) -> column ();
//销售额
$total_price = Reconciliation :: find () -> alias ( 'r' ) -> where ([
'in' , 'r.order_id' , $order_id ,
'r.status' => 1
]) -> andWhere ( $data ? ? '' ) -> sum ( 'total_price' );
//供货金额
$total_buy_price = Reconciliation :: find () -> alias ( 'r' ) -> where ([
'in' , 'r.order_id' , $order_id ,
'r.status' => 1
]) -> andWhere ( $data ? ? '' ) -> sum ( 'total_buy_price' );
$profit = bcsub ( $total_price , $total_buy_price , 4 );
return $profit ? ? 0 ;
}
/**
* @ doc - name 门诊收入药品销售
*/
public function actionStoreIncomeDrugSale ()
{
2024-11-01 11:15:14 +08:00
// 获取请求参数
2024-09-19 11:32:39 +08:00
$get = \Yii :: $app -> request -> get ();
2024-11-01 11:15:14 +08:00
// 如果设置了开始时间和结束时间,则计算时间戳
2024-09-19 11:32:39 +08:00
if ( ! empty ( $get [ 'start_time' ]) && ! empty ( $get [ 'end_time' ])) {
$start_time = strtotime ( $get [ 'start_time' ] . ' ' . '00:00:00' );
$end_time = strtotime ( $get [ 'end_time' ] . ' ' . '23:59:59' );
}
2024-11-01 11:15:14 +08:00
// 获取当前用户身份
2024-09-19 11:32:39 +08:00
$user = \Yii :: $app -> user -> identity ;
2024-11-01 11:15:14 +08:00
// 初始化查询对象
2024-09-19 11:32:39 +08:00
$query = ProductOrder :: find () -> alias ( 'p' );
2024-11-01 11:15:14 +08:00
// $Reconciliation_query = Reconciliation::find()->alias('r');
2024-09-19 11:32:39 +08:00
$Ledger_query = Ledger :: find () -> alias ( 're' ) -> where ([ 'order_type' => 2 , 'status' => [ 0 , 1 ]]);
2024-11-01 11:15:14 +08:00
// 计算本月的开始和结束时间戳
2024-09-19 11:32:39 +08:00
$month_start = strtotime ( date ( 'Y-m-01 00:00:00' ));
$month_end = strtotime ( date ( 'Y-m-t 23:59:59' ));
2024-11-01 11:15:14 +08:00
// 如果设置了开始时间和结束时间,则应用到查询中
2024-09-19 11:32:39 +08:00
if ( ! empty ( $start_time ) && ! empty ( $end_time )) {
$data = [ 'between' , 'p.created_at' , $start_time , $end_time ];
}
if ( ! empty ( $start_time ) && ! empty ( $end_time )) {
$register_data = [ 'between' , 're.xd_time' , $start_time , $end_time ];
}
2024-12-13 13:05:48 +08:00
if ( $user -> role == UserRoleEnum :: SUPER_ADMIN ) {
return $this -> actionPlatformStoreIncomeDrugSale ();
}
2024-11-01 11:15:14 +08:00
// 如果用户角色是店铺管理员, 则添加店铺ID条件到查询中
2024-09-19 11:32:39 +08:00
if ( $user -> role == UserRoleEnum :: STORE_ADMIN ) {
$query -> andWhere ([ 'p.store_id' => $user -> store_id ]);
2024-11-01 11:15:14 +08:00
// $Reconciliation_query->andWhere(['r.store_id' => $user->store_id, 'r.status' => 1]);
2024-09-19 11:32:39 +08:00
$Ledger_query -> andWhere ([ 're.user_id' => $user -> store_id ]);
}
2024-12-12 18:32:37 +08:00
// TODO 临时救急
switch ( $user -> role ) {
case UserRoleEnum :: STORE_ADMIN :
// 门店
if ( empty ( $user -> store_id )) throw new \yii\db\Exception ( '门店信息获取失败' );
$where = [ 're.user_id' => $user -> store_id ];
break ;
case UserRoleEnum :: PROVINCE_DAI :
// 省代
if ( empty ( $user -> province_id )) throw new Exception ( '省区经理信息获取失败' . $user -> province_id );
$store_id = Store :: find () -> select ([ 'id' ]) -> where ([ 'province_id' => $user -> province_id ]) -> column ();
$where = [ 'in' , 're.user_id' , $store_id ];
break ;
case UserRoleEnum :: CITY_DAI :
// 市代
if ( empty ( $user -> city_id )) throw new Exception ( '市区经理信息获取失败' . $user -> city_id );
$store_id = Store :: find () -> select ([ 'id' ]) -> where ([ 'city_id' => $user -> city_id ]) -> column ();
$where = [ 'in' , 're.user_id' , $store_id ];
break ;
case UserRoleEnum :: SUPPLY :
// 业务员
if ( empty ( $user -> code )) throw new Exception ( '市区经理信息获取失败' . $user -> code );
$store_id = Store :: find () -> select ([ 'id' ]) -> where ([ 'code' => $user -> code ]) -> column ();
$where = [ 'in' , 're.user_id' , $store_id ];
break ;
default :
// 不需要额外的条件,直接排序
throw new Exception ( '角色参数错误' );
break ;
}
// ds($where);
$Ledger_query -> andWhere ( $where );
2024-09-19 11:32:39 +08:00
2024-11-01 11:15:14 +08:00
// 计算订单销售总额
2024-09-19 11:32:39 +08:00
$order_sale = $query -> andWhere ([
'and' ,
[ '<>' , 'p.refund_status' , 3 ],
[ '<>' , 'p.status' , '4' ],
[ '=' , 'p.is_pay' , 1 ],
]) -> andWhere ( $data ? ? '' ) -> sum ( 'total_pay_price' );
2024-11-01 11:15:14 +08:00
// 获取订单ID列表
2024-09-19 11:32:39 +08:00
$order_id = $query -> andWhere ([
'and' ,
[ '<>' , 'p.refund_status' , 3 ],
[ '<>' , 'p.status' , '4' ],
[ '=' , 'p.is_pay' , 1 ],
]) -> column ();
2024-11-01 11:15:14 +08:00
// 初始化价格总和
2024-09-19 11:32:39 +08:00
$price = 0 ;
2024-11-01 11:15:14 +08:00
$number = 0 ;
// 根据用户角色计算金额
2024-09-19 11:32:39 +08:00
if ( $user -> role == UserRoleEnum :: STORE_ADMIN ) {
2024-11-01 11:15:14 +08:00
// TODO 这里有修改
2024-09-19 11:32:39 +08:00
foreach ( $order_id as $val ) {
2024-11-01 11:15:14 +08:00
$Ledger = Ledger :: find () -> alias ( 're' )
-> where ([ 're.user_id' => $user -> store_id , 'order_id' => $val , 'order_type' => 1 , 'status' => [ 0 , 1 ]])
-> all ();
2024-09-19 11:32:39 +08:00
foreach ( $Ledger as $v ) {
$price_data [] = $v [ 'money' ];
2024-11-01 11:15:14 +08:00
$price = bcadd ( $price , $v [ 'money' ], 3 );
$number += $v [ 'number' ];
2024-09-19 11:32:39 +08:00
}
}
} else {
2024-12-12 18:23:18 +08:00
// TODO 这里有修改
2024-09-19 11:32:39 +08:00
foreach ( $order_id as $val ) {
2024-12-12 18:23:18 +08:00
$Ledger = Ledger :: find () -> alias ( 're' )
2024-12-12 18:23:45 +08:00
-> where ([ 're.user_id' => 2 , 'order_id' => $val , 'order_type' => 1 , 'status' => [ 0 , 1 ]])
2024-12-12 18:23:18 +08:00
-> all ();
2024-09-19 11:32:39 +08:00
foreach ( $Ledger as $v ) {
$price_data [] = $v [ 'money' ];
2024-11-01 11:15:14 +08:00
$price = bcadd ( $price , $v [ 'money' ], 3 );
$number += $v [ 'number' ];
2024-09-19 11:32:39 +08:00
}
}
}
2024-11-01 11:15:14 +08:00
// 获取注册金流水数据
2024-09-19 11:32:39 +08:00
$register_price_data = $Ledger_query -> select ( 'money' ) -> andWhere ( $register_data ? ? '' ) -> column ();
2024-11-01 11:15:14 +08:00
// 计算注册金总额
2024-09-19 11:32:39 +08:00
$register_price = $Ledger_query -> andWhere ( $register_data ? ? '' ) -> sum ( 'money' );
2024-11-01 11:15:14 +08:00
// 计算利润
2024-12-04 10:47:14 +08:00
$profit = bcadd ( $price , $register_price , 2 );
2024-09-19 11:32:39 +08:00
2024-11-01 11:15:14 +08:00
// 返回统计结果
2024-09-19 11:32:39 +08:00
return [
'order_sale' => $order_sale ? ? 0 ,
'store_income' => $profit ? ? 0 ,
'register_price' => $register_price ? ? 0 ,
2024-11-01 11:21:36 +08:00
'register_price_data' => $register_price_data ? ? 0 ,
2024-11-01 11:15:14 +08:00
'price' => $price ? ? 0 , // 利润
2024-11-01 11:21:36 +08:00
'price_data' => $price_data ? ? 0 ,
'total_number' => $number ? ? 0
2024-09-19 11:32:39 +08:00
];
2024-11-01 11:15:14 +08:00
2024-09-19 11:32:39 +08:00
}
/**
* @ doc - name 平台门诊收入药品销售
*/
public function actionPlatformStoreIncomeDrugSale ()
{
$get = \Yii :: $app -> request -> get ();
if ( ! empty ( $get [ 'start_time' ]) && ! empty ( $get [ 'end_time' ])) {
$start_time = strtotime ( $get [ 'start_time' ] . ' ' . '00:00:00' );
$end_time = strtotime ( $get [ 'end_time' ] . ' ' . '23:59:59' );
}
$data = [];
if ( ! empty ( $start_time ) && ! empty ( $end_time )) {
$data = [ 'between' , 'created_at' , $start_time , $end_time ];
}
$where = [];
$admin = \Yii :: $app -> user -> identity ;
2024-11-01 11:15:14 +08:00
if ( $admin -> store_id ) {
2024-09-19 11:32:39 +08:00
$where [ 'store_id' ] = $admin -> store_id ;
}
2024-11-01 11:15:14 +08:00
$total = ProductOrder :: find () -> where ([
2024-09-19 11:32:39 +08:00
'and' ,
[ '<>' , 'refund_status' , 3 ],
[ '=' , 'is_pay' , 1 ],
[ '<>' , 'status' , '4' ],
2025-04-07 10:31:51 +08:00
// ])->andWhere($data)->andWhere($where)->select('sum(total_pay_price) as total_sale_price,sum(market_price+trans_expenses+process_price) as total_market_price')->asArray()->one();
]) -> andWhere ( $data ) -> andWhere ( $where ) -> select ( 'total_pay_price,market_price,trans_expenses,process_price' ) -> asArray () -> all ();
$totalPrice = 0 ;
$storeIncome = 0 ;
foreach ( $total as $v ) {
$totalPrice = bcadd ( $totalPrice , $v [ 'total_pay_price' ], 2 );
// 将所有金额转换为字符串以确保高精度计算
$totalPayPrice = ( string ) $v [ 'total_pay_price' ];
$marketPrice = ( string ) $v [ 'market_price' ];
$transExpenses = ( string ) $v [ 'trans_expenses' ];
$processPrice = ( string ) $v [ 'process_price' ];
// 分步计算,提高可读性
$profit = bcsub ( $totalPayPrice , $marketPrice , 2 ); // 总支付价格减去市场价
$profit = bcsub ( $profit , $transExpenses , 2 ); // 再减去运输费用
$profit = bcsub ( $profit , $processPrice , 2 ); // 最后减去加工费用
// 累加到门诊收入
$storeIncome = bcadd ( $storeIncome , $profit , 2 );
}
2024-09-19 11:32:39 +08:00
return [
2025-04-07 10:31:51 +08:00
'order_sale' => $totalPrice ,
'store_income' => $storeIncome ,
2024-09-19 11:32:39 +08:00
];
}
}