2017-08-04 16:28:16 +02:00
< ? php
include_once __DIR__ . '/../../core.php' ;
2019-12-21 19:07:02 +01:00
// Verifico se è già stata eseguita l'apertura bilancio
$bilancio_gia_aperto = $dbo -> fetchNum ( 'SELECT id FROM co_movimenti WHERE is_apertura=1 AND data BETWEEN ' . prepare ( $_SESSION [ 'period_start' ]) . ' AND ' . prepare ( $_SESSION [ 'period_end' ]));
$msg = tr ( 'Sei sicuro di voler aprire il bilancio?' );
$btn_class = 'btn-info' ;
2019-12-24 12:36:35 +01:00
if ( $bilancio_gia_aperto ) {
2019-12-21 19:07:02 +01:00
$msg .= ' ' . tr ( 'I movimenti di apertura già esistenti verranno annullati e ricreati' ) . '.' ;
$btn_class = 'btn-default' ;
}
?>
< div class = " text-right " >
< button type = " button " class = " btn btn-lg <?php echo $btn_class ; ?> " data - op = " apri-bilancio " data - title = " <?php echo tr('Apertura bilancio'); ?> " data - backto = " record-list " data - msg = " <?php echo $msg ; ?> " data - button = " <?php echo tr('Riprendi saldi'); ?> " data - class = " btn btn-lg btn-warning " onclick = " message( this ); " >< i class = " fa fa-folder-open " ></ i > < ? php echo tr ( 'Apertura bilancio' ); ?> </button>
</ div >
< ? php
2019-07-08 15:48:48 +02:00
// Livello 1
2020-05-18 17:38:14 +02:00
$query1 = 'SELECT * FROM `co_pianodeiconti1` ORDER BY id ASC' ;
2019-07-08 15:48:48 +02:00
$primo_livello = $dbo -> fetchArray ( $query1 );
2017-08-04 16:28:16 +02:00
2019-07-08 17:11:48 +02:00
foreach ( $primo_livello as $conto_primo ) {
2018-02-10 19:42:52 +01:00
$totale_attivita = [];
$totale_passivita = [];
2017-08-04 16:28:16 +02:00
2018-02-10 19:42:52 +01:00
$costi = [];
$ricavi = [];
2019-07-08 17:11:48 +02:00
$titolo = $conto_primo [ 'descrizione' ] == 'Economico' ? tr ( 'Conto economico' ) : tr ( 'Stato patrimoniale' );
2019-07-08 15:48:48 +02:00
echo '
< hr >
< h2 > '.$titolo.' </ h2 >
< div class = " pull-right " >
< br > '.Prints::getLink(' Mastrino ', $conto_primo[' id '], null, tr(' Stampa '), null, ' lev = 1 ').'
</ div >
< div class = " clearfix " ></ div >
2019-12-27 10:28:53 +01:00
2019-07-08 15:48:48 +02:00
< div style = " padding-left:10px; " > ' ;
2017-08-04 16:28:16 +02:00
2017-09-14 18:48:45 +02:00
// Livello 2
2019-07-08 15:48:48 +02:00
$query2 = " SELECT * FROM `co_pianodeiconti2` WHERE idpianodeiconti1=' " . $conto_primo [ 'id' ] . " ' ORDER BY numero ASC " ;
$secondo_livello = $dbo -> fetchArray ( $query2 );
2017-08-04 16:28:16 +02:00
2019-07-08 17:11:48 +02:00
foreach ( $secondo_livello as $conto_secondo ) {
2017-09-14 18:48:45 +02:00
// Livello 2
2019-07-08 15:48:48 +02:00
echo '
2019-12-27 10:28:53 +01:00
< div class = " pull-right " >
2019-07-08 15:48:48 +02:00
'.Prints::getLink(' Mastrino ', $conto_secondo[' id '], ' btn - info btn - xs ', ' ', null, ' lev = 2 ').'
2019-12-27 10:28:53 +01:00
< button type = " button " class = " btn btn-warning btn-xs " data - toggle = " tooltip " title = " Modifica questo conto... " onclick = " launch_modal( \ 'Modifica conto \ ', \ ''. $structure->fileurl ('edit_conto.php').'?id='. $conto_secondo['id'] .'&lvl=2 \ '); " >
< i class = " fa fa-edit " ></ i >
</ button >
2019-07-08 15:48:48 +02:00
</ div >
2019-12-27 10:28:53 +01:00
< h5 >< b > '.$conto_secondo[' numero '].' '.$conto_secondo[' descrizione '].' </ b ></ h5 >
2019-07-08 15:48:48 +02:00
< div style = " padding-left:10px; " >
< table class = " table table-striped table-hover table-condensed " style = " margin-bottom:0; " > ' ;
2019-07-08 17:11:48 +02:00
2017-09-14 18:48:45 +02:00
// Livello 3
2020-02-25 16:12:06 +01:00
$query3 = 'SELECT `co_pianodeiconti3`.*, movimenti.numero_movimenti, movimenti.totale, anagrafica.idanagrafica, anagrafica.deleted_at FROM `co_pianodeiconti3` LEFT OUTER JOIN (SELECT idanagrafica, idconto_cliente, idconto_fornitore, deleted_at FROM an_anagrafiche) AS anagrafica ON co_pianodeiconti3.id IN (anagrafica.idconto_cliente, idconto_fornitore) LEFT OUTER JOIN (SELECT COUNT(idconto) AS numero_movimenti, idconto, SUM( ROUND(totale,2)) AS totale FROM co_movimenti WHERE data BETWEEN ' . prepare ( $_SESSION [ 'period_start' ]) . ' AND ' . prepare ( $_SESSION [ 'period_end' ]) . ' GROUP BY idconto) movimenti ON co_pianodeiconti3.id=movimenti.idconto WHERE `idpianodeiconti2` = ' . prepare ( $conto_secondo [ 'id' ]) . ' ORDER BY numero ASC' ;
2019-07-08 15:48:48 +02:00
$terzo_livello = $dbo -> fetchArray ( $query3 );
2017-08-04 16:28:16 +02:00
2019-07-08 17:11:48 +02:00
foreach ( $terzo_livello as $conto_terzo ) {
2017-09-14 18:48:45 +02:00
// Se il conto non ha documenti collegati posso eliminarlo
2020-02-05 14:51:39 +01:00
$movimenti = $conto_terzo [ 'numero_movimenti' ];
2017-08-04 16:28:16 +02:00
2020-01-28 16:09:20 +01:00
$totale_conto = $conto_terzo [ 'totale' ];
2019-07-08 15:48:48 +02:00
$totale_conto = ( $conto_primo [ 'descrizione' ] == 'Patrimoniale' ) ? $totale_conto : - $totale_conto ;
// Somma dei totali
if ( $conto_primo [ 'descrizione' ] == 'Patrimoniale' ) {
if ( $totale_conto > 0 ) {
$totale_attivita [] = abs ( $totale_conto );
} else {
$totale_passivita [] = abs ( $totale_conto );
}
} else {
if ( $totale_conto > 0 ) {
$totale_ricavi [] = abs ( $totale_conto );
} else {
$totale_costi [] = abs ( $totale_conto );
}
2017-09-14 18:48:45 +02:00
}
2017-08-04 16:28:16 +02:00
2019-07-08 15:48:48 +02:00
echo '
2020-02-25 16:12:06 +01:00
< tr >
< td > ' ;
2020-02-27 16:19:07 +01:00
2020-02-25 16:12:06 +01:00
// Possibilità di esplodere i movimenti del conto
2019-07-08 15:48:48 +02:00
if ( ! empty ( $movimenti )) {
echo '
< a href = " javascript:; " class = " btn btn-primary btn-xs plus-btn " >< i class = " fa fa-plus " ></ i ></ a > ' ;
2017-08-04 16:28:16 +02:00
}
2020-02-25 16:12:06 +01:00
// Span con i pulsanti
2019-07-08 15:48:48 +02:00
echo '
2019-12-27 10:28:53 +01:00
< span class = " hide tools pull-right " > ' ;
2017-08-04 16:28:16 +02:00
2020-02-25 16:12:06 +01:00
// Possibilità di visionare l'anagrafica
$id_anagrafica = $conto_terzo [ 'idanagrafica' ];
$anagrafica_deleted = $conto_terzo [ 'deleted_at' ];
2020-02-27 16:19:07 +01:00
echo isset ( $id_anagrafica ) ? Modules :: link ( 'Anagrafiche' , $id_anagrafica , ' <i title="' . ( isset ( $anagrafica_deleted ) ? 'Anagrafica eliminata' : 'Visualizza anagrafica' ) . '" class="btn btn-' . ( isset ( $anagrafica_deleted ) ? 'danger' : 'primary' ) . ' btn-xs fa fa-user" ></i>' , null ) : '' ;
2020-02-25 16:12:06 +01:00
2019-07-08 15:48:48 +02:00
// Stampa mastrino
if ( ! empty ( $movimenti )) {
2017-09-14 18:48:45 +02:00
echo '
2019-07-08 15:48:48 +02:00
'.Prints::getLink(' Mastrino ', $conto_terzo[' id '], ' btn - info btn - xs ', ' ', null, ' lev = 3 ' );
2017-09-14 18:48:45 +02:00
}
2017-08-04 16:28:16 +02:00
2020-02-25 16:12:06 +01:00
// Possibilità di modificare il nome del conto livello3
echo '
< button type = " button " class = " btn btn-warning btn-xs " data - toggle = " tooltip " title = " Modifica questo conto... " onclick = " launch_modal( \ 'Modifica conto \ ', \ ''. $structure->fileurl ('edit_conto.php').'?id='. $conto_terzo['id'] .' \ '); " >
< i class = " fa fa-edit " ></ i >
</ button > ' ;
// Possibilità di eliminare il conto se non ci sono movimenti collegati
2019-12-27 10:28:53 +01:00
if ( $numero_movimenti <= 0 ) {
2019-07-08 15:48:48 +02:00
echo '
< a class = " btn btn-danger btn-xs ask " data - toggle = " tooltip " title = " '.tr('Elimina').' " data - backto = " record-list " data - op = " del " data - idconto = " '. $conto_terzo['id'] .' " >
< i class = " fa fa-trash " ></ i >
</ a > ' ;
}
2017-08-04 16:28:16 +02:00
2020-02-25 16:12:06 +01:00
echo ' </span>' ;
// Span con info del conto
2019-12-27 10:28:53 +01:00
echo '
2020-02-25 16:12:06 +01:00
< span style = " '.(!empty( $movimenti ) ? '' : 'opacity: 0.5;').' " class = " clickable " id = " movimenti-'. $conto_terzo['id'] .' " > ' ;
2017-08-04 16:28:16 +02:00
2019-07-08 15:48:48 +02:00
echo '
2020-02-25 16:12:06 +01:00
& nbsp ; '.$conto_secondo[' numero '].' . '.$conto_terzo[' numero '].' '.$conto_terzo[' descrizione ' ];
2019-12-27 10:28:53 +01:00
2020-02-25 16:12:06 +01:00
echo ' </span>' ;
echo '
2019-07-08 15:48:48 +02:00
< div id = " conto_'. $conto_terzo['id'] .' " style = " display:none; " ></ div >
</ td >
2019-12-27 10:28:53 +01:00
2020-02-25 16:12:06 +01:00
< td width = " 100 " align = " right " valign = " top " style = " '.(!empty( $movimenti ) ? '' : 'opacity: 0.5;').' " >
2019-07-08 15:48:48 +02:00
'.moneyFormat(sum($totale_conto), 2).'
</ td >
</ tr > ' ;
}
2017-08-04 16:28:16 +02:00
2019-07-08 15:48:48 +02:00
echo '
</ table > ' ;
2017-08-04 16:28:16 +02:00
2017-09-14 18:48:45 +02:00
// Possibilità di inserire un nuovo conto
2019-07-08 15:48:48 +02:00
echo '
2019-07-08 17:11:48 +02:00
< button type = " button " class = " btn btn-xs btn-primary " data - toggle = " tooltip " title = " '.tr('Aggiungi un nuovo conto...').' " onclick = " add_conto('. $conto_secondo['id'] .') " >
2019-07-08 15:48:48 +02:00
< i class = " fa fa-plus-circle " ></ i >
</ button >
2019-12-27 10:28:53 +01:00
2019-07-08 15:48:48 +02:00
< br >< br >
</ div > ' ;
}
2017-08-04 16:28:16 +02:00
2019-07-08 15:48:48 +02:00
echo '
</ div >
2019-12-27 10:28:53 +01:00
2019-07-08 15:48:48 +02:00
< table class = " table table-condensed table-hover " > '
;
// Riepiloghi
if ( $conto_primo [ 'descrizione' ] == 'Patrimoniale' ) {
2018-02-10 19:42:52 +01:00
$attivita = abs ( sum ( $totale_attivita ));
$passivita = abs ( sum ( $totale_passivita ));
$utile_perdita = abs ( sum ( $totale_ricavi )) - abs ( sum ( $totale_costi ));
2017-08-04 16:28:16 +02:00
if ( $utile_perdita < 0 ) {
$pareggio1 = $attivita + abs ( $utile_perdita );
$pareggio2 = abs ( $passivita );
} else {
$pareggio1 = $attivita ;
$pareggio2 = abs ( $passivita ) + abs ( $utile_perdita );
}
2017-09-14 18:48:45 +02:00
// Attività
2019-07-08 15:48:48 +02:00
echo '
< tr >
< th class = " text-right " >
< big > '.tr(' Totale attività ').' :</ big >
</ th >
< td class = " text-right " width = " 150 " >
< big > '.moneyFormat($attivita, 2).' </ big >
</ td >
< td width = " 50 " ></ td > ' ;
2017-08-04 16:28:16 +02:00
2017-09-14 18:48:45 +02:00
// Passività
2019-07-08 15:48:48 +02:00
echo '
< th class = " text-right " >
< big > '.tr(' Passività ').' :</ big >
</ th >
< td class = " text-right " width = " 150 " >
< big > '.moneyFormat($passivita, 2).' </ big >
</ td >
</ tr > ' ;
2017-08-04 16:28:16 +02:00
2017-09-14 18:48:45 +02:00
// Perdita d'esercizio
2017-08-04 16:28:16 +02:00
if ( $utile_perdita < 0 ) {
2019-07-08 15:48:48 +02:00
echo '
< tr >
< th class = " text-right " >
< big > '.tr("Perdita d' esercizio " ).':</big>
</ th >
< td class = " text-right " >
< big > '.moneyFormat(sum($utile_perdita), 2).' </ big >
</ td >
< td ></ td >
< td ></ td >
< td ></ td >
</ tr > ' ;
2017-08-04 16:28:16 +02:00
} else {
2019-07-08 15:48:48 +02:00
echo '
< tr >
< td ></ td >
< td ></ td >
< td ></ td >
< th class = " text-right " >
< big > '.tr(' Utile ').' :</ big >
</ th >
< td class = " text-right " >
< big > '.moneyFormat(sum($utile_perdita), 2).' </ big >
</ td >
</ tr > ' ;
2017-08-04 16:28:16 +02:00
}
2017-09-14 18:48:45 +02:00
// Totale a pareggio
2019-07-08 15:48:48 +02:00
echo '
< tr >
< th class = " text-right " >
< big > '.tr(' Totale a pareggio ').' :</ big >
</ th >
< td class = " text-right " width = " 150 " >
< big > '.moneyFormat(sum($pareggio1), 2).' </ big >
</ td >
< td width = " 50 " ></ td >
2019-12-27 10:28:53 +01:00
2019-07-08 15:48:48 +02:00
< th class = " text-right " >
< big > '.tr(' Totale a pareggio ').' :</ big >
</ th >
< td class = " text-right " width = " 150 " >
< big > '.moneyFormat(sum($pareggio2), 2).' </ big >
</ td >
</ tr > ' ;
2017-08-04 16:28:16 +02:00
} else {
2019-07-08 15:48:48 +02:00
echo '
< tr >
< th class = " text-right " >
< big > '.tr(' Ricavi ').' :</ big >
</ th >
< td class = " text-right " width = " 150 " >
< big > '.moneyFormat(sum($totale_ricavi), 2).' </ big >
</ td >
</ tr >
2019-12-27 10:28:53 +01:00
2019-07-08 15:48:48 +02:00
< tr >
< th class = " text-right " >
< big > '.tr(' Costi ').' :</ big >
</ th >
< td class = " text-right " width = " 150 " >
< big > '.moneyFormat(sum($totale_costi), 2).' </ big >
</ td >
</ tr >
2019-12-27 10:28:53 +01:00
2019-07-08 15:48:48 +02:00
< tr >
< th class = " text-right " >
< big > '.tr(' Utile / perdita ').' :</ big >
</ th >
< td class = " text-right " width = " 150 " >
< big > '.moneyFormat(sum($totale_ricavi) - abs(sum($totale_costi)), 2).' </ big >
</ td >
</ tr > ' ;
2017-08-04 16:28:16 +02:00
}
2019-07-08 15:48:48 +02:00
2019-07-08 17:11:48 +02:00
echo '
2019-07-08 15:48:48 +02:00
</ table > ' ;
}
2019-12-21 19:07:02 +01:00
// Verifico se è già stata eseguita l'apertura bilancio
$bilancio_gia_chiuso = $dbo -> fetchNum ( 'SELECT id FROM co_movimenti WHERE is_chiusura=1 AND data BETWEEN ' . prepare ( $_SESSION [ 'period_start' ]) . ' AND ' . prepare ( $_SESSION [ 'period_end' ]));
$msg = tr ( 'Sei sicuro di voler aprire il bilancio?' );
$btn_class = 'btn-info' ;
2019-12-24 12:36:35 +01:00
if ( $bilancio_gia_chiuso ) {
2019-12-21 19:07:02 +01:00
$msg .= ' ' . tr ( 'I movimenti di apertura già esistenti verranno annullati e ricreati' ) . '.' ;
$btn_class = 'btn-default' ;
}
?>
< div class = " text-right " >
< button type = " button " class = " btn btn-lg <?php echo $btn_class ; ?> " data - op = " chiudi-bilancio " data - title = " <?php echo tr('Chiusura bilancio'); ?> " data - backto = " record-list " data - msg = " <?php echo $msg ; ?> " data - button = " <?php echo tr('Chiudi bilancio'); ?> " data - class = " btn btn-lg btn-primary " onclick = " message( this ); " >< i class = " fa fa-folder " ></ i > < ? php echo tr ( 'Chiusura bilancio' ); ?> </button>
</ div >
2019-07-08 15:48:48 +02:00
< script >
2020-01-02 16:05:00 +01:00
var tr = '' ;
2019-07-08 15:48:48 +02:00
$ ( document ) . ready ( function (){
2019-12-27 10:28:53 +01:00
$ ( " tr " ) . each ( function () {
2019-07-08 15:48:48 +02:00
$ ( this ) . on ( " mouseover " , function () {
$ ( this ) . find ( " .tools " ) . removeClass ( " hide " );
});
2019-12-27 10:28:53 +01:00
2019-07-08 15:48:48 +02:00
$ ( this ) . on ( " mouseleave " , function () {
$ ( this ) . find ( " .tools " ) . addClass ( " hide " );
});
2020-01-02 16:05:00 +01:00
});
2019-12-27 10:28:53 +01:00
2020-01-02 16:05:00 +01:00
$ ( " span[id^=movimenti-] " ) . each ( function () {
2019-07-08 15:48:48 +02:00
$ ( this ) . on ( " click " , function () {
2019-12-27 10:28:53 +01:00
var movimenti = $ ( this ) . parent () . find ( " div[id^=conto_] " );
2019-07-08 15:48:48 +02:00
if ( ! movimenti . html ()) {
2020-01-02 16:05:00 +01:00
var id_conto = $ ( this ) . attr ( " id " ) . split ( " - " ) . pop ();
2019-07-08 15:48:48 +02:00
load_movimenti ( movimenti . attr ( " id " ), id_conto );
} else {
movimenti . slideToggle ();
}
2019-12-27 10:28:53 +01:00
2019-07-08 15:48:48 +02:00
$ ( this ) . find ( " .plus-btn i " ) . toggleClass ( " fa-plus " ) . toggleClass ( " fa-minus " );
});
})
});
function add_conto ( id ) {
2019-12-21 19:07:02 +01:00
launch_modal ( " <?php echo tr('Nuovo conto'); ?> " , " <?php echo $structure->fileurl ('add_conto.php'); ?>?id= " + id );
2019-07-08 15:48:48 +02:00
}
function load_movimenti ( selector , id_conto ) {
2019-07-15 09:34:14 +02:00
$ ( " #main_loading " ) . show ();
2019-12-27 10:28:53 +01:00
2019-07-08 15:48:48 +02:00
$ . ajax ({
2019-12-21 19:07:02 +01:00
url : " <?php echo $structure->fileurl ('dettagli_conto.php'); ?> " ,
2019-07-08 15:48:48 +02:00
type : " get " ,
data : {
id_module : globals . id_module ,
id_conto : id_conto ,
},
success : function ( data ){
$ ( " # " + selector ) . html ( data );
$ ( " # " + selector ) . slideToggle ();
2019-12-27 10:28:53 +01:00
2019-07-15 09:34:14 +02:00
$ ( " #main_loading " ) . fadeOut ();
2019-07-08 15:48:48 +02:00
}
});
2017-08-04 16:28:16 +02:00
}
2019-12-21 19:07:02 +01:00
</ script >