1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 15:42:51 +01:00
invoiceninja/app/Export/CSV/CreditExport.php

253 lines
8.4 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
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. 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) {
2024-08-22 08:45:06 +02:00
2024-07-14 13:28:54 +02:00
/** @var \App\Models\Credit $credit */
2023-08-18 15:36:54 +02:00
$row = $this->buildRow($credit);
return $this->processMetaData($row, $credit);
})->toArray();
2024-01-14 05:05:00 +01: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) {
2024-01-14 05:05:00 +01:00
2023-08-18 15:36:54 +02:00
$report_keys = explode(".", $value);
2024-01-14 05:05:00 +01:00
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')
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);
})
2023-08-24 05:22:35 +02:00
->where('company_id', $this->company->id)
->where('is_deleted', $this->input['include_deleted'] ?? false);
2023-08-18 13:36:50 +02:00
$query = $this->addDateRange($query, 'credits');
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
if($this->input['status'] ?? false) {
$query = $this->addCreditStatusFilter($query, $this->input['status']);
}
if($this->input['document_email_attachment'] ?? false) {
$this->queueDocuments($query);
}
2023-08-18 13:36:50 +02:00
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();
2024-05-01 00:32:11 +02:00
\League\Csv\CharsetConverter::addTo($this->csv, 'UTF-8', 'UTF-8');
2022-04-08 03:25:53 +02:00
//insert the header
$this->csv->insertOne($this->buildHeader());
2022-04-27 07:41:52 +02:00
$query->cursor()
->each(function ($credit) {
2024-07-14 13:28:54 +02:00
/** @var \App\Models\Credit $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
}
2024-01-14 05:05:00 +01: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) {
2024-01-14 05:05:00 +01:00
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;
2024-01-14 05:05:00 +01: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
}
2024-03-05 03:58:26 +01:00
public function addCreditStatusFilter($query, $status): Builder
{
$status_parameters = explode(',', $status);
if (in_array('all', $status_parameters)) {
return $query;
}
$credit_filters = [];
if (in_array('draft', $status_parameters)) {
$credit_filters[] = Credit::STATUS_DRAFT;
}
if (in_array('sent', $status_parameters)) {
$credit_filters[] = Credit::STATUS_SENT;
}
if (in_array('partial', $status_parameters)) {
$credit_filters[] = Credit::STATUS_PARTIAL;
}
if (in_array('applied', $status_parameters)) {
$credit_filters[] = Credit::STATUS_APPLIED;
}
if (count($credit_filters) >= 1) {
$query->whereIn('status_id', $credit_filters);
}
return $query;
}
2024-01-14 05:05:00 +01:00
private function decorateAdvancedFields(Credit $credit, array $entity): array
2022-04-08 03:25:53 +02:00
{
2023-12-12 00:46:23 +01:00
// if (in_array('country_id', $this->input['report_keys'])) {
// $entity['country'] = $credit->client->country ? ctrans("texts.country_{$credit->client->country->name}") : '';
// }
2024-01-14 05:05:00 +01:00
2023-12-12 00:46:23 +01:00
// if (in_array('currency_id', $this->input['report_keys'])) {
// $entity['currency_id'] = $credit->client->currency() ? $credit->client->currency()->code : $credit->company->currency()->code;
// }
2022-04-08 03:54:17 +02:00
2023-12-12 00:46:23 +01: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
2023-12-12 00:46:23 +01:00
// if (in_array('client_id', $this->input['report_keys'])) {
// $entity['client'] = $credit->client->present()->name();
// }
2022-04-08 03:25:53 +02:00
2023-12-12 00:46:23 +01:00
// if (in_array('status_id', $this->input['report_keys'])) {
// $entity['status'] = $credit->stringStatus($credit->status_id);
// }
2022-04-08 03:25:53 +02:00
2023-12-12 00:46:23 +01:00
// if(in_array('credit.status', $this->input['report_keys'])) {
// $entity['credit.status'] = $credit->stringStatus($credit->status_id);
// }
2023-07-05 04:56:37 +02:00
if (in_array('credit.assigned_user_id', $this->input['report_keys'])) {
2024-01-14 05:05:00 +01:00
$entity['credit.assigned_user_id'] = $credit->assigned_user ? $credit->assigned_user->present()->name() : '';
}
2024-01-14 05:05:00 +01:00
if (in_array('credit.user_id', $this->input['report_keys'])) {
2024-06-28 07:16:08 +02:00
$entity['credit.user_id'] = $credit->user ? $credit->user->present()->name() : ''; //@phpstan-ignore-line
}
2022-04-08 03:25:53 +02:00
return $entity;
}
}