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

215 lines
6.4 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-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']));
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();
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 16:04:32 +02:00
2023-10-26 04:57:44 +02:00
$query->cursor()
->each(function ($resource) {
$this->iterateItems($resource);
2023-09-12 03:22:59 +02:00
2023-10-26 04:57:44 +02:00
foreach($this->storage_array as $row) {
$this->storage_item_array[] = $this->processItemMetaData($row, $resource);
}
2023-09-12 03:22:59 +02:00
2023-10-26 04:57:44 +02:00
$this->storage_array = [];
2023-09-12 03:22:59 +02:00
2023-10-26 04:57:44 +02:00
});
2023-09-12 03:22:59 +02:00
2023-10-26 04:57:44 +02:00
return array_merge(['columns' => $header], $this->storage_item_array);
2023-09-12 03:22:59 +02:00
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-10-26 04:57:44 +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-10-26 04:57:44 +02:00
$item_array = [];
2023-07-16 12:34:31 +02:00
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.")) {
2023-10-20 00:39:25 +02:00
$tmp_key = str_replace("item.", "", $key);
2023-07-16 12:34:31 +02:00
2023-10-26 04:57:44 +02:00
if($tmp_key == 'type_id') {
2023-10-20 00:39:25 +02:00
$tmp_key = 'type';
2023-10-26 04:57:44 +02:00
}
2023-07-16 12:34:31 +02:00
2023-10-26 04:57:44 +02:00
if($tmp_key == 'tax_id') {
2023-10-20 00:39:25 +02:00
$tmp_key = 'tax_category';
2023-10-26 04:57:44 +02:00
}
2022-04-28 06:02:59 +02:00
2023-10-20 00:39:25 +02:00
if (property_exists($item, $tmp_key)) {
$item_array[$key] = $item->{$tmp_key};
2023-10-26 04:57:44 +02:00
} else {
2023-08-28 16:04:32 +02:00
$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-10-20 00:22:33 +02:00
$entity = array_merge(array_flip(array_values($this->input['report_keys'])), $entity);
2022-05-10 08:59:41 +02:00
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);
}
if (in_array('quote.assigned_user_id', $this->input['report_keys'])) {
$entity['quote.assigned_user_id'] = $quote->assigned_user ? $quote->assigned_user->present()->name(): '';
}
if (in_array('quote.user_id', $this->input['report_keys'])) {
$entity['quote.user_id'] = $quote->user ? $quote->user->present()->name(): '';
}
2022-04-28 06:02:59 +02:00
return $entity;
}
}