Fixed some issues with the monthly report and missing amounts.

This commit is contained in:
James Cole
2017-01-19 21:54:27 +01:00
parent 20bb151cf3
commit be868d37f2
7 changed files with 88 additions and 164 deletions

View File

@@ -1,107 +0,0 @@
<?php
/**
* Account.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types = 1);
namespace FireflyIII\Helpers\Collection;
use Illuminate\Support\Collection;
/**
* Class Account
*
* @package FireflyIII\Helpers\Collection
*/
class Account
{
/** @var Collection */
protected $accounts;
/** @var string */
protected $difference = '';
/** @var string */
protected $end = '';
/** @var string */
protected $start = '';
/**
* Account constructor.
*/
public function __construct()
{
$this->accounts = new Collection;
}
/**
* @return Collection
*/
public function getAccounts(): Collection
{
return $this->accounts;
}
/**
* @param Collection $accounts
*/
public function setAccounts(Collection $accounts)
{
$this->accounts = $accounts;
}
/**
* @return string
*/
public function getDifference(): string
{
return $this->difference;
}
/**
* @param string $difference
*/
public function setDifference(string $difference)
{
$this->difference = $difference;
}
/**
* @return string
*/
public function getEnd(): string
{
return $this->end;
}
/**
* @param string $end
*/
public function setEnd(string $end)
{
$this->end = $end;
}
/**
* @return string
*/
public function getStart(): string
{
return $this->start;
}
/**
* @param string $start
*/
public function setStart(string $start)
{
$this->start = $start;
}
}

View File

@@ -283,6 +283,29 @@ class AccountRepository implements AccountRepositoryInterface
return $last; return $last;
} }
/**
* Returns the date of the very first transaction in this account.
*
* @param Account $account
*
* @return TransactionJournal
*/
public function oldestJournal(Account $account): TransactionJournal
{
$first = $account->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->orderBy('transaction_journals.date', 'ASC')
->orderBy('transaction_journals.order', 'DESC')
->where('transaction_journals.user_id', $this->user->id)
->orderBy('transaction_journals.id', 'ASC')
->first(['transaction_journals.id']);
if (!is_null($first)) {
return TransactionJournal::find(intval($first->id));
}
return new TransactionJournal();
}
/** /**
* Returns the date of the very first transaction in this account. * Returns the date of the very first transaction in this account.
* *
@@ -292,18 +315,12 @@ class AccountRepository implements AccountRepositoryInterface
*/ */
public function oldestJournalDate(Account $account): Carbon public function oldestJournalDate(Account $account): Carbon
{ {
$first = new Carbon; $journal = $this->oldestJournal($account);
$date = $account->transactions() if (is_null($journal->id)) {
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') return new Carbon;
->orderBy('transaction_journals.date', 'ASC')
->orderBy('transaction_journals.order', 'DESC')
->orderBy('transaction_journals.id', 'ASC')
->first(['transaction_journals.date']);
if (!is_null($date)) {
$first = new Carbon($date->date);
} }
return $first; return $journal->date;
} }
/** /**

View File

@@ -15,6 +15,7 @@ namespace FireflyIII\Repositories\Account;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\TransactionJournal;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
/** /**
@@ -105,6 +106,15 @@ interface AccountRepositoryInterface
*/ */
public function newestJournalDate(Account $account): Carbon; public function newestJournalDate(Account $account): Carbon;
/**
* Returns the date of the very first transaction in this account.
*
* @param Account $account
*
* @return TransactionJournal
*/
public function oldestJournal(Account $account): TransactionJournal;
/** /**
* Returns the date of the very first transaction in this account. * Returns the date of the very first transaction in this account.
* *

View File

@@ -14,8 +14,6 @@ declare(strict_types = 1);
namespace FireflyIII\Repositories\Account; namespace FireflyIII\Repositories\Account;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Helpers\Collection\Account as AccountCollection;
use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction; use FireflyIII\Models\Transaction;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Database\Query\JoinClause; use Illuminate\Database\Query\JoinClause;
@@ -106,9 +104,9 @@ class AccountTasker implements AccountTaskerInterface
* @param Carbon $start * @param Carbon $start
* @param Carbon $end * @param Carbon $end
* *
* @return AccountCollection * @return array
*/ */
public function getAccountReport(Collection $accounts, Carbon $start, Carbon $end): AccountCollection public function getAccountReport(Collection $accounts, Carbon $start, Carbon $end): array
{ {
$startAmount = '0'; $startAmount = '0';
$endAmount = '0'; $endAmount = '0';
@@ -117,44 +115,51 @@ class AccountTasker implements AccountTaskerInterface
$yesterday = clone $start; $yesterday = clone $start;
$yesterday->subDay(); $yesterday->subDay();
$startSet = Steam::balancesById($ids, $yesterday); $startSet = Steam::balancesById($ids, $yesterday);
$backupSet = Steam::balancesById($ids, $start);
$endSet = Steam::balancesById($ids, $end); $endSet = Steam::balancesById($ids, $end);
Log::debug( Log::debug('Start of accountreport');
sprintf(
'getAccountReport from %s to %s for %d accounts.',
$start->format('Y-m-d'),
$end->format('Y-m-d'),
$accounts->count()
)
);
$accounts->each(
function (Account $account) use ($startSet, $endSet, $backupSet) {
$account->startBalance = $startSet[$account->id] ?? '0';
$account->endBalance = $endSet[$account->id] ?? '0';
// check backup set just in case: /** @var AccountRepositoryInterface $repository */
if ($account->startBalance === '0' && isset($backupSet[$account->id])) { $repository = app(AccountRepositoryInterface::class);
$account->startBalance = $backupSet[$account->id];
} $return = [
} 'start' => '0',
); 'end' => '0',
'difference' => '0',
'accounts' => [],
];
// summarize:
foreach ($accounts as $account) { foreach ($accounts as $account) {
$startAmount = bcadd($startAmount, $account->startBalance); $id = $account->id;
$endAmount = bcadd($endAmount, $account->endBalance); $entry = [
$diff = bcadd($diff, bcsub($account->endBalance, $account->startBalance)); 'name' => $account->name,
'id' => $account->id,
'start_balance' => '0',
'end_balance' => '0',
];
// get first journal date:
$first = $repository->oldestJournal($account);
$entry['start_balance'] = $startSet[$account->id] ?? '0';
$entry['end_balance'] = $endSet[$account->id] ?? '0';
if (!is_null($first->id) && $yesterday < $first->date && $end >= $first->date) {
// something about balance?
$entry['start_balance'] = $first->transactions()->where('account_id', $account->id)->first()->amount;
Log::debug(sprintf('Account was opened before %s, so opening balance is %f', $yesterday->format('Y-m-d'), $entry['start_balance']));
}
$return['start'] = bcadd($return['start'], $entry['start_balance']);
$return['end'] = bcadd($return['end'], $entry['end_balance']);
$return['accounts'][$id] = $entry;
} }
$object = new AccountCollection; foreach ($accounts as $account) {
$object->setStart($startAmount); $id = $account->id;
$object->setEnd($endAmount); $diff = bcadd($diff, bcsub($return['accounts'][$id]['end_balance'], $return['accounts'][$id]['start_balance']));
$object->setDifference($diff); }
$object->setAccounts($accounts); $return['difference'] = bcsub($return['end'], $return['start']);
return $return;
return $object;
} }
/** /**

View File

@@ -14,7 +14,6 @@ declare(strict_types = 1);
namespace FireflyIII\Repositories\Account; namespace FireflyIII\Repositories\Account;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Helpers\Collection\Account as AccountCollection;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
/** /**
@@ -54,8 +53,8 @@ interface AccountTaskerInterface
* @param Carbon $start * @param Carbon $start
* @param Carbon $end * @param Carbon $end
* *
* @return AccountCollection * @return array
*/ */
public function getAccountReport(Collection $accounts, Carbon $start, Carbon $end): AccountCollection; public function getAccountReport(Collection $accounts, Carbon $start, Carbon $end): array;
} }

View File

@@ -152,7 +152,7 @@ class Steam
public function balancesById(array $ids, Carbon $date): array public function balancesById(array $ids, Carbon $date): array
{ {
// abuse chart properties: // cache this property.
$cache = new CacheProperties; $cache = new CacheProperties;
$cache->addProperty($ids); $cache->addProperty($ids);
$cache->addProperty('balances'); $cache->addProperty('balances');

View File

@@ -8,23 +8,23 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for account in accountReport.getAccounts %} {% for account in accountReport.accounts %}
<tr> <tr>
<td data-value="{{ account.name }}"> <td data-value="{{ account.name }}">
<a href="{{ route('accounts.show',account.id) }}" title="{{ account.name }}">{{ account.name }}</a> <a href="{{ route('accounts.show',account.id) }}" title="{{ account.name }}">{{ account.name }}</a>
</td> </td>
<td class="hidden-xs" data-value="{{ account.startBalance }}" style="text-align: right;">{{ account.startBalance|formatAmount }}</td> <td class="hidden-xs" data-value="{{ account.start_balance }}" style="text-align: right;">{{ account.start_balance|formatAmount }}</td>
<td class="hidden-xs" data-value="{{ account.endBalance }}" style="text-align: right;">{{ account.endBalance|formatAmount }}</td> <td class="hidden-xs" data-value="{{ account.end_balance }}" style="text-align: right;">{{ account.end_balance|formatAmount }}</td>
<td style="text-align: right;" data-value="{{ (account.endBalance - account.startBalance) }}">{{ (account.endBalance - account.startBalance)|formatAmount }}</td> <td style="text-align: right;" data-value="{{ (account.end_balance - account.start_balance) }}">{{ (account.end_balance - account.start_balance)|formatAmount }}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
<tfoot> <tfoot>
<tr> <tr>
<td><em>{{ 'sumOfSums'|_ }}</em></td> <td><em>{{ 'sumOfSums'|_ }}</em></td>
<td class="hidden-xs" style="text-align: right;">{{ accountReport.getStart|formatAmount }}</td> <td class="hidden-xs" style="text-align: right;">{{ accountReport.start|formatAmount }}</td>
<td class="hidden-xs" style="text-align: right;">{{ accountReport.getEnd|formatAmount }}</td> <td class="hidden-xs" style="text-align: right;">{{ accountReport.end|formatAmount }}</td>
<td style="text-align: right;">{{ accountReport.getDifference|formatAmount }}</td> <td style="text-align: right;">{{ accountReport.difference|formatAmount }}</td>
</tr> </tr>
</tfoot> </tfoot>
</table> </table>