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

234 lines
6.7 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;
2023-12-02 04:09:50 +01:00
use App\Export\Decorators\Decorator;
2022-04-28 06:02:59 +02:00
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-12-02 04:09:50 +01:00
private Decorator $decorator;
2023-09-12 03:22:59 +02:00
private array $storage_array = [];
2024-01-14 05:05:00 +01:00
2023-09-12 03:22:59 +02:00
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();
2024-01-14 05:05:00 +01:00
$this->decorator = new Decorator();
2022-04-28 06:02:59 +02:00
}
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));
2024-01-14 05:05:00 +01:00
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()
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);
})
->with('client')->where('company_id', $this->company->id);
2024-06-14 09:09:44 +02:00
if(!$this->input['include_deleted'] ?? false) {
$query->where('is_deleted', 0);
}
2023-08-28 16:04:32 +02:00
$query = $this->addDateRange($query, 'quotes');
2024-02-13 05:25:18 +01: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->addQuoteStatusFilter($query, $this->input['status'] ?? '');
if($this->input['document_email_attachment'] ?? false) {
$this->queueDocuments($query);
}
2023-08-28 16:04:32 +02:00
return $query;
}
2024-01-14 05:05:00 +01:00
2023-08-28 16:04:32 +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-08-28 16:04:32 +02:00
2023-10-26 04:57:44 +02:00
$query->cursor()
->each(function ($resource) {
2024-08-22 08:45:06 +02:00
2024-07-14 13:28:54 +02:00
/** @var \App\Models\Quote $resource */
2023-10-26 04:57:44 +02:00
$this->iterateItems($resource);
2024-01-14 05:05:00 +01: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 = [];
2024-01-14 05:05:00 +01:00
2023-10-26 04:57:44 +02:00
});
2024-01-14 05:05:00 +01: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()
{
2024-01-14 05:05:00 +01:00
2022-04-28 06:02:59 +02:00
//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');
2022-04-28 06:02:59 +02:00
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());
2024-01-14 05:05:00 +01:00
2022-04-28 06:02:59 +02:00
$query->cursor()
->each(function ($quote) {
2024-08-22 08:45:06 +02:00
2024-07-14 13:28:54 +02:00
/** @var \App\Models\Quote $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);
2024-01-14 05:05:00 +01:00
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 = [];
$currency = $this->company->currency();
2022-04-28 06:02:59 +02:00
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
2024-01-14 05:05:00 +01:00
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);
2024-01-14 05:05:00 +01: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
}
}
2024-01-14 05:05:00 +01:00
2022-05-10 08:59:41 +02:00
$transformed_items = array_merge($transformed_quote, $item_array);
$entity = $this->decorateAdvancedFields($quote, $transformed_items);
$entity = array_merge(array_flip(array_values($this->input['report_keys'])), $entity);
2024-10-05 07:10:48 +02:00
$entity = $this->convertFloats($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
}
}
2024-01-14 05:05:00 +01:00
private function buildRow(Quote $quote): array
2022-04-28 06:02:59 +02:00
{
$transformed_quote = $this->quote_transformer->transform($quote);
$entity = [];
foreach (array_values($this->input['report_keys']) as $key) {
2024-01-14 05:05:00 +01:00
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 {
2023-12-02 04:09:50 +01:00
$entity[$key] = $this->decorator->transform($key, $quote);
}
2022-04-28 06:02:59 +02:00
}
$entity = $this->decorateAdvancedFields($quote, $entity);
2024-10-05 07:56:17 +02:00
return $entity;
2022-04-28 06:02:59 +02:00
}
2024-01-14 05:05:00 +01:00
private function decorateAdvancedFields(Quote $quote, array $entity): array
2022-04-28 06:02:59 +02:00
{
2024-01-14 05:05:00 +01:00
if (in_array('quote.assigned_user_id', $this->input['report_keys'])) {
2024-01-14 05:05:00 +01:00
$entity['quote.assigned_user_id'] = $quote->assigned_user ? $quote->assigned_user->present()->name() : '';
}
2024-01-14 05:05:00 +01:00
if (in_array('quote.user_id', $this->input['report_keys'])) {
2024-01-14 05:05:00 +01:00
$entity['quote.user_id'] = $quote->user ? $quote->user->present()->name() : '';
}
2022-04-28 06:02:59 +02:00
return $entity;
}
}