2015-06-05 10:02:40 +02:00
|
|
|
<?php
|
2016-02-05 12:08:25 +01:00
|
|
|
declare(strict_types = 1);
|
2015-06-05 10:02:40 +02:00
|
|
|
|
|
|
|
namespace FireflyIII\Repositories\Shared;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2015-12-27 21:17:04 +01:00
|
|
|
use DB;
|
2015-12-09 22:39:50 -02:00
|
|
|
use FireflyIII\Models\TransactionType;
|
2015-12-11 17:53:17 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2015-06-05 10:02:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ComponentRepository
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Repositories\Shared
|
|
|
|
*/
|
|
|
|
class ComponentRepository
|
|
|
|
{
|
|
|
|
|
2015-12-11 17:53:17 +01:00
|
|
|
/**
|
|
|
|
* @param $object
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-12-24 10:27:45 +01:00
|
|
|
protected function commonBalanceInPeriod($object, Carbon $start, Carbon $end, Collection $accounts)
|
2015-12-11 17:53:17 +01:00
|
|
|
{
|
2016-04-30 22:05:58 +02:00
|
|
|
// all balances based on transaction journals:
|
|
|
|
// TODO somehow exclude those with transactions below?
|
|
|
|
// TODO needs a completely new query.
|
|
|
|
$ids = $accounts->pluck('id')->toArray();
|
2015-12-27 21:17:04 +01:00
|
|
|
$entry = $object->transactionjournals()
|
|
|
|
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
|
|
|
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
|
|
|
|
->whereIn('accounts.id', $ids)
|
2016-04-30 22:05:58 +02:00
|
|
|
->transactionTypes([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE])
|
|
|
|
->before($end)
|
2015-12-27 21:17:04 +01:00
|
|
|
->after($start)
|
2016-01-15 17:38:09 +01:00
|
|
|
->first([DB::raw('SUM(`transactions`.`amount`) as `journalAmount`')]);
|
2016-02-05 12:28:05 +01:00
|
|
|
$amount = $entry->journalAmount ?? '0';
|
2015-12-27 21:17:04 +01:00
|
|
|
|
2016-04-30 22:05:58 +02:00
|
|
|
// all balances based on individual transactions (at the moment, it's an "or or"):
|
|
|
|
$entry = $object
|
|
|
|
->transactions()
|
|
|
|
// left join journals to get some meta-information.
|
|
|
|
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
|
|
|
// also left join transaction types so we can do the same type of filtering.
|
|
|
|
->leftJoin('transaction_types', 'transaction_journals.transaction_type_id', '=', 'transaction_types.id')
|
|
|
|
// need to do these manually.
|
|
|
|
->whereIn('transaction_types.type', [TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE])
|
|
|
|
->where('transaction_journals.date', '>=', $start->format('Y-m-d 00:00:00'))
|
|
|
|
->where('transaction_journals.date', '<=', $end->format('Y-m-d 00:00:00'))
|
|
|
|
->whereIn('transactions.account_id', $ids)
|
|
|
|
->first([DB::raw('SUM(`transactions`.`amount`) as `journalAmount`')]);
|
|
|
|
|
|
|
|
// sum of amount:
|
|
|
|
$extraAmount = $entry->journalAmount ?? '0';
|
|
|
|
$result = bcadd($amount, $extraAmount);
|
|
|
|
|
|
|
|
return $result;
|
2015-12-11 17:53:17 +01:00
|
|
|
}
|
2015-06-05 13:39:24 +02:00
|
|
|
}
|