1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00
invoiceninja/app/Export/CSV/QuoteExport.php

152 lines
4.1 KiB
PHP
Raw Normal View History

2022-04-08 06:54:59 +02:00
<?php
/**
* Quote Ninja (https://quoteninja.com).
*
* @link https://github.com/quoteninja/quoteninja source repository
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. Quote Ninja LLC (https://quoteninja.com)
2022-04-08 06:54:59 +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\Quote;
use App\Transformers\QuoteTransformer;
use App\Utils\Ninja;
2023-08-29 14:56:36 +02:00
use Illuminate\Contracts\Database\Eloquent\Builder;
2022-04-08 06:54:59 +02:00
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
2022-04-27 08:33:30 +02:00
class QuoteExport extends BaseExport
2022-04-08 06:54:59 +02:00
{
private $quote_transformer;
2023-03-01 13:19:15 +01:00
public string $date_key = 'date';
2022-04-27 08:33:30 +02:00
2023-03-08 21:49:18 +01:00
public Writer $csv;
2022-04-08 06:54:59 +02:00
private array $decorate_keys = [
'client',
'currency',
'invoice',
2022-04-08 06:54:59 +02:00
];
2022-04-27 08:33:30 +02:00
public function __construct(Company $company, array $input)
2022-04-08 06:54:59 +02:00
{
$this->company = $company;
2022-04-27 08:33:30 +02:00
$this->input = $input;
2022-04-08 06:54:59 +02:00
$this->quote_transformer = new QuoteTransformer();
}
2023-08-29 14:56:36 +02:00
private function init(): Builder
2022-04-08 06:54:59 +02:00
{
2023-08-29 14:56:36 +02:00
2022-04-08 06:54: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));
if (count($this->input['report_keys']) == 0) {
2023-08-29 14:56:36 +02:00
$this->input['report_keys'] = array_values($this->quote_report_keys);
}
2022-05-07 09:10:23 +02:00
2022-04-27 08:33:30 +02:00
$query = Quote::query()
2022-11-04 20:54:05 +01:00
->withTrashed()
2022-05-10 08:50:30 +02:00
->with('client')
->where('company_id', $this->company->id)
->where('is_deleted', 0);
2022-04-27 08:33:30 +02:00
$query = $this->addDateRange($query);
2023-08-29 14:56:36 +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();
$report = $query->cursor()
->map(function ($resource) {
return $this->buildRow($resource);
})->toArray();
return array_merge(['columns' => $header], $report);
}
public function run()
{
//load the CSV document from a string
$this->csv = Writer::createFromString();
$query = $this->init();
//insert the header
$this->csv->insertOne($this->buildHeader());
2022-04-27 08:33:30 +02:00
$query->cursor()
->each(function ($quote) {
$this->csv->insertOne($this->buildRow($quote));
});
2022-04-08 06:54:59 +02:00
return $this->csv->toString();
2022-04-08 06:54:59 +02:00
}
private function buildRow(Quote $quote) :array
{
2023-08-29 14:56:36 +02:00
$transformed_invoice = $this->quote_transformer->transform($quote);
2022-04-08 06:54:59 +02:00
$entity = [];
foreach (array_values($this->input['report_keys']) as $key) {
2022-05-10 08:50:30 +02:00
2023-08-29 14:56:36 +02:00
$parts = explode('.', $key);
2023-07-16 12:17:43 +02:00
2023-08-29 14:56:36 +02:00
if (is_array($parts) && $parts[0] == 'quote' && array_key_exists($parts[1], $transformed_invoice)) {
$entity[$key] = $transformed_invoice[$parts[1]];
} else {
$entity[$key] = $this->resolveKey($key, $quote, $this->quote_transformer);
2023-07-16 12:17:43 +02:00
}
2022-04-08 06:54:59 +02:00
}
return $this->decorateAdvancedFields($quote, $entity);
}
2023-08-29 14:56:36 +02:00
2022-04-08 06:54:59 +02:00
private function decorateAdvancedFields(Quote $quote, array $entity) :array
{
2023-08-29 14:56:36 +02:00
if (in_array('quote.currency_id', $this->input['report_keys'])) {
$entity['quote.currency'] = $quote->client->currency()->code;
}
2022-04-08 06:54:59 +02:00
2023-08-29 14:56:36 +02:00
if (in_array('quote.client_id', $this->input['report_keys'])) {
$entity['quote.client'] = $quote->client->present()->name();
}
2022-05-10 08:50:30 +02:00
2023-08-29 14:56:36 +02:00
if (in_array('quote.status', $this->input['report_keys'])) {
$entity['quote.status'] = $quote->stringStatus($quote->status_id);
}
2022-04-08 06:54:59 +02:00
2023-08-29 14:56:36 +02:00
if (in_array('quote.invoice_id', $this->input['report_keys'])) {
$entity['quote.invoice'] = $quote->invoice ? $quote->invoice->number : '';
}
2022-04-08 06:54:59 +02:00
return $entity;
}
}