openstamanager/modules/impianti/src/Import/CSV.php

218 lines
7.8 KiB
PHP
Raw Normal View History

2023-11-08 10:31:52 +01:00
<?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\Anagrafiche\Anagrafica;
use Modules\Anagrafiche\Sede;
use Modules\Impianti\Categoria;
2023-11-08 11:08:47 +01:00
use Modules\Impianti\Impianto;
2024-03-05 16:01:45 +01:00
use Models\Module;
2023-11-08 10:31:52 +01:00
/**
* 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',
],
2023-11-08 10:58:11 +01:00
[
'field' => 'id_sottocategoria',
'label' => 'Sottocategoria',
],
2023-11-08 10:31:52 +01:00
[
'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']);
2023-11-08 11:08:47 +01:00
2023-11-08 10:31:52 +01:00
// Gestione categoria e sottocategoria
$categoria = null;
2023-11-08 10:58:11 +01:00
$sottocategoria = null;
if (empty($record['id_categoria'])) {
$record['id_categoria'] = 'Nessuna';
}
// Categoria
$categoria = Categoria::where('nome', strtolower($record['id_categoria']))->first();
if (empty($categoria)) {
$categoria = Categoria::build($record['id_categoria']);
}
// Sotto-categoria
if (!empty($record['id_sottocategoria'])) {
$sottocategoria = Categoria::where('nome', $record['id_sottocategoria'])
->where('parent', $categoria->id)
->first();
if (empty($sottocategoria)) {
$sottocategoria = Categoria::build($record['id_sottocategoria']);
$sottocategoria->parent()->associate($categoria);
$sottocategoria->save();
2023-11-08 10:31:52 +01:00
}
}
2023-11-08 11:08:47 +01:00
2023-11-08 10:31:52 +01:00
// Individuazione impianto e generazione
$impianto = null;
2023-11-08 10:58:11 +01:00
2023-11-08 10:31:52 +01:00
// 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']);
2023-11-08 11:08:47 +01:00
}
2023-11-08 10:31:52 +01:00
if (!empty($record['data'])) {
$impianto->data = $record['data'];
$impianto->save();
}
2023-11-08 11:08:47 +01:00
2023-11-08 10:58:11 +01:00
$impianto->id_sottocategoria = $sottocategoria['id'];
2023-11-08 10:31:52 +01:00
$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;
2023-11-08 11:08:47 +01:00
$impianto->save();
2023-11-08 10:31:52 +01:00
}
2023-11-08 11:08:47 +01:00
2024-01-15 15:30:45 +01:00
// Gestione immagine
2023-11-08 10:31:52 +01:00
if (!empty($url) && !empty($record['import_immagine'])) {
$file_content = file_get_contents($url);
2023-11-08 11:08:47 +01:00
2023-11-08 10:31:52 +01:00
if (!empty($file_content)) {
if ($record['import_immagine'] == 2 || $record['import_immagine'] == 4) {
2024-01-15 15:30:45 +01:00
\Uploads::deleteLinked([
2024-03-05 16:01:45 +01:00
'id_module' => Module::find('Impianti')->id,
2023-11-08 10:31:52 +01:00
'id_record' => $impianto->id,
]);
2023-11-08 11:08:47 +01:00
2023-11-29 10:15:59 +01:00
$database->update('my_impianti', [
2023-11-08 10:31:52 +01:00
'immagine' => '',
], [
'id' => $impianto->id,
]);
}
2023-11-08 11:08:47 +01:00
2023-11-08 10:31:52 +01:00
$name = 'immagine_'.$impianto->id.'.'.Upload::getExtensionFromMimeType($file_content);
2023-11-08 11:08:47 +01:00
2024-01-15 15:30:45 +01:00
$upload = \Uploads::upload($file_content, [
2023-11-08 10:31:52 +01:00
'name' => 'Immagine',
'category' => 'Immagini',
'original_name' => $name,
2024-03-05 16:01:45 +01:00
'id_module' => Module::find('Impianti')->id,
2023-11-08 10:31:52 +01:00
'id_record' => $impianto->id,
], [
'thumbnails' => true,
]);
$filename = $upload->filename;
2023-11-08 11:08:47 +01:00
2023-11-08 10:31:52 +01:00
if ($record['import_immagine'] == 1 || $record['import_immagine'] == 2) {
if (!empty($filename)) {
2023-11-29 10:15:59 +01:00
$database->update('my_impianti', [
2023-11-08 10:31:52 +01:00
'immagine' => $filename,
], [
'id' => $impianto->id,
]);
}
}
}
}
2023-11-08 11:08:47 +01:00
2023-11-08 10:31:52 +01:00
unset($record['import_immagine']);
}
}
public static function getExample()
{
return [
2023-11-29 10:15:59 +01:00
['Matricola', 'Immagine', 'Import immagine', 'Nome', 'Cliente', 'Telefono', 'Categoria', 'Sottocategoria', 'Sede', 'Descrizione', 'Data installazione'],
2024-02-06 11:36:09 +01:00
['00001', 'https://openstamanager.com/moduli/budget/budget.webp', '2', 'Lavatrice', 'Mario Rossi', '+39 0429 60 25 12', 'Elettrodomestici', 'Marca1', '', '', '2023-01-01'],
['00002', 'https://openstamanager.com/moduli/3cx/3cx.webp', '2', 'Caldaia', 'Mario Rossi', '+39 0429 60 25 12', 'Elettrodomestici', 'Marca2', '', '', '2023-03-06'],
['00003', 'https://openstamanager.com/moduli/disponibilita-tecnici/tecnici.webp', '2', 'Forno', 'Mario Rossi', '+39 0429 60 25 12', 'Elettrodomestici', 'Marca3', '', '', '2023-04-01'],
['00004', 'https://openstamanager.com/moduli/distinta-base/distinta.webp', '2', 'Lavastoviglie', 'Mario Rossi', '+39 0429 60 25 12', 'Elettrodomestici', 'Marca4', '', '', '2023-08-06'],
2023-11-08 10:31:52 +01:00
];
}
2023-11-08 11:08:47 +01:00
}