Files
xk-api-yii/admin/controllers/base/SupplyController.php
2024-09-19 11:32:39 +08:00

405 lines
18 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 admin\controllers\AuthController;
use common\enums\UserRoleEnum;
use common\models\Admin;
use common\models\ProductOrder;
use common\models\Reconciliation;
use common\models\Store;
use yii\db\Exception;
/**
* @doc-module 业务员
*/
class SupplyController extends BaseAdminController
{
/**
* @doc-name 列表
* @doc-param int type 1销售数据2产品数据
* @doc-param int sort 1诊所2业务员3省4市
* @doc-param string start_time / optional
* @doc-param string end_time / optional
* @doc-param string limit_time 1当月2上月3累计 / optional
* @doc-return mixed @List{supply-string-业务员position-string-诊所位置mobile-string-诊所电话,offical_seal-string-公章code-string-推广码sale_price-float-销售金额drug_number-string-药品编码,drug_name-string-药名}
*/
public function actionList()
{
$get = \Yii::$app->request->get();
$admin = \Yii::$app->user->identity;
$this->requestValidate($get, [
['type', 'required'],
]);
$search = $get['search'];
$sort = $get['sort'];
$limit_time = $get['limit_time'];
if ($admin->role == UserRoleEnum::PROVINCE_DAI) {//省代
switch ($get['type']) {
case 1:
$query = Store::find()->alias('s')->where(['s.is_delete' => 0, 's.province_id' => $admin->province_id]);
break;
case 2:
$query = Reconciliation::find()->orderBy(['id' => SORT_DESC])->alias('r')->where(['r.status' => 1])->joinWith(['store' => function ($store) use ($admin) {
$store->alias('st');
$store->where(['st.province_id' => $admin->province_id]);
}]);
break;
default:
throw new Exception('参数错误');
}
} elseif ($admin->role == UserRoleEnum::CITY_DAI) {//市代
switch ($get['type']) {
case 1:
$query = Store::find()->alias('s')->where(['s.is_delete' => 0, 's.city_id' => $admin->city_id]);
break;
case 2:
$query = Reconciliation::find()->orderBy(['id' => SORT_DESC])->alias('r')->where(['r.status' => 1])->joinWith(['store' => function ($store) use ($admin) {
$store->alias('st');
$store->where(['st.city_id' => $admin->city_id]);
}]);
break;
default:
throw new Exception('参数错误');
}
} elseif ($admin->role == UserRoleEnum::SUPPLY) {//业务员
switch ($get['type']) {
case 1:
$query = Store::find()->alias('s')->where(['s.code' => $admin->code, 's.is_delete' => 0]);
break;
case 2:
$query = Reconciliation::find()->orderBy(['id' => SORT_DESC])->alias('r')->where(['r.status' => 1])->joinWith(['store' => function ($store) use ($admin) {
$store->where(['uid' => $admin->uid]);
}]);
break;
default:
throw new Exception('参数错误');
}
} else {
throw new Exception('角色错误');
}
switch ($get['type']) {
case 1:
if ($limit_time == 1) {
$start_time = strtotime(date('Y-m-01 00:00:00'));
$end_time = strtotime(date('Y-m-t 23:59:59'));
$query->joinWith(['reconciliation' => function ($r) use ($start_time, $end_time) {
$r->alias('re');
$r->where(['re.status' => 1])->andWhere([
'between', 're.pay_time', $start_time, $end_time]);
}]);
}
if ($limit_time == 2) {
$now = new \DateTime();
$now->modify('first day of last month');
$start_time = $now->format('Y-m-01 00:00:00');
$end_time = date("Y-m-d 23:59:59", strtotime(-date('d') . 'day'));
$query->joinWith(['reconciliation' => function ($r) use ($start_time, $end_time) {
$r->alias('re');
$r->where(['re.status' => 1])->andWhere([
'between', 're.pay_time', strtotime($start_time), strtotime($end_time)]);
}]);
}
if (!empty($sort)) {
switch ($sort) {
case 1:
if (!empty($search)) $query->andWhere(['like', 's.name', $search]);
break;
case 2:
if (!empty($search)) {
$query->joinWith(['admin' => function ($a) use ($search) {
$a->andWhere(['like', 'username', $search]);
}]);
}
break;
case 3:
if (!empty($search)) {
$query->joinWith(['province' => function ($p) use ($search) {
$p->alias('p')->where(['like', 'p.name', $search]);
}]);
}
break;
case 4:
if (!empty($search)) {
$query->joinWith(['city' => function ($c) use ($search) {
$c->alias('c')->where(['like', 'c.name', $search]);
}]);
}
break;
default:
throw new Exception('参数错误');
}
}
$this->field = [
Store::class => [
'id', 'name', 'position', 'mobile', 'offical_seal', 'code', 'uid',
'supply' => 'admin.username',
'province' => 'province.name',
'city' => 'city.name',
'city_id',
'province_id',
'sale_price' => function ($s) {
return $this->actionSalePrice($s->id);
}
]
];
return $this->create($query, $get);
case 2:
if ($limit_time == 1) {
$start_time = strtotime(date('Y-m-01 00:00:00'));
$end_time = strtotime(date('Y-m-t 23:59:59'));
$query->andWhere(['between', 'r.pay_time', $start_time, $end_time]);
}
if ($limit_time == 2) {
$now = new \DateTime();
$now->modify('first day of last month');
$start_time = $now->format('Y-m-01 00:00:00');
$end_time = date("Y-m-d 23:59:59", strtotime(-date('d') . 'day'));
$query->andWhere(['between', 'r.pay_time', strtotime($start_time), strtotime($end_time)]);
}
if (!empty($get['start_time']) && !empty($get['end_time'])) {
$query->andWhere(['between', 'r.pay_time', strtotime($get['start_time']), strtotime($get['end_time'])]);
}
if (!empty($sort)) {
switch ($sort) {
case 1:
$query->joinWith(['store' => function ($s) use ($search) {
$s->alias('s');
$s->andWhere([
'or',
['like', 's.name', $search],
['like', 's.shouzimu', $search],
]);
}]);
break;
case 2:
if (!empty($search)) {
$query->joinWith(['store' => function ($s) use ($search) {
$s->joinWith(['admin' => function ($a) use ($search) {
$a->andWhere(['like', 'username', $search]);
}]);
}]);
}
break;
case 3:
if (!empty($search)) {
$query->joinWith(['store' => function ($s) use ($search) {
$s->joinWith(['province' => function ($ss) use ($search) {
$ss->alias('pro');
$ss->andWhere(['like', 'pro.name', $search]);
}]);
}]);
}
break;
case 4:
if (!empty($search)) {
$query->joinWith(['store' => function ($s) use ($search) {
$s->joinWith(['city' => function ($ct) use ($search) {
$ct->alias('city');
$ct->andWhere(['like', 'city.name', $search]);
}]);
}]);
}
break;
default:
throw new Exception('参数错误');
}
}
//
// $query->joinWith(['drug' => function ($d) use ($search) {
// $d->alias('d');
// $d->andWhere([
// 'or',
// ['like', 'd.drug_name', $search],
// ['like', 'd.pinyin_simple', $search],
// ]);
// }]);
if (!empty($search)) {
if ($search == 1) {
$query->andWhere(['r.drug_type' => [1, 3]]);
} else {
$query->andWhere(['r.drug_type' => [2, 4]]);
}
}
$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', 'number', 'total_buy_price', 'total_price', 'status',
'drug_number' => 'drug.drug_number',
'drug_name' => 'drug.drug_name',
'store_number' => 'productOrder.store.id',
'store' => 'store.name',
'supply' => 'store.admin.username',
'province' => 'store.province.name',
'city' => 'store.city.name',
'province_id' => 'store.province_id',
'city_id' => 'store.city_id',
]
];
return $this->create($query, $get);
default:
throw new Exception('参数错误');
}
}
public function actionSalePrice($store)
{
$Reconciliation = Reconciliation::find()->where(['store_id' => $store, 'status' => 1])->sum('total_price');
return $Reconciliation ?? 0;
}
/**
* @doc-name 按照诊所统计
* @doc-param string start_time 起始时间
* @doc-param string end_time 终止时间
* @doc-param string limit_time 1当月2上月3累计 / optional
*/
public function actionStoreStatics()
{
$admin = \Yii::$app->user->identity;
$get = \Yii::$app->request->get();
$limit_time = $get['limit_time'];
if (!empty($get['start_time']) && !empty($get['end_time'])) {
$data = ['between', 'pay_time', strtotime($get['start_time']), strtotime($get['end_time'])];
}
if ($limit_time == 1) {
$start_time = strtotime(date('Y-m-01 00:00:00'));
$end_time = strtotime(date('Y-m-t 23:59:59'));
$data = ['between', 'pay_time', $start_time, $end_time];
}
if ($limit_time == 2) {
$now = new \DateTime();
$now->modify('first day of last month');
$start_time = $now->format('Y-m-01 00:00:00');
$end_time = date("Y-m-d 23:59:59", strtotime(-date('d') . 'day'));
$data = ['between', 'pay_time', strtotime($start_time), strtotime($end_time)];
}
if (!empty($get['start_time']) && !empty($get['end_time'])) {
$data = ['between', 'pay_time', strtotime($get['start_time']), strtotime($get['end_time'])];
}
if ($admin->role == UserRoleEnum::PROVINCE_DAI) {//省代
$store_id = Store::find()->select(['id'])->where(['province_id' => $admin->province_id])->column();
$total_price = Reconciliation::find()->where(['in', 'store_id', $store_id])->andWhere(['status' => 1])->andWhere($data ?? '')->sum('total_price');
$total_buy_price= Reconciliation::find()->where(['in', 'store_id', $store_id])->andWhere(['status' => 1])->andWhere($data ?? '')->sum('total_buy_price');
$store = Store::find()->where(['province_id' => $admin->province_id])->count();
} elseif ($admin->role == UserRoleEnum::CITY_DAI) {//市代
$store_id = Store::find()->select(['id'])->where(['city_id' => $admin->city_id])->column();
$total_price = Reconciliation::find()->where(['in', 'store_id', $store_id])->andWhere(['status' => 1])->andWhere($data ?? '')->sum('total_price');
$total_buy_price= Reconciliation::find()->where(['in', 'store_id', $store_id])->andWhere(['status' => 1])->andWhere($data ?? '')->sum('total_buy_price');
$store = Store::find()->where(['city_id' => $admin->city_id])->count();
} elseif ($admin->role == UserRoleEnum::SUPPLY) {//业务员
$store_id = Store::find()->select(['id'])->where(['code' => $admin->code])->column();
$total_price = Reconciliation::find()->where(['in', 'store_id', $store_id])->andWhere(['status' => 1])->andWhere($data ?? '')->sum('total_price');
$total_buy_price= Reconciliation::find()->where(['in', 'store_id', $store_id])->andWhere(['status' => 1])->andWhere($data ?? '')->sum('total_buy_price');
$store = Store::find()->where(['code' => $admin->code])->count();
} else {
throw new Exception('角色错误');
}
return [
'store_num' => $store ?? 0,
'total_price' => $total_price ?? 0,
'total_buy_price' => $total_buy_price ?? 0,
];
}
/**
* @doc-name 按照产品统计
* @doc-param string start_time 起始时间
* @doc-param string end_time 终止时间
* @doc-param string limit_time 1当月2上月3累计 / optional
*/
public function actionProductStatics()
{
$get = \Yii::$app->request->get();
$admin = \Yii::$app->user->identity;
$limit_time = $get['limit_time'];
if ($limit_time == 1) {
$start_time = strtotime(date('Y-m-01 00:00:00'));
$end_time = strtotime(date('Y-m-t 23:59:59'));
$data = ['between', 'pay_time', $start_time, $end_time];
}
if ($limit_time == 2) {
$now = new \DateTime();
$now->modify('first day of last month');
$start_time = $now->format('Y-m-01 00:00:00');
$end_time = date("Y-m-d 23:59:59", strtotime(-date('d') . 'day'));
$data = ['between', 'pay_time', strtotime($start_time), strtotime($end_time)];
}
if (!empty($get['start_time']) && !empty($get['end_time'])) {
$data = ['between', 'pay_time', strtotime($get['start_time']), strtotime($get['end_time'])];
}
if ($admin->role == UserRoleEnum::PROVINCE_DAI) {//省代
$store_id = Store::find()->select(['id'])->where(['province_id' => $admin->province_id])->column();
$all_sale = Reconciliation::find()->alias('r')->andWhere([
'and',
['r.status' => 1],
['in', 'r.store_id', $store_id]
])->andWhere($data ?? '')->sum('number');
$all_total_price = Reconciliation::find()->alias('r')->andWhere([
'and',
['r.status' => 1],
['in', 'r.store_id', $store_id]
])->andWhere($data ?? '')->sum('total_price');
} elseif ($admin->role == UserRoleEnum::CITY_DAI) {//市代
$store_id = Store::find()->select(['id'])->where(['city_id' => $admin->city_id])->column();
$all_sale = Reconciliation::find()->alias('r')->andWhere([
'and',
['r.status' => 1],
['in', 'r.store_id', $store_id]
])->andWhere($data ?? '')->sum('number');
$all_total_price = Reconciliation::find()->alias('r')->andWhere([
'and',
['r.status' => 1],
['in', 'r.store_id', $store_id]
])->andWhere($data ?? '')->sum('total_price');
} elseif ($admin->role == UserRoleEnum::SUPPLY) {//业务员
$store_id = Store::find()->select(['id'])->where(['code' => $admin->code])->column();
$all_sale = Reconciliation::find()->alias('r')->andWhere([
'and',
['r.status' => 1],
['in', 'r.store_id', $store_id]
])->andWhere($data ?? '')->sum('number');
$all_total_price = Reconciliation::find()->alias('r')->andWhere([
'and',
['r.status' => 1],
['in', 'r.store_id', $store_id]
])->andWhere($data ?? '')->sum('total_price');
} else {
throw new Exception('角色错误');
}
return [
'all_sale' => $all_sale ?? 0,
'all_total_price' => $all_total_price ?? 0,
];
}
}