diff --git a/app/Export/CSV/BaseExport.php b/app/Export/CSV/BaseExport.php index 15fec0b11667..84c003d7c0ba 100644 --- a/app/Export/CSV/BaseExport.php +++ b/app/Export/CSV/BaseExport.php @@ -879,10 +879,7 @@ class BaseExport $transformed_clients = $this->transformKeys($clients); - nlog($transformed_clients); - if(count($transformed_clients) > 0) { - nlog("yus"); $query->whereIn('client_id', $transformed_clients); } diff --git a/app/Export/CSV/InvoiceExport.php b/app/Export/CSV/InvoiceExport.php index a0f73ddc4e6a..674159294671 100644 --- a/app/Export/CSV/InvoiceExport.php +++ b/app/Export/CSV/InvoiceExport.php @@ -144,18 +144,6 @@ class InvoiceExport extends BaseExport private function decorateAdvancedFields(Invoice $invoice, array $entity): array { - // 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); // } diff --git a/app/Export/Decorators/ContactDecorator.php b/app/Export/Decorators/ContactDecorator.php index a8fdcbd17ef3..71707e9b0883 100644 --- a/app/Export/Decorators/ContactDecorator.php +++ b/app/Export/Decorators/ContactDecorator.php @@ -23,8 +23,13 @@ class ContactDecorator implements DecoratorInterface $contact = $entity; } elseif($entity->contacts) { $contact = $entity->contacts()->first(); + } elseif($entity->client) { + $contact = $entity->client->primary_contact->first() ?? $entity->client->contacts()->whereNotNull('email')->first(); + } elseif($entity->vendor) { + $contact = $entity->vendor->primary_contact->first() ?? $entity->vendor->contacts()->whereNotNull('email')->first(); } + if($contact && method_exists($this, $key)) { return $this->{$key}($contact); } elseif($contact && ($contact->{$key} ?? false)) { diff --git a/tests/Feature/Export/ReportCsvGenerationTest.php b/tests/Feature/Export/ReportCsvGenerationTest.php index cab75c368355..315e9b6fa86c 100644 --- a/tests/Feature/Export/ReportCsvGenerationTest.php +++ b/tests/Feature/Export/ReportCsvGenerationTest.php @@ -266,6 +266,42 @@ class ReportCsvGenerationTest extends TestCase } + public function testContactProps() + { + Invoice::factory()->count(5)->create( + [ + 'client_id'=> $this->client->id, + 'company_id' => $this->company->id, + 'user_id' => $this->user->id + ] + ); + + $data = [ + 'date_range' => 'all', + 'report_keys' => ['invoice.number','client.name', 'contact.email'], + 'send_email' => false, + 'client_id' => $this->client->hashed_id + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/reports/invoices', $data); + + $response->assertStatus(200); + + $arr = $response->json(); + + $hash = $arr['message']; + + $response = $this->poll($hash); + + $csv = $response->body(); + + $this->assertEquals('john@doe.com', $this->getFirstValueByColumn($csv, 'Contact Email')); + + } + public function testForcedInsertionOfMandatoryColumns() { $forced = ['client.name'];