From 45370ff5c41cb237bdfe9af1f126e634b5ca8987 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 18 Jul 2023 14:39:04 +1000 Subject: [PATCH] Fixes for purchase order export --- app/Export/CSV/BaseExport.php | 1 + app/Export/CSV/PurchaseOrderExport.php | 68 +++++----- app/Export/CSV/PurchaseOrderItemExport.php | 34 ++--- .../Export/ReportCsvGenerationTest.php | 128 +++++++++++++++++- 4 files changed, 179 insertions(+), 52 deletions(-) diff --git a/app/Export/CSV/BaseExport.php b/app/Export/CSV/BaseExport.php index e1bb510896cb..e29ce936c0ad 100644 --- a/app/Export/CSV/BaseExport.php +++ b/app/Export/CSV/BaseExport.php @@ -829,6 +829,7 @@ class BaseExport $key = str_replace('item.', '', $key); $key = str_replace('recurring_invoice.', '', $key); + $key = str_replace('purchase_order.', '', $key); $key = str_replace('invoice.', '', $key); $key = str_replace('quote.', '', $key); $key = str_replace('credit.', '', $key); diff --git a/app/Export/CSV/PurchaseOrderExport.php b/app/Export/CSV/PurchaseOrderExport.php index 68ec10dc3219..bfba71830f3c 100644 --- a/app/Export/CSV/PurchaseOrderExport.php +++ b/app/Export/CSV/PurchaseOrderExport.php @@ -30,39 +30,39 @@ class PurchaseOrderExport extends BaseExport public Writer $csv; public array $entity_keys = [ - 'amount' => 'amount', - 'balance' => 'balance', - 'vendor' => 'vendor_id', - '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', - 'custom_value4' => 'custom_value4', - 'date' => 'date', - 'discount' => 'discount', - 'due_date' => 'due_date', - 'exchange_rate' => 'exchange_rate', - 'footer' => 'footer', - 'number' => 'number', - 'paid_to_date' => 'paid_to_date', - 'partial' => 'partial', - 'partial_due_date' => 'partial_due_date', - 'po_number' => 'po_number', - 'private_notes' => 'private_notes', - 'public_notes' => 'public_notes', - 'status' => 'status_id', - 'tax_name1' => 'tax_name1', - 'tax_name2' => 'tax_name2', - 'tax_name3' => 'tax_name3', - 'tax_rate1' => 'tax_rate1', - 'tax_rate2' => 'tax_rate2', - 'tax_rate3' => 'tax_rate3', - 'terms' => 'terms', - 'total_taxes' => 'total_taxes', - 'currency_id' => 'currency_id', + 'amount' => 'purchase_order.amount', + 'balance' => 'purchase_order.balance', + 'vendor' => 'purchase_order.vendor_id', + // 'custom_surcharge1' => 'purchase_order.custom_surcharge1', + // 'custom_surcharge2' => 'purchase_order.custom_surcharge2', + // 'custom_surcharge3' => 'purchase_order.custom_surcharge3', + // 'custom_surcharge4' => 'purchase_order.custom_surcharge4', + 'custom_value1' => 'purchase_order.custom_value1', + 'custom_value2' => 'purchase_order.custom_value2', + 'custom_value3' => 'purchase_order.custom_value3', + 'custom_value4' => 'purchase_order.custom_value4', + 'date' => 'purchase_order.date', + 'discount' => 'purchase_order.discount', + 'due_date' => 'purchase_order.due_date', + 'exchange_rate' => 'purchase_order.exchange_rate', + 'footer' => 'purchase_order.footer', + 'number' => 'purchase_order.number', + 'paid_to_date' => 'purchase_order.paid_to_date', + 'partial' => 'purchase_order.partial', + 'partial_due_date' => 'purchase_order.partial_due_date', + 'po_number' => 'purchase_order.po_number', + 'private_notes' => 'purchase_order.private_notes', + 'public_notes' => 'purchase_order.public_notes', + 'status' => 'purchase_order.status_id', + 'tax_name1' => 'purchase_order.tax_name1', + 'tax_name2' => 'purchase_order.tax_name2', + 'tax_name3' => 'purchase_order.tax_name3', + 'tax_rate1' => 'purchase_order.tax_rate1', + 'tax_rate2' => 'purchase_order.tax_rate2', + 'tax_rate3' => 'purchase_order.tax_rate3', + 'terms' => 'purchase_order.terms', + 'total_taxes' => 'purchase_order.total_taxes', + 'currency_id' => 'purchase_order.currency_id', ]; private array $decorate_keys = [ @@ -129,7 +129,7 @@ class PurchaseOrderExport extends BaseExport $keyval = array_search($key, $this->entity_keys); if(!$keyval) { - $keyval = array_search(str_replace("invoice.", "", $key), $this->entity_keys) ?? $key; + $keyval = array_search(str_replace("purchase_order.", "", $key), $this->entity_keys) ?? $key; } if(!$keyval) { diff --git a/app/Export/CSV/PurchaseOrderItemExport.php b/app/Export/CSV/PurchaseOrderItemExport.php index 0d83c93d388e..2bb1eef01591 100644 --- a/app/Export/CSV/PurchaseOrderItemExport.php +++ b/app/Export/CSV/PurchaseOrderItemExport.php @@ -36,10 +36,10 @@ class PurchaseOrderItemExport extends BaseExport 'vendor' => 'vendor_id', 'vendor_number' => 'vendor.number', 'vendor_id_number' => 'vendor.id_number', - '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', @@ -81,10 +81,10 @@ class PurchaseOrderItemExport extends BaseExport 'tax_name3' => 'item.tax_name3', 'line_total' => 'item.line_total', 'gross_line_total' => 'item.gross_line_total', - // 'invoice1' => 'item.custom_value1', - // 'invoice2' => 'item.custom_value2', - // 'invoice3' => 'item.custom_value3', - // 'invoice4' => 'item.custom_value4', + 'purchase_order1' => 'item.custom_value1', + 'purchase_order2' => 'item.custom_value2', + 'purchase_order3' => 'item.custom_value3', + 'purchase_order4' => 'item.custom_value4', 'tax_category' => 'item.tax_id', 'type' => 'item.type_id', ]; @@ -138,7 +138,7 @@ class PurchaseOrderItemExport extends BaseExport private function iterateItems(PurchaseOrder $purchase_order) { - $transformed_invoice = $this->buildRow($purchase_order); + $transformed_purchase_order = $this->buildRow($purchase_order); $transformed_items = []; @@ -153,7 +153,7 @@ class PurchaseOrderItemExport extends BaseExport $keyval = $key; - $keyval = str_replace("custom_value", "invoice", $key); + $keyval = str_replace("custom_value", "purchase_order", $key); if($key == 'type_id') { $keyval = 'type'; @@ -183,7 +183,7 @@ class PurchaseOrderItemExport extends BaseExport } } - $transformed_items = array_merge($transformed_invoice, $item_array); + $transformed_items = array_merge($transformed_purchase_order, $item_array); $entity = $this->decorateAdvancedFields($purchase_order, $transformed_items); $this->csv->insertOne($entity); @@ -192,7 +192,7 @@ class PurchaseOrderItemExport extends BaseExport private function buildRow(PurchaseOrder $purchase_order) :array { - $transformed_invoice = $this->purchase_order_transformer->transform($purchase_order); + $transformed_purchase_order = $this->purchase_order_transformer->transform($purchase_order); $entity = []; @@ -200,17 +200,17 @@ class PurchaseOrderItemExport extends BaseExport $keyval = array_search($key, $this->entity_keys); if(!$keyval) { - $keyval = array_search(str_replace("invoice.", "", $key), $this->entity_keys) ?? $key; + $keyval = array_search(str_replace("purchase_order.", "", $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]; + if (array_key_exists($key, $transformed_purchase_order)) { + $entity[$keyval] = $transformed_purchase_order[$key]; + } elseif (array_key_exists($keyval, $transformed_purchase_order)) { + $entity[$keyval] = $transformed_purchase_order[$keyval]; } else { $entity[$keyval] = $this->resolveKey($keyval, $purchase_order, $this->purchase_order_transformer); } diff --git a/tests/Feature/Export/ReportCsvGenerationTest.php b/tests/Feature/Export/ReportCsvGenerationTest.php index 946918126873..14abc9ff5a73 100644 --- a/tests/Feature/Export/ReportCsvGenerationTest.php +++ b/tests/Feature/Export/ReportCsvGenerationTest.php @@ -525,7 +525,7 @@ class ReportCsvGenerationTest extends TestCase ])->post('/api/v1/reports/quote_items', $data); $csv = $response->streamedContent(); -nlog($csv); + $this->assertEquals('bob', $this->getFirstValueByColumn($csv, 'Client Name')); $this->assertEquals('1234', $this->getFirstValueByColumn($csv, 'Quote Number')); @@ -542,6 +542,132 @@ nlog($csv); } + public function testPurchaseOrderCsvGeneration() + { + + $vendor = + \App\Models\Vendor::factory()->create( + [ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'name' => 'Vendor 1', + ] + ); + + \App\Models\PurchaseOrder::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'vendor_id' => $vendor->id, + 'amount' => 100, + 'balance' => 50, + 'status_id' => 2, + 'discount' => 10, + 'number' => '1234', + 'public_notes' => 'Public', + 'private_notes' => 'Private', + 'terms' => 'Terms', + ]); + + $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/purchase_orders', $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, 'Number')); + $this->assertEquals('Public', $this->getFirstValueByColumn($csv, 'Public Notes')); + $this->assertEquals('Private', $this->getFirstValueByColumn($csv, 'Private Notes')); + $this->assertEquals('Terms', $this->getFirstValueByColumn($csv, 'Terms')); + } + + + public function testPurchaseOrderItemsCustomColumnsCsvGeneration() + { + + $vendor = + \App\Models\Vendor::factory()->create( + [ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'name' => 'Vendor 1', + ] + ); + + + \App\Models\PurchaseOrder::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'vendor_id' => $vendor->id, + 'amount' => 100, + 'balance' => 50, + 'number' => '1234', + 'po_number' => '1234', + 'status_id' => 2, + 'discount' => 10, + 'po_number' => '1234', + 'public_notes' => 'Public', + 'private_notes' => 'Private', + 'terms' => 'Terms', + 'line_items' => [ + [ + 'quantity' => 10, + 'cost' => 100, + 'line_total' => 1000, + 'is_amount_discount' => true, + 'discount' => 0, + 'notes' => 'item notes', + 'product_key' => 'product key', + 'custom_value1' => 'custom 1', + 'custom_value2' => 'custom 2', + 'custom_value3' => 'custom 3', + 'custom_value4' => 'custom 4', + 'tax_name1' => 'GST', + 'tax_rate1' => 10.00, + 'type_id' => '1', + ], + ] + ]); + + $data = [ + 'date_range' => 'all', + 'report_keys' => ["vendor.name","purchase_order.number","purchase_order.amount", "item.quantity", "item.cost", "item.line_total", "item.discount", "item.notes", "item.product_key", "item.custom_value1", "item.tax_name1", "item.tax_rate1",], + 'send_email' => false, + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/reports/purchase_order_items', $data); + + $csv = $response->streamedContent(); + + nlog($csv); + + $this->assertEquals('Vendor 1', $this->getFirstValueByColumn($csv, 'Vendor Name')); + $this->assertEquals('1234', $this->getFirstValueByColumn($csv, 'Purchase Order Number')); + $this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Quantity')); + $this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Cost')); + $this->assertEquals('1000', $this->getFirstValueByColumn($csv, 'Line Total')); + $this->assertEquals('0', $this->getFirstValueByColumn($csv, 'Discount')); + $this->assertEquals('item notes', $this->getFirstValueByColumn($csv, 'Notes')); + $this->assertEquals('product key', $this->getFirstValueByColumn($csv, 'Product')); + $this->assertEquals('custom 1', $this->getFirstValueByColumn($csv, 'Item Custom Value 1')); + $this->assertEquals('GST', $this->getFirstValueByColumn($csv, 'Tax Name 1')); + $this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Tax Rate 1')); + + } public function testQuoteCustomColumnsCsvGeneration() {