Fixes for zoho imports

This commit is contained in:
David Bomba 2023-05-16 17:41:54 +10:00
parent 4767c1a14a
commit 9668f6ceb7
2 changed files with 79 additions and 3 deletions

View File

@ -28,7 +28,7 @@ class ClientTransformer extends BaseTransformer
public function transform($data) public function transform($data)
{ {
if (isset($data['Company Name']) && $this->hasClient($data['Company Name'])) { if (isset($data['Company Name']) && $this->hasClient($data['Company Name'])) {
throw new ImportException('Client already exists'); throw new ImportException('Client already exists => '. $data['Company Name']);
} }
$settings = new \stdClass; $settings = new \stdClass;
@ -40,7 +40,7 @@ class ClientTransformer extends BaseTransformer
$client_id_proxy = array_key_exists('Customer ID', $data) ? 'Customer ID' : 'Primary Contact ID'; $client_id_proxy = array_key_exists('Customer ID', $data) ? 'Customer ID' : 'Primary Contact ID';
return [ $data = [
'company_id' => $this->company->id, 'company_id' => $this->company->id,
'name' => $this->getString($data, 'Display Name'), 'name' => $this->getString($data, 'Display Name'),
'phone' => $this->getString($data, 'Phone'), 'phone' => $this->getString($data, 'Phone'),
@ -72,5 +72,7 @@ class ClientTransformer extends BaseTransformer
], ],
], ],
]; ];
return $data;
} }
} }

View File

@ -40,7 +40,8 @@ class InvoiceTransformer extends BaseTransformer
$transformed = [ $transformed = [
'company_id' => $this->company->id, 'company_id' => $this->company->id,
'client_id' => $this->getClient($this->getString($invoice_data, 'Customer ID'), $this->getString($invoice_data, 'Primary Contact EmailID')), // 'client_id' => $this->getClient($this->getString($invoice_data, 'Customer ID'), $this->getString($invoice_data, 'Primary Contact EmailID')),
'client_id' => $this->harvestClient($invoice_data),
'number' => $this->getString($invoice_data, 'Invoice Number'), 'number' => $this->getString($invoice_data, 'Invoice Number'),
'date' => isset($invoice_data['Invoice Date']) ? date('Y-m-d', strtotime($invoice_data['Invoice Date'])) : null, 'date' => isset($invoice_data['Invoice Date']) ? date('Y-m-d', strtotime($invoice_data['Invoice Date'])) : null,
'due_date' => isset($invoice_data['Due Date']) ? date('Y-m-d', strtotime($invoice_data['Due Date'])) : null, 'due_date' => isset($invoice_data['Due Date']) ? date('Y-m-d', strtotime($invoice_data['Due Date'])) : null,
@ -80,4 +81,77 @@ class InvoiceTransformer extends BaseTransformer
return $transformed; return $transformed;
} }
private function harvestClient($invoice_data)
{
$client_email = $this->getString($invoice_data, 'Primary Contact EmailID');
if (strlen($client_email) > 2) {
$contacts = \App\Models\ClientContact::whereHas('client', function ($query) {
$query->where('is_deleted', false);
})
->where('company_id', $this->company->id)
->where('email', $client_email);
if ($contacts->count() >= 1) {
return $contacts->first()->client_id;
}
}
$client_name = $this->getString($invoice_data, 'Customer Name');
if(strlen($client_name) >= 2) {
$client_name_search = \App\Models\Client::where('company_id', $this->company->id)
->where('is_deleted', false)
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $client_name)),
]);
if ($client_name_search->count() >= 1) {
return $client_name_search->first()->id;
}
}
$customer_id = $this->getString($invoice_data, 'Customer ID');
$client_id_search = \App\Models\Client::where('company_id', $this->company->id)
->where('is_deleted', false)
->where('id_number', trim($customer_id));
if ($client_id_search->count() >= 1) {
return $client_id_search->first()->id;
}
$client_repository = app()->make(\App\Repositories\ClientRepository::class);
$client_repository->import_mode = true;
$client = $client_repository->save(
[
'name' => $client_name,
'contacts' => [
[
'first_name' => $client_name,
'email' => $client_email,
],
],
'address1' => $this->getString($invoice_data, 'Billing Address'),
'city' => $this->getString($invoice_data, 'Billing City'),
'state' => $this->getString($invoice_data, 'Billing State'),
'postal_code' => $this->getString($invoice_data, 'Billing Code'),
'country_id' => $this->getCountryId($this->getString($invoice_data, 'Billing Country')),
],
\App\Factory\ClientFactory::create(
$this->company->id,
$this->company->owner()->id
)
);
$client_repository = null;
return $client->id;
}
} }