From 5bfa7e71f4f51ac40e6ccd55103a80add1d33722 Mon Sep 17 00:00:00 2001 From: Pek5892 Date: Wed, 8 Nov 2023 10:31:52 +0100 Subject: [PATCH 01/14] =?UTF-8?q?Aggiunti=20import=20Attivit=C3=A0=20e=20I?= =?UTF-8?q?mpianti?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/impianti/src/Categoria.php | 50 ++++++ modules/impianti/src/Impianto.php | 22 ++- modules/impianti/src/Import/CSV.php | 215 ++++++++++++++++++++++++++ modules/interventi/src/Import/CSV.php | 184 ++++++++++++++++++++++ update/2_4_51.sql | 8 +- 5 files changed, 477 insertions(+), 2 deletions(-) create mode 100644 modules/impianti/src/Categoria.php create mode 100644 modules/impianti/src/Import/CSV.php create mode 100644 modules/interventi/src/Import/CSV.php diff --git a/modules/impianti/src/Categoria.php b/modules/impianti/src/Categoria.php new file mode 100644 index 000000000..b53ed0488 --- /dev/null +++ b/modules/impianti/src/Categoria.php @@ -0,0 +1,50 @@ +. + */ + + +namespace Modules\Impianti; + +use Common\SimpleModelTrait; +use Illuminate\Database\Eloquent\Model; +use Traits\HierarchyTrait; + + +class Categoria extends Model +{ + use SimpleModelTrait; + use HierarchyTrait; + + protected $table = 'my_impianti_categorie'; + protected static $parent_identifier = 'parent'; + + public static function build($nome) + { + $model = new static(); + + $model->nome = $nome; + $model->save(); + + return $model; + } + + public function impianti() + { + return $this->hasMany(Impianto::class, 'id_categoria'); + } +} diff --git a/modules/impianti/src/Impianto.php b/modules/impianti/src/Impianto.php index ceaa625b3..215271b58 100644 --- a/modules/impianti/src/Impianto.php +++ b/modules/impianti/src/Impianto.php @@ -22,6 +22,7 @@ namespace Modules\Impianti; use Common\SimpleModelTrait; use Illuminate\Database\Eloquent\Model; use Modules\Anagrafiche\Anagrafica; +use Modules\Impianti\Categoria; class Impianto extends Model { @@ -34,4 +35,23 @@ class Impianto extends Model { return $this->belongsTo(Anagrafica::class, 'idanagrafica'); } -} + public static function build($matricola, $nome, Categoria $categoria, $anagrafica) + { + $model = new static(); + + $model->matricola = $matricola; + $model->nome = $nome; + + $model->categoria()->associate($categoria); + + $model->save(); + + return $model; + } + + public function categoria() + { + return $this->belongsTo(Categoria::class, 'id_categoria'); + } + +} \ No newline at end of file diff --git a/modules/impianti/src/Import/CSV.php b/modules/impianti/src/Import/CSV.php new file mode 100644 index 000000000..e2a6a259f --- /dev/null +++ b/modules/impianti/src/Import/CSV.php @@ -0,0 +1,215 @@ +. + */ + +namespace Modules\Impianti\Import; + +use Importer\CSVImporter; +use Models\Upload; +use Modules; +use Modules\Anagrafiche\Anagrafica; +use Modules\Anagrafiche\Sede; +use Modules\Anagrafiche\Tipo; +use Modules\Impianti\Impianto; +use Modules\Impianti\Categoria; +use Uploads; + +/** + * Struttura per la gestione delle operazioni di importazione (da CSV) degli Impianti. + * + * @since 2.4.52 + */ +class CSV extends CSVImporter +{ + public function getAvailableFields() + { + return [ + [ + 'field' => 'matricola', + 'label' => 'Matricola', + 'primary_key' => true, + ], + [ + 'field' => 'immagine', + 'label' => 'Immagine', + 'names' => [ + 'Immagine', + 'Foto', + ], + ], + [ + 'field' => 'import_immagine', + 'label' => 'Import immagine', + ], + [ + 'field' => 'nome', + 'label' => 'Nome', + ], + [ + 'field' => 'cliente', + 'label' => 'Cliente', + ], + [ + 'field' => 'telefono', + 'label' => 'Telefono', + ], + [ + 'field' => 'id_categoria', + 'label' => 'Categoria', + ], + [ + 'field' => 'sede', + 'label' => 'Sede', + ], + [ + 'field' => 'descrizione', + 'label' => 'Descrizione', + ], + [ + 'field' => 'data', + 'label' => 'Data installazione', + ], + ]; + } + + public function import($record) + { + $database = database(); + $primary_key = $this->getPrimaryKey(); + + if (!empty($record['telefono'])) { + $anagrafica = Anagrafica::where('telefono', $record['telefono'])->first(); + } + + if (!empty($anagrafica)) { + $url = $record['immagine']; + unset($record['immagine']); + + // Gestione categoria e sottocategoria + $categoria = null; + + if (!empty($record['id_categoria'])) { + // Categoria + $categoria = Categoria::where('nome', strtolower($record['id_categoria']))->first(); + + if (empty($categoria)) { + $categoria = Categoria::build($record['id_categoria']); + } + } + + // Individuazione impianto e generazione + $impianto = null; + // Ricerca sulla base della chiave primaria se presente + if (!empty($primary_key)) { + $impianto = Impianto::where($primary_key, $record[$primary_key])->first(); + } + if (empty($impianto)) { + $impianto = Impianto::build($record['matricola'], $record['nome'], $categoria, $record['cliente']); + } + + if (!empty($record['data'])) { + $impianto->data = $record['data']; + $impianto->save(); + } + + + $tipo = Tipo::where('descrizione', 'Cliente')->first(); + $tipi = $anagrafica->tipi->pluck('idtipoanagrafica')->toArray(); + + $tipi[] = $tipo->id; + + $anagrafica->tipologie = $tipi; + $anagrafica->save(); + + $impianto->idanagrafica = $anagrafica->idanagrafica; + $impianto->save(); + + if (!empty($record['sede'])) { + $sede = Sede::where('nomesede', $record['sede']) + ->where('idanagrafica', $anagrafica->idanagrafica) + ->first(); + $impianto->idsede = $sede->id; + $impianto->save(); + + } + + + //Gestione immagine + if (!empty($url) && !empty($record['import_immagine'])) { + $file_content = file_get_contents($url); + + if (!empty($file_content)) { + if ($record['import_immagine'] == 2 || $record['import_immagine'] == 4) { + Uploads::deleteLinked([ + 'id_module' => Modules::get('Impianti')['id'], + 'id_record' => $impianto->id, + ]); + + $database->update('mg_articoli', [ + 'immagine' => '', + ], [ + 'id' => $impianto->id, + ]); + } + + $name = 'immagine_'.$impianto->id.'.'.Upload::getExtensionFromMimeType($file_content); + + $upload = Uploads::upload($file_content, [ + 'name' => 'Immagine', + 'category' => 'Immagini', + 'original_name' => $name, + 'id_module' => Modules::get('Impianti')['id'], + 'id_record' => $impianto->id, + ], [ + 'thumbnails' => true, + ]); + $filename = $upload->filename; + + if ($record['import_immagine'] == 1 || $record['import_immagine'] == 2) { + if (!empty($filename)) { + $database->update('mg_articoli', [ + 'immagine' => $filename, + ], [ + 'id' => $impianto->id, + ]); + } + } + } + } + + unset($record['import_immagine']); + } + + } + + public static function getExample() + { + return [ + ['Matricola', 'Nome', 'Categoria', 'Immagine', 'Data installazione', 'Cliente', 'Telefono', 'Sede'], + ['00001', 'Marca', 'Lavatrice','https://immagini.com/immagine.jpg', '01/10/2023', 'Mario Rossi', '04444444', 'Sede2'], + ['00002', 'Marca2', 'Lavastoviglie', 'https://immagini.com/immagine2.jpg', '12/09/2023', 'Mario Rossi', '04444444', 'Sede2'], + ['00003', 'Marca3', 'Frigorifero','https://immagini.com/immagine3.jpg', '20/09/2023', 'Mario Rossi', '04444444', 'Sede2'], + ['00004', 'Marca4', 'Caldaia', 'https://immagini.com/immagine4.jpg', '06/11/2023', 'Mario Rossi', '04444444', 'Sede2'], + [], + ['Import immagine = 1 -> Permette di importare l\'immagine come principale dell\'impianto mantenendo gli altri allegati già presenti'], + ['Import immagine = 2 -> Permette di importare l\'immagine come principale dell\'impianto rimuovendo tutti gli allegati presenti'], + ['Import immagine = 3 -> Permette di importare l\'immagine come allegato dell\'impianto mantenendo gli altri allegati già presenti'], + ['Import immagine = 4 -> Permette di importare l\'immagine come allegato dell\'impianto rimuovendo tutti gli allegati presenti'], + ]; + } +} \ No newline at end of file diff --git a/modules/interventi/src/Import/CSV.php b/modules/interventi/src/Import/CSV.php new file mode 100644 index 000000000..23d02aef2 --- /dev/null +++ b/modules/interventi/src/Import/CSV.php @@ -0,0 +1,184 @@ +. + */ + +namespace Modules\Interventi\Import; + +use Importer\CSVImporter; +use Modules\Anagrafiche\Anagrafica; +use Modules\Impianti\Impianto; +use Modules\Interventi\Intervento; +use Modules\Interventi\Stato; +use Modules\TipiIntervento\Tipo; +use Modules\Interventi\Components\Sessione; + +/** + * Struttura per la gestione delle operazioni di importazione (da CSV) degli Interventi. + * + * @since 2.4.52 + */ +class CSV extends CSVImporter +{ + public function getAvailableFields() + { + return [ + [ + 'field' => 'codice', + 'label' => 'Codice', + 'primary_key' => true, + ], + [ + 'field' => 'telefono', + 'label' => 'Telefono', + ], + [ + 'field' => 'data', + 'label' => 'Data', + ], + [ + 'field' => 'data_richiesta', + 'label' => 'Data richiesta', + ], + [ + 'field' => 'ora_inizio', + 'label' => 'Ora', + ], + [ + 'field' => 'tecnico', + 'label' => 'Tecnico', + ], + [ + 'field' => 'tipo', + 'label' => 'Tipo', + ], + [ + 'field' => 'note', + 'label' => 'Note', + ], + [ + 'field' => 'impianto', + 'label' => 'Impianto', + ], + [ + 'field' => 'richiesta', + 'label' => 'Richiesta', + ], + [ + 'field' => 'descrizione', + 'label' => 'Descrizione', + ], + [ + 'field' => 'stato', + 'label' => 'Stato', + ], + ]; + } + + public function import($record) + { + $database = database(); + $primary_key = $this->getPrimaryKey(); + + if (!empty($record['telefono'])) { + $anagrafica = Anagrafica::where('telefono', $record['telefono'])->first(); + } + + if (!empty($record['impianto'])){ + $impianto = Impianto::where('matricola', $record['impianto'])->first(); + } + + if (!empty($anagrafica) && !empty($impianto)) { + $intervento = null; + + // Ricerca sulla base della chiave primaria se presente + if (!empty($primary_key)) { + $intervento = Intervento::where($primary_key, $record[$primary_key])->first(); + } + + // Verifico tipo e stato per creare l'intervento + if (empty($record['tipo'])) { + $tipo = Tipo::where('codice', 'GEN')->first(); + } else { + $tipo = Tipo::where('codice', $record['tipo'])->first(); + } + unset($record['tipo']); + + if (empty($record['stato'])) { + $stato = Stato::where('descrizione', 'Completato')->first(); + } else { + $stato = Stato::where('descrizione', $record['stato'])->first(); + } + unset($record['stato']); + + // Crea l'intervento + if (empty($intervento)) { + $intervento = Intervento::build($anagrafica, $tipo, $stato, $record['data_richiesta']); + } + unset($record['codice']); + unset($record['data']); + unset($record['ora_inizio']); + unset($record['telefono']); + + // Collega l'impianto all'intervento + $database->query('INSERT INTO my_impianti_interventi(idimpianto, idintervento) VALUES('.prepare($impianto['id']).', '.prepare($intervento['id']).')'); + unset($record['impianto']); + + // Inserisce la data richiesta e la richiesta + $intervento->data_richiesta = $record['data_richiesta']; + $intervento->richiesta = $record['richiesta']; + + // Inserisce la descrizione se presente + if (!empty($record['descrizione'])) { + $intervento->descrizione = $record['descrizione']; + } + + // Inserisce le note se presenti + if (!empty($record['note'])) { + $intervento->informazioniaggiuntive = $record['note']; + } + + $intervento->save(); + + $inizio = date('Y-m-d H:i', strtotime($record['data'] . ' ' . $record['ora_inizio'])); + $fine= ''; + + // Verifica il tecnico e inserisce la sessione + $anagrafica_t = Anagrafica::where('ragione_sociale', $record['tecnico'])->first(); + $tipo = $database->fetchOne('SELECT idtipoanagrafica FROM an_tipianagrafiche_anagrafiche WHERE idanagrafica = '.prepare($anagrafica_t['idanagrafica'])); + $tecnico = TipoAnagrafica::where('descrizione','Tecnico')->first(); + + if ($tipo = $tecnico['idtipoanagrafica']){ + $anagrafica_t['tipo'] = $tecnico['descrizione']; + } + + if (!empty($record['data']) && !empty($record['ora_inizio']) && !empty($record['tecnico'])) { + $sessione = Sessione::build($intervento, $anagrafica_t, $inizio, $fine); + $sessione->save(); + } + } + } + + public static function getExample() + { + return [ + ['Codice', 'Telefono', 'Data', 'Data richiesta', 'Ora', 'Tecnico', 'Tipo', 'Note', 'Impianto', 'Richiesta', 'Descrizione', 'Stato'], + ['001', '044444444', '07/11/2023','03/11/2023', '18:30', 'Stefano Bianchi', '', '', '00000000001', 'Manutenzione ordinaria', 'eseguito intervento di manutenzione', 'Bozza'], + ['002', '044444444', '08/11/2023', '04/11/2023', '11:20', 'Stefano Bianchi', '', '', '00000000002', 'Manutenzione ordinaria', 'eseguito intervento di manutenzione', ''] + ]; + } +} \ No newline at end of file diff --git a/update/2_4_51.sql b/update/2_4_51.sql index bf4093369..a3cdb1ee0 100644 --- a/update/2_4_51.sql +++ b/update/2_4_51.sql @@ -71,4 +71,10 @@ INSERT INTO `zz_views` (`id_module`, `name`, `query`, `order`, `search`, `slow`, DELETE FROM `em_print_template` WHERE `em_print_template`.`id_print` IN (SELECT `id` FROM `zz_prints` WHERE `zz_prints`.`is_record` = 0); -- Il widget Notifiche interne è ora Note interne -UPDATE `zz_widgets` SET `text` = 'Note interne' WHERE `zz_widgets`.`name` = "Note interne"; \ No newline at end of file +UPDATE `zz_widgets` SET `text` = 'Note interne' WHERE `zz_widgets`.`name` = "Note interne"; + +-- Aggiunta importazione impianti +INSERT INTO `zz_imports` (`name`, `class`) VALUES ('Impianti', 'Modules\\Impianti\\Import\\CSV'); + +-- Aggiunta importazione attività +INSERT INTO `zz_imports` (`name`, `class`) VALUES ('Attività', 'Modules\\Interventi\\Import\\CSV'); \ No newline at end of file From dfcd04755eac6d46182445c73b0d4571d99230db Mon Sep 17 00:00:00 2001 From: Pek5892 Date: Wed, 8 Nov 2023 10:58:11 +0100 Subject: [PATCH 02/14] Aggiunte sottocategorie in impianti --- modules/categorie_impianti/actions.php | 82 +++++++++++++++++-------- modules/categorie_impianti/add.php | 31 +++++++++- modules/categorie_impianti/edit.php | 64 +++++++++++++++++-- modules/categorie_impianti/row-list.php | 38 ++++++++++++ modules/impianti/actions.php | 1 + modules/impianti/edit.php | 12 +++- modules/impianti/src/Import/CSV.php | 38 +++++++++--- update/2_4_51.sql | 6 +- 8 files changed, 227 insertions(+), 45 deletions(-) create mode 100644 modules/categorie_impianti/row-list.php diff --git a/modules/categorie_impianti/actions.php b/modules/categorie_impianti/actions.php index c7083efb3..1b11511f9 100755 --- a/modules/categorie_impianti/actions.php +++ b/modules/categorie_impianti/actions.php @@ -28,43 +28,75 @@ switch (filter('op')) { $nome = filter('nome'); $nota = filter('nota'); $colore = filter('colore'); + $id_original = filter('id_original') ?: null; if (isset($nome) && isset($nota) && isset($colore)) { - $dbo->query('UPDATE `my_impianti_categorie` SET `nome`='.prepare($nome).', `nota`='.prepare($nota).', `colore`='.prepare($colore).' WHERE `id`='.prepare($id_record)); + $database->table('my_impianti_categorie') + ->where('id', '=', $id_record) + ->update([ + 'nome' => $nome, + 'nota' => $nota, + 'colore' => $colore, + ]); + flash()->info(tr('Salvataggio completato!')); } else { flash()->error(tr('Ci sono stati alcuni errori durante il salvataggio!')); } + // Redirect alla categoria se si sta modificando una sottocategoria + if ($id_original != null) { + $database->commitTransaction(); + redirect(base_path().'/editor.php?id_module='.$id_module.'&id_record='.($id_original ?: $id_record)); + exit(); + } + break; case 'add': - $nome = post('nome'); - $nota = post('nota'); - $colore = post('colore'); + $nome = filter('nome'); + $nota = filter('nota'); + $colore = filter('colore'); - // Verifico che il nome non sia duplicato - $count = $dbo->fetchNum('SELECT `id` FROM `my_impianti_categorie` WHERE `nome`='.prepare($nome)); + $id_original = filter('id_original') ?: null; - if ($count != 0) { - flash()->error(tr('Categoria _NAME_ già esistente!', [ - '_NAME_' => $nome, + // Ricerca corrispondenze con stesso nome + $corrispondenze = $database->table('my_impianti_categorie') + ->where('nome', '=', $nome); + if (!empty($id_original)) { + $corrispondenze = $corrispondenze->where('parent', '=', $id_original); + } else { + $corrispondenze = $corrispondenze->whereNull('parent'); + } + $corrispondenze = $corrispondenze->get(); + + // Eventuale creazione del nuovo record + if ($corrispondenze->count() == 0) { + $id_record = $database->table('my_impianti_categorie') + ->insertGetId([ + 'nome' => $nome, + 'nota' => $nota, + 'colore' => $colore, + 'parent' => $id_original, + ]); + + flash()->info(tr('Aggiunta nuova tipologia di _TYPE_', [ + '_TYPE_' => 'categoria', ])); } else { - if (isset($nome)) { - $dbo->query('INSERT INTO `my_impianti_categorie` (`nome`, `colore`, `nota`) VALUES ('.prepare($nome).', '.prepare($colore).', '.prepare($nota).')'); - - $id_record = $dbo->lastInsertedID(); - - if (isAjaxRequest()) { - echo json_encode(['id' => $id_record, 'text' => $nome]); - } - - flash()->info(tr('Aggiunta nuova tipologia di _TYPE_', [ - '_TYPE_' => 'categoria', - ])); - } + $id_record = $corrispondenze->first()->id; + flash()->error(tr('Esiste già una categoria con lo stesso nome!')); } + + if (isAjaxRequest()) { + echo json_encode(['id' => $id_record, 'text' => $nome]); + } else { + // Redirect alla categoria se si sta aggiungendo una sottocategoria + $database->commitTransaction(); + redirect(base_path().'/editor.php?id_module='.$id_module.'&id_record='.($id_original ?: $id_record)); + exit(); + } + break; case 'delete': @@ -73,14 +105,14 @@ switch (filter('op')) { $id = $id_record; } - if ($dbo->fetchNum('SELECT * FROM `my_impianti` WHERE `id_categoria`='.prepare($id)) == 0) { + if ($dbo->fetchNum('SELECT * FROM `my_impianti` WHERE (`id_categoria`='.prepare($id).' ) AND `deleted_at` IS NULL') == 0) { $dbo->query('DELETE FROM `my_impianti_categorie` WHERE `id`='.prepare($id)); - flash()->info(tr('Tipologia di _TYPE_ eliminata con successo!', [ + flash()->info(tr('_TYPE_ eliminata con successo!', [ '_TYPE_' => 'categoria', ])); } else { - flash()->error(tr('Esistono ancora alcuni articoli sotto questa categoria!')); + flash()->error(tr('Esistono alcuni impianti collegati a questa categoria. Impossibile eliminarla.')); } break; diff --git a/modules/categorie_impianti/add.php b/modules/categorie_impianti/add.php index 5704f7d66..b922d281c 100755 --- a/modules/categorie_impianti/add.php +++ b/modules/categorie_impianti/add.php @@ -25,9 +25,18 @@ if (isset($id_record)) { include __DIR__.'/init.php'; } -?>
+?> - + +
@@ -48,14 +57,30 @@ if (isset($id_record)) {
+ + + +