2022-04-27 09:21:55 +02: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)
|
2022-04-27 09:21:55 +02:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Export\CSV;
|
|
|
|
|
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\RecurringInvoice;
|
|
|
|
use App\Transformers\RecurringInvoiceTransformer;
|
|
|
|
use App\Utils\Ninja;
|
|
|
|
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-03-01 13:27:14 +01:00
|
|
|
public array $entity_keys = [
|
2022-04-27 09:21:55 +02:00
|
|
|
'amount' => 'amount',
|
|
|
|
'balance' => 'balance',
|
|
|
|
'client' => 'client_id',
|
2023-07-18 07:11:45 +02:00
|
|
|
// 'custom_surcharge1' => 'custom_surcharge1',
|
|
|
|
// 'custom_surcharge2' => 'custom_surcharge2',
|
|
|
|
// 'custom_surcharge3' => 'custom_surcharge3',
|
|
|
|
// 'custom_surcharge4' => 'custom_surcharge4',
|
2022-04-27 09:21:55 +02:00
|
|
|
'custom_value1' => 'custom_value1',
|
|
|
|
'custom_value2' => 'custom_value2',
|
|
|
|
'custom_value3' => 'custom_value3',
|
|
|
|
'custom_value4' => 'custom_value4',
|
|
|
|
'date' => 'date',
|
|
|
|
'discount' => 'discount',
|
|
|
|
'due_date' => 'due_date',
|
|
|
|
'exchange_rate' => 'exchange_rate',
|
|
|
|
'footer' => 'footer',
|
|
|
|
'number' => 'number',
|
|
|
|
'paid_to_date' => 'paid_to_date',
|
|
|
|
'partial' => 'partial',
|
|
|
|
'partial_due_date' => 'partial_due_date',
|
|
|
|
'po_number' => 'po_number',
|
|
|
|
'private_notes' => 'private_notes',
|
|
|
|
'public_notes' => 'public_notes',
|
2022-11-29 23:06:12 +01:00
|
|
|
'next_send_date' => 'next_send_date',
|
2022-04-27 09:21:55 +02:00
|
|
|
'status' => 'status_id',
|
|
|
|
'tax_name1' => 'tax_name1',
|
|
|
|
'tax_name2' => 'tax_name2',
|
|
|
|
'tax_name3' => 'tax_name3',
|
|
|
|
'tax_rate1' => 'tax_rate1',
|
|
|
|
'tax_rate2' => 'tax_rate2',
|
|
|
|
'tax_rate3' => 'tax_rate3',
|
|
|
|
'terms' => 'terms',
|
|
|
|
'total_taxes' => 'total_taxes',
|
2022-05-10 09:04:24 +02:00
|
|
|
'currency' => 'currency_id',
|
2022-04-27 09:21:55 +02:00
|
|
|
'vendor' => 'vendor_id',
|
|
|
|
'project' => 'project_id',
|
2023-07-18 07:11:45 +02:00
|
|
|
'frequency_id' => 'frequency_id',
|
2022-04-27 09:21:55 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
private array $decorate_keys = [
|
|
|
|
'country',
|
|
|
|
'client',
|
|
|
|
'currency',
|
|
|
|
'status',
|
|
|
|
'vendor',
|
2022-06-21 11:57:17 +02:00
|
|
|
'project',
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-21 11:57:17 +02:00
|
|
|
if (count($this->input['report_keys']) == 0) {
|
2022-05-10 09:04:24 +02:00
|
|
|
$this->input['report_keys'] = array_values($this->entity_keys);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-05-07 09:10:23 +02:00
|
|
|
|
2022-04-27 09:21:55 +02:00
|
|
|
//insert the header
|
|
|
|
$this->csv->insertOne($this->buildHeader());
|
|
|
|
|
|
|
|
$query = RecurringInvoice::query()
|
|
|
|
->withTrashed()
|
|
|
|
->with('client')->where('company_id', $this->company->id)
|
2022-06-21 11:57:17 +02:00
|
|
|
->where('is_deleted', 0);
|
2022-04-27 09:21:55 +02:00
|
|
|
|
|
|
|
$query = $this->addDateRange($query);
|
|
|
|
|
|
|
|
$query->cursor()
|
2022-06-21 11:57:17 +02:00
|
|
|
->each(function ($invoice) {
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
private function buildRow(RecurringInvoice $invoice) :array
|
|
|
|
{
|
|
|
|
$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) {
|
2022-05-10 09:04:24 +02:00
|
|
|
$keyval = array_search($key, $this->entity_keys);
|
|
|
|
|
2023-07-18 07:11:45 +02:00
|
|
|
if(!$keyval) {
|
|
|
|
$keyval = array_search(str_replace("recurring_invoice.", "", $key), $this->entity_keys) ?? $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$keyval) {
|
|
|
|
$keyval = $key;
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (array_key_exists($key, $transformed_invoice)) {
|
2022-05-10 09:04:24 +02:00
|
|
|
$entity[$keyval] = $transformed_invoice[$key];
|
2023-07-18 07:11:45 +02:00
|
|
|
} elseif (array_key_exists($keyval, $transformed_invoice)) {
|
|
|
|
$entity[$keyval] = $transformed_invoice[$keyval];
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2023-07-18 07:11:45 +02:00
|
|
|
$entity[$keyval] = $this->resolveKey($keyval, $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
|
|
|
}
|
|
|
|
|
|
|
|
return $this->decorateAdvancedFields($invoice, $entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function decorateAdvancedFields(RecurringInvoice $invoice, array $entity) :array
|
|
|
|
{
|
2022-06-21 11:57:17 +02: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
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (in_array('currency_id', $this->input['report_keys'])) {
|
2022-05-10 09:04:24 +02:00
|
|
|
$entity['currency'] = $invoice->client->currency() ? $invoice->client->currency()->code : $invoice->company->currency()->code;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-04-27 09:21:55 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (in_array('client_id', $this->input['report_keys'])) {
|
2022-05-10 09:04:24 +02:00
|
|
|
$entity['client'] = $invoice->client->present()->name();
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-04-27 09:21:55 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (in_array('status_id', $this->input['report_keys'])) {
|
2022-05-10 09:04:24 +02:00
|
|
|
$entity['status'] = $invoice->stringStatus($invoice->status_id);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-04-27 09:21:55 +02:00
|
|
|
|
2022-06-21 11:57:17 +02: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
|
|
|
|
2022-06-21 11:57:17 +02: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'])) {
|
|
|
|
$entity['frequency_id'] = $invoice->frequencyForKey($invoice->frequency_id);
|
|
|
|
}
|
2022-11-29 23:06:12 +01:00
|
|
|
|
2022-04-27 09:21:55 +02:00
|
|
|
return $entity;
|
|
|
|
}
|
|
|
|
}
|