1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Export/CSV/DocumentExport.php

110 lines
3.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
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. 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;
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
{
2022-04-27 07:41:52 +02:00
private Company $company;
2022-04-08 03:54:17 +02:00
private $entity_transformer;
2023-03-01 13:19:15 +01:00
public $date_key = 'created_at';
2022-04-27 07:41:52 +02:00
2023-03-01 13:27:14 +01:00
public array $entity_keys = [
2022-04-08 03:54:17 +02:00
'record_type' => 'record_type',
2022-05-10 07:03:03 +02:00
// 'record_name' => 'record_name',
2022-04-08 03:54:17 +02:00
'name' => 'name',
'type' => 'type',
'created_at' => 'created_at',
];
private array $decorate_keys = [
];
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();
}
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();
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-08 03:54:17 +02:00
//insert the header
$this->csv->insertOne($this->buildHeader());
2022-04-27 07:41:52 +02:00
$query = Document::query()->where('company_id', $this->company->id);
$query = $this->addDateRange($query);
2022-04-08 03:54:17 +02:00
2022-04-27 07:41:52 +02:00
$query->cursor()
->each(function ($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
}
private function buildRow(Document $document) :array
{
$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);
}
private function decorateAdvancedFields(Document $document, array $entity) :array
{
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;
}
}