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
2024-03-22 15:52:24 +01:00
use Models\Module ;
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 ;
2024-03-22 17:43:28 +01:00
if ( $module -> getTranslation ( 'name' ) == 'Ddt di vendita' ) {
2018-09-27 17:27:39 +02:00
$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
2024-03-28 16:38:03 +01:00
$id_fatture = ( new Module ()) -> getByField ( 'name' , $module_fatture , Models\Locale :: getPredefined () -> id );
2018-09-27 17:27:39 +02:00
if ( ! isset ( $_SESSION [ 'module_' . $id_fatture ][ 'id_segment' ])) {
$segments = Modules :: getSegments ( $id_fatture );
2024-04-08 15:44:33 +02:00
$_SESSION [ 'module_' . $id_fatture ][ 'id_segment' ] = $segments [ 0 ][ 'id' ] ? ? null ;
2018-09-27 17:27:39 +02:00
}
$id_segment = $_SESSION [ 'module_' . $id_fatture ][ 'id_segment' ];
2022-03-03 13:08:49 +01:00
$idconto = $module_fatture == 'Fatture di vendita' ? setting ( 'Conto predefinito fatture di vendita' ) : setting ( 'Conto predefinito fatture di acquisto' );
2021-09-22 11:48:58 +02:00
$idtipodocumento = $dbo -> selectOne ( 'co_tipidocumento' , [ 'id' ], [
'predefined' => 1 ,
'dir' => $dir ,
])[ 'id' ];
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
2021-09-22 11:48:58 +02:00
$tipo_documento = Tipo :: where ( 'id' , post ( 'idtipodocumento' )) -> first ();
2020-02-14 13:12:02 +01:00
2024-03-27 16:59:46 +01:00
$stato_documenti_accodabili = ( new Stato ()) -> getByField ( 'name' , 'Bozza' , Models\Locale :: getPredefined () -> id );
2020-02-14 13:12:02 +01:00
$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' );
2024-01-19 15:25:36 +01:00
$raggruppamento = post ( 'raggruppamento' );
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
2024-01-19 15:25:36 +01:00
$id_sede = $raggruppamento == 'sede' ? $documento_import -> idsede_destinazione : 0 ;
if ( $raggruppamento == 'sede' ) {
2024-04-09 12:18:08 +02:00
$fattura = $documenti -> first ( fn ( $item , $key ) => $item -> anagrafica -> id == $id_anagrafica && $item -> idsede_destinazione == $id_sede );
2024-01-19 15:25:36 +01:00
} else {
2024-04-09 12:18:08 +02:00
$fattura = $documenti -> first ( fn ( $item , $key ) => $item -> anagrafica -> id == $id_anagrafica );
2024-01-19 15:25:36 +01:00
}
2020-07-16 10:37:56 +02:00
// Ricerca fattura per anagrafica se l'impostazione di accodamento è selezionata
if ( ! empty ( $accodare ) && empty ( $fattura )) {
2024-01-19 15:25:36 +01:00
if ( $raggruppamento == 'sede' ) {
$fattura = Fattura :: where ( 'idanagrafica' , $id_anagrafica )
-> where ( 'idstatodocumento' , $stato_documenti_accodabili -> id )
-> where ( 'idtipodocumento' , $tipo_documento -> id )
-> first ();
} else {
$fattura = Fattura :: where ( 'idanagrafica' , $id_anagrafica )
-> where ( 'idstatodocumento' , $stato_documenti_accodabili -> id )
-> where ( 'idtipodocumento' , $tipo_standard -> id )
-> where ( 'idsede_destinazione' , $id_sede )
-> first ();
}
2020-07-16 10:37:56 +02:00
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 );
2024-01-19 15:25:36 +01:00
$fattura -> idsede_destinazione = $id_sede ;
$fattura -> save ();
2020-07-16 10:37:56 +02:00
$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
2024-01-15 15:30:45 +01:00
// Fix per idconto righe fattura
2021-04-28 15:14:52 +02:00
$articolo = ArticoloOriginale :: find ( $copia -> idarticolo );
2024-04-08 15:44:33 +02:00
$copia -> id_conto = ( $articolo -> idconto_vendita ? : $idconto );
2021-04-28 15:14:52 +02:00
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
}
2024-01-15 15:30:45 +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!' ));
2024-01-15 15:30:45 +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!' ));
}
2024-01-15 15:30:45 +01:00
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' ] = [
2024-03-22 17:43:28 +01:00
'text' => '<span><i class="fa fa-file-code-o"></i> ' . tr ( 'Fattura _TYPE_' , [ '_TYPE_' => strtolower ( $module -> getTranslation ( 'name' ))]),
2024-03-22 15:52:24 +01:00
'data' => [
2024-03-22 17:43:28 +01:00
'title' => tr ( 'Fatturare i _TYPE_ selezionati?' , [ '_TYPE_' => strtolower ( $module -> getTranslation ( 'name' ))]),
2024-03-22 15:52:24 +01:00
'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 " ]}<br>
2023-08-04 14:54:28 +02:00
{[ " type " : " select " , " label " : " '.tr('Sezionale').' " , " name " : " id_segment " , " required " : 1 , " ajax-source " : " segmenti " , " select-options " : '.json_encode([' id_module ' => $id_fatture, ' is_sezionale ' => 1]).' , " value " : " '. $id_segment .' " , " select-options-escape " : true ]} < br >
2024-04-02 17:33:12 +02:00
{[ " type " : " select " , " label " : " '.tr('Tipo documento').' " , " name " : " idtipodocumento " , " required " : 1 , " values " : " query=SELECT `co_tipidocumento`.`id`, CONCAT(`codice_tipo_documento_fe`, \ ' - \ ', `co_tipidocumento_lang`.`name`) AS descrizione FROM `co_tipidocumento` LEFT JOIN `co_tipidocumento_lang` ON (`co_tipidocumento`.`id`=`co_tipidocumento_lang`.`id_record` AND `co_tipidocumento_lang`.`id_lang`='.prepare(Models \ Locale::getDefault()->id).') WHERE `enabled` = 1 AND `dir` ='.prepare( $dir ).' ORDER BY `codice_tipo_documento_fe` " , " value " : " '. $idtipodocumento .' " ]} < br >
2024-01-19 15:25:36 +01:00
{[ " type " : " select " , " label " : " '.tr('Raggruppa per').' " , " name " : " raggruppamento " , " required " : 1 , " values " : " list= \" cliente \" : \" Cliente \" , \" sede \" : \" Sede \" " ]} ' ,
2024-03-22 15:52:24 +01:00
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-warning' ,
'blank' => false ,
],
];
2019-05-13 17:01:43 +02:00
2024-01-15 15:30:45 +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 >
2024-04-02 17:33:12 +02:00
< br > {[ " type " : " select " , " label " : " '.tr('Stato').' " , " name " : " id_stato " , " required " : 1 , " values " : " query=SELECT `dt_statiddt`.`id`, `dt_statiddt_lang`.`name` as descrizione, `colore` as _bgcolor_ FROM dt_statiddt LEFT JOIN `dt_statiddt_lang` ON (`dt_statiddt`.`id` = `dt_statiddt_lang`.`id_record` AND `dt_statiddt_lang`.`id_lang` = '.prepare(Models \ Locale::getDefault()->id).') " ]} ' ,
2024-01-15 15:30:45 +01:00
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-warning' ,
'blank' => false ,
],
];
2021-02-01 14:46:41 +01:00
2018-07-16 13:27:44 +02:00
return $operations ;