Skip to content

Commit 700d29d

Browse files
committed
refactor: gestione firma attività
1 parent 4aa4229 commit 700d29d

6 files changed

Lines changed: 187 additions & 70 deletions

File tree

modules/interventi/actions.php

Lines changed: 76 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,6 @@
933933
if (is_writable(Uploads::getDirectory($id_module))) {
934934
if (post('firma_base64') != '') {
935935
// Salvataggio firma
936-
$firma_file = 'firma_'.time().'.jpg';
937936
$firma_nome = post('firma_nome');
938937

939938
$data = explode(',', post('firma_base64'));
@@ -948,20 +947,36 @@
948947
$img->contrast((float) setting('Contrasto firma Wacom'));
949948
}
950949

951-
if (!$img->save(base_dir().'/files/interventi/'.$firma_file)) {
950+
// Salva temporaneamente il file per l'upload
951+
$temp_file = base_dir().'/files/interventi/firma_temp_'.time().'.jpg';
952+
if (!$img->save($temp_file)) {
952953
flash()->error(tr('Impossibile creare il file.'));
953-
} elseif ($dbo->query('UPDATE in_interventi SET firma_file='.prepare($firma_file).', firma_data=NOW(), firma_nome = '.prepare($firma_nome).' WHERE id='.prepare($id_record))) {
954-
flash()->info(tr('Firma salvata correttamente.'));
954+
} else {
955+
// Upload del file in zz_files con key='signature_nome_data'
956+
$data_firma = date('Y-m-d');
957+
$upload = Uploads::upload($temp_file, [
958+
'name' => 'Firma',
959+
'category' => 'Firme',
960+
'id_module' => $id_module,
961+
'id_record' => $id_record,
962+
'key' => 'signature_'.$firma_nome.'_'.$data_firma,
963+
]);
955964

956-
$id_stato = setting("Stato dell'attività dopo la firma");
957-
$stato = $dbo->selectOne('in_statiintervento', '*', ['id' => $id_stato]);
958-
$intervento = Intervento::find($id_record);
959-
if (!empty($stato)) {
965+
if (empty($upload)) {
966+
flash()->error(tr('Errore durante il caricamento della firma!'));
967+
} else {
968+
flash()->info(tr('Firma salvata correttamente.'));
969+
970+
$id_stato = setting("Stato dell'attività dopo la firma");
971+
$stato = $dbo->selectOne('in_statiintervento', '*', ['id' => $id_stato]);
960972
$intervento = Intervento::find($id_record);
961-
$intervento->idstatointervento = $stato['id'];
962-
$intervento->save();
973+
if (!empty($stato)) {
974+
$intervento = Intervento::find($id_record);
975+
$intervento->idstatointervento = $stato['id'];
976+
$intervento->save();
977+
}
963978
}
964-
979+
965980
// Notifica chiusura intervento
966981
if (!empty($stato['notifica'])) {
967982
$template = Template::find($stato['id_email']);
@@ -998,9 +1013,7 @@
9981013
}
9991014
}
10001015
}
1001-
} else {
1002-
flash()->error(tr('Errore durante il salvataggio della firma nel database.'));
1003-
}
1016+
}
10041017
} else {
10051018
flash()->error(tr('Errore durante il salvataggio della firma.').'<br>'.tr('La firma risulta vuota.'));
10061019
}
@@ -1021,7 +1034,6 @@
10211034
if (post('firma_base64') != '') {
10221035
foreach ($id_records as $id_record) {
10231036
// Salvataggio firma
1024-
$firma_file = 'firma_'.time().'.jpg';
10251037
$firma_nome = post('firma_nome');
10261038

10271039
$data = explode(',', post('firma_base64'));
@@ -1031,59 +1043,71 @@
10311043
$constraint->aspectRatio();
10321044
});
10331045

1034-
if (!$img->save(base_dir().'/files/interventi/'.$firma_file)) {
1046+
// Salva temporaneamente il file per l'upload
1047+
$temp_file = base_dir().'/files/interventi/firma_temp_'.time().'.jpg';
1048+
if (!$img->save($temp_file)) {
10351049
flash()->error(tr('Impossibile creare il file!'));
1036-
} elseif ($dbo->query('UPDATE in_interventi SET firma_file='.prepare($firma_file).', firma_data=NOW(), firma_nome = '.prepare($firma_nome).' WHERE id='.prepare($id_record))) {
1037-
++$firmati;
1038-
1039-
$id_stato = setting("Stato dell'attività dopo la firma");
1040-
$stato = $dbo->selectOne('in_statiintervento', '*', ['id' => $id_stato]);
1041-
$intervento = Intervento::find($id_record);
1042-
if (!empty($stato)) {
1050+
} else {
1051+
// Upload del file in zz_files con key='signature_nome_data'
1052+
$data_firma = date('Y-m-d');
1053+
$upload = Uploads::upload($temp_file, [
1054+
'name' => 'Firma',
1055+
'category' => 'Firme',
1056+
'id_module' => $id_module,
1057+
'id_record' => $id_record,
1058+
'key' => 'signature_'.$firma_nome.'_'.$data_firma,
1059+
]);
1060+
1061+
if (!empty($upload)) {
1062+
++$firmati;
1063+
1064+
$id_stato = setting("Stato dell'attività dopo la firma");
1065+
$stato = $dbo->selectOne('in_statiintervento', '*', ['id' => $id_stato]);
10431066
$intervento = Intervento::find($id_record);
1044-
$intervento->idstatointervento = $stato['id'];
1045-
$intervento->save();
1046-
}
1047-
1048-
// Notifica chiusura intervento
1049-
if (!empty($stato['notifica'])) {
1050-
$template = Template::find($stato['id_email']);
1051-
1052-
if (!empty($stato['destinatari'])) {
1053-
$mail = Mail::build(auth()->getUser(), $template, $id_record);
1054-
$mail->addReceiver($stato['destinatari']);
1055-
$mail->save();
1067+
if (!empty($stato)) {
1068+
$intervento = Intervento::find($id_record);
1069+
$intervento->idstatointervento = $stato['id'];
1070+
$intervento->save();
10561071
}
10571072

1058-
if (!empty($stato['notifica_cliente'])) {
1059-
if (!empty($intervento->anagrafica->email)) {
1073+
// Notifica chiusura intervento
1074+
if (!empty($stato['notifica'])) {
1075+
$template = Template::find($stato['id_email']);
1076+
1077+
if (!empty($stato['destinatari'])) {
10601078
$mail = Mail::build(auth()->getUser(), $template, $id_record);
1061-
$mail->addReceiver($intervento->anagrafica->email);
1079+
$mail->addReceiver($stato['destinatari']);
10621080
$mail->save();
10631081
}
1064-
}
1065-
1066-
if (!empty($stato['notifica_tecnici'])) {
1067-
$tecnici_intervento = $dbo->select('in_interventi_tecnici', 'idtecnico', [], ['idintervento' => $id_record]);
1068-
$tecnici_assegnati = $dbo->select('in_interventi_tecnici_assegnati', 'id_tecnico AS idtecnico', [], ['id_intervento' => $id_record]);
1069-
$tecnici = array_unique(array_merge($tecnici_intervento, $tecnici_assegnati), SORT_REGULAR);
10701082

1071-
foreach ($tecnici as $tecnico) {
1072-
$mail_tecnico = $dbo->selectOne('an_anagrafiche', '*', ['idanagrafica' => $tecnico]);
1073-
if (!empty($mail_tecnico['email'])) {
1083+
if (!empty($stato['notifica_cliente'])) {
1084+
if (!empty($intervento->anagrafica->email)) {
10741085
$mail = Mail::build(auth()->getUser(), $template, $id_record);
1075-
$mail->addReceiver($mail_tecnico['email']);
1086+
$mail->addReceiver($intervento->anagrafica->email);
10761087
$mail->save();
10771088
}
10781089
}
1090+
1091+
if (!empty($stato['notifica_tecnici'])) {
1092+
$tecnici_intervento = $dbo->select('in_interventi_tecnici', 'idtecnico', [], ['idintervento' => $id_record]);
1093+
$tecnici_assegnati = $dbo->select('in_interventi_tecnici_assegnati', 'id_tecnico AS idtecnico', [], ['id_intervento' => $id_record]);
1094+
$tecnici = array_unique(array_merge($tecnici_intervento, $tecnici_assegnati), SORT_REGULAR);
1095+
1096+
foreach ($tecnici as $tecnico) {
1097+
$mail_tecnico = $dbo->selectOne('an_anagrafiche', '*', ['idanagrafica' => $tecnico]);
1098+
if (!empty($mail_tecnico['email'])) {
1099+
$mail = Mail::build(auth()->getUser(), $template, $id_record);
1100+
$mail->addReceiver($mail_tecnico['email']);
1101+
$mail->save();
1102+
}
1103+
}
1104+
}
10791105
}
1106+
} else {
1107+
++$non_firmati;
10801108
}
1081-
} else {
1082-
++$non_firmati;
10831109
}
10841110
}
1085-
} else {
1086-
flash()->error(tr('Errore durante il salvataggio della firma.').'<br>'.tr('La firma risulta vuota'));
10871111
}
10881112
} else {
10891113
flash()->error(tr("Non è stato possibile creare la cartella _DIRECTORY_ per salvare l'immagine della firma.", [

modules/interventi/buttons.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@
2222

2323
use Modules\Interventi\Stato;
2424

25-
if (empty($record['firma_file'])) {
25+
if (empty($record->signature)) {
2626
$frase = tr('Anteprima e firma');
2727
$info_firma = '';
2828
} else {
2929
$frase = tr('Nuova anteprima e firma');
30-
$info_firma = ''.tr('Firmato il _DATE_ alle _TIME_ da _PERSON_', [
31-
'_DATE_' => Translator::dateToLocale($record['firma_data']),
32-
'_TIME_' => Translator::timeToLocale($record['firma_data']),
33-
'_PERSON_' => '<b>'.$record['firma_nome'].'</b>',
30+
$info_firma = ''.tr('Firmato il _DATE_ da _PERSON_', [
31+
'_DATE_' => Translator::dateToLocale($record->signature_date),
32+
'_PERSON_' => '<b>'.$record->signature_name.'</b>',
3433
]).'';
3534
}
3635

modules/interventi/edit.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,16 +470,15 @@
470470
<div class="text-center row">
471471
<div class="col-md-12" >
472472
<?php
473-
if ($record['firma_file'] == '') {
473+
if (empty($intervento->signature)) {
474474
echo '
475475
<div class="alert alert-warning"><i class="fa fa-warning"></i> '.tr('Questo intervento non è ancora stato firmato dal cliente').'.</div>';
476476
} else {
477477
echo '
478-
<img src="'.base_path().'/files/interventi/'.$record['firma_file'].'" class="img-thumbnail"><div>&nbsp;</div>
479-
<div class="col-md-6 offset-md-3 alert alert-success"><i class="fa fa-check"></i> '.tr('Firmato il _DATE_ alle _TIME_ da _PERSON_', [
480-
'_DATE_' => Translator::dateToLocale($record['firma_data']),
481-
'_TIME_' => Translator::timeToLocale($record['firma_data']),
482-
'_PERSON_' => (!empty($record['firma_nome']) ? $record['firma_nome'] : $intervento->anagrafica->ragione_sociale),
478+
<img src="'.$intervento->signature.'" class="img-thumbnail"><div>&nbsp;</div>
479+
<div class="col-md-6 offset-md-3 alert alert-success"><i class="fa fa-check"></i> '.tr('Firmato il _DATE_ da _PERSON_', [
480+
'_DATE_' => Translator::dateToLocale($intervento->signature_date),
481+
'_PERSON_' => (!empty($intervento->signature_name) ? $intervento->signature_name : $intervento->anagrafica->ragione_sociale),
483482
]).'</div>';
484483
}
485484

modules/interventi/src/Intervento.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,66 @@ public static function getTranslatedFields()
282282
{
283283
return self::$translated_fields;
284284
}
285+
286+
public function getSignatureUploadAttribute()
287+
{
288+
$module = $this->getModule();
289+
$uploads = $module->files($this->id, true);
290+
291+
// Cerca il primo file con key che inizia con 'signature_'
292+
foreach ($uploads as $upload) {
293+
if (strpos($upload->key, 'signature_') === 0) {
294+
return $upload;
295+
}
296+
}
297+
298+
return null;
299+
}
300+
301+
public function getSignatureAttribute()
302+
{
303+
$upload = $this->signature_upload;
304+
305+
if (empty($upload)) {
306+
return null;
307+
}
308+
309+
$directory = '/'.$this->getModule()->upload_directory.'/';
310+
$image = $directory.$upload->filename;
311+
312+
return base_path().$image;
313+
}
314+
315+
public function getSignatureNameAttribute()
316+
{
317+
$upload = $this->signature_upload;
318+
319+
if (empty($upload)) {
320+
return null;
321+
}
322+
323+
// Estrae il nome dalla chiave 'signature_nome_data'
324+
// Rimuove il prefisso 'signature_' e la data finale (ultimi 11 caratteri: _YYYY-MM-DD)
325+
$key = $upload->key;
326+
$key = substr($key, strlen('signature_'));
327+
$key = substr($key, 0, -11); // Rimuove '_YYYY-MM-DD'
328+
329+
return $key;
330+
}
331+
332+
public function getSignatureDateAttribute()
333+
{
334+
$upload = $this->signature_upload;
335+
336+
if (empty($upload)) {
337+
return null;
338+
}
339+
340+
// Estrae la data dalla chiave 'signature_nome_data'
341+
// La data è negli ultimi 10 caratteri (YYYY-MM-DD)
342+
$key = $upload->key;
343+
$data_firma = substr($key, -10);
344+
345+
return $data_firma;
346+
}
285347
}

templates/interventi/body.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
$sessioni = $documento->sessioni->sortBy('orario_inizio');
3232

33-
$firma = !empty($documento['firma_file']) ? '<img src="'.base_dir().'/files/interventi/'.$documento['firma_file'].'" style="width:70mm;">' : '';
33+
$firma = !empty($documento->signature) ? '<img src="'.$documento->signature.'" style="width:70mm;">' : '';
3434

3535
echo '
3636
<br>
@@ -422,10 +422,10 @@
422422
<td rowspan="'.(count($sessioni) + 1).'" class="text-center" style="font-size:6pt; vertical-align:bottom; border-left:1px solid #aaa;">
423423
'.$firma.'<br>';
424424

425-
if (empty($documento['firma_file'])) {
425+
if (empty($documento->signature)) {
426426
echo ' <i>('.tr('Timbro e firma leggibile').')</i>';
427427
} else {
428-
echo ' <i>'.$documento['firma_nome'].'</i>';
428+
echo ' <i>'.$documento->signature_name.'</i>';
429429
}
430430

431431
echo '
@@ -476,10 +476,10 @@
476476
echo '<td rowspan="2" class="text-center" style="font-size:6pt; vertical-align:bottom; border-left:1px solid #aaa;">
477477
'.$firma.'<br>';
478478

479-
if (empty($documento['firma_file'])) {
479+
if (empty($documento->signature)) {
480480
echo ' <br><br><br><i>('.tr('Timbro e firma leggibile').')</i>';
481481
} else {
482-
echo ' <i>'.$documento['firma_nome'].'</i>';
482+
echo ' <i>'.$documento->signature_name.'</i>';
483483
}
484484

485485
echo '
@@ -523,10 +523,10 @@
523523
echo '<td class="text-center" style="font-size:6pt; vertical-align:bottom; border-left:1px solid #aaa;">
524524
'.$firma.'<br>';
525525

526-
if (empty($documento['firma_file'])) {
526+
if (empty($documento->signature)) {
527527
echo ' <br><br><br><i>('.tr('Timbro e firma leggibile').')</i>';
528528
} else {
529-
echo ' <i>'.$documento['firma_nome'].'</i>';
529+
echo ' <i>'.$documento->signature_name.'</i>';
530530
}
531531

532532
echo '

update/2_9_6.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,36 @@
4343

4444
$database->query('ALTER TABLE `mg_articoli` DROP COLUMN `immagine`');
4545
}
46+
47+
// Migrazione firme da in_interventi a zz_files
48+
$id_module = Module::where('name', 'Interventi')->first()->id;
49+
50+
if (!empty($id_module)) {
51+
$interventi = $database->fetchArray('SELECT `id`, `firma_file`, `firma_nome`, `firma_data` FROM `in_interventi` WHERE `firma_file` IS NOT NULL');
52+
53+
foreach ($interventi as $intervento) {
54+
$data_firma = !empty($intervento['firma_data']) ? date('Y-m-d', strtotime($intervento['firma_data'])) : date('Y-m-d');
55+
$key = 'signature_'.$intervento['firma_nome'].'_'.$data_firma;
56+
$file_exists = $database->selectOne('zz_files', ['id'], [
57+
'id_module' => $id_module,
58+
'id_record' => $intervento['id'],
59+
'key' => $key,
60+
]);
61+
62+
if (empty($file_exists)) {
63+
$database->insert('zz_files', [
64+
'id_module' => $id_module,
65+
'id_record' => $intervento['id'],
66+
'nome' => 'Firma',
67+
'filename' => $intervento['firma_file'],
68+
'original' => $intervento['firma_file'],
69+
'key' => $key,
70+
]);
71+
}
72+
}
73+
74+
$database->query('ALTER TABLE `in_interventi` DROP COLUMN `firma_file`');
75+
$database->query('ALTER TABLE `in_interventi` DROP COLUMN `firma_data`');
76+
$database->query('ALTER TABLE `in_interventi` DROP COLUMN `firma_nome`');
77+
}
78+

0 commit comments

Comments
 (0)