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