This commit is contained in:
James Cole
2016-11-20 12:08:43 +01:00
parent a1cef5c339
commit 9340ca09e6
4 changed files with 102 additions and 21 deletions

View File

@@ -38,10 +38,9 @@ class MonthReportGenerator implements ReportGeneratorInterface
*/ */
public function generate(): string public function generate(): string
{ {
$helper = app(ReportHelperInterface::class); /** @var ReportHelperInterface $helper */
$bills = $helper->getBillReport($this->start, $this->end, $this->accounts); $helper = app(ReportHelperInterface::class);
$bills = $helper->getBillReport($this->start, $this->end, $this->accounts);
// and some id's, joined:
$accountIds = join(',', $this->accounts->pluck('id')->toArray()); $accountIds = join(',', $this->accounts->pluck('id')->toArray());
$reportType = 'default'; $reportType = 'default';

View File

@@ -13,7 +13,10 @@ declare(strict_types = 1);
namespace FireflyIII\Helpers\Collection; namespace FireflyIII\Helpers\Collection;
use Carbon\Carbon;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log;
/** /**
* Class Bill * Class Bill
@@ -26,7 +29,11 @@ class Bill
/** /**
* @var Collection * @var Collection
*/ */
protected $bills; private $bills;
/** @var Carbon */
private $endDate;
/** @var Carbon */
private $startDate;
/** /**
* *
@@ -44,6 +51,43 @@ class Bill
$this->bills->push($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 * @return Collection
*/ */
@@ -62,4 +106,20 @@ class Bill
return $set; return $set;
} }
/**
* @param Carbon $endDate
*/
public function setEndDate(Carbon $endDate)
{
$this->endDate = $endDate;
}
/**
* @param Carbon $startDate
*/
public function setStartDate(Carbon $startDate)
{
$this->startDate = $startDate;
}
} }

View File

@@ -12,6 +12,7 @@
declare(strict_types = 1); declare(strict_types = 1);
namespace FireflyIII\Helpers\Collection; namespace FireflyIII\Helpers\Collection;
use Carbon\Carbon;
use FireflyIII\Models\Bill as BillModel; use FireflyIII\Models\Bill as BillModel;
/** /**
@@ -23,8 +24,6 @@ use FireflyIII\Models\Bill as BillModel;
class BillLine class BillLine
{ {
/** @var bool */
protected $active;
/** @var string */ /** @var string */
protected $amount; protected $amount;
/** @var BillModel */ /** @var BillModel */
@@ -35,10 +34,19 @@ class BillLine
protected $max; protected $max;
/** @var string */ /** @var string */
protected $min; protected $min;
/** @var Carbon */
private $lastHitDate;
/** @var int */ /** @var int */
private $transactionJournalId; private $transactionJournalId;
/**
* BillLine constructor.
*/
public function __construct()
{
$this->lastHitDate = new Carbon;
}
/** /**
* @return string * @return string
*/ */
@@ -124,15 +132,7 @@ class BillLine
*/ */
public function isActive(): bool public function isActive(): bool
{ {
return $this->active; return intval($this->bill->active) === 1;
}
/**
* @param bool $active
*/
public function setActive(bool $active)
{
$this->active = $active;
} }
/** /**
@@ -151,4 +151,21 @@ class BillLine
$this->hit = $hit; $this->hit = $hit;
} }
/**
* @param Carbon $lastHitDate
*/
public function setLastHitDate(Carbon $lastHitDate)
{
$this->lastHitDate = $lastHitDate;
}
/**
* @return Carbon
*/
public function getLastHitDate(): Carbon
{
return $this->lastHitDate;
}
} }

View File

@@ -19,7 +19,7 @@ use FireflyIII\Helpers\Collection\BillLine;
use FireflyIII\Helpers\Collection\Category as CategoryCollection; use FireflyIII\Helpers\Collection\Category as CategoryCollection;
use FireflyIII\Helpers\Collection\Expense; use FireflyIII\Helpers\Collection\Expense;
use FireflyIII\Helpers\Collection\Income; use FireflyIII\Helpers\Collection\Income;
use FireflyIII\Helpers\Collector\JournalCollector; use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Helpers\FiscalHelperInterface; use FireflyIII\Helpers\FiscalHelperInterface;
use FireflyIII\Models\Bill; use FireflyIII\Models\Bill;
use FireflyIII\Models\Category; use FireflyIII\Models\Category;
@@ -70,16 +70,17 @@ class ReportHelper implements ReportHelperInterface
/** @var BillRepositoryInterface $repository */ /** @var BillRepositoryInterface $repository */
$repository = app(BillRepositoryInterface::class); $repository = app(BillRepositoryInterface::class);
$bills = $repository->getBillsForAccounts($accounts); $bills = $repository->getBillsForAccounts($accounts);
$collector = new JournalCollector(auth()->user()); $collector = app(JournalCollectorInterface::class, [auth()->user()]);
$collector->setAccounts($accounts)->setRange($start, $end)->setBills($bills); $collector->setAccounts($accounts)->setRange($start, $end)->setBills($bills);
$journals = $collector->getJournals(); $journals = $collector->getJournals();
$collection = new BillCollection; $collection = new BillCollection;
$collection->setStartDate($start);
$collection->setEndDate($end);
/** @var Bill $bill */ /** @var Bill $bill */
foreach ($bills as $bill) { foreach ($bills as $bill) {
$billLine = new BillLine; $billLine = new BillLine;
$billLine->setBill($bill); $billLine->setBill($bill);
$billLine->setActive(intval($bill->active) === 1);
$billLine->setMin(strval($bill->amount_min)); $billLine->setMin(strval($bill->amount_min));
$billLine->setMax(strval($bill->amount_max)); $billLine->setMax(strval($bill->amount_max));
$billLine->setHit(false); $billLine->setHit(false);
@@ -94,15 +95,19 @@ class ReportHelper implements ReportHelperInterface
if (!is_null($first)) { if (!is_null($first)) {
$billLine->setTransactionJournalId($first->id); $billLine->setTransactionJournalId($first->id);
$billLine->setAmount($first->transaction_amount); $billLine->setAmount($first->transaction_amount);
$billLine->setLastHitDate($first->date);
$billLine->setHit(true); $billLine->setHit(true);
} }
// non active AND non hit? do not add: // bill is active, or bill is hit:
if ($billLine->isActive() || $billLine->isHit()) { if ($billLine->isActive() || $billLine->isHit()) {
$collection->addBill($billLine); $collection->addBill($billLine);
} }
} }
// do some extra filtering.
$collection->filterBills();
return $collection; return $collection;
} }