From a59b93449fcc8b9df963747894fa1fb479279b70 Mon Sep 17 00:00:00 2001 From: lq Date: Mon, 28 Oct 2024 09:19:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=9A=E5=8A=A1=E6=8E=A8?= =?UTF-8?q?=E5=B9=BF=E8=AE=A1=E7=AE=97=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/controllers/base/SupplyController.php | 217 ++++++++++++-------- 1 file changed, 129 insertions(+), 88 deletions(-) diff --git a/admin/controllers/base/SupplyController.php b/admin/controllers/base/SupplyController.php index e8d20f3..1733af3 100644 --- a/admin/controllers/base/SupplyController.php +++ b/admin/controllers/base/SupplyController.php @@ -44,8 +44,8 @@ class SupplyController extends BaseAdminController 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]); + $store->alias('s'); + $store->where(['s.province_id' => $admin->province_id]); }]); break; default: @@ -72,6 +72,7 @@ class SupplyController extends BaseAdminController 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(['uid' => $admin->uid]); }]); @@ -83,6 +84,8 @@ class SupplyController extends BaseAdminController throw new Exception('角色错误'); } + $start_time = 0; + $end_time = 0; switch ($get['type']) { case 1: if ($limit_time == 1) { @@ -97,19 +100,23 @@ class SupplyController extends BaseAdminController 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')); + $start_time = strtotime($now->format('Y-m-01 00:00:00')); + $end_time = strtotime(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)]); + 'between', 're.pay_time', $start_time, $end_time]); }]); } if (!empty($sort)) { switch ($sort) { case 1: - if (!empty($search)) $query->andWhere(['like', 's.name', $search]); + if (!empty($search)) $query->andWhere([ + 'or', + ['like', 's.name', $search], + ['like', 's.shouzimu', $search], + ]); break; case 2: if (!empty($search)) { @@ -145,8 +152,8 @@ class SupplyController extends BaseAdminController 'city' => 'city.name', 'city_id', 'province_id', - 'sale_price' => function ($s) { - return $this->actionSalePrice($s->id); + 'sale_price' => function ($s) use ($start_time, $end_time) { + return $this->actionSalePrice($s->id, $start_time, $end_time); } ] ]; @@ -171,14 +178,15 @@ class SupplyController extends BaseAdminController 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], - ]); - }]); + if (!empty($search)) { + $query->joinWith(['store' => function ($s) use ($search) { + $s->alias('st')->andWhere([ + 'or', + ['like', 'st.name', $search], + ['like', 'st.shouzimu', $search], + ]); + }]); + } break; case 2: if (!empty($search)) { @@ -214,51 +222,65 @@ class SupplyController extends BaseAdminController } } -// -// $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]]); + $lists = $query->select([ + 'r.id', + 'r.store_id', + 'r.drug_id', + 'r.number', + 'r.total_price', + ])->all(); + $list = []; + $totalPrice = 0; + $totalNumber = 0; + foreach ($lists as $v) { + if (array_key_exists($v->drug_id, $list)) { + $list[$v->drug_id]['total_price'] = bcadd($list[$v->drug_id]['total_price'], $v->total_price, 3); + $list[$v->drug_id]['number'] += $v->number; } else { - $query->andWhere(['r.drug_type' => [2, 4]]); + $list[$v->drug_id] = [ + 'drug_id' => $v->drug_id, + 'drug_name' => $v->drug->drug_name, + 'number' => $v->number, + 'drug_number' => $v->drug->drug_number, + 'total_price' => $v->total_price, + ]; } + $totalPrice = bcadd($v->total_price, $totalPrice, 3); + $totalNumber += $v->number; } - $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', - ] + // 根据金额排序 + usort($list, function ($a, $b) { + return $b['total_price'] <=> $a['total_price']; + }); + + return [ + 'list' => array_values($list), + 'totalPrice' => $totalPrice, + 'totalNumber' => $totalNumber, ]; - return $this->create($query, $get); default: throw new Exception('参数错误'); } } - public function actionSalePrice($store) + /** + * 统计销售金额 + * + * @param $store + * @param $start_time + * @param $end_time + * @return bool|int|mixed|string + */ + public function actionSalePrice($store, $start_time, $end_time) { - $Reconciliation = Reconciliation::find()->where(['store_id' => $store, 'status' => 1])->sum('total_price'); - return $Reconciliation ?? 0; + $reconciliation = Reconciliation::find()->where(['store_id' => $store, 'status' => 1]); + if (!empty($start_time) && !empty($end_time)) { + $reconciliation->andWhere(['between', 'pay_time', $start_time, $end_time]); + } + $reconciliation = $reconciliation->sum('total_price'); + // 获取执行的sql + return $reconciliation ?? 0; } /** @@ -337,12 +359,13 @@ class SupplyController extends BaseAdminController $get = \Yii::$app->request->get(); $admin = \Yii::$app->user->identity; $limit_time = $get['limit_time']; + $search = $get['search']; + $sort = $get['sort']; 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'); @@ -355,51 +378,69 @@ class SupplyController extends BaseAdminController } 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'); + $where = ['s.province_id' => $admin->province_id]; } 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'); + $where = ['s.city_id' => $admin->city_id]; } 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'); + $where = ['s.code' => $admin->code]; } else { throw new Exception('角色错误'); } + // TODO 这里数据不对 + $query = Reconciliation::find()->alias('r')->where(['r.status' => 1])->andWhere($data ?? '')->joinWith(['store' => function ($store) use ($where) { + $store->alias('s'); + $store->where($where); + }]); + + if (!empty($sort)) { + switch ($sort) { + case 1: + $query->joinWith(['store' => function ($s) use ($search) { + $s->alias('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('参数错误'); + } + } return [ - 'all_sale' => $all_sale ?? 0, - 'all_total_price' => $all_total_price ?? 0, + 'all_sale' => $query->sum('number') ?? 0, + 'all_total_price' => $query->sum('total_price') ?? 0, ]; } } \ No newline at end of file