From a3febc93887bbe435894052e601440a190ab7274 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 16 Jul 2023 20:17:43 +1000 Subject: [PATCH] Adjustments for Quote Exports --- app/Export/CSV/BaseExport.php | 19 ++++- app/Export/CSV/QuoteExport.php | 23 ++++-- app/Models/Quote.php | 2 +- .../Export/ReportCsvGenerationTest.php | 75 +++++++++++++++++++ 4 files changed, 110 insertions(+), 9 deletions(-) diff --git a/app/Export/CSV/BaseExport.php b/app/Export/CSV/BaseExport.php index ccbf10b45057..0a6e4c96dfd4 100644 --- a/app/Export/CSV/BaseExport.php +++ b/app/Export/CSV/BaseExport.php @@ -193,13 +193,13 @@ class BaseExport ]; protected array $quote_report_keys = [ - "quote_number" => "quote.number", + "number" => "quote.number", "amount" => "quote.amount", "balance" => "quote.balance", "paid_to_date" => "quote.paid_to_date", "po_number" => "quote.po_number", "date" => "quote.date", - "due_date" => "quote.due_date", + "valid_until" => "quote.due_date", "terms" => "quote.terms", "footer" => "quote.footer", "status" => "quote.status", @@ -347,6 +347,7 @@ class BaseExport '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), + 'quote' => $value = $this->resolveQuoteKey($parts[1], $entity, $transformer), 'purchase_order' => $value = $this->resolvePurchaseOrderKey($parts[1], $entity, $transformer), 'payment' => $value = $this->resolvePaymentKey($parts[1], $entity, $transformer), default => $value = '' @@ -511,6 +512,20 @@ class BaseExport return ''; } + private function resolveQuoteKey($column, $entity, $transformer) + { + nlog("searching for {$column}"); + + $transformed_entity = $transformer->transform($entity); + + if(array_key_exists($column, $transformed_entity)) { + return $transformed_entity[$column]; + } + + return ''; + + } + private function resolveInvoiceKey($column, $entity, $transformer) { nlog("searching for {$column}"); diff --git a/app/Export/CSV/QuoteExport.php b/app/Export/CSV/QuoteExport.php index 8031f01f05e2..5711e46067b5 100644 --- a/app/Export/CSV/QuoteExport.php +++ b/app/Export/CSV/QuoteExport.php @@ -43,7 +43,7 @@ class QuoteExport extends BaseExport 'custom_value4' => 'custom_value4', 'date' => 'date', 'discount' => 'discount', - 'due_date' => 'due_date', + 'valid_until' => 'due_date', 'exchange_rate' => 'exchange_rate', 'footer' => 'footer', 'number' => 'number', @@ -115,17 +115,28 @@ class QuoteExport extends BaseExport private function buildRow(Quote $quote) :array { - $transformed_quote = $this->quote_transformer->transform($quote); + $transformed_entity = $this->quote_transformer->transform($quote); $entity = []; foreach (array_values($this->input['report_keys']) as $key) { $keyval = array_search($key, $this->entity_keys); - if (array_key_exists($key, $transformed_quote)) { - $entity[$keyval] = $transformed_quote[$key]; - } else { - $entity[$keyval] = ''; + if(!$keyval) { + $keyval = array_search(str_replace("invoice.", "", $key), $this->entity_keys) ?? $key; + } + + if(!$keyval) { + $keyval = $key; + } + + if (array_key_exists($key, $transformed_entity)) { + $entity[$keyval] = $transformed_entity[$key]; + } elseif (array_key_exists($keyval, $transformed_entity)) { + $entity[$keyval] = $transformed_entity[$keyval]; + } + else { + $entity[$keyval] = $this->resolveKey($keyval, $quote, $this->quote_transformer); } } diff --git a/app/Models/Quote.php b/app/Models/Quote.php index ae0d4b2333d2..04c3cee2cd94 100644 --- a/app/Models/Quote.php +++ b/app/Models/Quote.php @@ -455,7 +455,7 @@ class Quote extends BaseModel case self::STATUS_DRAFT: return ctrans('texts.draft'); case self::STATUS_SENT: - return ctrans('texts.pending'); + return ctrans('texts.sent'); case self::STATUS_APPROVED: return ctrans('texts.approved'); case self::STATUS_EXPIRED: diff --git a/tests/Feature/Export/ReportCsvGenerationTest.php b/tests/Feature/Export/ReportCsvGenerationTest.php index 80d608560185..1ff8fbba5fed 100644 --- a/tests/Feature/Export/ReportCsvGenerationTest.php +++ b/tests/Feature/Export/ReportCsvGenerationTest.php @@ -355,6 +355,81 @@ class ReportCsvGenerationTest extends TestCase } +public function testQuoteCsvGeneration() + { + + \App\Models\Quote::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'client_id' => $this->client->id, + 'amount' => 100, + 'balance' => 50, + 'status_id' => 2, + 'discount' => 10, + 'po_number' => '1234', + 'public_notes' => 'Public', + 'private_notes' => 'Private', + 'terms' => 'Terms', + 'date' => '2020-01-01', + 'due_date' => '2020-01-01', + 'partial_due_date' => '2021-01-03', + 'partial' => 10, + 'discount' => 10, + 'custom_value1' => 'Custom 1', + 'custom_value2' => 'Custom 2', + 'custom_value3' => 'Custom 3', + 'custom_value4' => 'Custom 4', + 'footer' => 'Footer', + 'tax_name1' => 'Tax 1', + 'tax_rate1' => 10, + 'tax_name2' => 'Tax 2', + 'tax_rate2' => 20, + 'tax_name3' => 'Tax 3', + 'tax_rate3' => 30, + + ]); + + $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/quotes', $data); + + $response->assertStatus(200); + + $csv = $response->streamedContent(); + + $this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Amount')); + $this->assertEquals('50', $this->getFirstValueByColumn($csv, 'Balance')); + $this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Discount')); + $this->assertEquals('1234', $this->getFirstValueByColumn($csv, 'PO Number')); + $this->assertEquals('Public', $this->getFirstValueByColumn($csv, 'Public Notes')); + $this->assertEquals('Private', $this->getFirstValueByColumn($csv, 'Private Notes')); + $this->assertEquals('Terms', $this->getFirstValueByColumn($csv, 'Terms')); + $this->assertEquals('2020-01-01', $this->getFirstValueByColumn($csv, 'Date')); + $this->assertEquals('2020-01-01', $this->getFirstValueByColumn($csv, 'Valid Until')); + $this->assertEquals('2021-01-03', $this->getFirstValueByColumn($csv, 'Partial Due Date')); + $this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Partial/Deposit')); + $this->assertEquals('Custom 1', $this->getFirstValueByColumn($csv, 'Custom Value 1')); + $this->assertEquals('Custom 2', $this->getFirstValueByColumn($csv, 'Custom Value 2')); + $this->assertEquals('Custom 3', $this->getFirstValueByColumn($csv, 'Custom Value 3')); + $this->assertEquals('Custom 4', $this->getFirstValueByColumn($csv, 'Custom Value 4')); + $this->assertEquals('Footer', $this->getFirstValueByColumn($csv, 'Footer')); + $this->assertEquals('Tax 1', $this->getFirstValueByColumn($csv, 'Tax Name 1')); + $this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Tax Rate 1')); + $this->assertEquals('Tax 2', $this->getFirstValueByColumn($csv, 'Tax Name 2')); + $this->assertEquals('20', $this->getFirstValueByColumn($csv, 'Tax Rate 2')); + $this->assertEquals('Tax 3', $this->getFirstValueByColumn($csv, 'Tax Name 3')); + $this->assertEquals('30', $this->getFirstValueByColumn($csv, 'Tax Rate 3')); + $this->assertEquals('Expired', $this->getFirstValueByColumn($csv, 'Status')); + + } + public function testExpenseCsvGeneration() {