From 1794549a205624bab4c5b76947fb4b0971e5db65 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Wed, 28 Feb 2018 19:04:56 -0600 Subject: [PATCH] Move journal transaction code to repository #130 --- ...1807_create_journal_transactions_table.php | 1 - ...018_02_28_231813_create_journals_table.php | 2 +- app/Models/Journal.php | 1 + app/Repositories/JournalRepository.php | 141 ++++++++++++++++++ app/Support/Money.php | 10 +- 5 files changed, 151 insertions(+), 4 deletions(-) create mode 100644 app/Repositories/JournalRepository.php diff --git a/app/Database/migrations/2018_02_28_231807_create_journal_transactions_table.php b/app/Database/migrations/2018_02_28_231807_create_journal_transactions_table.php index 6ede1c32..3ce67d65 100644 --- a/app/Database/migrations/2018_02_28_231807_create_journal_transactions_table.php +++ b/app/Database/migrations/2018_02_28_231807_create_journal_transactions_table.php @@ -21,7 +21,6 @@ class CreateJournalTransactionsTable extends Migration $table->unsignedBigInteger('credit')->nullable(); $table->char('currency', 5); $table->text('memo')->nullable(); - $table->text('tags')->nullable(); $table->char('ref_class', 32)->nullable(); $table->integer('ref_class_id')->nullable(); $table->timestamps(); diff --git a/app/Database/migrations/2018_02_28_231813_create_journals_table.php b/app/Database/migrations/2018_02_28_231813_create_journals_table.php index ac0c500f..52ccdca7 100644 --- a/app/Database/migrations/2018_02_28_231813_create_journals_table.php +++ b/app/Database/migrations/2018_02_28_231813_create_journals_table.php @@ -16,7 +16,7 @@ class CreateJournalsTable extends Migration Schema::create('journals', function (Blueprint $table) { $table->increments('id'); $table->unsignedInteger('ledger_id')->nullable(); - $table->bigInteger('balance'); + $table->bigInteger('balance')->default(0); $table->char('currency', 5); $table->char('morphed_type', 32); $table->integer('morphed_id'); diff --git a/app/Models/Journal.php b/app/Models/Journal.php index 57b1578f..dfee267f 100644 --- a/app/Models/Journal.php +++ b/app/Models/Journal.php @@ -272,6 +272,7 @@ class Journal extends BaseModel * @param Money $debit * @param $memo * @param Carbon $post_date + * @param $transaction_group * @return JournalTransaction */ private function post(Money $credit = null, Money $debit = null, $memo = null, $post_date = null, $transaction_group) diff --git a/app/Repositories/JournalRepository.php b/app/Repositories/JournalRepository.php new file mode 100644 index 00000000..8c774bbd --- /dev/null +++ b/app/Repositories/JournalRepository.php @@ -0,0 +1,141 @@ + $journal->id, + 'credit' => $credit ? $credit->getAmount():null, + 'debit' => $debit ? $debit->getAmount():null, + 'currency_code' => config('phpvms.currency'), + 'memo' => $memo, + 'post_date' => $post_date ?: Carbon::now(), + 'transaction_group' => $transaction_group, + ]; + + if($reference !== null) { + $attrs['ref_class'] = \get_class($reference); + $attrs['ref_class_id'] = $reference->id; + } + + try { + $transaction = $this->create($attrs); + } catch (ValidatorException $e) { + throw $e; + } + + # Adjust the balance on the journal + $balance = new Money($journal->balance); + if($credit) { + $balance = $balance->add($credit); + } + + if($debit) { + $balance = $balance->subtract($debit); + } + + $journal->balance = $balance->getAmount(); + $journal->save(); + + return $transaction; + } + + /** + * @param Journal $journal + * @param Carbon|null $date + * @return Money + * @throws \UnexpectedValueException + * @throws \InvalidArgumentException + */ + public function getBalance(Journal $journal, Carbon $date=null) + { + if(!$date) { + $date = Carbon::now(); + } + + $credit = $this->getCreditBalanceOn($journal, $date); + $debit = $this->getDebitBalanceOn($journal, $date); + + return $credit->subtract($debit); + } + + /** + * Get the credit only balance of the journal based on a given date. + * @param Journal $journal + * @param Carbon $date + * @return Money + * @throws \UnexpectedValueException + * @throws \InvalidArgumentException + */ + public function getCreditBalanceOn(Journal $journal, Carbon $date) + { + $balance = $this->findWhere([ + 'journal_id' => $journal->id, + ['post_date', '<=', $date] + ], ['id', 'credit'])->sum('credit') ?: 0; + + return new Money($balance); + } + + /** + * @param Journal $journal + * @param Carbon $date + * @return Money + * @throws \UnexpectedValueException + * @throws \InvalidArgumentException + */ + public function getDebitBalanceOn(Journal $journal, Carbon $date): Money + { + $balance = $this->findWhere([ + 'journal_id' => $journal->id, + ['post_date', '<=', $date] + ], ['id', 'debit'])->sum('debit') ?: 0; + + return new Money($balance); + } +} diff --git a/app/Support/Money.php b/app/Support/Money.php index 398f0f91..105f057b 100644 --- a/app/Support/Money.php +++ b/app/Support/Money.php @@ -43,12 +43,18 @@ class Money } /** + * Create a new currency object using the currency setting + * Fall back to USD if it's not valid * @return Currency - * @throws \OutOfBoundsException */ public static function currency() { - return new Currency(config('phpvms.currency')); + try { + return new Currency(config('phpvms.currency', 'USD')); + } catch (\OutOfBoundsException $e) { + return new Currency('USD'); + } + } /**