Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.61% covered (success)
88.61%
70 / 79
0.00% covered (danger)
0.00%
0 / 1
CRAP
n/a
0 / 0
get_invoice_list_action
85.71% covered (success)
85.71%
12 / 14
0.00% covered (danger)
0.00%
0 / 1
3.03
1<?php
2
3/**
4 * @file
5 * Place action: invoice list for date range.
6 */
7
8declare(strict_types=1);
9
10require_once __DIR__.'/../../Legacy/common/session.php';
11
12/**
13 * @param \ConxHelper $conx
14 */
15function get_invoice_list_action(\ConxHelper $conx, $place, $ini, $end, &$invoices): int
16{
17    $retval = \ApplicationError::Success;
18    if (\success($retval)) {
19        $sql = "select ACCOUNTINVOICE.*, ";
20        $sql .= "TICKETBAI.valid as _bai ";
21        $sql .= "from ACCOUNTINVOICE ";
22        $sql .= "inner join PAYACCOUNT on ACCOUNTINVOICE.account = PAYACCOUNT.objid ";
23        $sql .= " left join TICKETBAI on TICKETBAI.invceac = ACCOUNTINVOICE.objid ";
24        $sql .= "where (ACCOUNTINVOICE.created between '".$ini."' and '".$end."')";
25        $tables = ['ACCOUNTINVOICE', 'PAYACCOUNT', 'TICKETBAI'];
26        $res = $conx->tenant->runsql($tables, $sql, $invoices);
27        if ($res === false) {
28            \addlog(__FILE__, \LogLevel::Error, "Invoice list query failed for place ".$place." dates ".$ini." - ".$end);
29            $retval = \ApplicationError::Database;
30        }
31    }
32    return $retval;
33}
34
35/**
36 * Action callable: get invoice list for place and date range.
37 *
38 * @param string $body Unused.
39 * @param array<string, mixed> $query Query params: device, session, place, dateini, dateend, timestamp.
40 * @param \ConxHelper $conx Connection helper.
41 * @param \Psr\Log\LoggerInterface $logger Logger.
42 * @param \UppServices\SessionService $sessionService Session service for check.
43 * @return array{output: string, contentType: string}
44 */
45return function (string $body, array $query, \ConxHelper $conx, \Psr\Log\LoggerInterface $logger, \UppServices\SessionService $sessionService): array {
46    $retval = \ApplicationError::Success;
47    $place = null;
48    $now = null;
49
50    $device = $query['device'] ?? null;
51    if ($device === null || $device === '') {
52        $logger->error("No device provided on URL. Operation cancelled.");
53        $retval = \ApplicationError::Parameters;
54    }
55
56    if (\success($retval)) {
57        $session = $query['session'] ?? null;
58        if ($session === null || $session === '') {
59            $logger->error("No session provided on URL. Operation cancelled.");
60            $retval = \ApplicationError::Parameters;
61        } else {
62            $unused = null;
63            $sessionObjid = null;
64            $retval = $sessionService->checkSession($conx, (string) $session, (string) $device, $unused, $sessionObjid);
65        }
66    }
67
68    if (\success($retval)) {
69        $place = $query['place'] ?? null;
70        if ($place === null || $place === '') {
71            $logger->error("Mandatory argument 'place' not provided in url parameters.");
72            $retval = \ApplicationError::Parameters;
73        }
74    }
75
76    if (\success($retval)) {
77        $conx->tenant = $place;
78    }
79
80    if (\success($retval) && $conx->tenant === null) {
81        $logger->error("Connection to place database failed.");
82        $retval = \ApplicationError::Database;
83    }
84
85    if (\success($retval)) {
86        $now = $conx->tenant->now(-60);
87    }
88
89    $ini = null;
90    $end = null;
91    $timestamp = null;
92    if (\success($retval)) {
93        $ini = $query['dateini'] ?? null;
94        if ($ini === null) {
95            $logger->error("Mandatory argument 'dateini' not provided in url parameters.");
96            $retval = \ApplicationError::Parameters;
97        } else {
98            $ini = date("Y-m-d H:i:s", (int) $ini);
99        }
100    }
101    if (\success($retval)) {
102        $end = $query['dateend'] ?? null;
103        if ($end === null) {
104            $logger->error("Mandatory argument 'dateend' not provided in url parameters.");
105            $retval = \ApplicationError::Parameters;
106        } else {
107            $end = date("Y-m-d H:i:s", (int) $end);
108        }
109    }
110    if (\success($retval)) {
111        $timestamp = $query['timestamp'] ?? null;
112        if ($timestamp === null) {
113            $logger->error("Mandatory argument 'timestamp' not provided in url parameters.");
114            $retval = \ApplicationError::Parameters;
115        }
116    }
117
118    $invces = [];
119    if (\success($retval)) {
120        $tsDatetime = is_numeric($timestamp) ? date("Y-m-d H:i:s", (int) $timestamp) : (string) $timestamp;
121        $startRange = ($tsDatetime > $ini) ? $tsDatetime : $ini;
122        $retval = get_invoice_list_action($conx, $place, $startRange, $end, $invces);
123    }
124
125    if (\success($retval)) {
126        foreach ($invces as &$invoice) {
127            $invoice['sentto'] = $invoice['_bai'] ? '1' : '0';
128        }
129        unset($invoice);
130    }
131
132    $result = [
133        "errorcode" => $retval,
134        "invoices" => \success($retval) ? $invces : null,
135        "timestamp" => \success($retval) ? $now : null,
136    ];
137    return ['output' => json_encode($result), 'contentType' => 'application/json; charset=utf-8'];
138};