1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Report extension

This commit is contained in:
David Bomba 2023-07-13 21:10:54 +10:00
parent 4f8ba1c930
commit df4620ac62
3 changed files with 200 additions and 55 deletions

View File

@ -264,6 +264,58 @@ class BaseExport
"assigned_user" => "payment.assigned_user_id",
];
protected array $expense_report_keys = [
'amount' => 'expense.amount',
'category' => 'expense.category_id',
'client' => 'expense.client_id',
'custom_value1' => 'expense.custom_value1',
'custom_value2' => 'expense.custom_value2',
'custom_value3' => 'expense.custom_value3',
'custom_value4' => 'expense.custom_value4',
'currency' => 'expense.currency_id',
'date' => 'expense.date',
'exchange_rate' => 'expense.exchange_rate',
'converted_amount' => 'expense.foreign_amount',
'invoice_currency_id' => 'expense.invoice_currency_id',
'payment_date' => 'expense.payment_date',
'number' => 'expense.number',
'payment_type_id' => 'expense.payment_type_id',
'private_notes' => 'expense.private_notes',
'project' => 'expense.project_id',
'public_notes' => 'expense.public_notes',
'tax_amount1' => 'expense.tax_amount1',
'tax_amount2' => 'expense.tax_amount2',
'tax_amount3' => 'expense.tax_amount3',
'tax_name1' => 'expense.tax_name1',
'tax_name2' => 'expense.tax_name2',
'tax_name3' => 'expense.tax_name3',
'tax_rate1' => 'expense.tax_rate1',
'tax_rate2' => 'expense.tax_rate2',
'tax_rate3' => 'expense.tax_rate3',
'transaction_reference' => 'expense.transaction_reference',
'vendor' => 'expense.vendor_id',
'invoice' => 'expense.invoice_id',
'user' => 'expense.user',
'assigned_user' => 'expense.assigned_user',
];
protected array $task_report_keys = [
'start_date' => 'task.start_date',
'end_date' => 'task.end_date',
'duration' => 'task.duration',
'rate' => 'task.rate',
'number' => 'task.number',
'description' => 'task.description',
'custom_value1' => 'task.custom_value1',
'custom_value2' => 'task.custom_value2',
'custom_value3' => 'task.custom_value3',
'custom_value4' => 'task.custom_value4',
'status' => 'task.status_id',
'project' => 'task.project_id',
'invoice' => 'task.invoice_id',
'client' => 'task.client_id',
];
protected function filterByClients($query)
{
if (isset($this->input['client_id']) && $this->input['client_id'] != 'all') {
@ -289,6 +341,7 @@ class BaseExport
match($parts[0]) {
'contact' => $value = $this->resolveClientContactKey($parts[1], $entity, $transformer),
'client' => $value = $this->resolveClientKey($parts[1], $entity, $transformer),
'expense' => $value = $this->resolveExpenseKey($parts[1], $entity, $transformer),
'vendor' => $value = $this->resolveVendorKey($parts[1], $entity, $transformer),
'vendor_contact' => $value = $this->resolveVendorContactKey($parts[1], $entity, $transformer),
'invoice' => $value = $this->resolveInvoiceKey($parts[1], $entity, $transformer),
@ -302,24 +355,62 @@ class BaseExport
private function resolveClientContactKey($column, $entity, $transformer)
{
if(!$entity->client) {
return "";
}
$primary_contact = $entity->client->primary_contact()->first() ?? $entity->client->contacts()->first();
return $primary_contact?->{$column} ?? '';
return $primary_contact ? $primary_contact?->{$column} ?? '' : '';
}
private function resolveVendorContactKey($column, $entity, $transformer)
{
if(!$entity->vendor)
return "";
$primary_contact = $entity->vendor->primary_contact()->first() ?? $entity->vendor->contacts()->first();
return $primary_contact?->{$column} ?? '';
return $primary_contact ? $primary_contact?->{$column} ?? '' : '';
}
private function resolveExpenseKey($column, $entity, $transformer)
{
// $transformed_entity = $transformer->includeExpense($entity);
// $manager = new Manager();
// $manager->setSerializer(new ArraySerializer());
// $transformed_entity = $manager->createData($transformed_entity)->toArray();
if($column == 'user' && $entity?->expense?->user)
return $entity->expense->user->present()->name() ?? ' ';
if($column == 'assigned_user' && $entity?->expense?->assigned_user)
return $entity->expense->assigned_user->present()->name() ?? ' ';
if($column == 'category' && $entity->expense) {
return $entity->expense->category?->name ?? ' ';
}
if(property_exists($entity, $column))
return $entity?->{$column} ?? '';
nlog("export: Could not resolve expense key: {$column}");
return '';
}
private function resolveVendorKey($column, $entity, $transformer)
{
if(!$entity->vendor)
return '';
$transformed_entity = $transformer->includeVendor($entity);
$manager = new Manager();
@ -327,10 +418,10 @@ class BaseExport
$transformed_entity = $manager->createData($transformed_entity)->toArray();
if($column == 'name')
return $transformed_entity['display_name'];
return $entity->vendor->present()->name() ?: '';
if($column == 'user_id')
return $entity->vendor->user->present()->name();
return $entity->vendor->user->present()->name() ?: '';
if($column == 'country_id')
return $entity->vendor->country ? ctrans("texts.country_{$entity->vendor->country->name}") : '';
@ -340,9 +431,7 @@ class BaseExport
}
if($column == 'status')
return $entity->stringStatus($entity->status_id);
return $entity->stringStatus($entity->status_id) ?: '';
if(array_key_exists($column, $transformed_entity))
return $transformed_entity[$column];
@ -356,6 +445,10 @@ class BaseExport
private function resolveClientKey($column, $entity, $transformer)
{
if(!$entity->client)
return '';
$transformed_client = $transformer->includeClient($entity);
$manager = new Manager();
@ -622,7 +715,7 @@ class BaseExport
public function buildHeader() :array
{
$header = [];
nlog($this->input['report_keys']);
foreach (array_merge($this->input['report_keys'], $this->forced_keys) as $value) {
@ -662,6 +755,26 @@ class BaseExport
$key = array_search($value, $this->item_report_keys);
}
if(!$key) {
$prefix = ctrans('texts.expense');
$key = array_search($value, $this->expense_report_keys);
}
if(!$key) {
$prefix = ctrans('texts.task');
$key = array_search($value, $this->task_report_keys);
}
if(!$key) {
$prefix = ctrans('texts.vendor');
$key = array_search($value, $this->vendor_report_keys);
}
if(!$key) {
$prefix = ctrans('texts.purchase_order');
$key = array_search($value, $this->purchase_order_report_keys);
}
if(!$key) {
$prefix = '';
}
@ -676,6 +789,7 @@ class BaseExport
$key = str_replace('vendor.', '', $key);
$key = str_replace('contact.', '', $key);
$key = str_replace('payment.', '', $key);
$key = str_replace('expense.', '', $key);
$header[] = "{$prefix} " . ctrans("texts.{$key}");
}

View File

@ -30,36 +30,38 @@ class ExpenseExport extends BaseExport
public Writer $csv;
public array $entity_keys = [
'amount' => 'amount',
'category' => 'category_id',
'client' => 'client_id',
'custom_value1' => 'custom_value1',
'custom_value2' => 'custom_value2',
'custom_value3' => 'custom_value3',
'custom_value4' => 'custom_value4',
'currency' => 'currency_id',
'date' => 'date',
'exchange_rate' => 'exchange_rate',
'converted_amount' => 'foreign_amount',
'invoice_currency_id' => 'invoice_currency_id',
'payment_date' => 'payment_date',
'number' => 'number',
'payment_type_id' => 'payment_type_id',
'private_notes' => 'private_notes',
'project' => 'project_id',
'public_notes' => 'public_notes',
'tax_amount1' => 'tax_amount1',
'tax_amount2' => 'tax_amount2',
'tax_amount3' => 'tax_amount3',
'tax_name1' => 'tax_name1',
'tax_name2' => 'tax_name2',
'tax_name3' => 'tax_name3',
'tax_rate1' => 'tax_rate1',
'tax_rate2' => 'tax_rate2',
'tax_rate3' => 'tax_rate3',
'transaction_reference' => 'transaction_reference',
'vendor' => 'vendor_id',
'invoice' => 'invoice_id',
'amount' => 'expense.amount',
'category' => 'expense.category',
'client' => 'expense.client_id',
'custom_value1' => 'expense.custom_value1',
'custom_value2' => 'expense.custom_value2',
'custom_value3' => 'expense.custom_value3',
'custom_value4' => 'expense.custom_value4',
'currency' => 'expense.currency_id',
'date' => 'expense.date',
'exchange_rate' => 'expense.exchange_rate',
'converted_amount' => 'expense.foreign_amount',
'invoice_currency_id' => 'expense.invoice_currency_id',
'payment_date' => 'expense.payment_date',
'number' => 'expense.number',
'payment_type_id' => 'expense.payment_type_id',
'private_notes' => 'expense.private_notes',
'project' => 'expense.project_id',
'public_notes' => 'expense.public_notes',
'tax_amount1' => 'expense.tax_amount1',
'tax_amount2' => 'expense.tax_amount2',
'tax_amount3' => 'expense.tax_amount3',
'tax_name1' => 'expense.tax_name1',
'tax_name2' => 'expense.tax_name2',
'tax_name3' => 'expense.tax_name3',
'tax_rate1' => 'expense.tax_rate1',
'tax_rate2' => 'expense.tax_rate2',
'tax_rate3' => 'expense.tax_rate3',
'transaction_reference' => 'expense.transaction_reference',
'vendor' => 'expense.vendor_id',
'invoice' => 'expense.invoice_id',
'user' => 'expense.user',
'assigned_user' => 'expense.assigned_user',
];
private array $decorate_keys = [
@ -120,13 +122,17 @@ class ExpenseExport extends BaseExport
$entity = [];
foreach (array_values($this->input['report_keys']) as $key) {
$parts = explode('.', $key);
$keyval = array_search($key, $this->entity_keys);
if (array_key_exists($key, $transformed_expense)) {
$entity[$keyval] = $transformed_expense[$key];
if (is_array($parts) && $parts[0] == 'expense' && array_key_exists($parts[1], $transformed_expense)) {
$entity[$key] = $transformed_expense[$parts[1]];
} elseif (array_key_exists($key, $transformed_expense)) {
$entity[$key] = $transformed_expense[$key];
} else {
$entity[$keyval] = '';
$entity[$key] = $this->resolveKey($key, $expense, $this->expense_transformer);
}
}
return $this->decorateAdvancedFields($expense, $entity);
@ -134,34 +140,44 @@ class ExpenseExport extends BaseExport
private function decorateAdvancedFields(Expense $expense, array $entity) :array
{
if (in_array('currency_id', $this->input['report_keys'])) {
$entity['currency'] = $expense->currency ? $expense->currency->code : '';
if (in_array('expense.currency_id', $this->input['report_keys'])) {
$entity['expense.currency_id'] = $expense->currency ? $expense->currency->code : '';
}
if (in_array('client_id', $this->input['report_keys'])) {
$entity['client'] = $expense->client ? $expense->client->present()->name() : '';
if (in_array('expense.client_id', $this->input['report_keys'])) {
$entity['expense.client'] = $expense->client ? $expense->client->present()->name() : '';
}
if (in_array('invoice_id', $this->input['report_keys'])) {
$entity['invoice'] = $expense->invoice ? $expense->invoice->number : '';
if (in_array('expense.invoice_id', $this->input['report_keys'])) {
$entity['expense.invoice_id'] = $expense->invoice ? $expense->invoice->number : '';
}
if (in_array('category_id', $this->input['report_keys'])) {
$entity['category'] = $expense->category ? $expense->category->name : '';
if (in_array('expense.category', $this->input['report_keys'])) {
$entity['expense.category'] = $expense->category ? $expense->category->name : '';
}
if (in_array('vendor_id', $this->input['report_keys'])) {
$entity['vendor'] = $expense->vendor ? $expense->vendor->name : '';
if (in_array('expense.vendor_id', $this->input['report_keys'])) {
$entity['expense.vendor'] = $expense->vendor ? $expense->vendor->name : '';
}
if (in_array('payment_type_id', $this->input['report_keys'])) {
$entity['payment_type'] = $expense->payment_type ? $expense->payment_type->name : '';
if (in_array('expense.payment_type_id', $this->input['report_keys'])) {
$entity['expense.payment_type_id'] = $expense->payment_type ? $expense->payment_type->name : '';
}
if (in_array('project_id', $this->input['report_keys'])) {
$entity['project'] = $expense->project ? $expense->project->name : '';
if (in_array('expense.project_id', $this->input['report_keys'])) {
$entity['expense.project_id'] = $expense->project ? $expense->project->name : '';
}
if (in_array('expense.user', $this->input['report_keys'])) {
$entity['expense.user'] = $expense->user ? $expense->user->present()->name() : '';
}
if (in_array('expense.assigned_user', $this->input['report_keys'])) {
$entity['expense.assigned_user'] = $expense->assigned_user ? $expense->assigned_user->present()->name() : '';
}
nlog($entity);
return $entity;
}
}

View File

@ -275,4 +275,19 @@ class Expense extends BaseModel
return $this->belongsTo(BankTransaction::class);
}
public function stringStatus()
{
if($this->is_deleted)
return ctrans('texts.deleted');
elseif($this->payment_date)
return ctrans('texts.paid');
elseif($this->invoice_id)
return ctrans('texts.invoiced');
elseif($this->should_be_invoiced)
return ctrans('texts.pending');
elseif($this->trashed())
return ctrans('texts.archived');
return ctrans('texts.logged');
}
}