| @@ -16,22 +16,24 @@ | ||
| 16 | 16 | ->leftJoinSub($itemsSub, 'oi_sum', fn($join) => $join->on('oi_sum.order_id', '=', 'o.id')); |
| 17 | 17 | |
| 18 | 18 | $query = $this->applyFilters($query, $params); |
| 19 | 19 | |
| 20 | + $groupKey = ReportHelper::sanitizeGroupKey($params['groupKey'] ?? null); | |
| 21 | + | |
| 20 | 22 | $groupKeyExpression = ''; |
| 21 | 23 | |
| 22 | - if (in_array($params['groupKey'], ['billing_country', 'shipping_country'])) { | |
| 23 | - $type = $params['groupKey'] === 'billing_country' ? 'billing' : 'shipping'; | |
| 24 | + if (in_array($groupKey, ['billing_country', 'shipping_country'])) { | |
| 25 | + $type = $groupKey === 'billing_country' ? 'billing' : 'shipping'; | |
| 24 | 26 | |
| 25 | 27 | $query->leftJoin('fct_order_addresses as a', function ($join) use ($type) { |
| 26 | 28 | $join->on('o.id', 'a.order_id')->where('a.type', $type); |
| 27 | 29 | }); |
| 28 | 30 | |
| 29 | - $groupKeyExpression = "COALESCE(a.country, 'Uncategorized') AS `{$params['groupKey']}`"; | |
| 30 | - } elseif ($params['groupKey'] === 'payment_method') { | |
| 31 | - $groupKeyExpression = "COALESCE(o.payment_method, 'Unknown') AS `{$params['groupKey']}`"; | |
| 31 | + $groupKeyExpression = "COALESCE(a.country, 'Uncategorized') AS `{$groupKey}`"; | |
| 32 | + } elseif ($groupKey === 'payment_method') { | |
| 33 | + $groupKeyExpression = "COALESCE(o.payment_method, 'Unknown') AS `{$groupKey}`"; | |
| 32 | 34 | } else { |
| 33 | - $groupKeyExpression = "COALESCE(o.{$params['groupKey']}, 'Unknown') AS `{$params['groupKey']}`"; | |
| 35 | + $groupKeyExpression = "COALESCE(o.{$groupKey}, 'Unknown') AS `{$groupKey}`"; | |
| 34 | 36 | } |
| 35 | 37 | |
| 36 | 38 | $query->selectRaw("{$groupKeyExpression}, |
| 37 | 39 | |
| @@ -72,9 +74,9 @@ | ||
| 72 | 74 | SUM(o.total_refund) / 100 AS total_refunds, |
| 73 | 75 | |
| 74 | 76 | COUNT(DISTINCT o.customer_id) AS customer_count"); |
| 75 | 77 | |
| 76 | - return $query->groupByRaw($params['groupKey'])->orderByRaw($params['groupKey'])->get()->toArray(); | |
| 78 | + return $query->groupByRaw($groupKey)->orderByRaw($groupKey)->get()->toArray(); | |
| 77 | 79 | } |
| 78 | 80 | |
| 79 | 81 | public function getRevenueData($params = []): array |
| 80 | 82 | { |