Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Invoice
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 6
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getInvoicesArray
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 parseFileName
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getInvoiceDirectory
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 downloadInvoiceFile
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 scan_dir_by_date
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace BuyerKiosk\Billing;
4class Invoice extends \Store {
5
6    public function __construct($store) {
7        parent::__construct($store);
8    }
9
10    public function getInvoicesArray() {
11        $invoices = array();
12        $dir = $this->getInvoiceDirectory();
13        $files = $this->scan_dir_by_date($dir);
14        foreach ($files as $file) {
15            if ($file != "." && $file != "..") {
16                //parse the file name and it to the array by the year
17                $invoice = $this->parseFileName($file);
18                $invoices[$invoice['year']][] = $invoice;
19            }
20        }
21        return $invoices;
22    }
23    public function parseFileName($fileName) {
24        //file name will come in as 202311165.pdf or 20231120938.pdf  where the first 4 digits are the year, the next 2 are the month, the next 2 are the day, and the last 3 are the store number. Store number can be between 3 and 6 digitls long at the end. Seperate those out.
25        $year = substr($fileName, 0, 4);
26        $month = substr($fileName, 4, 2);
27        $date = new \DateTime($year."-".$month."-01");
28        //Date string format should be like "January 2023"
29        $dateString = $this->getTypeNum()." - ".$date->format("F Y");
30        return array("year" => $year, "month" => $month, "storeNum" => $this->getTypeNum(), "fileName" => $fileName, "dateString" => $dateString);
31
32    }
33    public function getInvoiceDirectory() {
34        return rtrim($_ENV['HOME_DIR'], '/')."/invoices/".$this->getTypeNum();
35    }
36    public function downloadInvoiceFile($fileName) {
37        $file = $this->getInvoiceDirectory()."/".$fileName;
38        if (file_exists($file)) {
39            header('Content-Description: File Transfer');
40            header('Content-Type: application/pdf');
41            header('Content-Disposition: attachment; filename="'.$fileName.'"');
42            header('Expires: 0');
43            header('Cache-Control: must-revalidate');
44            header('Pragma: public');
45            header('Content-Length: '.filesize($file));
46            readfile($file);
47            exit;
48        }
49    }
50    function scan_dir_by_date($dir) {
51        $ignored = array('.', '..', '.svn', '.htaccess');
52
53        $files = array();
54        
55        // Check if directory exists before scanning
56        if (!is_dir($dir)) {
57            error_log("Directory does not exist: " . $dir);
58            return $files; // Return empty array
59        }
60        
61        foreach (scandir($dir) as $file) {
62            if (in_array($file, $ignored)) continue;
63            $files[$file] = filemtime($dir . '/' . $file);
64        }
65
66        arsort($files);
67        $files = array_keys($files);
68
69        return $files;
70    }
71
72}