mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-04 01:34:35 -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\ImportRequest;
|
||||||
use App\Http\Requests\Import\PreImportRequest;
|
use App\Http\Requests\Import\PreImportRequest;
|
||||||
use App\Jobs\Import\CSVImport;
|
use App\Jobs\Import\CSVImport;
|
||||||
|
use App\Jobs\Import\CSVIngest;
|
||||||
use Illuminate\Http\UploadedFile;
|
use Illuminate\Http\UploadedFile;
|
||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
@ -102,7 +103,7 @@ class ImportController extends Controller {
|
|||||||
|
|
||||||
public function import( ImportRequest $request ) {
|
public function import( ImportRequest $request ) {
|
||||||
$data = $request->all();
|
$data = $request->all();
|
||||||
|
nlog($data);
|
||||||
if ( empty( $data['hash'] ) ) {
|
if ( empty( $data['hash'] ) ) {
|
||||||
// Create a reference
|
// Create a reference
|
||||||
$data['hash'] = $hash = Str::random( 32 );
|
$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 );
|
return response()->json( [ 'message' => ctrans( 'texts.import_started' ) ], 200 );
|
||||||
}
|
}
|
||||||
|
@ -517,22 +517,41 @@ class BaseImport
|
|||||||
public function preTransform(array $data, $entity_type)
|
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
|
//sort the array by key
|
||||||
$keys = $this->column_map[$entity_type];
|
$keys = $this->column_map[$entity_type];
|
||||||
ksort($keys);
|
ksort($keys);
|
||||||
|
|
||||||
$data = array_map(function ($row) use ($keys) {
|
$keys = array_shift( $data );
|
||||||
return array_combine($keys, array_intersect_key($row, $keys));
|
|
||||||
}, $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->getCsvData($entity_type);
|
||||||
|
|
||||||
$data = $this->preTransform($data, $entity_type);
|
$data = $this->preTransformCsv($data, $entity_type);
|
||||||
|
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
$this->entity_count['clients'] = 0;
|
$this->entity_count['clients'] = 0;
|
||||||
@ -98,7 +98,7 @@ class Csv extends BaseImport implements ImportInterface
|
|||||||
|
|
||||||
$data = $this->getCsvData($entity_type);
|
$data = $this->getCsvData($entity_type);
|
||||||
|
|
||||||
$data = $this->preTransform($data, $entity_type);
|
$data = $this->preTransformCsv($data, $entity_type);
|
||||||
|
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
$this->entity_count['products'] = 0;
|
$this->entity_count['products'] = 0;
|
||||||
@ -125,7 +125,7 @@ class Csv extends BaseImport implements ImportInterface
|
|||||||
|
|
||||||
$data = $this->getCsvData($entity_type);
|
$data = $this->getCsvData($entity_type);
|
||||||
|
|
||||||
$data = $this->preTransform($data, $entity_type);
|
$data = $this->preTransformCsv($data, $entity_type);
|
||||||
|
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
$this->entity_count['invoices'] = 0;
|
$this->entity_count['invoices'] = 0;
|
||||||
@ -152,7 +152,7 @@ class Csv extends BaseImport implements ImportInterface
|
|||||||
|
|
||||||
$data = $this->getCsvData($entity_type);
|
$data = $this->getCsvData($entity_type);
|
||||||
|
|
||||||
$data = $this->preTransform($data, $entity_type);
|
$data = $this->preTransformCsv($data, $entity_type);
|
||||||
|
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
$this->entity_count['payments'] = 0;
|
$this->entity_count['payments'] = 0;
|
||||||
@ -179,7 +179,7 @@ class Csv extends BaseImport implements ImportInterface
|
|||||||
|
|
||||||
$data = $this->getCsvData($entity_type);
|
$data = $this->getCsvData($entity_type);
|
||||||
|
|
||||||
$data = $this->preTransform($data, $entity_type);
|
$data = $this->preTransformCsv($data, $entity_type);
|
||||||
|
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
$this->entity_count['vendors'] = 0;
|
$this->entity_count['vendors'] = 0;
|
||||||
@ -206,7 +206,7 @@ class Csv extends BaseImport implements ImportInterface
|
|||||||
|
|
||||||
$data = $this->getCsvData($entity_type);
|
$data = $this->getCsvData($entity_type);
|
||||||
|
|
||||||
$data = $this->preTransform($data, $entity_type);
|
$data = $this->preTransformCsv($data, $entity_type);
|
||||||
|
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
$this->entity_count['expenses'] = 0;
|
$this->entity_count['expenses'] = 0;
|
||||||
|
@ -83,7 +83,7 @@ class Invoicely extends BaseImport
|
|||||||
$data = $this->getCsvData($entity_type);
|
$data = $this->getCsvData($entity_type);
|
||||||
|
|
||||||
$data = $this->preTransform($data, $entity_type);
|
$data = $this->preTransform($data, $entity_type);
|
||||||
|
nlog($data);
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
$this->entity_count['invoices'] = 0;
|
$this->entity_count['invoices'] = 0;
|
||||||
return;
|
return;
|
||||||
|
@ -28,8 +28,9 @@ class ClientTransformer extends BaseTransformer {
|
|||||||
if ( isset( $data['Client Name'] ) && $this->hasClient( $data['Client Name'] ) ) {
|
if ( isset( $data['Client Name'] ) && $this->hasClient( $data['Client Name'] ) ) {
|
||||||
throw new ImportException('Client already exists');
|
throw new ImportException('Client already exists');
|
||||||
}
|
}
|
||||||
|
nlog($data);
|
||||||
|
|
||||||
return [
|
$transformed = [
|
||||||
'company_id' => $this->company->id,
|
'company_id' => $this->company->id,
|
||||||
'name' => $this->getString( $data, 'Client Name' ),
|
'name' => $this->getString( $data, 'Client Name' ),
|
||||||
'phone' => $this->getString( $data, 'Phone' ),
|
'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;
|
return $transformed;
|
||||||
}
|
}
|
||||||
|
@ -37,11 +37,13 @@ class CSVIngest implements ShouldQueue {
|
|||||||
|
|
||||||
public ?string $skip_header;
|
public ?string $skip_header;
|
||||||
|
|
||||||
public array $column_map;
|
public $column_map;
|
||||||
|
|
||||||
|
public array $request;
|
||||||
|
|
||||||
public function __construct( array $request, Company $company ) {
|
public function __construct( array $request, Company $company ) {
|
||||||
$this->company = $company;
|
$this->company = $company;
|
||||||
$this->request = $request;
|
$this->request = $request;
|
||||||
$this->hash = $request['hash'];
|
$this->hash = $request['hash'];
|
||||||
$this->import_type = $request['import_type'];
|
$this->import_type = $request['import_type'];
|
||||||
$this->skip_header = $request['skip_header'] ?? null;
|
$this->skip_header = $request['skip_header'] ?? null;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user