2018-03-09 11:56:58 +01: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 />.
*/
2018-03-09 11:56:58 +01:00
include_once __DIR__ . '/../../core.php' ;
2018-06-26 09:41:43 +02:00
2021-07-07 07:57:10 +02:00
use Modules\Articoli\Articolo as ArticoloOriginale ;
2020-02-14 12:37:55 +01:00
use Modules\DDT\DDT ;
2018-09-27 17:27:39 +02:00
use Modules\Fatture\Fattura ;
2020-02-14 13:12:02 +01:00
use Modules\Fatture\Stato ;
2018-09-27 17:27:39 +02:00
use Modules\Fatture\Tipo ;
if ( $module [ 'name' ] == 'Ddt di vendita' ) {
$dir = 'entrata' ;
2020-02-14 13:12:02 +01:00
$module_fatture = 'Fatture di vendita' ;
2018-09-27 17:27:39 +02:00
} else {
$dir = 'uscita' ;
2020-02-14 13:12:02 +01:00
$module_fatture = 'Fatture di acquisto' ;
2018-09-27 17:27:39 +02:00
}
// Segmenti
2020-02-14 13:12:02 +01:00
$id_fatture = Modules :: get ( $module_fatture )[ 'id' ];
2018-09-27 17:27:39 +02:00
if ( ! isset ( $_SESSION [ 'module_' . $id_fatture ][ 'id_segment' ])) {
$segments = Modules :: getSegments ( $id_fatture );
$_SESSION [ 'module_' . $id_fatture ][ 'id_segment' ] = isset ( $segments [ 0 ][ 'id' ]) ? $segments [ 0 ][ 'id' ] : null ;
}
$id_segment = $_SESSION [ 'module_' . $id_fatture ][ 'id_segment' ];
2021-04-28 15:29:47 +02:00
$idconto = setting ( 'Conto predefinito fatture di vendita' );
2018-09-27 17:27:39 +02:00
2018-03-15 17:41:09 +01:00
switch ( post ( 'op' )) {
2018-07-16 13:27:44 +02:00
case 'crea_fattura' :
2020-02-14 12:37:55 +01:00
$documenti = collect ();
$numero_totale = 0 ;
2018-07-16 13:27:44 +02:00
// Informazioni della fattura
2018-09-03 16:01:05 +02:00
if ( $dir == 'entrata' ) {
2020-02-14 13:12:02 +01:00
$descrizione_tipo = 'Fattura immediata di vendita' ;
2018-08-08 09:32:20 +02:00
} else {
2020-02-14 13:12:02 +01:00
$descrizione_tipo = 'Fattura immediata di acquisto' ;
2018-08-08 09:32:20 +02:00
}
2018-08-29 18:15:12 +02:00
2020-02-14 13:12:02 +01:00
$tipo_documento = Tipo :: where ( 'descrizione' , $descrizione_tipo ) -> first ();
$stato_documenti_accodabili = Stato :: where ( 'descrizione' , 'Bozza' ) -> first ();
$accodare = post ( 'accodare' );
2018-09-27 17:27:39 +02:00
2018-07-16 13:27:44 +02:00
$data = date ( 'Y-m-d' );
2018-09-27 17:27:39 +02:00
$id_segment = post ( 'id_segment' );
2018-09-24 10:51:48 +02:00
2018-03-15 17:41:09 +01:00
// Lettura righe selezionate
2018-07-16 13:27:44 +02:00
foreach ( $id_records as $id ) {
2020-02-14 12:37:55 +01:00
$documento_import = DDT :: find ( $id );
$anagrafica = $documento_import -> anagrafica ;
$id_anagrafica = $anagrafica -> id ;
2018-03-15 17:41:09 +01:00
2020-02-14 12:37:55 +01:00
// Proseguo solo se i documenti scelti sono fatturabili
2020-07-16 16:49:36 +02:00
if ( $documento_import -> isImportabile ()) {
2020-07-16 10:37:56 +02:00
$righe = $documento_import -> getRighe ();
if ( ! empty ( $righe )) {
++ $numero_totale ;
// Ricerca fattura per anagrafica tra le registrate
$fattura = $documenti -> first ( function ( $item , $key ) use ( $id_anagrafica ) {
return $item -> anagrafica -> id == $id_anagrafica ;
});
// Ricerca fattura per anagrafica se l'impostazione di accodamento è selezionata
if ( ! empty ( $accodare ) && empty ( $fattura )) {
$fattura = Fattura :: where ( 'idanagrafica' , $id_anagrafica )
-> where ( 'idstatodocumento' , $stato_documenti_accodabili -> id )
-> where ( 'idtipodocumento' , $tipo_documento -> id )
-> first ();
if ( ! empty ( $fattura )) {
$documenti -> push ( $fattura );
}
2020-02-14 13:12:02 +01:00
}
2020-07-16 10:37:56 +02:00
// Creazione fattura per anagrafica
if ( empty ( $fattura )) {
$fattura = Fattura :: build ( $anagrafica , $tipo_documento , $data , $id_segment );
$documenti -> push ( $fattura );
}
2018-03-15 17:41:09 +01:00
2020-07-16 10:37:56 +02:00
// Inserimento righe
foreach ( $righe as $riga ) {
$qta = $riga -> qta_rimanente ;
2018-03-15 17:41:09 +01:00
2020-07-16 10:37:56 +02:00
if ( $qta > 0 ) {
$copia = $riga -> copiaIn ( $fattura , $qta );
2018-03-09 11:56:58 +01:00
2021-04-28 15:14:52 +02:00
//Fix per idconto righe fattura
$articolo = ArticoloOriginale :: find ( $copia -> idarticolo );
$copia -> id_conto = ( $articolo -> idconto_vendita ? $articolo -> idconto_vendita : $idconto );
2020-07-16 10:37:56 +02:00
// Aggiornamento seriali dalla riga dell'ordine
if ( $copia -> isArticolo ()) {
$copia -> serials = $riga -> serials ;
}
2021-06-11 10:24:13 +02:00
$copia -> save ();
2020-02-14 12:37:55 +01:00
}
2018-03-15 17:41:09 +01:00
}
}
}
2018-03-09 11:56:58 +01:00
}
2018-03-15 17:41:09 +01:00
2020-02-14 12:37:55 +01:00
if ( $numero_totale > 0 ) {
2018-07-19 17:29:21 +02:00
flash () -> info ( tr ( '_NUM_ ddt fatturati!' , [
2020-02-14 12:37:55 +01:00
'_NUM_' => $numero_totale ,
2018-07-07 13:56:22 +02:00
]));
2018-03-15 17:41:09 +01:00
} else {
2018-07-19 17:29:21 +02:00
flash () -> warning ( tr ( 'Nessun ddt fatturato!' ));
2018-03-15 17:41:09 +01:00
}
break ;
2018-03-19 18:17:34 +01:00
2018-03-22 15:40:20 +01:00
case 'delete-bulk' :
2019-05-16 04:52:16 +02:00
foreach ( $id_records as $id ) {
2020-03-03 10:33:32 +01:00
$documento = DDT :: find ( $id );
try {
$documento -> delete ();
} catch ( InvalidArgumentException $e ) {
}
2019-05-16 04:52:16 +02:00
}
2018-06-26 09:41:43 +02:00
2019-05-16 04:52:16 +02:00
flash () -> info ( tr ( 'Ddt eliminati!' ));
2018-03-22 15:40:20 +01:00
break ;
2021-02-01 14:46:41 +01:00
case 'cambia_stato' :
$id_stato = post ( 'id_stato' );
$n_ddt = 0 ;
2021-02-18 18:48:44 +01:00
foreach ( $id_records as $id ) {
2021-02-01 14:46:41 +01:00
$ddt = DDT :: find ( $id );
$ddt -> idstatoddt = $id_stato ;
$ddt -> save ();
++ $n_ddt ;
}
if ( $n_ddt > 0 ) {
flash () -> info ( tr ( 'Stato cambiato a _NUM_ DDT!' , [
'_NUM_' => $n_ordini ,
]));
} else {
flash () -> warning ( tr ( 'Nessun DDT modificato!' ));
}
break ;
2018-03-09 11:56:58 +01:00
}
2019-05-13 17:01:43 +02:00
if ( App :: debug ()) {
2020-11-12 10:13:23 +01:00
$operations [ 'delete-bulk' ] = [
'text' => '<span><i class="fa fa-trash"></i> ' . tr ( 'Elimina selezionati' ) . '</span>' ,
'data' => [
'msg' => tr ( 'Vuoi davvero eliminare i ddt selezionati?' ),
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-danger' ,
],
2019-05-16 04:52:16 +02:00
];
2019-05-13 17:01:43 +02:00
}
2019-05-16 04:52:16 +02:00
2019-05-13 17:01:43 +02:00
$operations [ 'crea_fattura' ] = [
2020-11-12 17:11:09 +01:00
'text' => '<span><i class="fa fa-file-code-o"></i> ' . tr ( 'Fattura _TYPE_' , [ '_TYPE_' => strtolower ( $module [ 'name' ])]),
2018-03-09 11:56:58 +01:00
'data' => [
2020-11-12 17:11:09 +01:00
'title' => tr ( 'Fatturare i _TYPE_ selezionati?' , [ '_TYPE_' => strtolower ( $module [ 'name' ])]),
'msg' => '{[ "type": "checkbox", "label": "<small>' . tr ( 'Aggiungere alle _TYPE_ non ancora emesse?' , [ '_TYPE_' => strtolower ( $module_fatture )]) . '", "placeholder": "' . tr ( 'Aggiungere alle _TYPE_ nello stato bozza?' , [ '_TYPE_' => strtolower ( $module_fatture )]) . ' </ small > " , " name " : " accodare " ]}
2020-02-14 13:12:02 +01:00
< br > {[ " type " : " select " , " label " : " '.tr('Sezionale').' " , " name " : " id_segment " , " required " : 1 , " values " : " query=SELECT id, name AS descrizione FROM zz_segments WHERE id_module= \ ''. $id_fatture .' \ ' AND is_fiscale = 1 ORDER BY name " , " value " : " '. $id_segment .' " ]} ' ,
2018-03-09 11:56:58 +01:00
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-warning' ,
'blank' => false ,
],
2019-05-13 17:01:43 +02:00
];
2021-02-01 14:46:41 +01:00
$operations [ 'cambia_stato' ] = [
'text' => '<span><i class="fa fa-refresh"></i> ' . tr ( 'Cambia stato' ),
'data' => [
'title' => tr ( 'Vuoi davvero cambiare lo stato per questi DDT?' ),
'msg' => tr ( 'Seleziona lo stato in cui spostare tutti i DDT' ) . ' .< br >
< br > {[ " type " : " select " , " label " : " '.tr('Stato').' " , " name " : " id_stato " , " required " : 1 , " values " : " query=SELECT id, descrizione FROM dt_statiddt " ]} ' ,
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-warning' ,
'blank' => false ,
],
];
2018-07-16 13:27:44 +02:00
return $operations ;