2018-09-25 16:47:44 +02:00
< ? php
2018-12-25 11:32:19 +01:00
namespace Common\Components ;
2018-09-25 16:47:44 +02:00
2018-12-29 12:03:22 +01:00
use Common\Document ;
2018-09-25 17:13:23 +02:00
use Illuminate\Database\Eloquent\Builder ;
2018-12-29 12:03:22 +01:00
use Modules\Articoli\Articolo as Original ;
2018-10-04 17:25:42 +02:00
use UnexpectedValueException ;
2018-09-25 16:47:44 +02:00
abstract class Article extends Row
{
2019-02-14 17:49:58 +01:00
protected $serialRowID = null ;
2019-02-12 15:00:08 +01:00
protected $abilita_movimentazione = true ;
2018-09-25 16:47:44 +02:00
2019-02-12 17:21:27 +01:00
protected $qta_movimentazione = 0 ;
2019-01-02 14:15:16 +01:00
public static function build ( Document $document , Original $articolo )
2018-09-25 16:47:44 +02:00
{
2019-01-02 14:15:16 +01:00
$model = parent :: build ( $document , true );
2018-09-25 16:47:44 +02:00
$model -> articolo () -> associate ( $articolo );
$model -> descrizione = $articolo -> descrizione ;
$model -> abilita_serial = $articolo -> abilita_serial ;
$model -> um = $articolo -> um ;
return $model ;
}
2018-09-26 12:20:06 +02:00
abstract public function movimenta ( $qta );
2018-10-04 17:41:31 +02:00
2018-10-04 17:25:42 +02:00
abstract public function getDirection ();
2018-09-26 12:20:06 +02:00
2018-09-26 15:37:46 +02:00
/**
2019-02-12 17:21:27 +01:00
* Imposta i seriali collegati all ' articolo .
2018-09-26 15:37:46 +02:00
*
* @ param array $serials
*/
2018-10-04 17:25:42 +02:00
public function setSerialsAttribute ( $serials )
2018-09-25 16:47:44 +02:00
{
database () -> sync ( 'mg_prodotti' , [
2018-09-25 17:13:23 +02:00
'id_riga_' . $this -> serialRowID => $this -> id ,
2018-10-04 17:25:42 +02:00
'dir' => $this -> getDirection (),
2018-09-25 16:47:44 +02:00
'id_articolo' => $this -> idarticolo ,
], [
2018-09-28 16:43:40 +02:00
'serial' => array_clean ( $serials ),
2018-09-25 16:47:44 +02:00
]);
}
2019-02-12 17:21:27 +01:00
/**
* Rimuove i seriali collegati all ' articolo .
*
* @ param array $serials
*/
public function removeSerials ( $serials )
{
database () -> detach ( 'mg_prodotti' , [
'id_riga_' . $this -> serialRowID => $this -> id ,
'dir' => $this -> getDirection (),
'id_articolo' => $this -> idarticolo ,
], [
'serial' => array_clean ( $serials ),
]);
}
2018-09-26 15:37:46 +02:00
/**
* Restituisce l 'elenco dei seriali collegati all' articolo del documento .
*
* @ return array
*/
2018-09-25 17:13:23 +02:00
public function getSerialsAttribute ()
{
if ( empty ( $this -> abilita_serial )) {
return [];
}
// Individuazione dei seriali
2018-10-04 17:25:42 +02:00
$results = database () -> fetchArray ( 'SELECT serial FROM mg_prodotti WHERE serial IS NOT NULL AND id_riga_' . $this -> serialRowID . ' = ' . prepare ( $this -> id ));
2018-09-26 10:49:38 +02:00
2018-10-04 17:25:42 +02:00
return array_column ( $results , 'serial' );
}
2018-09-26 15:37:46 +02:00
/**
* Modifica la quantità dell ' articolo e movimenta automaticamente il magazzino .
*
2018-10-04 17:41:31 +02:00
* @ param float $value
2018-09-26 15:37:46 +02:00
*/
2018-09-25 16:47:44 +02:00
public function setQtaAttribute ( $value )
{
2018-10-04 17:25:42 +02:00
if ( ! $this -> cleanupSerials ( $value )) {
throw new UnexpectedValueException ();
}
2018-09-25 16:47:44 +02:00
$previous = $this -> qta ;
2018-10-04 17:25:42 +02:00
$diff = $value - $previous ;
2018-09-25 16:47:44 +02:00
2018-12-29 11:42:53 +01:00
$this -> attributes [ 'qta' ] = $value ;
2019-02-12 15:00:08 +01:00
if ( $this -> abilita_movimentazione ) {
2019-02-12 17:21:27 +01:00
$this -> qta_movimentazione += $diff ;
2019-02-12 15:00:08 +01:00
}
2018-09-25 16:47:44 +02:00
$database = database ();
// Se c'è un collegamento ad un ddt, aggiorno la quantità evasa
if ( ! empty ( $this -> idddt )) {
$database -> query ( 'UPDATE dt_righe_ddt SET qta_evasa = qta_evasa + ' . $diff . ' WHERE descrizione = ' . prepare ( $this -> descrizione ) . ' AND idarticolo = ' . prepare ( $this -> idarticolo ) . ' AND idddt = ' . prepare ( $this -> idddt ) . ' AND idiva = ' . prepare ( $this -> idiva ));
}
// Se c'è un collegamento ad un ordine, aggiorno la quantità evasa
if ( ! empty ( $this -> idordine )) {
$database -> query ( 'UPDATE or_righe_ordini SET qta_evasa = qta_evasa + ' . $diff . ' WHERE descrizione = ' . prepare ( $this -> descrizione ) . ' AND idarticolo = ' . prepare ( $this -> idarticolo ) . ' AND idordine = ' . prepare ( $this -> idordine ) . ' AND idiva = ' . prepare ( $this -> idiva ));
}
}
public function articolo ()
{
return $this -> belongsTo ( Original :: class , 'idarticolo' );
}
2018-12-25 13:58:40 +01:00
2019-02-12 15:00:08 +01:00
public function movimentazione ( $value = true )
{
$this -> abilita_movimentazione = $value ;
}
2019-02-12 17:21:27 +01:00
/**
* Salva l ' articolo , eventualmente movimentandone il magazzino .
*
* @ param array $options
*
* @ return bool
*/
public function save ( array $options = [])
{
if ( ! empty ( $this -> qta_movimentazione )) {
$this -> movimenta ( $this -> qta_movimentazione );
}
return parent :: save ( $options );
}
2018-12-29 12:03:22 +01:00
protected static function boot ()
{
parent :: boot ( true );
static :: addGlobalScope ( 'articles' , function ( Builder $builder ) {
$builder -> whereNotNull ( 'idarticolo' ) -> where ( 'idarticolo' , '<>' , 0 );
});
}
protected function usedSerials ()
{
if ( $this -> getDirection () == 'uscita' ) {
$results = database () -> fetchArray ( " SELECT serial FROM mg_prodotti WHERE serial IN (SELECT DISTINCT serial FROM mg_prodotti WHERE dir = 'entrata') AND serial IS NOT NULL AND id_riga_ " . $this -> serialRowID . ' = ' . prepare ( $this -> id ));
return array_column ( $results , 'serial' );
}
return [];
}
protected function cleanupSerials ( $new_qta )
{
// Se la nuova quantità è minore della precedente
if ( $this -> qta > $new_qta ) {
$seriali_usati = $this -> usedSerials ();
$count_seriali_usati = count ( $seriali_usati );
// Controllo sulla possibilità di rimuovere i seriali (se non utilizzati da documenti di vendita)
if ( $this -> getDirection () == 'uscita' && $new_qta < $count_seriali_usati ) {
return false ;
} else {
// Controllo sul numero di seriali effettivi da rimuovere
$seriali = $this -> serials ;
if ( $new_qta < count ( $seriali )) {
$rimovibili = array_diff ( $seriali , $seriali_usati );
// Rimozione dei seriali aggiuntivi
$serials = array_slice ( $rimovibili , 0 , $new_qta - $count_seriali_usati );
$this -> serials = array_merge ( $seriali_usati , $serials );
}
}
}
return true ;
}
2019-01-25 12:41:26 +01:00
2019-02-12 17:21:27 +01:00
protected function customInitCopiaIn ( $original )
2019-01-25 12:41:26 +01:00
{
$this -> articolo () -> associate ( $original -> articolo );
}
2019-02-12 15:00:08 +01:00
2019-02-12 17:21:27 +01:00
protected function customBeforeDataCopiaIn ( $original )
{
$this -> movimentazione ( false );
parent :: customBeforeDataCopiaIn ( $original );
}
protected function customAfterDataCopiaIn ( $original )
2019-02-12 15:00:08 +01:00
{
$this -> movimentazione ( true );
2019-02-12 17:21:27 +01:00
parent :: customAfterDataCopiaIn ( $original );
2019-02-12 15:00:08 +01:00
}
2018-09-25 16:47:44 +02:00
}