Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.99% covered (warning)
78.99%
94 / 119
0.00% covered (danger)
0.00%
0 / 1
CRAP
n/a
0 / 0
get_ticket_list_action
94.44% covered (success)
94.44%
34 / 36
0.00% covered (danger)
0.00%
0 / 1
4.00
1<?php
2
3/**
4 * @file
5 * Place action: ticket list for date range.
6 */
7
8declare(strict_types=1);
9
10require_once __DIR__.'/../../Legacy/common/session.php';
11
12/**
13 * Load tickets from place DB. SESSION (central) is not joined; caller must merge _device from central.
14 *
15 * @param \ddbb $conx_tenant
16 */
17function get_ticket_list_action($conx_tenant, $place, $ini, $end, $updated, &$tickets): int
18{
19    $retval = \ApplicationError::Success;
20    if (\success($retval)) {
21        $sql = "select TICKET.*, ";
22        $sql .= "LASTCHANGE.objid as _change, ";
23        $sql .= "TICKETBAI.valid as _bai, ";
24        $sql .= "IVBUSINESS.client as _ivclient, ";
25        $sql .= "ACBUSINESS.client as _acclient, ";
26        $sql .= "TICKETAUDIT.audit as _intill ";
27        $sql .= "from TICKET ";
28        $sql .= "left join (";
29        $sql .= "  select TICKETCHANGE.ticket, MAX(TICKETCHANGE.objid) as objid from TICKETCHANGE group by TICKETCHANGE.ticket";
30        $sql .= ") LASTCHANGE on LASTCHANGE.ticket = TICKET.objid";
31        $sql .= " left join TICKETBAI on TICKETBAI.tckchg = LASTCHANGE.objid ";
32        $sql .= "left join (";
33        $sql .= "  select TICKETINVOICE.ticket as ticket, TICKETINVOICE.client as client";
34        $sql .= "  from TICKETINVOICE ";
35        $sql .= "  inner join (";
36        $sql .= "    select ticket, MAX(objid) as maxid from TICKETINVOICE group by ticket ";
37        $sql .= "  ) LASTINVOICE on TICKETINVOICE.objid = LASTINVOICE.maxid ";
38        $sql .= "  where TICKETINVOICE.status = 'AC'";
39        $sql .= ") IVBUSINESS on IVBUSINESS.ticket = TICKET.objid ";
40        $sql .= "left join (";
41        $sql .= "  select ACCOUNTINVOICE.objid as account, PAYACCOUNT.client as client ";
42        $sql .= "  from ACCOUNTINVOICE inner join PAYACCOUNT on ACCOUNTINVOICE.account = PAYACCOUNT.objid ";
43        $sql .= ") ACBUSINESS on ACBUSINESS.account = TICKET.invceac ";
44        $sql .= "left join TICKETAUDIT on TICKETAUDIT.ticket = TICKET.objid ";
45        $sql .= "where TICKET.status not in ('PA') ";
46        if ($updated) {
47            $sql .= "and (TICKET.updated between '".$ini."' and '".$end."')";
48        } else {
49            $sql .= "and (TICKET.created between '".$ini."' and '".$end."')";
50        }
51        $tables = ['TICKET', 'TICKETINVOICE', 'ACCOUNTINVOICE', 'PAYACCOUNT', 'TICKETCHANGE', 'TICKETBAI', 'TICKETAUDIT'];
52        $res = $conx_tenant->runsql($tables, $sql, $tickets);
53        if ($res === false) {
54            \addlog(__FILE__, \LogLevel::Error, "Ticket list query failed for place ".$place);
55            $retval = \ApplicationError::Database;
56        }
57    }
58    return $retval;
59}
60
61/**
62 * Action callable: get ticket list for place and date range.
63 *
64 * @param string $body Unused.
65 * @param array<string, mixed> $query Query params: device, session, place, dateini, dateend, timestamp, updated.
66 * @param \ConxHelper $conx Connection helper.
67 * @param \Psr\Log\LoggerInterface $logger Logger.
68 * @param \UppServices\SessionService $sessionService Session service for check.
69 * @return array{output: string, contentType: string}
70 */
71return function (string $body, array $query, \ConxHelper $conx, \Psr\Log\LoggerInterface $logger, \UppServices\SessionService $sessionService): array {
72    $retval = \ApplicationError::Success;
73    $place = null;
74    $now = null;
75
76    $device = $query['device'] ?? null;
77    if ($device === null || $device === '') {
78        $logger->error("No device provided on URL. Operation cancelled.");
79        $retval = \ApplicationError::Parameters;
80    }
81
82    if (\success($retval)) {
83        $session = $query['session'] ?? null;
84        if ($session === null || $session === '') {
85            $logger->error("No session provided on URL. Operation cancelled.");
86            $retval = \ApplicationError::Parameters;
87        } else {
88            $unused = null;
89            $sessionObjid = null;
90            $retval = $sessionService->checkSession($conx, (string) $session, (string) $device, $unused, $sessionObjid);
91        }
92    }
93
94    if (\success($retval)) {
95        $place = $query['place'] ?? null;
96        if ($place === null || $place === '') {
97            $logger->error("Mandatory argument 'place' not provided in url parameters.");
98            $retval = \ApplicationError::Parameters;
99        }
100    }
101
102    if (\success($retval)) {
103        $conx->tenant = $place;
104    }
105
106    if (\success($retval) && $conx->tenant === null) {
107        $logger->error("Connection to place database failed.");
108        $retval = \ApplicationError::Database;
109    }
110
111    if (\success($retval)) {
112        $now = $conx->tenant->now(-60);
113    }
114
115    $ini = null;
116    $end = null;
117    $timestamp = null;
118    $updated = $query['updated'] ?? null;
119    if (\success($retval)) {
120        $ini = $query['dateini'] ?? null;
121        if ($ini === null) {
122            $logger->error("Mandatory argument 'dateini' not provided in url parameters.");
123            $retval = \ApplicationError::Parameters;
124        } else {
125            $ini = date("Y-m-d H:i:s", (int) $ini);
126        }
127    }
128    if (\success($retval)) {
129        $end = $query['dateend'] ?? null;
130        if ($end === null) {
131            $logger->error("Mandatory argument 'dateend' not provided in url parameters.");
132            $retval = \ApplicationError::Parameters;
133        } else {
134            $end = date("Y-m-d H:i:s", (int) $end);
135        }
136    }
137    if (\success($retval)) {
138        $timestamp = $query['timestamp'] ?? null;
139        if ($timestamp === null) {
140            $logger->error("Mandatory argument 'timestamp' not provided in url parameters.");
141            $retval = \ApplicationError::Parameters;
142        }
143    }
144
145    $tickets = [];
146    if (\success($retval)) {
147        $tsDatetime = is_numeric($timestamp) ? date("Y-m-d H:i:s", (int) $timestamp) : (string) $timestamp;
148        $startRange = ($tsDatetime > $ini) ? $tsDatetime : $ini;
149        $retval = get_ticket_list_action($conx->tenant, $place, $startRange, $end, $updated, $tickets);
150    }
151
152    if (\success($retval) && $conx->global !== null && count($tickets) > 0) {
153        $sessionObjids = array_unique(array_filter(array_column($tickets, 'session')));
154        if (count($sessionObjids) > 0) {
155            $deviceMap = [];
156            $results = [];
157            $res = $conx->global->query('SESSION', ['objid' => $sessionObjids], $results);
158            if ($res) {
159                foreach ($results as $row) {
160                    $deviceMap[$row['objid']] = $row['deviceid'] ?? null;
161                }
162            }
163            foreach ($tickets as &$ticket) {
164                $ticket['_device'] = $deviceMap[$ticket['session']] ?? null;
165            }
166            unset($ticket);
167        }
168    }
169
170    if (\success($retval)) {
171        foreach ($tickets as &$ticket) {
172            $ticket['client'] = $ticket['_ivclient'] ?? $ticket['_acclient'] ?? null;
173            $ticket['sentto'] = $ticket['_bai'] ? '1' : '0';
174            if (!empty($ticket['_device'])) {
175                $ticket['device'] = $ticket['_device'];
176            }
177            if (!empty($ticket['_intill'])) {
178                $ticket['intill'] = $ticket['_intill'];
179            }
180        }
181        unset($ticket);
182    }
183
184    $result = [
185        "errorcode" => $retval,
186        "tickets" => \success($retval) ? $tickets : null,
187        "timestamp" => \success($retval) ? $now : null,
188    ];
189    return ['output' => json_encode($result), 'contentType' => 'application/json; charset=utf-8'];
190};