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

1130 lines
50 KiB
PHP
Raw Normal View History

2024-09-19 11:32:39 +08:00
<?php
namespace admin\controllers\base;
use admin\components\BaseAdminController;
use admin\controllers\AuthController;
use common\enums\UserRoleEnum;
use common\helpers\FuncHelper;
use common\helpers\ArrayHelper;
use common\models\DrugStoreDrug;
use common\models\DrugStoreRelations;
use common\models\Ledger;
use common\models\Log;
use common\models\Prescription;
use common\models\ProductOrder;
use common\models\Reconciliation;
use common\models\Register;
use common\models\Store;
use common\models\StoreDoctor;
use common\models\DoctorInfo;
use common\services\ExportService;
use yii\db\Exception;
use yii\web\Response;
/**
* @doc-module 对账单
*/
class StatementAccountController extends BaseAdminController
{
/**
* @doc-name 对账单列表
* @doc-param int type 1诊所2药品
* @doc-param int store_id 诊所ID
*/
public function actionList()
{
$get = \Yii::$app->request->get();
$this->requestValidate($get, [
['type', 'required']
]);
//诊所
if ($get['type'] == 1) {
if (empty($get['store_id'])) {
throw new Exception('诊所ID不能为空');
}
$query = Store::find()->alias('s')->where(['s.id' => $get['store_id']]);
$this->field = [
Store::class => [
'id', 'name', 'shouzimu', 'position', 'pic', 'contact', 'mobile', 'offical_seal', 'see_rate',
'sale_number' => function ($s) {
return $this->actionStoreAllNumber($s->id);
},
'sale_price' => function ($s) {
return $this->actionStoreSalePrice($s->id);
},
'give_price' => function ($s) {
return $this->actionStoreGivePrice($s->id);
},
]
];
} else {//药品
$query = Ledger::find()->alias('l')->where(['l.status' => 1]);
$this->field = [
Ledger::class => [
'id', 'order_id', 'user_id', 'user_type', 'su_id', 'drugstore_id', 'drug_id', 'money', 'status',
'drug_number' => 'drug.drug_number',
'drug_name' => 'drug.drug_name',
'store_number' => 'productOrder.store.id',
'store' => 'productOrder.store.name',
'sale_number' => function ($s) {
return $this->actionEverySaleNumber($s->order_id, $s->drug_id);
},
'sale_price',
'give_price'
]
];
}
return $this->create($query, $get);
}
/**
* @doc-name 统计门店所有的药的销量
*/
public function actionStoreAllNumber($store_id = null)
{
$C_Drug = DrugStoreDrug::find()->where(['type' => [1, 3]])->column();//草药
$C_sale = DrugStoreRelations::find()->where(['in', 'drug_id', $C_Drug])->andWhere(['store_id' => $store_id])->sum('sale_number');
$CH_Drug = DrugStoreDrug::find()->where(['type' => [2, 4]])->column();//成药
$CH_sale = DrugStoreRelations::find()->where(['in', 'drug_id', $CH_Drug])->andWhere(['store_id' => $store_id])->sum('sale_number');
return [
'c_sale' => $C_sale,
'CH_sale' => $CH_sale,
];
}
/**
* @doc-name 统计每个药的销量
*/
public function actionCategorySaleNumber($order_id = null, $drug_id = null)
{
$order_id = 2;
$drug_id = 1;
$ProductOrder = ProductOrder::find()->where(['id' => $order_id])->one();
$DrugStoreRelations = DrugStoreRelations::find()->where(['store_id' => $ProductOrder->store_id, 'drug_id' => $drug_id])->one();
return $DrugStoreRelations->sale_number;
}
/**
* @doc-name 统计每个药的销量
*/
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();
return $DrugStoreRelations->sale_number;
}
/**
* @doc-name 对账单
* @doc-param int type 1诊所2药品
* @doc-param int drug_type 1草药2成药 / optional
* @doc-param int store_id 诊所ID / optional
* @doc-param int store_name 诊所name / optional
* @doc-param int drug_name 药品 / optional
* @doc-param string start_time 时间 / optional
* @doc-param string end_time 时间 / optional
*/
public function actionDuiZhangDan()
{
$get = \Yii::$app->request->get();
$this->requestValidate($get, [
['type', 'required']
]);
$user = \Yii::$app->user->identity;
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 ($get['type'] == 1) {
$query = Store::find()->alias('s');
if ($user->role == UserRoleEnum::STORE_ADMIN) {
$query->andWhere(['s.id' => $user->store_id]);
}
$store_name = $get['store_name'];
if (!empty($store_name)) {
$query->where(['like', 's.name', $store_name]);
}
if (!empty($start_time) && !empty($end_time)) {
if ($start_time==$end_time){//筛选某一天
$time=FuncHelper::getDayBE($get['start_time']);
$end_time=$time[1];
}
//按照下单时按筛选
$query->with(['reconciliation'=>function($r)use($start_time,$end_time){
$r->alias('re');
$r->andWhere(['between', 're.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);
},
'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);
},
'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 $this->actionRegister($m->id,$register_data);
return $price??0;
}
]
];
} else {//药品
2024-09-30 17:04:41 +08:00
// TODO 对账问题
// 设置查询条件查找状态为1的对账记录按ID降序排序
2024-09-19 11:32:39 +08:00
$query = Reconciliation::find()->alias('r')->where(['r.status' => 1])->orderBy(['id' => SORT_DESC]);
2024-09-30 17:04:41 +08:00
// 获取搜索参数:门店名称和药品名称
2024-09-19 11:32:39 +08:00
$store_name = $get['store_name'];
$drug_name = $get['drug_name'];
2024-09-30 17:04:41 +08:00
// 根据门店名称过滤门店
2024-09-19 11:32:39 +08:00
$query->joinWith(['store' => function ($s) use ($store_name) {
$s->alias('s');
if (!empty($store_name)) {
$s->andWhere([
'or',
['like', 's.name', $store_name],
['like', 's.shouzimu', $store_name],
]);
}
}]);
2024-09-30 17:04:41 +08:00
// 根据药品名称过滤药品
2024-09-19 11:32:39 +08:00
$query->joinWith(['drug' => function ($d) use ($drug_name) {
$d->alias('d');
if (!empty($drug_name)) {
$d->andWhere([
'or',
['like', 'd.drug_name', $drug_name],
['like', 'd.pinyin_simple', $drug_name],
]);
}
}]);
2024-09-30 17:04:41 +08:00
// 根据门店ID过滤对账记录
2024-09-19 11:32:39 +08:00
if (!empty($get['store_id'])) {
$query->andWhere(['r.store_id' => $get['store_id']]);
}
2024-09-30 17:04:41 +08:00
// 如果用户是门店管理员,只查询该门店的对账记录
2024-09-19 11:32:39 +08:00
if ($user->role == UserRoleEnum::STORE_ADMIN) {
$query->andWhere(['r.store_id' => $user->store_id]);
}
2024-09-30 17:04:41 +08:00
// 根据药品类型过滤对账记录
2024-09-19 11:32:39 +08:00
if (!empty($get['drug_type'])) {
if ($get['drug_type'] == 1) {
$query->andWhere(['r.drug_type' => [1, 3]]);
} else {
$query->andWhere(['r.drug_type' => [2, 4]]);
}
}
2024-09-30 17:04:41 +08:00
// 根据开始时间和结束时间过滤对账记录
2024-09-19 11:32:39 +08:00
if (!empty($start_time) && !empty($end_time)) {
if ($start_time==$end_time){//筛选当天
$time=FuncHelper::getDayBE($get['start_time']);
$end_time=$time[1];
}
$query->andWhere(['between', 'r.xd_time',$start_time, $end_time]);
}
2024-09-30 17:04:41 +08:00
// 按药品ID分组
// $query->groupBy('drug_id');
// TODO 这里有问题
// 定义查询字段及格式化函数
2024-09-19 11:32:39 +08:00
$this->field = [
Reconciliation::class => [
2024-09-30 17:04:41 +08:00
'id', 'order_id', 'store_id',
'pay_time' => function ($m) {
2024-09-19 11:32:39 +08:00
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',
2024-09-30 17:04:41 +08:00
'number',
'total_buy_price',
'total_price',
'sale_number' => 0,
2024-09-19 11:32:39 +08:00
'trans_fee'=>'productOrder.trans_expenses',
'work_fee' => 'productOrder.process_price',
'treatement_fee' => 'productOrder.treatement_price'
]
];
2024-09-30 17:04:41 +08:00
$list = $this->create($query, $get)->getModels();
// 总销量 sale_number
// 单个销量 number 不知道要这个干嘛
// 总价 total_price
// 总供货价 total_buy_price
$result = [];
foreach ($list as $value) {
if (!empty($result[$value['drug_id']])) {
$result[$value['drug_id']]['number'] = bcadd($result[$value['drug_id']]['sale_number'], $value['number'], 0);
$result[$value['drug_id']]['sale_number'] = bcadd($result[$value['drug_id']]['sale_number'], $value['number'], 2);
$result[$value['drug_id']]['total_buy_price'] = bcadd($result[$value['drug_id']]['total_buy_price'], $value['total_buy_price'], 2);
$result[$value['drug_id']]['total_price'] = bcadd($result[$value['drug_id']]['total_price'], $value['total_price'], 2);
} else {
$result[$value['drug_id']] = [
'id' => $value['drug_id'],
'drug_number' => $value['drug']['drug_number'],
'drug_name' => $value['drug']['drug_name'],
'number' => $value['number'],
'sale_number' => $value['number'],
'total_price' => $value['total_price'],
'total_buy_price' => $value['total_buy_price'],
'drug_type' => $value['drug_type'],
'drug_id' => $value['drug_id'],
'status' => $value['status'],
];
}
}
return [
'list' => array_values($result),
'pageSize' => 10,
'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'
// ]
// ];
2024-09-19 11:32:39 +08:00
}
return $this->create($query, $get);
}
/**
* @doc-name 销量
*/
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];
}
$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');
$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);
return [
'cao_drug_num' => $cao_drug_num ?? 0,
'cheng_drug_num' => $cheng_drug_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,
'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)
];
}
public function actionRegister($store_id,$register_data){
$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;
}
/**
* @doc-name 导出对账单
*/
public function actionZhangDanExport()
{
\Yii::$app->response->format = Response::FORMAT_RAW;
$get = \Yii::$app->request->get();
$this->requestValidate($get, [
['type', 'required']
]);
$user = \Yii::$app->user->identity;
$get['limit'] = 10000;
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 ($get['type'] == 1) {
$query = Store::find()->alias('s');
if ($user->role == UserRoleEnum::STORE_ADMIN) {
$query->andWhere(['s.id' => $user->store_id]);
}
$store_name = $get['store_name'];
if (!empty($store_name)) {
$query->where(['like', 's.name', $store_name]);
}
if (!empty($start_time) && !empty($end_time)) {
if ($start_time==$end_time){//筛选某一天
$time=FuncHelper::getDayBE($get['start_time']);
$end_time=$time[1];
}
//按照下单时按筛选
$query->with(['reconciliation'=>function($r)use($start_time,$end_time){
$r->alias('re');
$r->andWhere(['between', 're.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(),[
Store::class => [
'id', 'name',
'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);
},
'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);
},
'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){
$data[$key]['id'] = $v['id'];
$data[$key]['name'] = $v['name'];
$data[$key]['cao_drug_num'] = $v['sale_number']['cao_drug_num'];
$data[$key]['cao_sale_price'] = $v['sale_number']['cao_sale_price'];
$data[$key]['cao_give_price'] = $v['sale_number']['cao_give_price'];
$data[$key]['cheng_drug_num'] = $v['sale_number']['cheng_drug_num'];
$data[$key]['cheng_sale_price'] = $v['sale_number']['cheng_sale_price'];
$data[$key]['cheng_give_price'] = $v['sale_number']['cheng_give_price'];
$data[$key]['total_sale_price'] = $v['sale_number']['total_sale_price'];
$data[$key]['total_give_price'] = $v['sale_number']['total_give_price'];
$data[$key]['trans_fee'] = $v['trans_fee'];
$data[$key]['work_fee'] = $v['work_fee'];
$data[$key]['treatement_fee'] = $v['treatement_fee'];
$data[$key]['store_income'] = $v['sale_number']['store_income'];
$data[$key]['register_price'] = $v['register_price'];
}
} 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]);
}
$store_name = $get['store_name'];
$drug_name = $get['drug_name'];
$query->joinWith(['store' => function ($s) use ($store_name) {
$s->alias('s');
if (!empty($store_name)) {
$s->andWhere([
'or',
['like', 's.name', $store_name],
['like', 's.shouzimu', $store_name],
]);
}
}]);
$query->joinWith(['drug' => function ($d) use ($drug_name) {
$d->alias('d');
if (!empty($drug_name)) {
$d->andWhere([
'or',
['like', 'd.drug_name', $drug_name],
['like', 'd.pinyin_simple', $drug_name],
]);
}
}]);
//门店搜索
if (!empty($get['store_id'])) {
$query->andWhere(['r.store_id' => $get['store_id']]);
}
if (!empty($get['drug_type'])) {
if ($get['drug_type'] == 1) {
$query->andWhere(['r.drug_type' => [1, 3]]);
} else {
$query->andWhere(['r.drug_type' => [2, 4]]);
}
}
if (!empty($start_time) && !empty($end_time)) {
if ($start_time==$end_time){//筛选当天
$time=FuncHelper::getDayBE($get['start_time']);
$end_time=$time[1];
}
$query->andWhere(['between', 'r.xd_time',$start_time, $end_time]);
}
$query->groupBy('drug_id');
$parameter['header'] = ['药品名称', '药品编号', '销量', '供货金额', '销售金额'];
$exportData = ArrayHelper::toArray($this->create($query, $get)->getModels(),[
Reconciliation::class => [
'drug_name' => 'drug.drug_name',
'drug_number' => 'drug.drug_number',
'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);
},
]
]);
$data = [];
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{//医生
$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]);
$parameter['header'] = ['医生ID', '医生姓名', '总挂号费', '药品销售金额', '总利润'];
$exportData = ArrayHelper::toArray($this->create($query, $get)->getModels(),[
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_profit_price' => function ($r) use ($start_time, $end_time, $store_id) {
return $this->actionDrugProfit($r->su_id, $store_id, $start_time, $end_time);
},
]
]);
$data = [];
foreach($exportData as $k=>$v){
$data[$k]['su_id'] = $v['su_id'];
$data[$k]['name'] = $v['name'];
$data[$k]['register_price'] = $v['register_price'];
$data[$k]['drug_sale_price'] = $v['drug_sale_price'];
$data[$k]['drug_profit_price'] = $v['drug_profit_price'];
}
}
$parameter['data'] = $data;
ExportService::ExportByCors($parameter);
}
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];
}
$data=['between', 'pr.created_at', $start_time,$end_time];
}
if ($type==1){
$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');
}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');
}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');
}
return $price??0;
}
/**
* @doc-name 统计--全部诊所
* @doc-param string start_time 时间
* @doc-param string end_time 时间
*/
public function actionStoreStatistic()
{
2024-09-30 17:04:41 +08:00
// TODO 这里有问题(统计金额-诊所)需要重构
2024-09-19 11:32:39 +08:00
$get = \Yii::$app->request->get();
$admin=\Yii::$app->user->identity;
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']);
$start_time=$get['start_time'];
$end_time=$time[1];
}
$data=['between', 'p.created_at', $start_time, $end_time];
$datas=['between', 'r.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);
}
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
];
}
/**
* @doc-name 统计--所有药
* @doc-param string start_time 时间
* @doc-param string end_time 时间
* 平台 诊所 萧康
*/
public function actionDrugStatistic()
{
2024-09-30 17:04:41 +08:00
// TODO 这里有问题(统计金额-药品)
2024-09-19 11:32:39 +08:00
$get = \Yii::$app->request->get();
$admin=\Yii::$app->user->identity;
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']);
$start_time=$get['start_time'];
$end_time=$time[1];
}
2024-09-30 17:04:41 +08:00
$data=['between', 'xd_time', $start_time, $end_time];
2024-09-19 11:32:39 +08:00
}
if ($admin->role==UserRoleEnum::STORE_ADMIN){
2024-09-30 17:04:41 +08:00
$resData= Reconciliation::find()
->andWhere(['status' => 1,'store_id'=>$admin->store_id])
->andWhere($data??'')
->all();
2024-09-19 11:32:39 +08:00
}else{
2024-09-30 17:04:41 +08:00
$resData= Reconciliation::find()
->andWhere(['status' => 1])
->andWhere($data??'')
->all();
}
$allSale = 0;
$totalPrice = 0;
foreach ($resData as $key=>$value) {
$allSale = bcadd($allSale, $value->number, 0);
$totalPrice = bcadd($totalPrice, $value->total_price, 0);
2024-09-19 11:32:39 +08:00
}
return [
2024-09-30 17:04:41 +08:00
'all_sale'=>$allSale??0,
'all_total_price'=>$totalPrice??0,
2024-09-19 11:32:39 +08:00
];
}
/**
* @doc-name 诊所统计
* @doc-param string start_time 时间
* @doc-param string end_time 时间
*/
public function actionNewStoreStatistic()
{
$get = \Yii::$app->request->get();
$admin=\Yii::$app->user->identity;
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[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];
}
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');
$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');
// //挂号
// $register_price=Register::find()->alias('re')->where([
// 'and',
// ['<>','re.status',0],
// ['<>','re.status',4],
// ['<>','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_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');
$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([
'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([
'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([
'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],
// ])->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);
//所有药的进货金额
$drug_give_price=Reconciliation::find()->alias('r')->andWhere(['r.status' => 1])->andWhere($datas??'')->sum('total_buy_price');
}
$data= [
// '0'=>[
// 'type'=>'草药销售额',
// 'value'=>$cao_sale_price ?? "0",
// ],
// '1'=>[
// '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",
],
'7'=>[
'type'=>'诊疗费',
'value'=>$treatement_price ?? "0",
],
];
return $data;
}
// 计算总和的方法
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){
$andWhere['store_id'] = $model->store_id;
}
return (new \yii\db\Query())
->from($model::tableName())
->where($andWhere)
->andWhere($where)->groupBy('drug_id')
->sum($field);
}
/**
* @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 挂号费
* @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;
}
}