430 lines
18 KiB
PHP
430 lines
18 KiB
PHP
<?php
|
|
|
|
namespace admin\controllers\base;
|
|
|
|
use admin\components\BaseAdminController;
|
|
use common\enums\UserRoleEnum;
|
|
use common\models\oldDb\CashAccount;
|
|
use common\models\oldDb\Drug;
|
|
use common\models\oldDb\DrugStoreDrug;
|
|
use common\models\oldDb\DrugStoreRelations;
|
|
use common\models\oldDb\Prescription;
|
|
use common\models\oldDb\ProductOrder;
|
|
use common\models\oldDb\ProductOrderRefund;
|
|
use common\models\oldDb\Reconciliation;
|
|
use common\models\oldDb\Register;
|
|
use common\models\oldDb\Store;
|
|
use yii\db\ActiveRecord;
|
|
use yii\db\Exception;
|
|
|
|
|
|
class HomeController extends BaseAdminController
|
|
{
|
|
|
|
/**
|
|
* 获取首页统计数据
|
|
* @return array
|
|
*/
|
|
public function actionHomeCount()
|
|
{
|
|
$get = \Yii::$app->request->get();
|
|
$whereTime = $this->getWhereTime($get['start_time'], $get['end_time']);
|
|
$storeCount = Store::find()->where(['is_delete' => 0])->andWhere($whereTime);
|
|
$userCount = \common\models\oldDb\User::find()->where($whereTime);
|
|
$orderCount = ProductOrder::find()->where(['is_pay' => 1])->andWhere($whereTime);
|
|
$totalPrice = Reconciliation::find()->where(['status' => 1])->andWhere($whereTime);
|
|
$drugCount = Drug::find()->where($whereTime);
|
|
$inventoryWarning = DrugStoreDrug::find()->where(['<', 'stock', 100])->andWhere($whereTime);
|
|
$inventoryWarningByStore = DrugStoreRelations::find()->where(['<', 'stock', 100])->andWhere($whereTime);
|
|
// 处方订单
|
|
$prescriptionCount = Prescription::find()->where(['status' => 1])->andWhere($whereTime);
|
|
// 挂号人数
|
|
$registerCount = Register::find()->where(['in', 'status', [2,3]])->andWhere($whereTime);
|
|
return [
|
|
'store' => intval($storeCount->count()),
|
|
'user' => intval($userCount->count()),
|
|
'order' => intval($orderCount->count()),
|
|
'drug' => intval($drugCount->count()),
|
|
'total_price' => round($totalPrice->sum('total_price'), 2),
|
|
'inventory_warning' => intval($inventoryWarning->count()),
|
|
'inventory_warning_store' => intval($inventoryWarningByStore->count()),
|
|
'register' => intval($registerCount->count()),
|
|
'prescription' => intval($prescriptionCount->count()),
|
|
'prescription_china' => intval($prescriptionCount->andWhere(['prescription_type' => 1])->count()),
|
|
'prescription_west' => intval($prescriptionCount->andWhere(['prescription_type' => 2])->count())
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 获取首页统计数据 - 其他
|
|
* @return array
|
|
* @throws Exception
|
|
*/
|
|
public function actionHomeCountByStore()
|
|
{
|
|
$get = \Yii::$app->request->get();
|
|
$whereTime = $this->getWhereTime($get['start_time'], $get['end_time']);
|
|
$admin = \Yii::$app->user->identity;
|
|
$orderCount = ProductOrder::find()->where(['is_pay' => 1]);
|
|
$totalPrice = Reconciliation::find()->where(['status' => 1]);
|
|
// $inventoryWarning = DrugStoreRelations::find()->where(['<', 'stock', 100]);
|
|
$drug = DrugStoreRelations::find();
|
|
// 处方订单
|
|
$prescriptionCount = Prescription::find()->where(['status' => 1]);
|
|
// 挂号人数
|
|
$registerCount = Register::find()->where(['in', 'status', [2,3]]);
|
|
switch ($admin->role) {
|
|
case UserRoleEnum::STORE_ADMIN:
|
|
// 门店
|
|
if (empty($admin->store_id)) throw new Exception('门店信息获取失败');
|
|
$where = ['store_id' => $admin->store_id];
|
|
break;
|
|
case UserRoleEnum::PROVINCE_DAI:
|
|
// 省代
|
|
if (empty($admin->province_id)) throw new Exception('省区经理信息获取失败' . $admin->province_id);
|
|
$store_id = Store::find()->select(['id'])->where(['province_id' => $admin->province_id])->column();
|
|
$where = ['in', 'store_id', $store_id];
|
|
break;
|
|
case UserRoleEnum::CITY_DAI:
|
|
// 市代
|
|
if (empty($admin->city_id)) throw new Exception('市区经理信息获取失败' . $admin->city_id);
|
|
$store_id = Store::find()->select(['id'])->where(['city_id' => $admin->city_id])->column();
|
|
$where = ['in', 'store_id', $store_id];
|
|
break;
|
|
case UserRoleEnum::SUPPLY:
|
|
// 业务员
|
|
if (empty($admin->code)) throw new Exception('市区经理信息获取失败' . $admin->code);
|
|
$store_id = Store::find()->select(['id'])->where(['code' => $admin->code])->column();
|
|
$where = ['in', 'store_id', $store_id];
|
|
break;
|
|
default:
|
|
// 不需要额外的条件,直接排序
|
|
throw new Exception('角色参数错误');
|
|
break;
|
|
}
|
|
if (!empty($where)) {
|
|
$orderCount->andWhere($where)->andWhere($whereTime);
|
|
$totalPrice->andWhere($where)->andWhere($whereTime);
|
|
// $inventoryWarning->andWhere($where)->andWhere($whereTime);
|
|
$drug->where($where)->andWhere($whereTime);
|
|
$registerCount->andWhere($where)->andWhere($whereTime);
|
|
$prescriptionCount->andWhere($where)->andWhere($whereTime);
|
|
}
|
|
$totalPriceArr = 0;
|
|
$totalPriceSum = $totalPrice->select(['total_price'])->asArray()->all();
|
|
foreach ($totalPriceSum as $item) {
|
|
$totalPriceArr = bcadd($totalPriceArr, $item['total_price'], 2);
|
|
}
|
|
return [
|
|
'order' => intval($orderCount->count()),
|
|
'total_price' => round($totalPriceArr, 2),
|
|
// 'inventory_warning' => intval($inventoryWarning->count()),
|
|
'drug' => intval($drug->count()),
|
|
'register' => intval($registerCount->count()),
|
|
'prescription' => intval($prescriptionCount->count()),
|
|
'prescription_china' => intval($prescriptionCount->andWhere(['prescription_type' => 1])->count()),
|
|
'prescription_west' => intval($prescriptionCount->andWhere(['prescription_type' => 2])->count())
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 获取时间筛选条件
|
|
* @param $startTime
|
|
* @param $endTime
|
|
* @return array
|
|
*/
|
|
public function getWhereTime($startTime, $endTime, $field = 'created_at')
|
|
{
|
|
if (!empty($startTime) && !empty($endTime)) {
|
|
$where = ['between', $field, strtotime(date('Y-m-d 00:00:00', strtotime($startTime))), strtotime(date('Y-m-d 23:59:59', strtotime($endTime)))];
|
|
} else {
|
|
$time = time();
|
|
$where = ['between', $field, $time - 86400 * 7, $time];
|
|
}
|
|
return $where;
|
|
|
|
}
|
|
|
|
/**
|
|
* 获取销售量Top10的药品
|
|
* @return array|ActiveRecord[]
|
|
* @throws Exception
|
|
*/
|
|
public function actionTopDrugs()
|
|
{
|
|
$get = \Yii::$app->request->get();
|
|
$whereTime = $this->getWhereTime($get['start_time'], $get['end_time'], 'rec.created_at');
|
|
// 通过yii_reconciliation表统计出前10的药品
|
|
$query = Reconciliation::find()
|
|
->alias('rec')
|
|
->with(['drug' => function ($query) {
|
|
$query->select(['id', 'drug_name', 'drug_number', 'type', 'image']);
|
|
}])
|
|
->where($whereTime)
|
|
->select(['drug_id', 'sum(number) as number', 'sum(total_price) as total_price'])
|
|
->groupBy('drug_id')
|
|
->orderBy(['total_price' => SORT_DESC])
|
|
->limit(10)
|
|
->asArray();
|
|
|
|
// 根据角色查询自己有权限的数据
|
|
$admin = \Yii::$app->user->identity;
|
|
|
|
switch ($admin->role) {
|
|
case UserRoleEnum::SUPER_ADMIN:
|
|
break;
|
|
case UserRoleEnum::STORE_ADMIN:
|
|
// 门店
|
|
if (empty($admin->store_id)) throw new Exception('门店信息获取失败');
|
|
$store = $admin->store_id;
|
|
$query->joinWith(['store' => function ($s) use ($store) {
|
|
$s->alias('s');
|
|
$s->andWhere(['s.id' => $store]);
|
|
}]);
|
|
break;
|
|
case UserRoleEnum::PROVINCE_DAI:
|
|
// 省代
|
|
if (empty($admin->province_id)) throw new Exception('省区经理信息获取失败' . $admin->province_id);
|
|
$province = $admin->province_id;
|
|
$query->joinWith(['store' => function ($s) use ($province) {
|
|
$s->alias('s');
|
|
$s->andWhere(['s.province_id' => $province]);
|
|
}]);
|
|
break;
|
|
case UserRoleEnum::CITY_DAI:
|
|
// 市代
|
|
if (empty($admin->city_id)) throw new Exception('市区经理信息获取失败' . $admin->city_id);
|
|
$city = $admin->city_id;
|
|
$query->joinWith(['store' => function ($s) use ($city) {
|
|
$s->alias('s');
|
|
$s->andWhere(['s.city_id' => $city]);
|
|
}]);
|
|
break;
|
|
case UserRoleEnum::SUPPLY:
|
|
// 业务员
|
|
if (empty($admin->code)) throw new Exception('市区经理信息获取失败' . $admin->code);
|
|
$code = $admin->code;
|
|
$query->joinWith(['store' => function ($s) use ($code) {
|
|
$s->alias('s');
|
|
$s->andWhere(['s.code' => $code]);
|
|
}]);
|
|
break;
|
|
default:
|
|
throw new Exception('角色参数错误');
|
|
}
|
|
return $query->all();
|
|
}
|
|
|
|
/**
|
|
* 获取库存不足的药品
|
|
* @return array|ActiveRecord[]
|
|
* @throws Exception
|
|
*/
|
|
public function actionLowStock()
|
|
{
|
|
$admin = \Yii::$app->user->identity;
|
|
if ($admin->role != UserRoleEnum::SUPER_ADMIN) {
|
|
return $this->storeLowStock();
|
|
}
|
|
$list = DrugStoreDrug::find()
|
|
->with(['drug' => function ($query) {
|
|
$query->select(['id', 'drug_name', 'drug_number', 'type', 'image']);
|
|
}])
|
|
->where(['<', 'stock', 100])
|
|
->orderBy(['stock' => SORT_ASC])
|
|
->asArray()
|
|
->all();
|
|
if (empty($list)) {
|
|
$list = DrugStoreDrug::find()
|
|
->with(['drug' => function ($query) {
|
|
$query->select(['id', 'drug_name', 'drug_number', 'type', 'image']);
|
|
}])
|
|
->orderBy(['stock' => SORT_ASC])
|
|
->limit(10)
|
|
->asArray()
|
|
->all();
|
|
}
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* 获取除总仓库外的库存不足的药品
|
|
* @return array|ActiveRecord[]
|
|
* @throws Exception
|
|
*/
|
|
public function storeLowStock()
|
|
{
|
|
return [];
|
|
$selectStore = $this->request->get('store_id');
|
|
$query = DrugStoreRelations::find()
|
|
->with(['drug' => function ($query) {
|
|
$query->select(['id', 'drug_name', 'drug_number', 'type', 'image']);
|
|
}])
|
|
->select(['drug_id', 'store_id', 'stock'])
|
|
->asArray();
|
|
|
|
|
|
// 根据角色查询自己有权限的数据
|
|
$admin = \Yii::$app->user->identity;
|
|
|
|
switch ($admin->role) {
|
|
case UserRoleEnum::STORE_ADMIN:
|
|
// 门店
|
|
if (empty($admin->store_id)) throw new Exception('门店信息获取失败');
|
|
$where = ['store_id' => $admin->store_id];
|
|
break;
|
|
case UserRoleEnum::PROVINCE_DAI:
|
|
// 省代
|
|
if (empty($admin->province_id)) throw new Exception('省区经理信息获取失败' . $admin->province_id);
|
|
if (empty($selectStore)) throw new Exception('需要选择诊所');
|
|
$store_id = Store::find()->select(['id'])->where(['province_id' => $admin->province_id])->column();
|
|
if (!in_array($selectStore, $store_id)) throw new Exception('没有权限');
|
|
$where = ['store_id' => $selectStore];
|
|
break;
|
|
case UserRoleEnum::CITY_DAI:
|
|
// 市代
|
|
if (empty($admin->city_id)) throw new Exception('市区经理信息获取失败' . $admin->city_id);
|
|
if (empty($selectStore)) throw new Exception('需要选择诊所');
|
|
$store_id = Store::find()->select(['id'])->where(['city_id' => $admin->city_id])->column();
|
|
if (!in_array($selectStore, $store_id)) throw new Exception('没有权限');
|
|
$where = ['store_id' => $selectStore];
|
|
break;
|
|
case UserRoleEnum::SUPPLY:
|
|
// 业务员
|
|
if (empty($admin->code)) throw new Exception('市区经理信息获取失败' . $admin->code);
|
|
$store_id = Store::find()->select(['id'])->where(['code' => $admin->code])->column();
|
|
$where = ['in', 'store_id', $store_id];
|
|
break;
|
|
default:
|
|
// 不需要额外的条件,直接排序
|
|
throw new Exception('角色参数错误');
|
|
break;
|
|
}
|
|
|
|
$list = $query->where($where)->andWhere(['<', 'stock', 100])->orderBy(['stock' => SORT_ASC])->all();
|
|
if (empty($list)) {
|
|
$list = $query->where($where)->orderBy(['stock' => SORT_ASC])->limit(10)->all();
|
|
}
|
|
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* 排行前5的诊所
|
|
* @return array|ActiveRecord[]
|
|
*/
|
|
public function actionTopStore()
|
|
{
|
|
$get = \Yii::$app->request->get();
|
|
$whereTime = $this->getWhereTime($get['start_time'], $get['end_time']);
|
|
return CashAccount::find()
|
|
->with(['store' => function ($query) {
|
|
$query->select(['id', 'name']);
|
|
}])
|
|
->where(['user_type' => 1])
|
|
->andWhere($whereTime)
|
|
->select(['user_id', 'total_cash'])
|
|
->orderBy(['total_cash' => SORT_DESC])
|
|
->limit(5)
|
|
->asArray()
|
|
->all();
|
|
}
|
|
|
|
/**
|
|
* 获取最近一周的退款列表
|
|
* @return array|ActiveRecord[]
|
|
* @throws Exception
|
|
*/
|
|
public function actionOrderRefund()
|
|
{
|
|
|
|
$query = ProductOrderRefund::find()->alias('por')->with([
|
|
'order' => function ($query) {
|
|
$query->select(['id', 'order_no', 'items_price', 'cancel_remark']);
|
|
},
|
|
'order.orderItems' => function ($query) {
|
|
$query->select(['id', 'product_order_id', 'drug_id', 'drug_name']);
|
|
},
|
|
])
|
|
->where(['between', 'por.created_at', strtotime('-7 day'), time()])
|
|
->andWhere(['por.status' => 0])
|
|
// ->where(['por.status' => 0])
|
|
->orderBy(['id' => SORT_DESC])
|
|
->asArray();
|
|
|
|
// 根据角色查询自己有权限的数据
|
|
$admin = \Yii::$app->user->identity;
|
|
|
|
switch ($admin->role) {
|
|
case UserRoleEnum::SUPER_ADMIN:
|
|
// 管理员
|
|
break;
|
|
case UserRoleEnum::STORE_ADMIN:
|
|
// 门店
|
|
if (empty($admin->store_id)) throw new Exception('门店信息获取失败');
|
|
$where = ['store_id' => $admin->store_id];
|
|
break;
|
|
case UserRoleEnum::PROVINCE_DAI:
|
|
// 省代
|
|
if (empty($admin->province_id)) throw new Exception('省区经理信息获取失败' . $admin->province_id);
|
|
$store_id = Store::find()->select(['id'])->where(['province_id' => $admin->province_id])->column();
|
|
$where = ['in', 'store_id', $store_id];
|
|
break;
|
|
case UserRoleEnum::CITY_DAI:
|
|
// 市代
|
|
if (empty($admin->city_id)) throw new Exception('市区经理信息获取失败' . $admin->city_id);
|
|
$store_id = Store::find()->select(['id'])->where(['city_id' => $admin->city_id])->column();
|
|
$where = ['in', 'store_id', $store_id];
|
|
break;
|
|
case UserRoleEnum::SUPPLY:
|
|
// 业务员
|
|
if (empty($admin->code)) throw new Exception('市区经理信息获取失败' . $admin->code);
|
|
$store_id = Store::find()->select(['id'])->where(['code' => $admin->code])->column();
|
|
$where = ['in', 'store_id', $store_id];
|
|
break;
|
|
default:
|
|
// 不需要额外的条件,直接排序
|
|
throw new Exception('角色参数错误');
|
|
break;
|
|
}
|
|
if (!empty($where)) {
|
|
$query->joinWith('order', function ($query) use ($where) {
|
|
$query->where($where);
|
|
});
|
|
}
|
|
$list = $query->all();
|
|
|
|
foreach ($list as &$v) {
|
|
$v['order_names'] = implode(',', array_column($v['order']['orderItems'], 'drug_name'));
|
|
$v['created_at'] = date('Y-m-d H:i:s', $v['created_at']);
|
|
}
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* 近30天加入的诊所
|
|
* @return \yii\data\ActiveDataProvider
|
|
*/
|
|
public function actionNewStore()
|
|
{
|
|
$query = Store::find()->with(['drugStore', 'province', 'city', 'admin'])->where(['between', 'created_at', strtotime('-7 day'), time()]);
|
|
|
|
$this->field = [
|
|
Store::class => [
|
|
'id', 'plate_id', 'name', 'position', 'offical_seal', 'pic', 'contact', 'mobile', /*'qr_code',*/
|
|
'start_time', 'end_time',
|
|
'drugstore_id', 'see_rate',
|
|
'drugstore' => 'drugStore.name',
|
|
'code', 'uid', 'supply' => 'admin.username',
|
|
'province' => 'province.name',
|
|
'city' => 'city.name',
|
|
'province_id',
|
|
'city_id', 'erp_id',
|
|
'created_at' => function ($d) {
|
|
return date('Y-m-d H:i:s', $d['created_at']);
|
|
}
|
|
]
|
|
];
|
|
return $this->create($query, []);
|
|
}
|
|
} |