2022-04-08 03:54:17 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2022-04-27 05:20:41 +02:00
|
|
|
* @copyright Copyright (c) 2022. 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\Client;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Credit;
|
|
|
|
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
|
|
|
|
2022-04-27 07:41:52 +02:00
|
|
|
protected array $input;
|
2022-04-08 03:54:17 +02:00
|
|
|
|
|
|
|
private $entity_transformer;
|
|
|
|
|
2022-04-27 07:41:52 +02:00
|
|
|
protected $date_key = 'created_at';
|
|
|
|
|
2022-04-27 10:05:54 +02:00
|
|
|
protected 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();
|
|
|
|
|
2022-05-07 09:10:23 +02:00
|
|
|
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){
|
2022-04-08 03:54:17 +02:00
|
|
|
|
2022-04-27 07:41:52 +02:00
|
|
|
$this->csv->insertOne($this->buildRow($entity));
|
2022-04-08 03:54:17 +02:00
|
|
|
|
2022-04-27 07:41:52 +02:00
|
|
|
});
|
2022-04-08 03:54:17 +02:00
|
|
|
|
|
|
|
return $this->csv->toString();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildRow(Document $document) :array
|
|
|
|
{
|
|
|
|
|
|
|
|
$transformed_entity = $this->entity_transformer->transform($document);
|
|
|
|
|
|
|
|
$entity = [];
|
|
|
|
|
2022-04-27 07:41:52 +02:00
|
|
|
foreach(array_values($this->input['report_keys']) as $key){
|
2022-04-08 03:54:17 +02:00
|
|
|
|
2022-05-10 07:03:03 +02:00
|
|
|
$keyval = array_search($key, $this->entity_keys);
|
|
|
|
|
|
|
|
if(array_key_exists($key, $transformed_entity))
|
|
|
|
$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
|
|
|
|
{
|
|
|
|
|
2022-05-10 07:03:03 +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-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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|