mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Finish import changes
This commit is contained in:
parent
5932a837dd
commit
6af5084e0a
@ -23,11 +23,13 @@ class ImportService
|
|||||||
protected $invoiceRepo;
|
protected $invoiceRepo;
|
||||||
protected $clientRepo;
|
protected $clientRepo;
|
||||||
protected $contactRepo;
|
protected $contactRepo;
|
||||||
|
protected $processedRows = array();
|
||||||
|
|
||||||
public static $entityTypes = [
|
public static $entityTypes = [
|
||||||
ENTITY_CLIENT,
|
ENTITY_CLIENT,
|
||||||
ENTITY_CONTACT,
|
ENTITY_CONTACT,
|
||||||
ENTITY_INVOICE,
|
ENTITY_INVOICE,
|
||||||
|
ENTITY_PAYMENT,
|
||||||
ENTITY_TASK,
|
ENTITY_TASK,
|
||||||
];
|
];
|
||||||
|
|
||||||
@ -73,26 +75,45 @@ class ImportService
|
|||||||
RESULT_FAILURE => [],
|
RESULT_FAILURE => [],
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// Convert the data
|
||||||
|
$row_list = array();
|
||||||
|
$maps = $this->createMaps();
|
||||||
|
Excel::load($file, function ($reader) use ($source, $entityType, $maps, &$row_list, &$results) {
|
||||||
$this->checkData($entityType, count($reader->all()));
|
$this->checkData($entityType, count($reader->all()));
|
||||||
$maps = $this->createMaps();
|
|
||||||
|
|
||||||
|
|
||||||
|
$reader->each(function ($row) use ($source, $entityType, $maps, &$row_list, &$results) {
|
||||||
|
$data_index = $this->transformRow($source, $entityType, $row, $maps);
|
||||||
|
|
||||||
|
if ($data_index !== false){
|
||||||
|
if($data_index !== true){// Wasn't merged with another row
|
||||||
|
$row_list[] = array('row'=>$row, 'data_index'=>$data_index);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$results[RESULT_FAILURE][] = $row;
|
$results[RESULT_FAILURE][] = $row;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Save the data
|
||||||
|
foreach($row_list as $row_data){
|
||||||
|
$result = $this->saveData($source, $entityType, $row_data['row'], $row_data['data_index'], $maps);
|
||||||
|
if ($result) {
|
||||||
|
$results[RESULT_SUCCESS][] = $result;
|
||||||
|
} else {
|
||||||
|
$results[RESULT_FAILURE][] = $row_data['row'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function saveData($source, $entityType, $row, $maps)
|
private function transformRow($source, $entityType, $row, $maps)
|
||||||
{
|
{
|
||||||
$transformer = $this->getTransformer($source, $entityType, $maps);
|
$transformer = $this->getTransformer($source, $entityType, $maps);
|
||||||
$resource = $transformer->transform($row, $maps);
|
$resource = $transformer->transform($row, $maps);
|
||||||
|
|
||||||
if (!$resource) {
|
if (!$resource) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = $this->fractal->createData($resource)->toArray();
|
$data = $this->fractal->createData($resource)->toArray();
|
||||||
@ -103,10 +124,32 @@ class ImportService
|
|||||||
$invoice = Invoice::createNew();
|
$invoice = Invoice::createNew();
|
||||||
$data['invoice_number'] = $account->getNextInvoiceNumber($invoice);
|
$data['invoice_number'] = $account->getNextInvoiceNumber($invoice);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->validate($source, $data, $entityType) !== true) {
|
if ($this->validate($source, $data, $entityType) !== true) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if($entityType == ENTITY_INVOICE){
|
||||||
|
if(empty($this->processedRows[$data['invoice_number']])){
|
||||||
|
$this->processedRows[$data['invoice_number']] = $data;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
// Merge invoice items
|
||||||
|
$this->processedRows[$data['invoice_number']]['invoice_items'] = array_merge($this->processedRows[$data['invoice_number']]['invoice_items'], $data['invoice_items']);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
$this->processedRows[] = $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
end($this->processedRows);
|
||||||
|
return key($this->processedRows);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function saveData($source, $entityType, $row, $data_index, $maps)
|
||||||
|
{
|
||||||
|
$data = $this->processedRows[$data_index];
|
||||||
$entity = $this->{"{$entityType}Repo"}->save($data);
|
$entity = $this->{"{$entityType}Repo"}->save($data);
|
||||||
|
|
||||||
// if the invoice is paid we'll also create a payment record
|
// if the invoice is paid we'll also create a payment record
|
||||||
@ -198,10 +241,12 @@ class ImportService
|
|||||||
}
|
}
|
||||||
|
|
||||||
$invoiceMap = [];
|
$invoiceMap = [];
|
||||||
|
$invoiceClientMap = [];
|
||||||
$invoices = $this->invoiceRepo->all();
|
$invoices = $this->invoiceRepo->all();
|
||||||
foreach ($invoices as $invoice) {
|
foreach ($invoices as $invoice) {
|
||||||
if ($number = strtolower(trim($invoice->invoice_number))) {
|
if ($number = strtolower(trim($invoice->invoice_number))) {
|
||||||
$invoiceMap[$number] = $invoice->id;
|
$invoiceMap[$number] = $invoice->id;
|
||||||
|
$invoiceClientMap[$number] = $invoice->client_id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -222,6 +267,7 @@ class ImportService
|
|||||||
return [
|
return [
|
||||||
ENTITY_CLIENT => $clientMap,
|
ENTITY_CLIENT => $clientMap,
|
||||||
ENTITY_INVOICE => $invoiceMap,
|
ENTITY_INVOICE => $invoiceMap,
|
||||||
|
ENTITY_INVOICE.'_'.ENTITY_CLIENT => $invoiceClientMap,
|
||||||
'countries' => $countryMap,
|
'countries' => $countryMap,
|
||||||
'countries2' => $countryMap2,
|
'countries2' => $countryMap2,
|
||||||
'currencies' => $currencyMap,
|
'currencies' => $currencyMap,
|
||||||
@ -365,6 +411,8 @@ class ImportService
|
|||||||
$this->checkData($entityType, count($data));
|
$this->checkData($entityType, count($data));
|
||||||
$maps = $this->createMaps();
|
$maps = $this->createMaps();
|
||||||
|
|
||||||
|
// Convert the data
|
||||||
|
$row_list = array();
|
||||||
foreach ($data as $row) {
|
foreach ($data as $row) {
|
||||||
if ($hasHeaders) {
|
if ($hasHeaders) {
|
||||||
$hasHeaders = false;
|
$hasHeaders = false;
|
||||||
@ -372,8 +420,21 @@ class ImportService
|
|||||||
}
|
}
|
||||||
|
|
||||||
$row = $this->convertToObject($entityType, $row, $map);
|
$row = $this->convertToObject($entityType, $row, $map);
|
||||||
$result = $this->saveData($source, $entityType, $row, $maps);
|
$data_index = $this->transformRow($source, $entityType, $row, $maps);
|
||||||
|
|
||||||
|
if ($data_index !== false) {
|
||||||
|
if($data_index !== true){// Wasn't merged with another row
|
||||||
|
$row_list[] = array('row'=>$row, 'data_index'=>$data_index);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$results[RESULT_FAILURE][] = $row;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save the data
|
||||||
|
foreach($row_list as $row_data){
|
||||||
|
$result = $this->saveData($source, $entityType, $row_data['row'], $row_data['data_index'], $maps);
|
||||||
|
|
||||||
if ($result) {
|
if ($result) {
|
||||||
$results[RESULT_SUCCESS][] = $result;
|
$results[RESULT_SUCCESS][] = $result;
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user