2017-08-04 16:28:16 +02:00
< ? php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager : il software gestionale open source per l ' assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright ( C ) DevCode s . r . l .
2020-09-07 15:04:06 +02:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*/
2017-08-04 16:28:16 +02:00
include_once __DIR__ . '/../../core.php' ;
2024-03-05 16:01:45 +01:00
use Models\Module ;
2024-03-12 12:31:49 +01:00
use Models\PrintTemplate ;
2024-03-22 15:52:24 +01:00
use Modules\Fatture\Tipo ;
use Modules\Scadenzario\Scadenza ;
2024-04-24 12:18:53 +02:00
use Plugins\AssicurazioneCrediti\AssicurazioneCrediti ;
2023-07-07 15:17:40 +02:00
2017-08-04 16:28:16 +02:00
switch ( post ( 'op' )) {
2019-04-19 17:29:54 +02:00
case 'add' :
2022-09-01 12:52:33 +02:00
$idanagrafica = post ( 'idanagrafica' );
2019-05-04 00:32:28 +02:00
$data = post ( 'data' );
$tipo = post ( 'tipo' );
$da_pagare = post ( 'da_pagare' );
$descrizione = post ( 'descrizione' );
2024-01-12 11:05:16 +01:00
$iddocumento = post ( 'iddocumento' ) ? : '' ;
$data_emissione = post ( 'data_emissione' ) ? : date ( 'Y-m-d' );
2019-04-19 17:29:54 +02:00
2024-01-12 11:05:16 +01:00
$dbo -> query ( 'INSERT INTO co_scadenziario(idanagrafica, iddocumento, descrizione, tipo, data_emissione, scadenza, da_pagare, pagato) VALUES(' . prepare ( $idanagrafica ) . ', ' . prepare ( $iddocumento ) . ', ' . prepare ( $descrizione ) . ', ' . prepare ( $tipo ) . ', ' . prepare ( $data_emissione ) . ', ' . prepare ( $data ) . ', ' . prepare ( $da_pagare ) . " , '0') " );
2019-04-19 17:29:54 +02:00
$id_record = $dbo -> lastInsertedID ();
2019-05-04 00:32:28 +02:00
2024-04-24 12:18:53 +02:00
$assicurazione_crediti = AssicurazioneCrediti :: where ( 'id_anagrafica' , $idanagrafica ) -> where ( 'data_inizio' , '<=' , $data ) -> where ( 'data_fine' , '>=' , $data ) -> first ();
if ( ! empty ( $assicurazione_crediti )) {
$assicurazione_crediti -> fixTotale ();
$assicurazione_crediti -> save ();
}
2019-04-19 17:29:54 +02:00
flash () -> info ( tr ( 'Scadenza inserita!' ));
break ;
2017-08-04 16:28:16 +02:00
case 'update' :
2022-10-10 11:23:17 +02:00
$idanagrafica = post ( 'idanagrafica' );
2019-07-25 18:05:47 +02:00
$tipo = post ( 'tipo' );
$descrizione = post ( 'descrizione' );
$iddocumento = post ( 'iddocumento' ) ? : 0 ;
2024-02-05 17:07:33 +01:00
if ( ! empty ( $iddocumento )) {
2024-01-31 16:23:19 +01:00
$scadenze = database () -> table ( 'co_scadenziario' ) -> where ( 'iddocumento' , '=' , $iddocumento ) -> orderBy ( 'scadenza' ) -> get ();
}
2019-09-28 11:25:20 +02:00
$totale_pagato = 0 ;
$id_scadenza_non_completa = null ;
2024-04-02 14:38:02 +02:00
$da_pagare = 0 ;
2024-01-12 11:05:16 +01:00
2023-11-24 13:20:00 +01:00
foreach ( $scadenze as $id => $scadenza ) {
2024-01-12 11:05:16 +01:00
$pagato = floatval ( post ( 'pagato' )[ $id ]);
$data_scadenza = post ( 'scadenza' )[ $id ];
2020-01-14 22:18:52 +01:00
$data_concordata = post ( 'data_concordata' )[ $id ];
2023-11-24 13:20:00 +01:00
$da_pagare = post ( 'da_pagare' )[ $id ];
2021-02-18 18:48:44 +01:00
if ( ! empty ( $iddocumento )) {
2024-04-02 14:38:02 +02:00
$tipo_documento = Tipo :: find ( $documento -> idtipodocumento );
2020-01-14 22:18:52 +01:00
2021-02-18 18:48:44 +01:00
if ( $tipo_documento [ 'dir' ] == 'uscita' ) {
if ( $pagato > 0 ) {
2021-02-04 17:25:46 +01:00
$pagato = - $pagato ;
}
2021-02-18 18:48:44 +01:00
if ( $da_pagare > 0 ) {
2021-02-04 17:25:46 +01:00
$da_pagare = - $da_pagare ;
}
2021-02-18 18:48:44 +01:00
} else {
if ( $pagato < 0 ) {
2021-02-04 17:25:46 +01:00
$pagato = - $pagato ;
}
2021-02-18 18:48:44 +01:00
if ( $da_pagare < 0 ) {
2021-02-04 17:25:46 +01:00
$da_pagare = - $da_pagare ;
}
}
2021-02-18 18:48:44 +01:00
if ( ! empty ( $tipo_documento [ 'reversed' ])) {
2021-02-04 17:25:46 +01:00
$pagato = - $pagato ;
$da_pagare = - $da_pagare ;
}
}
2021-02-18 18:48:44 +01:00
2020-01-14 22:18:52 +01:00
$totale_pagato = sum ( $totale_pagato , $pagato );
2024-01-12 11:05:16 +01:00
$id_pagamento = post ( 'id_pagamento' )[ $id ] ? : $documento -> idpagamento ;
$id_banca_azienda = post ( 'id_banca_azienda' )[ $id ] ? : $documento -> id_banca_azienda ;
$id_banca_controparte = post ( 'id_banca_controparte' )[ $id ] ? : $documento -> id_banca_controparte ;
2020-01-14 22:18:52 +01:00
2024-01-31 16:23:19 +01:00
$id_scadenza = $scadenza -> id ? ? $scadenza [ 'id' ];
2020-01-14 22:18:52 +01:00
if ( ! empty ( $id_scadenza )) {
$database -> update ( 'co_scadenziario' , [
2022-10-10 11:23:17 +02:00
'idanagrafica' => $idanagrafica ,
2020-01-14 22:18:52 +01:00
'descrizione' => $descrizione ,
'da_pagare' => $da_pagare ,
'pagato' => $pagato ,
2024-01-12 11:05:16 +01:00
'scadenza' => $data_scadenza ,
2020-01-14 22:18:52 +01:00
'data_concordata' => $data_concordata ,
2023-11-27 11:18:54 +01:00
'id_pagamento' => $id_pagamento ,
2023-11-24 13:20:00 +01:00
'id_banca_azienda' => $id_banca_azienda ,
'id_banca_controparte' => $id_banca_controparte ,
2020-01-28 14:20:14 +01:00
'note' => post ( 'note' ),
2021-12-07 13:05:41 +01:00
'distinta' => post ( 'distinta' ) ? : null ,
2020-01-14 22:18:52 +01:00
], [ 'id' => $id_scadenza ]);
if ( $da_pagare == 0 ) {
2024-01-12 11:05:16 +01:00
$database -> delete ( 'co_scadenziario' , [ 'id' => $id_scadenza ]);
2019-09-28 11:25:20 +02:00
}
2020-01-14 22:18:52 +01:00
} else {
$database -> insert ( 'co_scadenziario' , [
2023-03-27 16:07:49 +02:00
'idanagrafica' => $idanagrafica ,
2020-01-14 22:18:52 +01:00
'descrizione' => $descrizione ,
'tipo' => $tipo ,
'iddocumento' => $iddocumento ,
'da_pagare' => $da_pagare ,
'pagato' => $pagato ,
2024-01-12 11:05:16 +01:00
'scadenza' => $data_scadenza ,
2020-01-14 22:18:52 +01:00
'data_concordata' => $data_concordata ,
'data_emissione' => date ( 'Y-m-d' ),
2020-01-28 14:20:14 +01:00
'note' => post ( 'note' ),
2020-01-14 22:18:52 +01:00
]);
$id_scadenza = $database -> lastInsertedID ();
}
2019-09-28 11:25:20 +02:00
2020-01-14 22:18:52 +01:00
if ( $pagato != $da_pagare ) {
$id_scadenza_non_completa = $id_scadenza ;
2017-08-04 16:28:16 +02:00
}
2024-04-24 12:18:53 +02:00
$assicurazione_crediti = AssicurazioneCrediti :: where ( 'id_anagrafica' , $idanagrafica ) -> where ( 'data_inizio' , '<=' , $scadenza ) -> where ( 'data_fine' , '>=' , $scadenza ) -> first ();
if ( ! empty ( $assicurazione_crediti )) {
$assicurazione_crediti -> fixTotale ();
$assicurazione_crediti -> save ();
}
2020-01-14 22:18:52 +01:00
}
2017-08-04 16:28:16 +02:00
2020-01-14 22:18:52 +01:00
flash () -> info ( tr ( 'Scadenze aggiornate!' ));
2019-09-28 11:25:20 +02:00
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' :
2024-04-24 12:18:53 +02:00
$scadenza = Scadenza :: find ( $id_record );
$assicurazione_crediti = AssicurazioneCrediti :: where ( 'id_anagrafica' , $scadenza -> idanagrafica ) -> where ( 'data_inizio' , '<=' , $scadenza -> scadenza ) -> where ( 'data_fine' , '>=' , $scadenza -> scadenza ) -> first ();
2019-04-19 17:29:54 +02:00
$dbo -> query ( " DELETE FROM co_scadenziario WHERE id=' " . $id_record . " ' " );
2024-04-24 12:18:53 +02:00
if ( ! empty ( $assicurazione_crediti )) {
$assicurazione_crediti -> fixTotale ();
$assicurazione_crediti -> save ();
}
2019-04-19 17:29:54 +02:00
flash () -> info ( tr ( 'Scadenza eliminata!' ));
2024-04-24 12:18:53 +02:00
2019-04-19 17:29:54 +02:00
break ;
2022-06-10 15:54:50 +02:00
case 'allega_fattura' :
2023-07-07 15:17:40 +02:00
$scadenza = Scadenza :: find ( $id_record );
2022-06-10 15:54:50 +02:00
$id_documento = post ( 'iddocumento' );
2024-04-18 17:44:05 +02:00
$print_predefined = PrintTemplate :: where ( 'predefined' , 1 ) -> where ( 'id_module' , ( new Module ()) -> getByField ( 'title' , 'Fatture di vendita' , Models\Locale :: getPredefined () -> id )) -> first ();
2022-06-10 15:54:50 +02:00
2024-03-12 12:31:49 +01:00
$print = Prints :: render ( $print_predefined -> id , $id_documento , null , true );
2022-06-10 15:54:50 +02:00
$upload = Uploads :: upload ( $print [ 'pdf' ], [
2023-07-07 15:17:40 +02:00
'name' => $scadenza -> descrizione ,
2023-07-07 15:23:52 +02:00
'original_name' => $scadenza -> descrizione . '.pdf' ,
2022-06-10 15:54:50 +02:00
'category' => 'Generale' ,
'id_module' => $id_module ,
'id_record' => $id_record ,
]);
flash () -> info ( tr ( 'Stampa allegata correttamente!' ));
break ;
2017-08-04 16:28:16 +02:00
}