Skip to content

Firefly III: a personal finances manager

Fixed #408

James Cole9340ca09e68f
diff --git a/app/Generator/Report/Standard/MonthReportGenerator.php b/app/Generator/Report/Standard/MonthReportGenerator.php
index 5bb42eee37..bcffeedff2 100644
--- a/app/Generator/Report/Standard/MonthReportGenerator.php
+++ b/app/Generator/Report/Standard/MonthReportGenerator.php
@@ -38,10 +38,9 @@ class MonthReportGenerator implements ReportGeneratorInterface
*/
public function generate(): string
{
- $helper = app(ReportHelperInterface::class);
- $bills = $helper->getBillReport($this->start, $this->end, $this->accounts);
-
- // and some id's, joined:
+ /** @var ReportHelperInterface $helper */
+ $helper = app(ReportHelperInterface::class);
+ $bills = $helper->getBillReport($this->start, $this->end, $this->accounts);
$accountIds = join(',', $this->accounts->pluck('id')->toArray());
$reportType = 'default';
diff --git a/app/Helpers/Collection/Bill.php b/app/Helpers/Collection/Bill.php
index ce83c87551..1cfac94d20 100644
--- a/app/Helpers/Collection/Bill.php
+++ b/app/Helpers/Collection/Bill.php
@@ -13,7 +13,10 @@ declare(strict_types = 1);
namespace FireflyIII\Helpers\Collection;
+use Carbon\Carbon;
+use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use Illuminate\Support\Collection;
+use Log;
/**
* Class Bill
@@ -26,7 +29,11 @@ class Bill
/**
* @var Collection
*/
- protected $bills;
+ private $bills;
+ /** @var Carbon */
+ private $endDate;
+ /** @var Carbon */
+ private $startDate;
/**
*
@@ -44,6 +51,43 @@ class Bill
$this->bills->push($bill);
}
+ /**
+ *
+ */
+ public function filterBills()
+ {
+ Log::debug('Now in filterBills()');
+ /** @var BillRepositoryInterface $repository */
+ $repository = app(BillRepositoryInterface::class);
+ $start = $this->startDate;
+ $end = $this->endDate;
+ $lines = $this->bills->filter(
+ function (BillLine $line) use ($repository, $start, $end) {
+ // next expected match?
+ $date = $start;
+ Log::debug(sprintf('Now at bill line for bill "%s"', $line->getBill()->name));
+ Log::debug(sprintf('Default date to use is start date: %s', $date->format('Y-m-d')));
+ if ($line->isHit()) {
+ $date = $line->getLastHitDate();
+ Log::debug(sprintf('Line was hit, see date: %s. Always include it.', $date->format('Y-m-d')));
+
+ return $line;
+ }
+ $expected = $repository->nextExpectedMatch($line->getBill(), $date);
+ Log::debug(sprintf('Next expected match is %s', $expected->format('Y-m-d')));
+ if ($expected <= $end && $expected >= $start) {
+ Log::debug('This date is inside report limits');
+
+ return $line;
+ }
+ Log::debug('This date is OUTSIDE report limits');
+
+ return false;
+ }
+ );
+ $this->bills = $lines;
+ }
+
/**
* @return Collection
*/
@@ -62,4 +106,20 @@ class Bill
return $set;
}
+ /**
+ * @param Carbon $endDate
+ */
+ public function setEndDate(Carbon $endDate)
+ {
+ $this->endDate = $endDate;
+ }
+
+ /**
+ * @param Carbon $startDate
+ */
+ public function setStartDate(Carbon $startDate)
+ {
+ $this->startDate = $startDate;
+ }
+
}
diff --git a/app/Helpers/Collection/BillLine.php b/app/Helpers/Collection/BillLine.php
index 07e49bc30b..fb28afed18 100644
--- a/app/Helpers/Collection/BillLine.php
+++ b/app/Helpers/Collection/BillLine.php
@@ -12,6 +12,7 @@
declare(strict_types = 1);
namespace FireflyIII\Helpers\Collection;
+use Carbon\Carbon;
use FireflyIII\Models\Bill as BillModel;
/**
@@ -23,8 +24,6 @@ use FireflyIII\Models\Bill as BillModel;
class BillLine
{
- /** @var bool */
- protected $active;
/** @var string */
protected $amount;
/** @var BillModel */
@@ -35,10 +34,19 @@ class BillLine
protected $max;
/** @var string */
protected $min;
-
+ /** @var Carbon */
+ private $lastHitDate;
/** @var int */
private $transactionJournalId;
+ /**
+ * BillLine constructor.
+ */
+ public function __construct()
+ {
+ $this->lastHitDate = new Carbon;
+ }
+
/**
* @return string
*/
@@ -124,15 +132,7 @@ class BillLine
*/
public function isActive(): bool
{
- return $this->active;
- }
-
- /**
- * @param bool $active
- */
- public function setActive(bool $active)
- {
- $this->active = $active;
+ return intval($this->bill->active) === 1;
}
/**
@@ -151,4 +151,21 @@ class BillLine
$this->hit = $hit;
}
+ /**
+ * @param Carbon $lastHitDate
+ */
+ public function setLastHitDate(Carbon $lastHitDate)
+ {
+ $this->lastHitDate = $lastHitDate;
+ }
+
+ /**
+ * @return Carbon
+ */
+ public function getLastHitDate(): Carbon
+ {
+ return $this->lastHitDate;
+ }
+
+
}
diff --git a/app/Helpers/Report/ReportHelper.php b/app/Helpers/Report/ReportHelper.php
index 063264df06..53e1a28fb9 100644
--- a/app/Helpers/Report/ReportHelper.php
+++ b/app/Helpers/Report/ReportHelper.php
@@ -19,7 +19,7 @@ use FireflyIII\Helpers\Collection\BillLine;
use FireflyIII\Helpers\Collection\Category as CategoryCollection;
use FireflyIII\Helpers\Collection\Expense;
use FireflyIII\Helpers\Collection\Income;
-use FireflyIII\Helpers\Collector\JournalCollector;
+use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Helpers\FiscalHelperInterface;
use FireflyIII\Models\Bill;
use FireflyIII\Models\Category;
@@ -70,16 +70,17 @@ class ReportHelper implements ReportHelperInterface
/** @var BillRepositoryInterface $repository */
$repository = app(BillRepositoryInterface::class);
$bills = $repository->getBillsForAccounts($accounts);
- $collector = new JournalCollector(auth()->user());
+ $collector = app(JournalCollectorInterface::class, [auth()->user()]);
$collector->setAccounts($accounts)->setRange($start, $end)->setBills($bills);
$journals = $collector->getJournals();
$collection = new BillCollection;
+ $collection->setStartDate($start);
+ $collection->setEndDate($end);
/** @var Bill $bill */
foreach ($bills as $bill) {
$billLine = new BillLine;
$billLine->setBill($bill);
- $billLine->setActive(intval($bill->active) === 1);
$billLine->setMin(strval($bill->amount_min));
$billLine->setMax(strval($bill->amount_max));
$billLine->setHit(false);
@@ -94,15 +95,19 @@ class ReportHelper implements ReportHelperInterface
if (!is_null($first)) {
$billLine->setTransactionJournalId($first->id);
$billLine->setAmount($first->transaction_amount);
+ $billLine->setLastHitDate($first->date);
$billLine->setHit(true);
}
- // non active AND non hit? do not add:
+ // bill is active, or bill is hit:
if ($billLine->isActive() || $billLine->isHit()) {
$collection->addBill($billLine);
}
}
+ // do some extra filtering.
+ $collection->filterBills();
+
return $collection;
}