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.
 
 
 
 
 

1650 righe
67 KiB

<?
function convertDate_SIMOG($array) {
foreach($array AS $key => $value) {
if (is_array($value)) {
$array[$key] = convertDate_SIMOG($value);
} else {
if ((stripos($key, "DATA_") !== false) || (stripos($key, "TERMINE_") !== false) && $key != "TERMINE_RIDOTTO") { $array[$key] = date2mysql($value); }
}
}
return $array;
}
function convertDate_FROMSIMOG($array) {
foreach($array AS $key => $value) {
if (is_array($value)) {
$array[$key] = convertDate_FROMSIMOG($value);
} else {
if ((stripos($key, "DATA_") !== false) || (stripos($key, "TERMINE_") !== false) && $key != "TERMINE_RIDOTTO") { $array[$key] = mysql2date($value); }
}
}
return $array;
}
function convertDate_DbDateSimog($array) {
foreach($array AS $key => $value) {
if (is_array($value)) {
$array[$key] = convertDate_DbDateSimog($value);
} else if (!empty($value)) {
if ((stripos($key, "DATA_") !== false) || (stripos($key, "TERMINE_") !== false) && $key != "TERMINE_RIDOTTO") {
$date = new DateTime(($value));
$array[$key] = $date->format(DateTime::ATOM);
}
}
}
return $array;
}
class anac {
public $info;
public $version;
public $modulo;
public $elemento;
public $codice_simog;
public $codice_lotto;
private $checksPDO;
private $db;
public function __construct() {
global $pdo;
$this->db = $pdo;
$this->version = self::getVersion();
$this->modulo = "anac";
}
public function updateStato($stato) {
if (empty($this->info["stato"]) || $this->info["stato"] < $stato) {
$this->db->go("UPDATE b_simog SET stato = :stato WHERE codice = :codice",[":stato"=>$stato,":codice"=>$this->info["codice"]]);
}
if (isset($_SESSION["ente"])) {
$_SESSION["ente"]->manageUpdates("anac",$this->info["codice"],$this);
}
}
public static function getVersion($data = "") {
$return = "3.04.7";
if(time() >= strtotime('2023-02-13 00:00:00')) {
$return = "3.04.8";
}
if(time() >= strtotime('2023-06-21 00:00:00') || DEMO_ENV || DEVELOP_ENV) {
$return = "3.04.9";
}
if (! empty($data)) {
$data = strtotime($data);
if($data < strtotime('2022-06-22')) {
$return = "3.04.5";
}
}
return $return;
}
public static function limiteSmartCIG($data = "") {
if (empty($data)) {
$data = time();
} else {
$data = strtotime($data);
}
$limite = 40000;
// TODO: Implementare controllo su data in caso di modifica normativa
return $limite;
}
public static function getSceltaContraenteFromGara($id,$smart_cig,$data = "") {
if ($smart_cig == "S") {
$source = "raccordoSmartGare.json";
} else if ($smart_cig == "N") {
$source = "raccordoRegularGare.json";
}
if (!empty($source)) {
$version = self::getVersion($data);
$path = self::getVersionPath($version);
if (!empty($path)) {
$path .= "/data/{$source}";
if (file_exists($path)) {
$options = jsonToArray($path);
if (!empty($options)) {
return $options[$id] ?? false;
}
}
}
}
return false;
}
public static function getXMLPath($codice_ente,$anno = NULL,$generale = false) {
if (is_numeric($codice_ente)) {
global $config;
$path = $config["mediaFolder"]."/enti/{$codice_ente}/XML190";
if (!empty($anno)) {
if (is_numeric($anno)) {
$path .= "/{$anno}";
if ($generale) { $path .= "_generale"; }
$path .= ".xml";
return $path;
}
}
return $path;
}
return false;
}
public static function getDateXML190($codice_ente,$anno,$generale) {
$path = self::getXMLPath($codice_ente,$anno,$generale);
if (file_exists($path)) {
adaptMemoryLimitToFile($path);
$existent = file_get_contents($path);
$existent = new SimpleXMLElement($existent);
if (!empty($existent)) {
$return["dataPubblicazione"] = $existent->metadata->dataPubbicazioneDataset;
$return["dataAggiornamento"] = $existent->metadata->dataUltimoAggiornamentoDataset;
}
}
return false;
}
public static function getURL190($codice_ente,$anno,$generale) {
global $config;
$path = self::getXMLPath($codice_ente,$anno,$generale);
if (!empty($path)) {
$clean = str_replace($config["mediaFolder"],"",$path);
return $config["link_sito"] ."/documenti". $clean;
}
}
public static function getSceltaContraente190($id,$smart_cig) {
if ($smart_cig == "S") {
$source = "raccordoSmart190.json";
} else if ($smart_cig == "N") {
$source = "raccordoRegular190.json";
}
if (!empty($source)) {
$version = self::getVersion();
$path = self::getVersionPath($version);
if (!empty($path)) {
$path .= "/data/{$source}";
if (file_exists($path)) {
$options = jsonToArray($path);
if (!empty($options)) {
return $options[$id] ?? false;
}
}
}
}
return false;
}
public static function parsePartecipante190($tmp) {
$partecipante = [];
if ($tmp["stato"] == "IT") {
$partecipante["codiceFiscale"] = $tmp["codice_fiscale"] ?? $tmp["codice_fiscale_impresa"];
} else {
$partecipante["identificativoFiscaleEstero"] = $tmp["codice_fiscale"] ?? $tmp["codice_fiscale_impresa"];
}
$partecipante["ragioneSociale"] = $tmp["ragione_sociale"];
if (!empty($tmp["ruolo"])) {
$partecipante["ruolo"] = $tmp["ruolo"];
}
return $partecipante;
}
public static function validateXML190($path) {
$errori = [];
if (file_exists($path)) {
$version = self::getVersion();
$pathDataSet = self::getVersionPath($version);
if (!empty($path) && !empty($pathDataSet) && is_file($path)) {
$pathDataSet .= "/utility/datasetAppaltiL190.xsd";
$xml = new DOMDocument();
libxml_use_internal_errors(true);
if ($xml->loadXML(file_get_contents($path))) {
if (!$xml->schemaValidate($pathDataSet)) {
$errori = libxml_get_errors();
libxml_clear_errors();
}
}
}
} else {
$errori[] = __("File non esistente");
}
return $errori;
}
public static function get190Data($codice_ente,$anno,$generale) {
global $pdo;
$selectLotti = "SELECT b_simog_lotti.codice, b_simog.codice_ente, b_simog.smart_cig, b_simog_lotti.oggetto,b_simog_lotti.id_scelta_contraente,b_simog_lotti.cig,
b_simog_lotti.importo,b_simog_lotti.190data_inizio,b_simog_lotti.190data_fine,b_simog_lotti.190importoAggiudicato,b_simog_lotti.190liquidazioni
FROM b_simog_lotti JOIN b_simog ON b_simog_lotti.codice_simog = b_simog.codice
WHERE b_simog.anno_riferimento = :anno AND b_simog_lotti.eliminato = 'N' AND b_simog.stato <> '90' AND b_simog_lotti.cig IS NOT NULL ";
if ($generale) {
$selectLotti .= " AND b_simog.codice_gestore = :ente ";
} else {
$selectLotti .= " AND b_simog.codice_ente = :ente ";
}
$selectLotti .= " ORDER BY b_simog_lotti.codice ";
$lotti = $pdo->go($selectLotti,[":anno"=>$anno,":ente"=>$codice_ente])->fetchAll(PDO::FETCH_ASSOC);
$data = [];
if (count($lotti) > 0) {
$qRaggruppamento = $pdo->prepare("SELECT * FROM r_raggruppamenti_simog WHERE r_raggruppamenti_simog.codice_padre = :codice_partecipante ");
$qPartecipanti = $pdo->prepare("SELECT * FROM r_partecipanti_simog WHERE r_partecipanti_simog.codice_lotto = :codice_lotto ");;
foreach($lotti AS $tmp) {
$lotto = [];
$lotto["cig"] = (!empty($tmp["cig"])) ? $tmp["cig"] : "0000000000";
if (!empty($enti[$tmp["codice_ente"]])) {
$beneficiario = $enti[$tmp["codice_ente"]];
} else {
$beneficiario = $enti[$tmp["codice_ente"]] = Ente::getInfoFromID($tmp["codice_ente"])[0];
}
$lotto["strutturaProponente"]["codiceFiscaleProp"] = $beneficiario["cf"];
$lotto["strutturaProponente"]["denominazione"] = $beneficiario["denominazione"];
$lotto["oggetto"] = substr(trim(strip_tags($tmp["oggetto"])),0,250);;
$lotto["oggetto"] = str_replace("<","&lt;",$lotto["oggetto"]);
$lotto["oggetto"] = str_replace("&lt;","min.",$lotto["oggetto"]);
$lotto["oggetto"] = str_replace("&gt;","mag.",$lotto["oggetto"]);
$lotto["sceltaContraente"] = self::getSceltaContraente190($tmp["id_scelta_contraente"],$tmp["smart_cig"]);
$partecipanti = [];
$raggruppamenti = [];
$aggiudicatari = [];
$raggruppamenti_aggiudicatari = [];
$bloccoPartecipanti = [];
$bloccoAggiudicatari = [];
$qPartecipanti->bindValue(":codice_lotto",$tmp["codice"]);
$qPartecipanti->execute();
if ($qPartecipanti->rowCount() > 0) {
while($partecipante = $qPartecipanti->fetch(PDO::FETCH_ASSOC)) {
$aggiudicatario = ($partecipante["aggiudicatario"] == "S") ? true : false;
$qRaggruppamento->bindValue(":codice_partecipante",$partecipante["codice"]);
$qRaggruppamento->execute();
if ($qRaggruppamento->rowCount() > 0) {
$partecipante["raggruppamento"] = $qRaggruppamento->fetchAll(PDO::FETCH_ASSOC);
$raggruppamenti[] = $partecipante;
if ($aggiudicatario) { $raggruppamenti_aggiudicatari[] = $partecipante; }
} else {
$partecipanti[] = $partecipante;
if ($aggiudicatario) { $aggiudicatari[] = $partecipante; }
}
}
if (!empty($raggruppamenti)) {
foreach($raggruppamenti AS $partecipante) {
$raggruppamento = [];
$partecipante["ruolo"] = "04-CAPOGRUPPO";
$raggruppamento["membro"][] = self::parsePartecipante190($partecipante);
foreach($partecipante["raggruppamento"] AS $sub) {
$raggruppamento["membro"][] = self::parsePartecipante190($sub);
}
$bloccoPartecipanti["raggruppamento"][] = $raggruppamento;
}
}
if (!empty($partecipanti)) {
foreach($partecipanti AS $partecipante) {
$bloccoPartecipanti["partecipante"][] = self::parsePartecipante190($partecipante);
}
}
if (!empty($raggruppamenti_aggiudicatari)) {
foreach($raggruppamenti_aggiudicatari AS $partecipante) {
$raggruppamento = [];
$partecipante["ruolo"] = "04-CAPOGRUPPO";
$raggruppamento["membro"][] = self::parsePartecipante190($partecipante);
foreach($partecipante["raggruppamento"] AS $sub) {
$raggruppamento["membro"][] = self::parsePartecipante190($sub);
}
$bloccoAggiudicatari["aggiudicatarioRaggruppamento"][] = $raggruppamento;
}
}
if (!empty($aggiudicatari)) {
foreach($aggiudicatari AS $partecipante) {
$bloccoAggiudicatari["aggiudicatario"][] = self::parsePartecipante190($partecipante);
}
}
}
$lotto["partecipanti"] = $bloccoPartecipanti;
$lotto["aggiudicatari"] = $bloccoAggiudicatari;
$lotto["importoAggiudicazione"] = ($tmp["190importoAggiudicato"] > 0) ? number_format($tmp["190importoAggiudicato"],2,".","") : 0;
$tempiCompletamento = [];
if (!empty($tmp["190data_inizio"]) && !empty($tmp["190data_fine"])) {
$tempiCompletamento["dataInizio"] = $tmp["190data_inizio"];
$tempiCompletamento["dataUltimazione"] = $tmp["190data_fine"];
}
$lotto["tempiCompletamento"] = $tempiCompletamento;
$lotto["importoSommeLiquidate"] = ($tmp["190liquidazioni"] > 0) ? number_format($tmp["190liquidazioni"],2,".","") : 0;
$data["lotto"][] = $lotto;
}
}
return $data;
}
public static function generaXML($codice_ente,$anno,$generale,$simulazione = false) {
$return = false;
$data = self::get190Data($codice_ente,$anno,$generale);
if (!empty($data)) {
$xml = [];
$xml["legge190:pubblicazione"]["@xmlns:legge190"]="legge190_1_0";
$xml["legge190:pubblicazione"]["@xmlns:xsi"] = "http://www.w3.org/2001/XMLSchema-instance";
$xml["legge190:pubblicazione"]["@xsi:schemaLocation"]="legge190_1_0 datasetAppaltiL190.xsd";
$enti = [];
$ente = $enti[$codice_ente] = Ente::getInfoFromID($codice_ente)[0];
$metadata = [];
$metadata["titolo"] = "Pubblicazione dati legge 190/2012";
$metadata["abstract"] = "Pubblicazione dati legge 190/2012 - Anno di riferimento " . $anno;
$metadata["dataPubblicazioneDataset"] = self::getDateXML190($codice_ente,$anno,$generale)["dataPubblicazione"] ?? date("Y-m-d");
$metadata["entePubblicatore"] = $ente["denominazione"] . " - Codice Fiscale " . $ente["cf"];
if (!$simulazione) {
$metadata["dataUltimoAggiornamentoDataset"] = date("Y-m-d");
} else {
$metadata["dataUltimoAggiornamentoDataset"] = self::getDateXML190($codice_ente,$anno,$generale)["dataAggiornamento"] ?? date("Y-m-d");
}
$metadata["annoRiferimento"] = $anno;
$metadata["urlFile"] = self::getURL190($codice_ente,$anno,$generale);
$metadata["licenza"] = "IODL";
$xml["legge190:pubblicazione"]["metadata"] = $metadata;
$xml["legge190:pubblicazione"]["data"] = $data;
$string_xml = html_entity_decode(array2XML($xml,"","",true),ENT_QUOTES,'UTF-8');
$string_xml = str_replace("&", "&amp;", $string_xml);
$string_xml = utf8_encode($string_xml);
$domxml = new DOMDocument('1.0');
$domxml->preserveWhiteSpace = true;
$domxml->formatOutput = true;
$domxml->loadXML($string_xml);
return $domxml->saveXML();
}
return $return;
}
public static function getVersionPath($version) {
$path = __DIR__ . "/versions/{$version}";
if (is_dir($path)) { return $path; }
return false;
}
public function printEditGara() {
return $this->includeView("/views/form-gara.php");
}
public function printEditLotto($codice_lotto = 0) {
return $this->includeView("/views/form-lotto.php",$codice_lotto);
}
public function printRequisiti() {
return $this->includeView("/views/requisiti.php");
}
public function printPubblica() {
return $this->includeView("/views/pubblica.php");
}
public function printDeleteLotto($codice_lotto) {
return $this->includeView("/views/delete-lotto.php",$codice_lotto);
}
public function printDeleteGara() {
return $this->includeView("/views/delete-gara.php");
}
public function printPrendiInCarico() {
return $this->includeView("/views/presa-in-carico.php");
}
public function printRicevutaCIG($codice_lotto = 0) {
return $this->includeView("/views/ricevuta-cig.php",$codice_lotto);
}
public function printScheda($codice_lotto,$type) {
return $this->includeView("/views/form-schede.php",$codice_lotto,["type"=>$type]);
}
private function includeView($view,$codice_lotto=0,$data = null) {
$path = self::getVersionPath($this->version);
if (!empty($path)) {
$path .= $view;
if (file_exists($path)) {
include($path);
return true;
}
}
return false;
}
public static function relazioniModulo($modulo,$codice_gara,$codice_lotto = null) {
global $pdo;
$bind = [];
$bind[":modulo"] = $modulo;
$bind[":codice_gara"] = $codice_gara;
$sql = "SELECT * FROM r_simog_moduli WHERE modulo = :modulo AND codice_gara = :codice_gara ";
if ($codice_lotto !== null) {
$bind[":codice_lotto"] = $codice_lotto;
$sql .= " AND codice_lotto = :codice_lotto ";
}
return $pdo->go($sql,$bind)->fetch(PDO::FETCH_ASSOC);
}
public function getRelazioni($codice_lotto = null) {
$bind = [":codice_simog"=>$this->info["codice"]];
$sql = "SELECT * FROM r_simog_moduli WHERE gara_simog = :codice_simog ";
if (!empty($codice_lotto)) {
$bind[":codice_lotto"] = $codice_lotto;
$sql .= " AND lotto_simog = :codice_lotto ";
}
return $this->db->go($sql,$bind)->fetchAll(PDO::FETCH_ASSOC);
}
public function getBreadcrumb($breadcrumb = []) {
$relazioni = $this->getRelazioni();
if (!empty($relazioni)) {
foreach($relazioni AS $relazione) {
if ($_SESSION["utente"]->permission($relazione["modulo"],$relazione["codice_gara"])) {
global $beRoot;
if (file_exists("{$beRoot}/{$relazione["modulo"]}/panel.php")) {
$infoModulo = Modulo::getInfo($relazione["modulo"]);
$breadcrumb["/{$relazione["modulo"]}/panel.php?codice={$relazione["codice_gara"]}"] = __($infoModulo["titolo"]) . " - " . __("Pannello");
}
}
}
}
return $breadcrumb;
}
public static function relazioniDisponibili() {
$return = [];
$return["pcp"] = ["link"=>"/backend/pcp/panel.php?codice=#codice_gara#&codice_round=#codice_fase#","updateCig"=>"/backend/pcp/anac/update.php"];
$return["gare"] = ["link"=>"/backend/gare/panel.php?codice=#codice_gara#&codice_round=#codice_fase#","updateCig"=>"/backend/gare/anac/update.php"];
$return["concorsi"] = ["link"=>"/backend/concorsi/panel.php?codice=#codice_gara#&codice_round=#codice_fase#","updateCig"=>"/backend/concorsi/anac/update.php"];
return $return;
}
public function saveRelazione($modulo,$codice_gara,$codice_lotto,$lotto_simog) {
return self::staticSaveRelazione($modulo,$codice_gara,$codice_lotto,$this->info["codice"],$lotto_simog);
}
public static function staticSaveRelazione($modulo,$codice_gara,$codice_lotto,$codice_simog,$lotto_simog) {
$relazioniDisponibili = self::relazioniDisponibili();
if (!empty($relazioniDisponibili[$modulo])) {
$relazione = [];
$relazione["modulo"] = $modulo;
$relazione["codice_gara"] = $codice_gara;
$relazione["codice_lotto"] = $codice_lotto;
$relazione["gara_simog"] = $codice_simog;
$relazione["lotto_simog"] = $lotto_simog;
$salva = new salva();
$salva->debug = false;
$salva->nome_tabella = "r_simog_moduli";
$salva->operazione = "INSERT";
$salva->oggetto = $relazione;
$codice = $salva->save();
if (!empty($codice)) { return true; }
}
}
public function updateRelazioni($codice_lotto) {
$relazioni = $this->getRelazioni($codice_lotto);
if (!empty($relazioni)) {
$relazioniDisponibili = self::relazioniDisponibili();
$lotto = $this->getLotti($codice_lotto)[0] ?? false;
if (!empty($lotto)) {
global $root;
foreach($relazioni AS $relazione) {
if (!empty($relazioniDisponibili[$relazione["modulo"]]["updateCig"])) {
include($root.$relazioniDisponibili[$relazione["modulo"]]["updateCig"]);
}
}
}
}
}
public static function labels190() {
$labels = [];
$labels["190data_inizio"] = "Data inizio contratto";
$labels["190data_fine"] = "Data fine contratto";
$labels["190importoAggiudicato"] = "Importo aggiudicato";
$labels["190liquidazioni"] = "Importo liquidato";
$labels["partecipanti"] = "Partecipanti";
$labels["aggiudicatari"] = "Aggiudicatari";
return $labels;
}
public function check190Lotto($lotto) {
$missing = [];
if (empty($lotto["190data_inizio"])) { $missing[] = "190data_inizio"; }
if (empty($lotto["190data_fine"])) { $missing[] = "190data_fine"; }
if (empty($lotto["190importoAggiudicato"])) { $missing[] = "190importoAggiudicato"; }
if (empty($lotto["190liquidazioni"])) { $missing[] = "190liquidazioni"; }
if (empty($this->checksPDO["partecipanti"])) {
$this->checksPDO["partecipanti"] = $this->db->prepare("SELECT codice FROM r_partecipanti_simog WHERE codice_lotto = :lotto");
$this->checksPDO["aggiudicatari"] = $this->db->prepare("SELECT codice FROM r_partecipanti_simog WHERE codice_lotto = :lotto AND aggiudicatario = 'S'");
}
$this->checksPDO["partecipanti"]->bindValue(":lotto",$lotto["codice"]);
$this->checksPDO["partecipanti"]->execute();
if (empty($this->checksPDO["partecipanti"]->rowCount())) { $missing[] = "partecipanti"; }
$this->checksPDO["aggiudicatari"]->bindValue(":lotto",$lotto["codice"]);
$this->checksPDO["aggiudicatari"]->execute();
if (empty($this->checksPDO["aggiudicatari"]->rowCount())) { $missing[] = "aggiudicatari"; }
return $missing;
}
public function addRequisitiObbligatori() {
if (empty($this->getDati()["requisiti"])) {
$requisiti = self::getListeSIMOG("CodRequisitoType",$this->info["data_invio_simog"]) ?? false;
if (!empty($requisiti)) {
foreach($requisiti AS $id => $descr) {
if (strpos($descr,"OBBLIGATORIO") !== FALSE) {
$requisito = [];
$requisito["codice_dettaglio"] = $id;
$requisito["descrizione"] = "";
$requisito["valore"] = "";
$requisito["flag_esclusione"] = "S";
$requisito["flag_comprova_offerta"] = "N";
$requisito["flag_avvalimento"] = "N";
$requisito["flag_bando_tipo"] = "N";
$requisito["flag_riservatezza"] = "N";
$requisito["DOCUMENTO"] = [];
$requisito["CIG"] = [];
$this->saveRequisito($requisito);
}
}
}
}
}
public function saveRequisito($requisito) {
$dati = $this->getDati();
$requisiti = $dati["requisiti"] ?? [];
if (empty($requisito["codice_dettaglio"]) && !empty($requisito["hidden_codice_dettaglio"])) {
$requisito["codice_dettaglio"] = $requisito["hidden_codice_dettaglio"];
}
if (!empty($requisito["codice_dettaglio"])) {
$id = $requisito["codice_dettaglio"];
$requisiti[$id] = $requisito;
$dati["requisiti"] = $requisiti;
$update = [];
$update["codice"] = $this->info["codice"];
$this->info["dati"] = $update["dati"] = json_encode($dati);
$salva = new salva();
$salva->debug = false;
$salva->nome_tabella = "b_simog";
$salva->operazione = "UPDATE";
$salva->oggetto = $update;
$codice = $salva->save();
if ($codice === $this->info["codice"]) {
$this->updateSimogLog("inviaRequisiti",2,__("Non sincronizzato"));
return true;
}
}
return false;
}
public function deleteRequisito($idRequisito) {
$dati = $this->getDati();
$requisito = self::getListeSIMOG("CodRequisitoType",$this->info["data_invio_simog"])[$idRequisito] ?? false;
if (!empty($requisito)) {
if (strpos($requisito,"OBBLIGATORIO") === FALSE) {
if (isset($dati["requisiti"][$idRequisito])) { unset($dati["requisiti"][$idRequisito]); }
$update = [];
$update["codice"] = $this->info["codice"];
$this->info["dati"] = $update["dati"] = json_encode($dati);
$salva = new salva();
$salva->debug = false;
$salva->nome_tabella = "b_simog";
$salva->operazione = "UPDATE";
$salva->oggetto = $update;
$codice = $salva->save();
if ($codice === $this->info["codice"]) {
return true;
}
}
}
return false;
}
public function printLoginButton() {
$path = __DIR__."/views/login-button.php";
if (file_exists($path)) {
include($path);
return true;
}
return false;
}
public function createFromSIMOG($response,$anno,$codice_ente,$smart) {
$return = false;
$path = self::getVersionPath($this->version);
if (!empty($path)) {
$path .= "/script/updateFromSIMOG.php";
if (file_exists($path)) {
$operation = "create";
include($path);
}
}
return $return;
}
public function getRequestArray($method,$codice_lotto=0) {
$return = false;
$path = self::getVersionPath($this->version);
if (!empty($path)) {
if (strpos($method,".") === false) {
$path .= "/script/{$method}.php";
if (file_exists($path)) {
$operation = "get";
include($path);
}
}
}
return $return;
}
public function manageResponse($response,$method,$codice_lotto=0,$massive = false) {
$return = false;
$path = self::getVersionPath($this->version);
if (!empty($path)) {
if (strpos($method,".") === false) {
$path .= "/script/{$method}.php";
if (file_exists($path)) {
$operation = "put";
include($path);
if (!empty($this->info["codice"])) { $this->getManagerRegistro()->addToLog("SIMOG",$method,$codice_lotto); }
}
}
}
return $return;
}
public function saveGara($post) {
$return = false;
$path = self::getVersionPath($this->version);
if (!empty($path)) {
$path .= "/script/saveGara.php";
if (file_exists($path)) {
include($path);
if (!empty($return["codice"])) {
$this->getManagerRegistro()->addToLog("SAVE",__("Dati gara"));
if (empty($post["codice"])) {
if ($this->modulo != "anac") {
$utenti = ente::getUtentiFromModulo($this->modulo,$_SESSION["ente"]->codice,"",$return["codice"]);
} else {
$utenti = [];
}
if ($_SESSION["utente"]->gerarchia > 15) {
$utenti[] = $_SESSION["utente"]->codice;
}
if (!empty($utenti)) {
foreach($utenti AS $utente) {
$permesso = [];
$permesso["sezione"] = "anac";
$permesso["codice_elemento"] = $this->info["codice"];
$permesso["codice_gestore"] = $_SESSION["ente"]->codice;
$permesso["funzione"] = "0";
$permesso["codice_utente"] = $utente;
$salva = new salva();
$salva->debug = false;
$salva->nome_tabella = "r_permessi";
$salva->operazione = "INSERT";
$salva->oggetto = $permesso;
$salva->save();
}
}
}
}
}
}
return $return;
}
public function saveLotto($lotto,$saveDati = false, bool $force = false) {
$return = false;
$path = self::getVersionPath($this->version);
if (!empty($path)) {
$path .= "/script/saveLotto.php";
if (file_exists($path)) {
include($path);
$this->getManagerRegistro()->addToLog("SAVE",__("Dati lotto"),$lotto["codice"] ?? 0);
}
}
return $return;
}
public function savePresaInCarico($post) {
$return = false;
$path = self::getVersionPath($this->version);
if (!empty($path)) {
$path .= "/script/savePresaInCarico.php";
if (file_exists($path)) {
include($path);
}
}
return $return;
}
public function savePubblica($post) {
$return = false;
$path = self::getVersionPath($this->version);
if (!empty($path)) {
$path .= "/script/savePubblica.php";
if (file_exists($path)) {
include($path);
$this->getManagerRegistro()->addToLog("SAVE",__("Dati pubblicazione"));
}
}
return $return;
}
public function getManagerRegistro() {
return new registriManager("anac",$this->info["codice"]);
}
public function addToLog($operazione,$oggetto) {
return $this->getManagerRegistro()->addToLog($operazione,$oggetto,$this->info["codice"]);
}
public function getLog($start=0,$length=-1) {
return $this->getManagerRegistro()->getLog($start,$length);
}
public function getLastSimogLog($method,$codice_lotto = 0) {
return $this->db->go("SELECT codice,esito,msg FROM b_simog_log WHERE codice_simog = :simog AND metodo = :method AND codice_lotto = :codice_lotto ORDER BY codice DESC LIMIT 0,1",
[":simog"=>$this->info["codice"],":method"=>$method,":codice_lotto"=>$codice_lotto])->fetch(PDO::FETCH_ASSOC) ?? FALSE;
}
public function updateSimogLog($method,$esito,$msg,$codice_lotto = 0) {
$log = $this->getLastSimogLog($method,$codice_lotto);
if (!empty($log)) {
$log["esito"] = $esito;
$log["msg"] = $msg;
$salva = new salva();
$salva->debug = false;
$salva->nome_tabella = "b_simog_log";
$salva->operazione = "UPDATE";
$salva->oggetto = $log;
$salva->save();
}
}
public function getDati() {
$return = json_decode($this->info["dati"],true);
if (isset($return["importo"]) && $return["importo"] > 0) {
$return["importo"] = number_format($return["importo"],2,".","");
}
return $return;
}
public static function getStati() {
return jsonToArray(__DIR__."/stati.json");
}
public static function getStatiSchede() {
return jsonToArray(__DIR__."/statiSchede.json");
}
public static function getListeSIMOG($type,$data="") {
$return = [];
$version = self::getVersion($data);
$path = self::getVersionPath($version);
if (!empty($path)) {
$path .= "/data/liste-simog.xml";
if (file_exists($path)) {
$schema = simplexml_load_file($path);
$schema = xmlToArray($schema);
if (!empty($schema["schema"]["xsd:simpleType"])) {
$selects = array();
foreach($schema["schema"]["xsd:simpleType"] AS $select) {
$selects[$select["@name"]] = array();
foreach ($select["xsd:restriction"]["xsd:enumeration"] as $value) {
$selects[$select["@name"]][$value["@value"]] = (!is_array($value["xsd:annotation"]["xsd:documentation"])) ? $value["xsd:annotation"]["xsd:documentation"] : implode(" " , $value["xsd:annotation"]["xsd:documentation"]);
if (strpos($selects[$select["@name"]][$value["@value"]],"***") !== false) {
$selects[$select["@name"]][$value["@value"]] = substr($selects[$select["@name"]][$value["@value"]],0,strpos($selects[$select["@name"]][$value["@value"]],"***"));
}
}
}
if (count($selects) > 0) {
$return = $selects;
}
}
}
}
if (!empty($return[$type])) {
return $return[$type];
} else {
return false;
}
}
public function showDateInvito() {
$procedureInvito = self::getOptions("procedure_invito");
if (!empty($procedureInvito)) {
$lotti = $this->getLotti();
foreach($lotti as $lotto) {
if (in_array($lotto["id_scelta_contraente"],$procedureInvito)) {
return true;
}
}
}
return false;
}
public function ignoreDateScadenza() {
$procedureInvito = self::getOptions("procedure_no_scadenza");
if (!empty($procedureInvito)) {
$lotti = $this->getLotti();
foreach($lotti as $lotto) {
if (in_array($lotto["id_scelta_contraente"],$procedureInvito)) {
return true;
}
}
}
return false;
}
public static function getOptions($type,$data="") {
$return = [];
if (strpos($type,".") === false) {
$version = self::getVersion($data);
$path = self::getVersionPath($version);
if (empty($data)) {
$data = time();
} else {
$data = strtotime($data);
}
if (!empty($path)) {
$path .= "/data/{$type}.json";
if (file_exists($path)) {
$options = jsonToArray($path);
if (!empty($options)) {
foreach($options AS $key => $opt) {
if (is_array($opt)) {
$insert = true;
if (!empty($opt["scadenza"])) {
if (strtotime($opt["scadenza"]) < $data) {
$insert = false;
}
}
if ($insert) { $return[$key] = $opt["titolo"]; }
} else {
$return[$key] = $opt;
}
}
}
}
}
}
if (empty($return)) { $return = false; }
return $return;
}
public function getLotti($codice = null) {
$bind = [":codice_simog"=>$this->info["codice"]];
$sql = "SELECT * FROM b_simog_lotti WHERE codice_simog = :codice_simog AND eliminato = 'N' ";
if ($codice !== null) {
$bind[":codice"] = $codice;
$sql .= " AND codice = :codice ";
}
return $this->db->go($sql . " ORDER BY ordinamento, codice",$bind)->fetchAll(PDO::FETCH_ASSOC);
}
public function getRaggruppamento($lotto,$partecipante) {
$bind = [":codice_lotto"=>$lotto,":codice_partecipante"=>$partecipante];
$sql = "SELECT r_raggruppamenti_simog.* FROM r_raggruppamenti_simog JOIN r_partecipanti_simog
ON r_raggruppamenti_simog.codice_padre = r_partecipanti_simog.codice
WHERE r_partecipanti_simog.codice_lotto = :codice_lotto AND r_partecipanti_simog.codice = :codice_partecipante ";
$return = $this->db->go($sql,$bind)->fetchAll(PDO::FETCH_ASSOC);
return $return;
}
public function savePartecipante($partecipante,$codice_lotto) {
if (!empty($this->getLotti($codice_lotto)[0])) {
return self::staticSavePartecipante($partecipante,$codice_lotto);
}
}
private static function staticSavePartecipante($partecipante,$codice_lotto) {
global $pdo;
$partecipante["ruolo"] = (!empty($partecipante["raggruppamento"])) ? "04-CAPOGRUPPO" : "";
$partecipante["codice_lotto"] = $codice_lotto;
$salva = new salva();
$salva->debug = false;
$salva->nome_tabella = "r_partecipanti_simog";
$salva->operazione = empty($partecipante["codice"]) ? "INSERT" : "UPDATE";
$salva->oggetto = $partecipante;
$codice = $salva->save();
if ($codice > 0) {
$pdo->go("DELETE FROM r_raggruppamenti_simog WHERE codice_padre = :codice",[":codice"=>$codice]);
if (!empty($partecipante["raggruppamento"])) {
foreach($partecipante["raggruppamento"] AS $sub) {
$sub["codice_padre"] = $codice;
$salva = new salva();
$salva->debug = false;
$salva->nome_tabella = "r_raggruppamenti_simog";
$salva->operazione = "INSERT";
$salva->oggetto = $sub;
$salva->save();
}
}
return true;
}
return false;
}
public function getPartecipantiLotto($codice_lotto) {
if (!empty($this->getLotti($codice_lotto)[0])) {
$sql = "SELECT * FROM r_partecipanti_simog WHERE r_partecipanti_simog.codice_lotto = :codice_lotto ";
$ooee = $this->db->go($sql,[":codice_lotto"=>$codice_lotto])->fetchAll(PDO::FETCH_ASSOC);
$return = [];
foreach($ooee AS $oe) {
$raggruppamento = $this->getRaggruppamento($codice_lotto,$oe["codice"]);
if (!empty($raggruppamento)) { $oe["raggruppamento"] = $raggruppamento; }
$return[] = $oe;
}
return $return;
}
return false;
}
public static function init($codice) {
if (!empty($codice) && !empty($_SESSION["utente"]) && !empty($_SESSION["ente"])) {
global $pdo;
$bind = [":codice"=>$codice,":codice_gestore"=>$_SESSION["ente"]->codice];
$sql = "SELECT * FROM b_simog WHERE codice = :codice AND codice_gestore = :codice_gestore";
if (!$_SESSION["utente"]->fromHere()) {
$sql .= " AND codice_ente = :codice_ente";
$bind[":codice_ente"] = $_SESSION["utente"]->codice_ente;
}
if ($_SESSION["utente"]->gerarchia > 10) {
$ids = Utente::getPermissionsInModulo($_SESSION["ente"]->codice,$_SESSION["utente"]->codice,"anac");
$ids = implode(",",$ids);
$sql .= " AND b_simog.codice IN ({$ids}) ";
}
$anac = $pdo->go($sql,$bind)->fetch(PDO::FETCH_ASSOC);
if (!empty($anac)) {
$continua = true;
if ($_SESSION["utente"]->codice_ente != $anac["codice_gestore"] && $_SESSION["utente"]->codice_ente != $anac["codice_ente"] && $_SESSION["utente"]->gerarchia > 9) {
$continua = false;
}
if ($continua) {
$instance = new anac();
$instance->info = $anac;
$instance->version = self::getVersion($anac["data_invio_simog"]);
return $instance;
}
}
}
return false;
}
private static function defaultAutenticationLevel() {
return $_SESSION["ente"]->defaultAutenticationLevel();
}
public function checkLock($id_cmd) {
$return = true;
$stato = $this->info["stato"] ?? 0;
if ($_SESSION["utente"]->readOnly) {
return true;
} else {
if (($stato >= 0) && !empty($id_cmd) && $_SESSION["utente"]->unlockSupporto()) {
$cmd = self::getInfoModulo($id_cmd);
$loa = $cmd["authenticationLevel"] ?? self::defaultAutenticationLevel();
if ($_SESSION["utente"]->authenticationLevel < $loa) {
return true;
}
if ($cmd !== false) {
if (($stato >= $cmd["stato_minimo"] || $cmd["stato_minimo"] == null) && ($stato <= $cmd["stato_massimo"] || $cmd["stato_massimo"] === null)) { $return = false; }
}
}
}
return $return;
}
public function saveCUI($lotto,$cui) {
$dati = $this->getLotti($lotto)[0]["dati"] ?? [];
if (!empty($dati)) {
$dati = json_decode($dati,true);
if (empty($dati["cui"])) { $dati["cui"] = []; }
$lastCui = end($dati["cui"]);
$dati["cui"][] = $cui;
$dati = json_encode($dati);
$salva = new salva();
$salva->debug = false;
$salva->nome_tabella = "b_simog_lotti";
$salva->operazione = "UPDATE";
$salva->oggetto = ["codice"=>$lotto,"dati"=>$dati];
if (!empty($salva->save())) {
if (empty($lastCui)) {
$cui = explode("-",$cui);
$cui = $cui[0];
$this->db->go("UPDATE b_simog_schede SET cui = :cui WHERE codice_lotto = :lotto",[":lotto"=>$lotto,":cui"=>$cui]);
}
}
}
}
public function sezioneFromRuolo($ruolo) {
$path = self::getVersionPath($this->version);
if (!empty($path)) {
$path .= "/data/ruoliSezioni.json";
if (file_exists($path)) {
$return = jsonToArray($path);
return $return[$ruolo] ?? false;
}
}
return false;
}
public function setScheda($tipologia,$data) {
$fields = $this->getFieldsScheda($tipologia);
$array = [];
foreach($fields AS $idCampo => $info) {
if ($info["insert"]) {
if(isset($data[$idCampo])) {
if (!empty($info["subElement"])) {
$array[$info["subElement"]][$idCampo] = $data[$idCampo];
} else {
if ($tipologia != "varianti") {
$array[$idCampo] = $data[$idCampo];
} else {
$array["Variante"][$idCampo] = $data[$idCampo];
}
}
}
if ((!isset($array[$idCampo]) || $array[$idCampo] == "") && isset($info["default"])) {
$array[$idCampo] = $info["default"];
}
}
}
return $array;
}
public function getLastCUI($lotto,$full = false) {
$return = 0;
$lottoDB = $this->getLotti($lotto)[0] ?? [];
if (!empty($lottoDB)) {
$dati = json_decode($lottoDB["dati"],true);
if (!empty($dati["cui"])) {
$return = end($dati["cui"]);
if (!$full) {
$return = explode("-",$return);
$return = $return[1];
}
}
}
return $return;
}
public function getSchede($lotto,$type = null) {
$codice_cui = $this->getLastCUI($lotto);
$return = false;
if ($codice_cui !== false) {
$bind = array(":codice_lotto"=>$lotto,":cui"=>$codice_cui);
$sql = "SELECT * FROM b_simog_schede WHERE cui = :cui AND codice_lotto = :codice_lotto AND codice NOT IN (SELECT codice_padre FROM b_simog_schede) ";
if (!empty($type)) {
$bind[":type"] = $type;
$sql .= " AND tipologia= :type ";
}
$risultato = $this->db->go($sql,$bind);
if ($risultato->rowCount() > 0) {
$tmp = $risultato->fetchAll(PDO::FETCH_ASSOC);
foreach($tmp AS $scheda) {
if ($scheda["stato"] <= 97) {
if (!is_array($return)) { $return = array(); }
$scheda["json"] = json_decode($scheda["json"],true);
$return[] = $scheda;
}
}
}
}
return $return;
}
public static function getAppaloType(array $lotto) : string {
$appaltoType="";
if (!empty($lotto["dati"]["flag_escluso"]) && $lotto["dati"]["flag_escluso"] == "S") {
$appaltoType = "Escluso";
} else if (!empty($lotto["dati"]["cig_acc_quadro"])) {
$appaltoType = "Adesione";
} else {
$appaltoType = "Aggiudicazione";
}
return $appaltoType;
}
public function getFieldsScheda($type) {
$path = self::getVersionPath($this->version);
if (!empty($path)) {
$path .= "/data/schedeLoaderFields.json";
if (file_exists($path)) {
$return = jsonToArray($path);
if (!empty($return[$type])) {
return $return[$type];
}
}
}
return false;
}
public function saveSchede($lotto,$schede,$tipologia,$stato) {
$errori = [];
$salva = new salva();
$salva->debug = false;
$salva->nome_tabella = "b_simog_schede";
$salva->operazione = "INSERT";
$codice_cui = $this->getLastCUI($lotto);
$schede_inserite = $this->getSchede($lotto,$tipologia);
if ($codice_cui !== false) {
foreach($schede AS $id => $scheda) {
$continua = true;
$errori = [];
if (!empty($scheda["id_locale"]) && !empty($schede_inserite)) {
foreach($schede_inserite AS $existent) {
if ($existent["id_locale"] == $scheda["id_locale"]) {
$json = convertDate_SIMOG($scheda["json"]);
$json = json_encode($json);
if($json == $existent["json"] && ! in_array($existent["stato"], array(0, 5))) $continua = false;
}
}
}
if ($continua) {
$scheda["codice_lotto"] = $lotto;
$scheda["tipologia"] = $tipologia;
$scheda["stato"] = $stato;
$scheda["cui"] = $codice_cui;
$scheda["codice_padre"] = (is_numeric($id)) ? $id : 0;
$scheda["json"] = convertDate_SIMOG($scheda["json"]);
if (!empty($scheda["json"]["ID_SCHEDA_SIMOG"])) { $scheda["response_id"] = $scheda["json"]["ID_SCHEDA_SIMOG"]; }
if ($tipologia=="varianti" && !empty($scheda["json"]["Motivi"])) {
$motivi = array();
foreach($scheda["json"]["Motivi"] AS $motivo) {
$motivi[] = array("ID_MOTIVO_VAR"=>$motivo);
}
$scheda["json"]["Motivi"] = $motivi;
}
$scheda["json"] = json_encode($scheda["json"]);
$salva->oggetto = $scheda;
$codice = $salva->save();
if ($codice !== false) {
if (empty($scheda["id_locale"])) {
$salva->operazione = "UPDATE";
$salva->oggetto = array("codice"=>$codice,"id_locale"=>$codice);
$codice = $salva->save();
$salva->operazione = "INSERT";
}
if(in_array($scheda["tipologia"], array('finanziamenti', 'requisiti', 'comuni')) && $stato > 0) {
$_exclude_codes = "";
$codici_padri = $this->db->go('SELECT codice_padre FROM b_simog_schede WHERE tipologia = "appalto" AND codice_lotto = :codice_lotto AND cui = :cui', array(':codice_lotto' => $scheda["codice_lotto"], ':cui' => $scheda["cui"]))->fetchAll(PDO::FETCH_COLUMN, 0);
if(! empty($codici_padri)) $_exclude_codes = 'AND codice NOT IN ('.implode(',', $codici_padri).')';
$this->db->go('UPDATE b_simog_schede SET stato = 1 WHERE codice_lotto = :codice_lotto AND cui = :cui AND tipologia = "appalto" '.$_exclude_codes.' AND stato > 1 AND stato < 97',array(':codice_lotto' => $scheda["codice_lotto"], ':cui' => $scheda["cui"]));
}
} else {
$errori[] = $id;
}
}
}
} else {
$errori[] = __("CUI Sconosciuto");
}
return $errori;
}
public function listSchede($lotto=null,$all=false) {
$path = self::getVersionPath($this->version);
$return = false;
if (!empty($path)) {
$path .= "/data/schedeLoader.json";
if (file_exists($path)) {
$return = jsonToArray($path);
if (!empty($return)) {
if (!$all && !empty($lotto)) {
$lotto = $this->getLotti($lotto)[0] ?? null;
if (!empty($lotto)) {
$lotto["dati"] = json_decode($lotto["dati"],true);
$aggiudicata = false;
$appaltoType="";
$datiComuni = $this->getSchede($lotto["codice"],"comuni");
if (!empty($datiComuni[0]) && $datiComuni[0]["stato"] == 3) {
$json = $datiComuni[0]["json"] ?? null;
if ($json["ESITO_PROCEDURA"]==="1") {
$aggiudicata = true;
}
$tipologia_procedura = $datiComuni["TIPOLOGIA_PROCEDURA"] ?? 0;
}
$appaltoType = anac::getAppaloType($lotto);
$tmp = $return;
$return = [];
foreach($tmp AS $id_scheda => $scheda) {
if (!$scheda["aggiudicazione"] || ($scheda["aggiudicazione"] && $aggiudicata)) {
if (empty($scheda["appalto"]) || (!empty($scheda["appalto"]) && in_array($appaltoType,$scheda["appalto"])!== false)) {
$return[$id_scheda] = $scheda;
}
}
}
}
}
}
}
}
return $return;
}
static function getModuli($smart="N",$stato = null) {
global $config;
$moduli = jsonToArray($config["jsonFolder"]."/anac-panel.json");
$tmp = [];
foreach($moduli AS $id_modulo => $modulo) {
$insert = true;
if (!$modulo["smart"]) {
if ($smart == "S") $insert = false;
}
if ($insert && $stato !== null) {
if ($modulo["stato_minimo"] > $stato) { $insert = false; }
}
if (is_array($modulo["stati_esclusi"]) && in_array($stato,$modulo["stati_esclusi"]) !== false) { $insert = false; }
if ($insert)$tmp[$id_modulo] = $modulo;
$moduli = $tmp;
}
return $moduli;
}
static function getInfoModulo($radice) {
$return = false;
if (!empty($radice)) {
$moduli = self::getModuli(false);
if (!empty($moduli[$radice])) {
$return = $moduli[$radice];
$return["id"] = $radice;
$return["titolo"] = __($return["titolo"]);
$return["descrizione"] = __($return["descrizione"]);
}
}
return $return;
}
static function massiveImport($ente,$anno_riferimento,$lotti) {
global $pdo;
$errors = [];
$ris_check_lotto = $pdo->prepare("SELECT b_simog_lotti.codice FROM b_simog_lotti JOIN b_simog ON b_simog_lotti.codice_simog = b_simog.codice
WHERE b_simog_lotti.cig = :cig AND b_simog.codice_gestore = :codice_ente ");
$ris_check_lotto->bindValue(':codice_ente',$ente);
$ris_insert_simog = $pdo->prepare("INSERT INTO b_simog (codice_ente,codice_gestore,stato,smart_cig,oggetto,anno_riferimento,utente_modifica)
VALUES (:codice_ente,:codice_ente,0,:smart_cig,:oggetto,:anno,:utente_modifica)");
$delete_simog = $pdo->prepare("INSERT INTO b_simog (codice_ente,codice_gestore,stato,smart_cig,oggetto,anno_riferimento,utente_modifica)
VALUES (:codice_ente,:codice_ente,0,:smart_cig,:oggetto,:anno,:utente_modifica)");
$ris_insert_simog->bindValue(':anno',$anno_riferimento);
$ris_insert_simog->bindValue(':codice_ente',$ente);
$ris_insert_simog->bindValue(':utente_modifica',$_SESSION["utente"]->codice);
$ris_insert_lotto_simog = $pdo->prepare("INSERT INTO b_simog_lotti (codice_simog,cig,id_scelta_contraente,oggetto,190data_inizio,190data_fine,190importoAggiudicato,190liquidazioni,utente_modifica)
VALUES (:codice_simog,:cig,:id_scelta_contraente,:oggetto,:190data_inizio,:190data_fine,:190importoAggiudicato,:190liquidazioni,:utente_modifica)");
$ris_insert_lotto_simog->bindValue(':utente_modifica',$_SESSION["utente"]->codice);
$ris_insert_partecipanti = $pdo->prepare("INSERT INTO r_partecipanti_simog (codice_lotto,codice_fiscale,ragione_sociale,stato,aggiudicatario,utente_modifica)
VALUES (:codice_lotto,:codice_fiscale,:ragione_sociale,:stato,:aggiudicatario,:utente_modifica)");
$ris_insert_partecipanti->bindValue(':utente_modifica',$_SESSION["utente"]->codice);
$ris_insert_raggruppamento = $pdo->prepare("INSERT INTO r_raggruppamenti_simog (codice_padre,codice_fiscale_impresa,ragione_sociale,stato,ruolo,utente_modifica)
VALUES (:codice_padre,:codice_fiscale,:ragione_sociale,:stato,:ruolo,:utente_modifica)");
$ris_insert_raggruppamento->bindValue(':utente_modifica',$_SESSION["utente"]->codice);
foreach($lotti AS $lotto) {
if (isset($lotto["cig"])) {
$smart = (is_numeric(substr($lotto["cig"], 0, 1))) ? "N" : "S";
$ris_check_lotto->bindValue(":cig",$lotto["cig"]);
$ris_check_lotto->execute();
if ($ris_check_lotto->rowCount() == 0 || str_replace("0","",$lotto["cig"]) == "") {
$lotto_simog = [];
if ($smart == "S") {
$source = "raccordoSmart190.json";
} else {
$source = "raccordoRegular190.json";
}
$lotto["id_scelta_contraente"] = "";
if (!empty($source)) {
$version = self::getVersion();
$path = self::getVersionPath($version);
if (!empty($path)) {
$path .= "/data/{$source}";
if (file_exists($path)) {
$options = jsonToArray($path);
if (!empty($options)) {
foreach($options AS $id => $value) {
$value = strtolower(preg_replace("/[^A-Za-z0-9]/", '', $value));
$current = strtolower(preg_replace("/[^A-Za-z0-9]/", '', $lotto["sceltaContraente"]));
if ($value == $current) {
$lotto["id_scelta_contraente"] = $id;
break;
}
}
}
}
}
}
if (!empty($lotto["id_scelta_contraente"])) {
$ris_insert_simog->bindValue(':smart_cig',$smart);
$ris_insert_simog->bindValue(':oggetto',$lotto["oggetto"]);
$ris_insert_simog->execute();
$codice_simog = $pdo->lastInsertId();
$lotto_simog[':codice_simog'] = $codice_simog;
$lotto_simog[':oggetto'] = $lotto["oggetto"];
$lotto_simog[':cig'] = $lotto["cig"];
$lotto_simog[':190data_inizio'] = null;
$lotto_simog[':190data_fine'] = null;
$lotto_simog[':190importoAggiudicato'] = null;
$lotto_simog[':190liquidazioni'] = null;
$lotto_simog[":id_scelta_contraente"] = $lotto["id_scelta_contraente"];
if (!empty($lotto["tempiCompletamento"]["dataInizio"])) { $lotto_simog[':190data_inizio'] = $lotto["tempiCompletamento"]["dataInizio"]; }
if (!empty($lotto["tempiCompletamento"]["dataUltimazione"])) { $lotto_simog[':190data_fine'] = $lotto["tempiCompletamento"]["dataUltimazione"]; }
if (!empty($lotto["importoAggiudicazione"])) { $lotto_simog[':190importoAggiudicato'] = $lotto["importoAggiudicazione"]; }
if (!empty($lotto["importoSommeLiquidate"])) { $lotto_simog[':190liquidazioni'] = $lotto["importoSommeLiquidate"]; }
foreach($lotto_simog AS $chiave => $valore) $ris_insert_lotto_simog->bindValue($chiave,$valore);
$ris_insert_lotto_simog->execute();
$codice_lotto = $pdo->lastInsertId();
if (!empty($lotto["partecipanti"])) {
$aggiudicatari = [];
if (isset($lotto["aggiudicatari"])) {
if (isset($lotto["aggiudicatari"]["aggiudicatario"])) {
$tmp_aggiudicatari = $lotto["aggiudicatari"]["aggiudicatario"];
if (isset($tmp_aggiudicatari["ragioneSociale"])) { $tmp_aggiudicatari = [$tmp_aggiudicatari]; }
foreach($tmp_aggiudicatari AS $tmp_agg) {
if (isset($tmp_agg["codiceFiscale"])) {
$aggiudicatari[] = $tmp_agg["codiceFiscale"];
} else if (isset($tmp_agg["identificativoFiscaleEstero"])) {
$aggiudicatari[] = $tmp_agg["identificativoFiscaleEstero"];
}
}
}
if (isset($lotto["aggiudicatari"]["aggiudicatarioRaggruppamento"])) {
$tmp_aggiudicatari = $lotto["aggiudicatari"]["aggiudicatarioRaggruppamento"];
if (isset($tmp_aggiudicatari["membro"])) { $tmp_aggiudicatari = [$tmp_aggiudicatari]; }
foreach($tmp_aggiudicatari AS $tmp_gruop) {
foreach($tmp_gruop["membro"] AS $tmp_agg) {
if (isset($tmp_agg["codiceFiscale"])) {
$aggiudicatari[] = $tmp_agg["codiceFiscale"];
} else if (isset($tmp_agg["identificativoFiscaleEstero"])) {
$aggiudicatari[] = $tmp_agg["identificativoFiscaleEstero"];
}
}
}
}
}
if (isset($lotto["partecipanti"]["partecipante"])) {
$partecipanti = $lotto["partecipanti"]["partecipante"];
if (isset($partecipanti["ragioneSociale"])) { $partecipanti = [$partecipanti]; }
foreach($partecipanti AS $partecipante) {
if (!empty($partecipante["codiceFiscale"]) || !empty($partecipante["identificativoFiscaleEstero"])) {
$codice_partecipante = 0;
$primo = "N";
$stato = "IT";
$ris_insert_partecipanti->bindValue(':codice_lotto',$codice_lotto);
if (isset($partecipante["codiceFiscale"])) {
$ris_insert_partecipanti->bindValue(':codice_fiscale',$partecipante["codiceFiscale"]);
if (in_array($partecipante["codiceFiscale"], $aggiudicatari) !== false) { $primo = "S"; }
} else {
$ris_insert_partecipanti->bindValue(':codice_fiscale',"");
}
if (isset($partecipante["identificativoFiscaleEstero"])) {
$stato = "EE";
$ris_insert_partecipanti->bindValue(':codice_fiscale',$partecipante["identificativoFiscaleEstero"]);
if (in_array($partecipante["identificativoFiscaleEstero"], $aggiudicatari) !== false) { $primo = "S"; }
}
$ris_insert_partecipanti->bindValue(':ragione_sociale',$partecipante["ragioneSociale"]);
$ris_insert_partecipanti->bindValue(':stato',$stato);
$ris_insert_partecipanti->bindValue(':aggiudicatario',$primo);
$ris_insert_partecipanti->execute();
}
}
}
if (isset($lotto["partecipanti"]["raggruppamento"])) {
$raggruppamenti = $lotto["partecipanti"]["raggruppamento"];
if (isset($raggruppamenti["membro"])) { $raggruppamenti = [$raggruppamenti]; }
foreach($raggruppamenti AS $group) {
foreach($group["membro"] AS $partecipante) {
if (!empty($partecipante["codiceFiscale"]) || !empty($partecipante["identificativoFiscaleEstero"])) {
if (isset($partecipante["ruolo"])) {
if ($partecipante["ruolo"] == "04-CAPOGRUPPO") { $codice_padre = 0; }
if (empty($codice_padre)) {
$ris_insert = $ris_insert_partecipanti;
$ris_insert->bindValue(':codice_lotto',$codice_lotto);
} else {
$ris_insert = $ris_insert_raggruppamento;
$ris_insert->bindValue(':codice_padre',$codice_padre);
$ris_insert->bindValue(':ruolo',$partecipante["ruolo"]);
}
$primo = "N";
$stato = "IT";
if (isset($partecipante["codiceFiscale"])) {
$ris_insert->bindValue(':codice_fiscale',$partecipante["codiceFiscale"]);
if (in_array($partecipante["codiceFiscale"], $aggiudicatari) !== false) { $primo = "S"; }
} else {
$ris_insert->bindValue(':codice_fiscale',"");
}
if (isset($partecipante["identificativoFiscaleEstero"])) {
$stato = "EE";
$ris_insert->bindValue(':codice_fiscale',$partecipante["identificativoFiscaleEstero"]);
if (in_array($partecipante["identificativoFiscaleEstero"], $aggiudicatari) !== false) { $primo = "S"; }
}
$ris_insert->bindValue(':ragione_sociale',$partecipante["ragioneSociale"]);
if (empty($codice_padre)) { $ris_insert->bindValue(':aggiudicatario',$primo); }
$ris_insert->bindValue(':stato',$stato);
$ris_insert->execute();
if ($partecipante["ruolo"] == "04-CAPOGRUPPO") { $codice_padre = $pdo->lastInsertId(); }
}
}
}
}
}
}
} else {
$errors[] = $lotto["cig"] . ": " . __("Impossibile risalire alla scelta del contraente");
}
} else {
$errors[] = $lotto["cig"] . ": " . __("Già presente");
}
} else {
$errors[] = "Verificare XML";
break;
}
}
return $errors;
}
public function printSuggerimenti($cig,$key) {
$suggerimenti = $this->suggerimenti($cig);
if (!empty($suggerimenti[$key])) {
include(__DIR__."/views/suggerimenti.php");
}
}
public function printSuggerimentiMultipli($cig,$key) {
$suggerimenti = $this->suggerimenti($cig);
if (!empty($suggerimenti[$key])) {
include(__DIR__."/views/suggerimentiMultipli.php");
}
}
public function printButtonSuggerimenti() {
include __DIR__."/views/buttonSuggerimenti.php";
}
private static function moduliSuggerimenti() {
$moduli = [];
$moduli["gare"] = ["titolo"=>"Gare e affidamenti","url"=>"/backend/gare/anac/suggestions.php","lost"=>"/backend/gare/anac/lost.php"];
$moduli["concorsi"] = ["titolo"=>"Concorsi","url"=>"/backend/concorsi/anac/suggestions.php","lost"=>"/backend/concorsi/anac/lost.php"];
$moduli["pcp"] = ["titolo"=>"PCP","url"=>"/backend/pcp/anac/suggestions.php","lost"=>"/backend/pcp/anac/lost.php"];
$moduli["stipula"] = ["titolo"=>"Stipula Contratti","url"=>"/backend/contratti/anac/suggestions.php","lost"=>"/backend/contratti/anac/lost.php"];
return $moduli;
}
public static function getProcedureMancanti(bool $bindLostElements = false) {
$moduli = self::moduliSuggerimenti();
global $pdo;
global $root;
$return = [];
if (!empty($moduli)) {
if ($bindLostElements) {
ini_set("max_execution_time",600);
}
$checkExistingLotto = $pdo->prepare("SELECT codice, codice_simog FROM b_simog_lotti WHERE cig = :cig");
foreach($moduli AS $idModulo => $data) {
$_getProcedureMancantiAnac = true;
if (!empty($data["lost"])) {
$path = $root . $data["lost"];
if (file_exists($path)) {
include($path);
}
}
}
}
return $return;
}
public static function importazioneSuggerimenti190Lotto(array $lotto, bool $force = false): Int {
$aggiornamenti = 0;
if (!empty($lotto["cig"])) {
$simog = self::init($lotto["codice_simog"]);
$suggerimenti = $simog->suggerimenti($lotto["cig"]);
if (!empty($suggerimenti)) {
if (!empty($suggerimenti["190"])) {
foreach($suggerimenti["190"] AS $suggerimento) {
if (!empty($suggerimento["data"])) {
foreach($suggerimento["data"] AS $key => $value) {
if (!empty($value) && (empty($lotto[$key]) || $force)) {
$aggiornamenti++;
self::updateDati190($lotto["cig"],$key,$value);
}
}
}
}
}
if (!empty($suggerimenti["partecipanti"])) {
foreach($suggerimenti["partecipanti"] AS $suggerimento) {
$partecipantiSimog = $simog->getPartecipantiLotto($lotto["codice"]);
if (!empty($suggerimento["data"])) {
if (empty($partecipantiSimog) || $force) {
$aggiornamenti++;
self::updatePartecipanti190($lotto["cig"],$suggerimento["data"]);
} else if (!empty($partecipantiSimog)) {
foreach($suggerimento["data"] AS $partecipanteSuggestion) {
$found = false;
foreach($partecipantiSimog AS $partecipanteSimog) {
if (strpos($partecipanteSimog["codice_fiscale"],$partecipanteSuggestion["codice_fiscale"]) !== false || strpos($partecipanteSuggestion["codice_fiscale"],$partecipanteSimog["codice_fiscale"]) !== false) {
$found = true;
$partecipanteSimog["aggiudicatario"] = $partecipanteSuggestion["aggiudicatario"];
self::staticSavePartecipante($partecipanteSimog,$lotto["codice"]);
}
}
if (!$found) {
self::staticSavePartecipante($partecipanteSuggestion,$lotto["codice"]);
}
}
}
}
}
}
}
}
return $aggiornamenti;
}
public static function importazioneMassivaSuggerimenti190(int $anno = null,int $ente = null,bool $force=false) {
global $pdo;
$aggiornamenti = 0;
$bind = [];
$sql = "SELECT codice FROM b_simog WHERE codice > 0 ";
if (!empty($anno)) {
$sql .= " AND anno_riferimento = :anno";
$bind[":anno"] = $anno;
}
if (!empty($ente)) {
$sql .= " AND codice_ente = :ente";
$bind[":ente"] = $ente;
}
$gare = $pdo->go($sql,$bind)->fetchAll(PDO::FETCH_COLUMN);
if (!empty($gare)) {
foreach($gare AS $gara) {
$simog = self::init($gara);
if (!empty($simog)) {
$lotti = $simog->getLotti();
if (!empty($lotti)) {
foreach($lotti AS $lotto) {
$aggiornamenti += self::importazioneSuggerimenti190Lotto($lotto,$force);
}
}
}
}
}
return $aggiornamenti;
}
public function suggerimenti($cig) {
$data = [];
if (strlen($cig) == 10 && $cig != "0000000000") {
global $root;
$moduli = self::moduliSuggerimenti();
if (!empty($moduli)) {
foreach($moduli AS $modulo => $info) {
if (!empty($info["url"])) {
if (file_exists($root.$info["url"])) {
include($root.$info["url"]);
}
}
}
}
}
return $data;
}
public static function updatePartecipanti190($cig,$partecipanti) {
global $pdo;
$lotti = $pdo->go("SELECT b_simog_lotti.codice FROM b_simog_lotti JOIN b_simog ON b_simog_lotti.codice_simog = b_simog.codice WHERE b_simog_lotti.cig = :cig ",[":cig"=>$cig]);
if ($lotti->rowCount() > 0) {
$delete = $pdo->prepare("DELETE FROM r_partecipanti_simog WHERE codice_lotto = :lotto");
while($lotto = $lotti->fetch(PDO::FETCH_ASSOC)) {
$lotto = $lotto["codice"];
$delete->bindValue(":lotto",$lotto);
$delete->execute();
if (!empty($partecipanti)) {
foreach($partecipanti AS $partecipante) {
self::staticSavePartecipante($partecipante,$lotto);
}
}
}
}
}
public static function updateDati190($cig,$chiave,$valore) {
global $pdo;
$keys = ["190data_inizio","190data_fine","190importoAggiudicato","190liquidazioni"];
if (in_array($chiave,$keys) !== false) {
$lotti = $pdo->go("SELECT b_simog_lotti.codice FROM b_simog_lotti JOIN b_simog ON b_simog_lotti.codice_simog = b_simog.codice WHERE b_simog_lotti.cig = :cig ",[":cig"=>$cig]);
if ($lotti->rowCount() > 0) {
while($lotto = $lotti->fetch(PDO::FETCH_ASSOC)) {
if ($chiave == "190importoAggiudicato" || $chiave == "190liquidazioni") {
$valore = number_format($valore,2,".","");
}
$lotto[$chiave] = $valore;
$salva = new salva();
$salva->debug = false;
$salva->nome_tabella = "b_simog_lotti";
$salva->operazione = "UPDATE";
$salva->oggetto = $lotto;
$salva->save();
}
}
}
}
}