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' ;
}
2020-08-06 10:41:21 +02:00
echo '
2019-12-21 19:07:02 +01:00
< div class = " text-right " >
2020-08-06 10:41:21 +02:00
< button type = " button " class = " btn btn-lg '. $btn_class .' " data - op = " apri-bilancio " data - title = " '.tr('Apertura bilancio').' " data - backto = " record-list " data - msg = " '. $msg .' " data - button = " '.tr('Riprendi saldi').' " data - class = " btn btn-lg btn-warning " onclick = " message( this ); " >
< i class = " fa fa-folder-open " ></ i > '.tr(' Apertura bilancio ').'
</ button >
</ div > ' ;
2019-12-21 19:07:02 +01:00
2019-07-08 15:48:48 +02:00
// Livello 1
2020-08-11 17:09:00 +02:00
$query1 = 'SELECT * FROM `co_pianodeiconti1` ORDER BY id DESC' ;
2019-07-08 15:48:48 +02:00
$primo_livello = $dbo -> fetchArray ( $query1 );
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 >
2020-08-06 14:42:59 +02:00
< div class = " box " >
< div class = " box-header " >
< h3 class = " box-title " > '.$titolo.' </ h3 >
< div class = " pull-right " >
'.Prints::getLink(' Mastrino ', $conto_primo[' id '], null, tr(' Stampa '), null, ' lev = 1 ').'
</ div >
2019-07-08 15:48:48 +02:00
</ div >
2019-12-27 10:28:53 +01:00
2020-08-06 14:42:59 +02:00
< div class = " box-body " > ' ;
2017-08-04 16:28:16 +02:00
2017-09-14 18:48:45 +02:00
// Livello 2
2020-08-06 14:42:59 +02:00
$query2 = 'SELECT * FROM `co_pianodeiconti2` WHERE idpianodeiconti1 = ' . prepare ( $conto_primo [ 'id' ]) . ' ORDER BY numero ASC' ;
2019-07-08 15:48:48 +02:00
$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
2020-08-06 14:42:59 +02:00
< button type = " button " class = " btn btn-warning btn-xs " onclick = " modificaConto('. $conto_secondo['id'] .', 2) " >
2019-12-27 10:28:53 +01:00
< 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 >
2020-08-06 14:42:59 +02:00
< div class = " table-responsive " >
< table class = " table table-striped table-hover table-condensed " >
< thead >
< tr >
< th > '.tr(' Descrizione ').' </ th >
< th style = " width: 10% " class = " text-center " > '.tr(' Importo ').' </ th >
< th style = " width: 10% " class = " text-center " > '.tr(' Importo reddito ').' </ th >
</ tr >
</ thead >
< tbody > ' ;
2019-07-08 17:11:48 +02:00
2017-09-14 18:48:45 +02:00
// Livello 3
2020-08-06 14:42:59 +02:00
$query3 = ' SELECT `co_pianodeiconti3` .* , movimenti . numero_movimenti , movimenti . totale , movimenti . totale_reddito , 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 , anagrafica . idconto_fornitore )
LEFT OUTER JOIN (
SELECT COUNT ( idconto ) AS numero_movimenti ,
idconto ,
SUM ( ROUND ( totale , 2 )) AS totale ,
SUM ( ROUND ( totale_reddito , 2 )) AS totale_reddito
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 );
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-08-06 10:41:21 +02:00
$numero_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' ];
2020-08-06 14:42:59 +02:00
$totale_reddito = $conto_terzo [ 'totale_reddito' ];
2020-08-11 16:52:07 +02:00
if ( $conto_primo [ 'descrizione' ] != 'Patrimoniale' ) {
2020-08-06 14:42:59 +02:00
$totale_conto = - $totale_conto ;
$totale_reddito = - $totale_reddito ;
}
2019-07-08 15:48:48 +02:00
// 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-08-06 14:42:59 +02:00
< tr style = " '.(!empty( $numero_movimenti ) ? '' : 'opacity: 0.5;').' " >
2020-02-25 16:12:06 +01:00
< td > ' ;
2020-02-27 16:19:07 +01:00
2020-02-25 16:12:06 +01:00
// Possibilità di esplodere i movimenti del conto
2020-08-06 10:41:21 +02:00
if ( ! empty ( $numero_movimenti )) {
2019-07-08 15:48:48 +02:00
echo '
2020-08-06 10:41:21 +02:00
< 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 '
2020-08-06 10:41:21 +02: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-08-06 10:41:21 +02:00
if ( isset ( $id_anagrafica )) {
echo 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>' );
}
2020-02-25 16:12:06 +01:00
2019-07-08 15:48:48 +02:00
// Stampa mastrino
2020-08-06 10:41:21 +02:00
if ( ! empty ( $numero_movimenti )) {
2017-09-14 18:48:45 +02:00
echo '
2020-08-06 10:41:21 +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-08-06 10:41:21 +02:00
// Pulsante per aggiornare il totale reddito del conto di livello 3
2020-08-12 13:21:03 +02:00
if ( $conto_secondo [ 'dir' ] == 'uscita' ) {
2020-08-11 17:23:25 +02:00
echo '
< button type = " button " class = " btn btn-info btn-xs " onclick = " aggiornaReddito('. $conto_terzo['id'] .') " >
< i class = " fa fa-refresh " ></ i >
</ button > ' ;
}
2020-08-06 10:41:21 +02:00
// Pulsante per modificare il nome del conto di livello 3
echo '
2020-08-06 14:42:59 +02:00
< button type = " button " class = " btn btn-warning btn-xs " onclick = " modificaConto('. $conto_terzo['id'] .') " >
2020-08-06 10:41:21 +02:00
< i class = " fa fa-edit " ></ i >
</ button > ' ;
2020-02-25 16:12:06 +01:00
// 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 '
2020-08-06 10:41:21 +02:00
< 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 > ' ;
2019-07-08 15:48:48 +02:00
}
2017-08-04 16:28:16 +02:00
2019-07-08 15:48:48 +02:00
echo '
2020-08-06 10:41:21 +02:00
</ span > ' ;
2020-02-25 16:12:06 +01:00
2020-08-06 10:41:21 +02:00
// Span con info del conto
2020-02-25 16:12:06 +01:00
echo '
2020-08-06 14:42:59 +02:00
< span class = " clickable " id = " movimenti-'. $conto_terzo['id'] .' " >
2020-08-12 13:30:34 +02:00
& nbsp ; '.$conto_secondo[' numero '].' . '.$conto_terzo[' numero '].' '.$conto_terzo[' descrizione '].($conto_terzo[' percentuale_deducibile '] < 100 ? ' < span class = " text-muted " > ( '.tr(' deducibile al _PERC_ % ', [' _PERC_ ' => Translator::numberToLocale($conto_terzo[' percentuale_deducibile '], 0)]).' ) </ span > ' : ' ').'
2020-08-06 10:41:21 +02:00
</ span >
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-08-06 14:42:59 +02:00
< td class = " text-right " >
'.moneyFormat($totale_conto, 2).'
</ td >
< td class = " text-right " >
'.moneyFormat($totale_reddito, 2).'
2019-07-08 15:48:48 +02:00
</ td >
</ tr > ' ;
}
2017-08-04 16:28:16 +02:00
2019-07-08 15:48:48 +02:00
echo '
2020-08-06 14:42:59 +02:00
</ tbody >
< tfoot >
< tr >
< th > '.tr(' Descrizione ').' </ th >
< th class = " text-center " > '.tr(' Importo ').' </ th >
< th class = " text-center " > '.tr(' Importo reddito ').' </ th >
</ tr >
</ tfoot >
2019-07-08 15:48:48 +02:00
</ 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 '
2020-08-06 10:41:21 +02:00
< button type = " button " class = " btn btn-xs btn-primary " data - toggle = " tooltip " title = " '.tr('Aggiungi un nuovo conto...').' " onclick = " aggiungiConto('. $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
2020-08-06 14:42:59 +02:00
< table class = " table table-condensed table-hover " > ' ;
2019-07-08 15:48:48 +02:00
// 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 '
2020-08-06 14:42:59 +02:00
< 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 '
2020-08-06 14:42:59 +02:00
< 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 '
2020-08-06 14:42:59 +02:00
< 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 '
2020-08-06 14:42:59 +02:00
< 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 '
2020-08-06 14:42:59 +02:00
< 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 >
< 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 '
2020-08-06 14:42:59 +02:00
< 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 >
< 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 >
< 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 '
2020-08-06 14:42:59 +02:00
</ table >
</ div >
</ div > ' ;
2019-07-08 15:48:48 +02:00
}
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' ;
}
2020-08-06 10:41:21 +02:00
echo '
2019-12-21 19:07:02 +01:00
< div class = " text-right " >
2020-08-06 10:41:21 +02:00
< button type = " button " class = " btn btn-lg '. $btn_class .' " data - op = " chiudi-bilancio " data - title = " '.tr('Chiusura bilancio').' " data - backto = " record-list " data - msg = " '. $msg .' " data - button = " '.tr('Chiudi bilancio').' " data - class = " btn btn-lg btn-primary " onclick = " message( this ); " >
< i class = " fa fa-folder " ></ i > '.tr(' Chiusura bilancio ').'
</ button >
2019-12-21 19:07:02 +01:00
</ div >
2020-08-06 14:42:59 +02:00
< div class = " clearfix " ></ div >
< hr >
2019-07-08 15:48:48 +02:00
< script >
2020-08-06 10:41:21 +02:00
$ ( document ) . ready ( function () {
$ ( " tr " ) . each ( function () {
$ ( this ) . on ( " mouseover " , function () {
$ ( this ) . find ( " .tools " ) . removeClass ( " hide " );
});
$ ( this ) . on ( " mouseleave " , function () {
$ ( this ) . find ( " .tools " ) . addClass ( " hide " );
});
2019-07-08 15:48:48 +02:00
});
2019-12-27 10:28:53 +01:00
2020-08-06 10:41:21 +02:00
$ ( " span[id^=movimenti-] " ) . each ( function () {
$ ( this ) . on ( " click " , function () {
let movimenti = $ ( this ) . parent () . find ( " div[id^=conto_] " );
2019-12-27 10:28:53 +01:00
2020-08-06 10:41:21 +02:00
if ( ! movimenti . html ()) {
let id_conto = $ ( this ) . attr ( " id " ) . split ( " - " ) . pop ();
2019-07-08 15:48:48 +02:00
2020-08-06 10:41:21 +02:00
caricaMovimenti ( movimenti . attr ( " id " ), id_conto );
} else {
movimenti . slideToggle ();
}
2019-07-08 15:48:48 +02:00
2020-08-06 10:41:21 +02:00
$ ( this ) . find ( " .plus-btn i " ) . toggleClass ( " fa-plus " ) . toggleClass ( " fa-minus " );
});
})
});
2019-12-27 10:28:53 +01:00
2020-08-06 10:41:21 +02:00
function aggiungiConto ( id_conto_lvl2 ) {
openModal ( " '.tr('Nuovo conto').' " , " '. $structure->fileurl ('add_conto.php').'?id= " + id_conto_lvl2 );
}
2019-07-08 15:48:48 +02:00
2020-08-06 14:42:59 +02:00
function modificaConto ( id_conto , level = 3 ) {
launch_modal ( " '.tr('Modifica conto').' " , " '. $structure->fileurl ('edit_conto.php').'?id= " + id_conto + " &lvl= " + level );
}
2020-08-06 10:41:21 +02:00
function caricaMovimenti ( selector , id_conto ) {
$ ( " #main_loading " ) . show ();
$ . ajax ({
url : " '. $structure->fileurl ('dettagli_conto.php').' " ,
type : " get " ,
data : {
id_module : globals . id_module ,
id_conto : id_conto ,
},
success : function ( data ){
$ ( " # " + selector ) . html ( data )
. slideToggle ();
$ ( " #main_loading " ) . fadeOut ();
}
});
}
2019-07-08 15:48:48 +02:00
2020-08-06 10:41:21 +02:00
function aggiornaReddito ( id_conto ){
2020-08-11 17:23:25 +02:00
openModal ( " '.tr('Ricalcola importo deducibile').' " , " '. $structure->fileurl ('aggiorna_reddito.php').'?id= " + id_conto )
2020-08-06 10:41:21 +02:00
}
</ script > ' ;