2015-02-25 08:19:14 -06:00
|
|
|
<?php
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* BillRepository.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-10-04 23:52:15 -05:00
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
2016-05-20 05:41:23 -05:00
|
|
|
*/
|
|
|
|
|
2016-02-05 05:08:25 -06:00
|
|
|
declare(strict_types = 1);
|
2015-02-25 08:19:14 -06:00
|
|
|
|
|
|
|
namespace FireflyIII\Repositories\Bill;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2015-05-08 07:00:49 -05:00
|
|
|
use DB;
|
2015-02-25 08:19:14 -06:00
|
|
|
use FireflyIII\Models\Bill;
|
2016-10-20 23:26:12 -05:00
|
|
|
use FireflyIII\Models\Transaction;
|
2015-02-25 08:19:14 -06:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2015-12-27 10:29:41 -06:00
|
|
|
use FireflyIII\Models\TransactionType;
|
2016-10-20 14:40:45 -05:00
|
|
|
use FireflyIII\Support\CacheProperties;
|
2016-03-03 01:40:25 -06:00
|
|
|
use FireflyIII\User;
|
2015-12-26 02:39:35 -06:00
|
|
|
use Illuminate\Database\Query\JoinClause;
|
2016-04-21 03:23:19 -05:00
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
2015-04-05 11:20:06 -05:00
|
|
|
use Illuminate\Support\Collection;
|
2016-10-20 14:40:45 -05:00
|
|
|
use Log;
|
2015-03-03 10:40:17 -06:00
|
|
|
use Navigation;
|
2015-02-25 08:19:14 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class BillRepository
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Repositories\Bill
|
|
|
|
*/
|
|
|
|
class BillRepository implements BillRepositoryInterface
|
|
|
|
{
|
2015-04-07 03:14:10 -05:00
|
|
|
|
2016-03-03 01:40:25 -06:00
|
|
|
/** @var User */
|
|
|
|
private $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* BillRepository constructor.
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
public function __construct(User $user)
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
2015-04-05 11:20:06 -05:00
|
|
|
/**
|
|
|
|
* @param Bill $bill
|
|
|
|
*
|
2016-04-06 02:27:45 -05:00
|
|
|
* @return bool
|
2015-04-05 11:20:06 -05:00
|
|
|
*/
|
2016-02-06 11:59:48 -06:00
|
|
|
public function destroy(Bill $bill): bool
|
2015-04-05 11:20:06 -05:00
|
|
|
{
|
2016-02-06 11:59:48 -06:00
|
|
|
$bill->delete();
|
|
|
|
|
|
|
|
return true;
|
2015-04-05 11:20:06 -05:00
|
|
|
}
|
|
|
|
|
2016-04-01 06:07:19 -05:00
|
|
|
/**
|
|
|
|
* Find a bill by ID.
|
|
|
|
*
|
|
|
|
* @param int $billId
|
|
|
|
*
|
|
|
|
* @return Bill
|
|
|
|
*/
|
|
|
|
public function find(int $billId) : Bill
|
|
|
|
{
|
|
|
|
$bill = $this->user->bills()->find($billId);
|
|
|
|
if (is_null($bill)) {
|
|
|
|
$bill = new Bill;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $bill;
|
|
|
|
}
|
|
|
|
|
2016-07-23 14:37:06 -05:00
|
|
|
/**
|
|
|
|
* Find a bill by name.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return Bill
|
|
|
|
*/
|
|
|
|
public function findByName(string $name) : Bill
|
|
|
|
{
|
2016-08-11 03:21:32 -05:00
|
|
|
$bills = $this->user->bills()->get(['bills.*']);
|
2016-07-23 14:37:06 -05:00
|
|
|
|
|
|
|
/** @var Bill $bill */
|
|
|
|
foreach ($bills as $bill) {
|
|
|
|
if ($bill->name === $name) {
|
|
|
|
return $bill;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Bill;
|
|
|
|
}
|
|
|
|
|
2016-01-20 08:21:27 -06:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-02-06 11:59:48 -06:00
|
|
|
public function getActiveBills(): Collection
|
2016-01-20 08:21:27 -06:00
|
|
|
{
|
|
|
|
/** @var Collection $set */
|
2016-03-03 01:40:25 -06:00
|
|
|
$set = $this->user->bills()
|
|
|
|
->where('active', 1)
|
|
|
|
->get(
|
|
|
|
[
|
|
|
|
'bills.*',
|
2016-10-05 22:26:38 -05:00
|
|
|
DB::raw('((bills.amount_min + bills.amount_max) / 2) AS expectedAmount'),
|
2016-03-03 01:40:25 -06:00
|
|
|
]
|
|
|
|
)->sortBy('name');
|
2016-01-20 08:21:27 -06:00
|
|
|
|
|
|
|
return $set;
|
|
|
|
}
|
|
|
|
|
2016-01-01 14:07:15 -06:00
|
|
|
/**
|
|
|
|
* Returns all journals connected to these bills in the given range. Amount paid
|
|
|
|
* is stored in "journalAmount" as a negative number.
|
|
|
|
*
|
|
|
|
* @param Collection $bills
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-02-06 11:59:48 -06:00
|
|
|
public function getAllJournalsInRange(Collection $bills, Carbon $start, Carbon $end): Collection
|
2016-01-01 14:07:15 -06:00
|
|
|
{
|
|
|
|
$ids = $bills->pluck('id')->toArray();
|
|
|
|
|
2016-08-26 02:30:52 -05:00
|
|
|
$set = $this->user->transactionJournals()
|
2016-03-03 01:40:25 -06:00
|
|
|
->leftJoin(
|
|
|
|
'transactions', function (JoinClause $join) {
|
|
|
|
$join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '<', 0);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
->whereIn('bill_id', $ids)
|
|
|
|
->before($end)
|
|
|
|
->after($start)
|
2016-09-16 05:07:45 -05:00
|
|
|
->groupBy(['transaction_journals.bill_id', 'transaction_journals.id'])
|
2016-03-03 01:40:25 -06:00
|
|
|
->get(
|
|
|
|
[
|
|
|
|
'transaction_journals.bill_id',
|
|
|
|
'transaction_journals.id',
|
2016-10-05 22:26:38 -05:00
|
|
|
DB::raw('SUM(transactions.amount) AS journalAmount'),
|
2016-03-03 01:40:25 -06:00
|
|
|
]
|
|
|
|
);
|
2016-01-01 14:07:15 -06:00
|
|
|
|
|
|
|
return $set;
|
|
|
|
}
|
|
|
|
|
2015-04-05 11:20:06 -05:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-02-06 11:59:48 -06:00
|
|
|
public function getBills(): Collection
|
2015-04-05 11:20:06 -05:00
|
|
|
{
|
2015-04-06 02:15:59 -05:00
|
|
|
/** @var Collection $set */
|
2016-03-03 01:40:25 -06:00
|
|
|
$set = $this->user->bills()->orderBy('name', 'ASC')->get();
|
2015-04-06 02:15:59 -05:00
|
|
|
|
2015-07-01 03:52:42 -05:00
|
|
|
$set = $set->sortBy(
|
|
|
|
function (Bill $bill) {
|
2015-09-08 12:36:54 -05:00
|
|
|
|
|
|
|
$int = $bill->active == 1 ? 0 : 1;
|
|
|
|
|
2015-09-08 12:38:04 -05:00
|
|
|
return $int . strtolower($bill->name);
|
2015-07-01 03:52:42 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2015-04-06 02:15:59 -05:00
|
|
|
return $set;
|
2015-04-05 11:20:06 -05:00
|
|
|
}
|
|
|
|
|
2015-12-12 03:33:19 -06:00
|
|
|
/**
|
|
|
|
* @param Collection $accounts
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-02-06 11:59:48 -06:00
|
|
|
public function getBillsForAccounts(Collection $accounts): Collection
|
2015-12-12 03:33:19 -06:00
|
|
|
{
|
2016-09-16 06:29:56 -05:00
|
|
|
$fields = ['bills.id',
|
|
|
|
'bills.created_at',
|
|
|
|
'bills.updated_at',
|
|
|
|
'bills.deleted_at',
|
|
|
|
'bills.user_id',
|
|
|
|
'bills.name',
|
|
|
|
'bills.match',
|
|
|
|
'bills.amount_min',
|
|
|
|
'bills.amount_max',
|
|
|
|
'bills.date',
|
|
|
|
'bills.repeat_freq',
|
|
|
|
'bills.skip',
|
|
|
|
'bills.automatch',
|
|
|
|
'bills.active',
|
|
|
|
'bills.name_encrypted',
|
|
|
|
'bills.match_encrypted'];
|
2016-10-01 02:37:18 -05:00
|
|
|
$ids = $accounts->pluck('id')->toArray();
|
|
|
|
$set = $this->user->bills()
|
|
|
|
->leftJoin(
|
|
|
|
'transaction_journals', function (JoinClause $join) {
|
|
|
|
$join->on('transaction_journals.bill_id', '=', 'bills.id')->whereNull('transaction_journals.deleted_at');
|
|
|
|
}
|
|
|
|
)
|
|
|
|
->leftJoin(
|
|
|
|
'transactions', function (JoinClause $join) {
|
|
|
|
$join->on('transaction_journals.id', '=', 'transactions.transaction_journal_id')->where('transactions.amount', '<', 0);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
->whereIn('transactions.account_id', $ids)
|
|
|
|
->whereNull('transaction_journals.deleted_at')
|
|
|
|
->groupBy($fields)
|
|
|
|
->get($fields);
|
2015-12-12 03:33:19 -06:00
|
|
|
|
|
|
|
$set = $set->sortBy(
|
|
|
|
function (Bill $bill) {
|
|
|
|
|
|
|
|
$int = $bill->active == 1 ? 0 : 1;
|
|
|
|
|
|
|
|
return $int . strtolower($bill->name);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return $set;
|
|
|
|
}
|
|
|
|
|
2016-01-20 08:21:27 -06:00
|
|
|
/**
|
|
|
|
* Get the total amount of money paid for the users active bills in the date range given.
|
2016-10-20 23:26:12 -05:00
|
|
|
* This amount will be negative (they're expenses). This method is equal to
|
|
|
|
* getBillsUnpaidInRange. So the debug comments are gone.
|
2016-01-20 08:21:27 -06:00
|
|
|
*
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-02-06 11:59:48 -06:00
|
|
|
public function getBillsPaidInRange(Carbon $start, Carbon $end): string
|
2016-01-20 08:21:27 -06:00
|
|
|
{
|
2016-10-20 23:26:12 -05:00
|
|
|
$bills = $this->getActiveBills();
|
|
|
|
$sum = '0';
|
2016-01-20 08:21:27 -06:00
|
|
|
/** @var Bill $bill */
|
|
|
|
foreach ($bills as $bill) {
|
2016-10-20 23:26:12 -05:00
|
|
|
$currentStart = clone $start;
|
|
|
|
while ($currentStart <= $end) {
|
2016-10-20 23:33:56 -05:00
|
|
|
$nextExpectedMatch = $this->nextExpectedMatch($bill, $currentStart);
|
2016-10-20 23:26:12 -05:00
|
|
|
if ($nextExpectedMatch > $end) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/** @var Collection $set */
|
|
|
|
$set = $bill->transactionJournals()->after($currentStart)->before($nextExpectedMatch)->get(['transaction_journals.*']);
|
|
|
|
if ($set->count() > 0) {
|
|
|
|
$journalIds = $set->pluck('id')->toArray();
|
|
|
|
$amount = strval(Transaction::whereIn('transaction_journal_id', $journalIds)->where('amount', '<', 0)->sum('amount'));
|
|
|
|
$sum = bcadd($sum, $amount);
|
|
|
|
Log::info(
|
|
|
|
sprintf(
|
2016-10-20 23:33:56 -05:00
|
|
|
'getBillsPaidInRange: Bill "%s" is PAID in period %s to %s (%d transaction(s)), add %f to sum (sum is now %f).', $bill->name,
|
2016-10-20 23:26:12 -05:00
|
|
|
$currentStart->format('Y-m-d'),
|
|
|
|
$nextExpectedMatch->format('Y-m-d'),
|
|
|
|
$set->count(),
|
|
|
|
$amount, $sum
|
|
|
|
)
|
|
|
|
);
|
2016-10-21 00:29:25 -05:00
|
|
|
// add day to make sure we jump to the next period.
|
|
|
|
$nextExpectedMatch->addDay();
|
|
|
|
}
|
|
|
|
if ($currentStart->diffInDays($nextExpectedMatch) === 0) {
|
|
|
|
$nextExpectedMatch->addDay();
|
2016-10-20 23:26:12 -05:00
|
|
|
}
|
|
|
|
$currentStart = clone $nextExpectedMatch;
|
2016-01-20 08:21:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 23:26:12 -05:00
|
|
|
return $sum;
|
2016-01-20 08:21:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the total amount of money due for the users active bills in the date range given. This amount will be positive.
|
|
|
|
*
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-02-06 11:59:48 -06:00
|
|
|
public function getBillsUnpaidInRange(Carbon $start, Carbon $end): string
|
2016-01-20 08:21:27 -06:00
|
|
|
{
|
2016-10-20 23:26:12 -05:00
|
|
|
$bills = $this->getActiveBills();
|
|
|
|
$sum = '0';
|
2016-01-20 08:21:27 -06:00
|
|
|
/** @var Bill $bill */
|
|
|
|
foreach ($bills as $bill) {
|
2016-10-20 23:26:12 -05:00
|
|
|
Log::debug(sprintf('Now at bill "%s" (%s)', $bill->name, $bill->repeat_freq));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Start at 2016-10-01, see when we expect the bill to hit:
|
|
|
|
*/
|
|
|
|
$currentStart = clone $start;
|
|
|
|
Log::debug(sprintf('First currentstart is %s', $currentStart->format('Y-m-d')));
|
|
|
|
|
|
|
|
while ($currentStart <= $end) {
|
|
|
|
Log::debug(sprintf('Currentstart is now %s.', $currentStart->format('Y-m-d')));
|
2016-10-20 23:33:56 -05:00
|
|
|
$nextExpectedMatch = $this->nextExpectedMatch($bill, $currentStart);
|
2016-10-20 23:26:12 -05:00
|
|
|
Log::debug(sprintf('next Expected match after %s is %s', $currentStart->format('Y-m-d'), $nextExpectedMatch->format('Y-m-d')));
|
|
|
|
/*
|
|
|
|
* If $nextExpectedMatch is after $end, we continue:
|
|
|
|
*/
|
|
|
|
if ($nextExpectedMatch > $end) {
|
2016-10-20 23:38:00 -05:00
|
|
|
Log::debug(
|
|
|
|
sprintf('nextExpectedMatch %s is after %s, so we skip this bill now.', $nextExpectedMatch->format('Y-m-d'), $end->format('Y-m-d'))
|
|
|
|
);
|
2016-10-20 23:26:12 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If it is not, we search for transactions between $currentStart and $nextExpectedMatch
|
|
|
|
*/
|
|
|
|
$count = $bill->transactionJournals()->after($currentStart)->before($nextExpectedMatch)->count();
|
|
|
|
Log::debug(sprintf('%d transactions found', $count));
|
|
|
|
|
|
|
|
if ($count === 0) {
|
|
|
|
$average = bcdiv(bcadd($bill->amount_max, $bill->amount_min), '2', 4);
|
|
|
|
$sum = bcadd($sum, $average);
|
|
|
|
Log::info(
|
|
|
|
sprintf(
|
2016-10-20 23:33:56 -05:00
|
|
|
'getBillsUnpaidInRange: Bill "%s" is unpaid in period %s to %s, add %f to sum (sum is now %f).', $bill->name,
|
2016-10-20 23:26:12 -05:00
|
|
|
$currentStart->format('Y-m-d'),
|
|
|
|
$nextExpectedMatch->format('Y-m-d'),
|
|
|
|
$average, $sum
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2016-10-20 23:38:00 -05:00
|
|
|
if ($count != 0) {
|
2016-10-21 00:29:25 -05:00
|
|
|
Log::info(sprintf('getBillsUnpaidInRange: Bill "%s" is paid (%d) between %s and %s so ignore it.', $bill->name, $count,
|
|
|
|
$currentStart->format('Y-m-d'),
|
|
|
|
$nextExpectedMatch->format('Y-m-d')
|
|
|
|
));
|
|
|
|
// add day to make sure we jump to the next period.
|
|
|
|
$nextExpectedMatch->addDay();
|
|
|
|
}
|
|
|
|
|
|
|
|
// if $currentStart and $nextExpectedMatch are equal, add a day for good measure.
|
|
|
|
if ($currentStart->diffInDays($nextExpectedMatch) === 0) {
|
|
|
|
Log::debug(
|
|
|
|
sprintf(
|
|
|
|
'$nextExpectedMatch (%s) is equal to $currentStart (%s). Added a day.', $nextExpectedMatch->format('Y-m-d'),
|
|
|
|
$currentStart->format('Y-m-d')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$nextExpectedMatch->addDay();
|
2016-10-20 23:38:00 -05:00
|
|
|
}
|
2016-10-20 23:26:12 -05:00
|
|
|
|
|
|
|
Log::debug(sprintf('Currentstart (%s) has become %s.', $currentStart->format('Y-m-d'), $nextExpectedMatch->format('Y-m-d')));
|
2016-10-21 00:29:25 -05:00
|
|
|
|
2016-10-20 23:26:12 -05:00
|
|
|
$currentStart = clone $nextExpectedMatch;
|
2016-01-20 08:21:27 -06:00
|
|
|
}
|
2016-10-20 23:26:12 -05:00
|
|
|
Log::debug(sprintf('end of bill "%s"', $bill->name));
|
2016-01-20 08:21:27 -06:00
|
|
|
}
|
2016-10-20 23:26:12 -05:00
|
|
|
Log::debug(sprintf('Sum became %f', $sum));
|
2016-01-20 08:21:27 -06:00
|
|
|
|
2016-10-20 23:26:12 -05:00
|
|
|
return $sum;
|
2016-01-20 08:21:27 -06:00
|
|
|
}
|
|
|
|
|
2015-04-05 11:20:06 -05:00
|
|
|
/**
|
2015-12-27 13:07:49 -06:00
|
|
|
* This method also returns the amount of the journal in "journalAmount"
|
|
|
|
* for easy access.
|
|
|
|
*
|
2015-04-05 11:20:06 -05:00
|
|
|
* @param Bill $bill
|
|
|
|
*
|
2016-04-21 03:23:19 -05:00
|
|
|
* @param int $page
|
|
|
|
* @param int $pageSize
|
|
|
|
*
|
|
|
|
* @return LengthAwarePaginator|Collection
|
2015-04-05 11:20:06 -05:00
|
|
|
*/
|
2016-04-21 03:23:19 -05:00
|
|
|
public function getJournals(Bill $bill, int $page, int $pageSize = 50): LengthAwarePaginator
|
2015-04-05 11:20:06 -05:00
|
|
|
{
|
2016-04-21 03:23:19 -05:00
|
|
|
$offset = ($page - 1) * $pageSize;
|
2016-08-26 02:30:52 -05:00
|
|
|
$query = $bill->transactionJournals()
|
2016-04-21 03:23:19 -05:00
|
|
|
->expanded()
|
2016-05-11 00:57:16 -05:00
|
|
|
->sortCorrectly();
|
2016-04-21 03:23:19 -05:00
|
|
|
$count = $query->count();
|
2016-04-29 14:52:15 -05:00
|
|
|
$set = $query->take($pageSize)->offset($offset)->get(TransactionJournal::queryFields());
|
2016-04-21 03:23:19 -05:00
|
|
|
$paginator = new LengthAwarePaginator($set, $count, $pageSize, $page);
|
|
|
|
|
|
|
|
return $paginator;
|
2015-04-05 11:20:06 -05:00
|
|
|
}
|
|
|
|
|
2015-04-07 03:14:10 -05:00
|
|
|
/**
|
|
|
|
* Get all journals that were recorded on this bill between these dates.
|
2016-04-25 11:43:09 -05:00
|
|
|
*
|
2015-04-07 03:14:10 -05:00
|
|
|
* @param Bill $bill
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-02-06 11:59:48 -06:00
|
|
|
public function getJournalsInRange(Bill $bill, Carbon $start, Carbon $end): Collection
|
2015-04-07 03:14:10 -05:00
|
|
|
{
|
2016-08-26 02:30:52 -05:00
|
|
|
return $bill->transactionJournals()->before($end)->after($start)->get();
|
2015-04-07 03:14:10 -05:00
|
|
|
}
|
|
|
|
|
2016-07-23 14:37:06 -05:00
|
|
|
/**
|
|
|
|
* @param $bill
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getOverallAverage($bill): string
|
|
|
|
{
|
2016-08-26 02:30:52 -05:00
|
|
|
$journals = $bill->transactionJournals()->get();
|
2016-07-23 14:37:06 -05:00
|
|
|
$sum = '0';
|
|
|
|
$count = strval($journals->count());
|
|
|
|
/** @var TransactionJournal $journal */
|
|
|
|
foreach ($journals as $journal) {
|
|
|
|
$sum = bcadd($sum, TransactionJournal::amountPositive($journal));
|
|
|
|
}
|
|
|
|
$avg = '0';
|
|
|
|
if ($journals->count() > 0) {
|
|
|
|
$avg = bcdiv($sum, $count);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $avg;
|
|
|
|
}
|
|
|
|
|
2015-04-05 11:20:06 -05:00
|
|
|
/**
|
|
|
|
* @param Bill $bill
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-02-06 11:59:48 -06:00
|
|
|
public function getPossiblyRelatedJournals(Bill $bill): Collection
|
2015-04-05 11:20:06 -05:00
|
|
|
{
|
2016-01-20 08:21:27 -06:00
|
|
|
$set = new Collection(
|
|
|
|
DB::table('transactions')->where('amount', '>', 0)->where('amount', '>=', $bill->amount_min)->where('amount', '<=', $bill->amount_max)
|
|
|
|
->get(['transaction_journal_id'])
|
2015-04-05 11:20:06 -05:00
|
|
|
);
|
2015-12-30 02:16:58 -06:00
|
|
|
$ids = $set->pluck('transaction_journal_id')->toArray();
|
2015-04-05 11:20:06 -05:00
|
|
|
|
|
|
|
$journals = new Collection;
|
|
|
|
if (count($ids) > 0) {
|
2016-08-26 02:30:52 -05:00
|
|
|
$journals = $this->user->transactionJournals()->transactionTypes([TransactionType::WITHDRAWAL])->whereIn('transaction_journals.id', $ids)->get(
|
2015-10-03 10:55:08 -05:00
|
|
|
['transaction_journals.*']
|
|
|
|
);
|
2015-04-05 11:20:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $journals;
|
|
|
|
}
|
|
|
|
|
2015-03-03 10:40:17 -06:00
|
|
|
/**
|
|
|
|
* Every bill repeats itself weekly, monthly or yearly (or whatever). This method takes a date-range (usually the view-range of Firefly itself)
|
|
|
|
* and returns date ranges that fall within the given range; those ranges are the bills expected. When a bill is due on the 14th of the month and
|
|
|
|
* you give 1st and the 31st of that month as argument, you'll get one response, matching the range of your bill.
|
|
|
|
*
|
|
|
|
* @param Bill $bill
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
2016-02-06 11:59:48 -06:00
|
|
|
* @return array
|
2015-03-03 10:40:17 -06:00
|
|
|
*/
|
2016-02-06 11:59:48 -06:00
|
|
|
public function getRanges(Bill $bill, Carbon $start, Carbon $end): array
|
2015-03-03 10:40:17 -06:00
|
|
|
{
|
2016-04-18 12:33:02 -05:00
|
|
|
$startOfBill = Navigation::startOfPeriod($start, $bill->repeat_freq);
|
2015-03-03 10:40:17 -06:00
|
|
|
|
|
|
|
|
|
|
|
// all periods of this bill up until the current period:
|
|
|
|
$billStarts = [];
|
|
|
|
while ($startOfBill < $end) {
|
|
|
|
|
|
|
|
$endOfBill = Navigation::endOfPeriod($startOfBill, $bill->repeat_freq);
|
|
|
|
|
|
|
|
$billStarts[] = [
|
|
|
|
'start' => clone $startOfBill,
|
|
|
|
'end' => clone $endOfBill,
|
|
|
|
];
|
|
|
|
// actually the next one:
|
|
|
|
$startOfBill = Navigation::addPeriod($startOfBill, $bill->repeat_freq, $bill->skip);
|
|
|
|
|
|
|
|
}
|
|
|
|
// for each
|
|
|
|
$validRanges = [];
|
|
|
|
foreach ($billStarts as $dateEntry) {
|
|
|
|
if ($dateEntry['end'] > $start && $dateEntry['start'] < $end) {
|
|
|
|
// count transactions for bill in this range (not relevant yet!):
|
|
|
|
$validRanges[] = $dateEntry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $validRanges;
|
|
|
|
}
|
|
|
|
|
2016-07-23 14:37:06 -05:00
|
|
|
/**
|
|
|
|
* @param Bill $bill
|
|
|
|
* @param Carbon $date
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getYearAverage(Bill $bill, Carbon $date): string
|
|
|
|
{
|
2016-08-26 02:30:52 -05:00
|
|
|
$journals = $bill->transactionJournals()
|
2016-07-23 14:37:06 -05:00
|
|
|
->where('date', '>=', $date->year . '-01-01')
|
|
|
|
->where('date', '<=', $date->year . '-12-31')
|
|
|
|
->get();
|
|
|
|
$sum = '0';
|
|
|
|
$count = strval($journals->count());
|
|
|
|
/** @var TransactionJournal $journal */
|
|
|
|
foreach ($journals as $journal) {
|
|
|
|
$sum = bcadd($sum, TransactionJournal::amountPositive($journal));
|
|
|
|
}
|
|
|
|
$avg = '0';
|
|
|
|
if ($journals->count() > 0) {
|
|
|
|
$avg = bcdiv($sum, $count);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $avg;
|
|
|
|
}
|
|
|
|
|
2015-04-05 11:20:06 -05:00
|
|
|
/**
|
|
|
|
* @param Bill $bill
|
|
|
|
*
|
2016-02-06 11:59:48 -06:00
|
|
|
* @return \Carbon\Carbon
|
2015-04-05 11:20:06 -05:00
|
|
|
*/
|
2016-02-06 11:59:48 -06:00
|
|
|
public function lastFoundMatch(Bill $bill): Carbon
|
2015-04-05 11:20:06 -05:00
|
|
|
{
|
2016-08-26 02:30:52 -05:00
|
|
|
$last = $bill->transactionJournals()->orderBy('date', 'DESC')->first();
|
2015-04-05 11:20:06 -05:00
|
|
|
if ($last) {
|
|
|
|
return $last->date;
|
|
|
|
}
|
|
|
|
|
2016-02-06 11:59:48 -06:00
|
|
|
return Carbon::now()->addDays(2); // in the future!
|
2015-04-05 11:20:06 -05:00
|
|
|
}
|
|
|
|
|
2015-02-25 08:19:14 -06:00
|
|
|
/**
|
2016-10-20 23:26:12 -05:00
|
|
|
* Given a bill and a date, this method will tell you at which moment this bill expects its next
|
|
|
|
* transaction. Whether or not it is there already, is not relevant.
|
|
|
|
*
|
|
|
|
* @param Bill $bill
|
|
|
|
* @param Carbon $date
|
|
|
|
*
|
|
|
|
* @return \Carbon\Carbon
|
|
|
|
*/
|
|
|
|
public function nextDateMatch(Bill $bill, Carbon $date): Carbon
|
|
|
|
{
|
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty($bill->id);
|
|
|
|
$cache->addProperty('nextDateMatch');
|
|
|
|
$cache->addProperty($date);
|
|
|
|
if ($cache->has()) {
|
|
|
|
return $cache->get();
|
|
|
|
}
|
|
|
|
// find the most recent date for this bill NOT in the future. Cache this date:
|
|
|
|
$start = clone $bill->date;
|
|
|
|
Log::debug('NextDatematch: Start is ' . $start->format('Y-m-d'));
|
|
|
|
|
|
|
|
|
|
|
|
while ($start <= $date) {
|
|
|
|
$start = Navigation::addPeriod($start, $bill->repeat_freq, $bill->skip);
|
|
|
|
Log::debug('NextDateMatch: Start is now ' . $start->format('Y-m-d'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$end = Navigation::addPeriod($start, $bill->repeat_freq, $bill->skip);
|
|
|
|
Log::debug('NextDateMatch: Final start is ' . $start->format('Y-m-d'));
|
|
|
|
Log::debug('NextDateMatch: Matching end is ' . $end->format('Y-m-d'));
|
|
|
|
$cache->store($start);
|
|
|
|
|
|
|
|
return $start;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given the date in $date, this method will return a moment in the future where the bill is expected to be paid.
|
|
|
|
*
|
2016-10-20 14:40:45 -05:00
|
|
|
* @param Bill $bill
|
|
|
|
* @param Carbon $date
|
2015-02-25 08:19:14 -06:00
|
|
|
*
|
2016-10-20 14:40:45 -05:00
|
|
|
* @return Carbon
|
2015-02-25 08:19:14 -06:00
|
|
|
*/
|
2016-10-20 14:40:45 -05:00
|
|
|
public function nextExpectedMatch(Bill $bill, Carbon $date): Carbon
|
2015-02-25 08:19:14 -06:00
|
|
|
{
|
2016-10-20 14:40:45 -05:00
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty($bill->id);
|
|
|
|
$cache->addProperty('nextExpectedMatch');
|
2016-10-20 23:26:12 -05:00
|
|
|
$cache->addProperty($date);
|
2016-10-20 14:40:45 -05:00
|
|
|
if ($cache->has()) {
|
|
|
|
return $cache->get();
|
2015-02-25 08:19:14 -06:00
|
|
|
}
|
2016-10-20 14:40:45 -05:00
|
|
|
// find the most recent date for this bill NOT in the future. Cache this date:
|
|
|
|
$start = clone $bill->date;
|
2016-10-20 23:41:33 -05:00
|
|
|
Log::debug('nextExpectedMatch: Start is ' . $start->format('Y-m-d'));
|
2016-10-20 14:40:45 -05:00
|
|
|
|
2016-10-21 00:29:25 -05:00
|
|
|
while ($start < $date) {
|
|
|
|
Log::debug(sprintf('$start (%s) < $date (%s)', $start->format('Y-m-d'), $date->format('Y-m-d')));
|
2016-10-20 14:40:45 -05:00
|
|
|
$start = Navigation::addPeriod($start, $bill->repeat_freq, $bill->skip);
|
|
|
|
Log::debug('Start is now ' . $start->format('Y-m-d'));
|
2016-10-21 00:29:25 -05:00
|
|
|
}
|
2016-10-20 14:40:45 -05:00
|
|
|
|
|
|
|
$end = Navigation::addPeriod($start, $bill->repeat_freq, $bill->skip);
|
|
|
|
|
|
|
|
// see if the bill was paid in this period.
|
|
|
|
$journalCount = $bill->transactionJournals()->before($end)->after($start)->count();
|
|
|
|
|
|
|
|
if ($journalCount > 0) {
|
|
|
|
// this period had in fact a bill. The new start is the current end, and we create a new end.
|
2016-10-20 23:26:12 -05:00
|
|
|
Log::debug(sprintf('Journal count is %d, so start becomes %s', $journalCount, $end->format('Y-m-d')));
|
2016-10-20 14:40:45 -05:00
|
|
|
$start = clone $end;
|
2016-10-21 00:29:25 -05:00
|
|
|
$end = Navigation::addPeriod($start, $bill->repeat_freq, $bill->skip);
|
2015-02-25 08:19:14 -06:00
|
|
|
}
|
2016-10-21 00:29:25 -05:00
|
|
|
Log::debug('nextExpectedMatch: Final start is ' . $start->format('Y-m-d'));
|
|
|
|
Log::debug('nextExpectedMatch: Matching end is ' . $end->format('Y-m-d'));
|
|
|
|
|
2016-10-20 14:40:45 -05:00
|
|
|
$cache->store($start);
|
2015-02-25 08:19:14 -06:00
|
|
|
|
2016-10-20 14:40:45 -05:00
|
|
|
return $start;
|
2015-02-25 08:19:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Bill $bill
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
2016-02-06 11:59:48 -06:00
|
|
|
* @return bool
|
2015-02-25 08:19:14 -06:00
|
|
|
*/
|
2016-02-06 11:59:48 -06:00
|
|
|
public function scan(Bill $bill, TransactionJournal $journal): bool
|
2015-02-25 08:19:14 -06:00
|
|
|
{
|
2015-11-20 13:13:10 -06:00
|
|
|
/*
|
|
|
|
* Can only support withdrawals.
|
|
|
|
*/
|
2015-12-09 18:39:50 -06:00
|
|
|
if (false === $journal->isWithdrawal()) {
|
2015-11-20 13:13:10 -06:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-15 08:24:23 -05:00
|
|
|
$destinationAccounts = TransactionJournal::destinationAccountList($journal);
|
|
|
|
$sourceAccounts = TransactionJournal::sourceAccountList($journal);
|
|
|
|
$matches = explode(',', $bill->match);
|
|
|
|
$description = strtolower($journal->description) . ' ';
|
|
|
|
$description .= strtolower(join(' ', $destinationAccounts->pluck('name')->toArray()));
|
|
|
|
$description .= strtolower(join(' ', $sourceAccounts->pluck('name')->toArray()));
|
2016-03-29 08:24:40 -05:00
|
|
|
|
2015-06-07 01:13:19 -05:00
|
|
|
$wordMatch = $this->doWordMatch($matches, $description);
|
2016-03-02 13:11:28 -06:00
|
|
|
$amountMatch = $this->doAmountMatch(TransactionJournal::amountPositive($journal), $bill->amount_min, $bill->amount_max);
|
2015-02-25 08:19:14 -06:00
|
|
|
|
2015-11-20 13:13:10 -06:00
|
|
|
|
2015-02-25 08:19:14 -06:00
|
|
|
/*
|
|
|
|
* If both, update!
|
|
|
|
*/
|
|
|
|
if ($wordMatch && $amountMatch) {
|
|
|
|
$journal->bill()->associate($bill);
|
|
|
|
$journal->save();
|
2015-06-07 01:13:19 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ($bill->id == $journal->bill_id) {
|
|
|
|
// if no match, but bill used to match, remove it:
|
|
|
|
$journal->bill_id = null;
|
|
|
|
$journal->save();
|
|
|
|
|
|
|
|
return true;
|
2015-02-25 08:19:14 -06:00
|
|
|
}
|
2015-06-07 01:13:19 -05:00
|
|
|
|
2015-06-13 03:02:36 -05:00
|
|
|
return false;
|
|
|
|
|
2015-02-25 08:19:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Bill
|
|
|
|
*/
|
2016-02-06 11:59:48 -06:00
|
|
|
public function store(array $data): Bill
|
2015-02-25 08:19:14 -06:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
$bill = Bill::create(
|
|
|
|
[
|
|
|
|
'name' => $data['name'],
|
|
|
|
'match' => $data['match'],
|
|
|
|
'amount_min' => $data['amount_min'],
|
|
|
|
'user_id' => $data['user'],
|
|
|
|
'amount_max' => $data['amount_max'],
|
|
|
|
'date' => $data['date'],
|
|
|
|
'repeat_freq' => $data['repeat_freq'],
|
|
|
|
'skip' => $data['skip'],
|
|
|
|
'automatch' => $data['automatch'],
|
|
|
|
'active' => $data['active'],
|
|
|
|
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
return $bill;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Bill $bill
|
|
|
|
* @param array $data
|
|
|
|
*
|
2015-05-26 13:57:31 -05:00
|
|
|
* @return Bill
|
2015-02-25 08:19:14 -06:00
|
|
|
*/
|
2016-02-06 11:59:48 -06:00
|
|
|
public function update(Bill $bill, array $data): Bill
|
2015-02-25 08:19:14 -06:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
$bill->name = $data['name'];
|
|
|
|
$bill->match = $data['match'];
|
|
|
|
$bill->amount_min = $data['amount_min'];
|
|
|
|
$bill->amount_max = $data['amount_max'];
|
|
|
|
$bill->date = $data['date'];
|
|
|
|
$bill->repeat_freq = $data['repeat_freq'];
|
|
|
|
$bill->skip = $data['skip'];
|
|
|
|
$bill->automatch = $data['automatch'];
|
|
|
|
$bill->active = $data['active'];
|
|
|
|
$bill->save();
|
|
|
|
|
|
|
|
return $bill;
|
|
|
|
}
|
2015-06-07 01:13:19 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param float $amount
|
|
|
|
* @param float $min
|
|
|
|
* @param float $max
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-02-06 11:59:48 -06:00
|
|
|
protected function doAmountMatch($amount, $min, $max): bool
|
2015-06-07 01:13:19 -05:00
|
|
|
{
|
|
|
|
if ($amount >= $min && $amount <= $max) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-09 02:41:54 -05:00
|
|
|
|
|
|
|
/**
|
2016-01-20 08:21:27 -06:00
|
|
|
* @param array $matches
|
|
|
|
* @param $description
|
2015-07-09 02:41:54 -05:00
|
|
|
*
|
2016-01-20 08:21:27 -06:00
|
|
|
* @return bool
|
2015-07-09 02:41:54 -05:00
|
|
|
*/
|
2016-02-06 11:59:48 -06:00
|
|
|
protected function doWordMatch(array $matches, $description): bool
|
2015-07-09 02:41:54 -05:00
|
|
|
{
|
2016-01-20 08:21:27 -06:00
|
|
|
$wordMatch = false;
|
|
|
|
$count = 0;
|
|
|
|
foreach ($matches as $word) {
|
|
|
|
if (!(strpos($description, strtolower($word)) === false)) {
|
|
|
|
$count++;
|
2015-07-09 02:41:54 -05:00
|
|
|
}
|
|
|
|
}
|
2016-01-20 08:21:27 -06:00
|
|
|
if ($count >= count($matches)) {
|
|
|
|
$wordMatch = true;
|
2015-12-27 10:29:41 -06:00
|
|
|
}
|
|
|
|
|
2016-01-20 08:21:27 -06:00
|
|
|
return $wordMatch;
|
2015-12-26 02:39:35 -06:00
|
|
|
}
|
2015-02-25 08:19:14 -06:00
|
|
|
}
|