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

Multi contact parsing

This commit is contained in:
David Bomba 2024-10-10 07:54:08 +11:00
parent 4d83185ded
commit 6390771870
4 changed files with 76 additions and 41 deletions

View File

@ -192,6 +192,32 @@ class BaseImport
return $grouped;
}
public function groupClients($csvData, $key)
{
if(!$key || !isset($csvData[0][$key]))
return $csvData;
$grouped = [];
// Group by client name / id.
$grouped = [];
foreach ($csvData as $contact_item) {
if (empty($contact_item[$key])) {
$this->error_array['client'][] = [
'client' => $contact_item,
'error' => 'No client identifier',
];
} else {
$grouped[$contact_item[$key]][] = $contact_item;
}
}
return $grouped;
}
private function groupInvoices($csvData, $key)

View File

@ -119,6 +119,7 @@ class Csv extends BaseImport implements ImportInterface
$entity_type = 'client';
$data = $this->getCsvData($entity_type);
nlog($data);
if (is_array($data)) {
$data = $this->preTransformCsv($data, $entity_type);
@ -138,8 +139,16 @@ class Csv extends BaseImport implements ImportInterface
$this->transformer = new ClientTransformer($this->company);
nlog($data);
$data = $this->groupClients($data, 'client.name');
nlog($data);
$client_count = $this->ingest($data, $entity_type);
nlog($client_count);
$this->entity_count['clients'] = $client_count;
}

View File

@ -347,8 +347,6 @@ class BaseTransformer
*/
public function getFloat($data, $field)
{
nlog($data);
nlog($field);
if (array_key_exists($field, $data)) {

View File

@ -26,8 +26,10 @@ class ClientTransformer extends BaseTransformer
*
* @return array|bool
*/
public function transform($data)
public function transform($client_data)
{
$data = reset($client_data);
if (isset($data['client.name']) && $this->hasClient($data['client.name'])) {
throw new ImportException('Client already exists');
}
@ -35,7 +37,7 @@ class ClientTransformer extends BaseTransformer
$settings = ClientSettings::defaults();
$settings->currency_id = (string) $this->getCurrencyByCode($data);
return [
$client = [
'company_id' => $this->company->id,
'name' => $this->getString($data, 'client.name'),
'phone' => $this->getString($data, 'client.phone'),
@ -70,48 +72,11 @@ class ClientTransformer extends BaseTransformer
'custom_value2' => $this->getString($data, 'client.custom_value2'),
'custom_value3' => $this->getString($data, 'client.custom_value3'),
'custom_value4' => $this->getString($data, 'client.custom_value4'),
// 'balance' => preg_replace(
// '/[^0-9,.]+/',
// '',
// $this->getFloat($data, 'client.balance')
// ),
// 'paid_to_date' => preg_replace(
// '/[^0-9,.]+/',
// '',
// $this->getFloat($data, 'client.paid_to_date')
// ),
'paid_to_date' => 0,
'balance' => 0,
'credit_balance' => 0,
'settings' => $settings,
'client_hash' => Str::random(40),
'contacts' => [
[
'first_name' => $this->getString(
$data,
'contact.first_name'
),
'last_name' => $this->getString($data, 'contact.last_name'),
'email' => $this->getString($data, 'contact.email'),
'phone' => $this->getString($data, 'contact.phone'),
'custom_value1' => $this->getString(
$data,
'contact.custom_value1'
),
'custom_value2' => $this->getString(
$data,
'contact.custom_value2'
),
'custom_value3' => $this->getString(
$data,
'contact.custom_value3'
),
'custom_value4' => $this->getString(
$data,
'contact.custom_value4'
),
],
],
'country_id' => isset($data['client.country_id'])
? $this->getCountryId($data['client.country_id'])
: null,
@ -119,5 +84,42 @@ class ClientTransformer extends BaseTransformer
? $this->getCountryId($data['client.shipping_country'])
: null,
];
$contacts = [];
foreach ($client_data as $data) {
$contacts[] = [
'first_name' => $this->getString(
$data,
'contact.first_name'
),
'last_name' => $this->getString($data, 'contact.last_name'),
'email' => $this->getString($data, 'contact.email'),
'phone' => $this->getString($data, 'contact.phone'),
'custom_value1' => $this->getString(
$data,
'contact.custom_value1'
),
'custom_value2' => $this->getString(
$data,
'contact.custom_value2'
),
'custom_value3' => $this->getString(
$data,
'contact.custom_value3'
),
'custom_value4' => $this->getString(
$data,
'contact.custom_value4'
),
];
}
$client['contacts'] = $contacts;
nlog($client);
return $client;
}
}