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

Fixes for report exports

This commit is contained in:
David Bomba 2023-10-18 15:22:38 +11:00
parent d62bdbedcb
commit 1163c42fd8
12 changed files with 110 additions and 10 deletions

View File

@ -231,7 +231,7 @@ class BaseExport
'po_number' => 'purchase_order.po_number',
'private_notes' => 'purchase_order.private_notes',
'public_notes' => 'purchase_order.public_notes',
'status' => 'purchase_order.status_id',
'status' => 'purchase_order.status',
'tax_name1' => 'purchase_order.tax_name1',
'tax_name2' => 'purchase_order.tax_name2',
'tax_name3' => 'purchase_order.tax_name3',
@ -430,6 +430,14 @@ class BaseExport
'project' => 'task.project_id',
];
protected array $forced_client_fields = [
"name" => "client.name",
];
protected array $forced_vendor_fields = [
"name" => "vendor.name",
];
protected function filterByClients($query)
{
if (isset($this->input['client_id']) && $this->input['client_id'] != 'all') {

View File

@ -93,6 +93,8 @@ class CreditExport extends BaseExport
$this->input['report_keys'] = array_values($this->credit_report_keys);
}
$this->input['report_keys'] = array_merge($this->input['report_keys'], array_diff($this->forced_client_fields, $this->input['report_keys']));
$query = Credit::query()
->withTrashed()
->with('client')

View File

@ -50,6 +50,8 @@ class InvoiceExport extends BaseExport
$this->input['report_keys'] = array_values($this->invoice_report_keys);
}
$this->input['report_keys'] = array_merge($this->input['report_keys'], array_diff($this->forced_client_fields, $this->input['report_keys']));
$query = Invoice::query()
->withTrashed()
->with('client')

View File

@ -62,6 +62,8 @@ class InvoiceItemExport extends BaseExport
$this->input['report_keys'] = array_values($this->mergeItemsKeys('invoice_report_keys'));
}
$this->input['report_keys'] = array_merge($this->input['report_keys'], array_diff($this->forced_client_fields, $this->input['report_keys']));
$query = Invoice::query()
->withTrashed()
->with('client')
@ -200,6 +202,27 @@ class InvoiceItemExport extends BaseExport
$entity['tax_category'] = $invoice->taxTypeString($entity['tax_category']);
}
if (in_array('invoice.country_id', $this->input['report_keys'])) {
$entity['invoice.country_id'] = $invoice->client->country ? ctrans("texts.country_{$invoice->client->country->name}") : '';
}
if (in_array('invoice.currency_id', $this->input['report_keys'])) {
$entity['invoice.currency_id'] = $invoice->client->currency() ? $invoice->client->currency()->code : $invoice->company->currency()->code;
}
if (in_array('invoice.client_id', $this->input['report_keys'])) {
$entity['invoice.client_id'] = $invoice->client->present()->name();
}
if (in_array('invoice.status', $this->input['report_keys'])) {
$entity['invoice.status'] = $invoice->stringStatus($invoice->status_id);
}
if (in_array('invoice.recurring_id', $this->input['report_keys'])) {
$entity['invoice.recurring_id'] = $invoice->recurring_invoice->number ?? '';
}
return $entity;
}

View File

@ -48,6 +48,8 @@ class PaymentExport extends BaseExport
$this->input['report_keys'] = array_values($this->payment_report_keys);
}
$this->input['report_keys'] = array_merge($this->input['report_keys'], array_diff($this->forced_client_fields, $this->input['report_keys']));
$query = Payment::query()
->withTrashed()
->where('company_id', $this->company->id)

View File

@ -54,7 +54,7 @@ class PurchaseOrderExport extends BaseExport
'po_number' => 'purchase_order.po_number',
'private_notes' => 'purchase_order.private_notes',
'public_notes' => 'purchase_order.public_notes',
'status' => 'purchase_order.status_id',
'status' => 'purchase_order.status',
'tax_name1' => 'purchase_order.tax_name1',
'tax_name2' => 'purchase_order.tax_name2',
'tax_name3' => 'purchase_order.tax_name3',
@ -95,6 +95,9 @@ class PurchaseOrderExport extends BaseExport
if (count($this->input['report_keys']) == 0) {
$this->input['report_keys'] = array_values($this->purchase_order_report_keys);
}
$this->input['report_keys'] = array_merge($this->input['report_keys'], array_diff($this->forced_vendor_fields, $this->input['report_keys']));
$query = PurchaseOrder::query()
->withTrashed()
->with('vendor')
@ -181,8 +184,8 @@ class PurchaseOrderExport extends BaseExport
$entity['vendor'] = $purchase_order->vendor->present()->name();
}
if (in_array('status_id', $this->input['report_keys'])) {
$entity['status'] = $purchase_order->stringStatus($purchase_order->status_id);
if (in_array('purchase_order.status', $this->input['report_keys'])) {
$entity['purchase_order.status'] = $purchase_order->stringStatus($purchase_order->status_id);
}
return $entity;

View File

@ -55,6 +55,8 @@ class PurchaseOrderItemExport extends BaseExport
$this->input['report_keys'] = array_values($this->mergeItemsKeys('purchase_order_report_keys'));
}
$this->input['report_keys'] = array_merge($this->input['report_keys'], array_diff($this->forced_vendor_fields, $this->input['report_keys']));
$query = PurchaseOrder::query()
->withTrashed()
->with('vendor')->where('company_id', $this->company->id)

View File

@ -56,6 +56,8 @@ class QuoteExport extends BaseExport
$this->input['report_keys'] = array_values($this->quote_report_keys);
}
$this->input['report_keys'] = array_merge($this->input['report_keys'], array_diff($this->forced_client_fields, $this->input['report_keys']));
$query = Quote::query()
->withTrashed()
->with('client')

View File

@ -57,6 +57,8 @@ class QuoteItemExport extends BaseExport
$this->input['report_keys'] = array_values($this->mergeItemsKeys('quote_report_keys'));
}
$this->input['report_keys'] = array_merge($this->input['report_keys'], array_diff($this->forced_client_fields, $this->input['report_keys']));
$query = Quote::query()
->withTrashed()
->with('client')->where('company_id', $this->company->id)

View File

@ -48,6 +48,8 @@ class RecurringInvoiceExport extends BaseExport
$this->input['report_keys'] = array_values($this->recurring_invoice_report_keys);
}
$this->input['report_keys'] = array_merge($this->input['report_keys'], array_diff($this->forced_client_fields, $this->input['report_keys']));
$query = RecurringInvoice::query()
->withTrashed()
->with('client')
@ -135,8 +137,8 @@ class RecurringInvoiceExport extends BaseExport
$entity['client'] = $invoice->client->present()->name();
}
if (in_array('status_id', $this->input['report_keys'])) {
$entity['status'] = $invoice->stringStatus($invoice->status_id);
if (in_array('recurring_invoice.status', $this->input['report_keys'])) {
$entity['recurring_invoice.status'] = $invoice->stringStatus($invoice->status_id);
}
if (in_array('project_id', $this->input['report_keys'])) {

View File

@ -60,6 +60,8 @@ class TaskExport extends BaseExport
$this->input['report_keys'] = array_values($this->task_report_keys);
}
$this->input['report_keys'] = array_merge($this->input['report_keys'], array_diff($this->forced_client_fields, $this->input['report_keys']));
$query = Task::query()
->withTrashed()
->where('company_id', $this->company->id)

View File

@ -20,7 +20,6 @@ use App\Models\Account;
use App\Models\Company;
use App\Models\Expense;
use App\Models\Invoice;
use Tests\MockAccountData;
use App\Models\CompanyToken;
use App\Models\ClientContact;
use App\Export\CSV\TaskExport;
@ -30,8 +29,6 @@ use App\Export\CSV\ProductExport;
use App\DataMapper\CompanySettings;
use App\Export\CSV\PaymentExport;
use App\Factory\CompanyUserFactory;
use App\Factory\InvoiceItemFactory;
use App\Services\Report\ARDetailReport;
use Illuminate\Routing\Middleware\ThrottleRequests;
/**
@ -262,6 +259,21 @@ class ReportCsvGenerationTest extends TestCase
}
public function testForcedInsertionOfMandatoryColumns()
{
$forced = ['client.name'];
$report_keys = ['invoice.number','client.name', 'invoice.amount'];
$array = array_merge($report_keys, array_diff($forced, $report_keys));
$this->assertEquals('client.name', $array[1]);
$report_keys = ['invoice.number','invoice.amount'];
$array = array_merge($report_keys, array_diff($forced, $report_keys));
$this->assertEquals('client.name', $array[2]);
}
public function testVendorCsvGeneration()
{
@ -322,7 +334,7 @@ class ReportCsvGenerationTest extends TestCase
$data = $export->returnJson();
$this->assertNotNull($data);
// nlog($data);
// nlog($data);
// $this->assertEquals(0, $this->traverseJson($data, 'columns.0.identifier'));
$this->assertEquals('Vendor Name', $this->traverseJson($data, 'columns.9.display_value'));
$this->assertEquals('vendor', $this->traverseJson($data, '0.0.entity'));
@ -1021,6 +1033,44 @@ class ReportCsvGenerationTest extends TestCase
'X-API-TOKEN' => $this->token,
])->post('/api/v1/reports/recurring_invoices', $data)->assertStatus(200);
}
public function testRecurringInvoiceColumnsCsvGeneration()
{
\App\Models\RecurringInvoice::factory()->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'client_id' => $this->client->id,
'amount' => 100,
'balance' => 50,
'number' => '1234',
'status_id' => 2,
'discount' => 10,
'po_number' => '1234',
'public_notes' => 'Public',
'private_notes' => 'Private',
'terms' => 'Terms',
'frequency_id' => 1,
]);
$data = [
'date_range' => 'all',
'report_keys' => [],
'send_email' => false,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/reports/recurring_invoices', $data);
$csv = $response->streamedContent();
$this->assertEquals('1234', $this->getFirstValueByColumn($csv, 'Recurring Invoice Invoice Number'));
$this->assertEquals('Daily', $this->getFirstValueByColumn($csv, 'Recurring Invoice How Often'));
$this->assertEquals('Active', $this->getFirstValueByColumn($csv, 'Recurring Invoice Status'));
}