Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.11% covered (success)
91.11%
41 / 45
0.00% covered (danger)
0.00%
0 / 1
CRAP
n/a
0 / 0
register_ticket_activity
90.70% covered (success)
90.70%
39 / 43
0.00% covered (danger)
0.00%
0 / 1
20.32
1<?php
2    include_once __DIR__."/registry.php";
3    require_once __DIR__."/../legacy/ddbb/conx.php";
4
5    /**
6     * Register ticket activity. Uses tenant DB.
7     *
8     * @param \ConxHelper $conx Connection helper (tenant must be set).
9     * @param string $session Session id.
10     * @param array $entrynfo Ticket entry info with 'place' and optionally 'objid'.
11     * @return int ApplicationError code.
12     */
13    function register_ticket_activity(\ConxHelper $conx, $session, $entrynfo){
14        $retval = ApplicationError::Success;
15
16        if (!array_key_exists('place', $entrynfo) || ($entrynfo['place'] == null)){
17            addlog(__FILE__, LogLevel::Error, "Ticket without place: ".PHP_EOL.print_r($entrynfo, true).PHP_EOL);
18            $retval = ApplicationError::Invalid;
19        }
20        if ($conx->tenant === null) {
21            addlog(__FILE__, LogLevel::Error, "Tenant connection not available for register_ticket_activity.");
22            $retval = ApplicationError::Database;
23        }
24        if (success($retval)){
25            $dstpath = AppConstants::RegLogPath();
26            if (!file_exists($dstpath) && !mkdir($dstpath, 0775)){
27                addlog(__FILE__, LogLevel::Error, "Could not create local registry log folder in '".$dstpath."'");
28                $error = error_get_last();
29                if ($error) addlog(__FILE__, LogLevel::Debug, $error['message']);
30            }
31
32            if (is_dir($dstpath)){
33                $lineinfo = $entrynfo;
34
35                // Merge from the committed TICKET row. After sync COMMIT, TICKETCHANGE
36                // may already have assigned F/R while the deferred payload still carries
37                // the empty series/invceno the GUEST client cleared for server allocation.
38                if (array_key_exists('objid', $entrynfo)){
39                    $results = [];
40                    $res = $conx->tenant->query('TICKET', [ 'objid' => $entrynfo['objid'] ], $results);
41                    if ($res){
42                        if (count($results) > 0){
43                            $ticket = $results[0];
44                            foreach($ticket as $field => &$value){
45                                if (!array_key_exists($field, $lineinfo)){
46                                    $lineinfo[$field] = $value;
47                                }
48                                else if (($field === 'series' || $field === 'invceno')
49                                    && ($lineinfo[$field] === null || $lineinfo[$field] === '')){
50                                    $lineinfo[$field] = $value;
51                                }
52                            } unset($value);
53
54                            $lineinfo['session'] = $session;    // replace the ticket session with the current session
55                        }
56                        else {
57                            addlog(__FILE__, LogLevel::Error, "Ticket not found for objid [".$entrynfo['objid']."]");
58                            $retval = ApplicationError::NotFound;
59                        }
60                    }
61                    else {
62                        $retval = ApplicationError::Database;
63                    }
64                }
65
66                if (success($retval)){
67                    $chainResult = registry_append_guarded(
68                        $conx,
69                        $entrynfo['place'],
70                        LogEvent::TicketUpdate,
71                        $conx->tenant->now(),
72                        $lineinfo
73                    );
74                    
75                    if (!success($chainResult)){
76                        addlog(__FILE__, LogLevel::Critical, "Fiscal activity link not written for place '".$entrynfo['place']."' (ticket update) — write failure details are in an earlier log line.");
77                    }
78                }
79            }
80        }
81
82        return $retval;
83    }
84?>