Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
54.17% |
39 / 72 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @file |
| 5 | * Place action: license extended period info (exndnfo). |
| 6 | */ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | require_once __DIR__.'/../../Legacy/common/session.php'; |
| 11 | |
| 12 | /** |
| 13 | * Action callable: get license extended period info for place. |
| 14 | * |
| 15 | * Opens central (PLACE.expires) and tenant (active TILL count). Price comes from LicenseService. |
| 16 | * |
| 17 | * @param string $body Unused. |
| 18 | * @param array<string, mixed> $query Query params: device, session, place. |
| 19 | * @param \ConxHelper $conx Connection helper. |
| 20 | * @param \Psr\Log\LoggerInterface $logger Logger. |
| 21 | * @param \UppServices\SessionService $sessionService Session service for check. |
| 22 | * @param \UppServices\LicenseService $licenseService License pricing catalog. |
| 23 | * @return array{output: string, contentType: string} |
| 24 | */ |
| 25 | return function ( |
| 26 | string $body, |
| 27 | array $query, |
| 28 | \ConxHelper $conx, |
| 29 | \Psr\Log\LoggerInterface $logger, |
| 30 | \UppServices\SessionService $sessionService, |
| 31 | \UppServices\LicenseService $licenseService |
| 32 | ): array { |
| 33 | $retval = \ApplicationError::Success; |
| 34 | $placeinfo = null; |
| 35 | $place = null; |
| 36 | |
| 37 | $device = $query['device'] ?? null; |
| 38 | if (!$device) { |
| 39 | $logger->error("No device provided on URL. Operation cancelled."); |
| 40 | $retval = \ApplicationError::Parameters; |
| 41 | } |
| 42 | |
| 43 | if (\success($retval)) { |
| 44 | $session = $query['session'] ?? null; |
| 45 | if (!$session) { |
| 46 | $logger->error("No session provided on URL. Operation cancelled."); |
| 47 | $retval = \ApplicationError::Parameters; |
| 48 | } else { |
| 49 | $userid = null; |
| 50 | $retval = $sessionService->checkSession($conx, (string) $session, (string) $device, $userid); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | if (\success($retval)) { |
| 55 | $place = $query['place'] ?? null; |
| 56 | if (!$place) { |
| 57 | $logger->error("Mandatory parameter 'place' not provided on URL. Operation cancelled."); |
| 58 | $retval = \ApplicationError::Parameters; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if (\success($retval)) { |
| 63 | $results = []; |
| 64 | $res = $conx->global->query('PLACE', ['objid' => $place, 'status' => ['PA', 'AC']], $results); |
| 65 | if ($res) { |
| 66 | if (count($results) > 0) { |
| 67 | $placeinfo = $results[0]; |
| 68 | } else { |
| 69 | $logger->error("No active place found with objid '".$place."'."); |
| 70 | $retval = \ApplicationError::NotFound; |
| 71 | } |
| 72 | } else { |
| 73 | $logger->error("Query failed for PLACE objid ".$place); |
| 74 | $retval = \ApplicationError::Database; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Open tenant DB to count active tills for EXTRATILL pricing. |
| 79 | if (\success($retval)) { |
| 80 | $conx->tenant = $place; |
| 81 | } |
| 82 | if (\success($retval) && $conx->tenant === null) { |
| 83 | $logger->error("Connection to place database failed."); |
| 84 | $retval = \ApplicationError::Database; |
| 85 | } |
| 86 | |
| 87 | $license = null; |
| 88 | if (\success($retval)) { |
| 89 | $extend_period = [ |
| 90 | '1M' => \AppConstants::Extend_Period_1M, |
| 91 | '3M' => \AppConstants::Extend_Period_3M, |
| 92 | '1Y' => \AppConstants::Extend_Period_1Y, |
| 93 | ]; |
| 94 | $activeTillCount = $licenseService->countActiveTills($conx); |
| 95 | $license = []; |
| 96 | $expiresBase = $placeinfo['expires'] ?? $conx->global->now(); |
| 97 | foreach ($extend_period as $period => $periodExpr) { |
| 98 | $resolved = $licenseService->resolveConcepts($activeTillCount, $period); |
| 99 | $dt1 = date("Y-m-d H:i:s", strtotime($periodExpr, strtotime($conx->global->now()))); |
| 100 | $dt2 = date("Y-m-d H:i:s", strtotime($periodExpr, strtotime($expiresBase))); |
| 101 | $license[$period] = [ |
| 102 | "expires" => ($dt1 > $dt2) ? $dt1 : $dt2, |
| 103 | 'price' => $resolved['total'], |
| 104 | 'taxRate' => $resolved['taxRate'], |
| 105 | 'taxTotal' => $resolved['taxTotal'], |
| 106 | 'totalWithTax' => $resolved['totalWithTax'], |
| 107 | 'currency' => \AppConstants::Currency, |
| 108 | 'concepts' => $resolved['concepts'], |
| 109 | ]; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | $result = [ |
| 114 | "errorcode" => $retval, |
| 115 | "license" => \success($retval) ? $license : null, |
| 116 | ]; |
| 117 | return ['output' => json_encode($result), 'contentType' => 'application/json; charset=utf-8']; |
| 118 | }; |