1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 15:42:51 +01:00
invoiceninja/app/Export/CSV/ClientExport.php

263 lines
8.9 KiB
PHP
Raw Normal View History

2022-04-07 04:17:02 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. 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;
2023-11-26 08:41:42 +01:00
use App\Export\Decorators\Decorator;
use App\Libraries\MultiDB;
2022-04-07 04:17:02 +02:00
use App\Models\Client;
use App\Models\Company;
2023-11-26 08:41:42 +01:00
use App\Transformers\ClientContactTransformer;
2022-04-07 04:17:02 +02:00
use App\Transformers\ClientTransformer;
2023-11-26 08:41:42 +01:00
use App\Utils\Ninja;
use App\Utils\Number;
2023-08-27 14:09:26 +02:00
use Illuminate\Database\Eloquent\Builder;
2023-11-26 08:41:42 +01:00
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
2022-04-07 04:17:02 +02:00
2022-04-27 07:17:45 +02:00
class ClientExport extends BaseExport
2022-04-07 04:17:02 +02:00
{
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-11-25 12:58:27 +01:00
private Decorator $decorator;
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',
'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',
2023-09-28 22:39:28 +02:00
'status' => 'status',
'payment_balance' => 'client.payment_balance',
'credit_balance' => 'client.credit_balance',
2023-09-28 22:48:50 +02:00
'classification' => 'client.classification',
2022-04-07 04:17:02 +02:00
];
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();
2023-11-25 12:58:27 +01:00
$this->decorator = new Decorator();
2022-04-07 04:17:02 +02:00
}
2023-08-27 14:09:26 +02:00
public function returnJson()
{
$query = $this->init();
$headerdisplay = $this->buildHeader();
2023-10-26 04:57:44 +02:00
$header = collect($this->input['report_keys'])->map(function ($key, $value) use ($headerdisplay) {
return ['identifier' => $key, 'display_value' => $headerdisplay[$value]];
})->toArray();
2023-08-27 14:09:26 +02:00
$report = $query->cursor()
->map(function ($client) {
2024-08-22 08:45:06 +02:00
2024-07-11 07:53:18 +02:00
/** @var \App\Models\Client $client */
2023-09-11 12:33:56 +02:00
$row = $this->buildRow($client);
return $this->processMetaData($row, $client);
2023-08-27 14:09:26 +02:00
})->toArray();
2024-01-14 05:05:00 +01:00
2023-08-27 14:09:26 +02:00
return array_merge(['columns' => $header], $report);
}
public function init(): Builder
2022-04-07 04:17:02 +02:00
{
MultiDB::setDb($this->company->db);
App::forgetInstance('translator');
App::setLocale($this->company->locale());
$t = app('translator');
$t->replace(Ninja::transformTranslations($this->company->settings));
if (count($this->input['report_keys']) == 0) {
2023-08-27 14:09:26 +02:00
$this->input['report_keys'] = array_values($this->client_report_keys);
}
2022-04-07 04:17:02 +02:00
2022-04-27 05:07:29 +02:00
$query = Client::query()->with('contacts')
2022-04-27 08:29:38 +02:00
->withTrashed()
->where('company_id', $this->company->id);
2024-06-14 09:09:44 +02:00
if(!$this->input['include_deleted'] ?? false) {
$query->where('is_deleted', 0);
2024-06-14 09:09:44 +02:00
}
2024-08-22 08:45:06 +02:00
$query = $this->addDateRange($query, ' clients');
2022-04-07 04:17:02 +02:00
if($this->input['document_email_attachment'] ?? false) {
$this->queueDocuments($query);
}
2023-10-26 04:57:44 +02:00
return $query;
2024-01-14 05:05:00 +01:00
2023-08-27 14:09:26 +02:00
}
public function run()
{
$query = $this->init();
//load the CSV document from a string
$this->csv = Writer::createFromString();
2024-05-01 00:32:11 +02:00
\League\Csv\CharsetConverter::addTo($this->csv, 'UTF-8', 'UTF-8');
2024-01-14 05:05:00 +01:00
2023-08-27 14:09:26 +02:00
//insert the header
$this->csv->insertOne($this->buildHeader());
2022-04-27 05:07:29 +02:00
$query->cursor()
->each(function ($client) {
2024-08-22 08:45:06 +02:00
/** @var \App\Models\Client $client */
$this->csv->insertOne($this->buildRow($client));
});
2022-04-07 04:17:02 +02:00
return $this->csv->toString();
2022-04-07 04:17:02 +02:00
}
2024-01-14 05:05:00 +01:00
private function buildRow(Client $client): array
2022-04-07 04:17:02 +02:00
{
$transformed_contact = false;
$transformed_client = $this->client_transformer->transform($client);
2023-07-15 02:06:00 +02:00
$transformed_contact = [];
if ($contact = $client->contacts()->first()) {
2022-04-07 04:17:02 +02:00
$transformed_contact = $this->contact_transformer->transform($contact);
}
2022-04-07 04:17:02 +02:00
$entity = [];
foreach (array_values($this->input['report_keys']) as $key) {
$parts = explode('.', $key);
2024-01-14 05:05:00 +01:00
2023-07-05 08:56:07 +02:00
if (is_array($parts) && $parts[0] == 'client' && array_key_exists($parts[1], $transformed_client)) {
2023-07-14 11:08:55 +02:00
$entity[$key] = $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)) {
2023-07-14 11:08:55 +02:00
$entity[$key] = $transformed_contact[$parts[1]];
} else {
2023-12-02 04:09:50 +01:00
$entity[$key] = $this->decorator->transform($key, $client);
2022-04-07 04:17:02 +02:00
}
}
2023-12-12 00:46:23 +01:00
return $this->decorateAdvancedFields($client, $entity);
2022-04-07 04:17:02 +02:00
}
2023-09-11 12:33:56 +02:00
public function processMetaData(array $row, $resource): array
{
$clean_row = [];
foreach (array_values($this->input['report_keys']) as $key => $value) {
2024-01-14 05:05:00 +01:00
2023-09-11 12:33:56 +02:00
$report_keys = explode(".", $value);
2024-01-14 05:05:00 +01:00
2023-09-11 12:33:56 +02:00
$column_key = $value;
$clean_row[$key]['entity'] = $report_keys[0];
$clean_row[$key]['id'] = $report_keys[1] ?? $report_keys[0];
$clean_row[$key]['hashed_id'] = $report_keys[0] == 'client' ? null : $resource->{$report_keys[0]}->hashed_id ?? null;
$clean_row[$key]['value'] = $row[$column_key];
2023-10-20 18:57:13 +02:00
$clean_row[$key]['identifier'] = $value;
2023-09-11 12:33:56 +02:00
2023-10-26 04:57:44 +02:00
if(in_array($clean_row[$key]['id'], ['paid_to_date', 'balance', 'credit_balance','payment_balance'])) {
2023-09-11 12:33:56 +02:00
$clean_row[$key]['display_value'] = Number::formatMoney($row[$column_key], $resource);
2023-10-26 04:57:44 +02:00
} else {
2023-09-11 12:33:56 +02:00
$clean_row[$key]['display_value'] = $row[$column_key];
2023-10-26 04:57:44 +02:00
}
2023-09-11 12:33:56 +02:00
}
return $clean_row;
}
2024-01-14 05:05:00 +01:00
private function decorateAdvancedFields(Client $client, array $entity): array
2022-04-07 04:17:02 +02:00
{
2023-07-14 11:08:55 +02:00
if (in_array('client.user', $this->input['report_keys'])) {
$entity['client.user'] = $client->user->present()->name();
}
if (in_array('client.assigned_user', $this->input['report_keys'])) {
$entity['client.assigned_user'] = $client->assigned_user ? $client->user->present()->name() : '';
}
2023-09-28 22:48:50 +02:00
if (in_array('client.classification', $this->input['report_keys']) && isset($client->classification)) {
$entity['client.classification'] = ctrans("texts.{$client->classification}") ?? '';
}
2024-04-23 04:45:43 +02:00
if (in_array('client.industry_id', $this->input['report_keys']) && isset($client->industry_id)) {
$entity['client.industry_id'] = ctrans("texts.industry_{$client->industry->name}") ?? '';
}
if (in_array('client.country_id', $this->input['report_keys']) && isset($client->country_id)) {
2024-04-24 01:21:00 +02:00
$entity['client.country_id'] = $client->country ? $client->country->full_name : '';
2024-04-23 04:45:43 +02:00
}
if (in_array('client.shipping_country_id', $this->input['report_keys']) && isset($client->shipping_country_id)) {
2024-04-24 01:21:00 +02:00
$entity['client.shipping_country_id'] = $client->shipping_country ? $client->shipping_country->full_name : '';
2024-04-23 04:45:43 +02:00
}
2022-04-07 04:17:02 +02:00
return $entity;
}
2022-12-04 23:28:08 +01:00
2024-06-16 06:58:03 +02:00
// private function calculateStatus($client)
// {
// if ($client->is_deleted) {
// return ctrans('texts.deleted');
// }
// if ($client->deleted_at) {
// return ctrans('texts.archived');
// }
// return ctrans('texts.active');
// }
2022-04-07 04:17:02 +02:00
}