diff --git a/modules/interventi/src/Stato.php b/modules/interventi/src/Stato.php index 6e2593c4f..1101fd184 100755 --- a/modules/interventi/src/Stato.php +++ b/modules/interventi/src/Stato.php @@ -33,10 +33,11 @@ class Stato extends Model return $this->hasMany(Intervento::class, 'idstatointervento'); } - public static function build($codice) + public static function build($codice, $colore) { $model = new static(); $model->codice = $codice; + $model->colore = $colore; $model->save(); return $model; @@ -56,6 +57,30 @@ class Stato extends Model ->first()->name; } + /** + * Imposta l'attributo name della lista. + */ + 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 intervento a partire dal nome. * diff --git a/modules/stati_intervento/actions.php b/modules/stati_intervento/actions.php index 272d02257..1341ec2f7 100755 --- a/modules/stati_intervento/actions.php +++ b/modules/stati_intervento/actions.php @@ -18,27 +18,32 @@ */ include_once __DIR__.'/../../core.php'; +use Modules\Interventi\Stato; switch (post('op')) { case 'update': - $dbo->update('in_statiintervento', [ - 'codice' => post('codice'), - 'colore' => post('colore'), - 'is_completato' => post('is_completato'), - 'is_fatturabile' => post('is_fatturabile'), - 'notifica' => post('notifica'), - 'notifica_cliente' => post('notifica_cliente'), - 'notifica_tecnico_sessione' => post('notifica_tecnico_sessione'), - 'notifica_tecnico_assegnato' => post('notifica_tecnico_sessione'), - 'id_email' => post('email') ?: null, - 'destinatari' => post('destinatari'), - ], ['id' => $id_record]); + $descrizione = post('descrizione'); + $stato_new = (new Stato())->getByName($descrizione)->id_record; - $dbo->update('in_statiintervento_lang', [ - 'name' => post('descrizione'), - ], ['id_record' => $id_record, 'id_lang' => setting('Lingua')]); + if (!empty($stato_new) && $stato_new != $id_record){ + flash()->error(tr('Questo nome è già stato utilizzato per un altro stato attività.')); + } else { + $stato->codice = post('codice'); + $stato->colore = post('colore'); + $stato->is_completato = post('is_completato'); + $stato->is_fatturabile = post('is_fatturabile'); + $stato->notifica = post('notifica'); + $stato->notifica_cliente = post('notifica_cliente'); + $stato->notifica_tecnico_sessione = post('notifica_tecnico_sessione'); + $stato->notifica_tecnico_assegnato = post('notifica_tecnico_assegnato'); + $stato->id_email = post('email') ?: null; + $stato->destinatari = post('destinatari'); - flash()->info(tr('Informazioni salvate correttamente.')); + $stato->name = $descrizione; + $stato->save(); + + flash()->info(tr('Informazioni salvate correttamente.')); + } break; @@ -47,19 +52,18 @@ switch (post('op')) { $descrizione = post('descrizione'); $colore = post('colore'); - // controllo che il codice non sia duplicato - if (count($dbo->fetchArray('SELECT `id` FROM `in_statiintervento` WHERE `codice`='.prepare($codice))) > 0) { - flash()->warning(tr('Attenzione: lo stato attività _COD_ risulta già esistente.', [ - '_COD_' => $codice, - ])); - } else { - $dbo->query('INSERT INTO in_statiintervento(codice, colore) VALUES ('.prepare($codice).', '.prepare($colore).')'); - $id_record = $database->lastInsertedID(); + $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 attività.')); + } else { + $stato = Stato::build($codice, $colore); + $id_record= $dbo->lastInsertedID(); + $stato->name = $descrizione; + $stato->save(); - $dbo->query('INSERT INTO in_statiintervento_lang (name, id_record, id_lang) VALUES ('.prepare($descrizione).', '.prepare($id_record).', '.prepare(setting('Lingua')).')'); flash()->info(tr('Nuovo stato attività aggiunto.')); } - break; case 'delete': diff --git a/modules/stati_intervento/edit.php b/modules/stati_intervento/edit.php index eed92122c..9dbb61def 100755 --- a/modules/stati_intervento/edit.php +++ b/modules/stati_intervento/edit.php @@ -39,7 +39,7 @@ if ($record['can_delete']) {
- {[ "type": "text", "label": "", "name": "codice", "value": "$codice$", "extra": "" ]} + {[ "type": "text", "label": "", "name": "codice", "value": "$codice$", "extra": "", "required":1 ]}
@@ -62,7 +62,7 @@ if ($record['can_delete']) {
- {[ "type": "select", "label": "", "name": "email", "value": "$id_email$", "values": "query=SELECT `em_templates`.`id`, `em_templates_lang`.`name` AS descrizione FROM `em_templates` LEFT JOIN `em_templates_lang` ON (`em_templates`.`id` = `em_templates_lang`.`id_record` AND `em_templates_lang`.`id_lang` = ) WHERE `id_module` = getByName('Interventi')->id_record; ?> AND `deleted_at` IS NULL", "disabled": ]} + {[ "type": "select", "label": "", "name": "email", "value": "$id_email$", "values": "query=SELECT `em_templates`.`id`, `em_templates_lang`.`name` AS descrizione FROM `em_templates` LEFT JOIN `em_templates_lang` ON (`em_templates`.`id` = `em_templates_lang`.`id_record` AND `em_templates_lang`.`id_lang` = ) WHERE `id_module` = getByName('Interventi')->id_record; ?> AND `deleted_at` IS NULL", "disabled": , "required":1 ]}
diff --git a/modules/stati_intervento/init.php b/modules/stati_intervento/init.php index 4092e3d92..8bf7921c1 100755 --- a/modules/stati_intervento/init.php +++ b/modules/stati_intervento/init.php @@ -18,8 +18,10 @@ */ include_once __DIR__.'/../../core.php'; +use Modules\Interventi\Stato; if (isset($id_record)) { $record = $dbo->fetchOne('SELECT * FROM `in_statiintervento` LEFT JOIN `in_statiintervento_lang` ON (`in_statiintervento`.`id` = `in_statiintervento_lang`.`id_record` AND `in_statiintervento_lang`.`id_lang` = "'.prepare(setting('Lingua')).'") WHERE `in_statiintervento`.`id`='.prepare($id_record)); + $stato = Stato::find($id_record); }