Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
70.30% |
71 / 101 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @file |
| 5 | * Place action: product list for date range. |
| 6 | */ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | require_once __DIR__.'/../../Legacy/common/session.php'; |
| 11 | |
| 12 | /** |
| 13 | * Action callable: get product list for place and date range. |
| 14 | * |
| 15 | * @param string $body Unused. |
| 16 | * @param array<string, mixed> $query Query params: device, session, place, dateini, dateend, timestamp. |
| 17 | * @param \ConxHelper $conx Connection helper. |
| 18 | * @param \Psr\Log\LoggerInterface $logger Logger. |
| 19 | * @param \UppServices\SessionService $sessionService Session service for check. |
| 20 | * @return array{output: string, contentType: string} |
| 21 | */ |
| 22 | return function (string $body, array $query, \ConxHelper $conx, \Psr\Log\LoggerInterface $logger, \UppServices\SessionService $sessionService): array { |
| 23 | $retval = \ApplicationError::Success; |
| 24 | $place = null; |
| 25 | $now = null; |
| 26 | |
| 27 | $device = $query['device'] ?? null; |
| 28 | if ($device === null || $device === '') { |
| 29 | $logger->error("No device provided on URL. Operation cancelled."); |
| 30 | $retval = \ApplicationError::Parameters; |
| 31 | } |
| 32 | |
| 33 | if (\success($retval)) { |
| 34 | $session = $query['session'] ?? null; |
| 35 | if ($session === null || $session === '') { |
| 36 | $logger->error("No session provided on URL. Operation cancelled."); |
| 37 | $retval = \ApplicationError::Parameters; |
| 38 | } else { |
| 39 | $unused = null; |
| 40 | $sessionObjid = null; |
| 41 | $retval = $sessionService->checkSession($conx, (string) $session, (string) $device, $unused, $sessionObjid); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | if (\success($retval)) { |
| 46 | $place = $query['place'] ?? null; |
| 47 | if ($place === null || $place === '') { |
| 48 | $logger->error("Mandatory argument 'place' not provided in url parameters."); |
| 49 | $retval = \ApplicationError::Parameters; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if (\success($retval)) { |
| 54 | $conx->tenant = $place; |
| 55 | } |
| 56 | |
| 57 | if (\success($retval) && $conx->tenant === null) { |
| 58 | $logger->error("Connection to place database failed."); |
| 59 | $retval = \ApplicationError::Database; |
| 60 | } |
| 61 | |
| 62 | if (\success($retval)) { |
| 63 | $now = $conx->tenant->now(-60); |
| 64 | } |
| 65 | |
| 66 | $ini = null; |
| 67 | $end = null; |
| 68 | $timestamp = null; |
| 69 | if (\success($retval)) { |
| 70 | $ini = $query['dateini'] ?? null; |
| 71 | if ($ini === null) { |
| 72 | $logger->error("Mandatory argument 'dateini' not provided in url parameters."); |
| 73 | $retval = \ApplicationError::Parameters; |
| 74 | } else { |
| 75 | $ini = date("Y-m-d H:i:s", (int) $ini); |
| 76 | } |
| 77 | } |
| 78 | if (\success($retval)) { |
| 79 | $end = $query['dateend'] ?? null; |
| 80 | if ($end === null) { |
| 81 | $logger->error("Mandatory argument 'dateend' not provided in url parameters."); |
| 82 | $retval = \ApplicationError::Parameters; |
| 83 | } else { |
| 84 | $end = date("Y-m-d H:i:s", (int) $end); |
| 85 | } |
| 86 | } |
| 87 | if (\success($retval)) { |
| 88 | $timestamp = $query['timestamp'] ?? null; |
| 89 | if ($timestamp === null) { |
| 90 | $logger->error("Mandatory argument 'timestamp' not provided in url parameters."); |
| 91 | $retval = \ApplicationError::Parameters; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | $ticketproducts = []; |
| 96 | if (\success($retval)) { |
| 97 | $sql = "select TICKETPRODUCT.objid, TICKETPRODUCT.product, TICKETPRODUCT.offer, TICKETPRODUCT.charge "; |
| 98 | $sql .= "from TICKETPRODUCT inner join TICKET on TICKETPRODUCT.ticket = TICKET.objid "; |
| 99 | $sql .= "where product in (select PRODUCT.objid from PRODUCT where place=".$place.") "; |
| 100 | $sql .= "and TICKET.status in ('AC', 'RD', 'PP', 'PD') "; |
| 101 | $sql .= "and TICKETPRODUCT.status = 'AC' "; |
| 102 | $sql .= "and TICKETPRODUCT.updated >= '".$timestamp."' "; |
| 103 | $sql .= "and TICKETPRODUCT.created >= '".$ini."' "; |
| 104 | $sql .= "and TICKETPRODUCT.updated <= '".$end."' "; |
| 105 | $res = $conx->tenant->runsql('TICKETPRODUCT', $sql, $ticketproducts); |
| 106 | if (!$res) { |
| 107 | $logger->error("Product list query failed for place ".$place); |
| 108 | $retval = \ApplicationError::Database; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | $indexed = []; |
| 113 | if (\success($retval)) { |
| 114 | foreach ($ticketproducts as &$ticketproduct) { |
| 115 | $product = $ticketproduct['product']; |
| 116 | if (!array_key_exists($product, $indexed)) { |
| 117 | $indexed[$product] = ['product' => null, 'ticketproducts' => []]; |
| 118 | } |
| 119 | $indexed[$product]['ticketproducts'][] = [ |
| 120 | "objid" => (int) $ticketproduct['objid'], |
| 121 | "inoffer" => $ticketproduct['offer'] ? true : false, |
| 122 | "charge" => round((float) $ticketproduct['charge'], 2), |
| 123 | ]; |
| 124 | } |
| 125 | unset($ticketproduct); |
| 126 | } |
| 127 | |
| 128 | if (\success($retval) && count($indexed) > 0) { |
| 129 | $products = []; |
| 130 | $res = $conx->tenant->query('PRODUCT', ['place' => $place, 'objid' => array_keys($indexed)], $products); |
| 131 | if (!$res) { |
| 132 | $logger->error("Product query failed for place ".$place); |
| 133 | $retval = \ApplicationError::Database; |
| 134 | } else { |
| 135 | foreach ($products as &$product) { |
| 136 | $indexed[$product['objid']]['product'] = &$product; |
| 137 | } |
| 138 | unset($product); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | $productlist = []; |
| 143 | if (\success($retval)) { |
| 144 | foreach ($indexed as $objid => &$info) { |
| 145 | $productlist[] = [ |
| 146 | 'objid' => $info['product']['objid'], |
| 147 | 'name' => $info['product']['name'], |
| 148 | 'price' => round((float) $info['product']['price'], 2), |
| 149 | 'items' => $info['ticketproducts'], |
| 150 | ]; |
| 151 | } |
| 152 | unset($info); |
| 153 | } |
| 154 | |
| 155 | $result = [ |
| 156 | "errorcode" => $retval, |
| 157 | "products" => \success($retval) ? $productlist : null, |
| 158 | "timestamp" => \success($retval) ? $now : null, |
| 159 | ]; |
| 160 | return ['output' => json_encode($result), 'contentType' => 'application/json; charset=utf-8']; |
| 161 | }; |