From 0dc891f837484e9f1a42500b623529e027dea9be Mon Sep 17 00:00:00 2001 From: Edward Mungai Date: Mon, 5 Apr 2021 15:19:37 +0200 Subject: [PATCH] Update: Prevent transaction dates at the beginning of the first day of the reporting period --- src/Exceptions/InvalidTransactionDate.php | 41 +++++++++++++++++++++++ src/Models/Transaction.php | 5 +++ tests/Unit/TransactionTest.php | 21 ++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 src/Exceptions/InvalidTransactionDate.php diff --git a/src/Exceptions/InvalidTransactionDate.php b/src/Exceptions/InvalidTransactionDate.php new file mode 100644 index 0000000..34540a4 --- /dev/null +++ b/src/Exceptions/InvalidTransactionDate.php @@ -0,0 +1,41 @@ + Auth::user()->id, + 'time' => Carbon::now(), + ] + ); + + parent::__construct($error . ' ' . $message, $code); + } +} diff --git a/src/Models/Transaction.php b/src/Models/Transaction.php index 04e89aa..8cecf73 100644 --- a/src/Models/Transaction.php +++ b/src/Models/Transaction.php @@ -33,6 +33,7 @@ use IFRS\Exceptions\UnpostedAssignment; use IFRS\Exceptions\RedundantTransaction; use IFRS\Exceptions\ClosedReportingPeriod; +use IFRS\Exceptions\InvalidTransactionDate; use IFRS\Exceptions\AdjustingReportingPeriod; /** @@ -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); } diff --git a/tests/Unit/TransactionTest.php b/tests/Unit/TransactionTest.php index 06ed95b..a4f0051 100644 --- a/tests/Unit/TransactionTest.php +++ b/tests/Unit/TransactionTest.php @@ -4,6 +4,7 @@ use Carbon\Carbon; use IFRS\User; + use IFRS\Exceptions\AdjustingReportingPeriod; use IFRS\Exceptions\ClosedReportingPeriod; use IFRS\Exceptions\HangingClearances; @@ -11,6 +12,8 @@ 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; @@ -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 @@ -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'), + ]); + } }