query("select * from $tpagamenti where idpagamenti = $pagamento") ; while ($db->next_record()) { $giorni = $db->f("Giorni") ; $modalita = $db->f("Modalita") ; } } else { $wpagamento = "O.idpagamento IN (1,3,4,5,6,8,9,10,11)" ; $modalita = "Anticipato, Bonifico, Titoli all'ordine, R.B. 60 gg, R.B. 90 gg, R.D. 60 gg, R.D. 90 gg" ; } $titolo = "Fatture scadute da incassare in data " . Date("d/m/Y") ; if ($agente != 'tutti' and $agente != '') { $wagente = "O.idagente = $agente" ; $db->query("select * from $tagenti where idagenti = $agente") ; while ($db->next_record()) $nome = $db->f("Nome") ; $Agente = "$nome" ; } else { $wagente = "1 = 1" ; $Agente = "Tutti" ; } if ($cliente != 'tutti' and $cliente != '') { $wcliente = "O.idcliente = $cliente" ; $db->query("select * from $tclienti where idclienti = $cliente") ; while ($db->next_record()) $nomecli = $db->f("Nome") ; $Cliente .= "$nomecli" ; } else { $wcliente = "1 = 1" ; $Cliente .= "Tutti" ; } if ($fornitore != 'tutti' and $fornitore != '') { $wfornitore = "O.idfornitore = $fornitore" ; $db->query("select * from $tfornitori where idfornitori = $fornitore") ; while ($db->next_record()) $nomefor = $db->f("Nome") ; $Fornitore .= "$nomefor" ; } else { $wfornitore = "1 = 1" ; $Fornitore .= "Tutti" ; } $data = date("Y-m-d", mktime(0,0,0, date("m"), date("d") - $giorni, date("Y"))) ; /****************************************************************************** * recupero le fatture scadute ma non pagate dell'anno in corso e dei quattro anni * precedenti ******************************************************************************/ $an1 = $anno - 1 ; $an2 = $anno - 2 ; $an3 = $anno - 3 ; $an4 = $anno - 4 ; if ($tipo == 0) { /* PRIMA DELLA 3.0 $query = " select *, case when (O.DataSpedFattura = '0000-00-00') then '2002-01-01' else O.DataSpedFattura end as DataSpedizioneFattura, F.Nome as NomeFornitore, F.CodIban as CodIban, C.nome as NomeCliente, C.Indirizzo as IndirizzoCliente, C.Citta as CittaCliente, C.Provincia as ProvinciaCliente, P.modalita as TipoPagamento from $tordini O inner join $tpagamenti P on P.IdPagamenti = O.idpagamento inner join $tfornitori F on F.IdFornitori = O.IdFornitore inner join $tclienti C on C.IdClienti = O.IdCliente where $wfornitore and $wagente and $wcliente and $wpagamento and $wnumero and $wdata and O.saldofattura = 0 and O.NumFattura <> 0 and O.dataspedfattura <= '$data' UNION ALL select *, O.DataSpedFattura as DataSpedizioneFattura, F.Nome as NomeFornitore, F.CodIban as CodIban, C.nome as NomeCliente, C.Indirizzo as IndirizzoCliente, C.Citta as CittaCliente, C.Provincia as ProvinciaCliente, P.modalita as TipoPagamento from ordini_$an1 O inner join pagamenti_$an1 P on P.IdPagamenti = O.idpagamento inner join fornitori_$an1 F on F.IdFornitori = O.IdFornitore inner join clienti_$an1 C on C.IdClienti = O.IdCliente where $wfornitore and $wagente and $wcliente and $wpagamento and $wnumero and $wdata and O.saldofattura = 0 and O.NumFattura <> 0 and O.dataspedfattura <= '$data' UNION ALL select *, O.DataSpedFattura as DataSpedizioneFattura, F.Nome as NomeFornitore, F.CodIban as CodIban, C.nome as NomeCliente, C.Indirizzo as IndirizzoCliente, C.Citta as CittaCliente, C.Provincia as ProvinciaCliente, P.modalita as TipoPagamento from ordini_$an2 O inner join pagamenti_$an2 P on P.IdPagamenti = O.idpagamento inner join fornitori_$an2 F on F.IdFornitori = O.IdFornitore inner join clienti_$an2 C on C.IdClienti = O.IdCliente where $wfornitore and $wagente and $wcliente and $wpagamento and $wnumero and $wdata and O.saldofattura = 0 and O.NumFattura <> 0 and O.dataspedfattura <= '$data' order by NomeCliente, DataSpedizioneFattura" ; ADESSO 3.0 */ $query = " select *, case when (O.DataSpedFattura = '0000-00-00') then '2002-01-01' else O.DataSpedFattura end as DataSpedizioneFattura, F.Nome as NomeFornitore, F.CodIban as CodIban, C.nome as NomeCliente, C.Indirizzo as IndirizzoCliente, C.Citta as CittaCliente, C.Provincia as ProvinciaCliente, P.modalita as TipoPagamento from ordini O inner join pagamenti P on P.IdPagamenti = O.idpagamento inner join fornitori F on F.IdFornitori = O.IdFornitore inner join clienti C on C.IdClienti = O.IdCliente where (O.Anno = $anno or O.Anno = $an1 or O.Anno = $an2 or O.Anno = $an3 or O.Anno = $an4) AND $wfornitore and $wagente and $wcliente and $wpagamento and $wnumero and $wdata and O.saldofattura = 0 and O.NumFattura <> 0 and O.dataspedfattura <= '$data' order by NomeCliente, DataSpedizioneFattura" ; } else { $giorni = 31 ; $data = date("Y-m-d", mktime(0,0,0, date("m"), date("d") - $giorni, date("Y"))) ; // $whereData = " O.dataspedfattura <= '$data' " ; $query = " select *, case when (O.DataSpedFattura = '0000-00-00') then '2002-01-01' else O.DataSpedFattura end as DataSpedizioneFattura, F.Nome as NomeFornitore, F.CodIban as CodIban, C.nome as NomeCliente, C.Indirizzo as IndirizzoCliente, C.Citta as CittaCliente, C.Provincia as ProvinciaCliente, P.modalita as TipoPagamento from $tordini O inner join $tpagamenti P on P.IdPagamenti = O.idpagamento inner join $tfornitori F on F.IdFornitori = O.IdFornitore inner join $tclienti C on C.IdClienti = O.IdCliente where $wfornitore and $wagente and $wcliente and $wpagamento and $wnumero and $wdata and O.saldofattura = 0 and O.NumFattura <> 0 and O.dataspedfattura <= '$data' order by NomeCliente, DataSpedizioneFattura" ; } debug (0, $query) ; list($n, $d) = $db->RecuperaDati($query) ; // print_r($d) ; exit ; for ($i=0; $i<$n; $i++) { $idC = $d['IdCliente'][$i] ; $idO = $d['IdOrdini'][$i] ; $nomeFornitore = str_replace("è", "e'", $d['NomeFornitore'][$i]) ; $nomeFornitore = str_replace("é", "e'", $nomeFornitore) ; $nomeFornitore = str_replace("à", "a'", $nomeFornitore) ; $nomeFornitore = str_replace("ì", "i'", $nomeFornitore) ; $nomeFornitore = str_replace("ò", "o'", $nomeFornitore) ; $CodIban = $d["CodIban"][$i] ; $nomeCliente = str_replace("è", "e'", $d['NomeCliente'][$i]) ; $nomeCliente = str_replace("é", "e'", $nomeCliente) ; $nomeCliente = str_replace("à", "a'", $nomeCliente) ; $nomeCliente = str_replace("ì", "i'", $nomeCliente) ; $nomeCliente = str_replace("ò", "o'", $nomeCliente) ; $indCliente = str_replace("è", "e'", $d['IndirizzoCliente'][$i]) ; $indCliente = str_replace("é", "e'", $indCliente) ; $indCliente = str_replace("à", "a'", $indCliente) ; $indCliente = str_replace("ì", "i'", $indCliente) ; $indCliente = str_replace("ò", "o'", $indCliente) ; $cittaCliente = str_replace("è", "e'", $d['CittaCliente'][$i]) ; $cittaCliente = str_replace("é", "e'", $cittaCliente) ; $cittaCliente = str_replace("à", "a'", $cittaCliente) ; $cittaCliente = str_replace("ì", "i'", $cittaCliente) ; $cittaCliente = str_replace("ò", "o'", $cittaCliente) ; $provCliente = str_replace("è", "e'", $d['ProvinciaCliente'][$i]) ; $provCliente = str_replace("é", "e'", $provCliente) ; $provCliente = str_replace("à", "a'", $provCliente) ; $provCliente = str_replace("ì", "i'", $provCliente) ; $provCliente = str_replace("ò", "o'", $provCliente) ; debug (0, "DataSpedizioneFattura = " . $d['DataSpedizioneFattura'][$i]) ; preg_match ("/([0-9]{4})[-\/]([0-9]{2})[-\/]([0-9]{2})/", $d['DataSpedizioneFattura'][$i], $reg_str) ; // ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $d['DataSpedizioneFattura'][$i], $reg_str) ; $ggpass = dateDiff(date("d"), date("m"), date("Y"), $reg_str[3], $reg_str[2], $reg_str[1]) ; if (! array_key_exists($idC, $arrVal)) { $arrVal[$idC] = array('NomeCliente' => $nomeCliente, 'IndirizzoCliente' => $indCliente, 'CittaCliente' => $cittaCliente, 'ProvinciaCliente' => $provCliente, 'ListaOrdini' => array() ) ; $arrTotSinCliente[$idC] = array('Incassato' => 0, 'NonIncassato' => 0 ) ; } if (! array_key_exists($idO, $arrVal[$idC]['ListaOrdini'])) { $arrVal[$idC]['ListaOrdini'][$idO] = array('Fornitore' => $nomeFornitore, 'Iban' => $CodIban, 'DataSpedFattura' => ItaDate($d['DataSpedFattura'][$i]), 'DataScadFattura' => ItaDate($d['DataScadFattura'][$i]), 'Giorni' => $ggpass, 'NumeroFattura' => $d['NumFattura'][$i], 'TipoPagamento' => $d['TipoPagamento'][$i], 'ImportoFattura' => nfe($d['ImportoFattura'][$i]), 'Incassata' => SiNo($d['SaldoFattura'][$i]), 'ImpIncassato' => nfe("0"), 'ImpNonIncassato' => nfe("0"), 'NumNA' => $d['NumNA'][$i], 'DataNA' => ItaDate($d['DataNA'][$i]), 'ImportoNA' => $d['ImportoNA'][$i], 'NumNC' => $d['NumNC'][$i], 'DataNC' => ItaDate($d['DataNC'][$i]), 'ImportoNC' => $d['ImportoNC'][$i], ) ; } if ($d['SaldoFattura'][$i]) { $arrVal[$idC]['ListaOrdini'][$idO]['ImpIncassato'] = nfe($d['ImportoFattura'][$i]) ; $arrTotSinCliente[$idC]['Incassato'] += $d['ImportoFattura'][$i] ; $totIncassato += $d['ImportoFattura'][$i] ; } else { $arrVal[$idC]['ListaOrdini'][$idO]['ImpNonIncassato'] = nfe($d['ImportoFattura'][$i]) ; $arrTotSinCliente[$idC]['NonIncassato'] += $d['ImportoFattura'][$i] ; $totNonIncassato += $d['ImportoFattura'][$i] ; } $totImporto += $d['ImportoFattura'][$i] ; $imponibile = nfe((100 * $d['ImportoFattura'][$i]) / 120) ; $totImponibile += ((100 * $d['ImportoFattura'][$i]) / 120) ; } // print_r($arrVal) ; exit ; /* print_r($arrTotSinCliente) ; print_r(nfe($arrTotSinCliente['13']['NonIncassato'] - $arrVal[13]['ListaOrdini']['58']['ImportoNC'])) ; exit ; */ class MYPDF extends TCPDF { //Page header public function Header() { $this->SetTextColor(255, 255, 255) ; $this->SetFillColor(0, 0, 255); // Set font $this->SetFont('helvetica', 'B', 10); // Title $this->Cell(12, 5, 'Cliente', "LTB", 0, 'L', 1) ; $this->SetFont('helvetica', 'B', 7); $this->Cell(55, 5, "Fornitore", "TB", 0, 'R', 1) ; $this->Cell(18, 5, "Num Fattura", "TB", 0, 'R', 1) ; $this->Cell(18, 5, "Pagamento", "TB", 0, 'R', 1) ; $this->Cell(22, 5, "Data Fattura", "TB", 0, 'R', 1) ; $this->Cell(22, 5, "Data Scadenza", "TB", 0, 'R', 1) ; $this->Cell(16, 5, "Giorni", "TB", 0, 'R', 1) ; $this->Cell(20, 5, "Importo ", "TBR", 1, 'R', 1) ; $this->SetFillColor(255, 255, 255); $this->SetFont('helvetica', '', 9) ; $this->Cell(150, 5, "Lista Fatture in data " . Date("d/m/Y") , "T", 1, 'L', 1) ; // $this->Cell(0, 0, "", B, 1, '', 1) ; } // Page footer public function Footer() { // Position at 15 mm from bottom $this->SetY(-15); // Set font $this->SetFont('helvetica', 'I', 8); // Page number $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M'); } } // create new PDF document $pdf = new MYPDF("P", PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // $pdf = new TCPDF("P", PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // set document information $pdf->SetCreator(PDF_CREATOR); $pdf->SetAuthor('Fabrizio Curcio'); $pdf->SetTitle('Lista Agenti'); $pdf->SetSubject('Lista Agenti'); $pdf->SetKeywords('PDF, lista, agenti, totali'); // set default header data $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING); // set header and footer fonts $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); //set margins $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); $pdf->SetHeaderMargin(PDF_MARGIN_HEADER + 5); $pdf->SetFooterMargin(PDF_MARGIN_FOOTER); //set auto page breaks $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM - 10); //set image scale factor $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); //set some language-dependent strings $pdf->setLanguageArray($l); // set default header data $pdf->SetHeaderData('', PDF_HEADER_LOGO_WIDTH, $hTitle, $hString); $pdf->setPrintHeader(true) ; // add a page $pdf->AddPage(); $pdf->SetDisplayMode('real') ; $pdf->SetDrawColor(0); $pdf->SetLineWidth(0.1); @reset($arrVal) ; foreach($arrVal as $idCliente => $arrValori) { $pdf->SetTextColor(255, 255, 255) ; $pdf->SetFillColor(0, 0, 255); $strCliente = $arrValori['NomeCliente'] . " - " . $arrValori['IndirizzoCliente'] . " - " . $arrValori['CittaCliente'] . " (" . $arrValori['ProvinciaCliente'] . ")" ; $pdf->SetFont('helvetica', '', 10) ; $pdf->Cell(0, 5, $strCliente , "LTR", 1, 'L', 1) ; $riga = 2 ; foreach($arrValori['ListaOrdini'] as $idOrdine => $arrValOrdini) { $pdf->SetFillColor(255, 255, 255) ; if (($riga % 2) == 0) $pdf->SetTextColor(255, 0, 0) ; else $pdf->SetTextColor(0, 159, 40) ; $riga ++ ; $pdf->SetFont('helvetica', '', 9) ; $pdf->Cell(75, 4, $arrValOrdini['Fornitore'], "L", 0, 'R', 1) ; $pdf->Cell(11, 4, $arrValOrdini['NumeroFattura'], "", 0, 'R', 1) ; $pdf->Cell(21, 4, $arrValOrdini['TipoPagamento'], 0, 0, 'R', 1) ; $pdf->Cell(20, 4, $arrValOrdini['DataSpedFattura'], 0, 0, 'R', 1) ; $pdf->Cell(20, 4, $arrValOrdini['DataScadFattura'], 0, 0, 'R', 1) ; $pdf->Cell(15, 4, $arrValOrdini['Giorni'], 0, 0, 'R', 1) ; $pdf->Cell(18, 4, $arrValOrdini['ImpNonIncassato'], "R", 1, 'R', 1) ; if ($arrValOrdini['NumNA'] > 0) { $pdf->Cell(84, 4, "NA: " . $arrValOrdini['NumNA'], 0, 0, 'R', 0) ; $pdf->Cell(40, 4, $arrValOrdini['DataNA'], 0, 0, 'R', 0) ; $pdf->Cell(56, 4, "-" . $arrValOrdini['ImportoNA'], 0, 1, 'R', 0) ; $arrTotSinCliente[$idCliente]['NonIncassato'] += $arrValOrdini['ImportoNA'] ; } if ($arrValOrdini['NumNC'] > 0) { $pdf->Cell(84, 4, "NC: " . $arrValOrdini['NumNC'], "L", 0, 'R', 0) ; $pdf->Cell(40, 4, $arrValOrdini['DataNC'], 0, 0, 'R', 0) ; $pdf->Cell(56, 4, "-" . $arrValOrdini['ImportoNC'], "R", 1, 'R', 0) ; $arrTotSinCliente[$idCliente]['NonIncassato'] -= $arrValOrdini['ImportoNC'] ; } /****************************************************** * 21.9.21 * Si stampa anche l'eventuale IBAN del fornitore *******************************************************/ if ($arrValOrdini['Iban'] != "") { $pdf->SetFont('helvetica', '', 6) ; $pdf->Cell(75, 5, "(IBAN: " . $arrValOrdini['Iban'] . ")", "L", 0, 'R', 0) ; $pdf->Cell(105, 5, "", "R", 1, 'R', 0) ; } } // $pdf->Cell(160, 0, "", "L", 0, 'R', 1) ; // $pdf->Cell(20, 0, "------------", "R", 1, 'R', 1) ; $pdf->SetFont('helvetica', '', 9) ; $pdf->SetFillColor(255, 255, 255); $pdf->SetTextColor(0, 0, 255) ; $pdf->Cell(100, 5, "", "LB", 0, 'L', 1) ; $pdf->Cell(60, 5, "Saldo Totale Cliente", "B", 0, 'R', 1) ; $pdf->Cell(20, 5, nfe($arrTotSinCliente[$idCliente]['NonIncassato']), "BR", 1, 'R', 1) ; $pdf->Ln() ; } $pdf->SetFont('helvetica', 'B', 10) ; $pdf->Cell(100, 5, "", "LTB", 0, 'L', 0) ; $pdf->Cell(50, 5, "Totale Agenzia", "TB", 0, 'L', 0) ; $pdf->Cell(30, 5, nfe($totNonIncassato), "TBR", 1, 'R', 0) ; // $pdf->Cell(0, 0, "", B, 1, '', 1) ; $pdf->Ln() ; //Close and output PDF document $pdf->Output('FattureInScadenza.pdf', 'I') ; exit ; ?>