From e92430f416aae481b5d0899f90f2836460572a00 Mon Sep 17 00:00:00 2001 From: Thomas Zilio Date: Fri, 3 May 2019 18:28:33 -0700 Subject: [PATCH] Aggiunta relazioni morph tra righe --- include/src/Components/Article.php | 6 ++++-- include/src/Components/Description.php | 27 +++++++++++++++++--------- include/src/Components/Discount.php | 6 ++++-- include/src/Components/MorphTrait.php | 20 +++++++++++++++++++ include/src/Components/Row.php | 10 ++++++---- include/src/Model.php | 5 +++++ modules/fatture/actions.php | 12 +++++------- modules/my_impianti/actions.php | 6 +++--- modules/my_impianti/buttons.php | 2 +- update/2_4_9.sql | 5 +++++ 10 files changed, 71 insertions(+), 28 deletions(-) create mode 100644 include/src/Components/MorphTrait.php diff --git a/include/src/Components/Article.php b/include/src/Components/Article.php index 29c490a36..f24dcbd7a 100644 --- a/include/src/Components/Article.php +++ b/include/src/Components/Article.php @@ -128,8 +128,10 @@ abstract class Article extends Row { parent::boot(true); - static::addGlobalScope('articles', function (Builder $builder) { - $builder->whereNotNull('idarticolo')->where('idarticolo', '<>', 0); + $table = parent::getTableName(); + + static::addGlobalScope('articles', function (Builder $builder) use ($table) { + $builder->whereNotNull($table.'.idarticolo')->where($table.'.idarticolo', '<>', 0); }); } diff --git a/include/src/Components/Description.php b/include/src/Components/Description.php index 5d6c48673..5e119aeaa 100644 --- a/include/src/Components/Description.php +++ b/include/src/Components/Description.php @@ -8,6 +8,8 @@ use Illuminate\Database\Eloquent\Builder; abstract class Description extends Model { + use MorphTrait; + protected $guarded = []; public static function build(Document $document, $bypass = false) @@ -38,7 +40,7 @@ abstract class Description extends Model $this->attributes['qta'] = $value; - $this->evasione($diff); + $this->evasione(-$diff); return $diff; } @@ -129,11 +131,10 @@ abstract class Description extends Model // Azioni specifiche successive $model->customAfterDataCopiaIn($this); - $model->save(); + $model->original_id = $this->id; + $model->original_type = $current; - // Rimozione quantità evasa - $this->qta_evasa = $this->qta_evasa + abs($attributes['qta']); - $this->save(); + $model->save(); return $model; } @@ -164,6 +165,12 @@ abstract class Description extends Model protected function evasione($diff) { + if ($this->hasOriginal()) { + $original = $this->getOriginal(); + + $original->qta_evasa -= $diff; + $original->save(); + } } /** @@ -199,13 +206,15 @@ abstract class Description extends Model { parent::boot(); + $table = parent::getTableName(); + if (!$bypass) { - static::addGlobalScope('descriptions', function (Builder $builder) { - $builder->where('is_descrizione', '=', 1); + static::addGlobalScope('descriptions', function (Builder $builder) use ($table) { + $builder->where($table.'.is_descrizione', '=', 1); }); } else { - static::addGlobalScope('not_descriptions', function (Builder $builder) { - $builder->where('is_descrizione', '=', 0); + static::addGlobalScope('not_descriptions', function (Builder $builder) use ($table) { + $builder->where($table.'.is_descrizione', '=', 0); }); } } diff --git a/include/src/Components/Discount.php b/include/src/Components/Discount.php index 9faf30fdd..b8d297bf1 100644 --- a/include/src/Components/Discount.php +++ b/include/src/Components/Discount.php @@ -43,8 +43,10 @@ abstract class Discount extends Row { parent::boot(true); - static::addGlobalScope('discounts', function (Builder $builder) { - $builder->where('is_sconto', '=', 1); + $table = parent::getTableName(); + + static::addGlobalScope('discounts', function (Builder $builder) use ($table) { + $builder->where($table.'.is_sconto', '=', 1); }); } } diff --git a/include/src/Components/MorphTrait.php b/include/src/Components/MorphTrait.php new file mode 100644 index 000000000..85aed26b5 --- /dev/null +++ b/include/src/Components/MorphTrait.php @@ -0,0 +1,20 @@ +original_type) && !empty($this->original); + } + + public function original() + { + return $this->morphedByMany($this->original_type, 'original', $this->table, 'id'); + } + + public function getOriginal(){ + return $this->original()->first(); + } +} diff --git a/include/src/Components/Row.php b/include/src/Components/Row.php index 32a8af6b2..9336aa232 100644 --- a/include/src/Components/Row.php +++ b/include/src/Components/Row.php @@ -173,13 +173,15 @@ abstract class Row extends Description { parent::boot(true); + $table = parent::getTableName(); + if (!$bypass) { - static::addGlobalScope('rows', function (Builder $builder) { - $builder->whereNull('idarticolo')->orWhere('idarticolo', '=', 0); + static::addGlobalScope('rows', function (Builder $builder) use ($table) { + $builder->whereNull($table.'.idarticolo')->orWhere($table.'.idarticolo', '=', 0); }); - static::addGlobalScope('not_discounts', function (Builder $builder) { - $builder->where('is_sconto', '=', 0); + static::addGlobalScope('not_discounts', function (Builder $builder) use ($table) { + $builder->where($table.'.is_sconto', '=', 0); }); } } diff --git a/include/src/Model.php b/include/src/Model.php index 36869e2e0..cd457b4e1 100644 --- a/include/src/Model.php +++ b/include/src/Model.php @@ -18,4 +18,9 @@ abstract class Model extends Original { return new static(); } + + public static function getTableName() + { + return with(new static())->getTable(); + } } diff --git a/modules/fatture/actions.php b/modules/fatture/actions.php index b08d49c7a..40663e853 100644 --- a/modules/fatture/actions.php +++ b/modules/fatture/actions.php @@ -607,8 +607,8 @@ switch (post('op')) { if (!empty($rsa[$i]['idarticolo'])) { add_movimento_magazzino($rsa[$i]['idarticolo'], $rsa[$i]['qta'], ['iddocumento' => $id_record]); } - - // Ripristino le quantità da evadere nel contratto + + // Ripristino le quantità da evadere nel contratto $dbo->update('co_righe_contratti', [ 'qta_evasa' => 0, @@ -617,16 +617,14 @@ switch (post('op')) { 'idcontratto' => $idcontratto, ] ); - } } else { - - $rs5 = $dbo->fetchArray('SELECT idarticolo, id, qta, descrizione FROM co_righe_documenti WHERE id = '.prepare($idriga).' AND idintervento IS NULL'); + $rs5 = $dbo->fetchArray('SELECT idarticolo, id, qta, descrizione FROM co_righe_documenti WHERE id = '.prepare($idriga).' AND idintervento IS NULL'); if (!empty($idarticolo)) { rimuovi_articolo_dafattura($rs5[0]['idarticolo'], $id_record, $idriga); } - - // Ripristino le quantità da evadere nel contratto + + // Ripristino le quantità da evadere nel contratto $dbo->update('co_righe_contratti', [ 'qta_evasa' => 0, diff --git a/modules/my_impianti/actions.php b/modules/my_impianti/actions.php index 7b0a24d0e..517f52b10 100644 --- a/modules/my_impianti/actions.php +++ b/modules/my_impianti/actions.php @@ -122,12 +122,12 @@ switch ($op) { $dbo->query('ALTER TABLE tmp DROP id'); $dbo->query('INSERT INTO my_impianti SELECT NULL,tmp. * FROM tmp'); $id_record = $dbo->lastInsertedID(); - $dbo->query ('DROP TEMPORARY TABLE tmp'); + $dbo->query('DROP TEMPORARY TABLE tmp'); - $dbo->query ('UPDATE my_impianti SET matricola = CONCAT (matricola, " (copia)") WHERE id = '.prepare($id_record)); + $dbo->query('UPDATE my_impianti SET matricola = CONCAT (matricola, " (copia)") WHERE id = '.prepare($id_record)); flash()->info(tr('Impianto duplicato correttamente!')); - + break; // Rimuovo impianto e scollego tutti i suoi componenti diff --git a/modules/my_impianti/buttons.php b/modules/my_impianti/buttons.php index b164baedd..5d0c2c3ee 100644 --- a/modules/my_impianti/buttons.php +++ b/modules/my_impianti/buttons.php @@ -10,4 +10,4 @@ echo '
-
'; \ No newline at end of file +'; diff --git a/update/2_4_9.sql b/update/2_4_9.sql index f6c4f1457..9c096edc8 100644 --- a/update/2_4_9.sql +++ b/update/2_4_9.sql @@ -301,3 +301,8 @@ UPDATE `zz_modules` `t1` INNER JOIN `zz_modules` `t2` ON (`t1`.`name` = 'Stato d -- Widget spazio utilizzato INSERT INTO `zz_widgets` (`id`, `name`, `type`, `id_module`, `location`, `class`, `query`, `bgcolor`, `icon`, `print_link`, `more_link`, `more_link_type`, `php_include`, `text`, `enabled`, `order`, `help`) VALUES (NULL, 'Spazio utilizzato', 'chart', (SELECT id FROM zz_modules WHERE name = 'Stato dei servizi'), 'controller_right', 'col-md-12', NULL, '#4ccc4c', 'fa fa-hdd-o', '', '', NULL, './modules/stato_servizi/widgets/spazio_utilizzato.php', 'Spazio utilizzato', '1', '1', NULL); + +-- Relazione tra le righe dei documenti +ALTER TABLE `co_righe_documenti` ADD `original_id` int(11), ADD `original_type` varchar(255); +ALTER TABLE `or_righe_ordini` ADD `original_id` int(11), ADD `original_type` varchar(255); +ALTER TABLE `dt_righe_ddt` ADD `original_id` int(11), ADD `original_type` varchar(255);