mirror of
https://github.com/devcode-it/openstamanager.git
synced 2025-06-05 22:09:38 +02:00
Introduzione sistema per esportazione dei record
This commit is contained in:
126
modules/anagrafiche/src/Export/CSV.php
Normal file
126
modules/anagrafiche/src/Export/CSV.php
Normal file
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Anagrafiche\Export;
|
||||
|
||||
use Exporter\CSVExporter;
|
||||
use Modules\Anagrafiche\Anagrafica;
|
||||
|
||||
/**
|
||||
* Struttura per la gestione delle operazioni di esportazione (in CSV) delle Anagrafiche.
|
||||
*
|
||||
* @since 2.4.18
|
||||
*/
|
||||
class CSV extends CSVExporter
|
||||
{
|
||||
public function getAvailableFields()
|
||||
{
|
||||
return [
|
||||
[
|
||||
'field' => 'codice',
|
||||
'label' => 'Codice',
|
||||
'primary_key' => true,
|
||||
],
|
||||
[
|
||||
'field' => 'ragione_sociale',
|
||||
'label' => 'Ragione sociale',
|
||||
],
|
||||
[
|
||||
'field' => 'codice_destinatario',
|
||||
'label' => 'Codice destinatario',
|
||||
],
|
||||
[
|
||||
'field' => 'provincia',
|
||||
'label' => 'Provincia',
|
||||
],
|
||||
[
|
||||
'field' => 'citta',
|
||||
'label' => 'Città',
|
||||
],
|
||||
[
|
||||
'field' => 'telefono',
|
||||
'label' => 'Telefono',
|
||||
],
|
||||
[
|
||||
'field' => 'indirizzo',
|
||||
'label' => 'Indirizzo',
|
||||
],
|
||||
[
|
||||
'field' => 'indirizzo2',
|
||||
'label' => 'Civico',
|
||||
],
|
||||
[
|
||||
'field' => 'cap',
|
||||
'label' => 'CAP',
|
||||
],
|
||||
[
|
||||
'field' => 'cellulare',
|
||||
'label' => 'Cellulare',
|
||||
],
|
||||
[
|
||||
'field' => 'fax',
|
||||
'label' => 'Fax',
|
||||
],
|
||||
[
|
||||
'field' => 'email',
|
||||
'label' => 'Email',
|
||||
],
|
||||
[
|
||||
'field' => 'pec',
|
||||
'label' => 'PEC',
|
||||
],
|
||||
[
|
||||
'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',
|
||||
],
|
||||
[
|
||||
'field' => 'codiceiban',
|
||||
'label' => 'IBAN',
|
||||
],
|
||||
[
|
||||
'field' => 'note',
|
||||
'label' => 'Note',
|
||||
],
|
||||
[
|
||||
'field' => 'id_nazione',
|
||||
'label' => 'Nazione',
|
||||
],
|
||||
[
|
||||
'field' => 'idagente',
|
||||
'label' => 'ID Agente',
|
||||
],
|
||||
[
|
||||
'field' => 'idpagamento_vendite',
|
||||
'label' => 'ID Pagamento',
|
||||
],
|
||||
[
|
||||
'field' => 'idtipoanagrafica',
|
||||
'label' => 'Tipo',
|
||||
],
|
||||
[
|
||||
'field' => 'tipo',
|
||||
'label' => 'Tipologia',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function getRecords()
|
||||
{
|
||||
return Anagrafica::all();
|
||||
}
|
||||
}
|
72
src/Exporter/CSVExporter.php
Normal file
72
src/Exporter/CSVExporter.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace Exporter;
|
||||
|
||||
use League\Csv\Writer;
|
||||
|
||||
/**
|
||||
* Classe dedicata alla gestione dell'importazione da file CSV.
|
||||
*
|
||||
* @since 2.4.18
|
||||
*/
|
||||
abstract class CSVExporter implements ExporterInterface
|
||||
{
|
||||
protected $csv;
|
||||
protected $records;
|
||||
|
||||
public function __construct($file)
|
||||
{
|
||||
// Impostazione automatica per i caratteri di fine riga
|
||||
if (!ini_get('auto_detect_line_endings')) {
|
||||
ini_set('auto_detect_line_endings', '1');
|
||||
}
|
||||
|
||||
// Gestione del file CSV
|
||||
$this->csv = Writer::createFromPath($file, 'w+');
|
||||
$this->csv->setDelimiter(';');
|
||||
}
|
||||
|
||||
abstract public function getAvailableFields();
|
||||
|
||||
abstract public function getRecords();
|
||||
|
||||
public function setRecords($records)
|
||||
{
|
||||
$this->records = $records;
|
||||
}
|
||||
|
||||
public function setHeader()
|
||||
{
|
||||
$fields = $this->getAvailableFields();
|
||||
$header = array_map(function ($item) {
|
||||
return $item['label'];
|
||||
}, $fields);
|
||||
|
||||
return $this->csv->insertOne($header);
|
||||
}
|
||||
|
||||
public function exportRecords()
|
||||
{
|
||||
$records = $this->records ?: $this->getRecords();
|
||||
foreach ($records as $record) {
|
||||
// Esportazione del record
|
||||
$this->export($record);
|
||||
}
|
||||
|
||||
return count($records);
|
||||
}
|
||||
|
||||
public function export($record)
|
||||
{
|
||||
$fields = $this->getAvailableFields();
|
||||
|
||||
$row = [];
|
||||
foreach ($fields as $field) {
|
||||
$nome = $field['field'];
|
||||
|
||||
$row[] = $record[$nome];
|
||||
}
|
||||
|
||||
return $this->csv->insertOne($row);
|
||||
}
|
||||
}
|
57
src/Exporter/ExporterInterface.php
Normal file
57
src/Exporter/ExporterInterface.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace Exporter;
|
||||
|
||||
/**
|
||||
* Interfaccia che definisce la struttura di base per la gestione delle esportazioni dei dati del gestionale in formati esterni.
|
||||
*
|
||||
* @since 2.4.18
|
||||
*/
|
||||
interface ExporterInterface
|
||||
{
|
||||
/**
|
||||
* Restitusice i campi disponibili all'esportazione.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getAvailableFields();
|
||||
|
||||
/**
|
||||
* Imposta l'header (potenziale) per il documento da importare.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function setHeader();
|
||||
|
||||
/**
|
||||
* Restituisce l'insieme dei record del gestionale da esportare.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getRecords();
|
||||
|
||||
/**
|
||||
* Imposta l'insieme dei record del gestionale da esportare.
|
||||
*
|
||||
* @param $records
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setRecords($records);
|
||||
|
||||
/**
|
||||
* Esporta l'insieme dei record del documento nel gestionale.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function exportRecords();
|
||||
|
||||
/**
|
||||
* Gestisce le operazioni di esportazione per un singolo record.
|
||||
*
|
||||
* @param $record
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function export($record);
|
||||
}
|
Reference in New Issue
Block a user