2022-04-27 09:21:55 +02: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)
|
2022-04-27 09:21:55 +02:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Export\CSV;
|
|
|
|
|
2023-12-02 04:09:50 +01:00
|
|
|
use App\Export\Decorators\Decorator;
|
2022-04-27 09:21:55 +02:00
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\RecurringInvoice;
|
|
|
|
use App\Transformers\RecurringInvoiceTransformer;
|
|
|
|
use App\Utils\Ninja;
|
2023-09-12 03:22:59 +02:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2022-04-27 09:21:55 +02:00
|
|
|
use Illuminate\Support\Facades\App;
|
|
|
|
use League\Csv\Writer;
|
|
|
|
|
|
|
|
class RecurringInvoiceExport extends BaseExport
|
|
|
|
{
|
|
|
|
private $invoice_transformer;
|
|
|
|
|
2023-03-01 13:19:15 +01:00
|
|
|
public string $date_key = 'date';
|
2022-04-27 09:21:55 +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 09:21:55 +02:00
|
|
|
public function __construct(Company $company, array $input)
|
|
|
|
{
|
|
|
|
$this->company = $company;
|
|
|
|
$this->input = $input;
|
|
|
|
$this->invoice_transformer = new RecurringInvoiceTransformer();
|
2023-12-02 04:09:50 +01:00
|
|
|
$this->decorator = new Decorator();
|
2022-04-27 09:21:55 +02:00
|
|
|
}
|
|
|
|
|
2023-09-12 03:22:59 +02:00
|
|
|
public function init(): Builder
|
2022-04-27 09:21:55 +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));
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (count($this->input['report_keys']) == 0) {
|
2023-09-12 03:22:59 +02:00
|
|
|
$this->input['report_keys'] = array_values($this->recurring_invoice_report_keys);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-05-07 09:10:23 +02:00
|
|
|
|
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 09:21:55 +02:00
|
|
|
$query = RecurringInvoice::query()
|
|
|
|
->withTrashed()
|
2023-09-12 03:22:59 +02:00
|
|
|
->with('client')
|
2024-06-14 09:09:44 +02:00
|
|
|
->whereHas('client', function ($q) {
|
2024-04-25 12:07:42 +02:00
|
|
|
$q->where('is_deleted', false);
|
|
|
|
})
|
2024-05-29 01:05:42 +02:00
|
|
|
->where('company_id', $this->company->id);
|
2024-06-14 09:09:44 +02:00
|
|
|
|
|
|
|
if(!$this->input['include_deleted'] ?? false) {
|
2024-05-29 01:05:42 +02:00
|
|
|
$query->where('is_deleted', 0);
|
|
|
|
}
|
2022-04-27 09:21:55 +02:00
|
|
|
|
2024-06-23 23:56:13 +02:00
|
|
|
$query = $this->addDateRange($query, 'recurring_invoices');
|
2022-04-27 09:21:55 +02:00
|
|
|
|
2024-03-26 04:12:32 +01:00
|
|
|
$clients = &$this->input['client_id'];
|
|
|
|
|
|
|
|
if($clients) {
|
|
|
|
$query = $this->addClientFilter($query, $clients);
|
|
|
|
}
|
|
|
|
|
2024-03-05 03:58:26 +01:00
|
|
|
$query = $this->addRecurringInvoiceStatusFilter($query, $this->input['status'] ?? '');
|
|
|
|
|
2023-09-12 03:22:59 +02:00
|
|
|
return $query;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
|
|
|
|
$query = $this->init();
|
|
|
|
|
|
|
|
//load the CSV document from a string
|
|
|
|
$this->csv = Writer::createFromString();
|
2024-05-01 00:32:11 +02:00
|
|
|
\League\Csv\CharsetConverter::addTo($this->csv, 'UTF-8', 'UTF-8');
|
2023-09-12 03:22:59 +02:00
|
|
|
|
|
|
|
//insert the header
|
|
|
|
$this->csv->insertOne($this->buildHeader());
|
|
|
|
|
2022-04-27 09:21:55 +02:00
|
|
|
$query->cursor()
|
2022-06-21 11:57:17 +02:00
|
|
|
->each(function ($invoice) {
|
2024-07-14 13:28:54 +02:00
|
|
|
|
|
|
|
/** @var \App\Models\RecurringInvoice $invoice */
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->csv->insertOne($this->buildRow($invoice));
|
|
|
|
});
|
2022-04-27 09:21:55 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
return $this->csv->toString();
|
2022-04-27 09:21:55 +02:00
|
|
|
}
|
|
|
|
|
2023-09-12 03:22:59 +02:00
|
|
|
|
|
|
|
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-09-12 03:22:59 +02:00
|
|
|
|
|
|
|
$report = $query->cursor()
|
|
|
|
->map(function ($resource) {
|
2024-07-14 13:28:54 +02:00
|
|
|
|
|
|
|
/** @var \App\Models\RecurringInvoice $resource */
|
2023-09-12 03:22:59 +02:00
|
|
|
$row = $this->buildRow($resource);
|
|
|
|
return $this->processMetaData($row, $resource);
|
|
|
|
})->toArray();
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-09-12 03:22:59 +02:00
|
|
|
return array_merge(['columns' => $header], $report);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
private function buildRow(RecurringInvoice $invoice): array
|
2022-04-27 09:21:55 +02:00
|
|
|
{
|
|
|
|
$transformed_invoice = $this->invoice_transformer->transform($invoice);
|
|
|
|
|
|
|
|
$entity = [];
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
foreach (array_values($this->input['report_keys']) as $key) {
|
2023-07-18 07:11:45 +02:00
|
|
|
|
2023-09-12 03:22:59 +02:00
|
|
|
$parts = explode('.', $key);
|
2023-07-18 07:11:45 +02:00
|
|
|
|
2023-09-12 03:22:59 +02:00
|
|
|
if (is_array($parts) && $parts[0] == 'recurring_invoice' && array_key_exists($parts[1], $transformed_invoice)) {
|
|
|
|
$entity[$key] = $transformed_invoice[$parts[1]];
|
2024-01-14 05:05:00 +01:00
|
|
|
} elseif($parts[0] == 'item') {
|
2023-12-02 05:49:19 +01:00
|
|
|
$entity[$key] = '';
|
2024-01-14 05:05:00 +01:00
|
|
|
} else {
|
2023-12-02 04:09:50 +01:00
|
|
|
// nlog($key);
|
|
|
|
$entity[$key] = $this->decorator->transform($key, $invoice);
|
|
|
|
// $entity[$key] = '';
|
|
|
|
// $entity[$key] = $this->resolveKey($key, $invoice, $this->invoice_transformer);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2023-07-18 07:11:45 +02:00
|
|
|
|
2022-04-27 09:21:55 +02:00
|
|
|
}
|
2023-12-12 00:46:23 +01:00
|
|
|
|
|
|
|
// return $entity;
|
|
|
|
return $this->decorateAdvancedFields($invoice, $entity);
|
2022-04-27 09:21:55 +02:00
|
|
|
}
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
private function decorateAdvancedFields(RecurringInvoice $invoice, array $entity): array
|
2022-04-27 09:21:55 +02:00
|
|
|
{
|
2023-12-12 00:46:23 +01:00
|
|
|
// if (in_array('country_id', $this->input['report_keys'])) {
|
|
|
|
// $entity['country'] = $invoice->client->country ? ctrans("texts.country_{$invoice->client->country->name}") : '';
|
|
|
|
// }
|
2022-05-10 09:04:24 +02:00
|
|
|
|
2023-12-12 00:46:23 +01:00
|
|
|
// if (in_array('currency_id', $this->input['report_keys'])) {
|
|
|
|
// $entity['currency'] = $invoice->client->currency() ? $invoice->client->currency()->code : $invoice->company->currency()->code;
|
|
|
|
// }
|
2022-04-27 09:21:55 +02:00
|
|
|
|
2023-12-12 00:46:23 +01:00
|
|
|
// if (in_array('client_id', $this->input['report_keys'])) {
|
|
|
|
// $entity['client'] = $invoice->client->present()->name();
|
|
|
|
// }
|
2022-04-27 09:21:55 +02:00
|
|
|
|
2023-12-12 00:46:23 +01:00
|
|
|
// if (in_array('recurring_invoice.status', $this->input['report_keys'])) {
|
|
|
|
// $entity['recurring_invoice.status'] = $invoice->stringStatus($invoice->status_id);
|
|
|
|
// }
|
2022-04-27 09:21:55 +02:00
|
|
|
|
2023-12-12 00:46:23 +01:00
|
|
|
// if (in_array('project_id', $this->input['report_keys'])) {
|
|
|
|
// $entity['project'] = $invoice->project ? $invoice->project->name : '';
|
|
|
|
// }
|
2022-04-27 09:21:55 +02:00
|
|
|
|
2023-12-12 00:46:23 +01:00
|
|
|
// if (in_array('vendor_id', $this->input['report_keys'])) {
|
|
|
|
// $entity['vendor'] = $invoice->vendor ? $invoice->vendor->name : '';
|
|
|
|
// }
|
2022-04-27 09:21:55 +02:00
|
|
|
|
2023-07-18 07:11:45 +02:00
|
|
|
if (in_array('recurring_invoice.frequency_id', $this->input['report_keys']) || in_array('frequency_id', $this->input['report_keys'])) {
|
2023-09-12 05:02:27 +02:00
|
|
|
$entity['recurring_invoice.frequency_id'] = $invoice->frequencyForKey($invoice->frequency_id);
|
2023-07-18 07:11:45 +02:00
|
|
|
}
|
2022-11-29 23:06:12 +01:00
|
|
|
|
2023-11-06 22:23:51 +01:00
|
|
|
if (in_array('recurring_invoice.auto_bill_enabled', $this->input['report_keys'])) {
|
|
|
|
$entity['recurring_invoice.auto_bill_enabled'] = $invoice->auto_bill_enabled ? ctrans('texts.yes') : ctrans('texts.no');
|
|
|
|
}
|
|
|
|
|
2023-11-22 07:52:02 +01:00
|
|
|
if (in_array('recurring_invoice.assigned_user_id', $this->input['report_keys'])) {
|
|
|
|
$entity['recurring_invoice.assigned_user_id'] = $invoice->assigned_user ? $invoice->assigned_user->present()->name() : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array('recurring_invoice.user_id', $this->input['report_keys'])) {
|
|
|
|
$entity['recurring_invoice.user_id'] = $invoice->user ? $invoice->user->present()->name() : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-27 09:21:55 +02:00
|
|
|
return $entity;
|
|
|
|
}
|
|
|
|
}
|