<?php
 namespace App\AutoBalanceServices; class ServiceBalanceSuggestion { private $idService; private $supplierInvoiceRowsDbTable; private $balanceComponent; private $balanceScore; private $balanceScoreDetails = []; private $supplierInvoiceRowsList = []; public function __construct($idService) { $this->idService = $idService; } public function setSupplierInvoiceRowsDbTable($supplierInvoiceRowsDbTable) { $this->supplierInvoiceRowsDbTable = $supplierInvoiceRowsDbTable; } public function process() { $suppInvRowsList = $this->getAllMatchingSupplierInvoiceRows(); if (!$suppInvRowsList) { return false; } $calculateScore = new CalculateScore($this->idService, $suppInvRowsList); $calculateScore->process(); $this->balanceScore = $calculateScore->getBalanceScorePercent(); $this->balanceScoreDetails = $calculateScore->getBalanceScoreDetails(); $this->supplierInvoiceRowsList = $calculateScore->getSupplierInvoiceRowsList(); $this->balanceComponent = $calculateScore->getBalanceComponent(); } private function getAllMatchingSupplierInvoiceRows(): array { $serviceEnt = \Entity\Service::select($this->idService); $orderEnt = $serviceEnt->order; $offerEnt = $serviceEnt->chosenOffer; $db = \DB::getInstance(); $prepVars = []; $getSupplierInvoiceRowsSQL = "
			SELECT idRow
			FROM `{$this->supplierInvoiceRowsDbTable}`
			WHERE reservationId = ?
		"; $prepVars[] = $orderEnt->getId(); if ($orderEnt->reservationNumber) { $getSupplierInvoiceRowsSQL .= " OR reservationNumber LIKE ?"; $prepVars[] = $orderEnt->reservationNumber; } if ($orderEnt->reservationCode) { $getSupplierInvoiceRowsSQL .= " OR reservationCode LIKE ?"; $prepVars[] = $orderEnt->reservationCode; } $getSupplierInvoiceRowsSQL .= " OR idService = ?"; $prepVars[] = $serviceEnt->getId(); if ($offerEnt->externalSystemReservationId) { $getSupplierInvoiceRowsSQL .= " OR externalReservationId LIKE ?"; $prepVars[] = $offerEnt->externalSystemReservationId; } if ($offerEnt->confirmationNumber) { $getSupplierInvoiceRowsSQL .= " OR confirmationNumber LIKE ?"; $prepVars[] = $offerEnt->confirmationNumber; } if ($serviceEnt->passengersTicketsList) { $prepStmt = []; foreach($serviceEnt->passengersTicketsList as $tkt) { if ($tkt) { $prepStmt[] = "?"; $prepStmt[] = "?"; $prepVars[] = $tkt; $prepVars[] = $tkt.'R'; } } if ($prepStmt) { $getSupplierInvoiceRowsSQL .= " OR ticketNo IN (".implode(", ", $prepStmt).")"; } } $getSupplierInvoiceRows = $db->GetAll($getSupplierInvoiceRowsSQL, $prepVars); if (!is_array($getSupplierInvoiceRows)) { throw new \DBException('Could not search for matched supplier invoice rows when auto-balance runs.'); } $return = []; foreach($getSupplierInvoiceRows as $rowDet) { $return[] = $rowDet['idRow']; } return $return; } public function getBalanceComponent() { return $this->balanceComponent; } public function getBalanceScoreDetails() { return $this->balanceScoreDetails; } public function getBalanceScore() { return $this->balanceScore; } public function getSupplierInvoiceRowsList() { return $this->supplierInvoiceRowsList; } }