Files
firefly-iii/app/lib/Firefly/Helper/Controllers/Account.php

143 lines
5.2 KiB
PHP
Raw Normal View History

<?php
namespace Firefly\Helper\Controllers;
/**
* Class Account
*
* @package Firefly\Helper\Controllers
*/
class Account implements AccountInterface
{
/**
* @param \Account $account
* @return \TransactionJournal|null
*/
public function openingBalanceTransaction(\Account $account)
{
return \TransactionJournal::withRelevantData()
->accountIs($account)
2014-08-30 14:26:33 +02:00
->leftJoin('transaction_types', 'transaction_types.id', '=',
'transaction_journals.transaction_type_id')
->where('transaction_types.type', 'Opening balance')
->first(['transaction_journals.*']);
}
2014-07-27 20:29:58 +02:00
/**
* Since it is entirely possible the database is messed up somehow it might be that a transaction
* journal has only one transaction. This is mainly caused by wrong deletions and other artefacts from the past.
2014-07-27 20:29:58 +02:00
*
2014-09-09 20:01:13 +02:00
* If it is the case, Firefly removes $item and continues like nothing ever happened. This will however,
* mess up some statisics but it's decided everybody should learn to live with that.
*
2014-09-09 20:01:13 +02:00
* Firefly might be needing some cleanup routine in the future.
*
* For now, Firefly simply warns the user of this.
*
* @param \Account $account
* @param $perPage
2014-09-09 20:01:13 +02:00
*
* @return array|mixed
* @throws \Firefly\Exception\FireflyException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2014-07-27 20:29:58 +02:00
*/
public function show(\Account $account, $perPage)
{
$start = \Session::get('start');
2014-08-30 14:26:33 +02:00
$end = \Session::get('end');
2014-07-27 20:29:58 +02:00
$stats = [
'accounts' => []
2014-07-27 20:29:58 +02:00
];
$items = [];
// build a query:
$query = \TransactionJournal::withRelevantData()
->defaultSorting()
->accountIs($account)
->after($start)
2014-08-30 14:26:33 +02:00
->before($end);
// filter some:
switch (\Input::get('type')) {
case 'transactions':
$query->transactionTypes(['Deposit', 'Withdrawal']);
break;
case 'transfers':
$query->transactionTypes(['Transfer']);
break;
2014-08-30 14:26:33 +02:00
}
switch (\Input::get('show')) {
case 'expenses':
case 'out':
$query->lessThan(0);
break;
case 'income':
case 'in':
$query->moreThan(0);
break;
2014-08-30 14:26:33 +02:00
}
2014-07-27 20:29:58 +02:00
// build paginator:
$totalItems = $query->count();
2014-08-30 14:26:33 +02:00
$page = max(1, intval(\Input::get('page')));
$skip = ($page - 1) * $perPage;
$result = $query->skip($skip)->take($perPage)->get(['transaction_journals.*']);
2014-07-27 20:29:58 +02:00
2014-08-30 14:26:33 +02:00
// get the relevant budgets, categories and accounts from this list:
2014-07-27 20:29:58 +02:00
/** @var $item \TransactionJournal */
2014-08-30 14:26:33 +02:00
foreach ($result as $index => $item) {
2014-07-27 20:29:58 +02:00
foreach ($item->components as $component) {
$stats[$component->class][$component->id] = $component;
2014-07-27 20:29:58 +02:00
}
2014-08-30 14:26:33 +02:00
if (count($item->transactions) < 2) {
\Session::flash('warning', 'Some transactions are incomplete; they will not be shown.');
2014-08-30 14:26:33 +02:00
unset($result[$index]);
continue;
}
$items[] = $item;
$fromAccount = $item->transactions[0]->account;
$toAccount = $item->transactions[1]->account;
2014-07-27 20:29:58 +02:00
$stats['accounts'][$fromAccount->id] = $fromAccount;
2014-08-30 14:26:33 +02:00
$stats['accounts'][$toAccount->id] = $toAccount;
2014-07-27 20:29:58 +02:00
}
$paginator = \Paginator::make($items, $totalItems, $perPage);
2014-08-30 14:26:33 +02:00
unset($result, $page, $item, $fromAccount, $toAccount);
2014-07-27 20:29:58 +02:00
2014-08-30 14:26:33 +02:00
// statistics (transactions)
$trIn = floatval(\Transaction::before($end)->after($start)->accountIs($account)->moreThan(0)
2014-08-30 14:26:33 +02:00
->transactionTypes(['Deposit', 'Withdrawal'])->sum('transactions.amount'));
$trOut = floatval(\Transaction::before($end)->after($start)->accountIs($account)->lessThan(0)
2014-08-30 14:26:33 +02:00
->transactionTypes(['Deposit', 'Withdrawal'])->sum('transactions.amount'));
$trDiff = $trIn + $trOut;
// statistics (transfers)
$trfIn = floatval(\Transaction::before($end)->after($start)->accountIs($account)->moreThan(0)
2014-08-30 14:26:33 +02:00
->transactionTypes(['Transfer'])->sum('transactions.amount'));
$trfOut = floatval(\Transaction::before($end)->after($start)->accountIs($account)->lessThan(0)
2014-08-30 14:26:33 +02:00
->transactionTypes(['Transfer'])->sum('transactions.amount'));
$trfDiff = $trfIn + $trfOut;
$stats['period'] = [
'in' => $trIn,
'out' => $trOut,
'diff' => $trDiff,
't_in' => $trfIn,
't_out' => $trfOut,
't_diff' => $trfDiff
];
2014-07-27 20:29:58 +02:00
$return = [
'journals' => $paginator,
'statistics' => $stats
];
return $return;
}
}