Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
38.95% covered (danger)
38.95%
37 / 95
0.00% covered (danger)
0.00%
0 / 3
CRAP
n/a
0 / 0
_register_verifactu_change_activity
70.21% covered (warning)
70.21%
33 / 47
0.00% covered (danger)
0.00%
0 / 1
22.77
_register_verifactu_invceac_activity
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 1
210
register_verifactu_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    function _register_verifactu_change_activity(\ConxHelper $conx, $session, &$verifactu){
6        $retval = ApplicationError::Success;
7        if ($conx->tenant === null) {
8            addlog(__FILE__, LogLevel::Error, "Tenant connection not available for _register_verifactu_change_activity.");
9            $retval = ApplicationError::Database;
10        }
11
12        if (success($retval)){
13            $change = null;
14            $results = [];
15            $res = $conx->tenant->query('TICKETCHANGE', [ 'objid' => $verifactu['tckchg'] ], $results);
16            if ($res){
17                if (count($results) > 0){
18                    $change = $results[0];
19                }
20                if (!$change){
21                    addlog(__FILE__, LogLevel::Error, "Cannot find change entry for verifactu (change objid [".$verifactu['tckchg']."])");
22                    $retval = ApplicationError::NotFound;
23                }
24            }
25            else {
26                $retval = ApplicationError::Database;
27            }
28        }
29
30        if (success($retval)){
31            $place = $conx->place();
32            if (!$place){
33                addlog(__FILE__, LogLevel::Error, "No place in connection for verifactu change '".$change['objid']."'");
34                $retval = ApplicationError::NotFound;
35            }
36        }
37
38        if (success($retval)){
39            $dstpath = AppConstants::RegLogPath();
40            if (!file_exists($dstpath) && !mkdir($dstpath, 0775)){
41                addlog(__FILE__, LogLevel::Error, "Could not create local registry log folder in '".$dstpath."'");
42                $error = error_get_last();
43                if ($error) addlog(__FILE__, LogLevel::Debug, $error['message']);
44            }
45
46            if (is_dir($dstpath)){
47                $lineinfo = [
48                    'objid' => $verifactu['objid'] ?? null,
49                    'session' => $session,
50                    'change' => 'UPDATE',
51                    'series' => $change['series'],
52                    'invceno' => $change['invoice'],
53                    'price' => $change['total'],
54                    'error' => $verifactu['error']
55                ];
56
57                switch($change['reason']){
58                    case 'F':
59                        $lineinfo['change'] = 'INSERT';
60                        break;
61                    case 'C':
62                        $lineinfo['change'] = 'DELETE';
63                        break;
64                }
65
66                $chainResult = registry_append_guarded($conx, $place, 'e4', $conx->tenant->now(), $lineinfo);
67                if (!success($chainResult)){
68                    addlog(__FILE__, LogLevel::Critical, "Fiscal activity link not written for place '".$place."' (verifactu change) — write failure details are in an earlier log line.");
69                }
70            }
71        }
72
73        return $retval;
74    }
75
76    function _register_verifactu_invceac_activity(\ConxHelper $conx, $session, &$verifactu){
77        $retval = ApplicationError::Success;
78        if ($conx->tenant === null) {
79            addlog(__FILE__, LogLevel::Error, "Tenant connection not available for _register_verifactu_invceac_activity.");
80            $retval = ApplicationError::Database;
81        }
82
83        if (success($retval)){
84            $invceac = null;
85            $results = [];
86            $res = $conx->tenant->query('ACCOUNTINVOICE', [ 'objid' => $verifactu['invceac'] ], $results);
87            if ($res){
88                if (count($results) > 0){
89                    $invceac = $results[0];
90                }
91                if (!$invceac){
92                    addlog(__FILE__, LogLevel::Error, "Cannot find account invoice entry for verifactu (account invoice objid [".$verifactu['invceac']."])");
93                    $retval = ApplicationError::NotFound;
94                }
95            }
96            else {
97                $retval = ApplicationError::Database;
98            }
99        }
100
101        if (success($retval)){
102            $place = $conx->place();
103            if (!$place){
104                addlog(__FILE__, LogLevel::Error, "No place in connection for verifactu invoice '".$invceac['objid']."'");
105                $retval = ApplicationError::NotFound;
106            }
107        }
108
109        if (success($retval)){
110            $dstpath = AppConstants::RegLogPath();
111            if (!file_exists($dstpath) && !mkdir($dstpath, 0775)){
112                addlog(__FILE__, LogLevel::Error, "Could not create local registry log folder in '".$dstpath."'");
113                $error = error_get_last();
114                if ($error) addlog(__FILE__, LogLevel::Debug, $error['message']);
115            }
116
117            if (is_dir($dstpath)){
118                $lineinfo = [
119                    'objid' => $verifactu['objid'] ?? null,
120                    'session' => $session,
121                    'change' => 'INSERT',
122                    'series' => $invceac['series'],
123                    'invceno' => $invceac['invoice'],
124                    'price' => $invceac['amount'],
125                    'error' => $verifactu['error']
126                ];
127                $chainResult = registry_append_guarded($conx, $place, 'e4', $conx->tenant->now(), $lineinfo);
128                if (!success($chainResult)){
129                    addlog(__FILE__, LogLevel::Critical, "Fiscal activity link not written for place '".$place."' (verifactu invceac) — write failure details are in an earlier log line.");
130                }
131            }
132        }
133
134        return $retval;
135    }
136
137    function register_verifactu_activity(\ConxHelper $conx, $session, &$verifactu){
138        if (($verifactu['tckchg'] ?? null) != null){
139            return _register_verifactu_change_activity($conx, $session, $verifactu);
140        }
141
142        if (($verifactu['invceac'] ?? null) != null){
143            return _register_verifactu_invceac_activity($conx, $session, $verifactu);
144        }
145
146        addlog(__FILE__, LogLevel::Error, "VERIFACTU entry must provide either 'tckchg' or 'invceac' values");
147        return ApplicationError::Invalid;
148    }
149?>