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
* Copyright ( C ) DevCode s . n . c .
*
* 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 ;
use Modules\Articoli\Articolo ;
use Modules\Articoli\Categoria ;
use Modules\Iva\Aliquota ;
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 ,
],
[
'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' ,
'label' => 'Fornitore' ,
'names' => [
'id_fornitore' ,
'Id Fornitore' ,
'Fornitore' ,
],
],
[
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' ,
],
];
}
public function import ( $record )
{
$database = database ();
$primary_key = $this -> getPrimaryKey ();
// 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
2020-10-02 16:45:09 +02:00
if ( ! empty ( $record [ 'id_categoria' ])) {
2020-08-07 10:14:09 +02:00
// Categoria
2020-10-02 16:45:09 +02:00
$categoria = Categoria :: where ( 'nome' , $record [ 'id_categoria' ]) -> first ();
2020-08-07 10:14:09 +02:00
if ( empty ( $categoria )) {
2020-10-02 16:45:09 +02:00
$categoria = Categoria :: build ( $record [ 'id_categoria' ]);
2020-08-06 18:51:56 +02:00
}
2020-08-07 10:14:09 +02:00
// Sotto-categoria
$sottocategoria = null ;
2020-10-02 16:45:09 +02:00
if ( ! empty ( $record [ 'id_sottocategoria' ])) {
$sottocategoria = Categoria :: where ( 'nome' , $record [ 'id_sottocategoria' ])
2020-08-07 10:14:09 +02:00
-> where ( 'parent' , $categoria -> id )
-> first ();
if ( empty ( $sottocategoria )) {
2020-10-02 16:45:09 +02:00
$sottocategoria = Categoria :: build ( $record [ 'id_categoria' ]);
2020-08-07 10:14:09 +02:00
$sottocategoria -> parent () -> associate ( $categoria );
$sottocategoria -> save ();
}
2020-08-06 18:51:56 +02:00
}
}
2020-10-02 16:45:09 +02:00
unset ( $record [ 'id_categoria' ]);
unset ( $record [ 'id_sottocategoria' ]);
2020-08-06 18:51:56 +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
unset ( $record [ 'codice_iva_vendita' ]);
2020-08-06 18:51:56 +02:00
2020-08-07 10:14:09 +02:00
// Individuazione articolo e generazione
$articolo = Articolo :: where ( $primary_key , $record [ $primary_key ]) -> first ();
if ( empty ( $articolo )) {
$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
2020-10-02 16:44:24 +02:00
$articolo -> setPrezzoVendita ( $record [ 'prezzo_vendita' ], ( $aliquota -> id ? $aliquota -> id : setting ( 'Iva predefinita' )));
unset ( $record [ 'prezzo_vendita' ]);
2020-11-13 10:56:32 +01:00
// Esportazione della quantità indicata
$qta_registrata = ( float ) ( $record [ 'qta' ]);
$nome_sede = $record [ '$nome_sede' ];
unset ( $record [ 'qta' ]);
unset ( $record [ 'nome_sede' ]);
2020-08-07 10:14:09 +02:00
// Salvataggio delle informazioni generali
$articolo -> fill ( $record );
$articolo -> save ();
2020-08-06 18:51:56 +02:00
2020-08-07 10:14:09 +02:00
// Movimentazione della quantità registrata
2020-11-13 10:56:32 +01:00
$qta_movimento = $qta_registrata - ( float ) ( $articolo -> qta );
$articolo -> movimenta ( $qta_movimento , tr ( 'Movimento da importazione' ), new Carbon (), false , [
/*
'idsede_azienda' => $partenza ,
'idsede_controparte' => $arrivo ,
*/
]);
2020-08-06 18:51:56 +02:00
}
public static function getExample ()
{
return [
[ 'Codice' , 'Barcode' , 'Descrizione' , 'Fornitore' , 'Quantità' , 'Unità di misura' , 'Prezzo acquisto' , 'Prezzo vendita' , 'Peso lordo (KG)' , 'Volume (M3)' , 'Categoria' , 'Sottocategoria' , 'Ubicazione' , 'Note' ],
[ '00004' , '719376861871' , 'Articolo' , 'Mario Rossi' , '10' , 'Kg' , '5,25' , '12,72' , '10,2' , '500' , 'Categoria4' , 'Sottocategoria2' , 'Scaffale 1' , 'Articolo di prova' ],
];
}
}