Skip to content

Firefly III: a personal finances manager

First attempt at including expense report.

James Colead01891a67dc
diff --git a/app/Helpers/Report/ReportHelper.php b/app/Helpers/Report/ReportHelper.php
index df80eaa951..b9a15f100c 100644
--- a/app/Helpers/Report/ReportHelper.php
+++ b/app/Helpers/Report/ReportHelper.php
@@ -473,4 +473,25 @@ class ReportHelper implements ReportHelperInterface
return $object;
}
+
+ /**
+ * Get a full report on the users expenses during the period for a list of accounts.
+ *
+ * @param Carbon $start
+ * @param Carbon $end
+ * @param Collection $accounts
+ *
+ * @return Expense
+ */
+ public function getExpenseReportForList($start, $end, Collection $accounts)
+ {
+ $object = new Expense;
+ $set = $this->query->expenseInPeriodCorrectedForList($start, $end, $accounts);
+ foreach ($set as $entry) {
+ $object->addToTotal($entry->amount_positive);
+ $object->addOrCreateExpense($entry);
+ }
+
+ return $object;
+ }
}
diff --git a/app/Helpers/Report/ReportHelperInterface.php b/app/Helpers/Report/ReportHelperInterface.php
index dd4166352a..6db8ce93e3 100644
--- a/app/Helpers/Report/ReportHelperInterface.php
+++ b/app/Helpers/Report/ReportHelperInterface.php
@@ -83,15 +83,26 @@ interface ReportHelperInterface
public function getCategoryReport(Carbon $start, Carbon $end, $shared);
/**
- * Get a full report on the users expenses during the period.
+ * Get a full report on the users expenses during the period.
+ *
+ * @param Carbon $start
+ * @param Carbon $end
+ * @param boolean $shared
+ *
+ * @return Expense
+ */
+ public function getExpenseReport($start, $end, $shared);
+
+ /**
+ * Get a full report on the users expenses during the period for a list of accounts.
*
* @param Carbon $start
* @param Carbon $end
- * @param boolean $shared
+ * @param Collection $accounts
*
* @return Expense
*/
- public function getExpenseReport($start, $end, $shared);
+ public function getExpenseReportForList($start, $end, Collection $accounts);
/**
* Get a full report on the users incomes during the period.
diff --git a/app/Helpers/Report/ReportQuery.php b/app/Helpers/Report/ReportQuery.php
index 9809a9137c..48b88d6886 100644
--- a/app/Helpers/Report/ReportQuery.php
+++ b/app/Helpers/Report/ReportQuery.php
@@ -12,7 +12,6 @@ use FireflyIII\Models\TransactionType;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;
-use Steam;
/**
* Class ReportQuery
@@ -301,7 +300,7 @@ class ReportQuery implements ReportQueryInterface
$query->orWhere(
function (Builder $q) {
$q->where('transaction_types.type', TransactionType::TRANSFER);
- $q->where('acm_from.data', '=', '"sharedAsset"');
+ $q->where('acm_from.data', '=', '"sharedAsset"');
$q->where('acm_to.data', '!=', '"sharedAsset"');
}
);
@@ -336,4 +335,65 @@ class ReportQuery implements ReportQueryInterface
return $data;
}
+
+ /**
+ * See ReportQueryInterface::incomeInPeriodCorrected
+ *
+ * This method returns all "expense" journals in a certain period, which are both transfers to a shared account
+ * and "ordinary" withdrawals. The query used is almost equal to ReportQueryInterface::journalsByRevenueAccount but it does
+ * not group and returns different fields.
+ *
+ * @param Carbon $start
+ * @param Carbon $end
+ * @param Collection $accounts
+ *
+ * @return Collection
+ *
+ */
+ public function expenseInPeriodCorrectedForList(Carbon $start, Carbon $end, Collection $accounts)
+ {
+ $ids = [];
+
+ /** @var Account $account */
+ foreach ($accounts as $account) {
+ $ids[] = $account->id;
+ }
+
+ $query = $this->queryJournalsWithTransactions($start, $end);
+ $query->where(
+ function (Builder $query) {
+ $query->where(
+ function (Builder $q) { // only get withdrawals not from a shared account
+ $q->where('transaction_types.type', TransactionType::WITHDRAWAL);
+ $q->where('acm_from.data', '!=', '"sharedAsset"');
+ }
+ );
+ $query->orWhere(
+ function (Builder $q) { // and transfers from a shared account.
+ $q->where('transaction_types.type', TransactionType::TRANSFER);
+ $q->where('acm_to.data', '=', '"sharedAsset"');
+ $q->where('acm_from.data', '!=', '"sharedAsset"');
+ }
+ );
+ }
+ );
+
+ // expense goes from the selected accounts:
+ $query->whereIn('ac_from.id', $ids);
+
+ $query->orderBy('transaction_journals.date');
+ $data = $query->get( // get everything
+ ['transaction_journals.*', 'transaction_types.type', 'ac_to.name as name', 'ac_to.id as account_id', 'ac_to.encrypted as account_encrypted']
+ );
+
+ $data->each(
+ function (TransactionJournal $journal) {
+ if (intval($journal->account_encrypted) == 1) {
+ $journal->name = Crypt::decrypt($journal->name);
+ }
+ }
+ );
+
+ return $data;
+ }
}
diff --git a/app/Helpers/Report/ReportQueryInterface.php b/app/Helpers/Report/ReportQueryInterface.php
index 8a234bfa4a..fa45453b3d 100644
--- a/app/Helpers/Report/ReportQueryInterface.php
+++ b/app/Helpers/Report/ReportQueryInterface.php
@@ -31,6 +31,22 @@ interface ReportQueryInterface
*/
public function expenseInPeriodCorrected(Carbon $start, Carbon $end, $includeShared = false);
+ /**
+ * See ReportQueryInterface::incomeInPeriodCorrected
+ *
+ * This method returns all "expense" journals in a certain period, which are both transfers to a shared account
+ * and "ordinary" withdrawals. The query used is almost equal to ReportQueryInterface::journalsByRevenueAccount but it does
+ * not group and returns different fields.
+ *
+ * @param Carbon $start
+ * @param Carbon $end
+ * @param Collection $accounts
+ *
+ * @return Collection
+ *
+ */
+ public function expenseInPeriodCorrectedForList(Carbon $start, Carbon $end, Collection $accounts);
+
/**
* Get a users accounts combined with various meta-data related to the start and end date.
*
diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php
index 9bbfbfa727..cf6928cd12 100644
--- a/app/Http/Controllers/ReportController.php
+++ b/app/Http/Controllers/ReportController.php
@@ -243,11 +243,11 @@ class ReportController extends Controller
// get report stuff!
$accounts = $this->helper->getAccountReportForList($start, $end, $list);
$incomes = $this->helper->getIncomeReportForList($start, $end, $list);
-// $expenses = $this->helper->getExpenseReportForList($start, $end, $list);
-// $budgets = $this->helper->getBudgetReportForList($start, $end, $list);
-// $categories = $this->helper->getCategoryReportForList($start, $end, $list);
-// $balance = $this->helper->getBalanceReportForList($start, $end, $list);
-// $bills = $this->helper->getBillReportForList($start, $end);
+ $expenses = $this->helper->getExpenseReportForList($start, $end, $list);
+ // $budgets = $this->helper->getBudgetReportForList($start, $end, $list);
+ // $categories = $this->helper->getCategoryReportForList($start, $end, $list);
+ // $balance = $this->helper->getBalanceReportForList($start, $end, $list);
+ // $bills = $this->helper->getBillReportForList($start, $end);
// continue!
return view(