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

1172 lines
52 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace admin\controllers\base;
use admin\components\BaseAdminController;
use common\enums\UserRoleEnum;
use common\helpers\ArrayHelper;
use common\helpers\FuncHelper;
use common\models\oldDb\DoctorInfo;
use common\models\oldDb\DrugStoreDrug;
use common\models\oldDb\DrugStoreRelations;
use common\models\oldDb\Ledger;
use common\models\oldDb\ProductOrder;
use common\models\oldDb\Reconciliation;
use common\models\oldDb\Register;
use common\models\oldDb\Store;
use common\models\oldDb\StoreDoctor;
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 {//药品
// 设置查询条件查找状态为1的对账记录按ID降序排序
$query = Reconciliation::find()->alias('r')->where(['r.status' => 1])->orderBy(['id' => SORT_DESC]);
// 获取搜索参数:门店名称和药品名称
$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],
]);
}
}]);
// 根据门店ID过滤对账记录
if (!empty($get['store_id'])) {
$query->andWhere(['r.store_id' => $get['store_id']]);
}
// 如果用户是门店管理员,只查询该门店的对账记录
if ($user->role == UserRoleEnum::STORE_ADMIN) {
$query->andWhere(['r.store_id' => $user->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]);
}
// 按药品ID分组
// $query->groupBy('drug_id');
// TODO 这里有问题
// 定义查询字段及格式化函数
$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',
'total_buy_price',
'total_price',
'sale_number' => 0,
'trans_fee' => 'productOrder.trans_expenses',
'work_fee' => 'productOrder.process_price',
'treatement_fee' => 'productOrder.treatement_price'
]
];
$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)
];
}
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 ?? '')->sum('number');
$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,
'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()
{
// TODO 这里有问题(统计金额-诊所)需要重构
// 获取请求参数和当前用户信息
$get = \Yii::$app->request->get();
$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];
}
// 构建产品创建时间的筛选条件
$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) {
// 如果是店铺管理员,调用获取店铺统计数据的方法
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,
'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
];
}
/**
* 获取平台统计数据
*
* @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
];
}
/**
* @doc-name 统计--所有药
* @doc-param string start_time 时间
* @doc-param string end_time 时间
* 平台 诊所 萧康
*/
public function actionDrugStatistic()
{
// TODO 这里有问题(统计金额-药品)
$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', '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 ?? '')
->all();
} else {
$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);
}
return [
'all_sale' => $allSale ?? 0,
'all_total_price' => $totalPrice ?? 0,
];
}
/**
* @doc-name 诊所统计
* @doc-param string start_time 时间
* @doc-param string end_time 时间
*/
public function actionNewStoreStatistic()
{
// TODO 为什么写两个统计
$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;
}
}