Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 101
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
KPIConfig
0.00% covered (danger)
0.00%
0 / 101
0.00% covered (danger)
0.00%
0 / 8
506
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getAll
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 getVisible
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 getByKey
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 update
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
20
 updateSingle
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
30
 resetDefaults
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
12
 toggleVisibility
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace BuyerKiosk\Workbook;
4
5use PDO;
6use PDOException;
7use Exception;
8
9/**
10 * KPIConfig - Manages KPI configuration and display settings
11 */
12class KPIConfig
13{
14    protected $store;
15    protected $storeDB;
16
17    /**
18     * Constructor
19     *
20     * @param \Store $store Store object
21     */
22    public function __construct(\Store $store)
23    {
24        $this->store = $store;
25        $this->storeDB = dbConnectByName($this->store->getDbName());
26    }
27
28    /**
29     * Get all KPI configurations ordered by sort order
30     *
31     * @return array KPI configurations
32     */
33    public function getAll(): array
34    {
35        try {
36            $stmt = $this->storeDB->prepare(
37                "SELECT * FROM workbook_kpi_config
38                 ORDER BY sortOrder ASC"
39            );
40            $stmt->execute();
41
42            return $stmt->fetchAll(PDO::FETCH_ASSOC);
43        } catch (PDOException $e) {
44            error_log("KPIConfig: Error fetching all configs - " . $e->getMessage());
45            return [];
46        }
47    }
48
49    /**
50     * Get only visible KPI configurations
51     *
52     * @return array Visible KPI configurations
53     */
54    public function getVisible(): array
55    {
56        try {
57            $stmt = $this->storeDB->prepare(
58                "SELECT * FROM workbook_kpi_config
59                 WHERE isVisible = 1
60                 ORDER BY sortOrder ASC"
61            );
62            $stmt->execute();
63
64            return $stmt->fetchAll(PDO::FETCH_ASSOC);
65        } catch (PDOException $e) {
66            error_log("KPIConfig: Error fetching visible configs - " . $e->getMessage());
67            return [];
68        }
69    }
70
71    /**
72     * Get configuration for a specific KPI
73     *
74     * @param string $kpiKey KPI key
75     * @return array|null KPI configuration or null if not found
76     */
77    public function getByKey(string $kpiKey): ?array
78    {
79        try {
80            $stmt = $this->storeDB->prepare(
81                "SELECT * FROM workbook_kpi_config
82                 WHERE kpiKey = :kpiKey"
83            );
84            $stmt->execute([':kpiKey' => $kpiKey]);
85
86            $result = $stmt->fetch(PDO::FETCH_ASSOC);
87            return $result ?: null;
88        } catch (PDOException $e) {
89            error_log("KPIConfig: Error fetching config for {$kpiKey} - " . $e->getMessage());
90            return null;
91        }
92    }
93
94    /**
95     * Update KPI configuration
96     *
97     * @param array $config Array of KPI configurations to update
98     *                      Format: [['kpiKey' => 'sales', 'isVisible' => 1, 'sortOrder' => 1], ...]
99     * @return bool Success status
100     */
101    public function update(array $config): bool
102    {
103        try {
104            $this->storeDB->beginTransaction();
105
106            $stmt = $this->storeDB->prepare(
107                "UPDATE workbook_kpi_config
108                 SET isVisible = :isVisible,
109                     sortOrder = :sortOrder,
110                     showGoal = :showGoal,
111                     showComps = :showComps
112                 WHERE kpiKey = :kpiKey"
113            );
114
115            foreach ($config as $kpi) {
116                if (!isset($kpi['kpiKey'])) {
117                    continue;
118                }
119
120                $stmt->execute([
121                    ':kpiKey' => $kpi['kpiKey'],
122                    ':isVisible' => $kpi['isVisible'] ?? 1,
123                    ':sortOrder' => $kpi['sortOrder'] ?? 0,
124                    ':showGoal' => $kpi['showGoal'] ?? 1,
125                    ':showComps' => $kpi['showComps'] ?? 1
126                ]);
127            }
128
129            $this->storeDB->commit();
130            return true;
131        } catch (PDOException $e) {
132            $this->storeDB->rollBack();
133            error_log("KPIConfig: Error updating config - " . $e->getMessage());
134            return false;
135        }
136    }
137
138    /**
139     * Update a single KPI configuration
140     *
141     * @param string $kpiKey KPI key
142     * @param array $updates Configuration updates
143     * @return bool Success status
144     */
145    public function updateSingle(string $kpiKey, array $updates): bool
146    {
147        try {
148            $allowedFields = ['isVisible', 'sortOrder', 'showGoal', 'showComps', 'displayName'];
149            $updateFields = [];
150            $params = [':kpiKey' => $kpiKey];
151
152            foreach ($updates as $field => $value) {
153                if (in_array($field, $allowedFields)) {
154                    $updateFields[] = "{$field} = :{$field}";
155                    $params[":{$field}"] = $value;
156                }
157            }
158
159            if (empty($updateFields)) {
160                return false;
161            }
162
163            $sql = "UPDATE workbook_kpi_config SET " . implode(', ', $updateFields) . " WHERE kpiKey = :kpiKey";
164            $stmt = $this->storeDB->prepare($sql);
165            $stmt->execute($params);
166
167            return $stmt->rowCount() > 0;
168        } catch (PDOException $e) {
169            error_log("KPIConfig: Error updating single config - " . $e->getMessage());
170            return false;
171        }
172    }
173
174    /**
175     * Reset all KPIs to default configuration
176     *
177     * @return bool Success status
178     */
179    public function resetDefaults(): bool
180    {
181        try {
182            $this->storeDB->beginTransaction();
183
184            // Truncate existing config
185            $this->storeDB->exec("TRUNCATE TABLE workbook_kpi_config");
186
187            // Insert defaults
188            $stmt = $this->storeDB->prepare(
189                "INSERT INTO workbook_kpi_config (kpiKey, displayName, sortOrder, isVisible, showGoal, showComps)
190                 VALUES (:kpiKey, :displayName, :sortOrder, :isVisible, :showGoal, :showComps)"
191            );
192
193            $defaults = [
194                ['kpiKey' => 'sales', 'displayName' => 'Sales', 'sortOrder' => 1, 'isVisible' => 1, 'showGoal' => 1, 'showComps' => 1],
195                ['kpiKey' => 'avgTrans', 'displayName' => 'Avg Trans', 'sortOrder' => 2, 'isVisible' => 1, 'showGoal' => 0, 'showComps' => 1],
196                ['kpiKey' => 'tradePercent', 'displayName' => 'Trade %', 'sortOrder' => 3, 'isVisible' => 1, 'showGoal' => 0, 'showComps' => 0],
197                ['kpiKey' => 'transactions', 'displayName' => 'Transactions', 'sortOrder' => 4, 'isVisible' => 1, 'showGoal' => 0, 'showComps' => 1],
198                ['kpiKey' => 'laborHours', 'displayName' => 'Labor Hours', 'sortOrder' => 5, 'isVisible' => 1, 'showGoal' => 0, 'showComps' => 0],
199                ['kpiKey' => 'laborPercent', 'displayName' => 'Labor %', 'sortOrder' => 6, 'isVisible' => 1, 'showGoal' => 0, 'showComps' => 0],
200                ['kpiKey' => 'salesPerLaborHour', 'displayName' => 'Sales/Labor Hr', 'sortOrder' => 7, 'isVisible' => 1, 'showGoal' => 0, 'showComps' => 0],
201                ['kpiKey' => 'buys', 'displayName' => 'Buys', 'sortOrder' => 8, 'isVisible' => 1, 'showGoal' => 1, 'showComps' => 1],
202                ['kpiKey' => 'totalWages', 'displayName' => 'Total Wages', 'sortOrder' => 9, 'isVisible' => 0, 'showGoal' => 0, 'showComps' => 0]
203            ];
204
205            foreach ($defaults as $default) {
206                $stmt->execute($default);
207            }
208
209            $this->storeDB->commit();
210            return true;
211        } catch (PDOException $e) {
212            $this->storeDB->rollBack();
213            error_log("KPIConfig: Error resetting to defaults - " . $e->getMessage());
214            return false;
215        }
216    }
217
218    /**
219     * Toggle visibility of a KPI
220     *
221     * @param string $kpiKey KPI key
222     * @return bool Success status
223     */
224    public function toggleVisibility(string $kpiKey): bool
225    {
226        try {
227            $stmt = $this->storeDB->prepare(
228                "UPDATE workbook_kpi_config
229                 SET isVisible = 1 - isVisible
230                 WHERE kpiKey = :kpiKey"
231            );
232            $stmt->execute([':kpiKey' => $kpiKey]);
233
234            return $stmt->rowCount() > 0;
235        } catch (PDOException $e) {
236            error_log("KPIConfig: Error toggling visibility for {$kpiKey} - " . $e->getMessage());
237            return false;
238        }
239    }
240}