Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
QueueModalService
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 9
306
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getBuyers
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getBuyersWithClockStatus
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
30
 getAllBuyers
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getQueueItem
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getQueueItemFromBuyQueue
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getStoreInfo
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getStore
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDb
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * QueueModalService
4 *
5 * Service class that encapsulates business logic for queue modals.
6 * Handles fetching buyers, queue items, and related data needed for modal rendering.
7 *
8 * @package BuyerKiosk\Queue
9 */
10
11namespace BuyerKiosk\Queue;
12
13use BuyerKiosk\WhenIWork\EmployeesController;
14use Store;
15use BuyQueue;
16use PDO;
17
18class QueueModalService
19{
20    /**
21     * @var \Slim\Slim Slim application instance
22     */
23    private $app;
24
25    /**
26     * @var Store Store object
27     */
28    private $store;
29
30    /**
31     * @var PDO Database connection
32     */
33    private $db;
34
35    /**
36     * Constructor
37     *
38     * @param \Slim\Slim $app Slim application instance
39     * @param Store $store Store object
40     */
41    public function __construct($app, Store $store)
42    {
43        $this->app = $app;
44        $this->store = $store;
45        $this->db = \dbConnectByName($store->getDbName());
46    }
47
48    /**
49     * Get buyers/employees for queue action modals
50     *
51     * If WhenIWork is enabled, returns clocked-in employees.
52     * Otherwise, returns all configured buyers from the store.
53     *
54     * @return array Array of buyer data with employeeID, employeeFirstName, employeeLastName
55     */
56    public function getBuyers(): array
57    {
58        if ($this->store->getWiwEnable()) {
59            $employees = new EmployeesController($this->app, $this->store);
60            return $employees->getClockedInEmployeesForQueue($employees->getClockedInEmployees());
61        }
62
63        return \getBuyersArray($this->store);
64    }
65
66    /**
67     * Get buyers/employees with clock-in status for queue action modals
68     *
69     * When WhenIWork is enabled, returns structured data with:
70     * - wiwEnabled: boolean indicating WhenIWork is active
71     * - clockedIn: array of employees currently clocked in
72     * - allEmployees: array of all active employees
73     *
74     * When WhenIWork is disabled, returns all active employees with wiwEnabled=false
75     *
76     * @return array{wiwEnabled: bool, clockedIn: array, allEmployees: array}
77     */
78    public function getBuyersWithClockStatus(): array
79    {
80        try {
81            $allEmployees = $this->getAllBuyers();
82
83            if (!$this->store->getWiwEnable()) {
84                return [
85                    'wiwEnabled' => false,
86                    'clockedIn' => [],
87                    'allEmployees' => $allEmployees
88                ];
89            }
90
91            $employeesController = new EmployeesController($this->app, $this->store);
92            $clockedInRaw = $employeesController->getClockedInEmployees();
93            $clockedIn = $employeesController->getClockedInEmployeesForQueue($clockedInRaw);
94
95            // Build a set of clocked-in employee IDs for quick lookup
96            $clockedInIds = [];
97            foreach ($clockedIn as $emp) {
98                $clockedInIds[$emp['employeeID']] = true;
99            }
100
101            // Mark all employees with their clock status
102            foreach ($allEmployees as &$emp) {
103                $emp['isClockedIn'] = isset($clockedInIds[$emp['employeeID']]);
104            }
105
106            return [
107                'wiwEnabled' => true,
108                'clockedIn' => $clockedIn,
109                'allEmployees' => $allEmployees
110            ];
111        } catch (\Exception $e) {
112            // Log the error and fall back to basic buyers list
113            error_log('[QueueModalService] getBuyersWithClockStatus error: ' . $e->getMessage());
114
115            // Fall back to non-WIW behavior
116            return [
117                'wiwEnabled' => false,
118                'clockedIn' => [],
119                'allEmployees' => \getBuyersArray($this->store)
120            ];
121        }
122    }
123
124    /**
125     * Get all active buyers/employees regardless of clock status
126     *
127     * @return array Array of buyer data with employeeID, employeeFirstName, employeeLastName
128     */
129    public function getAllBuyers(): array
130    {
131        if ($this->store->getWiwEnable()) {
132            $employees = new EmployeesController($this->app, $this->store);
133            return $employees->getClockedInEmployeesForQueue($employees->getEmployees());
134        }
135
136        return \getBuyersArray($this->store);
137    }
138
139    /**
140     * Get queue item by buyID
141     *
142     * @param int|string $buyId The buy ID
143     * @return array|null Queue item data or null if not found
144     */
145    public function getQueueItem($buyId): ?array
146    {
147        $item = \getQueueItemArray($this->store, $buyId);
148        return $item ?: null;
149    }
150
151    /**
152     * Get queue item using BuyQueue object
153     *
154     * @param int|string $buyId The buy ID
155     * @return array|null Queue item data or null if not found
156     */
157    public function getQueueItemFromBuyQueue($buyId): ?array
158    {
159        $buyQueue = new BuyQueue();
160        $buyQueue->createStore($this->store->getTypeNum());
161        $item = $buyQueue->getOneQueueItem($this->db, $buyId);
162
163        return $item ?: null;
164    }
165
166    /**
167     * Get store information for modal display
168     *
169     * @return array Store info array
170     */
171    public function getStoreInfo(): array
172    {
173        return \getStoreInfo($this->store);
174    }
175
176    /**
177     * Get the store object
178     *
179     * @return Store
180     */
181    public function getStore(): Store
182    {
183        return $this->store;
184    }
185
186    /**
187     * Get database connection
188     *
189     * @return PDO
190     */
191    public function getDb(): PDO
192    {
193        return $this->db;
194    }
195}