From 49083abc6a93e3f8d4e6d67f7afd2881097cebac Mon Sep 17 00:00:00 2001 From: Pek5892 Date: Thu, 7 Mar 2024 09:37:01 +0100 Subject: [PATCH] Allineamento add stati documento --- modules/anagrafiche/src/Tipo.php | 6 ++++-- modules/contratti/src/Stato.php | 6 ++++-- modules/fatture/src/Stato.php | 25 +++++++++++++++++++++++++ modules/iva/src/Aliquota.php | 6 ++++-- modules/pagamenti/src/Pagamento.php | 6 ++++-- modules/stati_fattura/actions.php | 25 +++++++++++++------------ modules/stati_fattura/init.php | 3 +++ 7 files changed, 57 insertions(+), 20 deletions(-) diff --git a/modules/anagrafiche/src/Tipo.php b/modules/anagrafiche/src/Tipo.php index 8d74735a9..c06998509 100755 --- a/modules/anagrafiche/src/Tipo.php +++ b/modules/anagrafiche/src/Tipo.php @@ -76,7 +76,9 @@ class Tipo extends Model */ public function setNameAttribute($value) { - $translated = database()->table($this->table.'_lang') + $table = database()->table($this->table.'_lang'); + + $translated = $table ->where('id_record', '=', $this->id) ->where('id_lang', '=', setting('Lingua')); @@ -85,7 +87,7 @@ class Tipo extends Model 'name' => $value ]); } else { - $translated->insert([ + $table->insert([ 'id_record' => $this->id, 'id_lang' => setting('Lingua'), 'name' => $value diff --git a/modules/contratti/src/Stato.php b/modules/contratti/src/Stato.php index b54694f64..24672d978 100755 --- a/modules/contratti/src/Stato.php +++ b/modules/contratti/src/Stato.php @@ -66,7 +66,9 @@ class Stato extends Model */ public function setNameAttribute($value) { - $translated = database()->table($this->table.'_lang') + $table = database()->table($this->table.'_lang'); + + $translated = $table ->where('id_record', '=', $this->id) ->where('id_lang', '=', setting('Lingua')); @@ -75,7 +77,7 @@ class Stato extends Model 'name' => $value ]); } else { - $translated->insert([ + $table->insert([ 'id_record' => $this->id, 'id_lang' => setting('Lingua'), 'name' => $value diff --git a/modules/fatture/src/Stato.php b/modules/fatture/src/Stato.php index 16c7be4c7..61e54edbb 100755 --- a/modules/fatture/src/Stato.php +++ b/modules/fatture/src/Stato.php @@ -47,6 +47,31 @@ class Stato extends Model ->first()->name; } + /** + * Imposta l'attributo name dell'articolo. + */ + public function setNameAttribute($value) + { + $table = database()->table($this->table.'_lang'); + + $translated = $table + ->where('id_record', '=', $this->id) + ->where('id_lang', '=', setting('Lingua')); + + if ($translated->count() > 0) { + $translated->update([ + 'name' => $value + ]); + } else { + $table->insert([ + 'id_record' => $this->id, + 'id_lang' => setting('Lingua'), + 'name' => $value + ]); + } + } + + /** * Ritorna l'id dello stato fattura a partire dal nome. * diff --git a/modules/iva/src/Aliquota.php b/modules/iva/src/Aliquota.php index 452c38752..529346adc 100755 --- a/modules/iva/src/Aliquota.php +++ b/modules/iva/src/Aliquota.php @@ -79,7 +79,9 @@ class Aliquota extends Model */ public function setNameAttribute($value) { - $translated = database()->table($this->table.'_lang') + $table = database()->table($this->table.'_lang'); + + $translated = $table ->where('id_record', '=', $this->id) ->where('id_lang', '=', setting('Lingua')); @@ -88,7 +90,7 @@ class Aliquota extends Model 'name' => $value ]); } else { - $translated->insert([ + $table->insert([ 'id_record' => $this->id, 'id_lang' => setting('Lingua'), 'name' => $value diff --git a/modules/pagamenti/src/Pagamento.php b/modules/pagamenti/src/Pagamento.php index 2fa0ea327..5e3e96906 100755 --- a/modules/pagamenti/src/Pagamento.php +++ b/modules/pagamenti/src/Pagamento.php @@ -185,7 +185,9 @@ class Pagamento extends Model */ public function setNameAttribute($value) { - $translated = database()->table($this->table.'_lang') + $table = database()->table($this->table.'_lang'); + + $translated = $table ->where('id_record', '=', $this->id) ->where('id_lang', '=', setting('Lingua')); @@ -194,7 +196,7 @@ class Pagamento extends Model 'name' => $value ]); } else { - $translated->insert([ + $table->insert([ 'id_record' => $this->id, 'id_lang' => setting('Lingua'), 'name' => $value diff --git a/modules/stati_fattura/actions.php b/modules/stati_fattura/actions.php index 61ea34c93..1adf1d179 100644 --- a/modules/stati_fattura/actions.php +++ b/modules/stati_fattura/actions.php @@ -18,19 +18,20 @@ */ include_once __DIR__.'/../../core.php'; +use Modules\Fatture\Stato; switch (post('op')) { case 'update': - $dbo->update('co_statidocumento', [ - 'icona' => post('icona'), - 'colore' => post('colore'), - ], ['id' => $id_record]); - - $dbo->update('co_statidocumento_lang', [ - 'name' => post('descrizione'), - ], ['id_record' => $id_record]); - - flash()->info(tr('Informazioni salvate correttamente.')); - + $descrizione = post('descrizione'); + $stato_new = Stato::find((new Stato())->getByName($descrizione)->id_record); + + if ($stato_new) { + flash()->error(tr('Questo nome è già stato utilizzato per un altro stato.')); + } else { + $stato->icona = post('icona'); + $stato->colore = post('colore'); + $stato->name = $descrizione; + $stato->save(); break; -} + } +} \ No newline at end of file diff --git a/modules/stati_fattura/init.php b/modules/stati_fattura/init.php index 355d92461..4abb68e76 100644 --- a/modules/stati_fattura/init.php +++ b/modules/stati_fattura/init.php @@ -18,7 +18,10 @@ */ include_once __DIR__.'/../../core.php'; +use Modules\Fatture\Stato; if (isset($id_record)) { $record = $dbo->fetchOne('SELECT * FROM `co_statidocumento` LEFT JOIN (`co_statidocumento_lang`) ON (`co_statidocumento_lang`.`id_record` = `co_statidocumento`.`id` AND `co_statidocumento_lang`.`id_lang` = '.prepare(setting('Lingua')).') WHERE `co_statidocumento`.`id`='.prepare($id_record)); + + $stato = Stato::find($id_record); }