2018-08-30 18:20:30 +02:00
< ? php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager : il software gestionale open source per l ' assistenza tecnica e la fatturazione
* Copyright ( C ) DevCode s . n . c .
*
* 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-08-30 18:20:30 +02:00
include_once __DIR__ . '/../../core.php' ;
2020-02-17 13:18:25 +01:00
use Carbon\Carbon ;
use Carbon\CarbonInterval ;
use Modules\Articoli\Articolo as ArticoloOriginale ;
use Modules\Contratti\Contratto ;
2019-11-02 15:55:16 +01:00
use Modules\Interventi\Intervento ;
use Modules\Interventi\Stato ;
use Modules\TipiIntervento\Tipo as TipoSessione ;
2020-02-17 13:18:25 +01:00
use Plugins\PianificazioneInterventi\Components\Articolo ;
use Plugins\PianificazioneInterventi\Components\Riga ;
use Plugins\PianificazioneInterventi\Promemoria ;
2019-11-02 15:55:16 +01:00
2019-03-20 10:16:14 +01:00
$operazione = filter ( 'op' );
2018-08-30 18:20:30 +02:00
// Pianificazione intervento
2019-03-20 10:16:14 +01:00
switch ( $operazione ) {
2018-08-30 18:20:30 +02:00
case 'add-promemoria' :
2020-02-17 13:18:25 +01:00
$contratto = Contratto :: find ( $id_parent );
$tipo = TipoSessione :: find ( filter ( 'idtipointervento' ));
2018-08-30 18:20:30 +02:00
2020-02-17 13:18:25 +01:00
$promemoria = Promemoria :: build ( $contratto , $tipo , filter ( 'data_richiesta' ));
echo $promemoria -> id ;
break ;
2018-08-30 18:20:30 +02:00
case 'edit-promemoria' :
2018-08-31 11:39:38 +02:00
$dbo -> update ( 'co_promemoria' , [
2018-08-30 18:20:30 +02:00
'data_richiesta' => post ( 'data_richiesta' ),
'idtipointervento' => post ( 'idtipointervento' ),
'richiesta' => post ( 'richiesta' ),
'idimpianti' => implode ( ',' , post ( 'idimpianti' )),
2019-05-21 15:21:55 +02:00
'idsede' => implode ( ',' , post ( 'idsede_c' )),
2018-08-30 18:20:30 +02:00
], [ 'id' => $id_record ]);
flash () -> info ( tr ( 'Promemoria inserito!' ));
2019-04-04 17:30:58 +02:00
2018-08-30 18:20:30 +02:00
break ;
// Eliminazione pianificazione
case 'delete-promemoria' :
2018-09-26 17:40:04 +02:00
$id = post ( 'id' );
2018-08-31 11:39:38 +02:00
$dbo -> query ( 'DELETE FROM `co_promemoria` WHERE id=' . prepare ( $id ));
2020-02-17 13:18:25 +01:00
$dbo -> query ( 'DELETE FROM `co_righe_promemoria` WHERE id_promemoria=' . prepare ( $id ));
2018-08-30 18:20:30 +02:00
flash () -> info ( tr ( 'Pianificazione eliminata!' ));
break ;
// Eliminazione tutti i promemoria di questo contratto con non hanno l'intervento associato
case 'delete-non-associati' :
2020-02-17 13:18:25 +01:00
$dbo -> query ( 'DELETE FROM `co_righe_promemoria` WHERE id_promemoria IN (SELECT id FROM `co_promemoria` WHERE idcontratto = :id_contratto AND idintervento IS NULL)' , [
2018-08-30 18:20:30 +02:00
':id_contratto' => $id_record ,
]);
2018-08-31 11:39:38 +02:00
$dbo -> query ( 'DELETE FROM `co_promemoria` WHERE idcontratto = :id_contratto AND idintervento IS NULL' , [
2018-08-30 18:20:30 +02:00
':id_contratto' => $id_record ,
]);
flash () -> info ( tr ( 'Tutti i promemoria non associati sono stati eliminati!' ));
break ;
2019-11-02 15:55:16 +01:00
// Pianificazione ciclica
2018-08-30 18:20:30 +02:00
case 'pianificazione' :
2018-08-31 09:41:43 +02:00
$intervallo = post ( 'intervallo' );
2020-02-17 13:18:25 +01:00
$data_inizio = post ( 'data_inizio' );
$count = 0 ;
2020-02-17 15:09:07 +01:00
$count_interventi = 0 ;
$count_promemoria = 0 ;
2020-02-17 13:18:25 +01:00
$date_con_promemoria = [];
$date_con_intervento = [];
if ( post ( 'pianifica_promemoria' )) {
$promemoria_originale = Promemoria :: find ( $id_record );
$contratto = $promemoria_originale -> contratto ;
// Promemoria del contratto raggruppati per data
$promemoria_contratto = $contratto -> promemoria ()
-> where ( 'idtipointervento' , $promemoria_originale -> tipo -> id )
-> get ()
-> groupBy ( function ( $item ) {
2020-05-04 18:15:18 +02:00
return $item -> data_richiesta -> toDateString ();
2020-02-17 13:18:25 +01:00
});
2020-05-04 18:11:56 +02:00
2020-02-17 13:18:25 +01:00
$date_preimpostate = $promemoria_contratto -> keys () -> toArray ();
$data_conclusione = $contratto -> data_conclusione ;
$data_inizio = new Carbon ( $data_inizio );
$data_richiesta = $data_inizio -> copy ();
$interval = CarbonInterval :: make ( $intervallo . ' days' );
$stato = Stato :: where ( 'codice' , 'WIP' ) -> first (); // Stato "In programmazione"
// Ciclo partendo dalla data_richiesta fino alla data conclusione del contratto
while ( $data_richiesta -> lessThanOrEqualTo ( $data_conclusione )) {
// Creazione ciclica del promemoria se non ne esiste uno per la data richiesta
$data_promemoria = $data_richiesta -> format ( 'Y-m-d' );
if ( ! in_array ( $data_promemoria , $date_preimpostate )) {
$promemoria_corrente = $promemoria_originale -> replicate ();
$promemoria_corrente -> data_richiesta = $data_richiesta ;
$promemoria_corrente -> idintervento = null ;
$promemoria_corrente -> save ();
// Copia delle righe
$righe = $promemoria_originale -> getRighe ();
foreach ( $righe as $riga ) {
$copia = $riga -> replicate ();
$copia -> setParent ( $promemoria_corrente );
$copia -> save ();
}
// Copia degli allegati
$allegati = $promemoria_originale -> uploads ();
foreach ( $allegati as $allegato ) {
$allegato -> copia ([
'id_module' => $allegato -> id_module ,
'id_plugin' => $allegato -> id_plugin ,
'id_record' => $promemoria_corrente -> id ,
]);
2018-08-30 18:20:30 +02:00
}
2020-02-17 15:09:07 +01:00
2020-02-17 18:43:49 +01:00
++ $count_promemoria ;
2020-02-17 13:18:25 +01:00
} else {
$promemoria_corrente = $promemoria_contratto [ $data_promemoria ] -> first ();
$date_con_promemoria [] = dateFormat ( $data_promemoria );
2018-08-30 18:20:30 +02:00
}
2020-05-04 18:15:18 +02:00
// Creazione intervento collegato se non presente
2020-02-17 13:18:25 +01:00
if ( post ( 'pianifica_intervento' ) && empty ( $promemoria -> intervento )) {
// Creazione intervento
$intervento = Intervento :: build ( $contratto -> anagrafica , $promemoria_originale -> tipo , $stato , $data_richiesta );
$intervento -> idsede_destinazione = $promemoria_corrente -> idsede ? : 0 ;
$intervento -> richiesta = $promemoria_corrente -> richiesta ;
$intervento -> idclientefinale = post ( 'idclientefinale' ) ? : 0 ;
$intervento -> save ();
// Aggiungo i tecnici selezionati
$idtecnici = post ( 'idtecnico' );
foreach ( $idtecnici as $idtecnico ) {
2020-02-17 15:09:07 +01:00
add_tecnico ( $intervento -> id , $idtecnico , $data_promemoria . ' ' . post ( 'orario_inizio' ), $data_promemoria . ' ' . post ( 'orario_fine' ));
2020-02-17 13:18:25 +01:00
}
2018-08-30 18:20:30 +02:00
2020-02-17 13:18:25 +01:00
// Copia delle informazioni del promemoria
$promemoria_corrente -> pianifica ( $intervento );
2018-08-30 18:20:30 +02:00
2020-02-17 18:43:49 +01:00
++ $count_interventi ;
2020-02-17 13:18:25 +01:00
} elseif ( post ( 'pianifica_intervento' )) {
$date_con_intervento [] = dateFormat ( $data_promemoria );
}
2018-08-30 18:20:30 +02:00
2020-02-17 13:18:25 +01:00
// Calcolo nuova data richiesta, non considero l'intervallo al primo ciclo
$data_richiesta = $data_richiesta -> add ( $interval );
++ $count ;
}
}
2018-08-30 18:20:30 +02:00
2020-02-17 13:18:25 +01:00
if ( $count == 0 ) {
flash () -> warning ( tr ( 'Nessun promemoria pianificato' ));
} else {
2020-02-17 15:09:07 +01:00
flash () -> info ( tr ( 'Sono stati creati _NUM_ promemoria!' , [
'_NUM_' => $count_promemoria ,
2020-02-17 13:18:25 +01:00
]));
if ( ! empty ( $date_con_promemoria )) {
flash () -> warning ( tr ( 'Le seguenti date presentano già un promemoria pianificato: _LIST_' , [
'_LIST_' => implode ( ', ' , $date_con_promemoria ),
]));
}
2020-02-17 15:09:07 +01:00
if ( post ( 'pianifica_intervento' )) {
flash () -> info ( tr ( 'Sono stati pianificati _NUM_ interventi!' , [
'_NUM_' => $count_interventi ,
2020-02-17 13:18:25 +01:00
]));
2020-02-17 15:09:07 +01:00
if ( ! empty ( $date_con_intervento )) {
flash () -> warning ( tr ( 'I promemoria delle seguenti date presentano già un intervento collegato: _LIST_' , [
'_LIST_' => implode ( ', ' , $date_con_intervento ),
]));
}
2020-02-17 13:18:25 +01:00
}
}
break ;
case 'manage_articolo' :
if ( post ( 'idriga' ) != null ) {
$articolo = Articolo :: find ( post ( 'idriga' ));
} else {
$originale = ArticoloOriginale :: find ( post ( 'idarticolo' ));
$articolo = Articolo :: build ( $promemoria , $originale );
2020-07-06 13:32:43 +02:00
$articolo -> id_dettaglio_fornitore = post ( 'id_dettaglio_fornitore' ) ? : null ;
2020-02-17 13:18:25 +01:00
}
2018-08-30 18:20:30 +02:00
$qta = post ( 'qta' );
2020-02-17 13:18:25 +01:00
$articolo -> descrizione = post ( 'descrizione' );
$articolo -> um = post ( 'um' ) ? : null ;
2018-08-30 18:20:30 +02:00
2020-02-17 13:18:25 +01:00
$articolo -> costo_unitario = post ( 'costo_unitario' ) ? : 0 ;
2020-02-17 19:11:40 +01:00
$articolo -> setPrezzoUnitario ( post ( 'prezzo_unitario' ), post ( 'idiva' ));
2020-02-17 13:18:25 +01:00
$articolo -> setSconto ( post ( 'sconto' ), post ( 'tipo_sconto' ));
2018-08-30 18:20:30 +02:00
2020-02-17 13:18:25 +01:00
try {
$articolo -> qta = $qta ;
} catch ( UnexpectedValueException $e ) {
flash () -> error ( tr ( 'Alcuni serial number sono già stati utilizzati!' ));
}
2018-08-30 18:20:30 +02:00
2020-02-17 13:18:25 +01:00
$articolo -> save ();
2018-08-30 18:20:30 +02:00
2020-02-17 13:18:25 +01:00
if ( post ( 'idriga' ) != null ) {
flash () -> info ( tr ( 'Articolo modificato!' ));
} else {
flash () -> info ( tr ( 'Articolo aggiunto!' ));
}
2018-08-30 18:20:30 +02:00
break ;
2020-02-17 13:18:25 +01:00
case 'manage_riga' :
if ( post ( 'idriga' ) != null ) {
$riga = Riga :: find ( post ( 'idriga' ));
} else {
$riga = Riga :: build ( $promemoria );
}
2018-08-30 18:20:30 +02:00
$qta = post ( 'qta' );
2020-02-17 13:18:25 +01:00
$riga -> descrizione = post ( 'descrizione' );
$riga -> um = post ( 'um' ) ? : null ;
2018-08-30 18:20:30 +02:00
2020-02-17 13:18:25 +01:00
$riga -> costo_unitario = post ( 'costo_unitario' ) ? : 0 ;
2020-02-17 19:11:40 +01:00
$riga -> setPrezzoUnitario ( post ( 'prezzo_unitario' ), post ( 'idiva' ));
2020-02-17 13:18:25 +01:00
$riga -> setSconto ( post ( 'sconto' ), post ( 'tipo_sconto' ));
2018-08-30 18:20:30 +02:00
2020-02-17 13:18:25 +01:00
$riga -> qta = $qta ;
2018-08-30 18:20:30 +02:00
2020-02-17 13:18:25 +01:00
$riga -> save ();
2018-08-30 18:20:30 +02:00
2020-02-17 13:18:25 +01:00
if ( post ( 'idriga' ) != null ) {
flash () -> info ( tr ( 'Riga modificata!' ));
} else {
flash () -> info ( tr ( 'Riga aggiunta!' ));
}
2018-08-30 18:20:30 +02:00
break ;
2020-02-17 13:18:25 +01:00
case 'delete_riga' :
$id_riga = post ( 'idriga' );
$type = post ( 'type' );
$riga = $promemoria -> getRiga ( $type , $id_riga );
if ( ! empty ( $riga )) {
try {
$riga -> delete ();
2018-08-30 18:20:30 +02:00
2020-02-17 13:18:25 +01:00
flash () -> info ( tr ( 'Riga rimossa!' ));
} catch ( InvalidArgumentException $e ) {
flash () -> error ( tr ( 'Alcuni serial number sono già stati utilizzati!' ));
}
}
2018-08-30 18:20:30 +02:00
break ;
}