2017-08-04 16:28:16 +02:00
< ? php
include_once __DIR__ . '/../../core.php' ;
2017-09-21 15:51:39 +02:00
$idconto = $_GET [ 'idconto' ];
2017-08-04 16:28:16 +02:00
$module_name = 'Piano dei conti' ;
2018-07-08 15:41:38 +02:00
$date_start = $_SESSION [ 'period_start' ];
$date_end = $_SESSION [ 'period_end' ];
2017-08-04 16:28:16 +02:00
// carica report html
$report = file_get_contents ( $docroot . '/templates/partitario_mastrino/partitario.html' );
$body = file_get_contents ( $docroot . '/templates/partitario_mastrino/partitario_body.html' );
include_once $docroot . '/templates/pdfgen_variables.php' ;
// Calcolo il percorso piano dei conti
2017-09-21 15:51:39 +02:00
if ( $_GET [ 'lev' ] == '3' ) {
2017-08-04 16:28:16 +02:00
$rs = $dbo -> fetchArray ( " SELECT idpianodeiconti2, CONCAT_WS(' ', numero, descrizione ) AS descrizione FROM co_pianodeiconti3 WHERE id= \" " . $idconto . '"' );
$percorso = $rs [ 0 ][ 'descrizione' ];
$idpianodeiconti2 = $rs [ 0 ][ 'idpianodeiconti2' ];
$rs = $dbo -> fetchArray ( " SELECT idpianodeiconti1, CONCAT_WS(' ', numero, descrizione ) AS descrizione FROM co_pianodeiconti2 WHERE id= \" " . $idpianodeiconti2 . '"' );
$percorso = $rs [ 0 ][ 'descrizione' ] . '<br> ' . $percorso ;
$idpianodeiconti1 = $rs [ 0 ][ 'idpianodeiconti1' ];
$rs = $dbo -> fetchArray ( " SELECT CONCAT_WS(' ', numero, descrizione ) AS descrizione FROM co_pianodeiconti1 WHERE id= \" " . $idpianodeiconti1 . '"' );
( $rs [ 0 ][ 'descrizione' ] == '01 Patrimoniale' ) ? $descrizione = 'Stato patrimoniale' : $descrizione = 'Conto economico' ;
$percorso = $descrizione . '<br> ' . $percorso ;
2017-09-21 15:51:39 +02:00
} elseif ( $_GET [ 'lev' ] == '2' ) {
2017-08-04 16:28:16 +02:00
$rs = $dbo -> fetchArray ( " SELECT idpianodeiconti1, CONCAT_WS(' ', numero, descrizione ) AS descrizione FROM co_pianodeiconti2 WHERE id= \" " . $idconto . '"' );
$percorso = $rs [ 0 ][ 'descrizione' ] . '<br> ' . $percorso ;
$idpianodeiconti1 = $rs [ 0 ][ 'idpianodeiconti1' ];
$rs = $dbo -> fetchArray ( " SELECT CONCAT_WS(' ', numero, descrizione ) AS descrizione FROM co_pianodeiconti1 WHERE id= \" " . $idpianodeiconti1 . '"' );
( $rs [ 0 ][ 'descrizione' ] == '01 Patrimoniale' ) ? $descrizione = 'Stato patrimoniale' : $descrizione = 'Conto economico' ;
$percorso = $descrizione . '<br> ' . $percorso ;
2017-09-21 15:51:39 +02:00
} elseif ( $_GET [ 'lev' ] == '1' ) {
2017-08-04 16:28:16 +02:00
$rs = $dbo -> fetchArray ( " SELECT CONCAT_WS(' ', numero, descrizione ) AS descrizione FROM co_pianodeiconti1 WHERE id= \" " . $idconto . '"' );
( $rs [ 0 ][ 'descrizione' ] == '01 Patrimoniale' ) ? $descrizione = 'Stato patrimoniale' : $descrizione = 'Conto economico' ;
$percorso = $descrizione . '<br> <br> ' ;
}
$body = str_replace ( '|percorso|' , $percorso , $body );
$body = str_replace ( '|info_fornitore|' , $f_ragionesociale . '<br>' . $f_indirizzo . '<br>' . $f_citta , $body );
2018-07-08 15:41:38 +02:00
$body = str_replace ( '|period_start|' , Translator :: dateToLocale ( $date_start ), $body );
$body = str_replace ( '|period_end|' , Translator :: dateToLocale ( $date_end ), $body );
2017-08-04 16:28:16 +02:00
// Stampa da livello 3
2017-09-21 15:51:39 +02:00
if ( $_GET [ 'lev' ] == '3' ) {
2017-08-04 16:28:16 +02:00
$body .= " <table style='table-layout:fixed; border-bottom:1px solid #777; border-right:1px solid #777; border-left:1px solid #777;' cellpadding='0' cellspacing='0'>
< col width = '80' >< col width = '452' >< col width = '80' >< col width = '80' >
< tbody > \n " ;
2018-02-04 17:26:25 +01:00
// Inizializzo saldo finale
2018-02-10 19:42:52 +01:00
$saldo_finale = [];
2017-08-04 16:28:16 +02:00
2018-02-04 17:26:25 +01:00
// Calcolo saldo iniziale
2019-11-22 17:15:17 +01:00
$rs = $dbo -> fetchArray ( 'SELECT SUM(totale) AS totale FROM co_movimenti WHERE idconto="' . $idconto . '" AND data < ' . prepare ( $date_start ) . '' );
2017-08-04 16:28:16 +02:00
$saldo_iniziale = $rs [ 0 ][ 'totale' ];
$saldo_finale = $saldo_iniziale ;
if ( $saldo_iniziale < 0 ) {
$dare = '' ;
$avere = abs ( $saldo_iniziale );
} else {
$dare = abs ( $saldo_iniziale );
$avere = '' ;
}
2019-05-02 10:03:57 +02:00
$body .= " <tr><td class='br bb padded'></td><td class='br bb padded'><b>SALDO INIZIALE</b></td><td class='br bb padded text-right'><b> " . moneyFormat ( abs ( $dare ), 2 ) . " </b></td><td class='bb padded text-right'><b> " . moneyFormat ( abs ( $avere ), 2 ) . " </b></td></tr> \n " ;
2017-08-04 16:28:16 +02:00
2019-11-22 17:15:17 +01:00
$rs = $dbo -> fetchArray ( ' SELECT * , SUM ( totale ) AS totale
FROM co_movimenti
WHERE idconto = '.prepare($idconto).' AND
data >= '.prepare($date_start).' AND
data <= '.prepare($date_end).'
GROUP BY idmastrino
ORDER BY data ASC ' );
2019-04-04 17:30:58 +02:00
// Inizializzo saldo finale
$saldo_finale2 = [];
2017-08-04 16:28:16 +02:00
for ( $i = 0 ; $i < sizeof ( $rs ); ++ $i ) {
if ( $rs [ $i ][ 'totale' ] >= 0 ) {
2019-05-02 10:03:57 +02:00
$dare = moneyFormat ( abs ( $rs [ $i ][ 'totale' ]), 2 );
2017-08-04 16:28:16 +02:00
$avere = '' ;
} else {
$dare = '' ;
2019-05-02 10:03:57 +02:00
$avere = moneyFormat ( abs ( $rs [ $i ][ 'totale' ]), 2 );
2017-08-04 16:28:16 +02:00
}
$body .= " <tr><td class='br bb padded text-center'> " . Translator :: dateToLocale ( $rs [ $i ][ 'data' ]) . " </td><td class='br bb padded'> " . $rs [ $i ][ 'descrizione' ] . " </td><td class='br bb padded text-right'> " . $dare . " </td><td class='bb padded text-right'> " . $avere . " </td></tr> \n " ;
2019-04-04 10:50:29 +02:00
$saldo_finale2 [] = $rs [ $i ][ 'totale' ];
2017-08-04 16:28:16 +02:00
}
2018-02-18 19:53:23 +01:00
if ( sum ( $saldo_finale ) < 0 ) {
2017-08-04 16:28:16 +02:00
$dare = '' ;
2019-04-04 17:30:58 +02:00
$avere = abs ( sum ( $saldo_finale ) + sum ( $saldo_finale2 ));
2017-08-04 16:28:16 +02:00
} else {
2019-04-04 17:30:58 +02:00
$dare = abs ( sum ( $saldo_finale ) + sum ( $saldo_finale2 ));
2017-08-04 16:28:16 +02:00
$avere = '' ;
}
2018-02-04 17:26:25 +01:00
// Mostro il saldo finale
2019-05-02 10:03:57 +02:00
$body .= " <tr><td class='br bb padded'></td><td class='br bb padded'><b>SALDO FINALE</b></td><td class='br bb padded text-right'><b> " . moneyFormat ( abs ( sum ( $dare )), 2 ) . " </b></td><td class='bb padded text-right'><b> " . moneyFormat ( abs ( sum ( $avere )), 2 ) . " </b></td></tr> \n " ;
2017-08-04 16:28:16 +02:00
$body .= " </tbody>
</ table > \n " ;
}
// Stampa da livello 2
2017-09-21 15:51:39 +02:00
elseif ( $_GET [ 'lev' ] == '2' ) {
2017-08-04 16:28:16 +02:00
$body .= " <table style='table-layout:fixed; border-bottom:1px solid #777; border-right:1px solid #777; border-left:1px solid #777;' cellpadding='0' cellspacing='0'>
< col width = '80' >< col width = '452' >< col width = '80' >< col width = '80' >
< tbody > \n " ;
2018-02-04 17:26:25 +01:00
// Ciclo fra i sotto-conti di livello 2
$rs3 = $dbo -> fetchArray ( 'SELECT id, numero, descrizione FROM co_pianodeiconti3 WHERE idpianodeiconti2="' . $idconto . '"' );
2017-08-04 16:28:16 +02:00
for ( $z = 0 ; $z < sizeof ( $rs3 ); ++ $z ) {
2018-02-10 19:42:52 +01:00
$v_dare = [];
$v_avere = [];
2017-08-04 16:28:16 +02:00
// Inizializzo saldo finale
2018-02-10 19:42:52 +01:00
$saldo_finale = [];
2017-08-04 16:28:16 +02:00
2018-02-04 17:26:25 +01:00
// Calcolo saldo iniziale
2019-11-22 17:15:17 +01:00
$rs = $dbo -> fetchArray ( 'SELECT SUM(totale) AS totale FROM co_movimenti WHERE idconto="' . $rs3 [ $z ][ 'id' ] . '" AND data < ' . prepare ( $date_start ) . '' );
2017-08-04 16:28:16 +02:00
$saldo_iniziale = $rs [ 0 ][ 'totale' ];
2018-02-10 19:42:52 +01:00
$saldo_finale [] = $saldo_iniziale ;
2017-08-04 16:28:16 +02:00
2018-02-18 19:53:23 +01:00
if ( $saldo_iniziale < 0 ) {
$v_avere [] = abs ( $saldo_iniziale );
2017-08-04 16:28:16 +02:00
} else {
2018-02-18 19:53:23 +01:00
$v_dare [] = abs ( $saldo_iniziale );
2017-08-04 16:28:16 +02:00
}
2019-11-22 17:15:17 +01:00
$rs = $dbo -> fetchArray ( 'SELECT * FROM co_movimenti WHERE idconto="' . $rs3 [ $z ][ 'id' ] . '" AND data >= ' . prepare ( $date_start ) . ' AND data <= ' . prepare ( $date_end ) . ' ORDER BY data ASC' );
2017-08-04 16:28:16 +02:00
for ( $i = 0 ; $i < sizeof ( $rs ); ++ $i ) {
if ( $rs [ $i ][ 'totale' ] >= 0 ) {
2018-02-10 19:42:52 +01:00
$v_dare [] = abs ( $rs [ $i ][ 'totale' ]);
2017-08-04 16:28:16 +02:00
} else {
2018-02-10 19:42:52 +01:00
$v_avere [] = abs ( $rs [ $i ][ 'totale' ]);
2017-08-04 16:28:16 +02:00
}
}
2018-02-10 19:42:52 +01:00
$totale = sum ( $v_dare ) - sum ( $v_avere );
2017-08-04 16:28:16 +02:00
if ( $totale >= 0 ) {
2019-05-02 10:03:57 +02:00
$dare = moneyFormat ( abs ( $totale ), 2 );
2017-08-04 16:28:16 +02:00
$avere = '' ;
} else {
$dare = '' ;
2019-05-02 10:03:57 +02:00
$avere = moneyFormat ( abs ( $totale ), 2 );
2017-08-04 16:28:16 +02:00
}
2018-02-04 17:26:25 +01:00
// Mostro il saldo finale del conto di livello 3
$body .= " <tr><td class='br bb padded'></td><td class='br bb padded'> " . $rs3 [ $z ][ 'numero' ] . ' ' . $rs3 [ $z ][ 'descrizione' ] . " </td><td class='br bb padded text-right'> " . $dare . " </td><td class='bb padded text-right'> " . $avere . " </td></tr> \n " ;
2017-08-04 16:28:16 +02:00
}
$body .= " </tbody>
</ table > \n " ;
}
// Stampa completa bilancio
2017-09-04 10:24:44 +02:00
elseif ( get ( 'lev' ) == '1' ) {
2018-02-10 19:42:52 +01:00
$ricavi = [];
$costi = [];
$totale_attivita = [];
$totale_passivita = [];
2017-08-04 16:28:16 +02:00
$body .= " <table style='table-layout:fixed; border-bottom:1px solid #777; border-right:1px solid #777; border-left:1px solid #777;' cellpadding='0' cellspacing='0'>
< col width = '80' >< col width = '452' >< col width = '80' >< col width = '80' >
< tbody > \n " ;
2018-02-04 17:26:25 +01:00
// Ciclo fra il conto principale scelto (Economico o Patrimoniale)
$rs1 = $dbo -> fetchArray ( 'SELECT id, numero, descrizione FROM co_pianodeiconti1 WHERE id="' . $idconto . '" ORDER BY numero DESC' );
2017-08-04 16:28:16 +02:00
for ( $x = 0 ; $x < sizeof ( $rs1 ); ++ $x ) {
// Ciclo fra i sotto-conti di livello 1
2018-02-04 17:26:25 +01:00
$rs2 = $dbo -> fetchArray ( 'SELECT id, numero, descrizione FROM co_pianodeiconti2 WHERE idpianodeiconti1="' . $rs1 [ $x ][ 'id' ] . '"' );
2017-08-04 16:28:16 +02:00
for ( $y = 0 ; $y < sizeof ( $rs2 ); ++ $y ) {
$body .= " <tr><th class='bb padded' colspan='4'><b> " . $rs2 [ $y ][ 'numero' ] . ' ' . $rs2 [ $y ][ 'descrizione' ] . " </b></th></tr> \n " ;
2018-02-04 17:26:25 +01:00
// Ciclo fra i sotto-conti di livello 2
$rs3 = $dbo -> fetchArray ( 'SELECT id, numero, descrizione FROM co_pianodeiconti3 WHERE idpianodeiconti2="' . $rs2 [ $y ][ 'id' ] . '"' );
2017-08-04 16:28:16 +02:00
for ( $z = 0 ; $z < sizeof ( $rs3 ); ++ $z ) {
2018-02-10 19:42:52 +01:00
$v_dare = [];
$v_avere = [];
2017-08-04 16:28:16 +02:00
2019-11-22 17:15:17 +01:00
$rs = $dbo -> fetchArray ( 'SELECT * FROM co_movimenti WHERE idconto="' . $rs3 [ $z ][ 'id' ] . '" AND data >= ' . prepare ( $date_start ) . ' AND data <= ' . prepare ( $date_end ) . ' ORDER BY data ASC' );
2017-08-04 16:28:16 +02:00
for ( $i = 0 ; $i < sizeof ( $rs ); ++ $i ) {
if ( $rs [ $i ][ 'totale' ] >= 0 ) {
2018-02-10 19:42:52 +01:00
$v_dare [] = abs ( $rs [ $i ][ 'totale' ]);
2017-08-04 16:28:16 +02:00
} else {
2018-02-10 19:42:52 +01:00
$v_avere [] = abs ( $rs [ $i ][ 'totale' ]);
2017-08-04 16:28:16 +02:00
}
}
2018-02-10 19:42:52 +01:00
$totale = sum ( $v_dare ) - sum ( $v_avere );
2017-08-04 16:28:16 +02:00
if ( $totale >= 0 ) {
$dare = abs ( $totale );
$avere = 0 ;
2018-02-10 19:42:52 +01:00
$totale_attivita [] = $dare ;
$costi [] = abs ( $dare );
2017-08-04 16:28:16 +02:00
} else {
$dare = 0 ;
$avere = abs ( $totale );
2018-02-10 19:42:52 +01:00
$totale_passivita [] = $avere ;
$ricavi [] = abs ( $avere );
2017-08-04 16:28:16 +02:00
}
2018-02-04 17:26:25 +01:00
// Mostro il saldo finale del conto di livello 3
2019-05-04 00:32:28 +02:00
$body .= " <tr><td class='br bb padded'></td><td class='br bb padded'> " . $rs3 [ $z ][ 'numero' ] . ' ' . $rs3 [ $z ][ 'descrizione' ] . " </td><td class='br bb padded text-right'> " . moneyFormat ( abs ( $dare ), 2 ) . " </td><td class='bb padded text-right'> " . moneyFormat ( abs ( $avere ), 2 ) . " </td></tr> \n " ;
2017-08-04 16:28:16 +02:00
}
}
}
// Stampa "Costi/Ricavi" se conto economico
if ( $rs1 [ 0 ][ 'descrizione' ] == 'Economico' ) {
2019-05-02 10:03:57 +02:00
$body .= " <tr><th colspan='3' class='br bb padded'>RICAVI</th><th align='right' class='bb padded'> " . moneyFormat ( sum ( $ricavi ), 2 ) . " </th></tr> \n " ;
2019-05-04 00:32:28 +02:00
$body .= " <tr><th colspan='3' class='br bb padded'>COSTI</th><th align='right' class='bb padded'> " . moneyFormat ( sum ( $costi ), 2 ) . " </th></tr> \n " ;
$body .= " <tr><th colspan='3' class='br padded'>UTILE</th><th class='padded' align='right'> " . moneyFormat ( sum ( $ricavi ) - sum ( $costi ), 2 ) . " </th></tr> \n " ;
2017-08-04 16:28:16 +02:00
}
// Stampa "Attività/Passività" se stato patrimoniale
else {
2018-02-10 19:42:52 +01:00
$costi = [];
$ricavi = [];
2017-08-04 16:28:16 +02:00
// Ciclo fra il conto economico per calcolare l'utile o la perdita
$rs1 = $dbo -> fetchArray ( 'SELECT id, numero, descrizione FROM co_pianodeiconti1 WHERE NOT id="' . $idconto . '" ORDER BY numero DESC' );
for ( $x = 0 ; $x < sizeof ( $rs1 ); ++ $x ) {
// Ciclo fra i sotto-conti di livello 1
$rs2 = $dbo -> fetchArray ( 'SELECT id, numero, descrizione FROM co_pianodeiconti2 WHERE idpianodeiconti1="' . $rs1 [ $x ][ 'id' ] . '"' );
for ( $y = 0 ; $y < sizeof ( $rs2 ); ++ $y ) {
// Ciclo fra i sotto-conti di livello 2
$rs3 = $dbo -> fetchArray ( 'SELECT id, numero, descrizione FROM co_pianodeiconti3 WHERE idpianodeiconti2="' . $rs2 [ $y ][ 'id' ] . '"' );
for ( $z = 0 ; $z < sizeof ( $rs3 ); ++ $z ) {
// Inizializzo saldo finale
2018-02-10 19:42:52 +01:00
$saldo_finale = [];
2017-08-04 16:28:16 +02:00
// Calcolo saldo iniziale
2019-11-22 17:15:17 +01:00
$rs = $dbo -> fetchArray ( 'SELECT SUM(totale) AS totale FROM co_movimenti WHERE idconto="' . $rs2 [ $y ][ 'id' ] . '" AND data < ' . prepare ( $date_start ) . '' );
2018-02-10 19:42:52 +01:00
$dare = [];
$avere = [];
2017-08-04 16:28:16 +02:00
2019-11-22 17:15:17 +01:00
$rs = $dbo -> fetchArray ( 'SELECT * FROM co_movimenti WHERE idconto="' . $rs3 [ $z ][ 'id' ] . '" AND data >= ' . prepare ( $date_start ) . ' AND data <= ' . prepare ( $date_end ) . ' ORDER BY data ASC' );
2017-08-04 16:28:16 +02:00
for ( $i = 0 ; $i < sizeof ( $rs ); ++ $i ) {
if ( $rs [ $i ][ 'totale' ] >= 0 ) {
2018-02-10 19:42:52 +01:00
$dare [] = abs ( $rs [ $i ][ 'totale' ]);
2017-08-04 16:28:16 +02:00
} else {
2018-02-10 19:42:52 +01:00
$avere [] = abs ( $rs [ $i ][ 'totale' ]);
2017-08-04 16:28:16 +02:00
}
}
2018-02-10 19:42:52 +01:00
$totale = sum ( $dare ) - sum ( $avere );
2017-08-04 16:28:16 +02:00
if ( $totale >= 0 ) {
2018-02-10 19:42:52 +01:00
$costi [] = abs ( $totale );
2017-08-04 16:28:16 +02:00
} else {
2018-02-10 19:42:52 +01:00
$ricavi [] = abs ( $totale );
2017-08-04 16:28:16 +02:00
}
}
}
}
$body .= " </tbody> \n " ;
$body .= " </table> \n " ;
// Tabella di riepilogo finale
2018-02-18 19:53:23 +01:00
$totale_attivita = abs ( sum ( $totale_attivita ));
$totale_passivita = abs ( sum ( $totale_passivita ));
$utile_perdita = abs ( sum ( $ricavi )) - abs ( sum ( $costi ));
2017-08-04 16:28:16 +02:00
if ( $utile_perdita < 0 ) {
$pareggio1 = $totale_attivita + abs ( $utile_perdita );
$pareggio2 = abs ( $totale_passivita );
} else {
$pareggio1 = $totale_attivita ;
$pareggio2 = abs ( $totale_passivita ) + abs ( $utile_perdita );
}
$body .= " <table style='table-layout:fixed; border-bottom:1px solid #777; border-right:1px solid #777; border-left:1px solid #777;' cellpadding='0' cellspacing='0'>
< col width = '173' >< col width = '173' >< col width = '173' >< col width = '173' >
< tbody > \n " ;
// Attività
2019-05-02 10:03:57 +02:00
$body .= " <tr><th class='br bb padded'>TOTALE ATTIVITÀ</th><th align='right' class='bb br padded'> " . moneyFormat ( $totale_attivita , 2 ) . " </th> \n " ;
2017-08-04 16:28:16 +02:00
// Passività
2019-05-02 10:03:57 +02:00
$body .= " <th class='br bb padded'>PASSIVITÀ</th><th align='right' class='bb padded'> " . moneyFormat ( $totale_passivita , 2 ) . " </th></tr> \n " ;
2017-08-04 16:28:16 +02:00
if ( $utile_perdita < 0 ) {
// Perdita d'esercizio
2019-05-02 10:03:57 +02:00
$body .= " <tr><th class='br bb padded'>PERDITA D'ESERCIZIO</th><th align='right' class='bb br padded'> " . moneyFormat ( abs ( $utile_perdita ), 2 ) . " </th> \n " ;
2017-08-04 16:28:16 +02:00
// Utile
$body .= " <th class='br bb padded'> </th><th align='right' class='bb padded'> </th></tr> \n " ;
} else {
// Perdita d'esercizio
$body .= " <tr><th class='br bb padded'> </th><th align='right' class='bb br padded'> </th> \n " ;
// Utile
2019-05-04 00:32:28 +02:00
$body .= " <th class='br bb padded'>UTILE</th><th align='right' class='bb padded'> " . moneyFormat ( abs ( $utile_perdita ), 2 ) . " </th></tr> \n " ;
2017-08-04 16:28:16 +02:00
}
// PAREGGIO 1
2019-05-02 10:03:57 +02:00
$body .= " <tr><th class='br padded'>TOTALE A PAREGGIO</th><th align='right' class='br padded'> " . moneyFormat ( $pareggio1 , 2 ) . " </th> \n " ;
2017-08-04 16:28:16 +02:00
// PAREGGIO 2
2019-05-02 10:03:57 +02:00
$body .= " <th class='br padded'>TOTALE A PAREGGIO</th><th align='right' class='padded'> " . moneyFormat ( $pareggio2 , 2 ) . " </th></tr> \n " ;
2017-08-04 16:28:16 +02:00
}
$body .= " </tbody>
</ table > \n " ;
}