From 3c5cdbb9319190c185aaa3b4ffc2e4ff9d6d48e7 Mon Sep 17 00:00:00 2001 From: Thomas Zilio Date: Fri, 25 Jan 2019 12:41:26 +0100 Subject: [PATCH] Fix #449 e miglioramenti vari --- core.php | 2 +- include/src/Components/Article.php | 31 +++---------------- include/src/Components/Description.php | 43 ++++++++++++++++++++++++-- modules/fatture/actions.php | 2 +- 4 files changed, 48 insertions(+), 30 deletions(-) diff --git a/core.php b/core.php index e29f09032..a900822b2 100644 --- a/core.php +++ b/core.php @@ -111,7 +111,7 @@ Monolog\ErrorHandler::register($logger, [], Monolog\Logger::ERROR, Monolog\Logge // Aggiunta di Monolog a Whoops if (App::debug()) { - $whoops->pushHandler(function (\Whoops\Exception\ErrorException $exception, $inspector, $run) use ($logger) { + $whoops->pushHandler(function ($exception, $inspector, $run) use ($logger) { $logger->addError($exception->getMessage(), [ 'code' => $exception->getCode(), 'message' => $exception->getMessage(), diff --git a/include/src/Components/Article.php b/include/src/Components/Article.php index 7c6819def..993a91e4e 100644 --- a/include/src/Components/Article.php +++ b/include/src/Components/Article.php @@ -96,32 +96,6 @@ abstract class Article extends Row return $this->belongsTo(Original::class, 'idarticolo'); } - 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::build($document, $this->articolo); - $model->save(); - - $model = $object::find($model->id); - $accepted = $model->getAttributes(); - - $attributes = array_intersect_key($attributes, $accepted); - $model->fill($attributes); - - return $model; - } - protected static function boot() { parent::boot(true); @@ -169,4 +143,9 @@ abstract class Article extends Row return true; } + + protected function customCopiaIn($original) + { + $this->articolo()->associate($original->articolo); + } } diff --git a/include/src/Components/Description.php b/include/src/Components/Description.php index b219e4d74..b552d4d5a 100644 --- a/include/src/Components/Description.php +++ b/include/src/Components/Description.php @@ -23,6 +23,11 @@ abstract class Description extends Model return $model; } + /** + * Imposta il proprietario dell'oggetto e l'ordine relativo all'interno delle righe. + * + * @param Document $document + */ public function setParent(Document $document) { $this->parent()->associate($document); @@ -35,8 +40,17 @@ abstract class Description extends Model $this->save(); } - public function copiaIn(Document $document) + /** + * Copia l'oggetto (articolo, riga, descrizione) nel corrispettivo per il documento indicato. + * + * @param Document $document + * @param float|null $qta + * + * @return self + */ + public function copiaIn(Document $document, $qta = null) { + // Individuazione classe di destinazione $class = get_class($document); $namespace = implode('\\', explode('\\', $class, -1)); @@ -46,18 +60,34 @@ abstract class Description extends Model $object = $namespace.'\\Components\\'.$type; + // Attributi dell'oggetto da copiare $attributes = $this->getAttributes(); unset($attributes['id']); - $model = $object::build($document); + if ($qta !== null) { + $attributes['qta'] = $qta; + } + + // Creazione del nuovo oggetto + $model = new $object(); + $model->setParent($document); + + $model->customCopiaIn($this); + $model->save(); + // Impostazione degli attributi $model = $object::find($model->id); $accepted = $model->getAttributes(); $attributes = array_intersect_key($attributes, $accepted); $model->fill($attributes); + $model->save(); + + // Rimozione quantità evasa + $this->qta_evasa = $this->qta_evasa + $attributes['qta']; + return $model; } @@ -65,6 +95,15 @@ abstract class Description extends Model abstract public function getParentID(); + /** + * Azione personalizzata per la copia dell'oggetto. + * + * @param $original + */ + protected function customCopiaIn($original) + { + } + protected static function boot($bypass = false) { parent::boot(); diff --git a/modules/fatture/actions.php b/modules/fatture/actions.php index c8fbb7225..f6eee76f3 100644 --- a/modules/fatture/actions.php +++ b/modules/fatture/actions.php @@ -444,7 +444,7 @@ switch (post('op')) { if (!empty(post('import'))) { // Replicazione delle righe del contratto sul documento - $righe = $dbo->fetchArray('SELECT idarticolo, idiva, desc_iva, iva, iva_indetraibile, descrizione, subtotale, um, qta, sconto, sconto_unitario, tipo_sconto, IFNULL( (SELECT mg_articoli.abilita_serial FROM mg_articoli WHERE mg_articoli.id=co_righe_contratti.idarticolo), 0 ) AS abilita_serial FROM co_righe_contratti WHERE idcontratto='.prepare($idcontratto)); + $righe = $dbo->fetchArray('SELECT *, IFNULL( (SELECT mg_articoli.abilita_serial FROM mg_articoli WHERE mg_articoli.id=co_righe_contratti.idarticolo), 0 ) AS abilita_serial FROM co_righe_contratti WHERE idcontratto='.prepare($idcontratto)); foreach ($righe as $key => $riga) { $subtot = $riga['subtotale'];