Skip to content

Commit

Permalink
Update: Prevent transaction dates at the beginning of the first day o…
Browse files Browse the repository at this point in the history
…f the reporting period
  • Loading branch information
ekmungai committed Apr 5, 2021
1 parent d0b87ef commit 0dc891f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/Exceptions/InvalidTransactionDate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* Eloquent IFRS Accounting
*
* @author Edward Mungai
* @copyright Edward Mungai, 2021, Germany
* @license MIT
*/

namespace IFRS\Exceptions;

use Carbon\Carbon;

use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Auth;

class InvalidTransactionDate extends IFRSException
{

/**
* Invalid Transaction Date Exception
*
* @param string $message
* @param int $code
*/
public function __construct(string $message = null, int $code = null)
{
$error = "Transaction date cannot be at the beginning of the first day of the Reporting Period. Use a Balance object instead ";

Log::notice(
$error . $message,
[
'user_id' => Auth::user()->id,
'time' => Carbon::now(),
]
);

parent::__construct($error . ' ' . $message, $code);
}
}
5 changes: 5 additions & 0 deletions src/Models/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use IFRS\Exceptions\UnpostedAssignment;
use IFRS\Exceptions\RedundantTransaction;
use IFRS\Exceptions\ClosedReportingPeriod;
use IFRS\Exceptions\InvalidTransactionDate;
use IFRS\Exceptions\AdjustingReportingPeriod;

/**
Expand Down Expand Up @@ -586,6 +587,10 @@ public function save(array $options = []): bool
{
$period = ReportingPeriod::getPeriod(Carbon::parse($this->transaction_date));

if (ReportingPeriod::periodStart($this->transaction_date)->eq(Carbon::parse($this->transaction_date))) {
throw new InvalidTransactionDate();
}

if ($period->status == ReportingPeriod::CLOSED) {
throw new ClosedReportingPeriod($period->calendar_year);
}
Expand Down
21 changes: 21 additions & 0 deletions tests/Unit/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

use Carbon\Carbon;
use IFRS\User;

use IFRS\Exceptions\AdjustingReportingPeriod;
use IFRS\Exceptions\ClosedReportingPeriod;
use IFRS\Exceptions\HangingClearances;
use IFRS\Exceptions\MissingLineItem;
use IFRS\Exceptions\PostedTransaction;
use IFRS\Exceptions\RedundantTransaction;
use IFRS\Exceptions\UnpostedAssignment;
use IFRS\Exceptions\InvalidTransactionDate;

use IFRS\Models\Account;
use IFRS\Models\Assignment;
use IFRS\Models\Currency;
Expand All @@ -21,9 +24,11 @@
use IFRS\Models\ReportingPeriod;
use IFRS\Models\Transaction;
use IFRS\Models\Vat;

use IFRS\Tests\TestCase;
use IFRS\Transactions\ClientInvoice;
use IFRS\Transactions\JournalEntry;

use Illuminate\Support\Facades\DB;

class TransactionTest extends TestCase
Expand Down Expand Up @@ -1004,4 +1009,20 @@ public function testTransactionPredicates()
$this->assertFalse($cleared->assignable);
$this->assertTrue($cleared->clearable);
}

/**
* Test Invalid Transaction Date.
*
* @return void
*/
public function testInvalidTransactionDate()
{

$this->expectException(InvalidTransactionDate::class);
$this->expectExceptionMessage('Transaction date cannot be at the beginning of the first day of the Reporting Period. Use a Balance object instead');

Transaction::create([
'transaction_date' => Carbon::parse(date('Y').'-01-01'),
]);
}
}

0 comments on commit 0dc891f

Please sign in to comment.