Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
45.26% covered (danger)
45.26%
43 / 95
0.00% covered (danger)
0.00%
0 / 3
CRAP
n/a
0 / 0
_register_ticketbai_change_activity
17.02% covered (danger)
17.02%
8 / 47
0.00% covered (danger)
0.00%
0 / 1
162.26
_register_ticketbai_invceac_activity
72.50% covered (warning)
72.50%
29 / 40
0.00% covered (danger)
0.00%
0 / 1
18.08
register_ticketbai_activity
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
3.33
1<?php
2    include_once __DIR__."/registry.php";
3    require_once __DIR__."/../legacy/ddbb/conx.php";
4
5    /**
6     * @param \ConxHelper $conx
7     * @param string $session
8     * @param array $ticketbai
9     * @return int
10     */
11    function _register_ticketbai_change_activity(\ConxHelper $conx, $session, &$ticketbai){
12        $retval = ApplicationError::Success;
13        if ($conx->tenant === null) {
14            addlog(__FILE__, LogLevel::Error, "Tenant connection not available for _register_ticketbai_change_activity.");
15            $retval = ApplicationError::Database;
16        }
17
18        // obtain the change for the given ticketbai
19        if (success($retval)){
20            $change = null;
21
22            $results = [];
23            $res = $conx->tenant->query('TICKETCHANGE', [ 'objid' => $ticketbai['tckchg'] ], $results);
24            if ($res){
25                if (count($results) > 0){
26                    $change = $results[0];
27                }
28
29                if (!$change){   // no change for the ticketbai
30                    addlog(__FILE__, LogLevel::Error, "Cannot find change entry for ticketbai (change objid [".$ticketbai['tckchg']."])");
31                    $retval = ApplicationError::NotFound;
32                }
33            }
34            else {
35                $retval = ApplicationError::Database;
36            }
37        }
38
39        // place comes from the tenant connection (one DB per place)
40        if (success($retval)){
41            $place = $conx->place();
42            if (!$place){
43                addlog(__FILE__, LogLevel::Error, "No place in connection for ticketbai change '".$change['objid']."'");
44                $retval = ApplicationError::NotFound;
45            }
46        }
47
48        if (success($retval)){
49            $dstpath = AppConstants::RegLogPath();
50            if (!file_exists($dstpath) && !mkdir($dstpath, 0775)){
51                addlog(__FILE__, LogLevel::Error, "Could not create local registry log folder in '".$dstpath."'");
52                $error = error_get_last();
53                if ($error) addlog(__FILE__, LogLevel::Debug, $error['message']);
54            }
55
56            if (is_dir($dstpath)){
57                $lineinfo = [
58                    'objid' => $ticketbai['objid'] ?? null,
59                    'session' => $session,
60                    'change' => 'UPDATE',
61                    'series' => $change['series'],
62                    'invceno' => $change['invoice'],
63                    'price' => $change['total'],
64                    'error' => $ticketbai['error']
65                ];
66
67                switch($change['reason']){
68                    case 'F':
69                        $lineinfo['change'] = 'INSERT';
70                        break;
71
72                    case 'C':
73                        $lineinfo['change'] = 'DELETE';
74                        break;
75                }
76
77                $chainResult = registry_append_guarded($conx, $place, LogEvent::TicketBAI, $conx->tenant->now(), $lineinfo);
78                if (!success($chainResult)){
79                    addlog(__FILE__, LogLevel::Critical, "Fiscal activity link not written for place '".$place."' (ticketbai change) — write failure details are in an earlier log line.");
80                }
81            }
82        }
83
84        return $retval;
85    }
86
87    /**
88     * @param \ConxHelper $conx
89     * @param string $session
90     * @param array $ticketbai
91     * @return int
92     */
93    function _register_ticketbai_invceac_activity(\ConxHelper $conx, $session, &$ticketbai){
94        $retval = ApplicationError::Success;
95        if ($conx->tenant === null) {
96            addlog(__FILE__, LogLevel::Error, "Tenant connection not available for _register_ticketbai_invceac_activity.");
97            $retval = ApplicationError::Database;
98        }
99
100        // obtain the change for the given ticketbai
101        if (success($retval)){
102            $invceac = null;
103
104            $results = [];
105            $res = $conx->tenant->query('ACCOUNTINVOICE', [ 'objid' => $ticketbai['invceac'] ], $results);
106            if ($res){
107                if (count($results) > 0){
108                    $invceac = $results[0];
109                }
110
111                if (!$invceac){   // no invoice account for the ticketbai
112                    addlog(__FILE__, LogLevel::Error, "Cannot find account invoice entry for ticketbai (account invoice objid [".$ticketbai['invceac']."])");
113                    $retval = ApplicationError::NotFound;
114                }
115            }
116            else {
117                $retval = ApplicationError::Database;
118            }
119        }
120
121        // place comes from the tenant connection (one DB per place)
122        if (success($retval)){
123            $place = $conx->place();
124            if (!$place){
125                addlog(__FILE__, LogLevel::Error, "No place in connection for ticketbai invoice '".$invceac['objid']."'");
126                $retval = ApplicationError::NotFound;
127            }
128        }
129
130        if (success($retval)){
131            $dstpath = AppConstants::RegLogPath();
132            if (!file_exists($dstpath) && !mkdir($dstpath, 0775)){
133                addlog(__FILE__, LogLevel::Error, "Could not create local registry log folder in '".$dstpath."'");
134                $error = error_get_last();
135                if ($error) addlog(__FILE__, LogLevel::Debug, $error['message']);
136            }
137
138            if (is_dir($dstpath)){
139                $lineinfo = [
140                    'objid' => $ticketbai['objid'] ?? null,
141                    'session' => $session,
142                    'change' => 'INSERT',
143                    'series' => $invceac['series'],
144                    'invceno' => $invceac['invoice'],
145                    'price' => $invceac['amount'],
146                    'error' => $ticketbai['error'],
147                ];
148
149                $chainResult = registry_append_guarded($conx, $place, LogEvent::TicketBAI, $conx->tenant->now(), $lineinfo);
150                if (!success($chainResult)){
151                    addlog(__FILE__, LogLevel::Critical, "Fiscal activity link not written for place '".$place."' (ticketbai invceac) — write failure details are in an earlier log line.");
152                }
153            }
154        }
155
156        return $retval;
157    }
158
159    /**
160     * Register TicketBAI activity (change or account invoice).
161     *
162     * @param \ConxHelper $conx Connection helper (tenant must be set).
163     * @param string $session Session id.
164     * @param array $ticketbai Entry with 'tckchg' or 'invceac'.
165     * @return int ApplicationError code.
166     */
167    function register_ticketbai_activity(\ConxHelper $conx, $session, &$ticketbai){
168        if (($ticketbai['tckchg'] ?? null) != null){
169            return _register_ticketbai_change_activity($conx, $session, $ticketbai);
170        }
171
172        if (($ticketbai['invceac'] ?? null) != null){
173            return _register_ticketbai_invceac_activity($conx, $session, $ticketbai);
174        }
175
176        addlog(__FILE__, LogLevel::Error, "TicketBAI entry must provide either 'tckchg' or 'invceac' values");
177        return ApplicationError::Invalid;
178    }
179?>