2017-08-04 16:28:16 +02:00
< ? php
include_once __DIR__ . '/../../core.php' ;
switch ( post ( 'op' )) {
2019-04-19 17:29:54 +02:00
case 'add' :
2019-05-04 00:32:28 +02:00
$data = post ( 'data' );
$tipo = post ( 'tipo' );
$da_pagare = post ( 'da_pagare' );
$descrizione = post ( 'descrizione' );
2019-04-19 17:29:54 +02:00
2019-05-04 00:32:28 +02:00
$dbo -> query ( 'INSERT INTO co_scadenziario(descrizione, tipo, data_emissione, scadenza, da_pagare, pagato, data_pagamento) VALUES(' . prepare ( $descrizione ) . ', ' . prepare ( $tipo ) . ', CURDATE(), ' . prepare ( $data ) . ', ' . prepare ( $da_pagare ) . " , '0', '0000-00-00') " );
2019-04-19 17:29:54 +02:00
$id_record = $dbo -> lastInsertedID ();
2019-05-04 00:32:28 +02:00
2019-04-19 17:29:54 +02:00
flash () -> info ( tr ( 'Scadenza inserita!' ));
break ;
2017-08-04 16:28:16 +02:00
case 'update' :
// Calcolo il totale da pagare
2019-04-19 17:42:16 +02:00
$rs = $dbo -> fetchArray ( 'SELECT SUM(da_pagare) AS totale_da_pagare, iddocumento FROM co_scadenziario GROUP BY iddocumento HAVING iddocumento=(SELECT iddocumento FROM co_scadenziario s WHERE id=' . prepare ( $id_record ) . ')' );
2018-11-30 16:10:15 +01:00
$totale_da_pagare = sum ( $rs [ 0 ][ 'totale_da_pagare' ], null , Settings :: get ( 'Cifre decimali per importi' ));
2017-08-04 16:28:16 +02:00
$totale_utente = 0 ;
2019-04-19 17:42:16 +02:00
// Verifico se il totale sommato è uguale al totale da pagare (solo per le scadenze delle fatture)
2018-07-19 15:33:32 +02:00
foreach ( post ( 'scadenza' ) as $idscadenza => $da_pagare ) {
2018-11-30 16:10:15 +01:00
$totale_utente = sum ( $totale_utente , $da_pagare );
2017-08-04 16:28:16 +02:00
}
2019-04-19 17:42:16 +02:00
if ( $totale_utente == $totale_da_pagare || empty ( $rs [ 0 ][ 'iddocumento' ])) {
2018-07-19 15:33:32 +02:00
foreach ( post ( 'scadenza' ) as $idscadenza => $da_pagare ) {
2017-08-04 16:28:16 +02:00
$dbo -> query ( 'UPDATE co_scadenziario SET da_pagare=' . prepare ( $da_pagare ) . ', pagato=' . prepare ( post ( 'pagato' )[ $idscadenza ]) . ', scadenza=' . prepare ( post ( 'data' )[ $idscadenza ]) . ' WHERE id=' . prepare ( $idscadenza ));
}
2018-07-19 17:29:21 +02:00
flash () -> info ( tr ( 'Scadenze aggiornate!' ));
2017-08-04 16:28:16 +02:00
} else {
2018-07-19 17:29:21 +02:00
flash () -> error ( tr ( 'Il totale degli importi inseriti non corrisponde al totale da pagare!' ));
2017-08-04 16:28:16 +02:00
}
break ;
2019-04-19 17:29:54 +02:00
2019-05-04 00:32:28 +02:00
case 'delete' :
2019-04-19 17:29:54 +02:00
$dbo -> query ( " DELETE FROM co_scadenziario WHERE id=' " . $id_record . " ' " );
flash () -> info ( tr ( 'Scadenza eliminata!' ));
break ;
2017-08-04 16:28:16 +02:00
}