| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- namespace addons\Store\common\services;
- use addons\Store\common\models\Store;
- use addons\Store\common\models\StoreCashierOrder;
- use addons\Store\common\models\StoreCommission;
- use addons\Store\common\models\StoreCommissionCheck;
- use common\enums\StatusEnum;
- use common\models\user\User;
- /**
- * 资产统计service
- */
- class StoreStatisticsService extends BaseService
- {
- public function __construct(int $mallId, int $storeId)
- {
- parent::__construct();
- $this->store_id = $storeId;
- $this->mall_id = $mallId;
- }
- /**
- * 提成明细(直接拿门店的明细修改)
- *
- * @param array $get
- * @param int $pageSize
- *
- * @return array
- */
- public function getIncomeLogList(array $get, int $pageSize): array
- {
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['store_id' => $this->store_id];
- $where[] = ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- // 是否没有提交筛选条件 (用于决定总数直接从store表拿还是要重新查询统计)
- $whereIsEmpty = true;
- if (isset($get['keyword']) && $get['keyword'] !== '') {
- $whereIsEmpty = false;
- $user_list = User::lists([
- 'where' => [
- ['mall_id' => $this->mall_id],
- [
- 'or',
- ['like', 'username', trim($get['keyword'])],
- ['like', 'nickname', trim($get['keyword'])],
- ['like', 'mobile', trim($get['keyword'])]
- ]
- ],
- 'select' => 'id'
- ]);
- $where[] = ['user_id' => array_column($user_list, 'id')];
- }
- if (!empty($get['user_id'])) {
- $whereIsEmpty = false;
- $where[] = ['user_id' => $get['user_id']];
- }
- if (isset($get['order_no']) && $get['order_no'] !== '') {
- $whereIsEmpty = false;
- $where[] = ['order_no' => $get['order_no']];
- }
- if (isset($get['is_settlement']) && $get['is_settlement'] !== '') {
- $whereIsEmpty = false;
- $where[] = ['is_settlement' => $get['is_settlement']];
- }
- if (!empty($get['date'])) {
- $whereIsEmpty = false;
- $get['date'][1] .= ' 23:59:59';
- array_walk($get['date'], function (&$item) {
- $item = strtotime($item);
- });
- $where[] = ['between', 'created_at', $get['date'][0], $get['date'][1]];
- }
- $params = [
- 'where' => $where,
- 'order' => 'id desc',
- 'with' => ['user'],
- 'limit' => $get['limit'] ?? $pageSize,
- ];
- $page_data = StoreCommission::listPage($params);
- $total_num = $total_order_money = $total_settled_amount = $unsettled_amount = 0;
- if ($page_data) {
- $store = Store::findOne(['id' => $this->store_id]);
- // 如果所有搜索条件为空则显示这个,否则需要查询
- if ($whereIsEmpty) {
- if ($store) {
- $commissionTotal = $store;
- }
- } else {
- // 按搜索结果 sum
- $commissionTotalField = [
- 'COUNT(*) `total_num`',
- 'SUM(`pay_money`) `total_order_money`',
- 'SUM(IF(`is_settlement` = 1, `amount_received`, 0)) `total_settled_amount`',
- 'SUM(IF(`is_settlement` = 0, `amount_received`, 0)) `unsettled_amount`'
- ];
- $commissionTotal = StoreCommission::getOne($where, true, [], '', $commissionTotalField);
- }
- if (isset($commissionTotal)) {
- $total_num = $commissionTotal['total_num'];
- $total_order_money = $commissionTotal['total_order_money'];
- $total_settled_amount = $commissionTotal['total_settled_amount'];
- $unsettled_amount = $commissionTotal['unsettled_amount'];
- }
- if (!empty($page_data['list'])) {
- $user_id_arr = array_column($page_data['list'], 'user_id');
- $user_list = User::lists(['where' => [['id' => $user_id_arr]], 'index' => 'id']);
- $order_no_arr = array_column($page_data['list'], 'order_no');
- foreach ($page_data['list'] as &$item) {
- if ($item['type'] == 2) $order_no_arr[] = $item['order_no'];
- }
- if (!empty($order_no_arr)) {
- $store_cashier_order_list = StoreCashierOrder::lists(['where' => [['order_no' => $order_no_arr]], 'select' => 'order_no,extra', 'index' => 'order_no']);
- }
- $ids = array_column($page_data['list'], 'id');
- $check_list = StoreCommissionCheck::lists([
- 'where' => [
- ['store_commission_id' => $ids],
- ['check_status' => [0, 1]]
- ],
- 'index' => 'store_commission_id'
- ]);
- foreach ($page_data['list'] as &$item) {
- $item['nickname'] = $item['mobile'] = $item['avatar_url'] = '';
- if (isset($user_list[$item['user_id']])) {
- $item['nickname'] = $user_list[$item['user_id']]['nickname'];
- $item['mobile'] = $user_list[$item['user_id']]['mobile'];
- $item['avatar_url'] = $user_list[$item['user_id']]['avatar_url'];
- }
- if ($item['type'] == 2) {
- if (isset($store_cashier_order_list[$item['order_no']])) {
- $addons_bonus = 0;
- $extra = json_decode($store_cashier_order_list[$item['order_no']]['extra'], true);
- if (!empty($extra)) {
- foreach ($extra as $k => $v) {
- if (in_array($k, ['agent_commission', 'distribution_commission', 'boss_commission', 'area_commission'])) $addons_bonus += $v;
- }
- }
- $item['addons_bonus'] = bcadd($addons_bonus, 0, 2);
- }
- }
- if (empty($store['payment_settlement_type'])) {
- $item['is_show'] = 0;
- } else {
- $item['is_show'] = 1;
- if (isset($check_list[$item['id']])) {
- $item['is_show'] = 0;
- }
- }
- }
- }
- }
- $order_total_arr = [];
- $order_total_arr[] = ['type' => 'total_num', 'name' => '总数', 'value' => $total_num ?: 0, 'prompt' => ''];
- $order_total_arr[] = ['type' => 'total_order_money', 'name' => '订单总金额', 'value' => $total_order_money ?: 0, 'prompt' => ''];
- $order_total_arr[] = ['type' => 'total_settled_amount', 'name' => '提成已结算总金额', 'value' => $total_settled_amount ?: 0, 'prompt' => ''];
- $order_total_arr[] = ['type' => 'unsettled_amount', 'name' => '提成未结算总金额', 'value' => $unsettled_amount ?: 0, 'prompt' => ''];
- $page_data['order_total_arr'] = $order_total_arr;
- $page_data['store_name'] = isset($store) ? $store['store_name'] : '';
- return $page_data;
- }
- }
|