fetchArray($query_fatt); $anno=$rs_testata[0]['anno']; $path_archivio = $rs_testata[0]['path_archivio']; $nome_modulo = "archivio_".$path_archivio."_".$anno; $tipo_doc = $rs_testata[0]['tipo_doc']; $numero_pri=$rs_testata[0]['numero']; $txt_suffisso=$rs_testata[0]['txt_suffisso']; $dir_pdf=$rs_testata[0]['dir_pdf']; // apertura testata documento $query="select * from co_documenti where id='".$iddocumento."'"; $rs_testata = $dbo->fetchArray($query); // apertura righe documento $query="select * from co_righe_documenti where iddocumento='".$iddocumento."' and riga_varia='0'"; $rs_righe = $dbo->fetchArray($query); // apertura anagrafica cedente $query="select * from an_anagrafiche where idanagrafica='".get_var("Azienda predefinita")."'"; $rs_cedente = $dbo->fetchArray($query); // apertura anagrafica cliente $query="select * from an_anagrafiche where idanagrafica='".$rs_testata[0]['idanagrafica']."'"; $rs_cliente = $dbo->fetchArray($query); // terzo intemediario $query="select * from an_anagrafiche where idanagrafica='".get_var("Terzo intermediario")."'"; $rs_intermediario = $dbo->fetchArray($query); // apertura dati banca $query="select * from co_banche where id='".$rs_testata[0]['idbanca_vendite']."'"; $rs_banca = $dbo->fetchArray($query); $scrivi_ritenuta="NO"; // dati del docuemnto $_DocData = substr($rs_testata[0]['data'],0,10); // cerco iva predifinita $query="select percentuale from co_iva where id='".get_var("Iva predefinita")."'"; $rs_iva = $dbo->fetchArray($query); $id_iva_pred=$rs_iva[0]['percentuale']; // impostazioni variabili $_progressivo = $rs_testata[0]['numero_esterno']; $_IdCodice = $rs_cedente[0]['piva']; $_CodFisc = $rs_cedente[0]['codice_fiscale']; $_Regimefiscale= substr(get_var("Regime Fiscale"), 0, 4) ; $_SedeIndirizzo= substr($rs_cedente[0]['indirizzo'], 0, 60); // 60 indirizzo fornitore $_SedeCap= substr($rs_cedente[0]['cap'], 0, 5); // 5 indirizzo fornitore $_SedeLocalita= substr($rs_cedente[0]['citta'], 0, 60); // 60 indirizzo fornitore $_SedePV= substr($rs_cedente[0]['provincia'], 0, 2); // 2 indirizzo fornitore $_SedeNazione= "IT"; // 2 indirizzo fornitore $_Denominazione = substr($rs_cedente[0]['ragione_sociale'], 0, 80); // 80 caratteri intestazione azienda fornitrice $_Denominazione = html_entity_decode($_Denominazione); $_Denominazione=str_replace("'","",$_Denominazione); $_SedeIndirizzo = html_entity_decode($_SedeIndirizzo, ENT_QUOTES); $_Denominazione = html_entity_decode($_Denominazione, ENT_QUOTES); // calcolo nuovo progressivo $_AnnoData = substr($_DocData,2,2); $query = "SELECT MAX(numero) AS max_progressivo FROM co_documenti_progressivo WHERE DATE_FORMAT( data, '%Y' ) = '".date("Y", strtotime($rs_testata[0]['data']))."'"; $rs_progressivo = $dbo->fetchArray($query); $_progressivo = $rs_progressivo[0]['max_progressivo'] + 1; $rs = $dbo->query("INSERT INTO `co_documenti_progressivo`( inviato,esito,idutente,id_documento, data, numero_esterno, numero ,numero_documento ) VALUES( '1','4',\"".$_SESSION['idutente']."\" , \"".$iddocumento."\", NOW(), \"".$_progressivo."_".$_AnnoData."\", \"".$_progressivo."\" , \"".$rs_testata[0]['numero_esterno']."\")"); $_progressivo = $_progressivo."_".$_AnnoData; $_progressivo=substr($_progressivo, 0, 10); // fine calcolo nuovo progressivo if ($rs_cliente[0]['tipo'] == 'Ente pubblico' ){ $_FormatoTrasmissione="FPA12"; $_CodiceDestinatario= $rs_cliente[0]['cod_sogei']; // 6 caratteri dell'ufficio destinazione della fattura } else { $_FormatoTrasmissione="FPR12"; $_CodiceDestinatario= $rs_cliente[0]['cod_sogei']; // 7 caratteri dell'ufficio destinazione della fattura . Se non asseganto deve essere la pec valorrizata 0000000 $_PECDestinatario= $rs_cliente[0]['cod_sogei_pec']; } if ($rs_cliente[0]['tipo'] == 'Privato' ){ $_FormatoTrasmissione="FPR12"; $_CodiceDestinatario= $rs_cliente[0]['cod_sogei']; // 7 caratteri dell'ufficio destinazione della fattura . Se non asseganto deve essere la pec valorrizata 0000000 $_PECDestinatario= $rs_cliente[0]['cod_sogei_pec']; } $esigibilita_iva = $rs_cliente[0]['esigibilita_iva']; if ( $esigibilita_iva == '' ){ $esigibilita_iva ="I"; } // dati del cliente $_ClienteIdCodice= substr($rs_cliente[0]['piva'], 0, 16); // 11-16 caratteri $_ClienteCodFicale= substr($rs_cliente[0]['codice_fiscale'], 0, 16); // 11-16 caratteri $_ClientePartitaIva= substr($rs_cliente[0]['piva'], 0, 16); // 11-16 caratteri $_ClienteDenominazione= substr($rs_cliente[0]['ragione_sociale'], 0, 80); // 80 caratteri $_ClienteIndirizzo= substr($rs_cliente[0]['indirizzo'], 0, 60); // 60 caratteri $_Clientecap= substr($rs_cliente[0]['cap'], 0, 5); // 5 caratteri $_ClienteComune= substr($rs_cliente[0]['citta'], 0, 60); // 80 caratteri $_ClientePv= substr($rs_cliente[0]['provincia'], 0, 2); // 80 caratteri $_ClienteNazione="IT"; $_ClienteDenominazione = html_entity_decode($_ClienteDenominazione); $_ClienteDenominazione=str_replace("'","",$_ClienteDenominazione); $_ClienteIndirizzo = html_entity_decode($_ClienteIndirizzo, ENT_QUOTES); $_ClienteDenominazione = html_entity_decode($_ClienteDenominazione, ENT_QUOTES); do { $code = date('y').secure_random_string(3); } while ($dbo->fetchNum('SELECT `id` FROM `co_documenti` WHERE `codice_xml` = '.prepare($code)) != 0); $dbo->query("UPDATE co_documenti SET codice_xml=\"".$code."\" WHERE id=\"".$iddocumento."\""); $file = "IT".$_IdCodice."_".$code.".xml"; $nfile = $docroot."/files/pa/".$file; // apertura e creazione file xml $myfile = fopen($nfile, "w") or die("Errore apertura file!"); // scrittura schema $_schema = '' . "\n"; if ($rs_cliente[0]['tipo'] == 'Ente pubblico' ){ $_schema .= ' 0 ) { $_IdCodice_trasmittente=$rs_intermediario[0]['piva'];; } $_Header .= ' ' . $_IdCodice_trasmittente . '' . "\n"; } if ($rs_cedente[0]['identificativo_xml'] == 'C' ){ $_Header .= ' ' . $rs_cedente[0]['codice_fiscale'] . '' . "\n"; } $_Header .= ' ' . "\n"; $_Header .= ' ' . $_progressivo . '' . "\n"; $_Header .= ' ' . $_FormatoTrasmissione . '' . "\n"; $_Header .= ' ' . $_CodiceDestinatario . '' . "\n"; if ( ($_CodiceDestinatario == "0000000") && ($rs_cliente[0]['tipo'] == 'Privato' ) ){ // caso in cui fattura a privato senza codice sogei asegnato necessita invio indirizzo pec if ($_PECDestinatario !='' ) { $_Header .= ' ' . $_PECDestinatario . '' . "\n"; } } $_Header .= ' ' . "\n"; // SEZIONE 1.2 $_Header .= ' ' . "\n"; $_Header .= ' ' . "\n"; $_Header .= ' ' . "\n"; $_Header .= ' ' . $_SedeNazione . '' . "\n"; $_Header .= ' ' . $_IdCodice . '' . "\n"; $_Header .= ' ' . "\n"; $_Header .= ' ' . "\n"; $_Header .= ' ' . $_Denominazione . '' . "\n"; $_Header .= ' ' . "\n"; $_Header .= ' ' . $_Regimefiscale . '' . "\n"; $_Header .= ' ' . "\n"; $_Header .= ' ' . "\n"; $_Header .= ' ' . $_SedeIndirizzo . '' . "\n"; $_Header .= ' ' . $_SedeCap . '' . "\n"; $_Header .= ' ' . $_SedeLocalita . '' . "\n"; $_Header .= ' ' . $_SedePV . '' . "\n"; $_Header .= ' ' . $_SedeNazione . '' . "\n"; $_Header .= ' ' . "\n"; $_Header .= ' ' . "\n"; // SEZIONE 1.3 // Rappresentante fiscale // SEZIONE 1.4 $_Header .= ' ' . "\n"; $_Header .= ' ' . "\n"; if ($rs_cliente[0]['identificativo_xml'] == 'I' ){ $_Header .= ' ' . "\n"; $_Header .= ' IT' . "\n"; $_Header .= ' ' . $_ClientePartitaIva . '' . "\n"; $_Header .= ' ' . "\n"; } $_Header .= ' ' . $_ClienteCodFicale . '' . "\n"; $_Header .= ' ' . "\n"; $_Header .= ' ' . $_ClienteDenominazione . '' . "\n"; $_Header .= ' ' . "\n"; $_Header .= ' ' . "\n"; $_Header .= ' ' . "\n"; $_Header .= ' ' . $_ClienteIndirizzo . '' . "\n"; $_Header .= ' ' . $_Clientecap . '' . "\n"; $_Header .= ' ' . $_ClienteComune . '' . "\n"; $_Header .= ' ' . $_ClientePv . '' . "\n"; $_Header .= ' ' . $_ClienteNazione . '' . "\n"; $_Header .= ' ' . "\n"; $_Header .= ' ' . "\n"; // SEZIONE 1.5 // Terzo intermediario if (get_var("Terzo intermediario") > 0 ){ $_IdCodice_Intermediario=$rs_intermediario[0]['piva']; $_ClienteCodFicale_Intermediario=$rs_intermediario[0]['piva']; $_Demnominzione_Intermediario=$rs_intermediario[0]['ragione_sociale']; $_Demnominzione_Intermediario = html_entity_decode($_Demnominzione_Intermediario, ENT_QUOTES); $_Header .= '' . "\n"; $_Header .= ' ' . "\n"; $_Header .= ' ' . "\n"; $_Header .= ' IT' . "\n"; $_Header .= ' ' . $_IdCodice_Intermediario . '' . "\n"; $_Header .= ' ' . "\n"; $_Header .= ' ' . $_ClienteCodFicale_Intermediario . '' . "\n"; $_Header .= ' ' . "\n"; $_Header .= ' ' . $_Demnominzione_Intermediario . '' . "\n"; $_Header .= ' ' . "\n"; $_Header .= ' ' . "\n"; $_Header .= '' . "\n"; $_Header .= 'TZ' . "\n"; } // CHIUDO SEZIONE 1 $_Header .= '' . "\n"; fwrite($myfile, $_Header); // SEZIONE 2.1 $_intestazione = ' ' . "\n"; $_intestazione .= ' ' . "\n"; $_intestazione .= ' ' . "\n"; $_intestazione .= ' ' . get_var_tipodoc2($iddocumento,'el_tipodocumento') . '' . "\n"; $_intestazione .= ' ' . get_var_tipodoc2($iddocumento,'el_divisa') . '' . "\n"; $_intestazione .= ' ' . $_DocData . '' . "\n"; $_intestazione .= ' ' . $rs_testata[0]['numero_documento'] . '' . "\n"; // aggiungere se di tipo ritenuta if ( $rs_testata[0]['ritenutaacconto'] != '0.000' ){ $scrivi_ritenuta="SI"; if ( $rs_cedente[0]['tipo'] == 'Privato' ) $rit='RT01' ; if ( $rs_cedente[0]['tipo'] != 'Privato' ) $rit='RT02' ; $prezzo_ritenuta = number_format($rs_testata[0]['ritenutaacconto'],2,".",""); $query="SELECT percentuale FROM co_ritenutaacconto WHERE id='".$rs_testata[0]['idritenutaacconto']."'"; $rs_ritenuta = $dbo->fetchArray($query); $_intestazione .= ' ' . "\n"; $_intestazione .= ' ' . $rit. '' . "\n"; $_intestazione .= ' ' . $prezzo_ritenuta. '' . "\n"; $_intestazione .= ' ' . $rs_ritenuta[0]['percentuale']. '' . "\n"; $_intestazione .= ' ' . get_var("Causale ritenuta d'acconto"). '' . "\n"; $_intestazione .= ' ' . "\n"; } // Bollo if ( $rs_testata[0]['bollo'] != '0.000' ){ $prezzo_bollo = number_format($rs_testata[0]['bollo'],2,".",""); $_intestazione .= ' ' . "\n"; $_intestazione .= ' SI' . "\n"; $_intestazione .= ' ' . $prezzo_bollo. '' . "\n"; $_intestazione .= ' ' . "\n"; } $query="SELECT sum(subtotale - sconto) as tot , sum(iva) as totiva FROM `co_righe_documenti` WHERE iddocumento='".$iddocumento."' and riga_varia='0'"; $rs_totale = $dbo->fetchArray($query); $totale_documento = number_format($rs_totale[0]['tot'] + $rs_totale[0]['totiva'] ,2,".",""); $_intestazione .= ' ' . $totale_documento . '' . "\n"; // Fine if ( $rs_testata[0]['el_causale_1'] <> '' ){ $_intestazione .= ' ' . $rs_testata[0]['el_causale_1']. '' . "\n"; } if ( $rs_testata[0]['el_causale_2'] <> '' ){ $_intestazione .= ' ' . $rs_testata[0]['el_causale_2']. '' . "\n"; } $_intestazione .= ' ' . "\n"; // aggiungo riferimento ordine di acq se presenti if ( $rs_testata[0]['el_idrif'] <> '' ){ $_intestazione .= ' ' . "\n"; $_intestazione .= ' 1' . "\n"; $_intestazione .= ' ' . $rs_testata[0]['el_idrif']. '' . "\n"; $_intestazione .= ' 1' . "\n"; $_intestazione .= ' ' . $rs_testata[0]['el_id_codicecup']. '' . "\n"; $_intestazione .= ' ' . $rs_testata[0]['el_id_codicecig']. '' . "\n"; $_intestazione .= ' ' . "\n"; } // aggiungo riferimento al documento di trasporto if ( $rs_testata[0]['el_idrif_ddt1'] <> '' ){ $data_ddt=substr($rs_testata[0]['el_idrif_ddt1_data'],0,10); $_intestazione .= ' ' . "\n"; $_intestazione .= ' ' . $rs_testata[0]['el_idrif_ddt1']. '' . "\n"; $_intestazione .= ' ' . $data_ddt. '' . "\n"; $_intestazione .= ' ' . "\n"; } $_intestazione .= ' ' . "\n"; fwrite($myfile, $_intestazione); // SEZIONE 2.2 $_beniservizi = ' ' . "\n"; fwrite($myfile, $_beniservizi); $test_iva_vuota="NO"; for( $i=0; $ifetchArray($query); $id_iva=$rs_iva[0]['percentuale']; $codice_natura_fe=$rs_iva[0]['codice_natura_fe']; if ( $id_iva == "") { $id_iva="0.00"; $codice_natura_fe="N2"; $test_iva_vuota="SI"; } if ( $rs_righe[$i]['idiva'] == '0') { $id_iva="0.00"; $codice_natura_fe="N2"; $test_iva_vuota="SI"; } $des_tmp=$rs_righe[$i]['descrizione']; $des_tmp = html_entity_decode($des_tmp); $des_tmp = html_entity_decode($des_tmp, ENT_QUOTES); $des_tmp=str_replace("'","",$des_tmp); // $des_tmp=str_replace("°","",$des_tmp); // $des_tmp=str_replace("’","",$des_tmp); $_beniservizi = ' ' . "\n"; $_beniservizi .= ' ' .$numero_linea. '' . "\n"; $_beniservizi .= ' ' .$des_tmp. '' . "\n"; $_beniservizi .= ' ' .$rs_righe[$i]['qta']. '' . "\n"; $_beniservizi .= ' ' .$prezzo_unitario . '' . "\n"; if ($rs_righe[$i]['tipo_sconto'] == '2' ){ if ($rs_righe[$i]['sconto_primo'] != '0.0000' ){ $_beniservizi .= ' ' . "\n"; $_beniservizi .= ' SC' . "\n"; $_beniservizi .= ' ' . number_format($rs_righe[$i]['sconto_primo'],2,".","") . '' . "\n"; $_beniservizi .= ' ' . "\n"; } if ($rs_righe[$i]['sconto_extra'] != '0.0000'){ $_beniservizi .= ' ' . "\n"; $_beniservizi .= ' SC' . "\n"; $_beniservizi .= ' ' . number_format($rs_righe[$i]['sconto_extra'] ,2,".",""). '' . "\n"; $_beniservizi .= ' ' . "\n"; } if ($rs_righe[$i]['sconto_extra1'] != '0.0000'){ $_beniservizi .= ' ' . "\n"; $_beniservizi .= ' SC' . "\n"; $_beniservizi .= ' ' . number_format($rs_righe[$i]['sconto_extra1'],2,".","") . '' . "\n"; $_beniservizi .= ' ' . "\n"; } } else { if ($rs_righe[$i]['sconto'] != '0.0000' ){ $_beniservizi .= ' ' . "\n"; $_beniservizi .= ' SC' . "\n"; $_beniservizi .= ' ' . number_format(($rs_righe[$i]['sconto'] / $rs_righe[$i]['qta'] ),2,".","") . '' . "\n"; $_beniservizi .= ' ' . "\n"; } } $_beniservizi .= ' ' .$prezzo_totale. '' . "\n"; if ( $id_iva =="" ) $id_iva = $id_iva_pred; $_beniservizi .= ' ' .$id_iva. '' . "\n"; if ( $scrivi_ritenuta =="SI" ) $_beniservizi .= ' SI' . "\n"; if ( $id_iva == "0.00") { if ( $codice_natura_fe == "") $codice_natura_fe="N3"; $_beniservizi .= ' ' .$codice_natura_fe . '' . "\n"; } $_beniservizi .= ' ' . "\n"; fwrite($myfile, $_beniservizi); } // riepilogo suddiviso per iva $query="SELECT sum(subtotale - sconto) as tot , sum(iva) as totiva ,idiva FROM `co_righe_documenti` WHERE iddocumento='".$iddocumento."' and riga_varia='0' and idiva<>'0' group by idiva"; $rs_riepiologo = $dbo->fetchArray($query); for( $i=0; $ifetchArray($query); $id_iva=$rs_iva[0]['percentuale']; $prezzo_unitario = number_format($rs_riepiologo[$i]['tot'],2,".",""); $imposta = number_format($rs_riepiologo[$i]['totiva'],2,".",""); $_beniservizi = ' ' . "\n"; $_beniservizi .= ' ' .$id_iva. '' . "\n"; if ( $id_iva == "0.00") { $codice_natura_fe=$rs_iva[0]['codice_natura_fe']; if ( $rs_iva[0]['codice_natura_fe'] == "") $codice_natura_fe="N3"; $_beniservizi .= ' ' .$codice_natura_fe. '' . "\n"; } $_beniservizi .= ' ' .$prezzo_unitario. '' . "\n"; $_beniservizi .= ' ' .$imposta. '' . "\n"; // $_beniservizi .= ' I' . "\n"; $_beniservizi .= ' ' .$esigibilita_iva. '' . "\n"; $_beniservizi .= ' ' . "\n"; fwrite($myfile, $_beniservizi); } if ( $test_iva_vuota == "SI") { $_beniservizi1 = ' ' . "\n"; $_beniservizi1 .= ' 00.00' . "\n"; $_beniservizi1 .= ' N2' . "\n"; $_beniservizi1 .= ' 00.00' . "\n"; $_beniservizi1 .= ' 00.00' . "\n"; // $_beniservizi1 .= ' I' . "\n"; $_beniservizi .= ' ' .$esigibilita_iva. '' . "\n"; $_beniservizi1 .= ' ' . "\n"; fwrite($myfile, $_beniservizi1); } $_beniservizi = ' ' . "\n"; fwrite($myfile, $_beniservizi); // SEZIONE 2.4 $query="SELECT * FROM `co_scadenziario` WHERE iddocumento='".$iddocumento."'"; $rs_scadenze = $dbo->fetchArray($query); if ( $rs_testata[0]['el_modalitapagamento'] <> '' ){ $_datipagamento = ' ' . "\n"; $query="SELECT prc FROM `co_pagamenti` WHERE id='".$rs_testata[0]['idpagamento']."'"; $rs_pagamento = $dbo->fetchArray($query); if ( $rs_pagamento[0]['prc'] == '100' ){ $_datipagamento .= ' TP02' . "\n"; } else { $_datipagamento .= ' TP01' . "\n"; } for( $i=0; $i' . "\n"; $_datipagamento .= ' ' .$doc_pagamento. '' . "\n"; $_datipagamento .= ' ' .$prezzo_rata. '' . "\n"; if( sizeof($rs_banca) > 0 ) { $_datipagamento .= ' ' .$rs_banca[0]['nome']. '' . "\n"; $_datipagamento .= ' ' .$rs_banca[0]['iban']. '' . "\n"; $_datipagamento .= ' ' .$rs_banca[0]['abi']. '' . "\n"; $_datipagamento .= ' ' .$rs_banca[0]['cab']. '' . "\n"; $_datipagamento .= ' ' .$rs_banca[0]['bic']. '' . "\n"; } $_datipagamento .= ' ' . "\n"; } $_datipagamento .= ' ' . "\n"; } fwrite($myfile, $_datipagamento); // SEZIONE 2.5 // Creo l'allegato se allegato $el_allegato=get_var_tipodoc2($iddocumento,'el_allegato'); if ( $el_allegato == '1' ){ $n_pages = 1; $orientation = 'P'; $body_table_params ="style='width:210mm;'"; $font_size = "10pt"; $ptype = "documenti_pa"; if( @file_exists($docroot."/templates/".$ptype."/custom/pdfgen.".$ptype.".php") ){ include( $docroot."/templates/".$ptype."/custom/pdfgen.".$ptype.".php"); } else{ include( $docroot."/templates/".$ptype."/pdfgen.".$ptype.".php"); } include( $docroot."/templates/pdfgen_variables.php" ); // Completamento e output del report $report = str_replace( '$body$', $body, $report ); $report = str_replace( '$footer$', $footer, $report ); $report = str_replace( '$body_table_params$', $body_table_params, $report ); $report = str_replace( '$font_size$', $font_size, $report ); $report = str_replace( '$docroot$', $docroot, $report ); //html $formato = get_var( 'Formato report' ); ( $formato == 'html' ) ? $html=true : $html=false; require_once( $docroot."/lib/spipu/html2pdf/html2pdf.class.php" ); require_once( $docroot."/lib/spipu/html2pdf/_tcpdf_5.0.002/barcodes.php" ); $file_dir = $docroot.'/files/pa/'.$rs_testata[0]['numero_documento'].'.pdf'; try { $html2pdf = new HTML2PDF($orientation, 'A4', 'it', true, 'UTF-8'); $html2pdf->writeHTML($report, $html); $html2pdf->Output( $file_dir , 'F'); } catch(HTML2PDF_exception $e) { echo $e; exit; } if( is_file($file_dir) ){ $file_64=base64_encode(file_get_contents($file_dir)); $_datiallegato = ' ' . "\n"; $_datiallegato .= ' ' .$rs_testata[0]['numero_documento'].'.pdf'. '' . "\n"; $_datiallegato .= ' PDF' . "\n"; $_datiallegato .= ' ' .$file_64. '' . "\n"; $_datiallegato .= ' ' . "\n"; fwrite($myfile, $_datiallegato); unlink($file_dir); } // chiusura file } $dati_chiusura = ' ' . "\n"; $dati_chiusura .= ' ' . "\n"; fwrite($myfile, $dati_chiusura); fclose($myfile); return $file; } function secure_random_string($length = 32) { if (function_exists('openssl_random_pseudo_bytes')) { $bytes = openssl_random_pseudo_bytes($length * 2); if ($bytes === false) { throw new \LengthException('$length is not accurate, unable to generate random string'); } return substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $length); } return random_string($length); } ?>