From 2773e56112cb873b0e9464b09910b9504c6e22ff Mon Sep 17 00:00:00 2001 From: lq Date: Fri, 11 Oct 2024 09:45:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=BB=9F=E8=AE=A1=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E9=97=AE=E9=A2=98=EF=BC=88=E4=B8=8D=E7=9F=A5=E9=81=93?= =?UTF-8?q?=E4=BB=96=E4=BB=AC=E4=B9=8B=E5=89=8D=E4=B8=BA=E4=BB=80=E4=B9=88?= =?UTF-8?q?=E8=BF=99=E6=A0=B7=E5=86=99=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base/StatementAccountController.php | 730 ++++++++++-------- 1 file changed, 387 insertions(+), 343 deletions(-) diff --git a/admin/controllers/base/StatementAccountController.php b/admin/controllers/base/StatementAccountController.php index c1271a1..f448759 100644 --- a/admin/controllers/base/StatementAccountController.php +++ b/admin/controllers/base/StatementAccountController.php @@ -116,7 +116,7 @@ class StatementAccountController extends BaseAdminController /** * @doc-name 统计每个药的销量 */ - public function actionEverySaleNumber($order_id = null, $drug_id = null,$start_time='',$end_time='') + public function actionEverySaleNumber($order_id = null, $drug_id = null, $start_time = '', $end_time = '') { $ProductOrder = ProductOrder::find()->where(['id' => $order_id])->one(); $DrugStoreRelations = DrugStoreRelations::find()->where(['store_id' => $ProductOrder->store_id, 'drug_id' => $drug_id])->one(); @@ -160,47 +160,45 @@ class StatementAccountController extends BaseAdminController if (!empty($start_time) && !empty($end_time)) { - if ($start_time==$end_time){//筛选某一天 - $time=FuncHelper::getDayBE($get['start_time']); + if ($start_time == $end_time) {//筛选某一天 + $time = FuncHelper::getDayBE($get['start_time']); - $end_time=$time[1]; + $end_time = $time[1]; } //按照下单时按筛选 - $query->with(['reconciliation'=>function($r)use($start_time,$end_time){ + $query->with(['reconciliation' => function ($r) use ($start_time, $end_time) { $r->alias('re'); - $r->andWhere(['between', 're.xd_time',$start_time,$end_time]); + $r->andWhere(['between', 're.xd_time', $start_time, $end_time]); }]); //挂号搜索 - $register_data=['between', 'r.xd_time',$start_time, $end_time]; + $register_data = ['between', 'r.xd_time', $start_time, $end_time]; } $this->field = [ Store::class => [ 'id', 'name', 'shouzimu', 'position', 'pic', 'contact', 'mobile', 'offical_seal', 'see_rate', - 'sale_number' => function ($s)use($start_time,$end_time) { - return $this->actionSaleNum($s->id,$start_time,$end_time); + 'sale_number' => function ($s) use ($start_time, $end_time) { + return $this->actionSaleNum($s->id, $start_time, $end_time); }, - 'trans_fee'=>function($k)use($start_time,$end_time){ - return $this->actionStoreTransFee($k->id,1,$start_time,$end_time); + 'trans_fee' => function ($k) use ($start_time, $end_time) { + return $this->actionStoreTransFee($k->id, 1, $start_time, $end_time); }, - 'work_fee'=>function($m)use($start_time,$end_time){ - return $this->actionStoreTransFee($m->id,2,$start_time,$end_time); + 'work_fee' => function ($m) use ($start_time, $end_time) { + return $this->actionStoreTransFee($m->id, 2, $start_time, $end_time); }, - 'treatement_fee'=>function($m)use($start_time,$end_time){ - return $this->actionStoreTransFee($m->id,3,$start_time,$end_time); + 'treatement_fee' => function ($m) use ($start_time, $end_time) { + return $this->actionStoreTransFee($m->id, 3, $start_time, $end_time); }, - 'register_price'=>function($m)use($register_data){ - $price= Ledger::find()->alias('r')->where(['user_id'=>$m->id,'status'=>1,'user_type'=>1,'order_type'=>2])->andWhere($register_data??'')->sum('money'); + 'register_price' => function ($m) use ($register_data) { + $price = Ledger::find()->alias('r')->where(['user_id' => $m->id, 'status' => 1, 'user_type' => 1, 'order_type' => 2])->andWhere($register_data ?? '')->sum('money'); // return $this->actionRegister($m->id,$register_data); - return $price??0; + return $price ?? 0; } ] ]; - } else {//药品 - // TODO 对账问题 // 设置查询条件,查找状态为1的对账记录,按ID降序排序 $query = Reconciliation::find()->alias('r')->where(['r.status' => 1])->orderBy(['id' => SORT_DESC]); @@ -254,13 +252,13 @@ class StatementAccountController extends BaseAdminController // 根据开始时间和结束时间过滤对账记录 if (!empty($start_time) && !empty($end_time)) { - if ($start_time==$end_time){//筛选当天 - $time=FuncHelper::getDayBE($get['start_time']); + if ($start_time == $end_time) {//筛选当天 + $time = FuncHelper::getDayBE($get['start_time']); - $end_time=$time[1]; + $end_time = $time[1]; } - $query->andWhere(['between', 'r.xd_time',$start_time, $end_time]); + $query->andWhere(['between', 'r.xd_time', $start_time, $end_time]); } @@ -275,7 +273,7 @@ class StatementAccountController extends BaseAdminController 'pay_time' => function ($m) { return date('Y-m-d H:i:s', $m->pay_time); }, - 'drug_id', 'drug_type', 'total_buy_price', 'status', + 'drug_id', 'drug_type', 'total_buy_price', 'status', 'drug_number' => 'drug.drug_number', 'drug_name' => 'drug.drug_name', 'store_number' => 'productOrder.store.id', @@ -284,7 +282,7 @@ class StatementAccountController extends BaseAdminController 'total_buy_price', 'total_price', 'sale_number' => 0, - 'trans_fee'=>'productOrder.trans_expenses', + 'trans_fee' => 'productOrder.trans_expenses', 'work_fee' => 'productOrder.process_price', 'treatement_fee' => 'productOrder.treatement_price' ] @@ -322,35 +320,6 @@ class StatementAccountController extends BaseAdminController 'page' => 1, 'total' => count($result) ]; - // 定义查询字段及格式化函数 -// $this->field = [ -// Reconciliation::class => [ -// 'id', 'order_id', 'store_id', -// 'pay_time' => function ($m) { -// return date('Y-m-d H:i:s', $m->pay_time); -// }, -// 'drug_id', 'drug_type', 'total_buy_price', 'status', -// 'drug_number' => 'drug.drug_number', -// 'drug_name' => 'drug.drug_name', -// 'store_number' => 'productOrder.store.id', -// 'store' => 'store.name', -// 'number' => function ($model) use ($start_time, $end_time) { -// return $this->calculateSum($model, 'number', $start_time, $end_time); -// }, -// 'total_buy_price' => function ($model) use ($start_time, $end_time) { -// return $this->calculateSum($model, 'total_buy_price', $start_time, $end_time); -// }, -// 'total_price' => function ($model) use ($start_time, $end_time) { -// return $this->calculateSum($model, 'total_price', $start_time, $end_time); -// }, -// 'sale_number' => function ($s)use($start_time,$end_time) { -// return $this->actionEverySaleNumber($s->order_id, $s->drug_id,$start_time,$end_time); -// }, -// 'trans_fee'=>'productOrder.trans_expenses', -// 'work_fee' => 'productOrder.process_price', -// 'treatement_fee' => 'productOrder.treatement_price' -// ] -// ]; } return $this->create($query, $get); @@ -359,31 +328,40 @@ class StatementAccountController extends BaseAdminController /** * @doc-name 销量 */ - public function actionSaleNum($id = null,$start_time='',$end_time='') + public function actionSaleNum($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]; + // 格式化开始时间 + $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]; + // 准备查询条件,用于筛选在开始时间和结束时间之间的数据 + $data = ['between', 'r.xd_time', $start_time, $end_time]; } + // 查询草药和成药的数量、进货价格、销售价格以及挂号收入 $cao_drug_num = Reconciliation::find()->alias('r')->where(['r.status' => 1, 'store_id' => $id, 'drug_type' => [1, 3]]) - ->andWhere($data??'')->groupBy('r.drug_id')->sum('number'); - $cheng_drug_num = Reconciliation::find()->alias('r')->where(['r.status' => 1, 'store_id' => $id, 'drug_type' => [2, 4]]) ->andWhere($data??'')->groupBy('r.drug_id')->sum('number'); + ->andWhere($data ?? '')->groupBy('r.drug_id')->sum('number'); + $cheng_drug_num = Reconciliation::find()->alias('r')->where(['r.status' => 1, 'store_id' => $id, 'drug_type' => [2, 4]])->andWhere($data ?? '')->sum('number'); - $cao_give_price = Reconciliation::find()->alias('r')->where(['r.status' => 1, 'store_id' => $id, 'drug_type' => [1, 3]]) ->andWhere($data??'')->groupBy('r.drug_id')->sum('total_buy_price'); - $cheng_give_price = Reconciliation::find()->alias('r')->where(['r.status' => 1, 'store_id' => $id, 'drug_type' => [2, 4]]) ->andWhere($data??'')->groupBy('r.drug_id')->sum('total_buy_price'); - $cao_sale_price = Reconciliation::find()->alias('r')->where(['r.status' => 1, 'store_id' => $id, 'drug_type' => [1, 3]]) ->andWhere($data??'')->groupBy('r.drug_id')->sum('total_price'); - $cheng_sale_price = Reconciliation::find()->alias('r')->where(['r.status' => 1, 'store_id' => $id, 'drug_type' => [2, 4]]) ->andWhere($data??'')->groupBy('r.drug_id')->sum('total_price'); - $register_price= Ledger::find()->alias('r')->where(['user_id'=>$id,'status'=>1,'user_type'=>1,'order_type'=>2])->andWhere($data??'')->sum('money'); - $total_sale_price = bcadd($cao_sale_price,$cheng_sale_price,4); - $total_give_price = bcadd($cao_give_price,$cheng_give_price,4); + $cao_give_price = Reconciliation::find()->alias('r')->where(['r.status' => 1, 'store_id' => $id, 'drug_type' => [1, 3]])->andWhere($data ?? '')->sum('total_buy_price'); + $cheng_give_price = Reconciliation::find()->alias('r')->where(['r.status' => 1, 'store_id' => $id, 'drug_type' => [2, 4]])->andWhere($data ?? '')->sum('total_buy_price'); + $cao_sale_price = Reconciliation::find()->alias('r')->where(['r.status' => 1, 'store_id' => $id, 'drug_type' => [1, 3]])->andWhere($data ?? '')->sum('total_price'); + $cheng_sale_price = Reconciliation::find()->alias('r')->where(['r.status' => 1, 'store_id' => $id, 'drug_type' => [2, 4]])->andWhere($data ?? '')->sum('total_price'); + $register_price = Ledger::find()->alias('r')->where(['user_id' => $id, 'status' => 1, 'user_type' => 1, 'order_type' => 2])->andWhere($data ?? '')->sum('money'); + + // 计算总销售价格和总进货价格 + $total_sale_price = bcadd($cao_sale_price, $cheng_sale_price, 4); + $total_give_price = bcadd($cao_give_price, $cheng_give_price, 4); + + // 返回包含各种统计数据的数组 return [ 'cao_drug_num' => $cao_drug_num ?? 0, 'cheng_drug_num' => $cheng_drug_num ?? 0, @@ -393,19 +371,20 @@ class StatementAccountController extends BaseAdminController 'cheng_sale_price' => $cheng_sale_price ?? 0, 'total_give_price' => $total_give_price, 'total_sale_price' => $total_sale_price, - 'store_income' => bcadd(bcsub($total_sale_price,$total_give_price,4),$register_price,4) + 'store_income' => bcadd(bcsub($total_sale_price, $total_give_price, 4), $register_price, 4) ]; } - public function actionRegister($store_id,$register_data){ + public function actionRegister($store_id, $register_data) + { - $register_price=Register::find()->alias('r')->where([ + $register_price = Register::find()->alias('r')->where([ 'and', - ['<>','r.status',0], - ['<>','r.status',4], - ['<>','r.status',7], - ])->andWhere(['r.store_id'=>$store_id])->andWhere($register_data??'')->sum('price'); - return $register_price??0; + ['<>', 'r.status', 0], + ['<>', 'r.status', 4], + ['<>', 'r.status', 7], + ])->andWhere(['r.store_id' => $store_id])->andWhere($register_data ?? '')->sum('price'); + return $register_price ?? 0; } /** @@ -439,46 +418,46 @@ class StatementAccountController extends BaseAdminController if (!empty($start_time) && !empty($end_time)) { - if ($start_time==$end_time){//筛选某一天 - $time=FuncHelper::getDayBE($get['start_time']); + if ($start_time == $end_time) {//筛选某一天 + $time = FuncHelper::getDayBE($get['start_time']); - $end_time=$time[1]; + $end_time = $time[1]; } //按照下单时按筛选 - $query->with(['reconciliation'=>function($r)use($start_time,$end_time){ + $query->with(['reconciliation' => function ($r) use ($start_time, $end_time) { $r->alias('re'); - $r->andWhere(['between', 're.xd_time',$start_time,$end_time]); + $r->andWhere(['between', 're.xd_time', $start_time, $end_time]); }]); //挂号搜索 - $register_data=['between', 'r.xd_time',$start_time, $end_time]; + $register_data = ['between', 'r.xd_time', $start_time, $end_time]; } - $parameter['header'] = ['诊所ID','名称', '草药销量', '草药销售金额', '草药供货金额', '成药销量', '成药销售金额','成药供货金额','总销售金额','总供货金额','快递费','加工费','总诊疗费','诊所总收入','总挂号费']; - $exportData = ArrayHelper::toArray($this->create($query, $get)->getModels(),[ + $parameter['header'] = ['诊所ID', '名称', '草药销量', '草药销售金额', '草药供货金额', '成药销量', '成药销售金额', '成药供货金额', '总销售金额', '总供货金额', '快递费', '加工费', '总诊疗费', '诊所总收入', '总挂号费']; + $exportData = ArrayHelper::toArray($this->create($query, $get)->getModels(), [ Store::class => [ 'id', 'name', - 'sale_number' => function ($s) use($start_time,$end_time) { - return $this->actionSaleNum($s->id,$start_time,$end_time); + 'sale_number' => function ($s) use ($start_time, $end_time) { + return $this->actionSaleNum($s->id, $start_time, $end_time); }, - 'trans_fee'=>function($k) use($start_time,$end_time){ - return $this->actionStoreTransFee($k->id,1,$start_time,$end_time); + 'trans_fee' => function ($k) use ($start_time, $end_time) { + return $this->actionStoreTransFee($k->id, 1, $start_time, $end_time); }, - 'work_fee'=>function($m) use($start_time,$end_time){ - return $this->actionStoreTransFee($m->id,2,$start_time,$end_time); + 'work_fee' => function ($m) use ($start_time, $end_time) { + return $this->actionStoreTransFee($m->id, 2, $start_time, $end_time); }, - 'treatement_fee'=>function($m) use($start_time,$end_time){ - return $this->actionStoreTransFee($m->id,3,$start_time,$end_time); + 'treatement_fee' => function ($m) use ($start_time, $end_time) { + return $this->actionStoreTransFee($m->id, 3, $start_time, $end_time); }, - 'register_price'=>function($m) use($register_data){ - $price= Ledger::find()->alias('r')->where(['user_id'=>$m->id,'status'=>1,'user_type'=>1,'order_type'=>2])->andWhere($register_data??'')->sum('money'); - return $price??0; + 'register_price' => function ($m) use ($register_data) { + $price = Ledger::find()->alias('r')->where(['user_id' => $m->id, 'status' => 1, 'user_type' => 1, 'order_type' => 2])->andWhere($register_data ?? '')->sum('money'); + return $price ?? 0; } ] ]); $data = []; - foreach($exportData as $key => $v){ + foreach ($exportData as $key => $v) { $data[$key]['id'] = $v['id']; $data[$key]['name'] = $v['name']; $data[$key]['cao_drug_num'] = $v['sale_number']['cao_drug_num']; @@ -495,7 +474,7 @@ class StatementAccountController extends BaseAdminController $data[$key]['store_income'] = $v['sale_number']['store_income']; $data[$key]['register_price'] = $v['register_price']; } - } elseif($get['type'] == 2) {//药品 + } elseif ($get['type'] == 2) {//药品 $query = Reconciliation::find()->alias('r')->where(['r.status' => 1])->orderBy(['id' => SORT_DESC]); if ($user->role == UserRoleEnum::STORE_ADMIN) { $query->andWhere(['store_id' => $user->store_id]); @@ -539,19 +518,19 @@ class StatementAccountController extends BaseAdminController if (!empty($start_time) && !empty($end_time)) { - if ($start_time==$end_time){//筛选当天 - $time=FuncHelper::getDayBE($get['start_time']); + if ($start_time == $end_time) {//筛选当天 + $time = FuncHelper::getDayBE($get['start_time']); - $end_time=$time[1]; + $end_time = $time[1]; } - $query->andWhere(['between', 'r.xd_time',$start_time, $end_time]); + $query->andWhere(['between', 'r.xd_time', $start_time, $end_time]); } $query->groupBy('drug_id'); $parameter['header'] = ['药品名称', '药品编号', '销量', '供货金额', '销售金额']; - $exportData = ArrayHelper::toArray($this->create($query, $get)->getModels(),[ + $exportData = ArrayHelper::toArray($this->create($query, $get)->getModels(), [ Reconciliation::class => [ 'drug_name' => 'drug.drug_name', 'drug_number' => 'drug.drug_number', @@ -568,14 +547,14 @@ class StatementAccountController extends BaseAdminController ]); $data = []; - foreach($exportData as $k=>$v){ + foreach ($exportData as $k => $v) { $data[$k]['drug_name'] = $v['drug_name']; $data[$k]['drug_number'] = $v['drug_number']; $data[$k]['number'] = $v['number']; $data[$k]['total_buy_price'] = $v['total_buy_price']; $data[$k]['total_price'] = $v['total_price']; } - }else{//医生 + } else {//医生 $get = \Yii::$app->request->get(); $this->requestValidate($get, [ ['store_id', 'required'] @@ -606,7 +585,7 @@ class StatementAccountController extends BaseAdminController $query = DoctorInfo::find()->where(['in', 'su_id', $doctor]); $parameter['header'] = ['医生ID', '医生姓名', '总挂号费', '药品销售金额', '总利润']; - $exportData = ArrayHelper::toArray($this->create($query, $get)->getModels(),[ + $exportData = ArrayHelper::toArray($this->create($query, $get)->getModels(), [ DoctorInfo::class => [ 'su_id', 'name', @@ -623,7 +602,7 @@ class StatementAccountController extends BaseAdminController ]); $data = []; - foreach($exportData as $k=>$v){ + foreach ($exportData as $k => $v) { $data[$k]['su_id'] = $v['su_id']; $data[$k]['name'] = $v['name']; $data[$k]['register_price'] = $v['register_price']; @@ -636,44 +615,44 @@ class StatementAccountController extends BaseAdminController ExportService::ExportByCors($parameter); } - public function actionStoreTransFee($store_id=null,$type=null,$start_time='',$end_time='') + public function actionStoreTransFee($store_id = null, $type = 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]; + $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', 'pr.created_at', $start_time,$end_time]; + $data = ['between', 'pr.created_at', $start_time, $end_time]; } - if ($type==1){ + if ($type == 1) { - $price= ProductOrder::find()->alias('pr')->where(['pr.store_id'=>$store_id,'pr.is_pay'=>1,'pr.is_settled'=>1])->andWhere([ + $price = ProductOrder::find()->alias('pr')->where(['pr.store_id' => $store_id, 'pr.is_pay' => 1, 'pr.is_settled' => 1])->andWhere([ 'or', - ['<>','pr.refund_status','3'], - ['<>','pr.status','4'] - ])->andWhere($data??'')->sum('trans_expenses'); + ['<>', 'pr.refund_status', '3'], + ['<>', 'pr.status', '4'] + ])->andWhere($data ?? '')->sum('trans_expenses'); - }elseif ($type==2){ - $price= ProductOrder::find()->alias('pr')->where(['pr.store_id'=>$store_id,'is_pay'=>1,'pr.is_settled'=>1])->andWhere([ + } elseif ($type == 2) { + $price = ProductOrder::find()->alias('pr')->where(['pr.store_id' => $store_id, 'is_pay' => 1, 'pr.is_settled' => 1])->andWhere([ 'or', - ['<>','pr.refund_status','3'], - ['<>','pr.status','4'] - ])->andWhere($data??'')->sum('process_price'); + ['<>', 'pr.refund_status', '3'], + ['<>', 'pr.status', '4'] + ])->andWhere($data ?? '')->sum('process_price'); - }else{ - $price= ProductOrder::find()->alias('pr')->where(['pr.store_id'=>$store_id,'is_pay'=>1,'pr.is_settled'=>1])->andWhere([ + } else { + $price = ProductOrder::find()->alias('pr')->where(['pr.store_id' => $store_id, 'is_pay' => 1, 'pr.is_settled' => 1])->andWhere([ 'or', - ['<>','pr.refund_status','3'], - ['<>','pr.status','4'] - ])->andWhere($data??'')->sum('treatement_price'); + ['<>', 'pr.refund_status', '3'], + ['<>', 'pr.status', '4'] + ])->andWhere($data ?? '')->sum('treatement_price'); } - return $price??0; + return $price ?? 0; } /** @@ -684,135 +663,115 @@ class StatementAccountController extends BaseAdminController public function actionStoreStatistic() { // TODO 这里有问题(统计金额-诊所)需要重构 + // 获取请求参数和当前用户信息 $get = \Yii::$app->request->get(); - $admin=\Yii::$app->user->identity; + $admin = \Yii::$app->user->identity; + // 如果请求参数中包含开始时间和结束时间,则进行处理 if (!empty($get['start_time']) && !empty($get['end_time'])) { + // 将开始时间字符串转换为时间戳,并设置时间为当天的00:00:00 $start_time = strtotime($get['start_time'] . ' ' . '00:00:00'); + // 将结束时间字符串转换为时间戳,并设置时间为当天的23:59:59 $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']); - $start_time=$get['start_time']; - $end_time=$time[1]; + // 如果开始时间或结束时间不为空,则进行条件筛选 + if (!empty($start_time) || !empty($end_time)) { + // 如果开始时间和结束时间相同,则表示用户只想筛选某一天的数据 + if ($start_time == $end_time) { + // 获取筛选当天的开始和结束时间 + $time = FuncHelper::getDayBE($get['start_time']); + $start_time = $get['start_time']; + $end_time = $time[1]; } - $data=['between', 'p.created_at', $start_time, $end_time]; + // 构建产品创建时间的筛选条件 + $data = ['between', 'p.created_at', $start_time, $end_time]; - $datas=['between', 'r.xd_time',$start_time, $end_time]; + // 构建注册时间的筛选条件 + $datas = ['between', 'r.xd_time', $start_time, $end_time]; - $register_data=['between', 're.xd_time', $start_time, $end_time]; + // 构建注册用户时间的筛选条件 + $register_data = ['between', 're.xd_time', $start_time, $end_time]; } - - if ($admin->role==UserRoleEnum::STORE_ADMIN){ - - $cao_all_num = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1,'r.store_id'=>$admin->store_id, 'drug_type' => [1, 3]])->andWhere($datas??'')->groupBy('r.drug_id')->sum('number'); - $cheng_all_num = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1,'r.store_id'=>$admin->store_id, 'drug_type' => [2, 4]])->andWhere($datas??'')->groupBy('r.drug_id')->sum('number'); - $cao_give_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1,'r.store_id'=>$admin->store_id, 'drug_type' => [1, 3]])->andWhere($datas??'')->sum('total_buy_price'); - $cheng_give_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1,'r.store_id'=>$admin->store_id, 'drug_type' => [2, 4]])->andWhere($datas??'')->sum('total_buy_price'); - $cao_sale_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1,'r.store_id'=>$admin->store_id, 'drug_type' => [1, 3]])->andWhere($datas??'')->sum('total_price'); - $cheng_sale_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1,'r.store_id'=>$admin->store_id, 'drug_type' => [2, 4]])->andWhere($datas??'')->sum('total_price'); - - $trans_expenses_price= ProductOrder::find()->alias('p')->where(['store_id'=>$admin->store_id,'is_pay'=>1,'is_settled'=>1])->andWhere([ - 'or', - ['<>','refund_status','3'], - ['<>','status','4'], - ])->andWhere($data??'')->sum('trans_expenses'); - $process_price= ProductOrder::find()->alias('p')->where(['store_id'=>$admin->store_id,'is_pay'=>1,'is_settled'=>1])->andWhere([ - 'or', - ['<>','refund_status','3'], - ['<>','status','4'], - ])->andWhere($data??'')->sum('process_price'); - $treatement_price= ProductOrder::find()->alias('p')->where(['store_id'=>$admin->store_id,'is_pay'=>1,'is_settled'=>1])->andWhere([ - 'or', - ['<>','refund_status','3'], - ['<>','status','4'], - ])->andWhere($data??'')->sum('treatement_price'); - $decoct_price_price= ProductOrder::find()->alias('p')->where(['is_decoct'=>1,'store_id'=>$admin->store_id,'is_pay'=>1,'is_settled'=>1])->andWhere([ - 'or', - ['<>','refund_status','3'], - ['<>','status','4'] - ])->andWhere($data??'')->sum('decoct_price'); - //挂号 -// $register_price=Register::find()->alias('re')->where([ -// 'and', -// ['<>','re.status',0], -// ['<>','re.status',4], -// ['<>','re.status',7], -// 'store_id'=>$admin->store_id -// ])->andWhere($register_data??'')->sum('price'); - $register_price=Ledger::find()->alias('re')->where(['user_id'=>$admin->store_id,'status'=>1,'user_type'=>1,'order_type'=>2])->andWhere($register_data??'')->sum('money'); - - //所有药的销售金额 - $drug_sale_price=Reconciliation::find()->alias('r')->andWhere(['r.status' => 1,'r.store_id'=>$admin->store_id])->andWhere($datas??'')->sum('total_price'); - //所有药的进货金额 - $drug_give_price=Reconciliation::find()->alias('r')->andWhere(['r.status' => 1,'r.store_id'=>$admin->store_id])->andWhere($datas??'')->sum('total_buy_price'); - - $store_income=bcadd($treatement_price,bcsub(bcadd($drug_sale_price,$register_price,4),$drug_give_price,4),2); - //总金额 - $all_sum=bcadd($drug_sale_price,$register_price,4); - $total_price = ProductOrder::find()->alias('p')->where(['store_id'=>$admin->store_id,'is_pay'=>1,'is_settled'=>1])->andWhere([ - 'or', - ['<>','refund_status','3'], - ['<>','status','4'], - ])->andWhere($data??'')->sum('total_pay_price'); - }else{ - - $cao_all_num = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'drug_type' => [1, 3]])->andWhere($datas??'')->groupBy('r.drug_id')->sum('number'); - $cheng_all_num = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'drug_type' => [2, 4]])->andWhere($datas??'')->groupBy('r.drug_id')->sum('number'); - $cao_give_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'drug_type' => [1, 3]])->andWhere($datas??'')->sum('total_buy_price'); - $cheng_give_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'drug_type' => [2, 4]])->andWhere($datas??'')->sum('total_buy_price'); - $cao_sale_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'drug_type' => [1, 3]])->andWhere($datas??'')->sum('total_price'); - $cheng_sale_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'drug_type' => [2, 4]])->andWhere($datas??'')->sum('total_price'); - - $trans_expenses_price= ProductOrder::find()->alias('p')->where(['is_pay'=>1,'is_settled'=>1])->andWhere([ - 'or', - ['<>','refund_status','3'], - ['<>','status','4'], - ])->andWhere($data??'')->sum('trans_expenses'); - $treatement_price= ProductOrder::find()->alias('p')->where(['is_pay'=>1,'is_settled'=>1])->andWhere([ - 'or', - ['<>','refund_status','3'], - ['<>','status','4'], - ])->andWhere($data??'')->sum('treatement_price'); - $process_price= ProductOrder::find()->alias('p')->where(['is_pay'=>1,'is_settled'=>1])->andWhere([ - 'or', - ['<>','refund_status','3'], - ['<>','status','4'], - ])->andWhere($data??'')->sum('process_price'); - $decoct_price_price= ProductOrder::find()->alias('p')->where(['is_pay'=>1,'is_settled'=>1,'is_decoct'=>1])->andWhere([ - 'or', - ['<>','refund_status','3'], - ['<>','status','4'], - ])->andWhere($data??'')->sum('decoct_price'); - $total_price = ProductOrder::find()->alias('p')->where(['is_pay'=>1,'is_settled'=>1])->andWhere([ - 'or', - ['<>','refund_status','3'], - ['<>','status','4'], - ])->andWhere($data??'')->sum('total_pay_price'); - - //挂号 -// $register_price=Register::find()->alias('re')->where([ -// 'and', -// ['<>','re.status',0], -// ['<>','re.status',4], -// ['<>','re.status',7], -// ])->andWhere($register_data??'')->sum('price'); - $register_price=Ledger::find()->alias('re')->where(['status'=>1,'user_type'=>1,'order_type'=>2])->andWhere($register_data??'')->sum('money'); - - //所有药的销售金额 - $drug_sale_price=Reconciliation::find()->alias('r')->andWhere(['r.status' => 1])->andWhere($datas??'')->sum('total_price'); - //所有药的进货金额 - $drug_give_price=Reconciliation::find()->alias('r')->andWhere(['r.status' => 1])->andWhere($datas??'')->sum('total_buy_price'); - - $store_income=bcadd($treatement_price,bcsub(bcadd($drug_sale_price,$register_price,4),$drug_give_price,4),2); - //总金额 - $all_sum=bcadd($drug_sale_price,$register_price,4); + // 根据当前用户的角色,调用不同的统计方法 + if ($admin->role == UserRoleEnum::STORE_ADMIN) { + // 如果是店铺管理员,调用获取店铺统计数据的方法 + return $this->getStoreSum($admin, $data, $datas, $register_data); + } else { + // 如果不是店铺管理员,调用获取管理员统计数据的方法 + return $this->getAdminSum($data, $datas, $register_data); } + } + /** + * 获取门店总统计数据 + * + * @param $admin + * @param $data + * @param $datas + * @param $register_data + * @return array + */ + private function getStoreSum($admin, $data, $datas, $register_data) + { + // 计算草药的总出库数量 + $cao_all_num = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'r.store_id' => $admin->store_id, 'drug_type' => [1, 3]])->andWhere($datas ?? '')->sum('number'); + // 计算成品药的总出库数量 + $cheng_all_num = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'r.store_id' => $admin->store_id, 'drug_type' => [2, 4]])->andWhere($datas ?? '')->sum('number'); + // 计算草药的总进货金额 + $cao_give_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'r.store_id' => $admin->store_id, 'drug_type' => [1, 3]])->andWhere($datas ?? '')->sum('total_buy_price'); + // 计算成品药的总进货金额 + $cheng_give_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'r.store_id' => $admin->store_id, 'drug_type' => [2, 4]])->andWhere($datas ?? '')->sum('total_buy_price'); + // 计算草药的总销售金额 + $cao_sale_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'r.store_id' => $admin->store_id, 'drug_type' => [1, 3]])->andWhere($datas ?? '')->sum('total_price'); + // 计算成品药的总销售金额 + $cheng_sale_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'r.store_id' => $admin->store_id, 'drug_type' => [2, 4]])->andWhere($datas ?? '')->sum('total_price'); + // 计算所有产品的运费总和 + $trans_expenses_price = ProductOrder::find()->alias('p')->where(['store_id' => $admin->store_id, 'is_pay' => 1, 'is_settled' => 1])->andWhere([ + 'or', + ['<>', 'refund_status', '3'], + ['<>', 'status', '4'], + ])->andWhere($data ?? '')->sum('trans_expenses'); + // 计算所有产品的加工费总和 + $process_price = ProductOrder::find()->alias('p')->where(['store_id' => $admin->store_id, 'is_pay' => 1, 'is_settled' => 1])->andWhere([ + 'or', + ['<>', 'refund_status', '3'], + ['<>', 'status', '4'], + ])->andWhere($data ?? '')->sum('process_price'); + // 计算所有产品的治疗费总和 + $treatement_price = ProductOrder::find()->alias('p')->where(['store_id' => $admin->store_id, 'is_pay' => 1, 'is_settled' => 1])->andWhere([ + 'or', + ['<>', 'refund_status', '3'], + ['<>', 'status', '4'], + ])->andWhere($data ?? '')->sum('treatement_price'); + // 计算所有代煎药的代煎费总和 + $decoct_price_price = ProductOrder::find()->alias('p')->where(['is_decoct' => 1, 'store_id' => $admin->store_id, 'is_pay' => 1, 'is_settled' => 1])->andWhere([ + 'or', + ['<>', 'refund_status', '3'], + ['<>', 'status', '4'] + ])->andWhere($data ?? '')->sum('decoct_price'); + // 计算所有挂号的总金额 + $register_price = Ledger::find()->alias('re')->where(['user_id' => $admin->store_id, 'status' => 1, 'user_type' => 1, 'order_type' => 2])->andWhere($register_data ?? '')->sum('money'); + + // 计算所有药的销售金额 + $drug_sale_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'r.store_id' => $admin->store_id])->andWhere($datas ?? '')->sum('total_price'); + // 计算所有药的进货金额 + $drug_give_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'r.store_id' => $admin->store_id])->andWhere($datas ?? '')->sum('total_buy_price'); + + // 计算门店总收入 + $store_income = bcadd($treatement_price, bcsub(bcadd($drug_sale_price, $register_price, 4), $drug_give_price, 4), 2); + // 计算所有药的销售金额和挂号金额的总和 + $all_sum = bcadd($drug_sale_price, $register_price, 4); + // 计算所有订单的总支付金额 + $total_price = ProductOrder::find()->alias('p')->where(['store_id' => $admin->store_id, 'is_pay' => 1, 'is_settled' => 1])->andWhere([ + 'or', + ['<>', 'refund_status', '3'], + ['<>', 'status', '4'], + ])->andWhere($data ?? '')->sum('total_pay_price'); + + // 返回包含各种统计结果的数组 return [ 'cao_all_num' => $cao_all_num ?? 0, 'cheng_all_num' => $cheng_all_num ?? 0, @@ -831,6 +790,90 @@ class StatementAccountController extends BaseAdminController 'drug_sale_price' => $drug_sale_price ?? 0, 'total_price' => $total_price ?? 0 ]; + + } + + /** + * 获取平台统计数据 + * + * @param $data + * @param $datas + * @param $register_data + * @return array + */ + private function getAdminSum($data, $datas, $register_data) + { + // 计算草药和成品药的销售数量 + $cao_all_num = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'drug_type' => [1, 3]])->andWhere($datas ?? '')->sum('number'); + $cheng_all_num = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'drug_type' => [2, 4]])->andWhere($datas ?? '')->sum('number'); + + // 计算草药和成品药的进货金额 + $cao_give_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'drug_type' => [1, 3]])->andWhere($datas ?? '')->sum('total_buy_price'); + $cheng_give_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'drug_type' => [2, 4]])->andWhere($datas ?? '')->sum('total_buy_price'); + + // 计算草药和成品药的销售金额 + $cao_sale_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'drug_type' => [1, 3]])->andWhere($datas ?? '')->sum('total_price'); + $cheng_sale_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'drug_type' => [2, 4]])->andWhere($datas ?? '')->sum('total_price'); + + // 计算物流费用、治疗费、加工费、煎药费和总消费金额 + $trans_expenses_price = ProductOrder::find()->alias('p')->where(['is_pay' => 1, 'is_settled' => 1])->andWhere([ + 'or', + ['<>', 'refund_status', '3'], + ['<>', 'status', '4'], + ])->andWhere($data ?? '')->sum('trans_expenses'); + $treatement_price = ProductOrder::find()->alias('p')->where(['is_pay' => 1, 'is_settled' => 1])->andWhere([ + 'or', + ['<>', 'refund_status', '3'], + ['<>', 'status', '4'], + ])->andWhere($data ?? '')->sum('treatement_price'); + $process_price = ProductOrder::find()->alias('p')->where(['is_pay' => 1, 'is_settled' => 1])->andWhere([ + 'or', + ['<>', 'refund_status', '3'], + ['<>', 'status', '4'], + ])->andWhere($data ?? '')->sum('process_price'); + $decoct_price_price = ProductOrder::find()->alias('p')->where(['is_pay' => 1, 'is_settled' => 1, 'is_decoct' => 1])->andWhere([ + 'or', + ['<>', 'refund_status', '3'], + ['<>', 'status', '4'], + ])->andWhere($data ?? '')->sum('decoct_price'); + $total_price = ProductOrder::find()->alias('p')->where(['is_pay' => 1, 'is_settled' => 1])->andWhere([ + 'or', + ['<>', 'refund_status', '3'], + ['<>', 'status', '4'], + ])->andWhere($data ?? '')->sum('total_pay_price'); + + // 挂号金额 + $register_price = Ledger::find()->alias('re')->where(['status' => 1, 'user_type' => 1, 'order_type' => 2])->andWhere($register_data ?? '')->sum('money'); + + // 所有药的销售金额和进货金额 + $drug_sale_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1])->andWhere($datas ?? '')->sum('total_price'); + $drug_give_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1])->andWhere($datas ?? '')->sum('total_buy_price'); + + // 计算门店收入 + $store_income = bcadd($treatement_price, bcsub(bcadd($drug_sale_price, $register_price, 4), $drug_give_price, 4), 2); + // 总金额 + $all_sum = bcadd($drug_sale_price, $register_price, 4); + + // 返回统计结果 + return [ + 'cao_all_num' => $cao_all_num ?? 0, + 'cheng_all_num' => $cheng_all_num ?? 0, + 'cao_give_price' => $cao_give_price ?? 0, + 'cheng_give_price' => $cheng_give_price ?? 0, + 'cao_sale_price' => $cao_sale_price ?? 0, + 'cheng_sale_price' => $cheng_sale_price ?? 0, + 'trans_expenses_price' => $trans_expenses_price ?? 0, + 'process_price' => $process_price ?? 0, + 'treatement_price' => $treatement_price ?? 0, + 'decoct_price_price' => $decoct_price_price ?? 0, + 'register_price' => $register_price ?? 0, + 'store_income' => $store_income ?? 0, + 'all_sum' => $all_sum ?? 0, + 'drug_give_price' => $drug_give_price ?? 0, + 'drug_sale_price' => $drug_sale_price ?? 0, + 'total_price' => $total_price ?? 0 + ]; + } /** @@ -843,7 +886,7 @@ class StatementAccountController extends BaseAdminController { // TODO 这里有问题(统计金额-药品) $get = \Yii::$app->request->get(); - $admin=\Yii::$app->user->identity; + $admin = \Yii::$app->user->identity; if (!empty($get['start_time']) && !empty($get['end_time'])) { $start_time = strtotime($get['start_time'] . ' ' . '00:00:00'); @@ -851,36 +894,36 @@ class StatementAccountController extends BaseAdminController } if (!empty($start_time) && !empty($end_time)) { - if ($start_time==$end_time){//筛选某一天 - $time=FuncHelper::getDayBE($get['start_time']); - $start_time=$get['start_time']; - $end_time=$time[1]; + if ($start_time == $end_time) {//筛选某一天 + $time = FuncHelper::getDayBE($get['start_time']); + $start_time = $get['start_time']; + $end_time = $time[1]; } - $data=['between', 'xd_time', $start_time, $end_time]; + $data = ['between', 'xd_time', $start_time, $end_time]; } - if ($admin->role==UserRoleEnum::STORE_ADMIN){ - $resData= Reconciliation::find() - ->andWhere(['status' => 1,'store_id'=>$admin->store_id]) - ->andWhere($data??'') + if ($admin->role == UserRoleEnum::STORE_ADMIN) { + $resData = Reconciliation::find() + ->andWhere(['status' => 1, 'store_id' => $admin->store_id]) + ->andWhere($data ?? '') ->all(); - }else{ - $resData= Reconciliation::find() + } else { + $resData = Reconciliation::find() ->andWhere(['status' => 1]) - ->andWhere($data??'') + ->andWhere($data ?? '') ->all(); } $allSale = 0; $totalPrice = 0; - foreach ($resData as $key=>$value) { + foreach ($resData as $key => $value) { $allSale = bcadd($allSale, $value->number, 0); $totalPrice = bcadd($totalPrice, $value->total_price, 0); } return [ - 'all_sale'=>$allSale??0, - 'all_total_price'=>$totalPrice??0, + 'all_sale' => $allSale ?? 0, + 'all_total_price' => $totalPrice ?? 0, ]; } @@ -893,7 +936,7 @@ class StatementAccountController extends BaseAdminController { $get = \Yii::$app->request->get(); - $admin=\Yii::$app->user->identity; + $admin = \Yii::$app->user->identity; if (!empty($get['start_time']) && !empty($get['end_time'])) { $start_time = strtotime($get['start_time'] . ' ' . '00:00:00'); @@ -901,44 +944,44 @@ class StatementAccountController extends BaseAdminController } if (!empty($start_time) && !empty($end_time)) { - if ($start_time==$end_time){//筛选某一天 - $time=FuncHelper::getDayBE($get['start_time']); + if ($start_time == $end_time) {//筛选某一天 + $time = FuncHelper::getDayBE($get['start_time']); - $end_time=$time[1]; + $end_time = $time[1]; } - $datas=['between', 'r.xd_time',$start_time, $end_time]; - $data=['between', 'p.created_at', $start_time, $end_time]; - $register_data=['between', 're.xd_time', $start_time,$end_time]; + $datas = ['between', 'r.xd_time', $start_time, $end_time]; + $data = ['between', 'p.created_at', $start_time, $end_time]; + $register_data = ['between', 're.xd_time', $start_time, $end_time]; } - if ($admin->role==UserRoleEnum::STORE_ADMIN){ + if ($admin->role == UserRoleEnum::STORE_ADMIN) { - $cao_sale_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1,'r.store_id'=>$admin->store_id, 'drug_type' => [1, 3]])->andWhere($datas??'')->sum('total_price'); - $cheng_sale_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1,'r.store_id'=>$admin->store_id, 'drug_type' => [2, 4]])->andWhere($datas??'')->sum('total_price'); + $cao_sale_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'r.store_id' => $admin->store_id, 'drug_type' => [1, 3]])->andWhere($datas ?? '')->sum('total_price'); + $cheng_sale_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'r.store_id' => $admin->store_id, 'drug_type' => [2, 4]])->andWhere($datas ?? '')->sum('total_price'); - $trans_expenses_price= ProductOrder::find()->alias('p')->where(['store_id'=>$admin->store_id,'is_pay'=>1,'is_settled'=>1])->andWhere([ + $trans_expenses_price = ProductOrder::find()->alias('p')->where(['store_id' => $admin->store_id, 'is_pay' => 1, 'is_settled' => 1])->andWhere([ 'or', - ['<>','refund_status','3'], - ['<>','status','4'], - ])->andWhere($data??'')->sum('trans_expenses'); - $process_price= ProductOrder::find()->alias('p')->where(['store_id'=>$admin->store_id,'is_pay'=>1,'is_settled'=>1])->andWhere([ + ['<>', 'refund_status', '3'], + ['<>', 'status', '4'], + ])->andWhere($data ?? '')->sum('trans_expenses'); + $process_price = ProductOrder::find()->alias('p')->where(['store_id' => $admin->store_id, 'is_pay' => 1, 'is_settled' => 1])->andWhere([ 'or', - ['<>','refund_status','3'], - ['<>','status','4'], - ])->andWhere($data??'')->sum('process_price'); - $treatement_price= ProductOrder::find()->alias('p')->where(['store_id'=>$admin->store_id,'is_pay'=>1,'is_settled'=>1])->andWhere([ + ['<>', 'refund_status', '3'], + ['<>', 'status', '4'], + ])->andWhere($data ?? '')->sum('process_price'); + $treatement_price = ProductOrder::find()->alias('p')->where(['store_id' => $admin->store_id, 'is_pay' => 1, 'is_settled' => 1])->andWhere([ 'or', - ['<>','refund_status','3'], - ['<>','status','4'], - ])->andWhere($data??'')->sum('treatement_price'); - $decoct_price_price= ProductOrder::find()->alias('p')->where(['is_decoct'=>1,'store_id'=>$admin->store_id,'is_pay'=>1,'is_settled'=>1])->andWhere([ + ['<>', 'refund_status', '3'], + ['<>', 'status', '4'], + ])->andWhere($data ?? '')->sum('treatement_price'); + $decoct_price_price = ProductOrder::find()->alias('p')->where(['is_decoct' => 1, 'store_id' => $admin->store_id, 'is_pay' => 1, 'is_settled' => 1])->andWhere([ 'or', - ['<>','refund_status','3'], - ['<>','status','4'], - ])->andWhere($data??'')->sum('decoct_price'); + ['<>', 'refund_status', '3'], + ['<>', 'status', '4'], + ])->andWhere($data ?? '')->sum('decoct_price'); - $register_price=Ledger::find()->alias('re')->where(['user_id'=>$admin->store_id,'status'=>1,'user_type'=>1,'order_type'=>2])->andWhere($register_data??'')->sum('money'); + $register_price = Ledger::find()->alias('re')->where(['user_id' => $admin->store_id, 'status' => 1, 'user_type' => 1, 'order_type' => 2])->andWhere($register_data ?? '')->sum('money'); // //挂号 // $register_price=Register::find()->alias('re')->where([ // 'and', @@ -947,34 +990,34 @@ class StatementAccountController extends BaseAdminController // ['<>','re.status',7], // ])->andWhere($register_data??'')->sum('price'); //所有药的进货金额 - $drug_give_price=Reconciliation::find()->alias('r')->andWhere(['r.status' => 1,'r.store_id'=>$admin->store_id])->andWhere($datas??'')->sum('total_buy_price'); + $drug_give_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'r.store_id' => $admin->store_id])->andWhere($datas ?? '')->sum('total_buy_price'); - $drug_price=bcadd($cao_sale_price,$cheng_sale_price,4); + $drug_price = bcadd($cao_sale_price, $cheng_sale_price, 4); - }else{ - $cao_sale_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'drug_type' => [1, 3]])->andWhere($datas??'')->sum('total_price'); - $cheng_sale_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'drug_type' => [2, 4]])->andWhere($datas??'')->sum('total_price'); + } else { + $cao_sale_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'drug_type' => [1, 3]])->andWhere($datas ?? '')->sum('total_price'); + $cheng_sale_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1, 'drug_type' => [2, 4]])->andWhere($datas ?? '')->sum('total_price'); - $trans_expenses_price= ProductOrder::find()->alias('p')->where(['is_pay'=>1,'is_settled'=>1])->andWhere([ + $trans_expenses_price = ProductOrder::find()->alias('p')->where(['is_pay' => 1, 'is_settled' => 1])->andWhere([ 'or', - ['<>','refund_status','3'], - ['<>','status','4'], - ])->andWhere($data??'')->sum('trans_expenses'); - $process_price= ProductOrder::find()->alias('p')->where(['is_pay'=>1,'is_settled'=>1])->andWhere([ + ['<>', 'refund_status', '3'], + ['<>', 'status', '4'], + ])->andWhere($data ?? '')->sum('trans_expenses'); + $process_price = ProductOrder::find()->alias('p')->where(['is_pay' => 1, 'is_settled' => 1])->andWhere([ 'or', - ['<>','refund_status','3'], - ['<>','status','4'], - ])->andWhere($data??'')->sum('process_price'); - $treatement_price= ProductOrder::find()->alias('p')->where(['is_pay'=>1,'is_settled'=>1])->andWhere([ + ['<>', 'refund_status', '3'], + ['<>', 'status', '4'], + ])->andWhere($data ?? '')->sum('process_price'); + $treatement_price = ProductOrder::find()->alias('p')->where(['is_pay' => 1, 'is_settled' => 1])->andWhere([ 'or', - ['<>','refund_status','3'], - ['<>','status','4'], - ])->andWhere($data??'')->sum('treatement_price'); - $decoct_price_price= ProductOrder::find()->alias('p')->where(['is_pay'=>1,'is_settled'=>1,'is_decoct'=>1])->andWhere([ + ['<>', 'refund_status', '3'], + ['<>', 'status', '4'], + ])->andWhere($data ?? '')->sum('treatement_price'); + $decoct_price_price = ProductOrder::find()->alias('p')->where(['is_pay' => 1, 'is_settled' => 1, 'is_decoct' => 1])->andWhere([ 'or', - ['<>','refund_status','3'], - ['<>','status','4'], - ])->andWhere($data??'')->sum('decoct_price'); + ['<>', 'refund_status', '3'], + ['<>', 'status', '4'], + ])->andWhere($data ?? '')->sum('decoct_price'); //挂号 // $register_price=Register::find()->alias('re')->where([ @@ -983,13 +1026,13 @@ class StatementAccountController extends BaseAdminController // ['<>','re.status',4], // ['<>','re.status',7], // ])->andWhere($register_data??'')->sum('price'); - $register_price=Ledger::find()->alias('re')->where(['status'=>1,'user_type'=>1,'order_type'=>2])->andWhere($register_data??'')->sum('money'); - $drug_price=bcadd($cao_sale_price,$cheng_sale_price,4); + $register_price = Ledger::find()->alias('re')->where(['status' => 1, 'user_type' => 1, 'order_type' => 2])->andWhere($register_data ?? '')->sum('money'); + $drug_price = bcadd($cao_sale_price, $cheng_sale_price, 4); //所有药的进货金额 - $drug_give_price=Reconciliation::find()->alias('r')->andWhere(['r.status' => 1])->andWhere($datas??'')->sum('total_buy_price'); + $drug_give_price = Reconciliation::find()->alias('r')->andWhere(['r.status' => 1])->andWhere($datas ?? '')->sum('total_buy_price'); } - $data= [ + $data = [ // '0'=>[ // 'type'=>'草药销售额', // 'value'=>$cao_sale_price ?? "0", @@ -998,33 +1041,33 @@ class StatementAccountController extends BaseAdminController // 'type'=>'成药销售额', // 'value'=>$cheng_sale_price ?? "0", // ], - '1'=>[ - 'type'=>'药品销售金额', - 'value'=>$drug_price ?? "0", - ], - '2' => [ - 'type'=>'供货金额', - 'value'=>$drug_give_price ?? "0", - ], - '3'=>[ - 'type'=>'运费', - 'value'=>$trans_expenses_price ?? "0", - ], - // '4'=>[ - // 'type'=>'代煎费', - // 'value'=>$decoct_price_price ?? "0", - // ], - '5'=>[ - 'type'=>'挂号费', - 'value'=>$register_price ?? "0", - ], - '6'=>[ - 'type'=>'加工费', - 'value'=>$process_price ?? "0", + '1' => [ + 'type' => '药品销售金额', + 'value' => $drug_price ?? "0", ], - '7'=>[ - 'type'=>'诊疗费', - 'value'=>$treatement_price ?? "0", + '2' => [ + 'type' => '供货金额', + 'value' => $drug_give_price ?? "0", + ], + '3' => [ + 'type' => '运费', + 'value' => $trans_expenses_price ?? "0", + ], + // '4'=>[ + // 'type'=>'代煎费', + // 'value'=>$decoct_price_price ?? "0", + // ], + '5' => [ + 'type' => '挂号费', + 'value' => $register_price ?? "0", + ], + '6' => [ + 'type' => '加工费', + 'value' => $process_price ?? "0", + ], + '7' => [ + 'type' => '诊疗费', + 'value' => $treatement_price ?? "0", ], ]; @@ -1032,13 +1075,14 @@ class StatementAccountController extends BaseAdminController } // 计算总和的方法 - protected function calculateSum($model, $field, $start_time, $end_time) { + protected function calculateSum($model, $field, $start_time, $end_time) + { $where = []; if ($start_time && $end_time) { $where = ['between', 'xd_time', $start_time, $end_time]; } $andWhere = ['drug_id' => $model->drug_id]; - if($model->store_id){ + if ($model->store_id) { $andWhere['store_id'] = $model->store_id; } return (new \yii\db\Query()) @@ -1048,7 +1092,7 @@ class StatementAccountController extends BaseAdminController ->sum($field); } - /** + /** * @doc-name 药品总利润 */ public function actionDrugProfit($su_id = null, $store_id = null, $start_time = '', $end_time = '')