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
use Modules\Articoli\Articolo as Original ;
2018-09-25 17:13:23 +02:00
use Illuminate\Database\Eloquent\Builder ;
2018-10-04 17:25:42 +02:00
use UnexpectedValueException ;
2018-12-25 11:32:19 +01:00
use Common\Document ;
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-12-25 11:32:19 +01:00
public static function make ( Document $document , Original $articolo )
2018-09-25 16:47:44 +02:00
{
2018-12-25 11:33:15 +01:00
$model = parent :: make ( $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
/**
* Imposta i seriali collegati all ' articolo del documento .
*
* @ 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
]);
}
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' );
}
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 ) {
2018-10-04 17:41:31 +02:00
$seriali_usati = $this -> usedSerials ();
2018-10-04 17:25:42 +02:00
$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 ;
2018-09-25 17:13:23 +02:00
}
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-09-26 15:37:46 +02:00
parent :: setQtaAttribute ( $value );
2018-09-25 16:47:44 +02:00
$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' );
}
2018-12-25 13:58:40 +01:00
public function copiaIn ( Document $document )
{
$class = get_class ( $document );
$namespace = implode ( '\\' , explode ( '\\' , $class , - 1 ));
$current = get_class ( $this );
$pieces = explode ( '\\' , $current );
$type = end ( $pieces );
$object = $namespace . '\\Components\\' . $type ;
$attributes = $this -> getAttributes ();
unset ( $attributes [ 'id' ]);
$model = $object :: make ( $document , $this -> articolo );
$model -> save ();
$model = $object :: find ( $model -> id );
$accepted = $model -> getAttributes ();
$attributes = array_intersect_key ( $attributes , $accepted );
$model -> fill ( $attributes );
return $model ;
}
2018-09-25 16:47:44 +02:00
}