Skip to content

Commit

Permalink
clean up reports
Browse files Browse the repository at this point in the history
  • Loading branch information
daveearley committed Nov 17, 2024
1 parent f71d42f commit d27b37d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace HiEvents\Services\Domain\Report\Reports;

use HiEvents\DomainObjects\Status\OrderStatus;
use HiEvents\Services\Domain\Report\AbstractReportService;
use Illuminate\Support\Carbon;

Expand All @@ -11,6 +12,7 @@ protected function getSqlQuery(Carbon $startDate, Carbon $endDate): string
{
$startDateString = $startDate->format('Y-m-d H:i:s');
$endDateString = $endDate->format('Y-m-d H:i:s');
$completedStatus = OrderStatus::COMPLETED->name;

return <<<SQL
WITH filtered_orders AS (
Expand All @@ -22,7 +24,7 @@ protected function getSqlQuery(Carbon $startDate, Carbon $endDate): string
oi.id AS order_item_id
FROM order_items oi
JOIN orders o ON oi.order_id = o.id
WHERE o.status = 'COMPLETED'
WHERE o.status = '$completedStatus'
AND o.event_id = :event_id
AND o.created_at BETWEEN '$startDateString' AND '$endDateString'
AND oi.deleted_at IS NULL
Expand Down
18 changes: 13 additions & 5 deletions backend/app/Services/Domain/Report/Reports/PromoCodesReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace HiEvents\Services\Domain\Report\Reports;

use HiEvents\DomainObjects\Status\OrderStatus;
use HiEvents\Services\Domain\Report\AbstractReportService;
use Illuminate\Support\Carbon;

Expand All @@ -11,6 +12,13 @@ protected function getSqlQuery(Carbon $startDate, Carbon $endDate): string
{
$startDateString = $startDate->format('Y-m-d H:i:s');
$endDateString = $endDate->format('Y-m-d H:i:s');
$reservedString = OrderStatus::RESERVED->name;
$translatedStringMap = [
'Expired' => __('Expired'),
'Limit Reached' => __('Limit Reached'),
'Deleted' => __('Deleted'),
'Active' => __('Active'),
];

return <<<SQL
WITH order_totals AS (
Expand All @@ -27,7 +35,7 @@ protected function getSqlQuery(Carbon $startDate, Carbon $endDate): string
JOIN order_items oi ON oi.order_id = o.id
WHERE
o.deleted_at IS NULL
AND o.status NOT IN ('RESERVED')
AND o.status NOT IN ('$reservedString')
AND o.event_id = :event_id
AND o.created_at >= '$startDateString'
AND o.created_at <= '$endDateString'
Expand Down Expand Up @@ -67,10 +75,10 @@ protected function getSqlQuery(Carbon $startDate, Carbon $endDate): string
THEN pc.max_allowed_usages - COUNT(ot.order_id)::integer
END as remaining_uses,
CASE
WHEN pc.expiry_date < CURRENT_TIMESTAMP THEN 'Expired'
WHEN pc.max_allowed_usages IS NOT NULL AND COUNT(ot.order_id) >= pc.max_allowed_usages THEN 'Limit Reached'
WHEN pc.deleted_at IS NOT NULL THEN 'Deleted'
ELSE 'Active'
WHEN pc.expiry_date < CURRENT_TIMESTAMP THEN '{$translatedStringMap['Expired']}'
WHEN pc.max_allowed_usages IS NOT NULL AND COUNT(ot.order_id) >= pc.max_allowed_usages THEN '{$translatedStringMap['Limit Reached']}'
WHEN pc.deleted_at IS NOT NULL THEN '{$translatedStringMap['Deleted']}'
ELSE '{$translatedStringMap['Active']}'
END as status
FROM promo_codes pc
LEFT JOIN order_totals ot ON pc.id = ot.promo_code_id
Expand Down

0 comments on commit d27b37d

Please sign in to comment.