Aggiunta relazioni morph tra righe

This commit is contained in:
Thomas Zilio 2019-05-03 18:28:33 -07:00
parent e68565863c
commit e92430f416
10 changed files with 71 additions and 28 deletions

View File

@ -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);
});
}

View File

@ -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);
});
}
}

View File

@ -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);
});
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace Common\Components;
trait MorphTrait
{
public function hasOriginal()
{
return !empty($this->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();
}
}

View File

@ -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);
});
}
}

View File

@ -18,4 +18,9 @@ abstract class Model extends Original
{
return new static();
}
public static function getTableName()
{
return with(new static())->getTable();
}
}

View File

@ -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,

View File

@ -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

View File

@ -10,4 +10,4 @@ echo '
<form action="" method="post" id="copia-impianto">
<input type="hidden" name="backto" value="record-edit">
<input type="hidden" name="op" value="copy">
</form>';
</form>';

View File

@ -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);