1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00

Added getString helper to CSV import

This commit is contained in:
Hillel Coren 2015-12-31 17:09:45 +02:00
parent db2e37d31b
commit 06c1534093
4 changed files with 47 additions and 40 deletions

View File

@ -19,6 +19,11 @@ class BaseTransformer extends TransformerAbstract
return isset($this->maps[ENTITY_CLIENT][$name]);
}
protected function getString($data, $field)
{
return (isset($data->$field) && $data->$field) ? $data->$field : null;
}
protected function getClientId($name)
{
$name = strtolower($name);

View File

@ -13,19 +13,19 @@ class ClientTransformer extends BaseTransformer
return new Item($data, function ($data) {
return [
'name' => isset($data->name) ? $data->name : null,
'work_phone' => isset($data->work_phone) ? $data->work_phone : null,
'address1' => isset($data->address1) ? $data->address1 : null,
'city' => isset($data->city) ? $data->city : null,
'state' => isset($data->state) ? $data->state : null,
'postal_code' => isset($data->postal_code) ? $data->postal_code : null,
'private_notes' => isset($data->notes) ? $data->notes : null,
'name' => $this->getString($data, 'name'),
'work_phone' => $this->getString($data, 'work_phone'),
'address1' => $this->getString($data, 'address1'),
'city' => $this->getString($data, 'city'),
'state' => $this->getString($data, 'state'),
'postal_code' => $this->getString($data, 'postal_code'),
'private_notes' => $this->getString($data, 'notes'),
'contacts' => [
[
'first_name' => isset($data->first_name) ? $data->first_name : null,
'last_name' => isset($data->last_name) ? $data->last_name : null,
'email' => isset($data->email) ? $data->email : null,
'phone' => isset($data->phone) ? $data->phone : null,
'first_name' => $this->getString($data, 'first_name'),
'last_name' => $this->getString($data, 'last_name'),
'email' => $this->getString($data, 'email'),
'phone' => $this->getString($data, 'phone'),
],
],
'country_id' => isset($data->country) ? $this->getCountryId($data->country) : null,

View File

@ -20,14 +20,14 @@ class InvoiceTransformer extends BaseTransformer
'client_id' => $this->getClientId($data->name),
'invoice_number' => isset($data->invoice_number) ? $this->getInvoiceNumber($data->invoice_number) : null,
'paid' => isset($data->paid) ? (float) $data->paid : null,
'po_number' => isset($data->po_number) ? $data->po_number : null,
'terms' => isset($data->terms) ? $data->terms : null,
'public_notes' => isset($data->notes) ? $data->notes : null,
'po_number' => $this->getString($data, 'po_number'),
'terms' => $this->getString($data, 'terms'),
'public_notes' => $this->getString($data, 'public_notes'),
'invoice_date_sql' => isset($data->invoice_date) ? $data->invoice_date : null,
'invoice_items' => [
[
'product_key' => '',
'notes' => isset($data->notes) ? $data->notes : null,
'notes' => $this->getString($data, 'notes'),
'cost' => isset($data->amount) ? (float) $data->amount : null,
'qty' => 1,
]

View File

@ -64,7 +64,7 @@ class ImportService
foreach ($files as $entityType => $file) {
$results[$entityType] = $this->execute($source, $entityType, $file);
}
return $results;
}
@ -76,26 +76,27 @@ class ImportService
];
// Convert the data
$row_list = array();
$row_list = array();
$maps = $this->createMaps();
Excel::load($file, function ($reader) use ($source, $entityType, $maps, &$row_list, &$results) {
$this->checkData($entityType, count($reader->all()));
$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);
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){
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;
@ -124,29 +125,29 @@ class ImportService
$invoice = Invoice::createNew();
$data['invoice_number'] = $account->getNextInvoiceNumber($invoice);
}
if ($this->validate($source, $data, $entityType) !== true) {
return false;
}
if($entityType == ENTITY_INVOICE){
if(empty($this->processedRows[$data['invoice_number']])){
if ($entityType == ENTITY_INVOICE) {
if (empty($this->processedRows[$data['invoice_number']])) {
$this->processedRows[$data['invoice_number']] = $data;
}
else{
} 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;
}
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];
@ -421,20 +422,21 @@ class ImportService
$row = $this->convertToObject($entityType, $row, $map);
$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);
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){
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 {