1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 23:52:33 +01:00
invoiceninja/app/Export/CSV/DocumentExport.php

142 lines
4.0 KiB
PHP
Raw Normal View History

2022-04-08 03:54:17 +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-08 03:54:17 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Export\CSV;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Document;
use App\Transformers\DocumentTransformer;
use App\Utils\Ninja;
2023-09-12 03:01:14 +02:00
use Illuminate\Database\Eloquent\Builder;
2022-04-08 03:54:17 +02:00
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
2022-04-27 07:41:52 +02:00
class DocumentExport extends BaseExport
2022-04-08 03:54:17 +02:00
{
private $entity_transformer;
2023-03-08 07:56:34 +01:00
public string $date_key = 'created_at';
2022-04-27 07:41:52 +02:00
2023-03-08 21:49:18 +01:00
public Writer $csv;
2023-03-01 13:27:14 +01:00
public array $entity_keys = [
2022-04-08 03:54:17 +02:00
'record_type' => 'record_type',
'name' => 'name',
'type' => 'type',
'created_at' => 'created_at',
];
2022-04-27 07:41:52 +02:00
public function __construct(Company $company, array $input)
2022-04-08 03:54:17 +02:00
{
$this->company = $company;
2022-04-27 07:41:52 +02:00
$this->input = $input;
2022-04-08 03:54:17 +02:00
$this->entity_transformer = new DocumentTransformer();
}
2023-08-28 00:00:43 +02:00
public function returnJson()
2022-04-08 03:54:17 +02:00
{
2023-08-28 00:00:43 +02:00
$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-28 00:00:43 +02:00
$report = $query->cursor()
->map(function ($document) {
2024-07-14 13:28:54 +02:00
/** @var \App\Models\Document $document */
2023-08-28 00:00:43 +02:00
$row = $this->buildRow($document);
2023-09-11 12:33:56 +02:00
return $this->processMetaData($row, $document);
2023-08-28 00:00:43 +02:00
})->toArray();
2024-01-14 05:05:00 +01:00
2023-08-28 00:00:43 +02:00
return array_merge(['columns' => $header], $report);
}
private function init(): Builder
{
2022-04-08 03:54:17 +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) {
2022-05-10 07:03:03 +02:00
$this->input['report_keys'] = array_values($this->entity_keys);
}
2022-05-07 09:10:23 +02:00
2022-04-27 07:41:52 +02:00
$query = Document::query()->where('company_id', $this->company->id);
$query = $this->addDateRange($query, 'documents');
2024-02-13 05:25:18 +01:00
if($this->input['document_email_attachment'] ?? false) {
$this->queueDocuments($query);
}
2022-04-08 03:54:17 +02:00
2023-08-28 00:00:43 +02:00
return $query;
}
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');
2023-08-28 00:00:43 +02:00
//insert the header
$this->csv->insertOne($this->buildHeader());
2022-04-27 07:41:52 +02:00
$query->cursor()
->each(function ($entity) {
2024-07-14 13:28:54 +02:00
/** @var mixed $entity */
$this->csv->insertOne($this->buildRow($entity));
});
2022-04-08 03:54:17 +02:00
return $this->csv->toString();
2022-04-08 03:54:17 +02:00
}
2024-01-14 05:05:00 +01:00
private function buildRow(Document $document): array
2022-04-08 03:54:17 +02:00
{
$transformed_entity = $this->entity_transformer->transform($document);
$entity = [];
foreach (array_values($this->input['report_keys']) as $key) {
2022-05-10 07:03:03 +02:00
$keyval = array_search($key, $this->entity_keys);
if (array_key_exists($key, $transformed_entity)) {
2022-05-10 07:03:03 +02:00
$entity[$keyval] = $transformed_entity[$key];
} else {
$entity[$keyval] = '';
}
2022-04-08 03:54:17 +02:00
}
return $this->decorateAdvancedFields($document, $entity);
}
2024-01-14 05:05:00 +01:00
private function decorateAdvancedFields(Document $document, array $entity): array
2022-04-08 03:54:17 +02:00
{
if (in_array('record_type', $this->input['report_keys'])) {
2022-04-08 04:04:33 +02:00
$entity['record_type'] = class_basename($document->documentable);
}
2022-04-08 04:04:33 +02:00
2022-05-10 07:03:03 +02:00
// if(in_array('record_name', $this->input['report_keys']))
// $entity['record_name'] = $document->hashed_id;
2022-04-08 03:54:17 +02:00
return $entity;
}
}