Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Cleaned up some bill related code.
James Cole6a6ec9fbe409
diff --git a/app/Helpers/Report/ReportQuery.php b/app/Helpers/Report/ReportQuery.phpindex 4d73653c9f..69044a06ff 100644--- a/app/Helpers/Report/ReportQuery.php+++ b/app/Helpers/Report/ReportQuery.php@@ -21,7 +21,11 @@ use Steam;class ReportQuery implements ReportQueryInterface{/**- * See ReportQueryInterface::incomeInPeriodCorrected+ * See ReportQueryInterface::incomeInPeriodCorrected.+ *+ * This method's length is caused mainly by the query build stuff. Therefor:+ *+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)** @param Carbon $start* @param Carbon $enddiff --git a/app/Http/Controllers/Chart/BillController.php b/app/Http/Controllers/Chart/BillController.phpindex 522df0d2e2..dfcc3c5b2e 100644--- a/app/Http/Controllers/Chart/BillController.php+++ b/app/Http/Controllers/Chart/BillController.php@@ -9,10 +9,8 @@ use FireflyIII\Models\TransactionJournal;use FireflyIII\Repositories\Account\AccountRepositoryInterface;use FireflyIII\Repositories\Bill\BillRepositoryInterface;use FireflyIII\Support\CacheProperties;-use Illuminate\Support\Collection;use Response;use Session;-use Steam;/*** Class BillController@@ -39,19 +37,14 @@ class BillController extends Controller* Shows all bills and whether or not theyve been paid this month (pie chart).** @param BillRepositoryInterface $repository- * @param AccountRepositoryInterface $accounts** @return \Symfony\Component\HttpFoundation\Response*/- public function frontpage(BillRepositoryInterface $repository, AccountRepositoryInterface $accounts)+ public function frontpage(BillRepositoryInterface $repository){-$start = Session::get('start', Carbon::now()->startOfMonth());$end = Session::get('end', Carbon::now()->endOfMonth());--- // chart properties for cache:- $cache = new CacheProperties();+ $cache = new CacheProperties(); // chart properties for cache:$cache->addProperty($start);$cache->addProperty($end);$cache->addProperty('bills');@@ -60,46 +53,13 @@ class BillController extends Controllerreturn Response::json($cache->get()); // @codeCoverageIgnore}- $bills = $repository->getActiveBills();- $paid = new Collection; // journals.- $unpaid = new Collection; // bills+ $set = $repository->getBillsForChart($start, $end);+ // optionally expand this set with credit card data+ $set = $repository->getCreditCardInfoForChart($set, $start, $end);+ $paid = $set->get('paid');+ $unpaid = $set->get('unpaid');- /** @var Bill $bill */- foreach ($bills as $bill) {- $ranges = $repository->getRanges($bill, $start, $end);-- foreach ($ranges as $range) {- // paid a bill in this range?- $journals = $repository->getJournalsInRange($bill, $range['start'], $range['end']);- if ($journals->count() == 0) {- $unpaid->push([$bill, $range['start']]);- } else {- $paid = $paid->merge($journals);- }-- }- }-- $creditCards = $accounts->getCreditCards();- foreach ($creditCards as $creditCard) {- $balance = Steam::balance($creditCard, $end, true);- $date = new Carbon($creditCard->getMeta('ccMonthlyPaymentDate'));- if ($balance < 0) {- // unpaid! create a fake bill that matches the amount.- $description = $creditCard->name;- $amount = $balance * -1;- $fakeBill = $repository->createFakeBill($description, $date, $amount);- unset($description, $amount);- $unpaid->push([$fakeBill, $date]);- }- if ($balance == 0) {- // find transfer(s) TO the credit card which should account for- // anything paid. If not, the CC is not yet used.- $journals = $accounts->getTransfersInRange($creditCard, $start, $end);- $paid = $paid->merge($journals);- }- }// build chart:$data = $this->generator->frontpage($paid, $unpaid);diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.phpindex 052343f1c5..b86eefe26c 100644--- a/app/Repositories/Bill/BillRepository.php+++ b/app/Repositories/Bill/BillRepository.php@@ -10,6 +10,7 @@ use FireflyIII\Models\Transaction;use FireflyIII\Models\TransactionJournal;use Illuminate\Support\Collection;use Navigation;+use Steam;/*** Class BillRepository@@ -39,6 +40,7 @@ class BillRepository implements BillRepositoryInterfacereturn $amount;}+/*** Create a fake bill to help the chart controller.*@@ -383,4 +385,84 @@ class BillRepository implements BillRepositoryInterfacereturn false;}++ /**+ * Gets a collection of paid bills and a collection of unpaid bills to be used+ * in the pie chart on the front page.+ *+ * @param Carbon $start+ * @param Carbon $end+ *+ * @return Collection+ */+ public function getBillsForChart(Carbon $start, Carbon $end)+ {+ $paid = new Collection;+ $unpaid = new Collection;++ $bills = $this->getActiveBills();+ /** @var Bill $bill */+ foreach ($bills as $bill) {+ $ranges = $this->getRanges($bill, $start, $end);++ foreach ($ranges as $range) {+ // paid a bill in this range?+ $journals = $this->getJournalsInRange($bill, $range['start'], $range['end']);+ if ($journals->count() == 0) {+ $unpaid->push([$bill, $range['start']]);+ } else {+ $paid = $paid->merge($journals);+ }++ }+ }+ $set = new Collection;+ $set->put('paid', $paid);+ $set->put('unpaid', $unpaid);++ return $set;+ }++ /**+ * Takes the paid/unpaid bills collection set up before and expands it using+ * credit cards the user might have.+ *+ * @param Collection $set+ * @param Carbon $start+ * @param Carbon $end+ *+ * @return Collection+ */+ public function getCreditCardInfoForChart(Collection $set, Carbon $start, Carbon $end)+ {++ $accounts = app('FireflyIII\Repositories\Account\AccountRepositoryInterface');+ $creditCards = $accounts->getCreditCards();+ $paid = $set->get('paid');+ $unpaid = $set->get('unpaid');++ foreach ($creditCards as $creditCard) {+ $balance = Steam::balance($creditCard, $end, true);+ $date = new Carbon($creditCard->getMeta('ccMonthlyPaymentDate'));+ if ($balance < 0) {+ // unpaid! create a fake bill that matches the amount.+ $description = $creditCard->name;+ $amount = $balance * -1;+ $fakeBill = $this->createFakeBill($description, $date, $amount);+ unset($description, $amount);+ $unpaid->push([$fakeBill, $date]);+ }+ if ($balance == 0) {+ // find transfer(s) TO the credit card which should account for+ // anything paid. If not, the CC is not yet used.+ $journals = $accounts->getTransfersInRange($creditCard, $start, $end);+ $paid = $paid->merge($journals);+ }+ }+ $set = new Collection;+ $set->put('paid', $paid);+ $set->put('unpaid', $unpaid);++ return $set;+ }}diff --git a/app/Repositories/Bill/BillRepositoryInterface.php b/app/Repositories/Bill/BillRepositoryInterface.phpindex f2c32e20a7..fe36b810a5 100644--- a/app/Repositories/Bill/BillRepositoryInterface.php+++ b/app/Repositories/Bill/BillRepositoryInterface.php@@ -15,6 +15,29 @@ use Illuminate\Support\Collection;interface BillRepositoryInterface{+ /**+ * Takes the paid/unpaid bills collection set up before and expands it using+ * credit cards the user might have.+ *+ * @param Collection $set+ * @param Carbon $start+ * @param Carbon $end+ *+ * @return Collection+ */+ public function getCreditCardInfoForChart(Collection $set, Carbon $start, Carbon $end);++ /**+ * Gets a collection of paid bills and a collection of unpaid bills to be used+ * in the pie chart on the front page.+ *+ * @param Carbon $start+ * @param Carbon $end+ *+ * @return Collection+ */+ public function getBillsForChart(Carbon $start, Carbon $end);+/*** Returns the sum of all payments connected to this bill between the dates.*