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

173 lines
5.5 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-12-02 04:09:50 +01:00
use App\Export\Decorators\Decorator;
2023-10-26 04:57:44 +02:00
use App\Libraries\MultiDB;
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\Transformers\InvoiceTransformer;
2023-10-26 04:57:44 +02:00
use App\Utils\Ninja;
2023-09-12 03:22:59 +02:00
use Illuminate\Database\Eloquent\Builder;
2023-10-26 04:57:44 +02:00
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
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;
2023-12-02 04:09:50 +01:00
private Decorator $decorator;
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();
2023-12-02 04:09:50 +01:00
$this->decorator = new Decorator();
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)
2024-03-05 03:58:26 +01:00
->where('is_deleted', $this->input['include_deleted']);
2022-04-27 08:29:38 +02:00
$query = $this->addDateRange($query);
2022-04-08 06:47:10 +02:00
if($this->input['status'] ?? false) {
2023-04-24 06:55:56 +02:00
$query = $this->addInvoiceStatusFilter($query, $this->input['status']);
}
if($this->input['document_email_attachment'] ?? false) {
$this->queueDocuments($query);
}
2023-08-28 07:13:55 +02:00
return $query;
}
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-28 07:13:55 +02:00
$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();
2024-01-14 05:05:00 +01:00
2023-08-28 07:13:55 +02:00
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
}
2024-01-14 05:05:00 +01: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 {
2023-12-02 04:09:50 +01:00
$entity[$key] = $this->decorator->transform($key, $invoice);
2023-07-05 08:56:07 +02:00
}
2022-04-08 06:47:10 +02:00
}
2024-01-14 05:05:00 +01:00
2023-12-12 00:46:23 +01:00
return $this->decorateAdvancedFields($invoice, $entity);
2022-04-08 06:47:10 +02:00
}
2024-01-14 05:05:00 +01:00
private function decorateAdvancedFields(Invoice $invoice, array $entity): array
2022-04-08 06:47:10 +02:00
{
2024-01-14 05:05:00 +01:00
2023-12-12 00:46:23 +01: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-12-12 00:46:23 +01: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-12-12 00:46:23 +01:00
// if (in_array('invoice.client_id', $this->input['report_keys'])) {
// $entity['invoice.client_id'] = $invoice->client->present()->name();
// }
2022-04-08 06:47:10 +02:00
2023-12-12 00:46:23 +01:00
// if (in_array('invoice.status', $this->input['report_keys'])) {
// $entity['invoice.status'] = $invoice->stringStatus($invoice->status_id);
// }
2023-10-17 12:38:35 +02:00
2024-03-05 06:30:54 +01:00
if (in_array('invoice.recurring_id', $this->input['report_keys'])) {
$entity['invoice.recurring_id'] = $invoice->recurring_invoice->number ?? '';
}
2023-10-17 12:38:35 +02:00
2023-11-06 22:23:51 +01:00
if (in_array('invoice.auto_bill_enabled', $this->input['report_keys'])) {
$entity['invoice.auto_bill_enabled'] = $invoice->auto_bill_enabled ? ctrans('texts.yes') : ctrans('texts.no');
}
if (in_array('invoice.assigned_user_id', $this->input['report_keys'])) {
2024-01-14 05:05:00 +01:00
$entity['invoice.assigned_user_id'] = $invoice->assigned_user ? $invoice->assigned_user->present()->name() : '';
}
2024-01-14 05:05:00 +01:00
if (in_array('invoice.user_id', $this->input['report_keys'])) {
2024-01-14 05:05:00 +01:00
$entity['invoice.user_id'] = $invoice->user ? $invoice->user->present()->name() : '';
}
2022-04-08 06:47:10 +02:00
return $entity;
}
2020-12-16 12:52:40 +01:00
}