mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-16 18:25:00 -06:00
Move some methods around, refactoring.
This commit is contained in:
parent
ea7ee7ee9a
commit
c64771b76b
@ -20,6 +20,7 @@ use FireflyIII\Http\Requests\AccountFormRequest;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
|
||||
use FireflyIII\Repositories\Account\AccountTaskerInterface;
|
||||
use FireflyIII\Support\CacheProperties;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Collection;
|
||||
@ -196,12 +197,14 @@ class AccountController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ARI $repository
|
||||
* @param Account $account
|
||||
* @param AccountTaskerInterface $tasker
|
||||
* @param AccountCrudInterface $crud
|
||||
* @param ARI $repository
|
||||
* @param Account $account
|
||||
*
|
||||
* @return View
|
||||
*/
|
||||
public function show(ARI $repository, Account $account)
|
||||
public function show(AccountTaskerInterface $tasker, AccountCrudInterface $crud, ARI $repository, Account $account)
|
||||
{
|
||||
// show journals from current period only:
|
||||
$subTitleIcon = config('firefly.subIconsByIdentifier.' . $account->accountType->type);
|
||||
@ -240,16 +243,15 @@ class AccountController extends Controller
|
||||
|
||||
|
||||
if ($cache->has()) {
|
||||
$entries = $cache->get();
|
||||
|
||||
return view('accounts.show', compact('account', 'what', 'entries', 'subTitleIcon', 'journals', 'subTitle'));
|
||||
// $entries = $cache->get();
|
||||
// return view('accounts.show', compact('account', 'what', 'entries', 'subTitleIcon', 'journals', 'subTitle'));
|
||||
}
|
||||
|
||||
$assets = $crud->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT]);
|
||||
while ($end >= $start) {
|
||||
$end = Navigation::startOfPeriod($end, $range);
|
||||
$currentEnd = Navigation::endOfPeriod($end, $range);
|
||||
$spent = $this->spentInPeriod($account, $end, $currentEnd);
|
||||
$earned = $this->earnedInPeriod($account, $end, $currentEnd);
|
||||
$spent = $tasker->amountOutInPeriod(new Collection([$account]), $assets, $end, $currentEnd);
|
||||
$earned = $tasker->amountInInPeriod(new Collection([$account]), $assets, $end, $currentEnd);
|
||||
$dateStr = $end->format('Y-m-d');
|
||||
$dateName = Navigation::periodShow($end, $range);
|
||||
$entries->push([$dateStr, $dateName, $spent, $earned]);
|
||||
@ -391,69 +393,4 @@ class AccountController extends Controller
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Asset accounts actually earn money by being the destination of a deposit or the destination
|
||||
* of a transfer. The money moves to them.
|
||||
*
|
||||
* A revenue account doesn't really earn money itself. Money is earned "from" the revenue account.
|
||||
* So, the call to find out how many money has been earned by/from a revenue account is slightly different.
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param Account $account
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function earnedInPeriod(Account $account, Carbon $start, Carbon $end)
|
||||
{
|
||||
/** @var ARI $repository */
|
||||
$repository = app(ARI::class);
|
||||
$collection = new Collection([$account]);
|
||||
$type = $account->accountType->type;
|
||||
switch ($type) {
|
||||
case AccountType::DEFAULT:
|
||||
case AccountType::ASSET:
|
||||
return $repository->earnedInPeriod($collection, $start, $end);
|
||||
case AccountType::REVENUE:
|
||||
return $repository->earnedFromInPeriod($collection, $start, $end);
|
||||
default:
|
||||
return '0';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Asset accounts actually spend money by being the source of a withdrawal or the source
|
||||
* of a transfer. The money moves away from them.
|
||||
*
|
||||
* An expense account doesn't really spend money itself. Money is spent "at" the expense account.
|
||||
* So, the call to find out how many money has been spent on/at an expense account is slightly different.
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param Account $account
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function spentInPeriod(Account $account, Carbon $start, Carbon $end): string
|
||||
{
|
||||
/** @var ARI $repository */
|
||||
$repository = app(ARI::class);
|
||||
$collection = new Collection([$account]);
|
||||
$type = $account->accountType->type;
|
||||
switch ($type) {
|
||||
case AccountType::DEFAULT:
|
||||
case AccountType::ASSET:
|
||||
return $repository->spentInPeriod($collection, $start, $end);
|
||||
case AccountType::EXPENSE:
|
||||
return $repository->spentAtInPeriod($collection, $start, $end);
|
||||
default:
|
||||
return '0';
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -60,6 +60,8 @@ class ConfigurationController extends Controller
|
||||
|
||||
/**
|
||||
* @param ConfigurationRequest $request
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function store(ConfigurationRequest $request)
|
||||
{
|
||||
|
@ -112,6 +112,8 @@ class LoginController extends Controller
|
||||
/**
|
||||
* Show the application login form.
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function showLoginForm(Request $request)
|
||||
|
@ -117,6 +117,8 @@ class RegisterController extends Controller
|
||||
* OLD
|
||||
* Show the application registration form.
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function showRegistrationForm(Request $request)
|
||||
|
@ -139,6 +139,8 @@ class BudgetController extends Controller
|
||||
/**
|
||||
* Shows a budget list with spent/left/overspent.
|
||||
*
|
||||
* @param BudgetRepositoryInterface $repository
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
public function frontpage(BudgetRepositoryInterface $repository)
|
||||
|
@ -17,7 +17,7 @@ namespace FireflyIII\Http\Controllers\Chart;
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Generator\Chart\Report\ReportChartGeneratorInterface;
|
||||
use FireflyIII\Http\Controllers\Controller;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Repositories\Account\AccountTaskerInterface;
|
||||
use FireflyIII\Support\CacheProperties;
|
||||
use Illuminate\Support\Collection;
|
||||
use Navigation;
|
||||
@ -92,15 +92,16 @@ class ReportController extends Controller
|
||||
|
||||
|
||||
/**
|
||||
* @param AccountRepositoryInterface $repository
|
||||
* @param string $reportType
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Collection $accounts
|
||||
* @param AccountTaskerInterface $accountTasker
|
||||
* @param string $reportType
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Collection $accounts
|
||||
*
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
* @internal param AccountRepositoryInterface $repository
|
||||
*/
|
||||
public function yearInOut(AccountRepositoryInterface $repository, string $reportType, Carbon $start, Carbon $end, Collection $accounts)
|
||||
public function yearInOut(AccountTaskerInterface $accountTasker, string $reportType, Carbon $start, Carbon $end, Collection $accounts)
|
||||
{
|
||||
// chart properties for cache:
|
||||
$cache = new CacheProperties;
|
||||
@ -120,8 +121,8 @@ class ReportController extends Controller
|
||||
while ($currentStart <= $end) {
|
||||
$currentEnd = Navigation::endOfPeriod($currentStart, '1M');
|
||||
$date = $currentStart->format('Y-m');
|
||||
$spent = $repository->spentInPeriod($accounts, $currentStart, $currentEnd);
|
||||
$earned = $repository->earnedInPeriod($accounts, $currentStart, $currentEnd);
|
||||
$spent = $accountTasker->amountOutInPeriod($accounts, $accounts, $currentStart, $currentEnd);
|
||||
$earned = $accountTasker->amountInInPeriod($accounts, $accounts, $currentStart, $currentEnd);
|
||||
$spentArray[$date] = bcmul($spent, '-1');
|
||||
$earnedArray[$date] = $earned;
|
||||
$currentStart = Navigation::addPeriod($currentStart, '1M', 0);
|
||||
@ -145,15 +146,16 @@ class ReportController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AccountRepositoryInterface $repository
|
||||
* @param string $reportType
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Collection $accounts
|
||||
* @param AccountTaskerInterface $accountTasker
|
||||
* @param string $reportType
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Collection $accounts
|
||||
*
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
* @internal param AccountRepositoryInterface $repository
|
||||
*/
|
||||
public function yearInOutSummarized(AccountRepositoryInterface $repository, string $reportType, Carbon $start, Carbon $end, Collection $accounts)
|
||||
public function yearInOutSummarized(AccountTaskerInterface $accountTasker, string $reportType, Carbon $start, Carbon $end, Collection $accounts)
|
||||
{
|
||||
|
||||
// chart properties for cache:
|
||||
@ -174,8 +176,8 @@ class ReportController extends Controller
|
||||
while ($currentStart <= $end) {
|
||||
$currentEnd = Navigation::endOfPeriod($currentStart, '1M');
|
||||
$date = $currentStart->format('Y-m');
|
||||
$spent = $repository->spentInPeriod($accounts, $currentStart, $currentEnd);
|
||||
$earned = $repository->earnedInPeriod($accounts, $currentStart, $currentEnd);
|
||||
$spent = $accountTasker->amountOutInPeriod($accounts, $accounts, $currentStart, $currentEnd);
|
||||
$earned = $accountTasker->amountInInPeriod($accounts, $accounts, $currentStart, $currentEnd);
|
||||
$spentArray[$date] = bcmul($spent, '-1');
|
||||
$earnedArray[$date] = $earned;
|
||||
$currentStart = Navigation::addPeriod($currentStart, '1M', 0);
|
||||
|
@ -17,7 +17,7 @@ use Carbon\Carbon;
|
||||
use FireflyIII\Crud\Account\AccountCrudInterface;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
|
||||
use FireflyIII\Repositories\Account\AccountTaskerInterface;
|
||||
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
|
||||
use FireflyIII\Repositories\Category\CategoryRepositoryInterface as CRI;
|
||||
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
||||
@ -97,12 +97,13 @@ class JsonController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ARI $accountRepository
|
||||
* @param AccountCrudInterface $crud
|
||||
* @param AccountTaskerInterface $accountTasker
|
||||
* @param AccountCrudInterface $crud
|
||||
*
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
* @internal param ARI $accountRepository
|
||||
*/
|
||||
public function boxIn(ARI $accountRepository, AccountCrudInterface $crud)
|
||||
public function boxIn(AccountTaskerInterface $accountTasker, AccountCrudInterface $crud)
|
||||
{
|
||||
$start = session('start', Carbon::now()->startOfMonth());
|
||||
$end = session('end', Carbon::now()->endOfMonth());
|
||||
@ -116,7 +117,8 @@ class JsonController extends Controller
|
||||
return Response::json($cache->get());
|
||||
}
|
||||
$accounts = $crud->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET, AccountType::CASH]);
|
||||
$amount = $accountRepository->earnedInPeriod($accounts, $start, $end);
|
||||
$assets = $crud->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
|
||||
$amount = $accountTasker->amountInInPeriod($accounts, $assets, $start, $end);
|
||||
$data = ['box' => 'in', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount];
|
||||
$cache->store($data);
|
||||
|
||||
@ -124,12 +126,13 @@ class JsonController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ARI $accountRepository
|
||||
* @param AccountCrudInterface $crud
|
||||
* @param AccountCrudInterface $crud
|
||||
* @param AccountTaskerInterface $accountTasker
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
* @internal param ARI $accountRepository
|
||||
*/
|
||||
public function boxOut(ARI $accountRepository, AccountCrudInterface $crud)
|
||||
public function boxOut(AccountCrudInterface $crud, AccountTaskerInterface $accountTasker)
|
||||
{
|
||||
$start = session('start', Carbon::now()->startOfMonth());
|
||||
$end = session('end', Carbon::now()->endOfMonth());
|
||||
@ -144,7 +147,8 @@ class JsonController extends Controller
|
||||
}
|
||||
|
||||
$accounts = $crud->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET, AccountType::CASH]);
|
||||
$amount = $accountRepository->spentInPeriod($accounts, $start, $end);
|
||||
$assets = $crud->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
|
||||
$amount = $accountTasker->amountOutInPeriod($accounts, $assets, $start, $end);
|
||||
|
||||
$data = ['box' => 'out', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount];
|
||||
$cache->store($data);
|
||||
|
@ -15,7 +15,6 @@ namespace FireflyIII\Http\Controllers;
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Crud\Account\AccountCrudInterface;
|
||||
use FireflyIII\Http\Requests\NewUserFormRequest;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
|
||||
use Preferences;
|
||||
use Session;
|
||||
use View;
|
||||
|
@ -99,6 +99,7 @@ class CsvImporter implements ImporterInterface
|
||||
* @param array $row
|
||||
*
|
||||
* @return ImportEntry
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function importSingleRow(int $index, array $row): ImportEntry
|
||||
{
|
||||
|
@ -63,7 +63,7 @@ class IngDescription implements SpecificInterface
|
||||
$this->removeIBANIngDescription();
|
||||
$this->removeNameIngDescription();
|
||||
break;
|
||||
case 'BA' : // Betaalautomaat
|
||||
case 'BA': // Betaalautomaat
|
||||
$this->addNameIngDescription();
|
||||
break;
|
||||
}
|
||||
@ -80,7 +80,7 @@ class IngDescription implements SpecificInterface
|
||||
*/
|
||||
protected function addNameIngDescription()
|
||||
{
|
||||
$this->row[8] = $this->row[1] . " " . $this->row[8];
|
||||
$this->row[8] = $this->row[1] . ' ' . $this->row[8];
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -23,7 +23,6 @@ use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Query\JoinClause;
|
||||
use Illuminate\Support\Collection;
|
||||
use Steam;
|
||||
@ -51,108 +50,6 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is almost the same as ::earnedInPeriod, but only works for revenue accounts
|
||||
* instead of the implied asset accounts for ::earnedInPeriod. ::earnedInPeriod will tell you
|
||||
* how much money was earned by the given asset accounts. This method will tell you how much money
|
||||
* these given revenue accounts sent. Ie. how much money was made FROM these revenue accounts.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function earnedFromInPeriod(Collection $accounts, Carbon $start, Carbon $end): string
|
||||
{
|
||||
$query = $this->user->transactionJournals()->expanded()->sortCorrectly()
|
||||
->transactionTypes([TransactionType::DEPOSIT]);
|
||||
|
||||
if ($end >= $start) {
|
||||
$query->before($end)->after($start);
|
||||
}
|
||||
|
||||
if ($accounts->count() > 0) {
|
||||
$accountIds = $accounts->pluck('id')->toArray();
|
||||
$query->leftJoin(
|
||||
'transactions as source', function (JoinClause $join) {
|
||||
$join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('source.amount', '<', 0);
|
||||
}
|
||||
);
|
||||
$query->whereIn('source.account_id', $accountIds);
|
||||
$query->whereNull('source.deleted_at');
|
||||
|
||||
}
|
||||
// remove group by
|
||||
$query->getQuery()->getQuery()->groups = null;
|
||||
|
||||
// get id's
|
||||
$ids = $query->get(['transaction_journals.id'])->pluck('id')->toArray();
|
||||
|
||||
// that should do it:
|
||||
$sum = $this->user->transactions()
|
||||
->whereIn('transaction_journal_id', $ids)
|
||||
->where('amount', '>', '0')
|
||||
->whereNull('transactions.deleted_at')
|
||||
->sum('amount');
|
||||
|
||||
return strval($sum);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function earnedInPeriod(Collection $accounts, Carbon $start, Carbon $end): string
|
||||
{
|
||||
$query = $this->user->transactionJournals()->expanded()->sortCorrectly()
|
||||
->transactionTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER]);
|
||||
|
||||
if ($end >= $start) {
|
||||
$query->before($end)->after($start);
|
||||
}
|
||||
|
||||
if ($accounts->count() > 0) {
|
||||
$accountIds = $accounts->pluck('id')->toArray();
|
||||
$query->leftJoin(
|
||||
'transactions as destination', function (JoinClause $join) {
|
||||
$join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')->where('destination.amount', '>', 0);
|
||||
}
|
||||
);
|
||||
$query->leftJoin(
|
||||
'transactions as source', function (JoinClause $join) {
|
||||
$join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('source.amount', '<', 0);
|
||||
}
|
||||
);
|
||||
|
||||
$query->whereIn('destination.account_id', $accountIds);
|
||||
$query->whereNotIn('source.account_id', $accountIds);
|
||||
$query->whereNull('destination.deleted_at');
|
||||
$query->whereNull('source.deleted_at');
|
||||
|
||||
}
|
||||
// remove group by
|
||||
$query->getQuery()->getQuery()->groups = null;
|
||||
|
||||
// get id's
|
||||
$ids = $query->get(['transaction_journals.id'])->pluck('id')->toArray();
|
||||
|
||||
|
||||
// that should do it:
|
||||
$sum = $this->user->transactions()
|
||||
->whereIn('transaction_journal_id', $ids)
|
||||
->where('amount', '>', '0')
|
||||
->whereNull('transactions.deleted_at')
|
||||
->sum('amount');
|
||||
|
||||
return strval($sum);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will call AccountRepositoryInterface::journalsInPeriod and get all withdrawaks made from the given $accounts,
|
||||
* as well as the transfers that move away from those $accounts. This is a slightly sharper selection
|
||||
@ -514,98 +411,4 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
|
||||
return $journal;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is almost the same as ::spentInPeriod, but only works for expense accounts
|
||||
* instead of the implied asset accounts for ::spentInPeriod. ::spentInPeriod will tell you
|
||||
* how much money was spent by the given asset accounts. This method will tell you how much money
|
||||
* these given expense accounts received. Ie. how much money was spent AT these expense accounts.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function spentAtInPeriod(Collection $accounts, Carbon $start, Carbon $end): string
|
||||
{
|
||||
/** @var HasMany $query */
|
||||
$query = $this->user->transactionJournals()->expanded()
|
||||
->transactionTypes([TransactionType::WITHDRAWAL]);
|
||||
if ($end >= $start) {
|
||||
$query->before($end)->after($start);
|
||||
}
|
||||
|
||||
if ($accounts->count() > 0) {
|
||||
$accountIds = $accounts->pluck('id')->toArray();
|
||||
$query->leftJoin(
|
||||
'transactions as destination', function (JoinClause $join) {
|
||||
$join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')->where('destination.amount', '>', 0);
|
||||
}
|
||||
);
|
||||
$query->whereIn('destination.account_id', $accountIds);
|
||||
|
||||
}
|
||||
// remove group by
|
||||
$query->getQuery()->getQuery()->groups = null;
|
||||
|
||||
// that should do it:
|
||||
$sum = strval($query->sum('destination.amount'));
|
||||
if (is_null($sum)) {
|
||||
$sum = '0';
|
||||
}
|
||||
$sum = bcmul($sum, '-1');
|
||||
|
||||
return $sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function spentInPeriod(Collection $accounts, Carbon $start, Carbon $end): string
|
||||
{
|
||||
/** @var HasMany $query */
|
||||
$query = $this->user->transactionJournals()->expanded()
|
||||
->transactionTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER]);
|
||||
if ($end >= $start) {
|
||||
$query->before($end)->after($start);
|
||||
}
|
||||
|
||||
if ($accounts->count() > 0) {
|
||||
$accountIds = $accounts->pluck('id')->toArray();
|
||||
$query->leftJoin(
|
||||
'transactions as source', function (JoinClause $join) {
|
||||
$join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('source.amount', '<', 0);
|
||||
}
|
||||
);
|
||||
|
||||
$query->leftJoin(
|
||||
'transactions as destination', function (JoinClause $join) {
|
||||
$join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')->where('destination.amount', '>', 0);
|
||||
}
|
||||
);
|
||||
$query->whereIn('source.account_id', $accountIds);
|
||||
$query->whereNotIn('destination.account_id', $accountIds);
|
||||
$query->whereNull('source.deleted_at');
|
||||
$query->whereNull('destination.deleted_at');
|
||||
$query->distinct();
|
||||
|
||||
}
|
||||
// remove group by
|
||||
$query->getQuery()->getQuery()->groups = null;
|
||||
$ids = $query->get(['transaction_journals.id'])->pluck('id')->toArray();
|
||||
|
||||
$sum = $this->user->transactions()
|
||||
->whereIn('transaction_journal_id', $ids)
|
||||
->where('amount', '<', '0')
|
||||
->whereNull('transactions.deleted_at')
|
||||
->sum('amount');
|
||||
|
||||
return strval($sum);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -41,15 +41,6 @@ interface AccountRepositoryInterface
|
||||
*/
|
||||
public function earnedFromInPeriod(Collection $accounts, Carbon $start, Carbon $end): string;
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function earnedInPeriod(Collection $accounts, Carbon $start, Carbon $end): string;
|
||||
|
||||
/**
|
||||
* This method will call AccountRepositoryInterface::journalsInPeriod and get all withdrawaks made from the given $accounts,
|
||||
* as well as the transfers that move away from those $accounts. This is a slightly sharper selection
|
||||
@ -160,27 +151,4 @@ interface AccountRepositoryInterface
|
||||
*/
|
||||
public function openingBalanceTransaction(Account $account) : TransactionJournal;
|
||||
|
||||
/**
|
||||
* This method is almost the same as ::spentInPeriod, but only works for expense accounts
|
||||
* instead of the implied asset accounts for ::spentInPeriod. ::spentInPeriod will tell you
|
||||
* how much money was spent by the given asset accounts. This method will tell you how much money
|
||||
* these given expense accounts received. Ie. how much money was spent AT these expense accounts.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function spentAtInPeriod(Collection $accounts, Carbon $start, Carbon $end): string;
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function spentInPeriod(Collection $accounts, Carbon $start, Carbon $end): string;
|
||||
|
||||
}
|
||||
|
@ -14,10 +14,12 @@ declare(strict_types = 1);
|
||||
namespace FireflyIII\Repositories\Account;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Collection;
|
||||
use FireflyIII\Helpers\Collection\Account as AccountCollection;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Database\Query\JoinClause;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
use Steam;
|
||||
|
||||
@ -26,7 +28,7 @@ use Steam;
|
||||
*
|
||||
* @package FireflyIII\Repositories\Account
|
||||
*/
|
||||
class AccountTasker
|
||||
class AccountTasker implements AccountTaskerInterface
|
||||
{
|
||||
/** @var User */
|
||||
private $user;
|
||||
@ -41,6 +43,64 @@ class AccountTasker
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see self::amountInPeriod
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $excluded
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function amountInInPeriod(Collection $accounts, Collection $excluded, Carbon $start, Carbon $end): string
|
||||
{
|
||||
$idList = [
|
||||
'accounts' => $accounts->pluck('id')->toArray(),
|
||||
'exclude' => $excluded->pluck('id')->toArray(),
|
||||
];
|
||||
|
||||
Log::debug(
|
||||
'Now calling amountInInPeriod.',
|
||||
['accounts' => $idList['accounts'], 'excluded' => $idList['exclude'],
|
||||
'start' => $start->format('Y-m-d'),
|
||||
'end' => $end->format('Y-m-d'),
|
||||
]
|
||||
);
|
||||
|
||||
return $this->amountInPeriod($idList, $start, $end, true);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @see self::amountInPeriod
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $excluded
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function amountOutInPeriod(Collection $accounts, Collection $excluded, Carbon $start, Carbon $end): string
|
||||
{
|
||||
$idList = [
|
||||
'accounts' => $accounts->pluck('id')->toArray(),
|
||||
'exclude' => $excluded->pluck('id')->toArray(),
|
||||
];
|
||||
|
||||
Log::debug(
|
||||
'Now calling amountOutInPeriod.',
|
||||
['accounts' => $idList['accounts'], 'excluded' => $idList['exclude'],
|
||||
'start' => $start->format('Y-m-d'),
|
||||
'end' => $end->format('Y-m-d'),
|
||||
]
|
||||
);
|
||||
|
||||
return $this->amountInPeriod($idList, $start, $end, false);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
@ -96,4 +156,62 @@ class AccountTasker
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Will return how much money has been going out (ie. spent) by the given account(s).
|
||||
* Alternatively, will return how much money has been coming in (ie. earned) by the given accounts.
|
||||
*
|
||||
* Enter $incoming=true for any money coming in (income)
|
||||
* Enter $incoming=false for any money going out (expenses)
|
||||
*
|
||||
* This means any money going out or in. You can also submit accounts to exclude,
|
||||
* so transfers between accounts are not included.
|
||||
*
|
||||
* As a general rule:
|
||||
*
|
||||
* - Asset accounts should return both expenses and earnings. But could return 0.
|
||||
* - Expense accounts (where money is spent) should only return earnings (the account gets money).
|
||||
* - Revenue accounts (where money comes from) should only return expenses (they spend money).
|
||||
*
|
||||
* @param array $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param bool $incoming
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function amountInPeriod(array $accounts, Carbon $start, Carbon $end, bool $incoming): string
|
||||
{
|
||||
$joinModifier = $incoming ? '<' : '>';
|
||||
$selection = $incoming ? '>' : '<';
|
||||
|
||||
$query = Transaction
|
||||
::distinct()
|
||||
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
||||
->leftJoin(
|
||||
'transactions as other_side', function (JoinClause $join) use ($joinModifier) {
|
||||
$join->on('transaction_journals.id', '=', 'other_side.transaction_journal_id')->where('other_side.amount', $joinModifier, 0);
|
||||
}
|
||||
)
|
||||
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
||||
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
|
||||
->where('transaction_journals.user_id', $this->user->id)
|
||||
->whereNull('transactions.deleted_at')
|
||||
->whereNull('transaction_journals.deleted_at')
|
||||
->whereIn('transactions.account_id', $accounts['accounts'])
|
||||
->where('transactions.amount', $selection, 0);
|
||||
if (count($accounts['exclude']) > 0) {
|
||||
$query->whereNotIn('other_side.account_id', $accounts['exclude']);
|
||||
}
|
||||
|
||||
$result = $query->get(['transactions.id', 'transactions.amount']);
|
||||
$sum = strval($result->sum('amount'));
|
||||
if (strlen($sum) === 0) {
|
||||
Log::debug('Sum is empty.');
|
||||
$sum = '0';
|
||||
}
|
||||
Log::debug(sprintf('Result is %s', $sum));
|
||||
|
||||
return $sum;
|
||||
}
|
||||
}
|
@ -25,6 +25,30 @@ use Illuminate\Support\Collection;
|
||||
interface AccountTaskerInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Collection $excluded
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @see AccountTasker::amountInPeriod()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function amountInInPeriod(Collection $accounts, Collection $excluded, Carbon $start, Carbon $end): string;
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Collection $excluded
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @see AccountTasker::amountInPeriod()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function amountOutInPeriod(Collection $accounts, Collection $excluded, Carbon $start, Carbon $end): string;
|
||||
|
||||
/**
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
|
@ -42,6 +42,7 @@ class ImportJobRepository implements ImportJobRepositoryInterface
|
||||
* @param string $fileType
|
||||
*
|
||||
* @return ImportJob
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function create(string $fileType): ImportJob
|
||||
{
|
||||
|
@ -15,7 +15,6 @@ namespace FireflyIII\Support;
|
||||
|
||||
use Cache;
|
||||
use FireflyIII\Models\Configuration;
|
||||
use FireflyIII\Models\Preference;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
|
@ -8,8 +8,6 @@
|
||||
*/
|
||||
declare(strict_types = 1);
|
||||
|
||||
use Illuminate\Foundation\Inspiring;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Console Routes
|
||||
@ -20,7 +18,3 @@ use Illuminate\Foundation\Inspiring;
|
||||
| simple approach to interacting with each command's IO methods.
|
||||
|
|
||||
*/
|
||||
|
||||
Artisan::command('inspire', function () {
|
||||
$this->comment(Inspiring::quote());
|
||||
});
|
||||
|
@ -4,6 +4,9 @@ use Illuminate\Foundation\Testing\WithoutMiddleware;
|
||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
/**
|
||||
* Class ExampleTest
|
||||
*/
|
||||
class ExampleTest extends TestCase
|
||||
{
|
||||
/**
|
||||
|
@ -1,5 +1,8 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class TestCase
|
||||
*/
|
||||
abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
|
||||
{
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user