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\Articoli\Import ;
2020-11-13 10:56:32 +01:00
use Carbon\Carbon ;
2020-08-07 10:14:09 +02:00
use Importer\CSVImporter ;
2021-10-18 10:23:39 +02:00
use Models\Upload ;
use Modules ;
2020-12-04 15:40:47 +01:00
use Modules\Anagrafiche\Anagrafica ;
2020-11-13 17:54:58 +01:00
use Modules\Anagrafiche\Sede ;
2020-08-07 10:14:09 +02:00
use Modules\Articoli\Articolo ;
use Modules\Articoli\Categoria ;
use Modules\Iva\Aliquota ;
2021-07-28 12:06:29 +02:00
use Plugins\ListinoClienti\DettaglioPrezzo ;
2021-10-18 10:23:39 +02:00
use Uploads ;
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 ) degli Articoli .
*
* @ 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 ,
],
2021-10-15 19:06:26 +02:00
[
'field' => 'immagine' ,
'label' => 'Immagine' ,
'names' => [
'Immagine' ,
'Foto' ,
],
],
2020-08-06 18:51:56 +02:00
[
'field' => 'descrizione' ,
'label' => 'Descrizione' ,
],
[
'field' => 'qta' ,
'label' => 'Quantità' ,
],
[
'field' => 'um' ,
'label' => 'Unità di misura' ,
'names' => [
'Unità di misura' ,
'Unità misura' ,
'Unit` di misura' ,
'um' ,
],
],
[
'field' => 'prezzo_acquisto' ,
'label' => 'Prezzo acquisto' ,
],
[
'field' => 'prezzo_vendita' ,
'label' => 'Prezzo vendita' ,
],
[
'field' => 'peso_lordo' ,
'label' => 'Peso lordo (KG)' ,
'names' => [
'Peso lordo (KG)' ,
'Peso' ,
],
],
[
'field' => 'volume' ,
'label' => 'Volume (M3)' ,
'names' => [
'Volume (M3)' ,
'Volume' ,
],
],
[
2020-10-02 16:45:09 +02:00
'field' => 'id_categoria' ,
2020-08-06 18:51:56 +02:00
'label' => 'Categoria' ,
'names' => [
'Categoria' ,
2020-08-07 10:14:09 +02:00
'categoria' ,
2020-08-06 18:51:56 +02:00
'idcategoria' ,
],
],
[
2020-10-02 16:45:09 +02:00
'field' => 'id_sottocategoria' ,
2020-08-06 18:51:56 +02:00
'label' => 'Sottocategoria' ,
'names' => [
'Sottocategoria' ,
2020-08-07 10:14:09 +02:00
'sottocategoria' ,
2020-08-06 18:51:56 +02:00
'idsottocategoria' ,
],
],
[
'field' => 'barcode' ,
'label' => 'Barcode' ,
'names' => [
'barcode' ,
'Barcode' ,
'EAN' ,
],
],
[
'field' => 'id_fornitore' ,
2020-12-04 16:16:41 +01:00
'label' => 'Fornitore predefinito' ,
2020-08-06 18:51:56 +02:00
'names' => [
'id_fornitore' ,
'Id Fornitore' ,
'Fornitore' ,
2020-12-04 16:16:41 +01:00
'Fornitore predefinito' ,
2020-08-06 18:51:56 +02:00
],
],
[
2020-08-07 10:14:09 +02:00
'field' => 'codice_iva_vendita' ,
2020-08-06 18:51:56 +02:00
'label' => 'Codice IVA vendita' ,
'names' => [
'Codice IVA vendita' ,
2020-08-07 10:14:09 +02:00
'codice_iva_vendita' ,
2020-08-06 18:51:56 +02:00
],
],
[
'field' => 'ubicazione' ,
'label' => 'Ubicazione' ,
],
[
'field' => 'note' ,
'label' => 'Note' ,
],
2020-12-04 16:16:41 +01:00
[
'field' => 'anagrafica_listino' ,
'label' => 'Anagrafica listino' ,
],
[
'field' => 'qta_minima' ,
'label' => 'Qta minima' ,
],
[
'field' => 'qta_massima' ,
'label' => 'Qta massima' ,
],
[
'field' => 'prezzo_listino' ,
'label' => 'Prezzo listino' ,
],
[
'field' => 'sconto_listino' ,
'label' => 'Sconto listino' ,
],
[
'field' => 'dir' ,
'label' => 'Cliente/Fornitore listino' ,
],
2020-08-06 18:51:56 +02:00
];
}
2021-02-26 15:47:20 +01:00
/**
* Procedura di inizializzazione per l ' importazione .
* Effettua una rimozione di tutti i dettagli prezzi per le coppie Articolo - Anagrafica presenti nel CSV .
*
* @ return mixed | void
*/
public function init ()
{
$database = database ();
$primary_key = $this -> getPrimaryKey ();
// Count the numbers of rows in a CSV
$number = $this -> csv -> each ( function ( $row ) {
return true ;
});
$rows = $this -> getRows ( 0 , $number );
$first_record = $this -> getRecord ( $rows [ 1 ]);
if ( ! isset ( $first_record [ 'anagrafica_listino' ]) || empty ( $this -> getPrimaryKey ())) {
return ;
}
2021-03-01 09:41:49 +01:00
2021-02-26 15:47:20 +01:00
foreach ( $rows as $row ) {
// Interpretazione secondo la selezione
$record = $this -> getRecord ( $row );
if ( empty ( $record [ 'anagrafica_listino' ])) {
continue ;
}
$articolo = Articolo :: where ( $primary_key , $record [ $primary_key ])
-> first ();
$anagrafica = Anagrafica :: where ( 'ragione_sociale' , $record [ 'anagrafica_listino' ])
-> first ();
if ( empty ( $articolo ) || empty ( $anagrafica )) {
continue ;
}
$database -> query ( 'DELETE FROM mg_prezzi_articoli WHERE id_articolo = ' . prepare ( $articolo -> id ) . ' AND id_anagrafica = ' . prepare ( $anagrafica -> id ));
}
}
2020-08-06 18:51:56 +02:00
public function import ( $record )
{
$database = database ();
$primary_key = $this -> getPrimaryKey ();
2021-10-15 19:06:26 +02:00
$url = $record [ 'immagine' ];
unset ( $record [ 'immagine' ]);
2020-08-06 18:51:56 +02:00
// Fix per campi con contenuti derivati da query implicite
if ( ! empty ( $record [ 'id_fornitore' ])) {
2020-08-07 10:14:09 +02:00
$record [ 'id_fornitore' ] = $database -> fetchOne ( 'SELECT idanagrafica AS id FROM an_anagrafiche WHERE LOWER(ragione_sociale) = LOWER(' . prepare ( $record [ 'id_fornitore' ]) . ')' )[ 'id' ];
2020-08-06 18:51:56 +02:00
}
2020-08-07 10:14:09 +02:00
// Gestione categoria e sottocategoria
2021-02-26 15:47:20 +01:00
$categoria = null ;
2021-04-26 09:35:01 +02:00
$sottocategoria = null ;
2020-10-02 16:45:09 +02:00
if ( ! empty ( $record [ 'id_categoria' ])) {
2020-08-07 10:14:09 +02:00
// Categoria
2021-02-26 15:47:20 +01:00
$categoria = Categoria :: where ( 'nome' , strtolower ( $record [ 'id_categoria' ])) -> first ();
2021-01-21 19:13:43 +01:00
2021-02-26 15:47:20 +01:00
if ( empty ( $categoria )) {
$categoria = Categoria :: build ( $record [ 'id_categoria' ]);
2020-08-06 18:51:56 +02:00
}
2020-08-07 10:14:09 +02:00
// Sotto-categoria
2020-10-02 16:45:09 +02:00
if ( ! empty ( $record [ 'id_sottocategoria' ])) {
2021-04-26 09:35:01 +02:00
$sottocategoria = Categoria :: where ( 'nome' , $record [ 'id_sottocategoria' ])
2021-02-26 15:47:20 +01:00
-> where ( 'parent' , $categoria -> id )
2020-08-07 10:14:09 +02:00
-> first ();
if ( empty ( $sottocategoria )) {
2021-04-26 09:35:01 +02:00
$sottocategoria = Categoria :: build ( $record [ 'id_sottocategoria' ]);
$sottocategoria -> parent () -> associate ( $categoria );
$sottocategoria -> save ();
2020-08-07 10:14:09 +02:00
}
2020-08-06 18:51:56 +02:00
}
}
2021-07-07 07:57:10 +02:00
// Gestione um
$um = null ;
if ( ! empty ( $record [ 'um' ])) {
$um = $database -> fetchOne ( 'SELECT id FROM `mg_unitamisura` WHERE `valore`=' . prepare ( $record [ 'um' ]));
if ( empty ( $um )) {
2021-07-06 13:08:24 +02:00
$database -> query ( 'INSERT INTO `mg_unitamisura` (`valore`) VALUES (' . prepare ( $record [ 'um' ]) . ')' );
2021-07-07 07:57:10 +02:00
}
}
2021-07-06 13:08:24 +02:00
2020-08-07 10:14:09 +02:00
// Individuazione dell'IVA di vendita tramite il relativo Codice
$aliquota = null ;
if ( ! empty ( $record [ 'codice_iva_vendita' ])) {
$aliquota = Aliquota :: where ( 'codice' , $record [ 'codice_iva_vendita' ]) -> first ();
2020-08-06 18:51:56 +02:00
}
2020-08-07 10:14:09 +02:00
// Individuazione articolo e generazione
2021-02-26 15:47:20 +01:00
$articolo = null ;
// Ricerca sulla base della chiave primaria se presente
if ( ! empty ( $primary_key )) {
$articolo = Articolo :: where ( $primary_key , $record [ $primary_key ]) -> first ();
}
2020-08-07 10:14:09 +02:00
if ( empty ( $articolo )) {
2021-04-26 09:35:01 +02:00
$articolo = Articolo :: build ( $record [ 'codice' ], $record [ 'descrizione' ], $categoria , $sottocategoria );
2020-08-06 18:51:56 +02:00
}
2020-08-07 10:14:09 +02:00
$articolo -> idiva_vendita = $aliquota -> id ;
$articolo -> attivo = 1 ;
2020-08-06 18:51:56 +02:00
2020-11-13 10:56:32 +01:00
// Prezzo di vendita
2021-02-26 15:47:20 +01:00
$articolo -> setPrezzoVendita ( $record [ 'prezzo_vendita' ], $aliquota -> id ? $aliquota -> id : setting ( 'Iva predefinita' ));
2020-10-02 16:44:24 +02:00
2020-11-13 10:56:32 +01:00
// Esportazione della quantità indicata
$qta_registrata = ( float ) ( $record [ 'qta' ]);
2020-12-04 16:16:41 +01:00
$nome_sede = $record [ 'nome_sede' ];
2020-11-13 10:56:32 +01:00
2021-02-26 15:47:20 +01:00
// Salvataggio delle informazioni generali
$articolo -> fill ([
'codice' => $record [ 'codice' ],
'descrizione' => $record [ 'descrizione' ],
'prezzo_acquisto' => $record [ 'prezzo_acquisto' ],
'peso_lordo' => $record [ 'peso_lordo' ],
'volume' => $record [ 'volume' ],
2021-07-06 13:08:24 +02:00
'um' => $record [ 'um' ],
2021-02-26 15:47:20 +01:00
'barcode' => $record [ 'barcode' ],
'id_fornitore' => $record [ 'id_fornitore' ],
'ubicazione' => $record [ 'ubicazione' ],
'note' => $record [ 'note' ],
]);
$articolo -> save ();
// Aggiornamento dettaglio prezzi
2020-12-04 16:16:41 +01:00
$dettagli [ 'anagrafica_listino' ] = $record [ 'anagrafica_listino' ];
$dettagli [ 'qta_minima' ] = $record [ 'qta_minima' ];
$dettagli [ 'qta_massima' ] = $record [ 'qta_massima' ];
2020-12-04 16:40:01 +01:00
$dettagli [ 'prezzo_listino' ] = $record [ 'prezzo_listino' ];
$dettagli [ 'sconto_listino' ] = $record [ 'sconto_listino' ];
2020-12-04 16:16:41 +01:00
$dettagli [ 'dir' ] = $record [ 'dir' ];
2021-02-26 15:47:20 +01:00
$this -> aggiornaDettaglioPrezzi ( $articolo , $dettagli );
2020-12-04 16:16:41 +01:00
2020-08-07 10:14:09 +02:00
// Movimentazione della quantità registrata
2020-11-13 17:54:58 +01:00
$giacenze = $articolo -> getGiacenze ();
2021-02-26 15:47:20 +01:00
$anagrafica_azienda = Anagrafica :: find ( setting ( 'Azienda predefinita' ));
2020-11-13 17:54:58 +01:00
$id_sede = 0 ;
if ( ! empty ( $nome_sede )) {
$sede = Sede :: where ( 'nomesede' , $nome_sede )
-> where ( 'idanagrafica' , $anagrafica_azienda -> id )
-> first ();
$id_sede = $sede -> id ;
}
2021-02-08 10:59:19 +01:00
$qta_movimento = $qta_registrata - $giacenze [ $id_sede ][ 0 ];
2020-11-13 10:56:32 +01:00
$articolo -> movimenta ( $qta_movimento , tr ( 'Movimento da importazione' ), new Carbon (), false , [
2021-02-05 13:27:18 +01:00
'idsede' => $id_sede ,
2020-11-13 10:56:32 +01:00
]);
2021-10-15 19:06:26 +02:00
//Gestione immagine
2021-10-18 10:23:39 +02:00
if ( ! empty ( $url )) {
2021-10-15 19:06:26 +02:00
$file_content = file_get_contents ( $url );
2021-10-18 10:23:39 +02:00
if ( ! empty ( $file_content )) {
$name = 'immagine_' . $articolo -> id . '.' . Upload :: getExtensionFromMimeType ( $file_content );
2021-10-15 19:06:26 +02:00
$upload = Uploads :: upload ( $file_content , [
'name' => 'Immagine' ,
'category' => 'Immagini' ,
'original_name' => $name ,
'id_module' => Modules :: get ( 'Articoli' )[ 'id' ],
'id_record' => $articolo -> id ,
], [
'thumbnails' => true ,
]);
$filename = $upload -> filename ;
if ( ! empty ( $filename )) {
$database -> update ( 'mg_articoli' , [
'immagine' => $filename ,
], [
'id' => $articolo -> id ,
]);
}
}
}
2020-08-06 18:51:56 +02:00
}
public static function getExample ()
{
return [
2020-12-04 16:16:41 +01:00
[ 'Codice' , 'Barcode' , 'Descrizione' , 'Fornitore predefinito' , 'Quantità' , 'Unità di misura' , 'Prezzo acquisto' , 'Prezzo vendita' , 'Peso lordo (KG)' , 'Volume (M3)' , 'Categoria' , 'Sottocategoria' , 'Ubicazione' , 'Note' , 'Anagrafica listino' , 'Qta minima' , 'Qta massima' , 'Prezzo listino' , 'Sconto listino' , 'Cliente/Fornitore listino' ],
[ '00004' , '719376861871' , 'Articolo' , 'Mario Rossi' , '10' , 'Kg' , '5.25' , '12.72' , '10.2' , '500' , 'Categoria4' , 'Sottocategoria2' , 'Scaffale 1' , 'Articolo di prova' , 'Mario Rossi' , '' , '' , '10' , '5' , 'Fornitore' ],
[ '00004' , '719376861871' , 'Articolo' , 'Mario Rossi' , '10' , 'Kg' , '5.25' , '12.72' , '10.2' , '500' , 'Categoria4' , 'Sottocategoria2' , 'Scaffale 1' , 'Articolo di prova' , 'Mario Rossi' , '1' , '10' , '9' , '' , 'Fornitore' ],
[ '00004' , '719376861871' , 'Articolo' , 'Mario Rossi' , '10' , 'Kg' , '5.25' , '12.72' , '10.2' , '500' , 'Categoria4' , 'Sottocategoria2' , 'Scaffale 1' , 'Articolo di prova' , 'Mario Rossi' , '11' , '20' , '8' , '5' , 'Fornitore' ],
2020-12-04 16:40:01 +01:00
[ '00004' , '719376861871' , 'Articolo' , 'Mario Rossi' , '10' , 'Kg' , '5.25' , '12.72' , '10.2' , '500' , 'Categoria4' , 'Sottocategoria2' , 'Scaffale 1' , 'Articolo di prova' , 'Mario Verdi' , '1' , '10' , '20' , '10' , 'Cliente' ],
2020-08-06 18:51:56 +02:00
];
}
2021-02-26 15:47:20 +01:00
protected function aggiornaDettaglioPrezzi ( Articolo $articolo , $dettagli )
{
// Listini
$anagrafica = Anagrafica :: where ( 'ragione_sociale' , $dettagli [ 'anagrafica_listino' ]) -> first ();
$dettagli [ 'dir' ] = strtolower ( $dettagli [ 'dir' ]);
if ( $dettagli [ 'dir' ] == 'fornitore' ) {
$dettagli [ 'dir' ] = 'uscita' ;
} elseif ( $dettagli [ 'dir' ] == 'cliente' ) {
$dettagli [ 'dir' ] = 'entrata' ;
} else {
$dettagli [ 'dir' ] = null ;
}
if ( ! empty ( $anagrafica ) && ! empty ( $dettagli [ 'dir' ])) {
$dettaglio_predefinito = DettaglioPrezzo :: build ( $articolo , $anagrafica , $dettagli [ 'dir' ]);
$dettaglio_predefinito -> sconto_percentuale = $dettagli [ 'sconto_listino' ];
$dettaglio_predefinito -> setPrezzoUnitario ( $dettagli [ 'prezzo_listino' ]);
if ( $dettagli [ 'qta_minima' ] !== null && ! empty ( $dettagli [ 'qta_massima' ])) {
$dettaglio_predefinito -> minimo = $dettagli [ 'qta_minima' ];
$dettaglio_predefinito -> massimo = $dettagli [ 'qta_massima' ];
}
$dettaglio_predefinito -> save ();
}
}
2020-08-06 18:51:56 +02:00
}