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

199 lines
6.8 KiB
PHP
Raw Normal View History

2022-04-08 03:25:53 +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 03:25:53 +02:00
*
* @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;
2023-08-18 15:36:54 +02:00
use App\Libraries\MultiDB;
2023-10-26 04:57:44 +02:00
use App\Models\Company;
use App\Models\Credit;
2022-04-08 03:25:53 +02:00
use App\Transformers\CreditTransformer;
2023-10-26 04:57:44 +02:00
use App\Utils\Ninja;
use App\Utils\Number;
2023-09-12 03:01:14 +02:00
use Illuminate\Database\Eloquent\Builder;
2023-10-26 04:57:44 +02:00
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
2022-04-08 03:25:53 +02:00
2022-04-27 07:41:52 +02:00
class CreditExport extends BaseExport
2022-04-08 03:25:53 +02:00
{
2022-04-27 07:41:52 +02:00
private CreditTransformer $credit_transformer;
2023-12-02 04:09:50 +01:00
private Decorator $decorator;
2023-03-01 13:19:15 +01:00
public string $date_key = 'created_at';
2022-04-08 03:25:53 +02:00
2023-03-08 21:49:18 +01:00
public Writer $csv;
2022-04-27 07:41:52 +02:00
public function __construct(Company $company, array $input)
2022-04-08 03:25:53 +02:00
{
$this->company = $company;
2022-04-27 07:41:52 +02:00
$this->input = $input;
2022-04-08 03:25:53 +02:00
$this->credit_transformer = new CreditTransformer();
2023-12-02 04:09:50 +01:00
$this->decorator = new Decorator();
2022-04-08 03:25:53 +02:00
}
2023-08-18 15:36:54 +02:00
public function returnJson()
2023-08-18 13:36:50 +02:00
{
$query = $this->init();
2023-08-24 05:22:35 +02:00
$headerdisplay = $this->buildHeader();
2023-08-23 12:36:25 +02:00
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-24 12:41:06 +02:00
2023-08-18 13:36:50 +02:00
$report = $query->cursor()
2023-08-18 15:36:54 +02:00
->map(function ($credit) {
$row = $this->buildRow($credit);
return $this->processMetaData($row, $credit);
})->toArray();
2023-08-23 12:36:25 +02:00
2023-08-23 12:36:56 +02:00
return array_merge(['columns' => $header], $report);
2023-08-18 15:36:54 +02:00
}
2023-09-11 12:33:56 +02:00
public function processMetaData(array $row, $resource): array
2023-08-18 15:36:54 +02:00
{
$clean_row = [];
2023-08-24 05:22:35 +02:00
foreach (array_values($this->input['report_keys']) as $key => $value) {
2023-08-18 15:36:54 +02:00
$report_keys = explode(".", $value);
2023-08-24 05:22:35 +02:00
$column_key = $value;
2023-08-18 15:36:54 +02:00
$clean_row[$key]['entity'] = $report_keys[0];
2023-08-20 23:46:14 +02:00
$clean_row[$key]['id'] = $report_keys[1] ?? $report_keys[0];
2023-09-12 03:01:14 +02:00
$clean_row[$key]['hashed_id'] = $report_keys[0] == 'credit' ? null : $resource->{$report_keys[0]}->hashed_id ?? null;
2023-08-18 15:36:54 +02:00
$clean_row[$key]['value'] = $row[$column_key];
2023-08-23 12:36:25 +02:00
$clean_row[$key]['identifier'] = $value;
2023-08-23 12:36:56 +02:00
2023-10-26 04:57:44 +02:00
if(in_array($clean_row[$key]['id'], ['paid_to_date','total_taxes','amount', 'balance', 'partial', 'refunded', 'applied','unit_cost','cost','price'])) {
2023-09-12 03:01:14 +02:00
$clean_row[$key]['display_value'] = Number::formatMoney($row[$column_key], $resource->client);
2023-10-26 04:57:44 +02:00
} else {
2023-08-18 15:36:54 +02:00
$clean_row[$key]['display_value'] = $row[$column_key];
2023-10-26 04:57:44 +02:00
}
2023-08-18 15:36:54 +02:00
}
return $clean_row;
2023-08-18 13:36:50 +02:00
}
private function init(): Builder
2022-04-08 03:25:53 +02:00
{
2023-08-18 13:36:50 +02:00
2022-04-08 03:25:53 +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-18 15:36:54 +02:00
if (count($this->input['report_keys']) == 0) {
2023-08-24 05:22:35 +02:00
$this->input['report_keys'] = array_values($this->credit_report_keys);
2023-08-18 15:36:54 +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-18 13:36:50 +02:00
$query = Credit::query()
->withTrashed()
2023-08-24 05:22:35 +02:00
->with('client')
->where('company_id', $this->company->id)
2023-08-18 13:36:50 +02:00
->where('is_deleted', 0);
$query = $this->addDateRange($query);
return $query;
}
2023-08-18 15:36:54 +02:00
public function run(): string
2023-08-18 13:36:50 +02:00
{
$query = $this->init();
2022-04-08 03:25:53 +02:00
//load the CSV document from a string
$this->csv = Writer::createFromString();
//insert the header
$this->csv->insertOne($this->buildHeader());
2022-04-27 07:41:52 +02:00
$query->cursor()
->each(function ($credit) {
$this->csv->insertOne($this->buildRow($credit));
});
2022-04-08 03:25:53 +02:00
return $this->csv->toString();
2022-04-08 03:25:53 +02:00
}
2022-04-08 03:54:17 +02:00
private function buildRow(Credit $credit) :array
2022-04-08 03:25:53 +02:00
{
2022-04-08 03:54:17 +02:00
$transformed_credit = $this->credit_transformer->transform($credit);
2022-04-08 03:25:53 +02:00
$entity = [];
foreach (array_values($this->input['report_keys']) as $key) {
2023-08-24 05:22:35 +02:00
$keyval = $key;
$credit_key = str_replace("credit.", "", $key);
$searched_credit_key = array_search(str_replace("credit.", "", $key), $this->credit_report_keys) ?? $key;
2023-07-05 04:56:37 +02:00
2023-08-24 05:22:35 +02:00
if (isset($transformed_credit[$credit_key])) {
$entity[$keyval] = $transformed_credit[$credit_key];
} elseif (isset($transformed_credit[$keyval])) {
2023-07-05 04:56:37 +02:00
$entity[$keyval] = $transformed_credit[$keyval];
2023-10-26 04:57:44 +02:00
} elseif(isset($transformed_credit[$searched_credit_key])) {
2023-08-24 05:22:35 +02:00
$entity[$keyval] = $transformed_credit[$searched_credit_key];
2023-10-26 04:57:44 +02:00
} else {
2023-12-02 04:09:50 +01:00
// nlog($key);
$entity[$key] = $this->decorator->transform($key, $credit);
// $entity[$key] = '';
// $entity[$keyval] = $this->resolveKey($keyval, $credit, $this->credit_transformer);
}
2023-08-24 05:22:35 +02:00
2022-04-08 03:25:53 +02:00
}
2022-04-08 03:54:17 +02:00
return $this->decorateAdvancedFields($credit, $entity);
2022-04-08 03:25:53 +02:00
}
2022-04-08 03:54:17 +02:00
private function decorateAdvancedFields(Credit $credit, array $entity) :array
2022-04-08 03:25:53 +02:00
{
if (in_array('country_id', $this->input['report_keys'])) {
$entity['country'] = $credit->client->country ? ctrans("texts.country_{$credit->client->country->name}") : '';
}
2023-07-05 04:56:37 +02:00
if (in_array('currency_id', $this->input['report_keys'])) {
2023-07-05 04:56:37 +02:00
$entity['currency_id'] = $credit->client->currency() ? $credit->client->currency()->code : $credit->company->currency()->code;
}
2022-04-08 03:54:17 +02:00
if (in_array('invoice_id', $this->input['report_keys'])) {
$entity['invoice'] = $credit->invoice ? $credit->invoice->number : '';
}
2022-05-10 06:25:16 +02:00
if (in_array('client_id', $this->input['report_keys'])) {
2022-05-10 06:25:16 +02:00
$entity['client'] = $credit->client->present()->name();
}
2022-04-08 03:25:53 +02:00
if (in_array('status_id', $this->input['report_keys'])) {
2022-05-10 06:25:16 +02:00
$entity['status'] = $credit->stringStatus($credit->status_id);
}
2022-04-08 03:25:53 +02:00
2023-07-05 04:56:37 +02:00
if(in_array('credit.status', $this->input['report_keys'])) {
$entity['credit.status'] = $credit->stringStatus($credit->status_id);
}
if (in_array('credit.assigned_user_id', $this->input['report_keys'])) {
$entity['credit.assigned_user_id'] = $credit->assigned_user ? $credit->assigned_user->present()->name(): '';
}
if (in_array('credit.user_id', $this->input['report_keys'])) {
$entity['credit.user_id'] = $credit->user ? $credit->user->present()->name(): '';
}
2022-04-08 03:25:53 +02:00
return $entity;
}
}