382 lines
15 KiB
PHP
382 lines
15 KiB
PHP
<?php
|
|
|
|
namespace admin\controllers\base;
|
|
|
|
use admin\components\BaseAdminController;
|
|
use admin\controllers\AuthController;
|
|
use admin\models\User;
|
|
use common\enums\UserRoleEnum;
|
|
use common\jobs\SyncHospitalJob;
|
|
use common\models\CashAccount;
|
|
use common\models\Drug;
|
|
use common\models\DrugStoreDrug;
|
|
use common\models\DrugStoreRelations;
|
|
use common\models\PrescripOrderRefund;
|
|
use common\models\ProductOrder;
|
|
use common\models\ProductOrderRefund;
|
|
use common\models\Reconciliation;
|
|
use common\models\Store;
|
|
use common\services\PlatformService;
|
|
use linslin\yii2\curl\Curl;
|
|
use yii\db\ActiveRecord;
|
|
use yii\db\Exception;
|
|
|
|
|
|
class HomeController extends BaseAdminController
|
|
{
|
|
|
|
/**
|
|
* 获取首页统计数据
|
|
* @return array
|
|
*/
|
|
public function actionHomeCount()
|
|
{
|
|
$storeCount = Store::find()->where(['is_delete' => 0]);
|
|
$userCount = User::find();
|
|
$orderCount = ProductOrder::find()->where(['is_pay' => 1]);
|
|
$totalPrice = Reconciliation::find()->where(['status' => 1]);
|
|
$drugCount = Drug::find();
|
|
$inventoryWarning = DrugStoreDrug::find()->where(['<', 'stock', 100]);
|
|
$inventoryWarningByStore = DrugStoreRelations::find()->where(['<', 'stock', 100]);
|
|
return [
|
|
'store' => intval($storeCount->count()),
|
|
'user' => intval($userCount->count()),
|
|
'order' => intval($orderCount->count()),
|
|
'drug' => intval($drugCount->count()),
|
|
'total_price' => intval($totalPrice->sum('total_price')),
|
|
'inventory_warning' => intval($inventoryWarning->count()),
|
|
'inventory_warning_store' => intval($inventoryWarningByStore->count())
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 获取首页统计数据 - 其他
|
|
* @return array
|
|
* @throws Exception
|
|
*/
|
|
public function actionHomeCountByStore()
|
|
{
|
|
$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();
|
|
|
|
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);
|
|
$totalPrice->andWhere($where);
|
|
$inventoryWarning->andWhere($where);
|
|
$drug->where($where);
|
|
}
|
|
return [
|
|
'order' => intval($orderCount->count()),
|
|
'total_price' => intval($totalPrice->sum('total_price')),
|
|
'inventory_warning' => intval($inventoryWarning->count()),
|
|
'drug' => intval($drug->count())
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 获取销售量Top10的药品
|
|
* @return array|ActiveRecord[]
|
|
* @throws Exception
|
|
*/
|
|
public function actionTopDrugs()
|
|
{
|
|
// 通过yii_reconciliation表统计出前10的药品
|
|
$query = Reconciliation::find()
|
|
->with(['drug' => function ($query) {
|
|
$query->select(['id', 'drug_name', 'drug_number', 'type', 'image']);
|
|
}])
|
|
->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()
|
|
{
|
|
$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()
|
|
{
|
|
return CashAccount::find()
|
|
->with(['store' => function ($query) {
|
|
$query->select(['id', 'name']);
|
|
}])
|
|
->where(['user_type' => 1])
|
|
->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(['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, []);
|
|
}
|
|
} |