Skip to content

Commit

Permalink
Added options array for auto summarizing invoices.
Browse files Browse the repository at this point in the history
  • Loading branch information
firebed committed Feb 27, 2024
1 parent 5250f84 commit 58163c8
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 22 deletions.
27 changes: 17 additions & 10 deletions src/Actions/GroupClassifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ class GroupClassifications

/**
* @param InvoiceDetails[] $rows
* @param array $options
* @return array
*/
public function handle(?array $rows): array
public function handle(?array $rows, array $options = []): array
{
if (empty($rows)) {
return [[], []];
Expand All @@ -31,25 +33,28 @@ public function handle(?array $rows): array
$this->groupExpensesClassifications($row);
}

return [$this->flattenIncomeClassifications(), $this->flattenExpensesClassifications()];
return [$this->flattenIncomeClassifications($options), $this->flattenExpensesClassifications($options)];
}

private function flattenIncomeClassifications(): array
private function flattenIncomeClassifications(array $options = []): array
{
$flattenedIncomeClassifications = [];

foreach ($this->incomeClassifications as $iclsCategory => $iclsTypes) {
$category = IncomeClassificationCategory::from($iclsCategory);

foreach ($iclsTypes as $iclsType => $amount) {
$id = count($flattenedIncomeClassifications) + 1;
$type = $iclsType ? IncomeClassificationType::from($iclsType) : null;

$icls = new IncomeClassification();
$icls->setClassificationType($type);
$icls->setClassificationCategory($category);
$icls->setAmount(round($amount, 2));
$icls->setId($id);

if (isset($options['enableClassificationIds']) && $options['enableClassificationIds']) {
$id = count($flattenedIncomeClassifications) + 1;
$icls->setId($id);
}

$flattenedIncomeClassifications[] = $icls;
}
Expand All @@ -58,7 +63,7 @@ private function flattenIncomeClassifications(): array
return $flattenedIncomeClassifications;
}

private function flattenExpensesClassifications(): array
private function flattenExpensesClassifications(array $options = []): array
{
$flattenedExpensesClassifications = [];

Expand All @@ -73,17 +78,19 @@ private function flattenExpensesClassifications(): array

foreach ($eclsVatExemptions as $vatExemption => $amounts) {
$exemption = $vatExemption ? VatExemption::from($vatExemption) : null;

$id = count($flattenedExpensesClassifications) + 1;


$ecls = new ExpensesClassification();
$ecls->setClassificationType($type);
$ecls->setClassificationCategory($category);
$ecls->setAmount(round($amounts['amount'], 2));
$ecls->setVatAmount(round($amounts['vatAmount'], 2));
$ecls->setVatCategory($vat);
$ecls->setVatExemptionCategory($exemption);
$ecls->setId($id);

if (isset($options['enableClassificationIds']) && $options['enableClassificationIds']) {
$id = count($flattenedExpensesClassifications) + 1;
$ecls->setId($id);
}

$flattenedExpensesClassifications[] = $ecls;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Actions/SummarizeInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@

class SummarizeInvoice
{
public function handle(Invoice $invoice): InvoiceSummary
public function handle(Invoice $invoice, array $options = []): InvoiceSummary
{
$summary = new InvoiceSummary();

$sumInvoiceRows = new SummarizeInvoiceRows();
$sumInvoiceRows->handle($invoice->getInvoiceDetails());
$sumInvoiceRows->handle($invoice->getInvoiceDetails(), $options);
$sumInvoiceRows->saveTotals($summary);

$sumInvoiceTaxes = new SummarizeInvoiceTaxes();
$sumInvoiceTaxes->handle($invoice->getTaxesTotals());
$sumInvoiceTaxes->handle($invoice->getTaxesTotals(), $options);
$sumInvoiceTaxes->saveTotals($summary);

$classificationsGroup = new GroupClassifications();
[$icls, $ecls] = $classificationsGroup->handle($invoice->getInvoiceDetails());
[$icls, $ecls] = $classificationsGroup->handle($invoice->getInvoiceDetails(), $options);

$summary->setIncomeClassifications($icls);
$summary->setExpensesClassifications($ecls);
Expand Down
3 changes: 2 additions & 1 deletion src/Actions/SummarizeInvoiceRows.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ class SummarizeInvoiceRows

/**
* @param InvoiceDetails[]|null $rows
* @param array $options
* @return void
*/
public function handle(?array $rows): void
public function handle(?array $rows, array $options = []): void
{
if (empty($rows)) {
return;
Expand Down
3 changes: 2 additions & 1 deletion src/Actions/SummarizeInvoiceTaxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ class SummarizeInvoiceTaxes

/**
* @param TaxesTotals|null $taxes
* @param array $options
* @return void
*/
public function handle(?TaxesTotals $taxes): void
public function handle(?TaxesTotals $taxes, array $options = []): void
{
if (empty($taxes)) {
return;
Expand Down
10 changes: 8 additions & 2 deletions src/Models/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,15 @@ public function setInvoiceSummary(InvoiceSummary $invoiceSummary): void
$this->set('invoiceSummary', $invoiceSummary);
}

public function summarizeInvoice(): InvoiceSummary
/**
* @param array{
* enableClassificationIds: bool,
* } $options
* @return InvoiceSummary
*/
public function summarizeInvoice(array $options = []): InvoiceSummary
{
$summary = (new SummarizeInvoice)->handle($this);
$summary = (new SummarizeInvoice)->handle($this, $options);
$this->setInvoiceSummary($summary);

return $summary;
Expand Down
36 changes: 32 additions & 4 deletions tests/SummarizeInvoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,45 +194,73 @@ public function test_it_summarizes_invoice_row_expenses_classifications()
$ecls1 = $this->findExpensesClassification($ecls, ExpenseClassificationType::E3_102_001, ExpenseClassificationCategory::CATEGORY_2_1);
$this->assertNotNull($ecls1);
$this->assertEquals(301.10, $ecls1->getAmount());
$this->assertEmpty($ecls1->getId());

$ecls2 = $this->findExpensesClassification($ecls, ExpenseClassificationType::E3_102_001, ExpenseClassificationCategory::CATEGORY_2_3);
$this->assertNotNull($ecls2);
$this->assertEquals(700.80, $ecls2->getAmount());
$this->assertEmpty($ecls2->getId());

$ecls3 = $this->findExpensesClassification($ecls, ExpenseClassificationType::E3_102_002, ExpenseClassificationCategory::CATEGORY_2_1);
$this->assertNotNull($ecls3);
$this->assertEquals(500.88, $ecls3->getAmount());
$this->assertEmpty($ecls3->getId());

$ecls4 = $this->findExpensesClassification($ecls, ExpenseClassificationType::E3_102_002, ExpenseClassificationCategory::CATEGORY_2_3);
$this->assertNotNull($ecls4);
$this->assertEquals(600.66, $ecls4->getAmount());
$this->assertEmpty($ecls4->getId());

$ecls5 = $this->findExpensesClassification($ecls, ExpenseClassificationType::E3_102_006, ExpenseClassificationCategory::CATEGORY_2_6);
$this->assertNotNull($ecls5);
$this->assertEquals(600, $ecls5->getAmount());
$this->assertEmpty($ecls5->getId());

$ecls6 = $this->findExpensesClassification($ecls, null, ExpenseClassificationCategory::CATEGORY_2_6);
$this->assertNotNull($ecls6);
$this->assertEquals(61, $ecls6->getAmount());
$this->assertEmpty($ecls6->getId());

$ecls7 = $this->findExpensesClassification($ecls, null, ExpenseClassificationCategory::CATEGORY_2_7);
$this->assertNotNull($ecls7);
$this->assertEquals(115.66, $ecls7->getAmount());
$this->assertEmpty($ecls7->getId());

$ecls8 = $this->findExpensesClassification($ecls, null, ExpenseClassificationCategory::CATEGORY_2_7, VatCategory::VAT_1);
$this->assertNotNull($ecls8);
$this->assertEquals(90, $ecls8->getAmount());
$this->assertEquals(21.6, $ecls8->getVatAmount());
$this->assertEmpty($ecls8->getId());

$ecls10 = $this->findExpensesClassification($ecls, null, ExpenseClassificationCategory::CATEGORY_2_7, VatCategory::VAT_7, VatExemption::TYPE_12);
$this->assertNotNull($ecls10);
$this->assertEquals(50, $ecls10->getAmount());
$this->assertEquals(0, $ecls10->getVatAmount());
$ecls9 = $this->findExpensesClassification($ecls, null, ExpenseClassificationCategory::CATEGORY_2_7, VatCategory::VAT_7, VatExemption::TYPE_12);
$this->assertNotNull($ecls9);
$this->assertEquals(50, $ecls9->getAmount());
$this->assertEquals(0, $ecls9->getVatAmount());
$this->assertEmpty($ecls9->getId());

$ecls10 = $this->findExpensesClassification($ecls, ExpenseClassificationType::E3_102_006, ExpenseClassificationCategory::CATEGORY_2_7, VatCategory::VAT_7, VatExemption::TYPE_13);
$this->assertNotNull($ecls10);
$this->assertEquals(160, $ecls10->getAmount());
$this->assertEquals(0, $ecls10->getVatAmount());
$this->assertEmpty($ecls10->getId());
}

public function test_classification_ids_are_empty_when_disabled()
{
$row = new InvoiceDetails();
$row->addExpensesClassification($this->createEcls(ExpenseClassificationType::E3_102_001, ExpenseClassificationCategory::CATEGORY_2_1, 100.55));

$invoice = new Invoice();
$invoice->addInvoiceDetails($row);
$summary = $invoice->summarizeInvoice(['enableClassificationIds' => true]);

$ecls = $summary->getExpensesClassifications();

$ecls1 = $this->findExpensesClassification($ecls, ExpenseClassificationType::E3_102_001, ExpenseClassificationCategory::CATEGORY_2_1);
$this->assertNotNull($ecls1);
$this->assertEquals(100.55, $ecls1->getAmount());
$this->assertEquals(0, $ecls1->getVatAmount());
$this->assertEquals(1, $ecls1->getId());
}

public function test_it_summarizes_invoice_taxes()
Expand Down

0 comments on commit 58163c8

Please sign in to comment.