Some extended code for the category report.

This commit is contained in:
James Cole
2016-11-12 06:48:38 +01:00
parent 32b5a84a0c
commit 6d60d64a82
12 changed files with 716 additions and 20 deletions

View File

@@ -16,7 +16,11 @@ namespace FireflyIII\Generator\Report\Category;
use Carbon\Carbon;
use FireflyIII\Generator\Report\ReportGeneratorInterface;
use FireflyIII\Helpers\Collector\JournalCollector;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionType;
use Illuminate\Support\Collection;
use Log;
/**
* Class MonthReportGenerator
@@ -39,13 +43,17 @@ class MonthReportGenerator implements ReportGeneratorInterface
*/
public function generate(): string
{
$accountIds = join(',', $this->accounts->pluck('id')->toArray());
$reportType = 'category';
$accountIds = join(',', $this->accounts->pluck('id')->toArray());
$categoryIds = join(',', $this->categories->pluck('id')->toArray());
$reportType = 'category';
$accountSummary = $this->getAccountSummary();
$categorySummary = $this->getCategorySummary();
// render!
return view('reports.category.month', compact('accountIds', 'reportType'))
return view('reports.category.month', compact('accountIds', 'categoryIds', 'reportType', 'accountSummary', 'categorySummary'))
->with('start', $this->start)->with('end', $this->end)
->with('categories', $this->categories)
->with('accounts', $this->accounts)
->render();
}
@@ -96,4 +104,226 @@ class MonthReportGenerator implements ReportGeneratorInterface
return $this;
}
/**
* @return array
*/
private function getAccountSummary(): array
{
$spent = $this->getSpentAccountSummary();
$earned = $this->getEarnedAccountSummary();
$return = [];
/**
* @var int $accountId
* @var string $entry
*/
foreach ($spent as $accountId => $entry) {
if (!isset($return[$accountId])) {
$return[$accountId] = ['spent' => 0, 'earned' => 0];
}
$return[$accountId]['spent'] = $entry;
}
unset($entry);
/**
* @var int $accountId
* @var string $entry
*/
foreach ($earned as $accountId => $entry) {
if (!isset($return[$accountId])) {
$return[$accountId] = ['spent' => 0, 'earned' => 0];
}
$return[$accountId]['earned'] = $entry;
}
return $return;
}
/**
* @return array
*/
private function getCategorySummary(): array
{
$spent = $this->getSpentCategorySummary();
$earned = $this->getEarnedCategorySummary();
$return = [];
/**
* @var int $categoryId
* @var string $entry
*/
foreach ($spent as $categoryId => $entry) {
if (!isset($return[$categoryId])) {
$return[$categoryId] = ['spent' => 0, 'earned' => 0];
}
$return[$categoryId]['spent'] = $entry;
}
unset($entry);
/**
* @var int $categoryId
* @var string $entry
*/
foreach ($earned as $categoryId => $entry) {
if (!isset($return[$categoryId])) {
$return[$categoryId] = ['spent' => 0, 'earned' => 0];
}
$return[$categoryId]['earned'] = $entry;
}
return $return;
}
/**
* @return array
*/
private function getEarnedAccountSummary(): array
{
$transactions = $this->getIncomes();
$result = [];
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
$accountId = $transaction->account_id;
$result[$accountId] = $result[$accountId] ?? '0';
$result[$accountId] = bcadd($transaction->transaction_amount, $result[$accountId]);
}
return $result;
}
/**
* @return array
*/
private function getEarnedCategorySummary(): array
{
$transactions = $this->getIncomes();
$result = [];
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
$jrnlCatId = intval($transaction->transaction_journal_category_id);
$transCatId = intval($transaction->transaction_category_id);
$categoryId = max($jrnlCatId, $transCatId);
$result[$categoryId] = $result[$categoryId] ?? '0';
$result[$categoryId] = bcadd($transaction->transaction_amount, $result[$categoryId]);
}
return $result;
}
/**
* @return Collection
*/
private function getExpenses(): Collection
{
$collector = new JournalCollector(auth()->user());
$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)
->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
->setCategories($this->categories)->getOpposingAccount()->disableFilter();
$accountIds = $this->accounts->pluck('id')->toArray();
$transactions = $collector->getJournals();
$transactions = $transactions->filter(
function (Transaction $transaction) use ($accountIds) {
$opposing = $transaction->opposing_account_id;
// remove internal transfer
if (in_array($opposing, $accountIds)) {
Log::debug(sprintf('Filtered #%d because its opposite is in accounts.', $transaction->id));
return null;
}
// remove positive amount
if (bccomp($transaction->transaction_amount, '0') === 1) {
Log::debug(sprintf('Filtered #%d because amount is %f.', $transaction->id, $transaction->transaction_amount));
return null;
}
return $transaction;
}
);
return $transactions;
}
/**
* @return Collection
*/
private function getIncomes(): Collection
{
$collector = new JournalCollector(auth()->user());
$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)
->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER])
->setCategories($this->categories)->getOpposingAccount();
$accountIds = $this->accounts->pluck('id')->toArray();
$transactions = $collector->getJournals();
$transactions = $transactions->filter(
function (Transaction $transaction) use ($accountIds) {
$opposing = $transaction->opposing_account_id;
// remove internal transfer
if (in_array($opposing, $accountIds)) {
Log::debug(sprintf('Filtered #%d because its opposite is in accounts.', $transaction->id));
return null;
}
// remove positive amount
if (bccomp($transaction->transaction_amount, '0') === -1) {
Log::debug(sprintf('Filtered #%d because amount is %f.', $transaction->id, $transaction->transaction_amount));
return null;
}
return $transaction;
}
);
return $transactions;
}
/**
* @return array
*/
private function getSpentAccountSummary(): array
{
$transactions = $this->getExpenses();
$result = [];
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
$accountId = $transaction->account_id;
$result[$accountId] = $result[$accountId] ?? '0';
$result[$accountId] = bcadd($transaction->transaction_amount, $result[$accountId]);
}
return $result;
}
/**
* @return array
*/
private function getSpentCategorySummary(): array
{
$transactions = $this->getExpenses();
$result = [];
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
$jrnlCatId = intval($transaction->transaction_journal_category_id);
$transCatId = intval($transaction->transaction_category_id);
$categoryId = max($jrnlCatId, $transCatId);
$result[$categoryId] = $result[$categoryId] ?? '0';
$result[$categoryId] = bcadd($transaction->transaction_amount, $result[$categoryId]);
}
return $result;
}
}