Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
1.52% covered (danger)
1.52%
1 / 66
0.00% covered (danger)
0.00%
0 / 3
CRAP
n/a
0 / 0
_get_printer_device
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 1
240
register_drawer_activity
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
110
register_drawer_event
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2    include_once __DIR__."/registry.php";
3
4    /**
5     * Obtain printer device and place. Uses ConxHelper for tenant (PRINTER, RASPPI) and global (PLACE).
6     *
7     * @param \ConxHelper $conx Connection helper (tenant must be set for device's place).
8     * @param array|null $place Output: place row.
9     * @param mixed $device Input: printer objid; output: printer row.
10     * @return int ApplicationError code.
11     */
12    function _get_printer_device(\ConxHelper $conx, &$place, &$device){
13        $retval = ApplicationError::Success;
14        
15        $place = null;
16
17        // obtain the device instance out of its objid
18        if (success($retval)){
19            $results = [];
20            $conn = $conx->for('PRINTER');
21            $res = ($conn !== null) && $conn->query('PRINTER', [ 'status' => 'AC', 'objid' => $device ], $results);
22            if ($res){
23                if (count($results) > 0){
24                    $device = $results[0];
25                }
26                else {
27                    addlog(__FILE__, LogLevel::Error, "No active printer device found for device: '".$device."'");
28                    $retval = ApplicationError::NotFound;
29                }
30            }
31            else {
32                $retval = ApplicationError::Database;
33            }
34        }
35
36        // validate that the device's service is active
37        if (success($retval)){
38            $results = [];
39            $sql = "select * from RASPPI where objid=".$device['rasppi']." and status != 'DE'";
40            $conn = $conx->for('RASPPI');
41            $res = ($conn !== null) && $conn->runsql(['RASPPI'], $sql, $results);
42            if ($res){
43                if (count($results) === 0){
44                    addlog(__FILE__, LogLevel::Error, "Service '".$device['rasppi']."' for device: '".$device['objid']."' is not active");
45                    $retval = ApplicationError::NotFound;
46                }
47            }
48            else {
49                $retval = ApplicationError::Database;
50            }
51        }
52
53        // obtain place objid from tenant context
54        if (success($retval)){
55            $place = $conx->place();
56            if ($place === null){
57                addlog(__FILE__, LogLevel::Error, "No place context for the device: '".$device['objid']."'");
58                $retval = ApplicationError::NotFound;
59            }
60        }
61
62        // obtain the place row from global and validate status AC
63        if (success($retval)){
64            $results = [];
65            $res = $conx->global->query('PLACE', [ 'objid' => $place, 'status' => 'AC' ], $results);
66            if ($res){
67                if (count($results) > 0){
68                    $place = $results[0];
69                }
70                else {
71                    $place = null;
72                }
73
74                if (!$place){
75                    addlog(__FILE__, LogLevel::Error, "No active place found for the device: '".$device['objid']."'");
76                    $retval = ApplicationError::NotFound;
77                }
78            }
79            else {
80                $retval = ApplicationError::Database;
81            }
82        }
83
84        return $retval;
85    }
86
87    /**
88     * Register drawer activity. Requires ConxHelper with tenant set for the device's place.
89     *
90     * @param \ConxHelper $conx Connection helper.
91     * @param string $session Session id.
92     * @param mixed $device Printer objid (resolved to row by _get_printer_device).
93     * @param string|null $timestamp Optional timestamp; uses conx->tenant->now() if null.
94     * @return int ApplicationError code.
95     */
96    function register_drawer_activity(\ConxHelper $conx, $session, $device, $timestamp = null){
97        $retval = ApplicationError::Success;
98
99        // obtain the device related information
100        if (success($retval)){
101            $retval = _get_printer_device($conx, $place, $device);
102        }
103
104        if (success($retval)){
105            $dstpath = AppConstants::RegLogPath();
106            if (!file_exists($dstpath) && !mkdir($dstpath, 0775)){
107                addlog(__FILE__, LogLevel::Error, "Could not create local registry log folder in '".$dstpath."'");
108                $error = error_get_last();
109                if ($error) addlog(__FILE__, LogLevel::Debug, $error['message']);
110            }
111
112            if (is_dir($dstpath)){
113                $dstfile = sprintf('%s/%05d_ticket_activity.csv', $dstpath, $place['objid']);
114
115                $lineinfo = [
116                    'session' => $session,
117                    'drawer' => $device['name']
118                ];
119
120                if ($timestamp == null){
121                    $conn = $conx->tenant;
122                    $timestamp = ($conn !== null) ? $conn->now() : $conx->global->now();
123                }
124
125                $chainResult = append_logline($dstfile, $timestamp, $place['objid'], LogEvent::Drawer, $lineinfo);
126                if (!success($chainResult)){
127                    addlog(__FILE__, LogLevel::Critical, "Fiscal activity link not written for place '".$place['objid']."' (drawer) — write failure details are in an earlier log line.");
128                }
129            }
130        }
131
132        return $retval;
133    }
134
135    /**
136     * Register drawer event (from sync change).
137     *
138     * @param \ConxHelper $conx Connection helper.
139     * @param string $session Session id.
140     * @param array $event Event with 'printer', 'created'.
141     * @return int ApplicationError code.
142     */
143    function register_drawer_event(\ConxHelper $conx, $session, &$event){
144        $retval = ApplicationError::Success;
145
146        if (success($retval)){
147            $retval = register_drawer_activity($conx, $session, $event['printer'], $event['created']);
148        }
149
150        return $retval;
151    }
152?>