1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Export/CSV/InvoiceExport.php

156 lines
4.7 KiB
PHP
Raw Normal View History

2020-12-14 05:59:15 +01: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)
2020-12-14 05:59:15 +01:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-12-14 05:59:15 +01:00
*/
2020-12-14 11:43:07 +01:00
namespace App\Export\CSV;
2020-12-14 05:59:15 +01:00
2023-04-24 06:55:56 +02:00
use App\Utils\Ninja;
use App\Utils\Number;
use League\Csv\Writer;
2020-12-14 06:03:23 +01:00
use App\Models\Company;
2022-04-08 06:47:10 +02:00
use App\Models\Invoice;
2023-04-24 06:55:56 +02:00
use App\Libraries\MultiDB;
use App\Export\CSV\BaseExport;
2022-04-08 06:47:10 +02:00
use Illuminate\Support\Facades\App;
2023-04-24 06:55:56 +02:00
use App\Transformers\InvoiceTransformer;
2023-09-12 03:22:59 +02:00
use Illuminate\Database\Eloquent\Builder;
2020-12-14 06:03:23 +01:00
2022-04-27 08:29:38 +02:00
class InvoiceExport extends BaseExport
2020-12-14 05:59:15 +01:00
{
2022-04-08 06:47:10 +02:00
private $invoice_transformer;
2023-03-01 13:19:15 +01:00
public string $date_key = 'date';
2022-04-27 08:29:38 +02:00
2023-03-08 21:49:18 +01:00
public Writer $csv;
2022-04-27 08:29:38 +02:00
public function __construct(Company $company, array $input)
2020-12-16 12:52:40 +01:00
{
$this->company = $company;
2022-04-27 08:29:38 +02:00
$this->input = $input;
2022-04-08 06:54:59 +02:00
$this->invoice_transformer = new InvoiceTransformer();
2020-12-16 12:52:40 +01:00
}
2020-12-14 06:03:23 +01:00
2023-08-28 07:13:55 +02:00
public function init(): Builder
2020-12-16 12:52:40 +01:00
{
2023-08-28 07:13:55 +02:00
2022-04-08 06:47:10 +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-28 07:13:55 +02:00
$this->input['report_keys'] = array_values($this->invoice_report_keys);
}
2023-10-18 06:22:38 +02:00
$this->input['report_keys'] = array_merge($this->input['report_keys'], array_diff($this->forced_client_fields, $this->input['report_keys']));
2022-04-27 08:29:38 +02:00
$query = Invoice::query()
->withTrashed()
2022-05-10 08:50:30 +02:00
->with('client')
->where('company_id', $this->company->id)
->where('is_deleted', 0);
2022-04-27 08:29:38 +02:00
$query = $this->addDateRange($query);
2022-04-08 06:47:10 +02:00
2023-08-28 07:13:55 +02:00
if(isset($this->input['status'])) {
2023-04-24 06:55:56 +02:00
$query = $this->addInvoiceStatusFilter($query, $this->input['status']);
}
2023-08-28 07:13:55 +02:00
return $query;
}
public function returnJson()
{
$query = $this->init();
$headerdisplay = $this->buildHeader();
$header = collect($this->input['report_keys'])->map(function ($key, $value) use($headerdisplay){
2023-10-06 18:43:02 +02:00
return ['identifier' => $key, 'display_value' => $headerdisplay[$value]];
2023-08-28 07:13:55 +02:00
})->toArray();
$report = $query->cursor()
->map(function ($resource) {
2023-09-11 12:33:56 +02:00
$row = $this->buildRow($resource);
return $this->processMetaData($row, $resource);
2023-08-28 07:13:55 +02:00
})->toArray();
return array_merge(['columns' => $header], $report);
}
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());
2022-04-27 08:29:38 +02:00
$query->cursor()
->each(function ($invoice) {
$this->csv->insertOne($this->buildRow($invoice));
});
2022-04-08 06:47:10 +02:00
return $this->csv->toString();
2022-04-08 06:47:10 +02:00
}
2022-04-08 06:54:59 +02:00
private function buildRow(Invoice $invoice) :array
2022-04-08 06:47:10 +02:00
{
$transformed_invoice = $this->invoice_transformer->transform($invoice);
$entity = [];
foreach (array_values($this->input['report_keys']) as $key) {
2022-05-10 06:51:39 +02:00
2023-08-28 07:13:55 +02:00
$parts = explode('.', $key);
2023-07-05 08:56:07 +02:00
2023-08-28 07:13:55 +02:00
if (is_array($parts) && $parts[0] == 'invoice' && array_key_exists($parts[1], $transformed_invoice)) {
$entity[$key] = $transformed_invoice[$parts[1]];
} else {
$entity[$key] = $this->resolveKey($key, $invoice, $this->invoice_transformer);
2023-07-05 08:56:07 +02:00
}
2022-04-08 06:47:10 +02:00
}
return $this->decorateAdvancedFields($invoice, $entity);
}
2022-04-08 06:54:59 +02:00
private function decorateAdvancedFields(Invoice $invoice, array $entity) :array
2022-04-08 06:47:10 +02:00
{
2023-08-28 14:44:04 +02:00
2023-08-28 07:13:55 +02:00
if (in_array('invoice.country_id', $this->input['report_keys'])) {
$entity['invoice.country_id'] = $invoice->client->country ? ctrans("texts.country_{$invoice->client->country->name}") : '';
}
2022-05-10 06:51:39 +02:00
2023-08-28 07:13:55 +02:00
if (in_array('invoice.currency_id', $this->input['report_keys'])) {
$entity['invoice.currency_id'] = $invoice->client->currency() ? $invoice->client->currency()->code : $invoice->company->currency()->code;
}
2022-04-08 06:47:10 +02:00
2023-08-28 07:13:55 +02:00
if (in_array('invoice.client_id', $this->input['report_keys'])) {
2023-08-28 12:24:48 +02:00
$entity['invoice.client_id'] = $invoice->client->present()->name();
}
2022-04-08 06:47:10 +02:00
2023-08-28 07:13:55 +02:00
if (in_array('invoice.status', $this->input['report_keys'])) {
2023-08-28 12:24:48 +02:00
$entity['invoice.status'] = $invoice->stringStatus($invoice->status_id);
}
2023-10-17 12:38:35 +02:00
if (in_array('invoice.recurring_id', $this->input['report_keys'])) {
$entity['invoice.recurring_id'] = $invoice->recurring_invoice->number ?? '';
}
2023-04-24 06:55:56 +02:00
2022-04-08 06:47:10 +02:00
return $entity;
}
2020-12-16 12:52:40 +01:00
}