diff --git a/app/Export/CSV/BaseExport.php b/app/Export/CSV/BaseExport.php index e29ce936c0ad..ab2873a965a7 100644 --- a/app/Export/CSV/BaseExport.php +++ b/app/Export/CSV/BaseExport.php @@ -137,6 +137,35 @@ class BaseExport "user" => "invoice.user_id", ]; + protected array $recurring_invoice_report_keys = [ + "invoice_number" => "recurring_invoice.number", + "amount" => "recurring_invoice.amount", + "balance" => "recurring_invoice.balance", + "paid_to_date" => "recurring_invoice.paid_to_date", + "po_number" => "recurring_invoice.po_number", + "date" => "recurring_invoice.date", + "due_date" => "recurring_invoice.due_date", + "terms" => "recurring_invoice.terms", + "footer" => "recurring_invoice.footer", + "status" => "recurring_invoice.status", + "public_notes" => "recurring_invoice.public_notes", + "private_notes" => "recurring_invoice.private_notes", + "uses_inclusive_taxes" => "recurring_invoice.uses_inclusive_taxes", + "is_amount_discount" => "recurring_invoice.is_amount_discount", + "partial" => "recurring_invoice.partial", + "partial_due_date" => "recurring_invoice.partial_due_date", + "surcharge1" => "recurring_invoice.custom_surcharge1", + "surcharge2" => "recurring_invoice.custom_surcharge2", + "surcharge3" => "recurring_invoice.custom_surcharge3", + "surcharge4" => "recurring_invoice.custom_surcharge4", + "exchange_rate" => "recurring_invoice.exchange_rate", + "tax_amount" => "recurring_invoice.total_taxes", + "assigned_user" => "recurring_invoice.assigned_user_id", + "user" => "recurring_invoice.user_id", + "frequency_id" => "recurring_invoice.frequency_id", + "next_send_date" => "recurring_invoice.next_send_date" + ]; + protected array $purchase_order_report_keys = [ 'amount' => 'purchase_order.amount', 'balance' => 'purchase_order.balance', @@ -352,6 +381,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), + 'recurring_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), @@ -764,7 +794,7 @@ class BaseExport { $header = []; - nlog($this->input['report_keys']); + // nlog($this->input['report_keys']); foreach (array_merge($this->input['report_keys'], $this->forced_keys) as $value) { @@ -782,6 +812,11 @@ class BaseExport $key = array_search($value, $this->invoice_report_keys); } + if(!$key) { + $prefix = ctrans('texts.recurring_invoice')." "; + $key = array_search($value, $this->recurring_invoice_report_keys); + } + if(!$key) { $prefix = ctrans('texts.payment')." "; $key = array_search($value, $this->payment_report_keys); @@ -851,8 +886,6 @@ class BaseExport } } -// nlog($header); - return $header; } } diff --git a/app/Export/CSV/RecurringInvoiceExport.php b/app/Export/CSV/RecurringInvoiceExport.php index 877fd04710fc..6df397c64855 100644 --- a/app/Export/CSV/RecurringInvoiceExport.php +++ b/app/Export/CSV/RecurringInvoiceExport.php @@ -32,10 +32,10 @@ class RecurringInvoiceExport extends BaseExport 'amount' => 'amount', 'balance' => 'balance', 'client' => 'client_id', - 'custom_surcharge1' => 'custom_surcharge1', - 'custom_surcharge2' => 'custom_surcharge2', - 'custom_surcharge3' => 'custom_surcharge3', - 'custom_surcharge4' => 'custom_surcharge4', + // 'custom_surcharge1' => 'custom_surcharge1', + // 'custom_surcharge2' => 'custom_surcharge2', + // 'custom_surcharge3' => 'custom_surcharge3', + // 'custom_surcharge4' => 'custom_surcharge4', 'custom_value1' => 'custom_value1', 'custom_value2' => 'custom_value2', 'custom_value3' => 'custom_value3', @@ -65,7 +65,8 @@ class RecurringInvoiceExport extends BaseExport 'currency' => 'currency_id', 'vendor' => 'vendor_id', 'project' => 'project_id', - 'frequency' => 'frequency_id' + 'frequency_id' => 'frequency_id', + 'next_send_date' => 'next_send_date' ]; private array $decorate_keys = [ @@ -126,11 +127,22 @@ class RecurringInvoiceExport extends BaseExport foreach (array_values($this->input['report_keys']) as $key) { $keyval = array_search($key, $this->entity_keys); + if(!$keyval) { + $keyval = array_search(str_replace("recurring_invoice.", "", $key), $this->entity_keys) ?? $key; + } + + if(!$keyval) { + $keyval = $key; + } + if (array_key_exists($key, $transformed_invoice)) { $entity[$keyval] = $transformed_invoice[$key]; + } elseif (array_key_exists($keyval, $transformed_invoice)) { + $entity[$keyval] = $transformed_invoice[$keyval]; } else { - $entity[$keyval] = ''; + $entity[$keyval] = $this->resolveKey($keyval, $invoice, $this->invoice_transformer); } + } return $this->decorateAdvancedFields($invoice, $entity); @@ -162,7 +174,9 @@ class RecurringInvoiceExport extends BaseExport $entity['vendor'] = $invoice->vendor ? $invoice->vendor->name : ''; } - $entity['frequency'] = $invoice->frequencyForKey($invoice->frequency_id); + if (in_array('recurring_invoice.frequency_id', $this->input['report_keys']) || in_array('frequency_id', $this->input['report_keys'])) { + $entity['frequency_id'] = $invoice->frequencyForKey($invoice->frequency_id); + } return $entity; } diff --git a/tests/Feature/Export/ReportCsvGenerationTest.php b/tests/Feature/Export/ReportCsvGenerationTest.php index 14abc9ff5a73..c46ee9eb5c92 100644 --- a/tests/Feature/Export/ReportCsvGenerationTest.php +++ b/tests/Feature/Export/ReportCsvGenerationTest.php @@ -411,6 +411,47 @@ class ReportCsvGenerationTest extends TestCase } + public function testRecurringInvoiceCustomColumnsCsvGeneration() + { + + \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' => ["client.name","recurring_invoice.number","recurring_invoice.amount", "recurring_invoice.frequency_id"], + '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(); + + nlog($csv); + + $this->assertEquals('bob', $this->getFirstValueByColumn($csv, 'Client Name')); + $this->assertEquals('1234', $this->getFirstValueByColumn($csv, 'Recurring Invoice Invoice Number')); + $this->assertEquals('Daily', $this->getFirstValueByColumn($csv, 'Recurring Invoice How Often')); + + } + + public function testInvoiceItemsCustomColumnsCsvGeneration() { @@ -983,7 +1024,7 @@ class ReportCsvGenerationTest extends TestCase $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('Daily', $this->getFirstValueByColumn($csv, 'Frequency')); + $this->assertEquals('Daily', $this->getFirstValueByColumn($csv, 'How Often')); }