Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
58.33% covered (warning)
58.33%
35 / 60
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ComebackCashAdminPageController
58.33% covered (warning)
58.33%
35 / 60
50.00% covered (danger)
50.00%
1 / 2
14.86
0.00% covered (danger)
0.00%
0 / 1
 main
28.57% covered (danger)
28.57%
10 / 35
0.00% covered (danger)
0.00%
0 / 1
14.11
 getEventStatistics
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2/**
3 * ComebackCashAdminPageController
4 *
5 * Handles rendering of the Comeback Cash admin management pages.
6 * These pages are for store owners/managers to create, configure,
7 * and manage Comeback Cash promotional events.
8 *
9 * The admin interface provides:
10 * - Event creation and configuration
11 * - Event lifecycle management (draft, scheduled, active, ended)
12 * - Reporting and analytics
13 * - Settings configuration
14 */
15
16namespace BuyerKiosk\ComebackCash\Controllers;
17
18use BuyerKiosk\Core\Controllers\BaseController;
19use BuyerKiosk\StoreController;
20
21class ComebackCashAdminPageController extends BaseController
22{
23    /**
24     * Render the main Comeback Cash admin page
25     *
26     * GET /admin/:typeNum/comeback-cash/
27     *
28     * Displays the full event management interface with:
29     * - Events list with filtering by side and status
30     * - Create/Edit event forms
31     * - Event statistics and reporting
32     *
33     * @param string $typeNum Store type number
34     */
35    public function main($typeNum)
36    {
37        $app = $this->_app;
38
39        // Check authentication
40        if (!$app->user) {
41            $app->redirect($app->urlFor('login'));
42            return;
43        }
44
45        // Check user has store access
46        if (!$app->user->checkStoreGroup($typeNum)) {
47            $app->notAuthorized();
48            return;
49        }
50
51        // Check user has admin permission for comeback cash
52        // Using uri_store_settings as the primary admin permission
53        if (!$app->user->checkAccess('uri_store_settings')) {
54            $app->notAuthorized();
55            return;
56        }
57
58        // Get store
59        $storeController = new StoreController($typeNum);
60        $store = $storeController->getStore();
61
62        if (!$store) {
63            $app->notFound();
64            return;
65        }
66
67        // Get store directory for CSS/asset paths
68        $storeDir = \getStoreDirectory($store);
69
70        // Get database connection
71        $db = \dbConnectByName($store->getDbName());
72
73        // Get summary statistics for the dashboard
74        $stats = $this->getEventStatistics($db);
75
76        // Render the admin template
77        $app->render('admin/comeback-cash/index.html', [
78            'page' => [
79                'title' => 'Comeback Cash Management',
80                'description' => 'Create and manage Comeback Cash promotional events'
81            ],
82            'store' => [
83                'typeNum' => $store->getTypeNum(),
84                'name' => $store->getCompanyName(),
85                'city' => $store->getCity(),
86                'storeType' => $storeDir
87            ],
88            'typeNum' => $typeNum,
89            'stats' => $stats,
90            'csrf_token' => \NoCSRF::generate('csrf_token'),
91            'user' => $app->user,
92            'ably_key' => $_ENV['ABLY_KEY'] ?? ''
93        ]);
94    }
95
96    /**
97     * Get summary statistics for the Comeback Cash dashboard
98     *
99     * @param \PDO $db Database connection
100     * @return array Statistics array
101     */
102    private function getEventStatistics($db)
103    {
104        $stats = [
105            'activeEvents' => 0,
106            'totalCouponsIssued' => 0,
107            'totalCouponsRedeemed' => 0,
108            'totalValueIssued' => 0,
109            'totalValueRedeemed' => 0
110        ];
111
112        try {
113            // Check if tables exist first
114            $tableCheck = $db->query("SHOW TABLES LIKE 'ccEvents'");
115            if ($tableCheck->rowCount() === 0) {
116                return $stats;
117            }
118
119            // Count active events
120            $stmt = $db->query("SELECT COUNT(*) as cnt FROM ccEvents WHERE status = 'active'");
121            $row = $stmt->fetch(\PDO::FETCH_ASSOC);
122            $stats['activeEvents'] = (int)($row['cnt'] ?? 0);
123
124            // Get coupon statistics
125            $tableCheck = $db->query("SHOW TABLES LIKE 'ccCoupons'");
126            if ($tableCheck->rowCount() > 0) {
127                $stmt = $db->query("
128                    SELECT
129                        COUNT(*) as total_issued,
130                        SUM(CASE WHEN status = 'redeemed' THEN 1 ELSE 0 END) as total_redeemed,
131                        SUM(original_value) as value_issued,
132                        SUM(CASE WHEN status = 'redeemed' THEN original_value ELSE 0 END) as value_redeemed
133                    FROM ccCoupons
134                ");
135                $row = $stmt->fetch(\PDO::FETCH_ASSOC);
136                $stats['totalCouponsIssued'] = (int)($row['total_issued'] ?? 0);
137                $stats['totalCouponsRedeemed'] = (int)($row['total_redeemed'] ?? 0);
138                $stats['totalValueIssued'] = (float)($row['value_issued'] ?? 0);
139                $stats['totalValueRedeemed'] = (float)($row['value_redeemed'] ?? 0);
140            }
141        } catch (\Exception $e) {
142            error_log("ComebackCashAdminPageController::getEventStatistics error: " . $e->getMessage());
143        }
144
145        return $stats;
146    }
147}