From 93205afa9d74b5f2f7cac55e2eb31ce8f03fd612 Mon Sep 17 00:00:00 2001 From: Okan Date: Wed, 12 Jun 2024 14:55:02 +0300 Subject: [PATCH] Fixed incorrect invoice row line number. --- src/Actions/SquashInvoiceRows.php | 10 +++++++++- tests/SquashInvoiceRowsTest.php | 20 +++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Actions/SquashInvoiceRows.php b/src/Actions/SquashInvoiceRows.php index 3a9d3d5..5a4f8ea 100644 --- a/src/Actions/SquashInvoiceRows.php +++ b/src/Actions/SquashInvoiceRows.php @@ -9,7 +9,7 @@ class SquashInvoiceRows { /** - * @var array Variable to store rows with RecType. + * @var InvoiceDetails[] Variable to store rows with RecType. */ private array $rowsWithRecType = []; @@ -196,6 +196,8 @@ private function aggregateExpenseClassifications(string $rowKey, ?array $classif */ private function mergeAndRoundResults(): array { + $lineNumber = 1; + foreach ($this->squashedRows as $key => $row) { $clsLineNumber = 1; @@ -207,10 +209,16 @@ private function mergeAndRoundResults(): array $row->setExpensesClassification($this->mapClassifications($this->squashedEcls[$key], $clsLineNumber)); } + $row->setLineNumber($lineNumber++); + $this->roundRow($row); $this->roundClassifications($row); $this->adjustClassificationAmount($row); } + + foreach ($this->rowsWithRecType as $row) { + $row->setLineNumber($lineNumber++); + } return array_merge(array_values($this->squashedRows), $this->rowsWithRecType); } diff --git a/tests/SquashInvoiceRowsTest.php b/tests/SquashInvoiceRowsTest.php index ef72a76..89b57e9 100644 --- a/tests/SquashInvoiceRowsTest.php +++ b/tests/SquashInvoiceRowsTest.php @@ -76,6 +76,7 @@ public function test_same_vat_category_rows_are_squashed() $rows = $invoice->getInvoiceDetails(); $this->assertNotNull($rows); $this->assertCount(1, $rows); + $this->assertEquals(1, $rows[0]->getLineNumber()); $this->assertEquals(VatCategory::VAT_1, $rows[0]->getVatCategory()); $this->assertEquals(12.09, $rows[0]->getNetValue()); $this->assertEquals(2.91, $rows[0]->getVatAmount()); @@ -136,6 +137,7 @@ public function test_same_vat_exemption_category_rows_are_squashed() $rows = $invoice->getInvoiceDetails(); $this->assertNotNull($rows); $this->assertCount(1, $rows); + $this->assertEquals(1, $rows[0]->getLineNumber()); $this->assertEquals(VatCategory::VAT_1, $rows[0]->getVatCategory()); $this->assertEquals(VatExemption::TYPE_5, $rows[0]->getVatExemptionCategory()); $this->assertEquals(20, $rows[0]->getNetValue()); @@ -183,6 +185,7 @@ public function test_same_vat_category_with_income_and_expense_classifications() $rows = $invoice->getInvoiceDetails(); $this->assertNotNull($rows); $this->assertCount(1, $rows); + $this->assertEquals(1, $rows[0]->getLineNumber()); $this->assertEquals(VatCategory::VAT_1, $rows[0]->getVatCategory()); $this->assertEquals(20, $rows[0]->getNetValue()); @@ -251,6 +254,7 @@ public function test_same_mixed_vat_exemption_category_rows_are_squashed() $this->assertNotNull($rows); $this->assertCount(2, $rows); + $this->assertEquals(1, $rows[0]->getLineNumber()); $this->assertEquals(VatCategory::VAT_1, $rows[0]->getVatCategory()); $this->assertEquals(VatExemption::TYPE_5, $rows[0]->getVatExemptionCategory()); $this->assertEquals(20, $rows[0]->getNetValue()); @@ -263,6 +267,7 @@ public function test_same_mixed_vat_exemption_category_rows_are_squashed() $this->assertEquals(IncomeClassificationType::E3_106, $rows[0]->getIncomeClassification()[0]->getClassificationType()); $this->assertEquals(20, $rows[0]->getIncomeClassification()[0]->getAmount()); + $this->assertEquals(2, $rows[1]->getLineNumber()); $this->assertEquals(VatCategory::VAT_1, $rows[1]->getVatCategory()); $this->assertEquals(VatExemption::TYPE_4, $rows[1]->getVatExemptionCategory()); $this->assertEquals(30, $rows[1]->getNetValue()); @@ -329,6 +334,7 @@ public function test_same_tax_category_rows_are_squashed() $rows = $invoice->getInvoiceDetails(); $this->assertIsArray($rows); $this->assertEquals(100, $rows[0]->getNetValue()); + $this->assertEquals(1, $rows[0]->getLineNumber()); $this->assertEquals(24, $rows[0]->getVatAmount()); $this->assertEquals(20, $rows[0]->getWithheldAmount()); $this->assertEquals(1.5, $rows[0]->getStampDutyAmount()); @@ -396,6 +402,7 @@ public function test_mixed_same_tax_category_rows_are_squashed() $this->assertIsArray($rows); $this->assertCount(2, $rows); + $this->assertEquals(1, $rows[0]->getLineNumber()); $this->assertEquals(50, $rows[0]->getNetValue()); $this->assertEquals(12, $rows[0]->getVatAmount()); $this->assertEquals(10, $rows[0]->getWithheldAmount()); @@ -409,6 +416,7 @@ public function test_mixed_same_tax_category_rows_are_squashed() $this->assertEquals(FeesPercentCategory::TYPE_1, $rows[0]->getFeesPercentCategory()); $this->assertEquals(OtherTaxesPercentCategory::TAX_2, $rows[0]->getOtherTaxesPercentCategory()); + $this->assertEquals(2, $rows[1]->getLineNumber()); $this->assertEquals(50, $rows[1]->getNetValue()); $this->assertEquals(12, $rows[1]->getVatAmount()); $this->assertEquals(10, $rows[1]->getWithheldAmount()); @@ -441,7 +449,17 @@ public function test_rows_with_rec_type_are_not_squashed() } $invoice->squashInvoiceRows(); - $this->assertCount(6, $invoice->getInvoiceDetails()); + $rows = $invoice->getInvoiceDetails(); + + $this->assertIsArray($rows); + $this->assertCount(6, $rows); + $this->assertEquals(1, $rows[0]->getLineNumber()); + $this->assertCount(5, array_filter($invoice->getInvoiceDetails(), fn($row) => $row->getRecType() === RecType::TYPE_5)); + $this->assertEquals(2, $rows[1]->getLineNumber()); + $this->assertEquals(3, $rows[2]->getLineNumber()); + $this->assertEquals(4, $rows[3]->getLineNumber()); + $this->assertEquals(5, $rows[4]->getLineNumber()); + $this->assertEquals(6, $rows[5]->getLineNumber()); } } \ No newline at end of file