Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
19.42% covered (danger)
19.42%
20 / 103
0.00% covered (danger)
0.00%
0 / 3
CRAP
n/a
0 / 0
_register_ticketbai_change_activity
31.37% covered (danger)
31.37%
16 / 51
0.00% covered (danger)
0.00%
0 / 1
110.41
_register_ticketbai_invceac_activity
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 1
240
register_ticketbai_activity
33.33% covered (danger)
33.33%
2 / 6
0.00% covered (danger)
0.00%
0 / 1
5.67
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        // obtain the place for the obtained change
40        if (success($retval)){
41            $place = null;
42
43            $results = [];
44            $res = $conx->tenant->query('TICKET', [ 'objid' => $change['ticket'] ], $results);
45            if ($res){
46                if (count($results) > 0){
47                    $place = $results[0]['place'];
48                }
49
50                if (!$place){   // no place for the ticket
51                    addlog(__FILE__, LogLevel::Error, "Cannot find place entry for change '".$change['objid']."'");
52                    $retval = ApplicationError::NotFound;
53                }
54            }
55            else {
56                $retval = ApplicationError::Database;
57            }
58        }
59
60        if (success($retval)){
61            $dstpath = AppConstants::RegLogPath();
62            if (!file_exists($dstpath) && !mkdir($dstpath, 0775)){
63                addlog(__FILE__, LogLevel::Error, "Could not create local registry log folder in '".$dstpath."'");
64                $error = error_get_last();
65                if ($error) addlog(__FILE__, LogLevel::Debug, $error['message']);
66            }
67
68            if (is_dir($dstpath)){
69                $dstfile = sprintf('%s/%05d_ticket_activity.csv', $dstpath, $place);
70
71                $lineinfo = [
72                    'session' => $session,
73                    'change' => 'UPDATE',
74                    'series' => $change['series'],
75                    'invceno' => $change['invoice'],
76                    'price' => $change['total'],
77                    'error' => $ticketbai['error']
78                ];
79
80                switch($change['reason']){
81                    case 'F':
82                        $lineinfo['change'] = 'INSERT';
83                        break;
84
85                    case 'C':
86                        $lineinfo['change'] = 'DELETE';
87                        break;
88                }
89
90                append_logline($dstfile, $conx->tenant->now(), $place, LogEvent::TicketBAI, $lineinfo);
91            }
92        }
93
94        return $retval;
95    }
96
97    /**
98     * @param \ConxHelper $conx
99     * @param string $session
100     * @param array $ticketbai
101     * @return int
102     */
103    function _register_ticketbai_invceac_activity(\ConxHelper $conx, $session, &$ticketbai){
104        $retval = ApplicationError::Success;
105        if ($conx->tenant === null) {
106            addlog(__FILE__, LogLevel::Error, "Tenant connection not available for _register_ticketbai_invceac_activity.");
107            $retval = ApplicationError::Database;
108        }
109
110        // obtain the change for the given ticketbai
111        if (success($retval)){
112            $invceac = null;
113
114            $results = [];
115            $res = $conx->tenant->query('ACCOUNTINVOICE', [ 'objid' => $ticketbai['invceac'] ], $results);
116            if ($res){
117                if (count($results) > 0){
118                    $invceac = $results[0];
119                }
120
121                if (!$invceac){   // no invoice account for the ticketbai
122                    addlog(__FILE__, LogLevel::Error, "Cannot find account invoice entry for ticketbai (account invoice objid [".$ticketbai['invceac']."])");
123                    $retval = ApplicationError::NotFound;
124                }
125            }
126            else {
127                $retval = ApplicationError::Database;
128            }
129        }
130
131        // obtain the place for the obtained invoice
132        if (success($retval)){
133            $place = null;
134
135            $results = [];
136            $res = $conx->tenant->query('PAYACCOUNT', [ 'objid' => $invceac['account'] ], $results);
137            if ($res){
138                if (count($results) > 0){
139                    $place = $results[0]['place'];
140                }
141
142                if (!$place){   // no place for the ticket
143                    addlog(__FILE__, LogLevel::Error, "Cannot find place entry for invoice account '".$invceac['objid']."'");
144                    $retval = ApplicationError::NotFound;
145                }
146            }
147            else {
148                $retval = ApplicationError::Database;
149            }
150        }
151
152        if (success($retval)){
153            $dstpath = AppConstants::RegLogPath();
154            if (!file_exists($dstpath) && !mkdir($dstpath, 0775)){
155                addlog(__FILE__, LogLevel::Error, "Could not create local registry log folder in '".$dstpath."'");
156                $error = error_get_last();
157                if ($error) addlog(__FILE__, LogLevel::Debug, $error['message']);
158            }
159
160            if (is_dir($dstpath)){
161                $dstfile = sprintf('%s/%05d_ticket_activity.csv', $dstpath, $place);
162
163                $lineinfo = [
164                    'session' => $session,
165                    'change' => 'INSERT',
166                    'series' => $invceac['series'],
167                    'invceno' => $invceac['invoice'],
168                    'price' => $invceac['amount'],
169                    'error' => $ticketbai['error'],
170                ];
171
172                append_logline($dstfile, $conx->tenant->now(), $place, LogEvent::TicketBAI, $lineinfo);
173            }
174        }
175
176        return $retval;
177    }
178
179    /**
180     * Register TicketBAI activity (change or account invoice).
181     *
182     * @param \ConxHelper $conx Connection helper (tenant must be set).
183     * @param string $session Session id.
184     * @param array $ticketbai Entry with 'tckchg' or 'invceac'.
185     * @return int ApplicationError code.
186     */
187    function register_ticketbai_activity(\ConxHelper $conx, $session, &$ticketbai){
188        if ($ticketbai['tckchg'] != null){
189            return _register_ticketbai_change_activity($conx, $session, $ticketbai);
190        }
191
192        if ($ticketbai['invceac'] != null){
193            return _register_ticketbai_invceac_activity($conx, $session, $ticketbai);
194        }
195
196        addlog(__FILE__, LogLevel::Error, "TicketBAI entry must provide either 'tckchg' or 'invceac' values");
197        return ApplicationError::Invalid;
198    }
199?>