mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Building report from issue #386
This commit is contained in:
@@ -15,11 +15,9 @@ namespace FireflyIII\Http\Controllers\Report;
|
||||
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
|
||||
use FireflyIII\Helpers\Report\ReportHelperInterface;
|
||||
use FireflyIII\Http\Controllers\Controller;
|
||||
use FireflyIII\Models\Category;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
||||
use FireflyIII\Support\CacheProperties;
|
||||
use Illuminate\Support\Collection;
|
||||
@@ -42,71 +40,15 @@ class CategoryController extends Controller
|
||||
*/
|
||||
public function categoryPeriodReport(Carbon $start, Carbon $end, Collection $accounts)
|
||||
{
|
||||
|
||||
/** @var CategoryRepositoryInterface $repository */
|
||||
$repository = app(CategoryRepositoryInterface::class);
|
||||
$categories = $repository->getCategories();
|
||||
$data = [];
|
||||
$report = $repository->getCategoryPeriodReport($categories, $accounts, $start, $end, true);
|
||||
$report = $this->filterCategoryPeriodReport($report);
|
||||
$periods = Navigation::listOfPeriods($start, $end);
|
||||
|
||||
// income only:
|
||||
/** @var JournalCollectorInterface $collector */
|
||||
$collector = app(JournalCollectorInterface::class);
|
||||
$collector->setAccounts($accounts)->setRange($start, $end)
|
||||
->withOpposingAccount()
|
||||
->enableInternalFilter()
|
||||
->setCategories($categories);
|
||||
|
||||
$transactions = $collector->getJournals();
|
||||
|
||||
// this is the date format we need:
|
||||
// define period to group on:
|
||||
$carbonFormat = Navigation::preferredCarbonFormat($start, $end);
|
||||
|
||||
// this is the set of transactions for this period
|
||||
// in these budgets. Now they must be grouped (manually)
|
||||
// id, period => amount
|
||||
$income = [];
|
||||
foreach ($transactions as $transaction) {
|
||||
$categoryId = max(intval($transaction->transaction_journal_category_id), intval($transaction->transaction_category_id));
|
||||
$date = $transaction->date->format($carbonFormat);
|
||||
|
||||
if (!isset($income[$categoryId])) {
|
||||
$income[$categoryId]['name'] = $this->getCategoryName($categoryId, $categories);
|
||||
$income[$categoryId]['sum'] = '0';
|
||||
$income[$categoryId]['entries'] = [];
|
||||
}
|
||||
|
||||
if (!isset($income[$categoryId]['entries'][$date])) {
|
||||
$income[$categoryId]['entries'][$date] = '0';
|
||||
}
|
||||
$income[$categoryId]['entries'][$date] = bcadd($income[$categoryId]['entries'][$date], $transaction->transaction_amount);
|
||||
}
|
||||
|
||||
// and now the same for stuff without a category:
|
||||
/** @var JournalCollectorInterface $collector */
|
||||
$collector = app(JournalCollectorInterface::class);
|
||||
$collector->setAllAssetAccounts()->setRange($start, $end);
|
||||
$collector->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER]);
|
||||
$collector->withoutCategory();
|
||||
$transactions = $collector->getJournals();
|
||||
|
||||
$income[0]['entries'] = [];
|
||||
$income[0]['name'] = strval(trans('firefly.no_category'));
|
||||
$income[0]['sum'] = '0';
|
||||
|
||||
foreach ($transactions as $transaction) {
|
||||
$date = $transaction->date->format($carbonFormat);
|
||||
|
||||
if (!isset($income[0]['entries'][$date])) {
|
||||
$income[0]['entries'][$date] = '0';
|
||||
}
|
||||
$income[0]['entries'][$date] = bcadd($income[0]['entries'][$date], $transaction->transaction_amount);
|
||||
}
|
||||
|
||||
$periods = Navigation::listOfPeriods($start, $end);
|
||||
|
||||
$income = $this->filterCategoryPeriodReport($income);
|
||||
|
||||
$result = view('reports.partials.category-period', compact('categories', 'periods', 'income'))->render();
|
||||
$result = view('reports.partials.category-period', compact('categories', 'periods', 'report'))->render();
|
||||
|
||||
return $result;
|
||||
}
|
||||
@@ -148,18 +90,20 @@ class CategoryController extends Controller
|
||||
*/
|
||||
private function filterCategoryPeriodReport(array $data): array
|
||||
{
|
||||
/**
|
||||
* @var int $categoryId
|
||||
* @var array $set
|
||||
*/
|
||||
foreach ($data as $categoryId => $set) {
|
||||
$sum = '0';
|
||||
foreach ($set['entries'] as $amount) {
|
||||
$sum = bcadd($amount, $sum);
|
||||
}
|
||||
$data[$categoryId]['sum'] = $sum;
|
||||
if (bccomp('0', $sum) === 0) {
|
||||
unset($data[$categoryId]);
|
||||
foreach ($data as $key => $set) {
|
||||
/**
|
||||
* @var int $categoryId
|
||||
* @var array $set
|
||||
*/
|
||||
foreach ($set as $categoryId => $info) {
|
||||
$sum = '0';
|
||||
foreach ($info['entries'] as $amount) {
|
||||
$sum = bcadd($amount, $sum);
|
||||
}
|
||||
$data[$key][$categoryId]['sum'] = $sum;
|
||||
if (bccomp('0', $sum) === 0) {
|
||||
unset($data[$key][$categoryId]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user