Skip to content

Commit

Permalink
Fixed incorrect invoice row line number.
Browse files Browse the repository at this point in the history
  • Loading branch information
firebed committed Jun 12, 2024
1 parent d601063 commit 93205af
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/Actions/SquashInvoiceRows.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down
20 changes: 19 additions & 1 deletion tests/SquashInvoiceRowsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 93205af

Please sign in to comment.