2020-07-22 10:52:18 +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 />.
*/
2020-07-22 10:52:18 +02:00
2020-07-24 10:20:42 +02:00
namespace API\App\v1 ;
2020-07-22 10:52:18 +02:00
2020-07-24 10:20:42 +02:00
use API\App\AppResource ;
use API\Exceptions\InternalError ;
2020-07-22 10:52:18 +02:00
use Auth ;
use Carbon\Carbon ;
2020-07-24 10:20:42 +02:00
use Modules\Articoli\Articolo as ArticoloOriginale ;
2020-07-22 10:52:18 +02:00
use Modules\Interventi\Components\Articolo ;
2021-08-30 14:28:16 +02:00
use Modules\Interventi\Components\Descrizione ;
2020-07-22 10:52:18 +02:00
use Modules\Interventi\Components\Riga ;
use Modules\Interventi\Components\Sconto ;
use Modules\Interventi\Intervento ;
2020-07-24 10:20:42 +02:00
use UnexpectedValueException ;
2020-07-22 10:52:18 +02:00
2020-07-28 09:42:05 +02:00
class RigheInterventi extends AppResource
2020-07-22 10:52:18 +02:00
{
2020-07-28 12:12:27 +02:00
public function getCleanupData ( $last_sync_at )
2020-07-22 10:52:18 +02:00
{
// Periodo per selezionare interventi
$today = new Carbon ();
$start = $today -> copy () -> subMonths ( 2 );
2022-12-06 16:47:21 +01:00
$end = $today -> copy () -> addMonth ();
2020-07-22 10:52:18 +02:00
// Informazioni sull'utente
$user = Auth :: user ();
$id_tecnico = $user -> id_anagrafica ;
2021-02-25 16:33:19 +01:00
// Elenco di interventi di interesse
$risorsa_interventi = $this -> getRisorsaInterventi ();
$interventi = $risorsa_interventi -> getCleanupData ( $last_sync_at );
// Elenco sessioni degli interventi da rimuovere
$da_interventi = [];
if ( ! empty ( $interventi )) {
$query = ' SELECT in_righe_interventi . id
FROM in_righe_interventi
2021-02-26 10:36:21 +01:00
INNER JOIN in_interventi ON in_righe_interventi . idintervento = in_interventi . id
2021-02-25 16:33:19 +01:00
WHERE
2021-02-25 18:30:08 +01:00
in_interventi . id IN ( '.implode(' , ', $interventi).' ) ' ;
$records = database () -> fetchArray ( $query );
2021-02-26 11:04:06 +01:00
2021-02-25 16:33:19 +01:00
$da_interventi = array_column ( $records , 'id' );
}
2020-07-22 10:52:18 +02:00
2021-02-25 16:33:19 +01:00
$mancanti = $this -> getMissingIDs ( 'in_righe_interventi' , 'id' , $last_sync_at );
2020-07-22 10:52:18 +02:00
$results = array_unique ( array_merge ( $da_interventi , $mancanti ));
return $results ;
}
2020-07-28 12:12:27 +02:00
public function getModifiedRecords ( $last_sync_at )
2020-07-22 10:52:18 +02:00
{
// Periodo per selezionare interventi
$today = new Carbon ();
$start = $today -> copy () -> subMonths ( 2 );
2022-12-06 16:47:21 +01:00
$end = $today -> copy () -> addMonth ();
2020-07-22 10:52:18 +02:00
// Informazioni sull'utente
$user = Auth :: user ();
$id_tecnico = $user -> id_anagrafica ;
2021-02-25 16:33:19 +01:00
// Elenco di interventi di interesse
$risorsa_interventi = $this -> getRisorsaInterventi ();
$interventi = $risorsa_interventi -> getModifiedRecords ( null );
if ( empty ( $interventi )) {
return [];
}
$id_interventi = array_keys ( $interventi );
$query = 'SELECT in_righe_interventi.id, in_righe_interventi.updated_at FROM in_righe_interventi WHERE in_righe_interventi.idintervento IN (' . implode ( ',' , $id_interventi ) . ')' ;
2020-07-22 10:52:18 +02:00
// Filtro per data
if ( $last_sync_at ) {
2020-07-28 09:42:05 +02:00
$query .= ' AND in_righe_interventi.updated_at > ' . prepare ( $last_sync_at );
2020-07-22 10:52:18 +02:00
}
2021-02-25 16:33:19 +01:00
$records = database () -> fetchArray ( $query );
2020-07-22 10:52:18 +02:00
2021-02-24 11:48:38 +01:00
return $this -> mapModifiedRecords ( $records );
2020-07-22 10:52:18 +02:00
}
2020-07-28 12:12:27 +02:00
public function retrieveRecord ( $id )
2020-07-22 10:52:18 +02:00
{
// Individuazione riga tramite classi
2020-07-24 10:20:42 +02:00
$riga = $this -> getRecord ( $id );
2020-07-22 10:52:18 +02:00
// Generazione del record ristretto ai campi di interesse
$record = [
'id' => $riga -> id ,
'id_intervento' => $riga -> idintervento ,
'descrizione' => $riga -> descrizione ,
'qta' => $riga -> qta ,
'um' => $riga -> um ,
'ordine' => $riga -> order ,
// Caratteristiche della riga
'id_articolo' => $riga -> idarticolo ,
'is_articolo' => intval ( $riga -> isArticolo ()),
'is_riga' => intval ( $riga -> isRiga ()),
'is_descrizione' => intval ( $riga -> isDescrizione ()),
'is_sconto' => intval ( $riga -> isSconto ()),
// Campi contabili
'costo_unitario' => $riga -> costo_unitario ,
'prezzo_unitario' => $riga -> prezzo_unitario ,
'tipo_sconto' => $riga -> tipo_sconto ,
'sconto_percentuale' => $riga -> sconto_percentuale ,
'sconto_unitario' => $riga -> sconto_unitario ,
'id_iva' => $riga -> idiva ,
'iva_unitaria' => $riga -> iva_unitaria ,
'prezzo_unitario_ivato' => $riga -> prezzo_unitario_ivato ,
'sconto_iva_unitario' => $riga -> sconto_iva_unitario ,
'sconto_unitario_ivato' => $riga -> sconto_unitario_ivato ,
// Campi contabili di riepilogo
'imponibile' => $riga -> imponibile ,
'sconto' => $riga -> sconto ,
'totale_imponibile' => $riga -> totale_imponibile ,
'iva' => $riga -> iva ,
'totale' => $riga -> totale ,
];
return $record ;
}
2020-07-24 10:20:42 +02:00
2020-07-28 12:12:27 +02:00
public function createRecord ( $data )
2020-07-24 10:20:42 +02:00
{
$intervento = Intervento :: find ( $data [ 'id_intervento' ]);
if ( $data [ 'is_articolo' ]) {
$originale = ArticoloOriginale :: find ( $data [ 'id_articolo' ]);
$riga = Articolo :: build ( $intervento , $originale );
} elseif ( $data [ 'is_sconto' ]) {
// TODO: sconti
} else {
$riga = Riga :: build ( $intervento );
}
$this -> aggiornaRecord ( $riga , $data );
$riga -> save ();
return [
'id' => $riga -> id ,
];
}
2020-07-28 12:12:27 +02:00
public function updateRecord ( $data )
2020-07-24 10:20:42 +02:00
{
$riga = $this -> getRecord ( $data [ 'id' ]);
$this -> aggiornaRecord ( $riga , $data );
$riga -> save ();
return [];
}
2020-07-28 12:12:27 +02:00
public function deleteRecord ( $id )
2020-07-24 10:20:42 +02:00
{
$riga = $this -> getRecord ( $id );
$riga -> delete ();
}
2021-02-25 16:33:19 +01:00
protected function getRisorsaInterventi ()
{
return new Interventi ();
}
2020-07-28 12:12:27 +02:00
protected function getRecord ( $id )
{
// Individuazione delle caratteristiche del record
$data = database () -> fetchOne ( ' SELECT idintervento AS id_intervento ,
IF ( idarticolo IS NULL OR idarticolo = 0 , 0 , 1 ) AS is_articolo ,
is_descrizione ,
is_sconto
FROM in_righe_interventi WHERE in_righe_interventi . id = ' . prepare ( $id ));
// Individuazione riga tramite classi
$type = $this -> getType ( $data );
$intervento = Intervento :: find ( $data [ 'id_intervento' ]);
return $intervento -> getRiga ( $type , $id );
}
protected function getType ( $data )
{
if ( ! empty ( $data [ 'is_sconto' ])) {
$type = Sconto :: class ;
2021-08-30 14:28:16 +02:00
} elseif ( ! empty ( $data [ 'is_descrizione' ])) {
$type = Descrizione :: class ;
2020-07-28 12:12:27 +02:00
} elseif ( ! empty ( $data [ 'is_articolo' ])) {
$type = Articolo :: class ;
} else {
$type = Riga :: class ;
}
return $type ;
}
2020-07-24 10:20:42 +02:00
protected function aggiornaRecord ( $record , $data )
{
$record -> descrizione = $data [ 'descrizione' ];
$record -> um = $data [ 'um' ] ? : null ;
2021-02-24 09:24:29 +01:00
if ( empty ( $data [ 'id_iva' ])) {
2021-02-23 17:45:30 +01:00
if ( $data [ 'is_articolo' ]) {
2021-02-24 09:24:29 +01:00
$originale = ArticoloOriginale :: find ( $data [ 'id_articolo' ]);
$data [ 'id_iva' ] = $originale -> idiva_vendita ;
} else {
$data [ 'id_iva' ] = setting ( 'Iva predefinita' );
2021-02-23 17:45:30 +01:00
}
2021-02-12 18:36:34 +01:00
}
2020-07-24 10:20:42 +02:00
try {
$record -> qta = $data [ 'qta' ];
} catch ( UnexpectedValueException $e ) {
throw new InternalError ();
}
2021-02-24 17:24:19 +01:00
// Impostazione prezzo unitario
$data [ 'prezzo_unitario' ] = $data [ 'prezzo_unitario' ] ? : 0 ;
$record -> setPrezzoUnitario ( $data [ 'prezzo_unitario' ], $data [ 'id_iva' ]);
// Impostazione sconto
$sconto = $data [ 'sconto_percentuale' ] ? : $data [ 'sconto_unitario' ];
if ( ! empty ( $sconto )) {
$record -> setSconto ( $sconto , $data [ 'tipo_sconto' ]);
}
2020-07-24 10:20:42 +02:00
}
2020-07-22 10:52:18 +02:00
}