mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Refactor for report exports
This commit is contained in:
parent
b1de2e6b3b
commit
fec6fe8f31
@ -13,6 +13,7 @@ namespace App\Export\CSV;
|
|||||||
|
|
||||||
use App\Utils\Number;
|
use App\Utils\Number;
|
||||||
use App\Models\Client;
|
use App\Models\Client;
|
||||||
|
use App\Utils\Helpers;
|
||||||
use App\Models\Company;
|
use App\Models\Company;
|
||||||
use App\Models\Expense;
|
use App\Models\Expense;
|
||||||
use App\Models\Invoice;
|
use App\Models\Invoice;
|
||||||
@ -28,7 +29,7 @@ use League\Fractal\Serializer\ArraySerializer;
|
|||||||
class BaseExport
|
class BaseExport
|
||||||
{
|
{
|
||||||
use MakesHash;
|
use MakesHash;
|
||||||
|
|
||||||
public Company $company;
|
public Company $company;
|
||||||
|
|
||||||
public array $input;
|
public array $input;
|
||||||
@ -43,8 +44,6 @@ class BaseExport
|
|||||||
|
|
||||||
public string $client_description = 'All Clients';
|
public string $client_description = 'All Clients';
|
||||||
|
|
||||||
public array $forced_keys = [];
|
|
||||||
|
|
||||||
protected array $vendor_report_keys = [
|
protected array $vendor_report_keys = [
|
||||||
'address1' => 'vendor.address1',
|
'address1' => 'vendor.address1',
|
||||||
'address2' => 'vendor.address2',
|
'address2' => 'vendor.address2',
|
||||||
@ -271,6 +270,7 @@ class BaseExport
|
|||||||
"date" => "credit.date",
|
"date" => "credit.date",
|
||||||
"due_date" => "credit.due_date",
|
"due_date" => "credit.due_date",
|
||||||
"terms" => "credit.terms",
|
"terms" => "credit.terms",
|
||||||
|
"discount" => "credit.discount",
|
||||||
"footer" => "credit.footer",
|
"footer" => "credit.footer",
|
||||||
"status" => "credit.status",
|
"status" => "credit.status",
|
||||||
"public_notes" => "credit.public_notes",
|
"public_notes" => "credit.public_notes",
|
||||||
@ -283,6 +283,10 @@ class BaseExport
|
|||||||
"surcharge2" => "credit.custom_surcharge2",
|
"surcharge2" => "credit.custom_surcharge2",
|
||||||
"surcharge3" => "credit.custom_surcharge3",
|
"surcharge3" => "credit.custom_surcharge3",
|
||||||
"surcharge4" => "credit.custom_surcharge4",
|
"surcharge4" => "credit.custom_surcharge4",
|
||||||
|
"custom_value1" => "credit.custom_value1",
|
||||||
|
"custom_value2" => "credit.custom_value2",
|
||||||
|
"custom_value3" => "credit.custom_value3",
|
||||||
|
"custom_value4" => "credit.custom_value4",
|
||||||
"exchange_rate" => "credit.exchange_rate",
|
"exchange_rate" => "credit.exchange_rate",
|
||||||
"tax_amount" => "credit.total_taxes",
|
"tax_amount" => "credit.total_taxes",
|
||||||
"assigned_user" => "credit.assigned_user_id",
|
"assigned_user" => "credit.assigned_user_id",
|
||||||
@ -832,11 +836,13 @@ class BaseExport
|
|||||||
|
|
||||||
public function buildHeader() :array
|
public function buildHeader() :array
|
||||||
{
|
{
|
||||||
|
$helper = new Helpers();
|
||||||
|
|
||||||
$header = [];
|
$header = [];
|
||||||
|
|
||||||
// nlog($this->input['report_keys']);
|
// nlog($this->input['report_keys']);
|
||||||
|
|
||||||
foreach (array_merge($this->input['report_keys'], $this->forced_keys) as $value) {
|
foreach ($this->input['report_keys'] as $value) {
|
||||||
|
|
||||||
$key = array_search($value, $this->entity_keys);
|
$key = array_search($value, $this->entity_keys);
|
||||||
nlog("{$key} => {$value}");
|
nlog("{$key} => {$value}");
|
||||||
@ -913,11 +919,20 @@ class BaseExport
|
|||||||
$key = str_replace('contact.', '', $key);
|
$key = str_replace('contact.', '', $key);
|
||||||
$key = str_replace('payment.', '', $key);
|
$key = str_replace('payment.', '', $key);
|
||||||
$key = str_replace('expense.', '', $key);
|
$key = str_replace('expense.', '', $key);
|
||||||
// nlog($key);
|
|
||||||
if(in_array($key, ['quote1','quote2','quote3','quote4','credit1','credit2','credit3','credit4','purchase_order1','purchase_order2','purchase_order3','purchase_order4']))
|
// if( in_array($key, ['quote1','quote2','quote3','quote4','credit1','credit2','credit3','credit4','purchase_order1','purchase_order2','purchase_order3','purchase_order4']))
|
||||||
|
if(stripos($value, 'custom_value') !== false)
|
||||||
{
|
{
|
||||||
$number = substr($key, -1);
|
$parts = explode(".", $value);
|
||||||
$header[] = ctrans('texts.item') . " ". ctrans("texts.custom_value{$number}");
|
|
||||||
|
if(in_array($parts[0], ['credit','quote','invoice','purchase_order','recurring_invoice'])){
|
||||||
|
$entity = "invoice".substr($parts[1], -1);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$entity = $parts[0].substr($parts[1], -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
$header[] = $helper->makeCustomField($this->company->custom_fields, $entity);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -925,8 +940,6 @@ class BaseExport
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// nlog($header);
|
|
||||||
|
|
||||||
return $header;
|
return $header;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,9 +79,6 @@ class ClientExport extends BaseExport
|
|||||||
'client.industry',
|
'client.industry',
|
||||||
];
|
];
|
||||||
|
|
||||||
public array $forced_keys = [
|
|
||||||
];
|
|
||||||
|
|
||||||
public function __construct(Company $company, array $input)
|
public function __construct(Company $company, array $input)
|
||||||
{
|
{
|
||||||
$this->company = $company;
|
$this->company = $company;
|
||||||
|
@ -30,51 +30,6 @@ class CreditExport extends BaseExport
|
|||||||
|
|
||||||
public Writer $csv;
|
public Writer $csv;
|
||||||
|
|
||||||
public array $entity_keys = [
|
|
||||||
'amount' => 'amount',
|
|
||||||
'balance' => 'balance',
|
|
||||||
'client' => 'client_id',
|
|
||||||
'country' => 'country_id',
|
|
||||||
'custom_surcharge1' => 'custom_surcharge1',
|
|
||||||
'custom_surcharge2' => 'custom_surcharge2',
|
|
||||||
'custom_surcharge3' => 'custom_surcharge3',
|
|
||||||
'custom_surcharge4' => 'custom_surcharge4',
|
|
||||||
'currency' => 'currency',
|
|
||||||
'custom_value1' => 'custom_value1',
|
|
||||||
'custom_value2' => 'custom_value2',
|
|
||||||
'custom_value3' => 'custom_value3',
|
|
||||||
'custom_value4' => 'custom_value4',
|
|
||||||
'date' => 'date',
|
|
||||||
'discount' => 'discount',
|
|
||||||
'due_date' => 'due_date',
|
|
||||||
'exchange_rate' => 'exchange_rate',
|
|
||||||
'footer' => 'footer',
|
|
||||||
'invoice' => 'invoice_id',
|
|
||||||
'number' => 'number',
|
|
||||||
'paid_to_date' => 'paid_to_date',
|
|
||||||
'partial' => 'partial',
|
|
||||||
'partial_due_date' => 'partial_due_date',
|
|
||||||
'po_number' => 'po_number',
|
|
||||||
'private_notes' => 'private_notes',
|
|
||||||
'public_notes' => 'public_notes',
|
|
||||||
'status' => 'status_id',
|
|
||||||
'tax_name1' => 'tax_name1',
|
|
||||||
'tax_name2' => 'tax_name2',
|
|
||||||
'tax_name3' => 'tax_name3',
|
|
||||||
'tax_rate1' => 'tax_rate1',
|
|
||||||
'tax_rate2' => 'tax_rate2',
|
|
||||||
'tax_rate3' => 'tax_rate3',
|
|
||||||
'terms' => 'terms',
|
|
||||||
'total_taxes' => 'total_taxes',
|
|
||||||
];
|
|
||||||
|
|
||||||
private array $decorate_keys = [
|
|
||||||
'country',
|
|
||||||
'client',
|
|
||||||
'invoice',
|
|
||||||
'currency',
|
|
||||||
];
|
|
||||||
|
|
||||||
public function __construct(Company $company, array $input)
|
public function __construct(Company $company, array $input)
|
||||||
{
|
{
|
||||||
$this->company = $company;
|
$this->company = $company;
|
||||||
@ -86,13 +41,13 @@ class CreditExport extends BaseExport
|
|||||||
{
|
{
|
||||||
$query = $this->init();
|
$query = $this->init();
|
||||||
|
|
||||||
$headerdisplay = $this->buildHeader();
|
|
||||||
$header = [];
|
$header = [];
|
||||||
|
$headerdisplay = $this->buildHeader();
|
||||||
|
|
||||||
foreach ($this->input['report_keys'] as $key => $value) {
|
foreach ($this->input['report_keys'] as $key => $value) {
|
||||||
$header[] = ['identifier' => $value, 'display_value' => $headerdisplay[$key]];
|
$header[] = ['identifier' => $value, 'display_value' => $headerdisplay[$key]];
|
||||||
}
|
}
|
||||||
|
|
||||||
$report = $query->cursor()
|
$report = $query->cursor()
|
||||||
->map(function ($credit) {
|
->map(function ($credit) {
|
||||||
$row = $this->buildRow($credit);
|
$row = $this->buildRow($credit);
|
||||||
@ -105,21 +60,18 @@ class CreditExport extends BaseExport
|
|||||||
private function processMetaData(array $row, Credit $credit): array
|
private function processMetaData(array $row, Credit $credit): array
|
||||||
{
|
{
|
||||||
$clean_row = [];
|
$clean_row = [];
|
||||||
|
foreach (array_values($this->input['report_keys']) as $key => $value) {
|
||||||
foreach ($this->input['report_keys'] as $key => $value) {
|
|
||||||
|
|
||||||
$report_keys = explode(".", $value);
|
$report_keys = explode(".", $value);
|
||||||
|
|
||||||
$column_key = str_replace("credit.", "", $value);
|
$column_key = $value;
|
||||||
$column_key = array_search($column_key, $this->entity_keys);
|
|
||||||
|
|
||||||
$clean_row[$key]['entity'] = $report_keys[0];
|
$clean_row[$key]['entity'] = $report_keys[0];
|
||||||
$clean_row[$key]['id'] = $report_keys[1] ?? $report_keys[0];
|
$clean_row[$key]['id'] = $report_keys[1] ?? $report_keys[0];
|
||||||
$clean_row[$key]['hashed_id'] = $report_keys[0] == 'credit' ? null : $credit->{$report_keys[0]}->hashed_id ?? null;
|
$clean_row[$key]['hashed_id'] = $report_keys[0] == 'credit' ? null : $credit->{$report_keys[0]}->hashed_id ?? null;
|
||||||
$clean_row[$key]['value'] = $row[$column_key];
|
$clean_row[$key]['value'] = $row[$column_key];
|
||||||
$clean_row[$key]['identifier'] = $value;
|
$clean_row[$key]['identifier'] = $value;
|
||||||
|
|
||||||
if(in_array($clean_row[$key]['id'], ['amount', 'balance', 'partial', 'refunded', 'applied','unit_cost','cost','price']))
|
if(in_array($clean_row[$key]['id'], ['paid_to_date','total_taxes','amount', 'balance', 'partial', 'refunded', 'applied','unit_cost','cost','price']))
|
||||||
$clean_row[$key]['display_value'] = Number::formatMoney($row[$column_key], $credit->client);
|
$clean_row[$key]['display_value'] = Number::formatMoney($row[$column_key], $credit->client);
|
||||||
else
|
else
|
||||||
$clean_row[$key]['display_value'] = $row[$column_key];
|
$clean_row[$key]['display_value'] = $row[$column_key];
|
||||||
@ -139,12 +91,13 @@ class CreditExport extends BaseExport
|
|||||||
$t->replace(Ninja::transformTranslations($this->company->settings));
|
$t->replace(Ninja::transformTranslations($this->company->settings));
|
||||||
|
|
||||||
if (count($this->input['report_keys']) == 0) {
|
if (count($this->input['report_keys']) == 0) {
|
||||||
$this->input['report_keys'] = array_values($this->entity_keys);
|
$this->input['report_keys'] = array_values($this->credit_report_keys);
|
||||||
}
|
}
|
||||||
|
|
||||||
$query = Credit::query()
|
$query = Credit::query()
|
||||||
->withTrashed()
|
->withTrashed()
|
||||||
->with('client')->where('company_id', $this->company->id)
|
->with('client')
|
||||||
|
->where('company_id', $this->company->id)
|
||||||
->where('is_deleted', 0);
|
->where('is_deleted', 0);
|
||||||
|
|
||||||
$query = $this->addDateRange($query);
|
$query = $this->addDateRange($query);
|
||||||
@ -160,11 +113,9 @@ class CreditExport extends BaseExport
|
|||||||
|
|
||||||
//insert the header
|
//insert the header
|
||||||
$this->csv->insertOne($this->buildHeader());
|
$this->csv->insertOne($this->buildHeader());
|
||||||
// nlog($this->input['report_keys']);
|
|
||||||
|
|
||||||
$query->cursor()
|
$query->cursor()
|
||||||
->each(function ($credit) {
|
->each(function ($credit) {
|
||||||
// nlog($this->buildRow($credit));
|
|
||||||
$this->csv->insertOne($this->buildRow($credit));
|
$this->csv->insertOne($this->buildRow($credit));
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -178,22 +129,22 @@ class CreditExport extends BaseExport
|
|||||||
$entity = [];
|
$entity = [];
|
||||||
|
|
||||||
foreach (array_values($this->input['report_keys']) as $key) {
|
foreach (array_values($this->input['report_keys']) as $key) {
|
||||||
$keyval = array_search($key, $this->entity_keys);
|
|
||||||
|
$keyval = $key;
|
||||||
if(!$keyval)
|
$credit_key = str_replace("credit.", "", $key);
|
||||||
$keyval = array_search(str_replace("credit.", "", $key), $this->entity_keys) ?? $key;
|
$searched_credit_key = array_search(str_replace("credit.", "", $key), $this->credit_report_keys) ?? $key;
|
||||||
|
|
||||||
if(!$keyval)
|
|
||||||
$keyval = $key;
|
|
||||||
|
|
||||||
if (array_key_exists($key, $transformed_credit)) {
|
if (isset($transformed_credit[$credit_key])) {
|
||||||
$entity[$keyval] = $transformed_credit[$key];
|
$entity[$keyval] = $transformed_credit[$credit_key];
|
||||||
} elseif (array_key_exists($keyval, $transformed_credit)) {
|
} elseif (isset($transformed_credit[$keyval])) {
|
||||||
$entity[$keyval] = $transformed_credit[$keyval];
|
$entity[$keyval] = $transformed_credit[$keyval];
|
||||||
|
} elseif(isset($transformed_credit[$searched_credit_key])){
|
||||||
|
$entity[$keyval] = $transformed_credit[$searched_credit_key];
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$entity[$keyval] = $this->resolveKey($keyval, $credit, $this->credit_transformer);
|
$entity[$keyval] = $this->resolveKey($keyval, $credit, $this->credit_transformer);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->decorateAdvancedFields($credit, $entity);
|
return $this->decorateAdvancedFields($credit, $entity);
|
||||||
|
@ -67,10 +67,6 @@ class VendorExport extends BaseExport
|
|||||||
'vendor.currency',
|
'vendor.currency',
|
||||||
];
|
];
|
||||||
|
|
||||||
public array $forced_keys = [
|
|
||||||
// 'vendor.status'
|
|
||||||
];
|
|
||||||
|
|
||||||
public function __construct(Company $company, array $input)
|
public function __construct(Company $company, array $input)
|
||||||
{
|
{
|
||||||
$this->company = $company;
|
$this->company = $company;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user