. */ declare(strict_types=1); namespace FireflyIII\Helpers\Collection; use Carbon\Carbon; use FireflyIII\Repositories\Bill\BillRepositoryInterface; use Illuminate\Support\Collection; use Log; /** * Class Bill. * * @codeCoverageIgnore */ class Bill { /** @var Collection The bills. */ private $bills; /** @var Carbon End date of the collection. */ private $endDate; /** @var Carbon Start date of the collection. */ private $startDate; /** * Bill constructor. */ public function __construct() { $this->bills = new Collection; } /** * Add a bill line. * * @param BillLine $bill */ public function addBill(BillLine $bill): void { $this->bills->push($bill); } /** * Filter the bills (yes how very descriptive). */ public function filterBills(): void { 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; } /** * Bills getter. * * @return Collection */ public function getBills(): Collection { return $this->bills; } /** * End date setter. * * @param Carbon $endDate */ public function setEndDate(Carbon $endDate): void { $this->endDate = $endDate; } /** * Start date setter. * * @param Carbon $startDate */ public function setStartDate(Carbon $startDate): void { $this->startDate = $startDate; } }