2015-06-05 03:02:40 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace FireflyIII\Repositories\Shared;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2015-12-11 10:53:17 -06:00
|
|
|
use FireflyIII\Models\Account;
|
2015-12-09 18:39:50 -06:00
|
|
|
use FireflyIII\Models\TransactionType;
|
2015-06-05 12:02:23 -05:00
|
|
|
use FireflyIII\Support\CacheProperties;
|
2015-06-05 03:02:40 -05:00
|
|
|
use Illuminate\Database\Query\JoinClause;
|
2015-12-11 10:53:17 -06:00
|
|
|
use Illuminate\Support\Collection;
|
2015-06-05 03:02:40 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ComponentRepository
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Repositories\Shared
|
|
|
|
*/
|
|
|
|
class ComponentRepository
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-05 09:49:16 -05:00
|
|
|
* @param $object
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2015-06-05 03:02:40 -05:00
|
|
|
*
|
2015-06-05 09:49:16 -05:00
|
|
|
* @param bool $shared
|
2015-06-05 03:02:40 -05:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-08-02 00:04:43 -05:00
|
|
|
protected function commonBalanceInPeriod($object, Carbon $start, Carbon $end, $shared = false)
|
2015-06-05 03:02:40 -05:00
|
|
|
{
|
2015-07-07 03:05:11 -05:00
|
|
|
$cache = new CacheProperties; // we must cache this.
|
2015-06-05 12:02:23 -05:00
|
|
|
$cache->addProperty($object->id);
|
|
|
|
$cache->addProperty(get_class($object));
|
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
|
|
|
$cache->addProperty($shared);
|
2015-08-02 00:04:43 -05:00
|
|
|
$cache->addProperty('balanceInPeriod');
|
2015-06-05 12:02:23 -05:00
|
|
|
|
2015-06-06 16:09:12 -05:00
|
|
|
if ($cache->has()) {
|
2015-06-05 12:02:23 -05:00
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
|
2015-07-07 03:05:11 -05:00
|
|
|
if ($shared === true) { // shared is true: always ignore transfers between accounts!
|
2015-12-09 18:39:50 -06:00
|
|
|
$sum = $object->transactionjournals()
|
|
|
|
->transactionTypes([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE])
|
|
|
|
->before($end)
|
|
|
|
->after($start)
|
2015-09-25 13:40:24 -05:00
|
|
|
->get(['transaction_journals.*'])->sum('amount');
|
2015-06-05 03:02:40 -05:00
|
|
|
} else {
|
|
|
|
// do something else, SEE budgets.
|
|
|
|
// get all journals in this month where the asset account is NOT shared.
|
2015-07-07 03:05:11 -05:00
|
|
|
$sum = $object->transactionjournals()->before($end)->after($start)
|
2015-06-05 06:39:24 -05:00
|
|
|
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
|
|
|
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
|
2015-12-09 18:39:50 -06:00
|
|
|
->transactionTypes([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE])
|
2015-06-05 06:39:24 -05:00
|
|
|
->leftJoin(
|
|
|
|
'account_meta', function (JoinClause $join) {
|
|
|
|
$join->on('account_meta.account_id', '=', 'accounts.id')->where('account_meta.name', '=', 'accountRole');
|
|
|
|
}
|
2015-09-25 13:40:24 -05:00
|
|
|
)->where('account_meta.data', '!=', '"sharedAsset"')->get(['transaction_journals.*'])->sum('amount');
|
2015-06-05 03:02:40 -05:00
|
|
|
}
|
|
|
|
|
2015-06-05 12:02:23 -05:00
|
|
|
$cache->store($sum);
|
|
|
|
|
2015-06-05 03:02:40 -05:00
|
|
|
return $sum;
|
|
|
|
}
|
2015-12-11 10:53:17 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $object
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function commonBalanceInPeriodForList($object, Carbon $start, Carbon $end, Collection $accounts)
|
|
|
|
{
|
|
|
|
$cache = new CacheProperties; // we must cache this.
|
|
|
|
$cache->addProperty($object->id);
|
|
|
|
$cache->addProperty(get_class($object));
|
|
|
|
$cache->addProperty($start);
|
|
|
|
$cache->addProperty($end);
|
|
|
|
$cache->addProperty($accounts);
|
|
|
|
$cache->addProperty('balanceInPeriodList');
|
|
|
|
|
|
|
|
$ids = [];
|
|
|
|
/** @var Account $account */
|
|
|
|
foreach ($accounts as $account) {
|
|
|
|
$ids[] = $account->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($cache->has()) {
|
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
|
|
|
|
$sum = $object->transactionjournals()
|
|
|
|
->transactionTypes([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE])
|
|
|
|
->before($end)
|
|
|
|
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
|
|
|
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
|
|
|
|
->whereIn('accounts.id', $ids)
|
|
|
|
->after($start)
|
|
|
|
->get(['transaction_journals.*'])->sum('amount');
|
|
|
|
|
|
|
|
$cache->store($sum);
|
|
|
|
|
|
|
|
return $sum;
|
|
|
|
}
|
2015-06-05 06:39:24 -05:00
|
|
|
}
|