diff --git a/app/Export/CSV/BaseExport.php b/app/Export/CSV/BaseExport.php index 00d6dfa65fd0..ccbf10b45057 100644 --- a/app/Export/CSV/BaseExport.php +++ b/app/Export/CSV/BaseExport.php @@ -734,53 +734,53 @@ class BaseExport $prefix = ''; if(!$key) { - $prefix = stripos($value, 'client.') !== false ? ctrans('texts.client') : ctrans('texts.contact'); + $prefix = stripos($value, 'client.') !== false ? ctrans('texts.client')." " : ctrans('texts.contact')." "; $key = array_search($value, $this->client_report_keys); } if(!$key) { - $prefix = ctrans('texts.invoice'); + $prefix = ctrans('texts.invoice')." "; $key = array_search($value, $this->invoice_report_keys); } if(!$key) { - $prefix = ctrans('texts.payment'); + $prefix = ctrans('texts.payment')." "; $key = array_search($value, $this->payment_report_keys); } if(!$key) { - $prefix = ctrans('texts.quote'); + $prefix = ctrans('texts.quote')." "; $key = array_search($value, $this->quote_report_keys); } if(!$key) { - $prefix = ctrans('texts.credit'); + $prefix = ctrans('texts.credit')." "; $key = array_search($value, $this->credit_report_keys); } if(!$key) { - $prefix = ctrans('texts.item'); + $prefix = ctrans('texts.item')." "; $key = array_search($value, $this->item_report_keys); } if(!$key) { - $prefix = ctrans('texts.expense'); + $prefix = ctrans('texts.expense')." "; $key = array_search($value, $this->expense_report_keys); } if(!$key) { - $prefix = ctrans('texts.task'); + $prefix = ctrans('texts.task')." "; $key = array_search($value, $this->task_report_keys); } if(!$key) { - $prefix = ctrans('texts.vendor'); + $prefix = ctrans('texts.vendor')." "; $key = array_search($value, $this->vendor_report_keys); } if(!$key) { - $prefix = ctrans('texts.purchase_order'); + $prefix = ctrans('texts.purchase_order')." "; $key = array_search($value, $this->purchase_order_report_keys); } @@ -800,7 +800,7 @@ class BaseExport $key = str_replace('payment.', '', $key); $key = str_replace('expense.', '', $key); - $header[] = "{$prefix} " . ctrans("texts.{$key}"); + $header[] = "{$prefix}" . ctrans("texts.{$key}"); } // nlog($header); diff --git a/app/Export/CSV/ClientExport.php b/app/Export/CSV/ClientExport.php index 21a862ec9d90..bcf93bf5897f 100644 --- a/app/Export/CSV/ClientExport.php +++ b/app/Export/CSV/ClientExport.php @@ -131,6 +131,8 @@ class ClientExport extends BaseExport $transformed_client = $this->client_transformer->transform($client); + $transformed_contact = []; + if ($contact = $client->contacts()->first()) { $transformed_contact = $this->contact_transformer->transform($contact); } @@ -139,9 +141,7 @@ class ClientExport extends BaseExport foreach (array_values($this->input['report_keys']) as $key) { $parts = explode('.', $key); - - $keyval = array_search($key, $this->entity_keys); - + if (is_array($parts) && $parts[0] == 'client' && array_key_exists($parts[1], $transformed_client)) { $entity[$key] = $transformed_client[$parts[1]]; } elseif (is_array($parts) && $parts[0] == 'contact' && array_key_exists($parts[1], $transformed_contact)) { diff --git a/tests/Feature/Export/ReportCsvGenerationTest.php b/tests/Feature/Export/ReportCsvGenerationTest.php new file mode 100644 index 000000000000..e0abc4070707 --- /dev/null +++ b/tests/Feature/Export/ReportCsvGenerationTest.php @@ -0,0 +1,175 @@ +faker = \Faker\Factory::create(); + + $this->withoutMiddleware( + ThrottleRequests::class + ); + + $this->withoutExceptionHandling(); + + $this->buildData(); + + + } + + public $company; + + public $user; + + public $payload; + + public $account; + + public $client; + + public $token; + + public $cu; + + /** + * start_date - Y-m-d + end_date - Y-m-d + date_range - + all + last7 + last30 + this_month + last_month + this_quarter + last_quarter + this_year + custom + is_income_billed - true = Invoiced || false = Payments + expense_billed - true = Expensed || false = Expenses marked as paid + include_tax - true tax_included || false - tax_excluded + */ + private function buildData() + { + $this->account = Account::factory()->create([ + 'hosted_client_count' => 1000, + 'hosted_company_count' => 1000, + ]); + + $this->account->num_users = 3; + $this->account->save(); + + $this->user = User::factory()->create([ + 'account_id' => $this->account->id, + 'confirmation_code' => 'xyz123', + 'email' => $this->faker->unique()->safeEmail(), + ]); + + $settings = CompanySettings::defaults(); + $settings->client_online_payment_notification = false; + $settings->client_manual_payment_notification = false; + + $this->company = Company::factory()->create([ + 'account_id' => $this->account->id, + 'settings' => $settings, + ]); + + $this->company->settings = $settings; + $this->company->save(); + + $this->cu = CompanyUserFactory::create($this->user->id, $this->company->id, $this->account->id); + $this->cu->is_owner = true; + $this->cu->is_admin = true; + $this->cu->is_locked = false; + $this->cu->save(); + + $this->token = \Illuminate\Support\Str::random(64); + + $company_token = new CompanyToken; + $company_token->user_id = $this->user->id; + $company_token->company_id = $this->company->id; + $company_token->account_id = $this->account->id; + $company_token->name = 'test token'; + $company_token->token = $this->token; + $company_token->is_system = true; + + $company_token->save(); + + $this->payload = [ + 'start_date' => '2000-01-01', + 'end_date' => '2030-01-11', + 'date_range' => 'custom', + 'is_income_billed' => true, + 'include_tax' => false, + ]; + + $this->client = Client::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'is_deleted' => 0, + 'name' => 'bob', + 'address1' => '1234' + ]); + } + + public function testClientCsvGeneration() + { + + $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/clients', $data); + + $csv = $response->streamedContent(); + + $reader = Reader::createFromString($csv); + $reader->setHeaderOffset(0); + + $res = $reader->fetchColumnByName('Street'); + $res = iterator_to_array($res, true); + + $this->assertEquals('1234', $res[1]); + + } +}