1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Export/CSV/QuoteItemExport.php

201 lines
5.8 KiB
PHP
Raw Normal View History

2022-04-28 06:02:59 +02:00
<?php
/**
* Quote Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Quote Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Export\CSV;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Quote;
use App\Transformers\QuoteTransformer;
use App\Utils\Ninja;
2023-09-12 03:22:59 +02:00
use Illuminate\Database\Eloquent\Builder;
2022-04-28 06:02:59 +02:00
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
class QuoteItemExport extends BaseExport
{
private $quote_transformer;
2023-03-01 13:19:15 +01:00
public string $date_key = 'date';
2022-04-28 06:02:59 +02:00
2023-03-08 21:49:18 +01:00
public Writer $csv;
2023-09-12 03:22:59 +02:00
private array $storage_array = [];
private array $storage_item_array = [];
2022-04-28 06:02:59 +02:00
private array $decorate_keys = [
'client',
'currency',
];
public function __construct(Company $company, array $input)
{
$this->company = $company;
$this->input = $input;
$this->quote_transformer = new QuoteTransformer();
}
2023-08-28 16:04:32 +02:00
public function init(): Builder
2022-04-28 06:02:59 +02:00
{
2023-08-28 16:04:32 +02:00
2022-04-28 06:02:59 +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-08-28 16:04:32 +02:00
if (count($this->input['report_keys']) == 0) {
$this->input['report_keys'] = array_values($this->mergeItemsKeys('quote_report_keys'));
}
2022-04-28 06:02:59 +02:00
2023-08-28 16:04:32 +02:00
$query = Quote::query()
->withTrashed()
->with('client')->where('company_id', $this->company->id)
->where('is_deleted', 0);
$query = $this->addDateRange($query);
return $query;
}
public function returnJson()
{
$query = $this->init();
$headerdisplay = $this->buildHeader();
$header = collect($this->input['report_keys'])->map(function ($key, $value) use($headerdisplay){
2023-10-06 18:43:02 +02:00
return ['identifier' => $key, 'display_value' => $headerdisplay[$value]];
2023-08-28 16:04:32 +02:00
})->toArray();
2023-09-12 03:22:59 +02:00
$query->cursor()
->each(function ($resource) {
$this->iterateItems($resource);
foreach($this->storage_array as $row) {
$this->storage_item_array[] = $this->processItemMetaData($row, $resource);
}
$this->storage_array = [];
});
return array_merge(['columns' => $header], $this->storage_item_array);
2023-08-28 16:04:32 +02:00
}
public function run()
{
2022-04-28 06:02:59 +02:00
//load the CSV document from a string
$this->csv = Writer::createFromString();
2023-08-28 16:04:32 +02:00
$query = $this->init();
2022-05-07 09:10:23 +02:00
2022-04-28 06:02:59 +02:00
//insert the header
$this->csv->insertOne($this->buildHeader());
2023-08-28 16:04:32 +02:00
2022-04-28 06:02:59 +02:00
$query->cursor()
->each(function ($quote) {
2022-04-28 06:02:59 +02:00
$this->iterateItems($quote);
});
2022-04-28 06:02:59 +02:00
2023-08-28 16:04:32 +02:00
$this->csv->insertAll($this->storage_array);
return $this->csv->toString();
2023-08-28 16:04:32 +02:00
2022-04-28 06:02:59 +02:00
}
private function iterateItems(Quote $quote)
{
$transformed_quote = $this->buildRow($quote);
$transformed_items = [];
foreach ($quote->line_items as $item) {
2023-07-16 12:34:31 +02:00
$item_array = [];
2023-08-28 16:04:32 +02:00
foreach (array_values(array_intersect($this->input['report_keys'], $this->item_report_keys)) as $key) { //items iterator produces item array
2023-07-16 12:34:31 +02:00
if (str_contains($key, "item.")) {
$key = str_replace("item.", "", $key);
if($key == 'type_id')
2023-08-28 16:04:32 +02:00
$key = 'type';
2023-07-16 12:34:31 +02:00
if($key == 'tax_id')
2023-08-28 16:04:32 +02:00
$key = 'tax_category';
2022-04-28 06:02:59 +02:00
2023-07-16 12:34:31 +02:00
if (property_exists($item, $key)) {
2023-08-28 16:04:32 +02:00
$item_array[$key] = $item->{$key};
}
else {
$item_array[$key] = '';
2023-07-16 12:34:31 +02:00
}
2022-04-28 06:02:59 +02:00
}
}
2023-08-28 16:04:32 +02:00
2022-05-10 08:59:41 +02:00
$transformed_items = array_merge($transformed_quote, $item_array);
$entity = $this->decorateAdvancedFields($quote, $transformed_items);
2023-08-28 16:04:32 +02:00
$this->storage_array[] = $entity;
2022-04-28 06:02:59 +02:00
}
}
private function buildRow(Quote $quote) :array
{
$transformed_quote = $this->quote_transformer->transform($quote);
$entity = [];
foreach (array_values($this->input['report_keys']) as $key) {
2023-08-28 16:04:32 +02:00
$parts = explode('.', $key);
2023-07-16 12:34:31 +02:00
2023-08-28 16:04:32 +02:00
if(is_array($parts) && $parts[0] == 'item') {
continue;
2023-07-16 12:34:31 +02:00
}
2023-08-28 16:04:32 +02:00
if (is_array($parts) && $parts[0] == 'quote' && array_key_exists($parts[1], $transformed_quote)) {
$entity[$key] = $transformed_quote[$parts[1]];
} elseif (array_key_exists($key, $transformed_quote)) {
$entity[$key] = $transformed_quote[$key];
} else {
$entity[$key] = $this->resolveKey($key, $quote, $this->quote_transformer);
}
2022-04-28 06:02:59 +02:00
}
2023-08-28 16:04:32 +02:00
2022-04-28 06:02:59 +02:00
return $this->decorateAdvancedFields($quote, $entity);
}
private function decorateAdvancedFields(Quote $quote, array $entity) :array
{
if (in_array('currency_id', $this->input['report_keys'])) {
2022-05-10 08:59:41 +02:00
$entity['currency'] = $quote->client->currency() ? $quote->client->currency()->code : $quote->company->currency()->code;
}
2022-04-28 06:02:59 +02:00
if (in_array('client_id', $this->input['report_keys'])) {
2022-05-10 08:59:41 +02:00
$entity['client'] = $quote->client->present()->name();
}
2022-04-28 06:02:59 +02:00
if (in_array('status_id', $this->input['report_keys'])) {
2022-05-10 08:59:41 +02:00
$entity['status'] = $quote->stringStatus($quote->status_id);
}
2022-04-28 06:02:59 +02:00
return $entity;
}
}