firefly-iii/app/Helpers/Collection/Bill.php

130 lines
3.5 KiB
PHP
Raw Normal View History

<?php
/**
* Bill.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Helpers\Collection;
2016-11-20 05:08:43 -06:00
use Carbon\Carbon;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use Illuminate\Support\Collection;
2016-11-20 05:08:43 -06:00
use Log;
/**
2017-11-15 05:25:49 -06:00
* Class Bill.
*/
class Bill
{
/**
* @var Collection
*/
2016-11-20 05:08:43 -06:00
private $bills;
2017-11-15 05:25:49 -06:00
/** @var Carbon */
2016-11-20 05:08:43 -06:00
private $endDate;
2017-11-15 05:25:49 -06:00
/** @var Carbon */
2016-11-20 05:08:43 -06:00
private $startDate;
/**
*
*/
public function __construct()
{
$this->bills = new Collection;
}
/**
* @param BillLine $bill
*/
public function addBill(BillLine $bill)
{
$this->bills->push($bill);
}
2016-11-20 05:08:43 -06:00
/**
*
*/
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
*/
2016-02-18 03:04:26 -06:00
public function getBills(): Collection
{
2015-06-16 23:11:35 -05:00
$set = $this->bills->sortBy(
2015-06-06 16:09:12 -05:00
function (BillLine $bill) {
2017-11-15 05:25:49 -06:00
$active = 0 === intval($bill->getBill()->active) ? 1 : 0;
2015-05-20 12:55:53 -05:00
$name = $bill->getBill()->name;
return $active . $name;
}
);
2015-06-16 23:11:35 -05:00
return $set;
}
2016-11-20 05:08:43 -06:00
/**
* @param Carbon $endDate
*/
public function setEndDate(Carbon $endDate)
{
$this->endDate = $endDate;
}
/**
* @param Carbon $startDate
*/
public function setStartDate(Carbon $startDate)
{
$this->startDate = $startDate;
}
2015-05-20 12:56:14 -05:00
}