1
0
mirror of https://github.com/devcode-it/openstamanager.git synced 2025-02-18 04:20:50 +01:00

Introduzione interfaccia per Importazione

This commit is contained in:
Thomas Zilio 2020-08-07 10:14:09 +02:00
parent be95e166b1
commit ef31110afa
7 changed files with 172 additions and 139 deletions

View File

@ -2,10 +2,10 @@
namespace Modules\Anagrafiche\Import; namespace Modules\Anagrafiche\Import;
use Imports\CSVImport; use Importer\CSVImporter;
use Modules\Anagrafiche\Anagrafica; use Modules\Anagrafiche\Anagrafica;
class CSV extends CSVImport class CSV extends CSVImporter
{ {
public function getAvailableFields() public function getAvailableFields()
{ {
@ -249,11 +249,6 @@ class CSV extends CSVImport
return; return;
} }
// se non imposto nessun codice evito di resettare quello calcolato automaticamente o già presente
if (empty($record['codice'])) {
unset($record['codice']);
}
$anagrafica->fill($record); $anagrafica->fill($record);
$anagrafica->tipologie = $tipologie; $anagrafica->tipologie = $tipologie;
$anagrafica->save(); $anagrafica->save();

View File

@ -16,6 +16,10 @@ class Articolo extends Model
use SoftDeletes; use SoftDeletes;
use RecordTrait; use RecordTrait;
protected $guarded = [
'qta',
];
protected $table = 'mg_articoli'; protected $table = 'mg_articoli';
public static function build($codice, $nome, Categoria $categoria = null, Categoria $sottocategoria = null) public static function build($codice, $nome, Categoria $categoria = null, Categoria $sottocategoria = null)

View File

@ -2,10 +2,12 @@
namespace Modules\Articoli\Import; namespace Modules\Articoli\Import;
use Imports\CSVImport; use Importer\CSVImporter;
use Modules\Anagrafiche\Anagrafica; use Modules\Articoli\Articolo;
use Modules\Articoli\Categoria;
use Modules\Iva\Aliquota;
class CSV extends CSVImport class CSV extends CSVImporter
{ {
public function getAvailableFields() public function getAvailableFields()
{ {
@ -58,20 +60,20 @@ class CSV extends CSVImport
], ],
], ],
[ [
'field' => 'id_categoria', 'field' => 'categoria',
'label' => 'Categoria', 'label' => 'Categoria',
'names' => [ 'names' => [
'Categoria', 'Categoria',
'id_categoria', 'categoria',
'idcategoria', 'idcategoria',
], ],
], ],
[ [
'field' => 'id_sottocategoria', 'field' => 'sottocategoria',
'label' => 'Sottocategoria', 'label' => 'Sottocategoria',
'names' => [ 'names' => [
'Sottocategoria', 'Sottocategoria',
'id_sottocategoria', 'sottocategoria',
'idsottocategoria', 'idsottocategoria',
], ],
], ],
@ -94,11 +96,11 @@ class CSV extends CSVImport
], ],
], ],
[ [
'field' => 'idiva_vendita', 'field' => 'codice_iva_vendita',
'label' => 'Codice IVA vendita', 'label' => 'Codice IVA vendita',
'names' => [ 'names' => [
'Codice IVA vendita', 'Codice IVA vendita',
'idiva_vendita', 'codice_iva_vendita',
], ],
], ],
[ [
@ -117,100 +119,60 @@ class CSV extends CSVImport
$database = database(); $database = database();
$primary_key = $this->getPrimaryKey(); $primary_key = $this->getPrimaryKey();
// Fix per campi con contenuti derivati da query implicite
if (!empty($record['id_fornitore'])) {
$record['id_fornitore'] = $database->fetchOne('SELECT idanagrafica AS id FROM an_anagrafiche WHERE LOWER(ragione_sociale) = LOWER('.prepare($record['id_fornitore']).')')['id'];
}
// Gestione categoria e sottocategoria
if (!empty($record['categoria'])) {
// Categoria
$categoria = Categoria::where('nome', $record['categoria'])->first();
if (empty($categoria)) {
$categoria = Categoria::build($record['categoria']);
}
// Sotto-categoria
$sottocategoria = null;
if (!empty($record['sottocategoria'])) {
$sottocategoria = Categoria::where('nome', $record['sottocategoria'])
->where('parent', $categoria->id)
->first();
if (empty($sottocategoria)) {
$sottocategoria = Categoria::build($record['categoria']);
$sottocategoria->parent()->associate($categoria);
$sottocategoria->save();
}
}
}
// 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();
}
unset($record['codice_iva_vendita']);
// 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);
}
$articolo->idiva_vendita = $aliquota->id;
$articolo->attivo = 1;
// Esportazione della quantità indicata
$qta = $record['qta']; $qta = $record['qta'];
unset($record['qta']); unset($record['qta']);
$record['attivo'] = 1; // Salvataggio delle informazioni generali
$articolo->fill($record);
$articolo->save();
// Fix per campi con contenuti derivati da query implicite // Movimentazione della quantità registrata
if (!empty($record['id_fornitore'])) { $articolo->movimenta($qta, tr('Movimento da importazione'));
$record['id_fornitore'] = $database->fetchOne('SELECT idanagrafica AS id FROM an_anagrafiche WHERE LOWER(ragione_sociale) = LOWER('.prepare($record['v']).')')['id'];
}
// Categorie
if (!empty($record['id_categoria'])) {
$rs_cat = $database->select('mg_categorie', 'id', [
'nome' => $record['id_categoria'],
]);
if (empty($rs_cat[0]['id'])) {
$database->insert('mg_categorie', [
'nome' => $record['id_categoria'],
]);
$record['id_categoria'] = $database->lastInsertedID();
} else {
$record['id_categoria'] = $rs_cat[0]['id'];
}
}
// Sottocategorie
if (!empty($record['id_sottocategoria'])) {
$rs_cat2 = $database->select('mg_categorie', 'id', [
'nome' => $record['id_sottocategoria'],
'parent' => $record['id_categoria'],
]);
if (empty($rs_cat2[0]['id'])) {
$database->insert('mg_categorie', [
'nome' => $record['id_sottocategoria'],
'parent' => $record['id_categoria'],
]);
$record['id_sottocategoria'] = $database->lastInsertedID();
} else {
$record['id_sottocategoria'] = $rs_cat2[0]['id'];
}
}
// Um
if (!empty($record['um'])) {
$rs_um = $database->select('mg_unitamisura', 'id', [
'valore' => $record['um'],
]);
if (empty($rs_um[0]['id'])) {
$database->insert('mg_unitamisura', [
'valore' => $record['um'],
]);
}
}
// Codice --> ID IVA vendita
if (!empty($record['idiva_vendita'])) {
$rs_iva = $database->select('co_iva', 'id', [
'codice' => $record['idiva_vendita'],
]);
if (!empty($rs_iva[0]['id'])) {
$record['idiva_vendita'] = $rs_iva[0]['id'];
}
}
// Insert o update
$insert = true;
if (!empty($primary_key)) {
$rs = $database->select('mg_articoli', $primary_key, [
$primary_key => $record[$primary_key],
]);
$insert = !in_array($record[$primary_key], $rs[0]);
}
// Insert
if ($insert) {
$record['id_categoria'] = (empty($record['id_categoria'])) ? 0 : $record['id_categoria'];
$database->insert('mg_articoli', $record);
add_movimento_magazzino($database->lastInsertedID(), $qta, [], 'Movimento da import', date());
}
// Update
else {
$database->update('mg_articoli', $record, [$primary_key => $record[$primary_key]]);
$rs = $database->select('mg_articoli', 'id', [
$primary_key => $record[$primary_key],
]);
add_movimento_magazzino($rs[0]['id'], $qta, [], 'Movimento da import', date());
}
} }
public static function getExample() public static function getExample()

View File

@ -61,7 +61,7 @@ switch (filter('op')) {
$primary_key = post('primary_key'); $primary_key = post('primary_key');
$csv->setPrimaryKey($primary_key); $csv->setPrimaryKey($primary_key);
$count = $csv->importSet($offset, $limit); $count = $csv->importRows($offset, $limit);
$more = $count == $limit; $more = $count == $limit;
echo json_encode([ echo json_encode([

View File

@ -45,8 +45,8 @@ if (empty($id_record)) {
</div>'; </div>';
// Lettura delle prime righe disponibili // Lettura delle prime righe disponibili
$righe = $csv->get(0, 10); $righe = $csv->getRows(0, 10);
$prima_riga = $righe[0]; $prima_riga = $csv->getHeader();
$numero_colonne = count($prima_riga); $numero_colonne = count($prima_riga);
// Trasformazione dei nomi indicati per i campi in lowercase // Trasformazione dei nomi indicati per i campi in lowercase

View File

@ -1,11 +1,11 @@
<?php <?php
namespace Imports; namespace Importer;
use Filter; use Filter;
use League\Csv\Reader; use League\Csv\Reader;
abstract class CSVImport abstract class CSVImporter implements ImporterInterface
{ {
protected $csv; protected $csv;
@ -38,13 +38,14 @@ abstract class CSVImport
abstract public function getAvailableFields(); abstract public function getAvailableFields();
/** public function getHeader()
* @param $offset {
* @param $length $first_row = $this->getRows(0, 1);
*
* @return array return array_shift($first_row);
*/ }
public function get($offset, $length)
public function getRows($offset, $length)
{ {
$rows = []; $rows = [];
for ($i = 0; $i < $length; ++$i) { for ($i = 0; $i < $length; ++$i) {
@ -61,17 +62,11 @@ abstract class CSVImport
return $rows; return $rows;
} }
/** public function importRows($offset, $length)
* @param $offset
* @param $length
*
* @return int
*/
public function importSet($offset, $length)
{ {
$associations = $this->getColumnAssociations(); $associations = $this->getColumnAssociations();
$rows = $this->get($offset, $length); $rows = $this->getRows($offset, $length);
foreach ($rows as $row) { foreach ($rows as $row) {
// Interpretazione della riga come record // Interpretazione della riga come record
$record = []; $record = [];
@ -89,31 +84,18 @@ abstract class CSVImport
return count($rows); return count($rows);
} }
/**
* @param $record
*
* @return bool
*/
abstract public function import($record); abstract public function import($record);
/**
* @return mixed
*/
public function getPrimaryKey() public function getPrimaryKey()
{ {
return $this->primary_key; return $this->primary_key;
} }
/**
* @param $field_key
*/
public function setPrimaryKey($field_key) public function setPrimaryKey($field_key)
{ {
$this->primary_key = $this->getAvailableFields()[$field_key]['field']; $this->primary_key = $this->getAvailableFields()[$field_key]['field'];
} }
abstract public static function getExample();
public static function createExample($filepath) public static function createExample($filepath)
{ {
$content = static::getExample(); $content = static::getExample();

View File

@ -0,0 +1,90 @@
<?php
namespace Importer;
/**
* Interfaccia che definisce la struttura di base per la gestione delle importazioni di documenti come dati del gestionale.
*/
interface ImporterInterface
{
/**
* Restituisce le associazioni impostate tra colonne e campi del documento.
*
* @return mixed
*/
public function getColumnAssociations();
/**
* Imposta l'associazione di una specifica colonna del documento al relativo campo del documento.
*
* @param $column_key
* @param $field_key
*
* @return mixed
*/
public function setColumnAssociation($column_key, $field_key);
/**
* Restitusice i campi disponibili all'importazione.
*
* @return mixed
*/
public function getAvailableFields();
/**
* Restituisce l'header (potenziale) per il documento da importare.
*
* @return mixed
*/
public function getHeader();
/**
* Restituisce un sottoinsieme delle righe del documento.
*
* @param $offset
* @param $length
*
* @return array
*/
public function getRows($offset, $length);
/**
* Importa un sottoinsieme delle righe del documento nel gestionale.
*
* @param $offset
* @param $length
*
* @return int
*/
public function importRows($offset, $length);
/**
* Gestisce le operazioni di importazione per un singolo record.
*
* @param $record
*
* @return bool
*/
public function import($record);
/**
* Restituisce la chiave primaria impostata dall'utente.
*
* @return mixed
*/
public function getPrimaryKey();
/**
* Imposta la chiave primaria selezionata dall'utente.
*
* @param $field_key
*/
public function setPrimaryKey($field_key);
/**
* Restituisce un esempio di dato importabile.
*
* @return array
*/
public static function getExample();
}