2022-04-08 10:10:14 +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-08 10:10:14 +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\Invoice;
|
|
|
|
use App\Transformers\InvoiceTransformer;
|
|
|
|
use App\Utils\Ninja;
|
2023-08-28 14:44:04 +02:00
|
|
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
2022-04-08 10:10:14 +02:00
|
|
|
use Illuminate\Support\Facades\App;
|
|
|
|
use League\Csv\Writer;
|
|
|
|
|
2022-04-27 08:29:38 +02:00
|
|
|
class InvoiceItemExport extends BaseExport
|
2022-04-08 10:10:14 +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-07-06 04:34:35 +02:00
|
|
|
private bool $force_keys = false;
|
|
|
|
|
2023-08-28 14:44:04 +02:00
|
|
|
private array $storage_array = [];
|
2022-04-08 10:10:14 +02:00
|
|
|
|
|
|
|
private array $decorate_keys = [
|
|
|
|
'client',
|
2022-05-10 08:13:06 +02:00
|
|
|
'currency_id',
|
2022-11-06 09:14:06 +01:00
|
|
|
'status'
|
2022-04-08 10:10:14 +02:00
|
|
|
];
|
|
|
|
|
2022-04-27 08:29:38 +02:00
|
|
|
public function __construct(Company $company, array $input)
|
2022-04-08 10:10:14 +02:00
|
|
|
{
|
|
|
|
$this->company = $company;
|
2022-04-27 08:29:38 +02:00
|
|
|
$this->input = $input;
|
2022-04-08 10:10:14 +02:00
|
|
|
$this->invoice_transformer = new InvoiceTransformer();
|
|
|
|
}
|
|
|
|
|
2023-08-28 14:44:04 +02:00
|
|
|
public function init(): Builder
|
2022-04-08 10:10:14 +02:00
|
|
|
{
|
2023-08-28 14:44:04 +02:00
|
|
|
|
2022-04-08 10:10:14 +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));
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (count($this->input['report_keys']) == 0) {
|
2023-07-06 04:34:35 +02:00
|
|
|
$this->force_keys = true;
|
2023-08-28 14:44:04 +02:00
|
|
|
$this->input['report_keys'] = array_values($this->mergeItemsKeys('invoice_report_keys'));
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-05-07 09:10:23 +02:00
|
|
|
|
2022-04-27 08:29:38 +02:00
|
|
|
$query = Invoice::query()
|
2022-11-04 20:54:05 +01:00
|
|
|
->withTrashed()
|
2022-04-27 08:29:38 +02:00
|
|
|
->with('client')->where('company_id', $this->company->id)
|
2023-02-16 02:36:09 +01:00
|
|
|
->where('is_deleted', 0);
|
2022-04-27 08:29:38 +02:00
|
|
|
|
|
|
|
$query = $this->addDateRange($query);
|
|
|
|
|
2023-08-28 14:44:04 +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){
|
|
|
|
return ['identifier' => $value, 'display_value' => $headerdisplay[$value]];
|
|
|
|
})->toArray();
|
|
|
|
|
2023-09-11 14:15:24 +02:00
|
|
|
$items = $query->cursor()
|
|
|
|
->map(function ($resource) {
|
2023-08-28 14:44:04 +02:00
|
|
|
$this->iterateItems($resource);
|
2023-09-11 14:15:24 +02:00
|
|
|
$row = $this->processMetaData($this->storage_array[0], $resource);
|
|
|
|
$this->storage_array = [];
|
|
|
|
return $row;
|
|
|
|
})->toArray();
|
2023-08-28 14:44:04 +02:00
|
|
|
|
2023-09-11 14:15:24 +02:00
|
|
|
|
|
|
|
return array_merge(['columns' => $header], $items);
|
2023-08-28 14:44:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
2023-02-16 02:36:09 +01:00
|
|
|
->each(function ($invoice) {
|
2022-04-27 08:29:38 +02:00
|
|
|
$this->iterateItems($invoice);
|
2023-02-16 02:36:09 +01:00
|
|
|
});
|
2022-04-08 10:10:14 +02:00
|
|
|
|
2023-08-28 14:44:04 +02:00
|
|
|
$this->csv->insertAll($this->storage_array);
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
return $this->csv->toString();
|
2022-04-08 10:10:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function iterateItems(Invoice $invoice)
|
|
|
|
{
|
|
|
|
$transformed_invoice = $this->buildRow($invoice);
|
|
|
|
|
|
|
|
$transformed_items = [];
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
foreach ($invoice->line_items as $item) {
|
2023-07-06 07:55:26 +02:00
|
|
|
$item_array = [];
|
|
|
|
|
2023-08-28 14:44:04 +02:00
|
|
|
foreach (array_values(array_intersect($this->input['report_keys'], $this->item_report_keys)) as $key) { //items iterator produces item array
|
2023-07-06 05:53:24 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (str_contains($key, "item.")) {
|
2023-07-06 05:53:24 +02:00
|
|
|
|
2022-04-08 10:10:14 +02:00
|
|
|
$key = str_replace("item.", "", $key);
|
2023-07-06 07:55:26 +02:00
|
|
|
|
2023-07-06 05:53:24 +02:00
|
|
|
if($key == 'type_id')
|
2023-08-28 14:44:04 +02:00
|
|
|
$key = 'type';
|
2023-07-06 05:53:24 +02:00
|
|
|
|
|
|
|
if($key == 'tax_id')
|
2023-08-28 14:44:04 +02:00
|
|
|
$key = 'tax_category';
|
2023-07-06 05:11:35 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (property_exists($item, $key)) {
|
2023-08-28 14:44:04 +02:00
|
|
|
$item_array[$key] = $item->{$key};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$item_array[$key] = '';
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-04-08 10:10:14 +02:00
|
|
|
}
|
|
|
|
}
|
2023-08-28 16:04:32 +02:00
|
|
|
|
2022-05-10 08:13:06 +02:00
|
|
|
$transformed_items = array_merge($transformed_invoice, $item_array);
|
|
|
|
$entity = $this->decorateAdvancedFields($invoice, $transformed_items);
|
|
|
|
|
2023-08-28 14:44:04 +02:00
|
|
|
$this->storage_array[] = $entity;
|
|
|
|
|
2022-04-08 10:10:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildRow(Invoice $invoice) :array
|
|
|
|
{
|
|
|
|
$transformed_invoice = $this->invoice_transformer->transform($invoice);
|
|
|
|
|
|
|
|
$entity = [];
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
foreach (array_values($this->input['report_keys']) as $key) {
|
2023-08-28 16:04:32 +02:00
|
|
|
|
2023-08-28 14:44:04 +02:00
|
|
|
$parts = explode('.', $key);
|
|
|
|
|
|
|
|
if(is_array($parts) && $parts[0] == 'item')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (is_array($parts) && $parts[0] == 'invoice' && array_key_exists($parts[1], $transformed_invoice)) {
|
|
|
|
$entity[$key] = $transformed_invoice[$parts[1]];
|
|
|
|
}else if (array_key_exists($key, $transformed_invoice)) {
|
|
|
|
$entity[$key] = $transformed_invoice[$key];
|
|
|
|
}
|
2023-08-28 16:04:32 +02:00
|
|
|
else {
|
2023-08-28 14:44:04 +02:00
|
|
|
$entity[$key] = $this->resolveKey($key, $invoice, $this->invoice_transformer);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-04-08 10:10:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->decorateAdvancedFields($invoice, $entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function decorateAdvancedFields(Invoice $invoice, array $entity) :array
|
|
|
|
{
|
2023-02-16 02:36:09 +01:00
|
|
|
if (in_array('currency_id', $this->input['report_keys'])) {
|
2022-05-10 08:13:06 +02:00
|
|
|
$entity['currency'] = $invoice->client->currency() ? $invoice->client->currency()->code : $invoice->company->currency()->code;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-04-08 10:10:14 +02:00
|
|
|
|
2023-07-06 05:53:24 +02:00
|
|
|
if(array_key_exists('type', $entity)) {
|
|
|
|
$entity['type'] = $invoice->typeIdString($entity['type']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(array_key_exists('tax_category', $entity)) {
|
|
|
|
$entity['tax_category'] = $invoice->taxTypeString($entity['tax_category']);
|
|
|
|
}
|
|
|
|
|
2022-04-08 10:10:14 +02:00
|
|
|
return $entity;
|
|
|
|
}
|
2023-09-11 14:15:24 +02:00
|
|
|
|
|
|
|
public function processMetaData(array $row, $resource): array
|
|
|
|
{
|
|
|
|
$entity = 'invoice';
|
|
|
|
$clean_row = [];
|
|
|
|
|
|
|
|
foreach (array_values($this->input['report_keys']) as $key => $value) {
|
|
|
|
|
|
|
|
$report_keys = explode(".", $value);
|
|
|
|
|
|
|
|
$column_key = $value;
|
|
|
|
|
|
|
|
if($value == 'type_id' || $value == 'item.type_id')
|
|
|
|
$column_key = 'type';
|
|
|
|
|
|
|
|
if($value == 'tax_id' || $value == 'item.tax_id')
|
|
|
|
$column_key = 'tax_category';
|
|
|
|
|
|
|
|
$clean_row[$key]['entity'] = $report_keys[0];
|
|
|
|
$clean_row[$key]['id'] = $report_keys[1] ?? $report_keys[0];
|
|
|
|
$clean_row[$key]['hashed_id'] = $report_keys[0] == $entity ? null : $resource->{$report_keys[0]}->hashed_id ?? null;
|
|
|
|
$clean_row[$key]['value'] = isset($row[$column_key]) ? $row[$column_key] : $row[$report_keys[1]];
|
|
|
|
$clean_row[$key]['identifier'] = $value;
|
|
|
|
$clean_row[$key]['display_value'] = isset($row[$column_key]) ? $row[$column_key] : $row[$report_keys[1]];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $clean_row;
|
|
|
|
}
|
|
|
|
|
2022-04-08 10:10:14 +02:00
|
|
|
}
|