Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 121 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| SalesReport | |
0.00% |
0 / 121 |
|
0.00% |
0 / 4 |
2162 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| createFromDataArray | |
0.00% |
0 / 63 |
|
0.00% |
0 / 1 |
1122 | |||
| getLiveFinancials | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
| insert | |
0.00% |
0 / 45 |
|
0.00% |
0 / 1 |
90 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BuyerKiosk\DailyReport; |
| 4 | |
| 5 | class SalesReport extends BaseReport { |
| 6 | public $date; |
| 7 | public $timeSubmitted; |
| 8 | |
| 9 | public $grossSalesRetail; |
| 10 | public $grossSalesCost; |
| 11 | public $grossSalesNumber; |
| 12 | public $grossSalesGM; |
| 13 | |
| 14 | public $returnsCost; |
| 15 | public $returnsRetail; |
| 16 | public $returnsNumber; |
| 17 | public $returnsGM; |
| 18 | |
| 19 | public $netSalesConinuingForFee; |
| 20 | public $continuingFee; |
| 21 | |
| 22 | public $netSalesCost; |
| 23 | public $netSalesRetail; |
| 24 | public $netSalesGM; |
| 25 | |
| 26 | public $buysCost; |
| 27 | public $buysRetail; |
| 28 | public $buysGM; |
| 29 | public $buysCount; |
| 30 | |
| 31 | public $averageRetail; |
| 32 | public $salesCount; |
| 33 | public $tradeCount; |
| 34 | public $tradeAmount; |
| 35 | public $laborPercentage; |
| 36 | |
| 37 | public $cashPaidIn; |
| 38 | public $cashPaidOut; |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | public function __construct($typeNum) |
| 44 | { |
| 45 | parent::__construct($typeNum); |
| 46 | } |
| 47 | |
| 48 | public function createFromDataArray($data) { |
| 49 | // Validate data structure |
| 50 | if (!isset($data['DAILYRESULTS']) || !isset($data['DAILYRESULTS'][0]) || !isset($data['DAILYRESULTS'][0]['Date'])) { |
| 51 | $this->date = date('Y-m-d'); |
| 52 | $this->log->LogDebug("DAILYRESULTS Date not found, using current date: " . $this->date); |
| 53 | } else { |
| 54 | $rawDate = $data['DAILYRESULTS'][0]['Date']; |
| 55 | // Convert date format from M/d/Y to Y-m-d |
| 56 | $dateTime = \DateTime::createFromFormat('m/d/Y', $rawDate); |
| 57 | if ($dateTime === false) { |
| 58 | // Try another common format |
| 59 | $dateTime = \DateTime::createFromFormat('n/j/Y', $rawDate); |
| 60 | } |
| 61 | if ($dateTime === false) { |
| 62 | // If still can't parse, try Y-m-d format (already correct) |
| 63 | $dateTime = \DateTime::createFromFormat('Y-m-d', $rawDate); |
| 64 | } |
| 65 | if ($dateTime === false) { |
| 66 | // Last resort - use current date |
| 67 | $this->date = date('Y-m-d'); |
| 68 | $this->log->LogDebug("Could not parse date format: " . $rawDate . ", using current date: " . $this->date); |
| 69 | } else { |
| 70 | $this->date = $dateTime->format('Y-m-d'); |
| 71 | $this->log->LogDebug("Converted date from " . $rawDate . " to " . $this->date); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // Check if HISTORY data exists |
| 76 | if (!isset($data['HISTORY']) || !isset($data['HISTORY'][0])) { |
| 77 | $this->log->LogDebug("HISTORY data not found, using default values for all fields"); |
| 78 | // Set all fields to default values |
| 79 | $this->grossSalesRetail = 0; |
| 80 | $this->grossSalesCost = 0; |
| 81 | $this->grossSalesNumber = 0; |
| 82 | $this->grossSalesGM = 0; |
| 83 | $this->returnsCost = 0; |
| 84 | $this->returnsRetail = 0; |
| 85 | $this->returnsNumber = 0; |
| 86 | $this->returnsGM = 0; |
| 87 | $this->netSalesConinuingForFee = 0; |
| 88 | $this->continuingFee = 0; |
| 89 | $this->netSalesCost = 0; |
| 90 | $this->netSalesRetail = 0; |
| 91 | $this->netSalesGM = 0; |
| 92 | $this->buysCost = 0; |
| 93 | $this->buysRetail = 0; |
| 94 | $this->buysGM = 0; |
| 95 | $this->buysCount = 0; |
| 96 | $this->averageRetail = 0; |
| 97 | $this->salesCount = 0; |
| 98 | $this->tradeCount = 0; |
| 99 | $this->tradeAmount = 0; |
| 100 | $this->cashPaidIn = 0; |
| 101 | $this->cashPaidOut = 0; |
| 102 | } else { |
| 103 | // Use existing logic with isset checks |
| 104 | $this->grossSalesRetail = isset($data['HISTORY'][0]['grossSalesRetail']) ? (float)$data['HISTORY'][0]['grossSalesRetail'] : 0; |
| 105 | $this->grossSalesCost = isset($data['HISTORY'][0]['grossSalesCost']) ? (float)$data['HISTORY'][0]['grossSalesCost'] : 0; |
| 106 | $this->grossSalesNumber = isset($data['HISTORY'][0]['grossSalesNumber']) ? (int)$data['HISTORY'][0]['grossSalesNumber'] : 0; |
| 107 | $this->grossSalesGM = isset($data['HISTORY'][0]['grossSalesGM']) ? (float)$data['HISTORY'][0]['grossSalesGM'] : 0; |
| 108 | $this->returnsCost = isset($data['HISTORY'][0]['returnsCost']) ? (float)$data['HISTORY'][0]['returnsCost'] : 0; |
| 109 | $this->returnsRetail = isset($data['HISTORY'][0]['returnsRetail']) ? (float)$data['HISTORY'][0]['returnsRetail'] : 0; |
| 110 | $this->returnsNumber = isset($data['HISTORY'][0]['returnsNumber']) ? (int)$data['HISTORY'][0]['returnsNumber'] : 0; |
| 111 | $this->returnsGM = isset($data['HISTORY'][0]['returnsGM']) ? (float)$data['HISTORY'][0]['returnsGM'] : 0; |
| 112 | $this->netSalesConinuingForFee = isset($data['HISTORY'][0]['netSalesContinuiingForFee']) ? (float)$data['HISTORY'][0]['netSalesContinuiingForFee'] : 0; |
| 113 | $this->continuingFee = isset($data['HISTORY'][0]['continuingFee']) ? (float)$data['HISTORY'][0]['continuingFee'] : 0; |
| 114 | $this->netSalesCost = isset($data['HISTORY'][0]['netSalesCost']) ? (float)$data['HISTORY'][0]['netSalesCost'] : 0; |
| 115 | $this->netSalesRetail = isset($data['HISTORY'][0]['netSalesRetail']) ? (float)$data['HISTORY'][0]['netSalesRetail'] : 0; |
| 116 | $this->netSalesGM = isset($data['HISTORY'][0]['netSalesGM']) ? (float)$data['HISTORY'][0]['netSalesGM'] : 0; |
| 117 | $this->buysCost = isset($data['HISTORY'][0]['buysCost']) ? (float)$data['HISTORY'][0]['buysCost'] : 0; |
| 118 | $this->buysRetail = isset($data['HISTORY'][0]['buysRetail']) ? (float)$data['HISTORY'][0]['buysRetail'] : 0; |
| 119 | $this->buysGM = isset($data['HISTORY'][0]['buysGM']) ? (float)$data['HISTORY'][0]['buysGM'] : 0; |
| 120 | $this->buysCount = isset($data['HISTORY'][0]['buysCount']) ? (int)$data['HISTORY'][0]['buysCount'] : 0; |
| 121 | $this->averageRetail = isset($data['HISTORY'][0]['averageRetail']) ? (float)$data['HISTORY'][0]['averageRetail'] : 0; |
| 122 | $this->salesCount = isset($data['HISTORY'][0]['salesCount']) ? (int)$data['HISTORY'][0]['salesCount'] : 0; |
| 123 | $this->tradeCount = isset($data['HISTORY'][0]['tradeCount']) ? (int)$data['HISTORY'][0]['tradeCount'] : 0; |
| 124 | $this->tradeAmount = isset($data['HISTORY'][0]['tradeAmount']) ? (float)$data['HISTORY'][0]['tradeAmount'] : 0; |
| 125 | $this->cashPaidIn = isset($data['HISTORY'][0]['cashPaidIn']) ? (float)$data['HISTORY'][0]['cashPaidIn'] : 0; |
| 126 | $this->cashPaidOut = isset($data['HISTORY'][0]['cashPaidOut']) ? (float)$data['HISTORY'][0]['cashPaidOut'] : 0; |
| 127 | } |
| 128 | |
| 129 | // Handle laborPercentage separately as it comes from DAILYRESULTS |
| 130 | $this->laborPercentage = isset($data['DAILYRESULTS'][0]['laborPercentage']) ? (float)$data['DAILYRESULTS'][0]['laborPercentage'] : 0; |
| 131 | } |
| 132 | public function getLiveFinancials() { |
| 133 | if(isset($this->date)) { |
| 134 | try { |
| 135 | $this->log->LogDebug("Getting live financials for date: ".$this->date); |
| 136 | $stmt = $this->storeDB->prepare("SELECT * FROM LiveFinancials WHERE date = :date"); |
| 137 | $stmt->bindValue(":date", $this->date); |
| 138 | $stmt->execute(); |
| 139 | return $stmt->fetch(\PDO::FETCH_ASSOC); |
| 140 | } catch (\PDOException $e) { |
| 141 | error_log("ERROR: SalesReport::getLiveFinancials - PDO Exception: " . $e->getMessage() . " for date: " . $this->date); |
| 142 | $this->log->LogError("Error getting live financials for date: ".$this->date); |
| 143 | $this->log->LogError($e->getMessage()); |
| 144 | } |
| 145 | } |
| 146 | $this->log->LogError("Date not set for live financials"); |
| 147 | return null; |
| 148 | } |
| 149 | |
| 150 | public function insert() { |
| 151 | $this->storeDB->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
| 152 | |
| 153 | $lf = $this->getLiveFinancials(); |
| 154 | if($lf !== false && $lf !== null && is_array($lf)) { |
| 155 | $buysGoal = isset($lf['buysGoal']) ? $lf['buysGoal'] : 0; |
| 156 | $salesGoal = isset($lf['salesGoal']) ? $lf['salesGoal'] : 0; |
| 157 | $buysOutstanding = isset($lf['buysOutstanding']) ? $lf['buysOutstanding'] : 0; |
| 158 | } else { |
| 159 | $buysGoal = 0; |
| 160 | $salesGoal = 0; |
| 161 | $buysOutstanding = 0; |
| 162 | } |
| 163 | try { |
| 164 | $stmt = $this->storeDB->prepare("INSERT INTO closeSalesReport |
| 165 | (`date`, timeSubmitted, grossSalesRetail, grossSalesCost, grossSalesNumber, grossSalesGM, returnsCost, returnsRetail, |
| 166 | returnsNumber, returnsGM, netSalesContinuingForFee, continuingFee, netSalesCost, netSalesRetail, netSalesGM, salesCount, |
| 167 | buysCost, buysRetail, buysGM, buysCount, averageRetail, tradeCount, tradeAmount, buysGoal, salesGoal, buysOutstanding, laborPercentage, cashPaidIn, cashPaidOut) VALUES |
| 168 | (:date, CURRENT_TIMESTAMP, :grossSalesRetail, :grossSalesCost, :grossSalesNumber, :grossSalesGM, :returnsCost, :returnsRetail, :returnsNumber, :returnsGM, |
| 169 | :netSalesContinuingForFee, :continuingFee, :netSalesCost, :netSalesRetail, :netSalesGM, :salesCount, :buysCost, :buysRetail, :buysGM, :buysCount, :averageRetail, :tradeCount, :tradeAmount, |
| 170 | :buysGoal, :salesGoal, :buysOutstanding, :laborPercentage, :cashPaidIn, :cashPaidOut)"); |
| 171 | $stmt->bindValue(":date",$this->date); |
| 172 | $stmt->bindValue(":grossSalesRetail",$this->grossSalesRetail); |
| 173 | $stmt->bindValue(":grossSalesCost",$this->grossSalesCost); |
| 174 | $stmt->bindValue(":grossSalesNumber",$this->grossSalesNumber); |
| 175 | $stmt->bindValue(":grossSalesGM",$this->grossSalesGM); |
| 176 | $stmt->bindValue(":returnsCost",$this->returnsCost); |
| 177 | $stmt->bindValue(":returnsRetail",$this->returnsRetail); |
| 178 | $stmt->bindValue(":returnsNumber",$this->returnsNumber); |
| 179 | $stmt->bindValue(":returnsGM",$this->returnsGM); |
| 180 | $stmt->bindValue(":netSalesContinuingForFee",$this->netSalesConinuingForFee); |
| 181 | $stmt->bindValue(":continuingFee",$this->continuingFee); |
| 182 | $stmt->bindValue(":netSalesCost",$this->netSalesCost); |
| 183 | $stmt->bindValue(":netSalesRetail",$this->netSalesRetail); |
| 184 | $stmt->bindValue(":netSalesGM",$this->netSalesGM); |
| 185 | $stmt->bindValue(":buysCost",$this->buysCost); |
| 186 | $stmt->bindValue(":buysRetail",$this->buysRetail); |
| 187 | $stmt->bindValue(":buysGM",$this->buysGM); |
| 188 | $stmt->bindValue(":buysCount",$this->buysCount); |
| 189 | $stmt->bindValue(":averageRetail",$this->averageRetail); |
| 190 | $stmt->bindValue(":salesCount",$this->salesCount); |
| 191 | $stmt->bindValue(":tradeCount",$this->tradeCount); |
| 192 | $stmt->bindValue(":tradeAmount",$this->tradeAmount); |
| 193 | $stmt->bindValue(":buysGoal", $buysGoal); |
| 194 | $stmt->bindValue(":salesGoal", $salesGoal); |
| 195 | $stmt->bindValue(":buysOutstanding", $buysOutstanding); |
| 196 | $stmt->bindValue(":laborPercentage",$this->laborPercentage); |
| 197 | $stmt->bindValue(":cashPaidIn",$this->cashPaidIn); |
| 198 | $stmt->bindValue(":cashPaidOut", $this->cashPaidOut); |
| 199 | if($stmt->execute()) { |
| 200 | return true; |
| 201 | } |
| 202 | $this->log->LogError($stmt->errorInfo()); |
| 203 | } catch (\PDOException $e) { |
| 204 | $this->log->LogError($e->getMessage()); |
| 205 | } |
| 206 | return false; |
| 207 | } |
| 208 | } |