Files
xk-api-yii/admin/controllers/doctor/DoctorController.php
2024-09-19 11:32:39 +08:00

805 lines
31 KiB
PHP

<?php
namespace admin\controllers\doctor;
use admin\components\BaseAdminController;
use admin\controllers\AuthController;
use common\enums\UserRoleEnum;
use common\enums\UserStatusEnum;
use common\helpers\FuncHelper;
use common\models\Department;
use common\models\DoctorInfo;
use common\models\DoctorPracticing;
use common\models\DoctorService;
use common\models\DoctorTitle;
use common\models\Ledger;
use common\models\ProductOrder;
use common\models\Reconciliation;
use common\models\Register;
use common\models\ServiceUser;
use common\models\Store;
use common\models\StoreDoctor;
use GuzzleHttp\Client;
use yii\base\Exception;
use function Symfony\Component\String\s;
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'];
$depart_id = $get['depart_id'];
$title_id = $get['title_id'];
$identity = $get['identity'];
$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',
'is_sync'=> 'docInfo.is_sync',
'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
])->with(['store'])->asArray()->all();
}
],
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()
{
$get = \Yii::$app->request->get();
$this->requestValidate($get, [
['doctor_id', 'required']
]);
$doctor = $get['doctor_id'];
$ServiceUser = ServiceUser::find()->alias('su')->where([
'su.id' => $get['doctor_id'],
'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');
if(empty($serviceUserId)){
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('医生信息错误');
}
if($serviceUser->docInfo->is_sync == 2){
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);
},
'treatement_price' => function ($r) use ($start_time, $end_time, $store_id) {
return $this->actionTreatementPrice($r->su_id, $store_id, $start_time, $end_time);
},
'process_price' => function ($r) use ($start_time, $end_time, $store_id) {
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;
}
/**
* @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()
{
$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');
}
$user = \Yii::$app->user->identity;
$query = ProductOrder::find()->alias('p');
// $Reconciliation_query = Reconciliation::find()->alias('r');
$Ledger_query = Ledger::find()->alias('re')->where(['order_type' => 2, 'status' => [0, 1]]);
$month_start = strtotime(date('Y-m-01 00:00:00'));
$month_end = strtotime(date('Y-m-t 23:59:59'));
if (!empty($start_time) && !empty($end_time)) {
$data = ['between', 'p.created_at', $start_time, $end_time];
}
// if (!empty($start_time) && !empty($end_time)) {
// $datas = ['between', 'r.xd_time', $start_time, $end_time];
// }
if (!empty($start_time) && !empty($end_time)) {
$register_data = ['between', 're.xd_time', $start_time, $end_time];
}
if ($user->role == UserRoleEnum::STORE_ADMIN) {
$query->andWhere(['p.store_id' => $user->store_id]);
// $Reconciliation_query->andWhere(['r.store_id' => $user->store_id, 'r.status' => 1]);
$Ledger_query->andWhere(['re.user_id' => $user->store_id]);
}
//订单销售
$order_sale = $query->andWhere([
'and',
['<>', 'p.refund_status', 3],
['<>', 'p.status', '4'],
['=', 'p.is_pay', 1],
])->andWhere($data ?? '')->sum('total_pay_price');
$order_id = $query->andWhere([
'and',
['<>', 'p.refund_status', 3],
['<>', 'p.status', '4'],
['=', 'p.is_pay', 1],
])->column();
$price = 0;
if ($user->role == UserRoleEnum::STORE_ADMIN) {
foreach ($order_id as $val) {
$Ledger = Ledger::find()->alias('re')->where(['re.user_id' => $user->store_id, 'order_id' => $val, 'order_type' => 1, 'status' => [0, 1]])->all();
foreach ($Ledger as $v) {
$price_data[] = $v['money'];
$price += $v['money'];
}
}
} else {
foreach ($order_id as $val) {
$Ledger = Ledger::find()->alias('re')->where(['order_id' => $val, 'order_type' => 1, 'status' => [0, 1]])->all();
foreach ($Ledger as $v) {
$price_data[] = $v['money'];
$price += $v['money'];
}
}
}
$register_price_data = $Ledger_query->select('money')->andWhere($register_data ?? '')->column();
$register_price = $Ledger_query->andWhere($register_data ?? '')->sum('money');
//利润
$profit = bcadd($price, $register_price, 4);
return [
'order_sale' => $order_sale ?? 0,
'store_income' => $profit ?? 0,
'register_price' => $register_price ?? 0,
'register_price_data' => $register_price_data,
'price' => $price ?? 0,//利润
'price_data' => $price_data,
];
}
/**
* @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;
if($admin->store_id){
$where['store_id'] = $admin->store_id;
}
$total=ProductOrder::find()->where([
'and',
['<>', 'refund_status', 3],
['=', 'is_pay', 1],
['<>', 'status', '4'],
])->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();
// $register_price = 0;
// $order_sale = 0;
// $all_price = 0;
// $all_store = ProductOrder::find()->alias('p')->select('p.store_id')->andWhere([
// 'and',
// ['<>', 'p.refund_status', 3],
// ['=', 'p.is_pay', 1],
// ['<>', 'p.status', '4'],
// ])->andWhere($data ?? '')->groupBy('store_id')->column();
// foreach ($all_store as $key => $value) {
// //订单销售
// $order_sale = ProductOrder::find()->alias('p')->andWhere([
// 'and',
// ['<>', 'p.refund_status', 3],
// ['=', 'p.is_pay', 1],
// ['=','p.store_id' ,$value],
// ['<>', 'p.status', '4'],
// ])->andWhere($data ?? '')->sum('total_pay_price');
// $order_id = ProductOrder::find()->alias('p')->andWhere([
// 'and',
// ['<>', 'p.refund_status', 3],
// ['<>', 'p.status', '4'],
// ['=', 'p.is_pay', 1],
// ['=','p.store_id' ,$value],
// ])->andWhere($data ?? '')->column();
// foreach ($order_id as $val) {
// $all_price += Ledger::find()->alias('re')->andWhere(['re.user_id' => $value,'re.user_type'=>1, 'order_id' => $val, 'order_type' => 1, 'status' => [0, 1]])->sum('money');
// }
// $register_price += Ledger::find()->alias('re')->where(['re.order_type' => 2 ,'re.user_type'=>1,'status' => [0, 1]])->andWhere(['re.user_id' => $value])->andWhere($register_data ?? '')->sum('money');
// }
// //利润
// $profit = bcadd($all_price, $register_price, 4);
return [
'order_sale' => $total['total_sale_price'] ?? 0,
'store_income' => bcsub($total['total_sale_price'],$total['total_market_price'],4),
];
}
}