mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Refactor for imports
This commit is contained in:
parent
89d0cb7bb7
commit
62c1b6b2f5
@ -14,6 +14,7 @@ namespace App\Http\Controllers;
|
||||
use App\Http\Requests\Import\ImportRequest;
|
||||
use App\Http\Requests\Import\PreImportRequest;
|
||||
use App\Jobs\Import\CSVImport;
|
||||
use App\Jobs\Import\CSVIngest;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Str;
|
||||
@ -102,7 +103,7 @@ class ImportController extends Controller {
|
||||
|
||||
public function import( ImportRequest $request ) {
|
||||
$data = $request->all();
|
||||
|
||||
nlog($data);
|
||||
if ( empty( $data['hash'] ) ) {
|
||||
// Create a reference
|
||||
$data['hash'] = $hash = Str::random( 32 );
|
||||
@ -116,7 +117,9 @@ class ImportController extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
CSVImport::dispatch( $data, auth()->user()->company() );
|
||||
unset($data['files']);
|
||||
// CSVImport::dispatch( $data, auth()->user()->company() );
|
||||
CSVIngest::dispatch( $data, auth()->user()->company() );
|
||||
|
||||
return response()->json( [ 'message' => ctrans( 'texts.import_started' ) ], 200 );
|
||||
}
|
||||
|
@ -517,22 +517,41 @@ class BaseImport
|
||||
public function preTransform(array $data, $entity_type)
|
||||
{
|
||||
|
||||
if (empty($this->column_map[$entity_type])) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
if ($this->skip_header) {
|
||||
array_shift($data);
|
||||
}
|
||||
|
||||
//sort the array by key
|
||||
$keys = $this->column_map[$entity_type];
|
||||
ksort($keys);
|
||||
|
||||
$data = array_map(function ($row) use ($keys) {
|
||||
return array_combine($keys, array_intersect_key($row, $keys));
|
||||
}, $data);
|
||||
$keys = array_shift( $data );
|
||||
|
||||
return array_map( function ( $values ) use ( $keys ) {
|
||||
return array_combine( $keys, $values );
|
||||
}, $data );
|
||||
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function preTransformCsv(array $data, $entity_type)
|
||||
{
|
||||
|
||||
if ( empty( $this->column_map[ $entity_type ] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( $this->skip_header ) {
|
||||
array_shift( $data );
|
||||
}
|
||||
|
||||
//sort the array by key
|
||||
$keys = $this->column_map[ $entity_type ];
|
||||
ksort( $keys );
|
||||
|
||||
$data = array_map( function ( $row ) use ( $keys ) {
|
||||
return array_combine( $keys, array_intersect_key( $row, $keys ) );
|
||||
}, $data );
|
||||
|
||||
|
||||
return $data;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -71,7 +71,7 @@ class Csv extends BaseImport implements ImportInterface
|
||||
|
||||
$data = $this->getCsvData($entity_type);
|
||||
|
||||
$data = $this->preTransform($data, $entity_type);
|
||||
$data = $this->preTransformCsv($data, $entity_type);
|
||||
|
||||
if (empty($data)) {
|
||||
$this->entity_count['clients'] = 0;
|
||||
@ -98,7 +98,7 @@ class Csv extends BaseImport implements ImportInterface
|
||||
|
||||
$data = $this->getCsvData($entity_type);
|
||||
|
||||
$data = $this->preTransform($data, $entity_type);
|
||||
$data = $this->preTransformCsv($data, $entity_type);
|
||||
|
||||
if (empty($data)) {
|
||||
$this->entity_count['products'] = 0;
|
||||
@ -125,7 +125,7 @@ class Csv extends BaseImport implements ImportInterface
|
||||
|
||||
$data = $this->getCsvData($entity_type);
|
||||
|
||||
$data = $this->preTransform($data, $entity_type);
|
||||
$data = $this->preTransformCsv($data, $entity_type);
|
||||
|
||||
if (empty($data)) {
|
||||
$this->entity_count['invoices'] = 0;
|
||||
@ -152,7 +152,7 @@ class Csv extends BaseImport implements ImportInterface
|
||||
|
||||
$data = $this->getCsvData($entity_type);
|
||||
|
||||
$data = $this->preTransform($data, $entity_type);
|
||||
$data = $this->preTransformCsv($data, $entity_type);
|
||||
|
||||
if (empty($data)) {
|
||||
$this->entity_count['payments'] = 0;
|
||||
@ -179,7 +179,7 @@ class Csv extends BaseImport implements ImportInterface
|
||||
|
||||
$data = $this->getCsvData($entity_type);
|
||||
|
||||
$data = $this->preTransform($data, $entity_type);
|
||||
$data = $this->preTransformCsv($data, $entity_type);
|
||||
|
||||
if (empty($data)) {
|
||||
$this->entity_count['vendors'] = 0;
|
||||
@ -206,7 +206,7 @@ class Csv extends BaseImport implements ImportInterface
|
||||
|
||||
$data = $this->getCsvData($entity_type);
|
||||
|
||||
$data = $this->preTransform($data, $entity_type);
|
||||
$data = $this->preTransformCsv($data, $entity_type);
|
||||
|
||||
if (empty($data)) {
|
||||
$this->entity_count['expenses'] = 0;
|
||||
|
@ -83,7 +83,7 @@ class Invoicely extends BaseImport
|
||||
$data = $this->getCsvData($entity_type);
|
||||
|
||||
$data = $this->preTransform($data, $entity_type);
|
||||
|
||||
nlog($data);
|
||||
if (empty($data)) {
|
||||
$this->entity_count['invoices'] = 0;
|
||||
return;
|
||||
|
@ -28,8 +28,9 @@ class ClientTransformer extends BaseTransformer {
|
||||
if ( isset( $data['Client Name'] ) && $this->hasClient( $data['Client Name'] ) ) {
|
||||
throw new ImportException('Client already exists');
|
||||
}
|
||||
nlog($data);
|
||||
|
||||
return [
|
||||
$transformed = [
|
||||
'company_id' => $this->company->id,
|
||||
'name' => $this->getString( $data, 'Client Name' ),
|
||||
'phone' => $this->getString( $data, 'Phone' ),
|
||||
@ -44,5 +45,7 @@ class ClientTransformer extends BaseTransformer {
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
return $transformed;
|
||||
}
|
||||
}
|
||||
|
@ -52,7 +52,6 @@ class InvoiceTransformer extends BaseTransformer {
|
||||
],
|
||||
];
|
||||
}
|
||||
nlog($transformed);
|
||||
|
||||
return $transformed;
|
||||
}
|
||||
|
@ -37,11 +37,13 @@ class CSVIngest implements ShouldQueue {
|
||||
|
||||
public ?string $skip_header;
|
||||
|
||||
public array $column_map;
|
||||
public $column_map;
|
||||
|
||||
public array $request;
|
||||
|
||||
public function __construct( array $request, Company $company ) {
|
||||
$this->company = $company;
|
||||
$this->request = $request;
|
||||
$this->request = $request;
|
||||
$this->hash = $request['hash'];
|
||||
$this->import_type = $request['import_type'];
|
||||
$this->skip_header = $request['skip_header'] ?? null;
|
||||
|
Loading…
x
Reference in New Issue
Block a user