gestione gare pubbliche
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

279 righe
7.6 KiB

<?php
class FVOEDocumentsCollection {
/**
* Selected Documents Lists
*
* @var array
*/
private array $documents;
/**
* All Documents Lists
*
* @var array
*/
private array $all_documents;
/**
* codice_fvoe
*
* @var int
*/
private int $codice_fvoe;
/**
* tipo_documento
*
* @var string
*/
private string $tipo_documento;
/**
* Query initialization
*
* @var bool
*/
private static bool $queryInitialized = false;
// Query Statements
private static PDOStatement $stmDocuments;
private static PDOStatement $stmHasPendingRequest;
private static PDOStatement $stmRequests;
private static PDOStatement $stmPendingRelease;
private static PDOStatement $stmRequestsCheck;
/**
* Richieste di documento in attesa
*
* @var array
*/
private static array $pendingRequests = [];
/**
* Class constructor
*
* @return void
*/
public function __construct(int $codice_fvoe, string $tipo_documento) {
global $pdo;
if (!self::$queryInitialized) {
self::$stmDocuments = $pdo->prepare("SELECT * FROM b_fvoe_documenti WHERE codice_fvoe = :codice_fvoe AND tipo_documento = :tipo_documento AND (id_documento IS NOT NULL OR id_documento <> '') ORDER BY data_fine_validita ASC");
self::$stmHasPendingRequest = $pdo->prepare("SELECT * FROM b_fvoe_documenti WHERE codice_fvoe = :codice_fvoe AND tipo_documento = :tipo_documento AND (id_documento IS NULL OR id_documento = '') ORDER BY codice DESC LIMIT 0,1");
self::$stmRequests = $pdo->prepare("SELECT COUNT(codice) FROM b_fvoe_operazioni WHERE codice_fvoe = :codice_fvoe AND tipo_documento = :tipo_documento AND operazione = 'RICHIESTA-DOCUMENTO' AND (esito IS NULL OR esito = '' OR esito = 'OK')");
self::$stmRequestsCheck = $pdo->prepare("SELECT COUNT(codice) FROM b_fvoe_operazioni WHERE codice_documento = :codice_documento AND operazione = 'RICHIESTA-DOCUMENTO' AND esito = 'OK'");
self::$stmPendingRelease = $pdo->prepare("SELECT * FROM b_fvoe_documenti WHERE codice_fvoe = :codice_fvoe AND tipo_documento = :tipo_documento AND (id_documento IS NULL OR id_documento = '') ORDER BY data_emissione DESC");
self::$queryInitialized = true;
}
$this->codice_fvoe = $codice_fvoe;
$this->tipo_documento = $tipo_documento;
self::$stmDocuments->execute([":codice_fvoe" => $codice_fvoe, ":tipo_documento" => $tipo_documento]);
$this->all_documents = $this->documents = self::$stmDocuments->fetchAll(PDO::FETCH_CLASS, "FVOEDocument");
}
/**
* Get all documents
*
* @return array
*/
public function all() : array
{
return $this->documents;
}
/**
* Get current documents
*
* @return FVOEDocumentsCollection
*/
public function getCurrentDocuments() : FVOEDocumentsCollection
{
$currentDate = new DateTime();
$this->documents = array_filter($this->all_documents, function ($document) use ($currentDate) {
// Restituiamo true se la data corrente è minore o uguale alla data di fine validità
return $currentDate <= $this->getValidityDate($document);
});
return $this;
}
/**
* Get old documents
*
* @return FVOEDocumentsCollection
*/
public function getOldDocuments() : FVOEDocumentsCollection
{
$currentDate = new DateTime();
$this->documents = array_filter($this->all_documents, function ($document) use ($currentDate) {
// Restituiamo true se la data corrente è maggiore alla data di fine validità
return $currentDate > $this->getValidityDate($document);
});
return $this;
}
/**
* Get document validity date
*
* @param FVOEDocument $document
* @return DateTime
*/
private function getValidityDate(FVOEDocument $document) : ?DateTime
{
$fields = ['data_fine_validita', 'data_emissione', 'data_inserimento', 'data_creazione'];
// Se tutti i campi sono vuoti, il documento non ha una data di scadenza
if (! array_filter($fields, fn($field) => ! empty($document->$field))) {
return null;
}
// Se ho una data di fine validità la restituisco
if(! empty($document->data_fine_validita)) {
return new DateTime($document->data_fine_validita);
}
// Se data_fine_validita è nulla, calcoliamo (per convenzione) la validità di 6 mesi dalla data_emissione, data_inserimento o dalla data_creazione
foreach (array_slice($fields, 1) as $field) {
if(! empty($document->$field)) {
return (new DateTime($document->$field))->modify('+6 months');
}
}
}
/**
* Get Document Status
*
* @return stdClass
*/
public function getStatus() : stdClass {
$statuses = (object) json_decode(json_encode(FVOEDocument::STATI));
$document = $this->latest();
if(! empty($document->codice)) {
return $document->stato;
}
return $statuses->{0};
}
/**
* Get latest document
*
* @return FVOEDocument
*/
public function latest() : ?FVOEDocument {
return ! empty($this->documents) ? end($this->documents) : null;
}
/**
* Check if have requests been made
*
* @return bool
*/
public function haveRequestsBeenMade() : bool {
self::$stmRequests->execute([':codice_fvoe' => $this->codice_fvoe, ':tipo_documento' => $this->tipo_documento]);
return self::$stmRequests->fetch(PDO::FETCH_COLUMN, 0) > 0;
}
/**
* Verifica se ci sono richieste asincrone il cui esito deve essere aggiornato
*
* @return bool
*/
public function hasPendingRequest() : bool
{
$this->checkPendingRequest();
return ! empty(self::$pendingRequests[$this->codice_fvoe][$this->tipo_documento]);
}
/**
* Get a pending request
*
* @return ?FVOEDocument
*/
public function getPendingRequest() : ?FVOEDocument
{
$this->checkPendingRequest();
return self::$pendingRequests[$this->codice_fvoe][$this->tipo_documento];
}
/**
* Verifica se siamo in attesa di ricevere un documento
*
* @return bool
*/
public function areThereDocumentsPendingRelease() : bool
{
self::$stmPendingRelease->execute([":codice_fvoe" => $this->codice_fvoe, ":tipo_documento" => $this->tipo_documento]);
if(self::$stmPendingRelease->rowCount() > 0) {
$documento = self::$stmPendingRelease->fetch(PDO::FETCH_ASSOC);
self::$stmRequestsCheck->execute([':codice_documento' => $documento["codice"]]);
if(self::$stmRequestsCheck->rowCount() > 0) {
return true;
}
}
return false;
}
/**
* Check pending request
*
* @return void
*/
private function checkPendingRequest() : void
{
if(! isset(self::$pendingRequests[$this->codice_fvoe]) || ! isset(self::$pendingRequests[$this->codice_fvoe][$this->tipo_documento])) {
self::$pendingRequests[$this->codice_fvoe][$this->tipo_documento] = null;
self::$stmHasPendingRequest->execute([":codice_fvoe" => $this->codice_fvoe, ":tipo_documento" => $this->tipo_documento]);
if(self::$stmHasPendingRequest->rowCount() > 0) {
self::$stmHasPendingRequest->setFetchMode(PDO::FETCH_CLASS, "FVOEDocument");
self::$pendingRequests[$this->codice_fvoe][$this->tipo_documento] = self::$stmHasPendingRequest->fetch();
$operation = FVOE::fetchLastOperation(self::$pendingRequests[$this->codice_fvoe][$this->tipo_documento]->codice, null, null, "RICHIESTA-DOCUMENTO", false, false);
if(! empty($operation["esito"])) {
self::$pendingRequests[$this->codice_fvoe][$this->tipo_documento] = null;
}
}
}
}
/**
* Get the documents number
*
* @return Int
*/
public function count() : Int {
return count($this->documents);
}
}