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('s'); $store->where(['s.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->alias('st'); $store->where(['uid' => $admin->uid]); }]); break; default: throw new Exception('参数错误'); } } else { throw new Exception('角色错误'); } $start_time = 0; $end_time = 0; 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 = 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', $start_time, $end_time]); }]); } if (!empty($sort)) { switch ($sort) { case 1: if (!empty($search)) $query->andWhere([ 'or', ['like', 's.name', $search], ['like', 's.shouzimu', $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) use ($start_time, $end_time) { return $this->actionSalePrice($s->id, $start_time, $end_time); } ] ]; 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: 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)) { $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('参数错误'); } } $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 { $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; } // 根据金额排序 usort($list, function ($a, $b) { return $b['total_price'] <=> $a['total_price']; }); return [ 'list' => array_values($list), 'totalPrice' => $totalPrice, 'totalNumber' => $totalNumber, ]; default: throw new Exception('参数错误'); } } /** * 统计销售金额 * * @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]); 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; } /** * @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']; $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'); $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) {//省代 $where = ['s.province_id' => $admin->province_id]; } elseif ($admin->role == UserRoleEnum::CITY_DAI) {//市代 $where = ['s.city_id' => $admin->city_id]; } elseif ($admin->role == UserRoleEnum::SUPPLY) {//业务员 $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' => $query->sum('number') ?? 0, 'all_total_price' => $query->sum('total_price') ?? 0, ]; } }