CSVs with custom columns

This commit is contained in:
David Bomba 2023-07-17 10:22:53 +10:00
parent e0807a3a6a
commit 18ca64c72c
2 changed files with 35 additions and 3 deletions

View File

@ -172,8 +172,8 @@ class ClientExport extends BaseExport
$entity['shipping_country'] = $client->shipping_country ? ctrans("texts.country_{$client->shipping_country->name}") : ''; $entity['shipping_country'] = $client->shipping_country ? ctrans("texts.country_{$client->shipping_country->name}") : '';
} }
if (in_array('client.currency', $this->input['report_keys'])) { if (in_array('client.currency_id', $this->input['report_keys'])) {
$entity['currency'] = $client->currency() ? $client->currency()->code : $client->company->currency()->code; $entity['client.currency_id'] = $client->currency() ? $client->currency()->code : $client->company->currency()->code;
} }
if (in_array('client.industry_id', $this->input['report_keys'])) { if (in_array('client.industry_id', $this->input['report_keys'])) {

View File

@ -143,10 +143,13 @@ class ReportCsvGenerationTest extends TestCase
$this->client = Client::factory()->create([ $this->client = Client::factory()->create([
'user_id' => $this->user->id, 'user_id' => $this->user->id,
// 'assigned_user_id', $this->user->id,
'company_id' => $this->company->id, 'company_id' => $this->company->id,
'is_deleted' => 0, 'is_deleted' => 0,
'name' => 'bob', 'name' => 'bob',
'address1' => '1234' 'address1' => '1234',
'balance' => 100,
'paid_to_date' => 50,
]); ]);
ClientContact::factory()->create([ ClientContact::factory()->create([
@ -307,6 +310,35 @@ class ReportCsvGenerationTest extends TestCase
} }
public function testClientCustomColumnsCsvGeneration()
{
$data = [
'date_range' => 'all',
'report_keys' => ["client.name","client.user","client.assigned_user","client.balance","client.paid_to_date","client.currency_id"],
'send_email' => false,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/reports/clients', $data);
$csv = $response->streamedContent();
$this->assertEquals('bob', $this->getFirstValueByColumn($csv, 'Name'));
$this->assertEquals(100, $this->getFirstValueByColumn($csv, 'Balance'));
$this->assertEquals(50, $this->getFirstValueByColumn($csv, 'Paid to Date'));
$this->assertEquals($this->user->present()->name(), $this->getFirstValueByColumn($csv, 'Client User'));
$this->assertEquals('', $this->getFirstValueByColumn($csv, 'Client Assigned User'));
$this->assertEquals('USD', $this->getFirstValueByColumn($csv, 'Client Currency'));
}
public function testClientContactCsvGeneration() public function testClientContactCsvGeneration()
{ {