Aggiunti import Attività e Impianti

This commit is contained in:
Pek5892 2023-11-08 10:31:52 +01:00
parent 0ccfaa0fff
commit 5bfa7e71f4
5 changed files with 477 additions and 2 deletions

View File

@ -0,0 +1,50 @@
<?php
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
* Copyright (C) DevCode s.r.l.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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');
}
}

View File

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

View File

@ -0,0 +1,215 @@
<?php
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
* Copyright (C) DevCode s.r.l.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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'],
];
}
}

View File

@ -0,0 +1,184 @@
<?php
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
* Copyright (C) DevCode s.r.l.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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', '']
];
}
}

View File

@ -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";
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');