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.
 
 
 
 
 

642 righe
24 KiB

<?
class mop {
public $progetto;
public $codice_sistema;
public $codice_locale;
private $db;
public static function tipologiaSchede() {
return jsonToArray(__DIR__."/schede.json");
}
public function __construct(Progetto $progetto)
{
global $pdo;
$this->progetto = $progetto;
$this->ente = Ente::getInfoFromID($progetto->info["codice_ente"])[0];
$this->codice_sistema = "01850570746TUTTOGARE";
$this->codice_locale = $this->ente["cf"] . $this->codice_sistema . ($this->progetto->info["id_monitoraggio"] ?? $this->progetto->info["codice"]);
$this->db = $pdo;
}
public function sendPack($codice,$username,$password) {
$pack = $this->getPacks($codice);
if (!empty($pack)) {
$ftp_connection = ftp_connect("sts.mef.gov.it");
if ($ftp_connection) {
if (@ftp_login($ftp_connection,$username,$password)) {
$local_path = $this->getPackStoreDir() . "/".$pack["nome"];
$remote_path = "Outbox/".$pack["nome"];
if (ftp_put($ftp_connection, $remote_path, $local_path, FTP_BINARY)) {
ftp_close($ftp_connection);
$this->db->go("UPDATE b_progetti_mop_pack SET timestamp_invio = CURRENT_TIMESTAMP, utente_invio = :utente WHERE codice = :codice",[":codice"=>$pack["codice"],":utente"=>$_SESSION["utente"]->codice??-1]);
self::updateStato($pack["codice"],50);
return true;
}
}
}
}
}
public function downloadEsito($codice,$username,$password) {
$pack = $this->getPacks($codice);
if (!empty($pack)) {
$ftp_connection = ftp_connect("sts.mef.gov.it");
if ($ftp_connection) {
if (@ftp_login($ftp_connection,$username,$password)) {
$local_path = $this->getPackStoreDir() . "/".$pack["nome"] . "-esito.txt";
$remote_path = "Inbox/".$pack["nome"];
if (ftp_get($ftp_connection, $local_path, $remote_path, FTP_BINARY)) {
ftp_close($ftp_connection);
if (file_exists($local_path)) {
return file_get_contents($local_path);
}
}
}
}
}
}
public function checkPack($codice,$esito) {
$pack = $this->getPacks($codice);
if (!empty($pack) && !empty($esito)) {
$file_lines = preg_split("/((\r?\n)|(\r\n?))/", $esito);
$errors = array();
$line_errors = array();
$line = 0;
$file_lines = array_filter($file_lines);
$mismatch = false;
if (!empty($file_lines)) {
foreach($file_lines AS $report) {
$line++;
$string = explode("|",$report);
$nome_file = substr($string[0], strpos($string[0], '_')+1);
if ($nome_file != $pack["nome"]) {
$error = array();
$error["livello"] = "ERRORE";
$error["codice_errore"] = "0x12";
$error["descrizione_errore"] = "Il report alla riga " . $line . " non &egrave; riferito al tracciato selezionato";
$errors[] = $error;
$mismatch = true;
break;
} else {
if ($string[1]!="FNFOK") {
$error = array();
$error["livello"] = "ERRORE";
$error["codice_errore"] = $string[1];
$error["descrizione_errore"] = $string[2] . " - Riga (".$string[5].")";
$error["scheda"] = $string[4];
if (empty($line_errors[$string[5]])) {
$line_errors[$string[5]] = array();
}
$errors[] = $error;
$line_errors[$string[5]][] = $error;
}
}
}
if ($mismatch) {
return false;
} else {
$matches = json_decode($pack["matches"],TRUE);
if (!empty($errors)) {
if (count($line_errors) < count($matches)) {
$stato = 95;
} else {
$stato = 90;
}
} else {
$stato = 100;
}
self::updateStato($pack["codice"],$stato,json_encode($errors));
if (!empty($matches) && !empty($line_errors)) {
$upd = $this->db->prepare("UPDATE b_progetti_monitoraggio SET stato = :stato, errori = :errori WHERE codice = :codice");
$updTrasmesso = $this->db->prepare("UPDATE b_progetti_monitoraggio SET trasmesso = 1 WHERE codice = :codice");
foreach($matches AS $line => $code) {
$upd->bindValue(":codice",$code);
$upd->bindValue(":errori","");
$updTrasmesso->bindValue(":codice",$code);
if (!empty($line_errors[$line])) {
$upd->bindValue(":stato",90);
$upd->bindValue(":errori",json_encode($line_errors[$line]));
$upd->execute();
} else {
$upd->bindValue(":stato",100);
$upd->execute();
$updTrasmesso->execute();
}
}
}
}
}
}
}
public static function updateStato($codice,$stato,$errori = "") {
global $pdo;
$pack = $pdo->go("SELECT * FROM b_progetti_mop_pack WHERE codice = :codice",[":codice"=>$codice])->fetch(PDO::FETCH_ASSOC);
if (!empty($pack)) {
$matches = json_decode($pack["matches"],true);
if (!empty($matches)) {
$upd = $pdo->prepare("UPDATE b_progetti_monitoraggio SET stato = :stato WHERE codice = :codice");
$upd->bindValue(":stato",$stato);
foreach($matches AS $match) {
$upd->bindValue(":codice",$match);
$upd->execute();
}
}
$pdo->go("UPDATE b_progetti_mop_pack SET stato = :stato, esito = :esito WHERE codice = :codice",[":codice"=>$codice,":stato"=>$stato,":esito"=>$errori]);
return true;
}
if (isset($_SESSION["ente"])) {
$_SESSION["ente"]->manageUpdates("mop",$codice);
}
}
public function getPackStoreDir() {
global $config;
return $config["mediaFolder"]."/{$_SESSION["ente"]->codice}/progetti/mop/{$this->progetto->info["codice_ente"]}/{$this->progetto->info["codice"]}";
}
public function createPack($codici) {
if (!empty($codici)) {
$tmp = $this->getScheda();
$schede = [];
foreach($tmp AS $scheda) {
if (in_array($scheda["codice"],$codici) && ($scheda["stato"] == 0 || $scheda["stato"] == 1 || $scheda["stato"] == 2)) {
$schede[] = ["tipo"=>$scheda["tipo"],"codice"=>$scheda["codice"],"stato"=>$scheda["stato"],"data"=>$scheda["data"]];
}
}
if (!empty($schede)) {
$date = date("dmY_H.i.s");
$matches = [];
$tracciato = "HH#".count($schede)."#".date("dmY")."#".PHP_EOL;
$count = 1;
foreach($schede AS $scheda) {
$matches[$count] = $scheda["codice"];
switch($scheda["stato"]) {
case 1: $stato = "M"; break;
case 2: $stato = "C"; break;
default: $stato = "I";
}
$row = substr($scheda["tipo"],0,3) . "#" . $count . "#" . $this->codice_sistema . "#" . $scheda["data"] . "#" . $stato . "#";
$tracciato .= $row . PHP_EOL;
$count++;
}
$tracciato .= "FF#";
$dir = $this->getPackStoreDir();
$filename= "dati_attuazione" . (($_SESSION["ente"]->getInfo()["ambienteTest"] == "S") ? "_test" : "") . "_{$this->codice_sistema}_{$date}.txt";
if (!is_dir($dir)) {
mkdir($dir,0777,true);
}
if (file_put_contents($dir."/".$filename,$tracciato)) {
$pack = [];
$pack["codice_progetto"] = $this->progetto->info["codice"];
$pack["stato"] = 10;
$pack["nome"] = $filename;
$pack["matches"] = json_encode($matches);
$pack["utente_creazione"] = $_SESSION["utente"]->codice ?? -1;
$pack["timestamp_creazione"] = date("d/m/Y H:i:s");
$salva = new salva();
$salva->debug = false;
$salva->nome_tabella = "b_progetti_mop_pack";
$salva->operazione = "INSERT";
$salva->oggetto = $pack;
if ($salva->save()) {
$updateStato = $this->db->prepare("UPDATE b_progetti_monitoraggio SET stato = 10 WHERE codice = :codice");
foreach($matches AS $codice) {
$updateStato->bindValue(":codice",$codice);
$updateStato->execute();
}
return true;
}
}
}
}
}
public static function stati() {
return [
0 => [
"label" => "Pronto",
"color" => "#ffd700"
],
1 => [
"label" => "Da aggiornare",
"color" => "#ffeA00"
],
2 => [
"label" => "Da cancellare",
"color" => "#CC2200"
],
10 => [
"label" =>"Da inviare",
"color" => "#40e0d0"
],
50 => [
"label" =>"In valutazione",
"color" => "#98fb98"
],
60 => [
"label" =>"Da controllare",
"color" => "#ff8c00"
],
90 => [
"label" =>"Respinto",
"color" => "#ff0000"
],
95 => [
"label" =>"Trasmesso con errori",
"color" => "#ff5500"
],
100 => [
"label" => "Trasmesso",
"color" => "#9acd32"
]
];
}
public function checkTimingVerifica($pack) {
if ($pack["stato"] == 50 && strtotime($pack["timestamp_invio"]) < strtotime("-2 days")) {
mop::updateStato($pack["codice"],60);
return true;
}
return false;
}
public function getPacks($codice = null) {
$bind = [":progetto" => $this->progetto->info["codice"]];
$sql = "SELECT * FROM b_progetti_mop_pack WHERE codice_progetto = :progetto ";
if (!empty($codice)) {
$sql .= " AND codice = :codice ";
$bind[":codice"] = $codice;
}
$sql .= " ORDER BY codice DESC";
$tmp = $this->db->go($sql,$bind);
if (!empty($tmp->rowCount())) {
$return = [];
while($pack = $tmp->fetch(PDO::FETCH_ASSOC)) {
if ($this->checkTimingVerifica($pack)) {
$pack["stato"] = 60;
}
if (empty($codice)) {
$return[] = $pack;
} else {
$return = $pack;
}
}
return $return;
}
}
public function getScheda($tipologia = null) {
$bind = [":progetto"=>$this->progetto->info["codice"]];
$sql = "SELECT * FROM b_progetti_monitoraggio WHERE codice_progetto = :progetto ";
if (!empty($tipologia)) {
$bind[":tipo"] = $tipologia;
$sql .= " AND tipo = :tipo";
}
return $this->db->go($sql,$bind)->fetchAll(PDO::FETCH_ASSOC);
}
public function getA0() {
return [$this->progetto->info["cup"],strip_tags($this->progetto->info["oggetto"])];
}
public function getA9() {
$dotazioni = $this->progetto->getDotazione();
$return = [];
if (!empty($dotazioni)) {
foreach($dotazioni AS $dotazione) {
$dotazioneInfo = json_decode($dotazione["extraInfo"],true);
$row = [];
$row[] = $dotazioneInfo["fonte"] ?? "007";
$row[] = number_format($dotazione["importo"],2,".","");
$row[] = (!empty($dotazioneInfo["numeroNorma"])) ? $dotazioneInfo["numeroNorma"] : "99999";
$row[] = (!empty($dotazioneInfo["annoNorma"])) ? $dotazioneInfo["annoNorma"] : "99999";
$row[] = (!empty($dotazioneInfo["tipo"])) ? $dotazioneInfo["tipo"] : "99999";
$row[] = (!empty($dotazioneInfo["numeroDeliberaCipe"])) ? $dotazioneInfo["numeroDeliberaCipe"] : "99999";
$row[] = (!empty($dotazioneInfo["annoDeliberaCipe"])) ? $dotazioneInfo["annoDeliberaCipe"] : "99999";
$row[] = (!empty($dotazioneInfo["estremiProvvedimento"])) ? $dotazioneInfo["estremiProvvedimento"] : "99999";
$row[] = $dotazioneInfo["istat"] ?? ""; // REGIONE
$row[] = $dotazioneInfo["istat"] ?? ""; // PROVINCIA
$row[] = $dotazioneInfo["istat"] ?? ""; // COMUNE
$row[] = $dotazioneInfo["descrizioneCofinanziatore"] ?? "";
$row[] = $dotazioneInfo["codiceFiscaleCofinanziatore"] ?? "";
$row[] = $dotazioneInfo["criticita"] ?? "";
$row[] = $dotazioneInfo["economie"] > 0 ? "S" : "N";
$row["_idRiferimento"] = $dotazione["codice"];
$return[] = $row;
}
}
return $return;
}
public function getA31() {
$return = [];
if (!empty($this->progetto->info["cup"])) {
$dotazioni = $this->progetto->getDotazione();
if (!empty($dotazioni)) {
foreach($dotazioni AS $dotazione) {
if (!empty($dotazione["codice_immobile"])) {
$immobile = progetto::getImmobili(null,$dotazione["codice_immobile"]);
$extraInfo = json_decode($immobile["extraInfo"],true);
if (!empty($extraInfo["miur"])) {
$row = [];
$row["cup"] = $this->progetto->info["cup"];
$row["miur"] = $extraInfo["miur"];
$row["fonte"] = 14;
$row["_idRiferimento"] = $immobile["codice"];
$return[] = $row;
}
}
}
}
}
return $return;
}
public function getA12() {
$dotazioni = $this->progetto->getDotazione();
$return = [];
if (!empty($dotazioni)) {
foreach($dotazioni AS $dotazione) {
$dotazioneInfo = json_decode($dotazione["extraInfo"],true);
if ($dotazioneInfo["economie"] > 0) {
$row = [];
$row[] = $dotazione["anno"];
$row[] = $dotazioneInfo["fonte"] ?? "007";
$row[] = number_format($dotazione["importo"],2,".","");
$row["_idRiferimento"] = $dotazione["codice"];
$return[] = $row;
}
}
}
return $return;
}
public function getA13() {
$cig = $this->progetto->getCIG();
$return = [];
if (!empty($cig)) {
foreach($cig AS $pro) {
if ($pro["percentuale_ribasso"] > 0 && $pro["importo_ribasso"] > 0) {
$return[] = ["cig"=>$pro["cig"],"percentuale"=>$pro["percentuale_ribasso"],"importo"=>$pro["importo_ribasso"],"_idRiferimento"=>$pro["codice"]];
}
}
}
return $return;
}
public function getA28() {
$cig = $this->progetto->getCIG();
$return = [];
if (!empty($cig)) {
foreach($cig AS $pro) {
$pro["_idRiferimento"] = $pro["codice"];
$return[] = $pro;
}
}
return $return;
}
public function getA29() {
$cig = $this->progetto->getCIG();
$return = [];
if (!empty($cig)) {
foreach($cig AS $pro) {
$steps = json_decode($pro["step"],true);
if (!empty($steps)) {
foreach($steps AS $idStep => $step) {
if (!empty($step["prevista"])) {
$step["cig"] = $pro["cig"];
$step["step"] = $idStep;
$step["_idRiferimento"] = $pro["codice"];
$step["_extraInfo"] = $idStep;
$return[] = $step;
}
}
}
}
}
return $return;
}
public function getA30() {
$cig = $this->progetto->getCIG();
$return = [];
if (!empty($cig)) {
foreach($cig AS $pro) {
$pro["_idRiferimento"] = $pro["codice"];
$pro["data_associazione"] = mysql2date($pro["data_associazione"]);
$return[] = $pro;
}
}
return $return;
}
public function prepareDataset($dato,$dataset = [],$count = 0) {
$array = json_decode($dato,true);
if (is_array($array)) {
foreach($array AS $index => $value) {
$dataset[$index] = $value;
}
} else {
$dataset[$count] = $dato;
}
return $dataset;
}
public function createRecord($tipologia) {
$scheda = self::tipologiaSchede()[$tipologia] ?? null;
if (!empty($scheda)) {
if ($tipologia == "A11P" || $tipologia == "A11E") {
$scheda["multiple"] = true;
$tmp = $this->progetto->getInfo("quadro-economico-".(($tipologia == "A11P") ? "previsto" : "effettivo"));
$dati = [];
if (!empty($tmp["value"])) {
$voci = json_decode($tmp["value"],true);
foreach($voci AS $idVoce => $importoVoce) {
$dati[] = [($tipologia == "A11P") ? "P" : "E",$idVoce,$importoVoce,"_idRiferimento"=>$tmp["codice"],"_extraInfo"=>$idVoce];
}
}
} else if ($tipologia == "A21") {
$scheda["multiple"] = true;
$tmp = $this->progetto->getInfo("iter-progetto");
$dati = [];
if (!empty($tmp["value"])) {
$iter = json_decode($tmp["value"],true);
foreach($iter AS $idFase => $infoFase) {
$r = ["_idRiferimento"=>$tmp["codice"],"_extraInfo"=>$idFase];
$r[] = $idFase;
$r[] = $infoFase["inizio_prevista"];
$r[] = $infoFase["inizio_effettiva"];
$r[] = $infoFase["fine_prevista"];
$r[] = $infoFase["fine_effettiva"];
$r[] = $infoFase["soggetto"];
$r[] = $infoFase["motivo"];
$r[] = $infoFase["note"];
$dati[] = $r;
}
}
} else if (isset($scheda["dati-richiesti"])) {
$continue = true;
$dati = [];
if (count($scheda["dati-richiesti"]) == 1 && $scheda["multiple"]) {
$key = $scheda["dati-richiesti"][0];
$keyInfo = progetto::getKeyInfo($key);
if ($keyInfo["multiple"]) {
$continue = false;
$source = $this->progetto->getInfo($key);
if (!empty($source)) {
foreach($source AS $dato) {
$dataset = $this->prepareDataset($dato["value"] ?? "");
$dataset["_idRiferimento"] = $dato["codice"];
$dati[] = $dataset;
}
}
}
}
if ($continue) {
$count = 0;
$dataset = [];
foreach($scheda["dati-richiesti"] AS $key) {
$dataset = $this->prepareDataset($this->progetto->getInfo($key)["value"] ?? "",$dataset,$count);
$count++;
}
$dati[] = $dataset;
}
} else {
if ($tipologia == "A0") {
$dati = [$this->getA0()];
} else if ($tipologia == "A9") {
$dati = $this->getA9();
} else if ($tipologia == "A12") {
$dati = $this->getA12();
} else if ($tipologia == "A13") {
$dati = $this->getA13();
} else if ($tipologia == "A28") {
$dati = $this->getA28();
} else if ($tipologia == "A29") {
$dati = $this->getA29();
} else if ($tipologia == "A30") {
$dati = $this->getA30();
} else if ($tipologia == "A31") {
$dati = $this->getA31();
}
}
if (isset($scheda["structure"]) && !empty($dati)) {
foreach($dati as $dataset) {
$record = [];
$count = 0;
$codice_riferimento = $dataset["_idRiferimento"] ?? null;
$extraInfo_riferimento = $dataset["_extraInfo"] ?? null;
foreach($scheda["structure"] AS $info) {
if (!empty($info["locale"])) {
$record[] = $this->codice_locale;
} else {
$key = (!empty($info["key"])) ? $info["key"] : $count;
$insert = $dataset[$key] ?? "";
if (!empty($info["date"])) {
$insert = str_replace("/","",$insert);
}
$record[] = substr($insert,$info["start"]??0,$info["max"]);
$count++;
}
}
$operazione = "INSERT";
$codice = null;
$stato = 0;
$dbScheda = $this->getScheda($tipologia);
$record = implode("|",$record);
if (!empty($dbScheda)) {
if (!$scheda["multiple"]) {
$dbs = $dbScheda[0];
$operazione = "UPDATE";
$codice = $dbs["codice"];
if ($dbs["trasmesso"] > 0) {
$stato = 1;
}
} else if (!empty($codice_riferimento)) {
foreach($dbScheda AS $dbs) {
if ($dbs["codice_riferimento"] == $codice_riferimento && ($dbs["extraInfo"] == $extraInfo_riferimento || $extraInfo_riferimento === null)) {
$operazione = "UPDATE";
$codice = $dbs["codice"];
if ($dbs["trasmesso"] > 0) {
$stato = 1;
}
break;
}
}
}
if (!empty($codice) && $dbs["data"] == $record) {
continue;
}
}
$salva = new salva();
$salva->debug = false;
$salva->nome_tabella = "b_progetti_monitoraggio";
$salva->operazione = $operazione;
$salva->oggetto = ["codice"=>$codice,"stato"=>$stato,"codice_progetto"=>$this->progetto->info["codice"],"tipo"=>$tipologia,"data"=>$record,"codice_riferimento"=>$codice_riferimento,"extraInfo"=>$extraInfo_riferimento];
$salva->save();
}
}
$this->checkDelete($tipologia);
}
}
public function checkDelete($tipologia) {
$scheda = self::tipologiaSchede()[$tipologia] ?? null;
if (!empty($scheda) && !empty($scheda["multiple"])) {
$records = $this->getScheda($tipologia);
if (!empty($records)) {
$sendDelete = $this->db->prepare("UPDATE b_progetti_monitoraggio SET stato = '2', trasmesso = '2', codice_riferimento = codice_riferimento * -1 WHERE codice = :codice");
$delete = $this->db->prepare("DELETE FROM b_progetti_monitoraggio WHERE codice = :codice");
$checkExist = $this->db->prepare("SELECT codice FROM b_progetti_info WHERE codice = :codice");
$tipologieDotazione = ["A9","A12","A31"];
$tipologieCIG = ["A13","A28","A29","A30"];
if (!empty($scheda["auto"])) {
if (in_array($tipologia,$tipologieDotazione) !== false) {
$riferimento = $this->progetto->getDotazione();
} else if (in_array($tipologia,$tipologieCIG) !== false) {
$riferimento = $this->progetto->getCIG();
}
}
foreach($records AS $rec) {
if ($rec["codice_riferimento"] > 0) {
if (empty($scheda["auto"])) {
$deleted = false;
$checkExist->bindValue(":codice",$rec["codice_riferimento"]);
$checkExist->execute();
if (empty($checkExist->rowCount())) {
$deleted = true;
}
} else if (isset($riferimento)) {
$deleted = true;
if (!empty($riferimento)) {
foreach($riferimento AS $ref) {
if ($rec["codice_riferimento"] == $ref["codice"]) {
$deleted = false;
break;
}
}
}
}
if ($deleted) {
if ($rec["trasmesso"] === 1) {
$sendDelete->bindValue(":codice",$rec["codice"]);
$sendDelete->execute();
} else {
$delete->bindValue(":codice",$rec["codice"]);
$delete->execute();
}
}
}
}
}
}
}
}
?>