Allineamento add stati intervento

This commit is contained in:
Pek5892 2024-03-07 15:08:26 +01:00
parent 675d1f11da
commit 91ddc0c77b
4 changed files with 60 additions and 29 deletions

View File

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

View File

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

View File

@ -39,7 +39,7 @@ if ($record['can_delete']) {
<div class="col-md-9">
<div class="row">
<div class="col-md-3">
{[ "type": "text", "label": "<?php echo tr('Codice'); ?>", "name": "codice", "value": "$codice$", "extra": "<?php echo $attr; ?>" ]}
{[ "type": "text", "label": "<?php echo tr('Codice'); ?>", "name": "codice", "value": "$codice$", "extra": "<?php echo $attr; ?>", "required":1 ]}
</div>
<div class="col-md-6">
@ -62,7 +62,7 @@ if ($record['can_delete']) {
<div class="row">
<div class="col-md-6">
{[ "type": "select", "label": "<?php echo tr('Template email'); ?>", "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` = <?php echo prepare(setting('Lingua')); ?>) WHERE `id_module` = <?php echo (new Module())->getByName('Interventi')->id_record; ?> AND `deleted_at` IS NULL", "disabled": <?php echo intval(empty($record['notifica'])); ?> ]}
{[ "type": "select", "label": "<?php echo tr('Template email'); ?>", "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` = <?php echo prepare(setting('Lingua')); ?>) WHERE `id_module` = <?php echo (new Module())->getByName('Interventi')->id_record; ?> AND `deleted_at` IS NULL", "disabled": <?php echo intval(empty($record['notifica'])); ?>, "required":1 ]}
</div>
<div class="col-md-6">

View File

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