phpvms/app/Repositories/JournalRepository.php

301 lines
7.9 KiB
PHP
Raw Normal View History

<?php
namespace App\Repositories;
use App\Contracts\Repository;
use App\Models\Journal;
use App\Models\JournalTransaction;
use App\Support\Money;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Prettus\Repository\Contracts\CacheableInterface;
use Prettus\Repository\Traits\CacheableRepository;
use Prettus\Validator\Exceptions\ValidatorException;
/**
* Class JournalRepository
*/
class JournalRepository extends Repository implements CacheableInterface
{
use CacheableRepository;
/**
* @return string
*/
public function model()
{
return JournalTransaction::class;
}
/**
* Return a Y-m-d string for the post date
2018-08-27 00:40:04 +08:00
*
* @param Carbon $date
2018-08-27 00:40:04 +08:00
*
* @return string
*/
public function formatPostDate(Carbon $date = null)
{
if (!$date) {
2018-08-27 00:40:04 +08:00
return;
}
return $date->setTimezone('UTC')->toDateString();
}
/**
* Recalculate the balance of the given journal
2018-08-27 00:40:04 +08:00
*
* @param Journal $journal
2018-08-27 00:40:04 +08:00
*
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
2018-08-27 00:40:04 +08:00
*
* @return Journal
*/
public function recalculateBalance(Journal $journal)
{
$where = [
2018-08-27 00:40:04 +08:00
'journal_id' => $journal->id,
];
$credits = Money::create($this->findWhere($where)->sum('credit') ?: 0);
$debits = Money::create($this->findWhere($where)->sum('debit') ?: 0);
$balance = $credits->subtract($debits);
$journal->balance = $balance->getAmount();
$journal->save();
return $journal;
}
/**
* Post a new transaction to a journal, and also adjust the balance
* on the transaction itself. A cron will run to reconcile the journal
* balance nightly, since they're not atomic operations
*
* @param Journal $journal
2018-08-27 00:40:04 +08:00
* @param Money|null $credit Amount to credit
* @param Money|null $debit Amount to debit
* @param Model|null $reference The object this is a reference to
* @param string|null $memo Memo for this transaction
* @param string|null $post_date Date of the posting
* @param string|null $transaction_group Grouping name for the summaries
* @param array|string|null $tags Tag used for grouping/finding items
2018-08-27 00:40:04 +08:00
*
* @throws ValidatorException
2018-08-27 00:40:04 +08:00
*
* @return mixed
*/
public function post(
Journal &$journal,
Money $credit = null,
Money $debit = null,
$reference = null,
$memo = null,
$post_date = null,
2018-03-06 20:40:49 +08:00
$transaction_group = null,
$tags = null
2018-08-27 00:40:04 +08:00
) {
// tags can be passed in a list
if ($tags && \is_array($tags)) {
2018-03-06 20:40:49 +08:00
$tags = implode(',', $tags);
}
if (!$post_date) {
$post_date = Carbon::now('UTC');
}
$attrs = [
'journal_id' => $journal->id,
'credit' => $credit ? $credit->getAmount() : null,
'debit' => $debit ? $debit->getAmount() : null,
'currency' => config('phpvms.currency'),
'memo' => $memo,
'post_date' => $post_date,
'transaction_group' => $transaction_group,
2018-08-27 00:40:04 +08:00
'tags' => $tags,
];
if ($reference !== null) {
2018-04-02 03:32:01 +08:00
$attrs['ref_model'] = \get_class($reference);
$attrs['ref_model_id'] = $reference->id;
}
try {
$transaction = $this->create($attrs);
} catch (ValidatorException $e) {
throw $e;
}
$journal->refresh();
return $transaction;
}
/**
* @param Journal $journal
* @param Carbon|null $date
2018-08-27 00:40:04 +08:00
*
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
2018-08-27 00:40:04 +08:00
*
* @return Money
*/
public function getBalance(Journal $journal = null, Carbon $date = null)
{
$journal->refresh();
if (!$date) {
$date = Carbon::now();
}
$credit = $this->getCreditBalanceBetween($date, $journal);
$debit = $this->getDebitBalanceBetween($date, $journal);
return $credit->subtract($debit);
}
/**
* Get the credit only balance of the journal based on a given date.
2018-08-27 00:40:04 +08:00
*
* @param Carbon $date
* @param Journal $journal
2018-03-04 23:45:23 +08:00
* @param Carbon|null $start_date
* @param null $transaction_group
2018-08-27 00:40:04 +08:00
*
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
2018-08-27 00:40:04 +08:00
*
* @return Money
*/
public function getCreditBalanceBetween(
Carbon $date,
Journal $journal = null,
Carbon $start_date = null,
$transaction_group = null
2018-08-27 00:40:04 +08:00
): Money {
$where = [];
if ($journal) {
$where['journal_id'] = $journal->id;
}
2018-03-04 23:45:23 +08:00
if ($transaction_group) {
$where['transaction_group'] = $transaction_group;
}
$query = JournalTransaction::where($where);
$query = $query->whereDate('post_date', '<=', $date->toDateString());
if ($start_date) {
$query = $query->whereDate('post_date', '>=', $start_date->toDateString());
}
$balance = $query->sum('credit') ?: 0;
return new Money($balance);
}
/**
* @param Carbon $date
* @param Journal $journal
2018-03-03 07:29:11 +08:00
* @param Carbon|null $start_date
* @param null $transaction_group
2018-08-27 00:40:04 +08:00
*
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
2018-08-27 00:40:04 +08:00
*
* @return Money
*/
public function getDebitBalanceBetween(
Carbon $date,
Journal $journal = null,
Carbon $start_date = null,
$transaction_group = null
2018-08-27 00:40:04 +08:00
): Money {
$where = [];
if ($journal) {
$where['journal_id'] = $journal->id;
}
if ($transaction_group) {
2018-03-04 23:45:23 +08:00
$where['transaction_group'] = $transaction_group;
}
$query = JournalTransaction::where($where);
$query = $query->whereDate('post_date', '<=', $date->toDateString());
if ($start_date) {
$query = $query->whereDate('post_date', '>=', $start_date->toDateString());
}
$balance = $query->sum('debit') ?: 0;
return new Money($balance);
}
/**
* Return all transactions for a given object
2018-08-27 00:40:04 +08:00
*
* @param $object
* @param null $journal
* @param Carbon|null $date
2018-08-27 00:40:04 +08:00
*
2018-03-03 07:29:11 +08:00
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
2018-08-27 00:40:04 +08:00
*
* @return array
*/
public function getAllForObject($object, $journal = null, Carbon $date = null)
{
2018-03-03 06:09:48 +08:00
$where = [
2018-04-02 03:32:01 +08:00
'ref_model' => \get_class($object),
'ref_model_id' => $object->id,
2018-03-03 06:09:48 +08:00
];
if ($journal) {
2018-03-03 06:09:48 +08:00
$where['journal_id'] = $journal->id;
}
if ($date) {
$date = $this->formatPostDate($date);
$where[] = ['post_date', '=', $date];
}
2018-03-03 07:29:11 +08:00
$transactions = $this->whereOrder($where, [
'credit' => 'desc',
2018-08-27 00:40:04 +08:00
'debit' => 'desc',
])->get();
return [
'credits' => new Money($transactions->sum('credit')),
'debits' => new Money($transactions->sum('debit')),
'transactions' => $transactions,
];
}
2018-03-03 07:29:11 +08:00
/**
* Delete all transactions for a given object
2018-08-27 00:40:04 +08:00
*
* @param $object
2018-03-03 07:29:11 +08:00
* @param null $journal
2018-08-27 00:40:04 +08:00
*
2018-03-03 07:29:11 +08:00
* @return void
*/
public function deleteAllForObject($object, $journal = null)
{
$where = [
2018-04-02 03:32:01 +08:00
'ref_model' => \get_class($object),
'ref_model_id' => $object->id,
2018-03-03 07:29:11 +08:00
];
if ($journal) {
$where['journal_id'] = $journal->id;
}
$this->deleteWhere($where);
}
}