2022-04-07 04:17:02 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2022-04-07 04:17:02 +02:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Export\CSV;
|
|
|
|
|
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Transformers\ClientContactTransformer;
|
|
|
|
use App\Transformers\ClientTransformer;
|
|
|
|
use App\Utils\Ninja;
|
|
|
|
use Illuminate\Support\Facades\App;
|
|
|
|
use League\Csv\Writer;
|
|
|
|
|
2022-04-27 07:17:45 +02:00
|
|
|
class ClientExport extends BaseExport
|
2022-04-07 04:17:02 +02:00
|
|
|
{
|
|
|
|
private $company;
|
|
|
|
|
|
|
|
private $client_transformer;
|
|
|
|
|
|
|
|
private $contact_transformer;
|
|
|
|
|
2023-03-08 21:49:18 +01:00
|
|
|
public Writer $csv;
|
|
|
|
|
2023-03-01 13:19:15 +01:00
|
|
|
public string $date_key = 'created_at';
|
2022-04-27 05:07:29 +02:00
|
|
|
|
2023-03-01 13:27:14 +01:00
|
|
|
public array $entity_keys = [
|
2022-04-07 04:17:02 +02:00
|
|
|
'address1' => 'client.address1',
|
|
|
|
'address2' => 'client.address2',
|
|
|
|
'balance' => 'client.balance',
|
|
|
|
'city' => 'client.city',
|
|
|
|
'country' => 'client.country_id',
|
|
|
|
'credit_balance' => 'client.credit_balance',
|
|
|
|
'custom_value1' => 'client.custom_value1',
|
|
|
|
'custom_value2' => 'client.custom_value2',
|
|
|
|
'custom_value3' => 'client.custom_value3',
|
|
|
|
'custom_value4' => 'client.custom_value4',
|
|
|
|
'id_number' => 'client.id_number',
|
|
|
|
'industry' => 'client.industry_id',
|
|
|
|
'last_login' => 'client.last_login',
|
|
|
|
'name' => 'client.name',
|
|
|
|
'number' => 'client.number',
|
|
|
|
'paid_to_date' => 'client.paid_to_date',
|
2022-05-10 05:39:08 +02:00
|
|
|
'client_phone' => 'client.phone',
|
2022-04-07 04:17:02 +02:00
|
|
|
'postal_code' => 'client.postal_code',
|
|
|
|
'private_notes' => 'client.private_notes',
|
|
|
|
'public_notes' => 'client.public_notes',
|
|
|
|
'shipping_address1' => 'client.shipping_address1',
|
|
|
|
'shipping_address2' => 'client.shipping_address2',
|
|
|
|
'shipping_city' => 'client.shipping_city',
|
|
|
|
'shipping_country' => 'client.shipping_country_id',
|
|
|
|
'shipping_postal_code' => 'client.shipping_postal_code',
|
|
|
|
'shipping_state' => 'client.shipping_state',
|
|
|
|
'state' => 'client.state',
|
|
|
|
'vat_number' => 'client.vat_number',
|
|
|
|
'website' => 'client.website',
|
|
|
|
'currency' => 'client.currency',
|
|
|
|
'first_name' => 'contact.first_name',
|
|
|
|
'last_name' => 'contact.last_name',
|
2022-05-10 05:39:08 +02:00
|
|
|
'contact_phone' => 'contact.phone',
|
2022-04-07 04:17:02 +02:00
|
|
|
'contact_custom_value1' => 'contact.custom_value1',
|
|
|
|
'contact_custom_value2' => 'contact.custom_value2',
|
|
|
|
'contact_custom_value3' => 'contact.custom_value3',
|
|
|
|
'contact_custom_value4' => 'contact.custom_value4',
|
|
|
|
'email' => 'contact.email',
|
2022-12-04 23:28:08 +01:00
|
|
|
'status' => 'status'
|
2022-04-07 04:17:02 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
private array $decorate_keys = [
|
|
|
|
'client.country_id',
|
|
|
|
'client.shipping_country_id',
|
|
|
|
'client.currency',
|
|
|
|
'client.industry',
|
|
|
|
];
|
|
|
|
|
2023-05-26 04:07:37 +02:00
|
|
|
public array $forced_keys = [
|
|
|
|
'status',
|
|
|
|
];
|
|
|
|
|
2022-04-27 05:07:29 +02:00
|
|
|
public function __construct(Company $company, array $input)
|
2022-04-07 04:17:02 +02:00
|
|
|
{
|
|
|
|
$this->company = $company;
|
2022-04-27 05:07:29 +02:00
|
|
|
$this->input = $input;
|
2022-04-07 04:17:02 +02:00
|
|
|
$this->client_transformer = new ClientTransformer();
|
|
|
|
$this->contact_transformer = new ClientContactTransformer();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
MultiDB::setDb($this->company->db);
|
|
|
|
App::forgetInstance('translator');
|
|
|
|
App::setLocale($this->company->locale());
|
|
|
|
$t = app('translator');
|
|
|
|
$t->replace(Ninja::transformTranslations($this->company->settings));
|
|
|
|
|
|
|
|
//load the CSV document from a string
|
|
|
|
$this->csv = Writer::createFromString();
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (count($this->input['report_keys']) == 0) {
|
2022-05-10 05:39:08 +02:00
|
|
|
$this->input['report_keys'] = array_values($this->entity_keys);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2023-05-26 04:07:37 +02:00
|
|
|
|
2022-04-07 04:17:02 +02:00
|
|
|
//insert the header
|
|
|
|
$this->csv->insertOne($this->buildHeader());
|
|
|
|
|
2022-04-27 05:07:29 +02:00
|
|
|
$query = Client::query()->with('contacts')
|
2022-04-27 08:29:38 +02:00
|
|
|
->withTrashed()
|
2022-04-27 05:07:29 +02:00
|
|
|
->where('company_id', $this->company->id)
|
2022-06-21 11:57:17 +02:00
|
|
|
->where('is_deleted', 0);
|
|
|
|
|
2022-04-27 05:07:29 +02:00
|
|
|
$query = $this->addDateRange($query);
|
2022-04-07 04:17:02 +02:00
|
|
|
|
2022-04-27 05:07:29 +02:00
|
|
|
$query->cursor()
|
2022-06-21 11:57:17 +02:00
|
|
|
->each(function ($client) {
|
|
|
|
$this->csv->insertOne($this->buildRow($client));
|
|
|
|
});
|
2022-04-07 04:17:02 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
return $this->csv->toString();
|
2022-04-07 04:17:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function buildRow(Client $client) :array
|
|
|
|
{
|
|
|
|
$transformed_contact = false;
|
|
|
|
|
|
|
|
$transformed_client = $this->client_transformer->transform($client);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($contact = $client->contacts()->first()) {
|
2022-04-07 04:17:02 +02:00
|
|
|
$transformed_contact = $this->contact_transformer->transform($contact);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-04-07 04:17:02 +02:00
|
|
|
|
|
|
|
$entity = [];
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
foreach (array_values($this->input['report_keys']) as $key) {
|
|
|
|
$parts = explode('.', $key);
|
2022-04-07 04:17:02 +02:00
|
|
|
|
2022-05-10 05:59:39 +02:00
|
|
|
$keyval = array_search($key, $this->entity_keys);
|
2022-04-07 04:17:02 +02:00
|
|
|
|
2023-07-05 08:56:07 +02:00
|
|
|
if (is_array($parts) && $parts[0] == 'client' && array_key_exists($parts[1], $transformed_client)) {
|
2022-05-10 05:39:08 +02:00
|
|
|
$entity[$keyval] = $transformed_client[$parts[1]];
|
2023-07-05 08:56:07 +02:00
|
|
|
} elseif (is_array($parts) && $parts[0] == 'contact' && array_key_exists($parts[1], $transformed_contact)) {
|
2022-05-10 05:39:08 +02:00
|
|
|
$entity[$keyval] = $transformed_contact[$parts[1]];
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
|
|
|
$entity[$keyval] = '';
|
2022-04-07 04:17:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->decorateAdvancedFields($client, $entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function decorateAdvancedFields(Client $client, array $entity) :array
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (in_array('client.country_id', $this->input['report_keys'])) {
|
|
|
|
$entity['country'] = $client->country ? ctrans("texts.country_{$client->country->name}") : '';
|
|
|
|
}
|
2022-04-07 04:17:02 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (in_array('client.shipping_country_id', $this->input['report_keys'])) {
|
|
|
|
$entity['shipping_country'] = $client->shipping_country ? ctrans("texts.country_{$client->shipping_country->name}") : '';
|
|
|
|
}
|
2022-04-07 04:17:02 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (in_array('client.currency', $this->input['report_keys'])) {
|
2022-05-10 05:39:08 +02:00
|
|
|
$entity['currency'] = $client->currency() ? $client->currency()->code : $client->company->currency()->code;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-04-07 04:17:02 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (in_array('client.industry_id', $this->input['report_keys'])) {
|
|
|
|
$entity['industry_id'] = $client->industry ? ctrans("texts.industry_{$client->industry->name}") : '';
|
|
|
|
}
|
2022-04-07 04:17:02 +02:00
|
|
|
|
2022-12-04 23:28:08 +01:00
|
|
|
$entity['status'] = $this->calculateStatus($client);
|
|
|
|
|
2022-04-07 04:17:02 +02:00
|
|
|
return $entity;
|
|
|
|
}
|
2022-12-04 23:28:08 +01:00
|
|
|
|
|
|
|
private function calculateStatus($client)
|
|
|
|
{
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($client->is_deleted) {
|
2022-12-04 23:28:08 +01:00
|
|
|
return ctrans('texts.deleted');
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-12-04 23:28:08 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($client->deleted_at) {
|
2022-12-04 23:28:08 +01:00
|
|
|
return ctrans('texts.arcvived');
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-12-04 23:28:08 +01:00
|
|
|
|
|
|
|
return ctrans('texts.active');
|
|
|
|
}
|
2022-04-07 04:17:02 +02:00
|
|
|
}
|