2018-09-25 16:47:44 +02:00
< ? php
namespace Base ;
use Modules\Articoli\Articolo as Original ;
2018-09-25 17:13:23 +02:00
use Illuminate\Database\Eloquent\Builder ;
2018-09-25 16:47:44 +02:00
abstract class Article extends Row
{
2018-09-25 17:13:23 +02:00
protected $serialRowID = 'documento' ;
2018-09-25 16:47:44 +02:00
protected static function boot ()
{
2018-09-26 12:06:24 +02:00
parent :: boot ( true );
2018-09-25 16:47:44 +02:00
static :: addGlobalScope ( 'articles' , function ( Builder $builder ) {
$builder -> whereNotNull ( 'idarticolo' ) -> where ( 'idarticolo' , '<>' , 0 );
});
}
2018-10-02 18:25:52 +02:00
public static function make ( Original $articolo )
2018-09-25 16:47:44 +02:00
{
2018-10-02 18:25:52 +02:00
$model = parent :: make ( 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-09-26 15:37:46 +02:00
/**
* Imposta i seriali collegati all ' articolo del documento .
*
* @ param array $serials
*/
2018-09-25 16:47:44 +02:00
public function setSerials ( $serials )
{
database () -> sync ( 'mg_prodotti' , [
2018-09-25 17:13:23 +02:00
'id_riga_' . $this -> serialRowID => $this -> id ,
2018-09-25 16:47:44 +02:00
'dir' => 'entrata' ,
'id_articolo' => $this -> idarticolo ,
], [
2018-09-28 16:43:40 +02:00
'serial' => array_clean ( $serials ),
2018-09-25 16:47:44 +02:00
]);
}
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
$list = 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-09-25 17:13:23 +02:00
return array_column ( $list , 'serial' );
}
2018-09-26 15:37:46 +02:00
/**
* Modifica la quantità dell ' articolo e movimenta automaticamente il magazzino .
*
* @ param double $value
*/
2018-09-25 16:47:44 +02:00
public function setQtaAttribute ( $value )
{
$previous = $this -> qta ;
2018-09-26 15:37:46 +02:00
parent :: setQtaAttribute ( $value );
2018-09-25 16:47:44 +02:00
$diff = $value - $previous ;
$this -> movimenta ( $diff );
$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' );
}
}