Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ekmungai committed Apr 5, 2021
2 parents 8796e20 + f789d61 commit 0f60b7e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 4 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);
}
}
3 changes: 2 additions & 1 deletion src/Models/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ public function transactionsQuery(Carbon $startDate, Carbon $endDate)
$transactionTable . '.transaction_no',
$transactionTable . '.reference',
$transactionTable . '.transaction_type',
$transactionTable . '.credited',
$transactionTable . '.narration'
)->distinct();

Expand Down Expand Up @@ -404,7 +405,7 @@ public function getTransactions(string $startDate = null, string $endDate = null
$transaction->type = Transaction::getType($transaction->transaction_type);
$transaction->date = Carbon::parse($transaction->transaction_date)->toFormattedDateString();
$transactions['transactions'][] = $transaction;
$transactions['total'] += $transaction->amount;
$transactions['total'] += $transaction->credited ? $transaction->amount * -1 : $transaction->amount;
}
return $transactions;
}
Expand Down
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
6 changes: 3 additions & 3 deletions tests/Unit/AccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -831,13 +831,13 @@ public function testAccountsTransactions()
$endDate = Carbon::now()->addWeeks(4)->toDateString();

$clientTransactions = $account1->getTransactions($startDate, $endDate);
$this->assertEquals($clientTransactions['total'], 195);
$this->assertEquals($clientTransactions['total'], -195);

$incomeTransactions = $account5->getTransactions($startDate, $endDate);
$this->assertEquals($incomeTransactions['total'], 125);
$this->assertEquals($incomeTransactions['total'], -125);

$vatTransactions = $account6->getTransactions($startDate, $endDate);
$this->assertEquals($vatTransactions['total'], 70);
$this->assertEquals($vatTransactions['total'], -70);
}

/**
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 0f60b7e

Please sign in to comment.