2020-08-06 18:51:56 +02:00
< ? php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager : il software gestionale open source per l ' assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright ( C ) DevCode s . r . l .
2020-09-07 15:04:06 +02:00
*
* 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 />.
*/
2020-08-06 18:51:56 +02:00
namespace Modules\Anagrafiche\Import ;
2020-08-07 10:14:09 +02:00
use Importer\CSVImporter ;
2020-08-06 18:51:56 +02:00
use Modules\Anagrafiche\Anagrafica ;
2024-02-05 17:07:33 +01:00
use Modules\Anagrafiche\Nazione ;
2024-02-06 17:25:50 +01:00
use Modules\Anagrafiche\Tipo ;
2020-08-06 18:51:56 +02:00
2020-08-07 11:05:08 +02:00
/**
* Struttura per la gestione delle operazioni di importazione ( da CSV ) delle Anagrafiche .
*
* @ since 2.4 . 17
*/
2020-08-07 10:14:09 +02:00
class CSV extends CSVImporter
2020-08-06 18:51:56 +02:00
{
public function getAvailableFields ()
{
return [
[
'field' => 'codice' ,
'label' => 'Codice' ,
'primary_key' => true ,
'names' => [
2021-02-26 13:23:06 +01:00
'Codice' ,
2020-08-06 18:51:56 +02:00
'Codice interno' ,
'Numero' ,
],
],
[
'field' => 'ragione_sociale' ,
'label' => 'Ragione sociale' ,
'names' => [
'Denominazione' ,
'Ragione sociale' ,
],
],
2020-11-30 12:03:39 +01:00
[
'field' => 'nome' ,
'label' => 'Nome' ,
],
[
'field' => 'cognome' ,
'label' => 'Cognome' ,
],
2020-08-06 18:51:56 +02:00
[
'field' => 'codice_destinatario' ,
'label' => 'Codice destinatario' ,
'names' => [
'Codice destinatario' ,
'Codice SDI' ,
'Codice univoco' ,
'Codice univoco ufficio' ,
'SDI' ,
],
],
[
'field' => 'provincia' ,
'label' => 'Provincia' ,
],
[
'field' => 'citta' ,
'label' => 'Città' ,
'names' => [
'Citt_' ,
'Città' ,
'Città' ,
'Citta' ,
],
],
[
'field' => 'telefono' ,
'label' => 'Telefono' ,
],
[
'field' => 'indirizzo' ,
'label' => 'Indirizzo' ,
],
[
'field' => 'cap' ,
'label' => 'CAP' ,
],
[
'field' => 'cellulare' ,
'label' => 'Cellulare' ,
],
[
'field' => 'fax' ,
'label' => 'Fax' ,
],
[
'field' => 'email' ,
'label' => 'Email' ,
'names' => [
'E-mail' ,
'Indirizzo email' ,
'Mail' ,
'Email' ,
],
],
[
'field' => 'pec' ,
'label' => 'PEC' ,
'names' => [
'E-mail PEC' ,
'Email certificata' ,
'Indirizzo email certificata' ,
'PEC' ,
],
],
2021-12-10 16:58:58 +01:00
[
'field' => 'sitoweb' ,
'label' => 'Sito Web' ,
'names' => [
2023-11-16 11:08:15 +01:00
'Sito web' ,
2021-12-10 16:58:58 +01:00
'Website' ,
2023-11-16 11:08:15 +01:00
'Sito' ,
2021-12-10 16:58:58 +01:00
],
],
2020-08-06 18:51:56 +02:00
[
'field' => 'codice_fiscale' ,
'label' => 'Codice Fiscale' ,
],
[
'field' => 'data_nascita' ,
'label' => 'Data di nascita' ,
],
[
'field' => 'luogo_nascita' ,
'label' => 'Luogo di nascita' ,
],
[
'field' => 'sesso' ,
'label' => 'Sesso' ,
],
[
'field' => 'piva' ,
'label' => 'Partita IVA' ,
'names' => [
'P.IVA' ,
'P.IVA/TAX ID' ,
'TAX ID' ,
'Partita IVA' ,
],
],
[
'field' => 'codiceiban' ,
'label' => 'IBAN' ,
],
[
'field' => 'note' ,
'label' => 'Note' ,
'names' => [
'Note Extra' ,
'Note' ,
],
],
[
'field' => 'id_nazione' ,
'label' => 'Nazione' ,
'names' => [
'Nazione' ,
'Paese' ,
'id_nazione' ,
'idnazione' ,
'nazione' ,
],
],
[
'field' => 'idagente' ,
'label' => 'ID Agente' ,
],
[
'field' => 'idpagamento_vendite' ,
'label' => 'ID Pagamento' ,
'names' => [
'Pagamento' ,
'ID Pagamento' ,
'id_pagamento' ,
'idpagamento_vendite' ,
'idpagamento' ,
],
],
[
2024-02-06 11:36:09 +01:00
'field' => 'tipologia' ,
'label' => 'Tipo di anagrafica (Cliente, Fornitore)' ,
2020-08-06 18:51:56 +02:00
'names' => [
'Tipo' ,
'tipo' ,
'idtipo' ,
],
],
[
'field' => 'tipo' ,
2023-09-29 11:23:56 +02:00
'label' => 'Tipologia (Privato, Ente pubblico, Azienda)' ,
2022-03-07 14:52:39 +01:00
'names' => [
2023-11-16 11:08:15 +01:00
'Tipologia' ,
2022-03-07 14:52:39 +01:00
],
],
[
'field' => 'split_payment' ,
'label' => 'Split Payment' ,
'names' => [
'Split Payment' ,
'split payment' ,
'split_payment' ,
],
],
2023-09-29 11:23:56 +02:00
[
'field' => 'id_settore' ,
'label' => 'Settore merceologico' ,
'names' => [
'settore' ,
'settore merceologico' ,
],
],
2020-08-06 18:51:56 +02:00
];
}
public function import ( $record )
{
$database = database ();
$primary_key = $this -> getPrimaryKey ();
$id_azienda = setting ( 'Azienda predefinita' );
2023-11-16 11:08:15 +01:00
// Compilo la ragione sociale se sono valorizzati cognome e nome
if ( ! $record [ 'ragione_sociale' ] && ( $record [ 'cognome' ] && $record [ 'nome' ])) {
2023-11-17 17:08:32 +01:00
$record [ 'ragione_sociale' ] = $record [ 'cognome' ] . ' ' . $record [ 'nome' ];
2023-11-16 11:08:15 +01:00
}
unset ( $record [ 'cognome' ]);
unset ( $record [ 'nome' ]);
2020-08-06 18:51:56 +02:00
// Individuazione del tipo dell'anagrafica
$tipologie = [];
2024-02-06 11:36:09 +01:00
if ( ! empty ( $record [ 'tipologia' ])) {
$tipi_selezionati = explode ( ',' , $record [ 'tipologia' ]);
2020-08-06 18:51:56 +02:00
foreach ( $tipi_selezionati as $tipo ) {
2024-03-20 11:13:28 +01:00
$id_tipo = ( new Tipo ()) -> getByField ( 'name' , $tipo );
2020-08-06 18:51:56 +02:00
2023-09-29 12:52:36 +02:00
// Creo il tipo anagrafica se non esiste
2024-02-06 11:36:09 +01:00
if ( empty ( $id_tipo )) {
$id_tipo = database () -> query ( 'INSERT INTO `an_tipianagrafiche` (`id`, `default`) VALUES (NULL, `1`)' );
2024-02-05 17:07:33 +01:00
$database -> insert ( 'an_tipianagrafiche_lang' , [
2024-03-20 16:15:37 +01:00
'id_lang' => \Models\Locale :: getDefault () -> id ,
2024-02-06 11:36:09 +01:00
'id_record' => $id_tipo ,
'name' => $tipo ,
2024-02-05 17:07:33 +01:00
])[ 'id' ];
2023-09-29 12:52:36 +02:00
2024-03-20 11:13:28 +01:00
$id_tipo = ( new Tipo ()) -> getByField ( 'name' , $tipo );
2020-08-06 18:51:56 +02:00
}
2023-10-05 10:26:57 +02:00
2024-02-06 11:36:09 +01:00
$tipologie [] = $id_tipo ;
2020-08-06 18:51:56 +02:00
}
}
2024-02-06 11:36:09 +01:00
unset ( $record [ 'tipologia' ]);
2020-08-06 18:51:56 +02:00
2024-01-08 14:34:17 +01:00
$tipo = '' ;
2023-09-29 11:23:56 +02:00
if ( ! empty ( $record [ 'tipo' ])) {
2024-01-08 14:34:17 +01:00
$tipo = $record [ 'tipo' ];
2023-09-29 11:23:56 +02:00
}
2023-09-29 12:52:36 +02:00
unset ( $record [ 'tipo' ]);
2023-09-29 11:23:56 +02:00
2020-08-06 18:51:56 +02:00
// Fix per campi con contenuti derivati da query implicite
if ( ! empty ( $record [ 'id_nazione' ])) {
2024-03-27 16:43:30 +01:00
$record [ 'id_nazione' ] = ( new Nazione ()) -> getByField ( 'name' , 'Italia' , \Models\Locale :: getPredefined () -> id );
2020-12-24 12:58:57 +01:00
} else {
unset ( $record [ 'id_nazione' ]);
2020-08-06 18:51:56 +02:00
}
2023-09-29 12:52:36 +02:00
// Creo il settore merceologico nel caso in cui non sia presente
$id_settore = '' ;
if ( ! empty ( $record [ 'id_settore' ])) {
$settore = $record [ 'id_settore' ];
2024-03-20 16:15:37 +01:00
$id_settore = $database -> fetchOne ( 'SELECT `an_settori`.`id` FROM `an_settori` LEFT JOIN (`an_settori_lang` ON`an_settori`.`id` = `an_settori_lang`.`id_record` AND `an_settori_lang`.`id_lang` = ' . prepare ( \Models\Locale :: getDefault () -> id ) . ') WHERE LOWER(`name`) = LOWER(' . prepare ( $settore ) . ')' )[ 'id' ];
2023-09-29 12:52:36 +02:00
if ( empty ( $id_settore )) {
2024-02-05 17:07:33 +01:00
$id_settore = database () -> query ( 'INSERT INTO `an_settori` (`id`, `created_at`, `updated_at`) VALUES (NULL, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)' );
$database -> insert ( 'an_settori_lang' , [
2024-03-20 16:15:37 +01:00
'id_lang' => \Models\Locale :: getDefault () -> id ,
2024-02-05 17:07:33 +01:00
'id_record' => $id_settore ,
'name' => $settore ,
2023-09-29 12:52:36 +02:00
])[ 'id' ];
}
2024-02-15 15:09:43 +01:00
unset ( $record [ 'id_settore' ]);
2023-09-29 12:52:36 +02:00
}
2020-08-06 18:51:56 +02:00
// Separazione dei campi relativi alla sede legale
$campi_sede = [
'indirizzo' ,
'citta' ,
'cap' ,
'provincia' ,
'km' ,
'id_nazione' ,
'telefono' ,
'fax' ,
'cellulare' ,
'email' ,
'idzona' ,
'gaddress' ,
'lat' ,
'lng' ,
];
$dati_sede = [];
foreach ( $campi_sede as $field ) {
2023-09-29 11:23:56 +02:00
if ( $primary_key != $field ) {
if ( isset ( $record [ $field ])) {
$dati_sede [ $field ] = trim ( $record [ $field ]);
unset ( $record [ $field ]);
}
2020-08-06 18:51:56 +02:00
}
}
// Ricerca di eventuale anagrafica corrispondente sulla base del campo definito come primary_key (es. codice)
if ( ! empty ( $primary_key )) {
2021-06-04 17:21:41 +02:00
$anagrafica = Anagrafica :: where ( $primary_key , '=' , trim ( $record [ $primary_key ])) -> first ();
2020-08-06 18:51:56 +02:00
}
// Se non trovo nessuna anagrafica corrispondente, allora la creo
if ( empty ( $anagrafica )) {
$anagrafica = Anagrafica :: build ( $record [ 'ragione_sociale' ]);
}
// Impedisco di aggiornare l'anagrafica Azienda
if ( $anagrafica -> id == $id_azienda ) {
2024-02-16 14:49:00 +01:00
return false ;
2020-08-06 18:51:56 +02:00
}
2023-11-16 11:08:15 +01:00
unset ( $record [ 'ragione_sociale' ]);
2020-08-06 18:51:56 +02:00
2024-02-16 14:49:00 +01:00
$id_banca = '' ;
if ( ! empty ( $record [ 'codiceiban' ])) {
$id_banca = $database -> fetchOne ( 'SELECT `co_banche`.`id` FROM `co_banche` WHERE LOWER(`iban`) = LOWER(' . prepare ( $record [ 'codiceiban' ]) . ') AND `id_anagrafica` = ' . $anagrafica -> id . ' AND deleted_at IS NULL' );
if ( empty ( $id_banca )) {
$id_banca = $database -> query ( 'INSERT INTO `co_banche` (`iban`, `nome`,`id_anagrafica`) VALUES (' . prepare ( $record [ 'codiceiban' ]) . ', "Banca da importazione ' . $anagrafica -> ragione_sociale . '", ' . $anagrafica -> id . ')' );
}
unset ( $record [ 'codiceiban' ]);
}
2020-08-06 18:51:56 +02:00
$anagrafica -> fill ( $record );
$anagrafica -> tipologie = $tipologie ;
2024-02-15 15:09:43 +01:00
$anagrafica -> id_settore = $id_settore ;
2024-01-08 14:34:17 +01:00
$anagrafica -> tipo = $tipo ;
2020-08-06 18:51:56 +02:00
$anagrafica -> save ();
$sede = $anagrafica -> sedeLegale ;
$sede -> fill ( $dati_sede );
$sede -> save ();
}
public static function getExample ()
{
return [
2023-11-17 17:08:32 +01:00
[ 'Codice' , 'Ragione sociale' , 'Nome' , 'Cognome' , 'Codice destinatario' , 'Provincia' , 'Città' , 'Telefono' , 'Indirizzo' , 'CAP' , 'Cellulare' , 'Fax' , 'Email' , 'PEC' , 'Sito Web' , 'Codice fiscale' , 'Data di nascita' , 'Luogo di nascita' , 'Sesso' , 'Partita IVA' , 'IBAN' , 'Note' , 'Nazione' , 'ID Agente' , 'ID pagamento' , 'Tipo' , 'Tipologia' , 'Split Payment' , 'Settore merceologico' ],
2024-02-06 11:36:09 +01:00
[ '001' , 'Mario Rossi' , '' , '' , '12345' , 'PD' , 'Este' , '+39 0429 60 25 12' , 'Via Rovigo, 51' , '35042' , '+39 321 12 34 567' , '' , 'email@anagrafica.it' , 'email@pec.it' , 'www.sito.it' , '' , '' , '' , '' , '123456789' , 'IT60 X054 2811 1010 0000 0123 456' , 'Note dell\'anagrafica di esempio' , 'Italia' , '' , '' , 'Cliente' , 'Privato' , '0' , 'Tessile' ],
2020-08-06 18:51:56 +02:00
];
}
}