1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 16:13:20 +01:00
invoiceninja/app/Export/CSV/ContactExport.php

157 lines
4.8 KiB
PHP
Raw Normal View History

2022-04-08 03:02:31 +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-08 03:02:31 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Export\CSV;
2023-12-02 04:09:50 +01:00
use App\Utils\Ninja;
2022-04-08 03:02:31 +02:00
use App\Models\Client;
2023-12-02 04:09:50 +01:00
use League\Csv\Writer;
2022-04-08 03:02:31 +02:00
use App\Models\Company;
2023-12-02 04:09:50 +01:00
use App\Libraries\MultiDB;
use App\Models\ClientContact;
2022-04-08 03:02:31 +02:00
use Illuminate\Support\Facades\App;
2023-12-02 04:09:50 +01:00
use App\Export\Decorators\Decorator;
use App\Transformers\ClientTransformer;
use App\Transformers\ClientContactTransformer;
use Illuminate\Database\Eloquent\Builder;
2022-04-08 03:02:31 +02:00
2022-04-27 07:17:45 +02:00
class ContactExport extends BaseExport
2022-04-08 03:02:31 +02:00
{
2022-04-27 07:17:45 +02:00
private ClientTransformer $client_transformer;
2022-04-08 03:02:31 +02:00
2022-04-27 07:17:45 +02:00
private ClientContactTransformer $contact_transformer;
2023-12-02 04:09:50 +01:00
private Decorator $decorator;
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-08 03:02:31 +02:00
2022-04-27 07:17:45 +02:00
public function __construct(Company $company, array $input)
2022-04-08 03:02:31 +02:00
{
$this->company = $company;
2022-04-27 07:17:45 +02:00
$this->input = $input;
2022-04-08 03:02:31 +02:00
$this->client_transformer = new ClientTransformer();
$this->contact_transformer = new ClientContactTransformer();
2023-12-02 04:09:50 +01:00
$this->decorator = new Decorator();
2022-04-08 03:02:31 +02:00
}
2023-08-27 23:41:02 +02:00
private function init(): Builder
2022-04-08 03:02:31 +02:00
{
2023-08-27 23:41:02 +02:00
2022-04-08 03:02:31 +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 23:41:02 +02:00
$this->input['report_keys'] = array_values($this->client_report_keys);
}
2022-05-07 09:10:23 +02:00
2022-04-27 07:17:45 +02:00
$query = ClientContact::query()
->where('company_id', $this->company->id);
$query = $this->addDateRange($query);
2023-08-27 23:41:02 +02:00
return $query;
}
public function run()
{
$query = $this->init();
//load the CSV document from a string
$this->csv = Writer::createFromString();
//insert the header
$this->csv->insertOne($this->buildHeader());
$query->cursor()->each(function ($contact) {
$this->csv->insertOne($this->buildRow($contact));
2022-04-27 07:17:45 +02:00
});
2022-04-08 03:02:31 +02:00
return $this->csv->toString();
2022-04-08 03:02:31 +02:00
}
2023-08-27 23:41:02 +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 23:41:02 +02:00
$report = $query->cursor()
->map(function ($contact) {
2023-09-11 12:33:56 +02:00
$row = $this->buildRow($contact);
return $this->processMetaData($row, $contact);
2023-08-27 23:41:02 +02:00
})->toArray();
return array_merge(['columns' => $header], $report);
}
2022-04-08 03:02:31 +02:00
private function buildRow(ClientContact $contact) :array
{
$transformed_contact = false;
$transformed_client = $this->client_transformer->transform($contact->client);
$transformed_contact = $this->contact_transformer->transform($contact);
$entity = [];
foreach (array_values($this->input['report_keys']) as $key) {
$parts = explode('.', $key);
2022-04-08 03:02:31 +02:00
if ($parts[0] == 'client' && array_key_exists($parts[1], $transformed_client)) {
2023-08-28 00:00:43 +02:00
$entity[$key] = $transformed_client[$parts[1]];
} elseif ($parts[0] == 'contact' && array_key_exists($parts[1], $transformed_contact)) {
2023-08-28 00:00:43 +02:00
$entity[$key] = $transformed_contact[$parts[1]];
} else {
2023-12-02 04:09:50 +01:00
// nlog($key);
$entity[$key] = $this->decorator->transform($key, $contact);
// $entity[$key] = '';
2022-04-08 03:02:31 +02:00
}
}
2023-12-02 04:09:50 +01:00
return $entity;
// return $this->decorateAdvancedFields($contact->client, $entity);
2022-04-08 03:02:31 +02:00
}
private function decorateAdvancedFields(Client $client, array $entity) :array
{
if (in_array('client.country_id', $this->input['report_keys'])) {
$entity['country'] = $client->country ? ctrans("texts.country_{$client->country->name}") : '';
}
2022-04-08 03:02:31 +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-08 03:02:31 +02:00
if (in_array('client.currency', $this->input['report_keys'])) {
2022-05-10 05:59:39 +02:00
$entity['currency'] = $client->currency() ? $client->currency()->code : $client->company->currency()->code;
}
2022-04-08 03:02:31 +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-08 03:02:31 +02:00
return $entity;
}
}