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

175 lines
5.3 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
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. 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')
2024-06-14 09:09:44 +02:00
->whereHas('client', function ($q) {
2024-04-25 10:28:43 +02:00
$q->where('is_deleted', false);
})
->where('company_id', $this->company->id);
2024-05-29 15:19:03 +02:00
2024-06-14 09:09:44 +02:00
if(!$this->input['include_deleted'] ?? false) {
$query->where('is_deleted', 0);
}
2022-04-27 08:29:38 +02:00
$query = $this->addDateRange($query);
2022-04-08 06:47:10 +02:00
2024-03-26 04:12:32 +01:00
$clients = &$this->input['client_id'];
if($clients) {
$query = $this->addClientFilter($query, $clients);
}
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();
2024-05-01 00:30:41 +02:00
\League\Csv\CharsetConverter::addTo($this->csv, 'UTF-8', 'UTF-8');
2023-08-28 07:13:55 +02:00
//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.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
}