2015-02-25 15:19:14 +01:00
|
|
|
<?php
|
2016-05-20 12:41:23 +02:00
|
|
|
/**
|
|
|
|
* BillRepository.php
|
2017-10-21 08:40:00 +02:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-05-20 12:41:23 +02:00
|
|
|
*
|
2017-10-21 08:40:00 +02:00
|
|
|
* This file is part of Firefly III.
|
2016-10-05 06:52:15 +02:00
|
|
|
*
|
2017-10-21 08:40:00 +02: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 14:44:05 +01:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-05-20 12:41:23 +02:00
|
|
|
*/
|
2017-04-09 07:44:22 +02:00
|
|
|
declare(strict_types=1);
|
2015-02-25 15:19:14 +01:00
|
|
|
|
|
|
|
namespace FireflyIII\Repositories\Bill;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2015-05-08 14:00:49 +02:00
|
|
|
use DB;
|
2018-02-22 20:07:14 +01:00
|
|
|
use FireflyIII\Factory\BillFactory;
|
2015-02-25 15:19:14 +01:00
|
|
|
use FireflyIII\Models\Bill;
|
2018-04-29 07:46:03 +02:00
|
|
|
use FireflyIII\Models\Note;
|
2016-10-21 06:26:12 +02:00
|
|
|
use FireflyIII\Models\Transaction;
|
2015-02-25 15:19:14 +01:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2018-02-25 19:09:05 +01:00
|
|
|
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
2018-02-22 20:07:14 +01:00
|
|
|
use FireflyIII\Services\Internal\Destroy\BillDestroyService;
|
|
|
|
use FireflyIII\Services\Internal\Update\BillUpdateService;
|
2016-10-20 21:40:45 +02:00
|
|
|
use FireflyIII\Support\CacheProperties;
|
2016-03-03 08:40:25 +01:00
|
|
|
use FireflyIII\User;
|
2015-12-26 09:39:35 +01:00
|
|
|
use Illuminate\Database\Query\JoinClause;
|
2018-02-06 18:11:33 +01:00
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
2015-04-05 18:20:06 +02:00
|
|
|
use Illuminate\Support\Collection;
|
2016-10-20 21:40:45 +02:00
|
|
|
use Log;
|
2015-02-25 15:19:14 +01:00
|
|
|
|
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Class BillRepository.
|
2018-07-25 19:43:02 +02:00
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
2015-02-25 15:19:14 +01:00
|
|
|
*/
|
|
|
|
class BillRepository implements BillRepositoryInterface
|
|
|
|
{
|
2016-03-03 08:40:25 +01:00
|
|
|
/** @var User */
|
|
|
|
private $user;
|
|
|
|
|
2018-09-03 08:41:03 +02:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2018-12-15 07:59:02 +01:00
|
|
|
if ('testing' === config('app.env')) {
|
2019-06-07 18:20:15 +02:00
|
|
|
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
|
2018-09-03 08:41:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-05 18:20:06 +02:00
|
|
|
/**
|
|
|
|
* @param Bill $bill
|
|
|
|
*
|
2016-04-06 09:27:45 +02:00
|
|
|
* @return bool
|
2017-12-22 18:32:43 +01:00
|
|
|
*
|
2018-04-02 15:10:40 +02:00
|
|
|
|
2015-04-05 18:20:06 +02:00
|
|
|
*/
|
2016-02-06 18:59:48 +01:00
|
|
|
public function destroy(Bill $bill): bool
|
2015-04-05 18:20:06 +02:00
|
|
|
{
|
2018-02-22 20:07:14 +01:00
|
|
|
/** @var BillDestroyService $service */
|
|
|
|
$service = app(BillDestroyService::class);
|
|
|
|
$service->destroy($bill);
|
2016-02-06 18:59:48 +01:00
|
|
|
|
|
|
|
return true;
|
2015-04-05 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
2016-04-01 13:07:19 +02:00
|
|
|
/**
|
|
|
|
* Find a bill by ID.
|
|
|
|
*
|
|
|
|
* @param int $billId
|
|
|
|
*
|
|
|
|
* @return Bill
|
|
|
|
*/
|
2018-02-16 15:19:19 +01:00
|
|
|
public function find(int $billId): ?Bill
|
2016-04-01 13:07:19 +02:00
|
|
|
{
|
2018-04-02 15:10:40 +02:00
|
|
|
return $this->user->bills()->find($billId);
|
2016-04-01 13:07:19 +02:00
|
|
|
}
|
|
|
|
|
2019-03-17 17:05:16 +01:00
|
|
|
/**
|
|
|
|
* Find bill by parameters.
|
|
|
|
*
|
|
|
|
* @param int|null $billId
|
|
|
|
* @param string|null $billName
|
|
|
|
*
|
|
|
|
* @return Bill|null
|
|
|
|
*/
|
2019-05-04 20:58:43 +02:00
|
|
|
public function findBill(?int $billId, ?string $billName): ?Bill
|
2019-03-17 17:05:16 +01:00
|
|
|
{
|
|
|
|
if (null !== $billId) {
|
|
|
|
$searchResult = $this->find((int)$billId);
|
|
|
|
if (null !== $searchResult) {
|
|
|
|
Log::debug(sprintf('Found bill based on #%d, will return it.', $billId));
|
|
|
|
|
|
|
|
return $searchResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (null !== $billName) {
|
|
|
|
$searchResult = $this->findByName((string)$billName);
|
|
|
|
if (null !== $searchResult) {
|
|
|
|
Log::debug(sprintf('Found bill based on "%s", will return it.', $billName));
|
|
|
|
|
|
|
|
return $searchResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Log::debug('Found nothing');
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-07-23 21:37:06 +02:00
|
|
|
/**
|
|
|
|
* Find a bill by name.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return Bill
|
|
|
|
*/
|
2018-02-16 15:19:19 +01:00
|
|
|
public function findByName(string $name): ?Bill
|
2016-07-23 21:37:06 +02:00
|
|
|
{
|
2016-08-11 10:21:32 +02:00
|
|
|
$bills = $this->user->bills()->get(['bills.*']);
|
2016-07-23 21:37:06 +02:00
|
|
|
|
2019-08-03 19:17:59 +02:00
|
|
|
// TODO no longer need to loop like this
|
|
|
|
|
2016-07-23 21:37:06 +02:00
|
|
|
/** @var Bill $bill */
|
|
|
|
foreach ($bills as $bill) {
|
|
|
|
if ($bill->name === $name) {
|
|
|
|
return $bill;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-16 15:19:19 +01:00
|
|
|
return null;
|
2016-07-23 21:37:06 +02:00
|
|
|
}
|
|
|
|
|
2016-01-20 15:21:27 +01:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-02-06 18:59:48 +01:00
|
|
|
public function getActiveBills(): Collection
|
2016-01-20 15:21:27 +01:00
|
|
|
{
|
|
|
|
/** @var Collection $set */
|
2016-03-03 08:40:25 +01:00
|
|
|
$set = $this->user->bills()
|
|
|
|
->where('active', 1)
|
2019-05-04 20:58:43 +02:00
|
|
|
->orderBy('bills.name', 'ASC')
|
|
|
|
->get(['bills.*', DB::raw('((bills.amount_min + bills.amount_max) / 2) AS expectedAmount'),]);
|
2016-01-20 15:21:27 +01:00
|
|
|
|
|
|
|
return $set;
|
|
|
|
}
|
|
|
|
|
2018-12-09 13:09:43 +01:00
|
|
|
/**
|
|
|
|
* Get all attachments.
|
|
|
|
*
|
|
|
|
* @param Bill $bill
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getAttachments(Bill $bill): Collection
|
|
|
|
{
|
|
|
|
return $bill->attachments()->get();
|
|
|
|
}
|
|
|
|
|
2015-04-05 18:20:06 +02:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-02-06 18:59:48 +01:00
|
|
|
public function getBills(): Collection
|
2015-04-05 18:20:06 +02:00
|
|
|
{
|
2015-04-06 09:15:59 +02:00
|
|
|
/** @var Collection $set */
|
2019-05-04 20:58:43 +02:00
|
|
|
$set = $this->user->bills()->orderBy('active', 'DESC')->orderBy('name', 'ASC')->get();
|
2015-07-01 10:52:42 +02:00
|
|
|
|
2015-04-06 09:15:59 +02:00
|
|
|
return $set;
|
2015-04-05 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
2015-12-12 10:33:19 +01:00
|
|
|
/**
|
|
|
|
* @param Collection $accounts
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-02-06 18:59:48 +01:00
|
|
|
public function getBillsForAccounts(Collection $accounts): Collection
|
2015-12-12 10:33:19 +01:00
|
|
|
{
|
2018-07-23 21:49:15 +02:00
|
|
|
$fields = ['bills.id', 'bills.created_at', 'bills.updated_at', 'bills.deleted_at', 'bills.user_id', 'bills.name', 'bills.match', 'bills.amount_min',
|
2018-12-09 13:09:43 +01:00
|
|
|
'bills.amount_max', 'bills.date', 'bills.transaction_currency_id', 'bills.repeat_freq', 'bills.skip', 'bills.automatch', 'bills.active',
|
|
|
|
'bills.name_encrypted',
|
2017-11-15 12:25:49 +01:00
|
|
|
'bills.match_encrypted',];
|
2016-10-01 09:37:18 +02:00
|
|
|
$ids = $accounts->pluck('id')->toArray();
|
|
|
|
$set = $this->user->bills()
|
|
|
|
->leftJoin(
|
2017-11-15 10:52:29 +01:00
|
|
|
'transaction_journals',
|
2019-08-16 21:21:38 +02:00
|
|
|
static function (JoinClause $join) {
|
2017-11-15 10:52:29 +01:00
|
|
|
$join->on('transaction_journals.bill_id', '=', 'bills.id')->whereNull('transaction_journals.deleted_at');
|
|
|
|
}
|
2016-10-01 09:37:18 +02:00
|
|
|
)
|
|
|
|
->leftJoin(
|
2017-11-15 10:52:29 +01:00
|
|
|
'transactions',
|
2019-08-16 21:21:38 +02:00
|
|
|
static function (JoinClause $join) {
|
2017-11-15 10:52:29 +01:00
|
|
|
$join->on('transaction_journals.id', '=', 'transactions.transaction_journal_id')->where('transactions.amount', '<', 0);
|
|
|
|
}
|
2016-10-01 09:37:18 +02:00
|
|
|
)
|
|
|
|
->whereIn('transactions.account_id', $ids)
|
|
|
|
->whereNull('transaction_journals.deleted_at')
|
2019-05-04 20:58:43 +02:00
|
|
|
->orderBy('bills.active', 'DESC')
|
|
|
|
->orderBy('bills.name', 'ASC')
|
2016-10-01 09:37:18 +02:00
|
|
|
->groupBy($fields)
|
|
|
|
->get($fields);
|
2015-12-12 10:33:19 +01:00
|
|
|
|
|
|
|
return $set;
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:21:27 +01:00
|
|
|
/**
|
|
|
|
* Get the total amount of money paid for the users active bills in the date range given.
|
2016-10-21 06:26:12 +02:00
|
|
|
* This amount will be negative (they're expenses). This method is equal to
|
|
|
|
* getBillsUnpaidInRange. So the debug comments are gone.
|
2016-01-20 15:21:27 +01:00
|
|
|
*
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-02-06 18:59:48 +01:00
|
|
|
public function getBillsPaidInRange(Carbon $start, Carbon $end): string
|
2016-01-20 15:21:27 +01:00
|
|
|
{
|
2016-10-21 06:26:12 +02:00
|
|
|
$bills = $this->getActiveBills();
|
|
|
|
$sum = '0';
|
2016-01-20 15:21:27 +01:00
|
|
|
/** @var Bill $bill */
|
|
|
|
foreach ($bills as $bill) {
|
2016-10-21 13:20:51 +02:00
|
|
|
/** @var Collection $set */
|
|
|
|
$set = $bill->transactionJournals()->after($start)->before($end)->get(['transaction_journals.*']);
|
|
|
|
if ($set->count() > 0) {
|
|
|
|
$journalIds = $set->pluck('id')->toArray();
|
2018-04-02 15:10:40 +02:00
|
|
|
$amount = (string)Transaction::whereIn('transaction_journal_id', $journalIds)->where('amount', '<', 0)->sum('amount');
|
2016-10-21 13:20:51 +02:00
|
|
|
$sum = bcadd($sum, $amount);
|
|
|
|
Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f', $amount, $sum));
|
2016-01-20 15:21:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-21 06:26:12 +02:00
|
|
|
return $sum;
|
2016-01-20 15:21:27 +01:00
|
|
|
}
|
|
|
|
|
2018-08-28 05:21:23 +02:00
|
|
|
/**
|
|
|
|
* Get the total amount of money paid for the users active bills in the date range given,
|
|
|
|
* grouped per currency.
|
|
|
|
*
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getBillsPaidInRangePerCurrency(Carbon $start, Carbon $end): array
|
|
|
|
{
|
|
|
|
$bills = $this->getActiveBills();
|
|
|
|
$return = [];
|
|
|
|
/** @var Bill $bill */
|
|
|
|
foreach ($bills as $bill) {
|
|
|
|
/** @var Collection $set */
|
|
|
|
$set = $bill->transactionJournals()->after($start)->before($end)->get(['transaction_journals.*']);
|
|
|
|
$currencyId = (int)$bill->transaction_currency_id;
|
|
|
|
if ($set->count() > 0) {
|
|
|
|
$journalIds = $set->pluck('id')->toArray();
|
|
|
|
$amount = (string)Transaction::whereIn('transaction_journal_id', $journalIds)->where('amount', '<', 0)->sum('amount');
|
|
|
|
$return[$currencyId] = $return[$currencyId] ?? '0';
|
|
|
|
$return[$currencyId] = bcadd($amount, $return[$currencyId]);
|
|
|
|
Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f (currency %d)', $amount, $return[$currencyId], $currencyId));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:21:27 +01: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 18:59:48 +01:00
|
|
|
public function getBillsUnpaidInRange(Carbon $start, Carbon $end): string
|
2016-01-20 15:21:27 +01:00
|
|
|
{
|
2016-10-21 06:26:12 +02:00
|
|
|
$bills = $this->getActiveBills();
|
|
|
|
$sum = '0';
|
2016-01-20 15:21:27 +01:00
|
|
|
/** @var Bill $bill */
|
|
|
|
foreach ($bills as $bill) {
|
2016-10-21 13:20:51 +02:00
|
|
|
Log::debug(sprintf('Now at bill #%d (%s)', $bill->id, $bill->name));
|
|
|
|
$dates = $this->getPayDatesInRange($bill, $start, $end);
|
|
|
|
$count = $bill->transactionJournals()->after($start)->before($end)->count();
|
|
|
|
$total = $dates->count() - $count;
|
2016-10-21 06:26:12 +02:00
|
|
|
|
2016-10-21 13:20:51 +02:00
|
|
|
Log::debug(sprintf('Dates = %d, journalCount = %d, total = %d', $dates->count(), $count, $total));
|
|
|
|
|
|
|
|
if ($total > 0) {
|
|
|
|
$average = bcdiv(bcadd($bill->amount_max, $bill->amount_min), '2');
|
2018-04-02 15:10:40 +02:00
|
|
|
$multi = bcmul($average, (string)$total);
|
2016-10-21 13:20:51 +02:00
|
|
|
$sum = bcadd($sum, $multi);
|
|
|
|
Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f', $multi, $sum));
|
2016-01-20 15:21:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-21 06:26:12 +02:00
|
|
|
return $sum;
|
2016-01-20 15:21:27 +01:00
|
|
|
}
|
|
|
|
|
2018-08-28 05:21:23 +02:00
|
|
|
/**
|
|
|
|
* Get the total amount of money due for the users active bills in the date range given.
|
|
|
|
*
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getBillsUnpaidInRangePerCurrency(Carbon $start, Carbon $end): array
|
|
|
|
{
|
|
|
|
$bills = $this->getActiveBills();
|
|
|
|
$return = [];
|
|
|
|
/** @var Bill $bill */
|
|
|
|
foreach ($bills as $bill) {
|
|
|
|
Log::debug(sprintf('Now at bill #%d (%s)', $bill->id, $bill->name));
|
|
|
|
$dates = $this->getPayDatesInRange($bill, $start, $end);
|
|
|
|
$count = $bill->transactionJournals()->after($start)->before($end)->count();
|
|
|
|
$total = $dates->count() - $count;
|
|
|
|
$currencyId = (int)$bill->transaction_currency_id;
|
|
|
|
|
|
|
|
Log::debug(sprintf('Dates = %d, journalCount = %d, total = %d', $dates->count(), $count, $total));
|
|
|
|
|
|
|
|
if ($total > 0) {
|
|
|
|
$average = bcdiv(bcadd($bill->amount_max, $bill->amount_min), '2');
|
|
|
|
$multi = bcmul($average, (string)$total);
|
|
|
|
$return[$currencyId] = $return[$currencyId] ?? '0';
|
|
|
|
$return[$currencyId] = bcadd($return[$currencyId], $multi);
|
|
|
|
Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f (for currency %d)', $multi, $return[$currencyId], $currencyId));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2018-05-07 20:35:14 +02:00
|
|
|
/**
|
|
|
|
* Get all bills with these ID's.
|
|
|
|
*
|
|
|
|
* @param array $billIds
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getByIds(array $billIds): Collection
|
|
|
|
{
|
|
|
|
return $this->user->bills()->whereIn('id', $billIds)->get();
|
|
|
|
}
|
|
|
|
|
2018-04-29 07:46:03 +02:00
|
|
|
/**
|
|
|
|
* Get text or return empty string.
|
|
|
|
*
|
|
|
|
* @param Bill $bill
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getNoteText(Bill $bill): string
|
|
|
|
{
|
|
|
|
/** @var Note $note */
|
|
|
|
$note = $bill->notes()->first();
|
|
|
|
if (null !== $note) {
|
|
|
|
return (string)$note->text;
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2016-07-23 21:37:06 +02:00
|
|
|
/**
|
2016-10-23 14:56:05 +02:00
|
|
|
* @param Bill $bill
|
2016-07-23 21:37:06 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-10-23 14:56:05 +02:00
|
|
|
public function getOverallAverage(Bill $bill): string
|
2016-07-23 21:37:06 +02:00
|
|
|
{
|
2018-02-25 19:09:05 +01:00
|
|
|
/** @var JournalRepositoryInterface $repos */
|
|
|
|
$repos = app(JournalRepositoryInterface::class);
|
|
|
|
$repos->setUser($this->user);
|
2016-08-26 09:30:52 +02:00
|
|
|
$journals = $bill->transactionJournals()->get();
|
2016-07-23 21:37:06 +02:00
|
|
|
$sum = '0';
|
2018-04-02 15:10:40 +02:00
|
|
|
$count = (string)$journals->count();
|
2016-07-23 21:37:06 +02:00
|
|
|
/** @var TransactionJournal $journal */
|
|
|
|
foreach ($journals as $journal) {
|
2018-02-25 19:09:05 +01:00
|
|
|
$sum = bcadd($sum, $repos->getJournalTotal($journal));
|
2016-07-23 21:37:06 +02:00
|
|
|
}
|
|
|
|
$avg = '0';
|
|
|
|
if ($journals->count() > 0) {
|
|
|
|
$avg = bcdiv($sum, $count);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $avg;
|
|
|
|
}
|
|
|
|
|
2018-02-06 18:11:33 +01:00
|
|
|
/**
|
|
|
|
* @param int $size
|
|
|
|
*
|
|
|
|
* @return LengthAwarePaginator
|
|
|
|
*/
|
|
|
|
public function getPaginator(int $size): LengthAwarePaginator
|
|
|
|
{
|
|
|
|
return $this->user->bills()->paginate($size);
|
|
|
|
}
|
|
|
|
|
2016-10-23 14:56:05 +02:00
|
|
|
/**
|
2017-06-28 15:45:28 +02:00
|
|
|
* The "paid dates" list is a list of dates of transaction journals that are linked to this bill.
|
|
|
|
*
|
2016-10-23 14:56:05 +02:00
|
|
|
* @param Bill $bill
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getPaidDatesInRange(Bill $bill, Carbon $start, Carbon $end): Collection
|
|
|
|
{
|
2018-12-09 13:09:43 +01:00
|
|
|
$dates = $bill->transactionJournals()->before($end)->after($start)->get(
|
|
|
|
[
|
|
|
|
'transaction_journals.id', 'transaction_journals.date',
|
|
|
|
]
|
|
|
|
)->pluck('date', 'id');
|
2016-10-23 14:56:05 +02:00
|
|
|
|
|
|
|
return $dates;
|
|
|
|
}
|
|
|
|
|
2016-10-21 13:20:51 +02:00
|
|
|
/**
|
|
|
|
* Between start and end, tells you on which date(s) the bill is expected to hit.
|
|
|
|
*
|
|
|
|
* @param Bill $bill
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getPayDatesInRange(Bill $bill, Carbon $start, Carbon $end): Collection
|
|
|
|
{
|
2018-08-06 19:14:30 +02:00
|
|
|
$set = new Collection;
|
2016-10-21 13:20:51 +02:00
|
|
|
$currentStart = clone $start;
|
2018-07-23 21:49:15 +02:00
|
|
|
Log::debug(sprintf('Now at bill "%s" (%s)', $bill->name, $bill->repeat_freq));
|
2016-10-21 13:20:51 +02:00
|
|
|
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')));
|
|
|
|
$nextExpectedMatch = $this->nextDateMatch($bill, $currentStart);
|
|
|
|
Log::debug(sprintf('Next Date match after %s is %s', $currentStart->format('Y-m-d'), $nextExpectedMatch->format('Y-m-d')));
|
2018-07-23 21:49:15 +02:00
|
|
|
if ($nextExpectedMatch > $end) {// If nextExpectedMatch is after end, we continue
|
2016-10-21 13:20:51 +02: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'))
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$set->push(clone $nextExpectedMatch);
|
|
|
|
Log::debug(sprintf('Now %d dates in set.', $set->count()));
|
|
|
|
$nextExpectedMatch->addDay();
|
|
|
|
|
|
|
|
Log::debug(sprintf('Currentstart (%s) has become %s.', $currentStart->format('Y-m-d'), $nextExpectedMatch->format('Y-m-d')));
|
|
|
|
|
|
|
|
$currentStart = clone $nextExpectedMatch;
|
|
|
|
}
|
|
|
|
$simple = $set->each(
|
|
|
|
function (Carbon $date) {
|
|
|
|
return $date->format('Y-m-d');
|
|
|
|
}
|
|
|
|
);
|
|
|
|
Log::debug(sprintf('Found dates between %s and %s:', $start->format('Y-m-d'), $end->format('Y-m-d')), $simple->toArray());
|
|
|
|
|
|
|
|
return $set;
|
|
|
|
}
|
|
|
|
|
2018-04-14 09:59:04 +02:00
|
|
|
/**
|
|
|
|
* Return all rules for one bill
|
|
|
|
*
|
|
|
|
* @param Bill $bill
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getRulesForBill(Bill $bill): Collection
|
|
|
|
{
|
|
|
|
return $this->user->rules()
|
|
|
|
->leftJoin('rule_actions', 'rule_actions.rule_id', '=', 'rules.id')
|
|
|
|
->where('rule_actions.action_type', 'link_to_bill')
|
|
|
|
->where('rule_actions.action_value', $bill->name)
|
|
|
|
->get(['rules.*']);
|
|
|
|
}
|
|
|
|
|
2018-04-08 17:36:37 +02:00
|
|
|
/**
|
|
|
|
* Return all rules related to the bills in the collection, in an associative array:
|
|
|
|
* 5= billid
|
|
|
|
*
|
|
|
|
* 5 => [['id' => 1, 'title' => 'Some rule'],['id' => 2, 'title' => 'Some other rule']]
|
|
|
|
*
|
|
|
|
* @param Collection $collection
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getRulesForBills(Collection $collection): array
|
|
|
|
{
|
|
|
|
$rules = $this->user->rules()
|
|
|
|
->leftJoin('rule_actions', 'rule_actions.rule_id', '=', 'rules.id')
|
|
|
|
->where('rule_actions.action_type', 'link_to_bill')
|
2018-05-07 20:35:14 +02:00
|
|
|
->get(['rules.id', 'rules.title', 'rule_actions.action_value', 'rules.active']);
|
2018-04-08 17:36:37 +02:00
|
|
|
$array = [];
|
|
|
|
foreach ($rules as $rule) {
|
|
|
|
$array[$rule->action_value] = $array[$rule->action_value] ?? [];
|
2018-05-07 20:35:14 +02:00
|
|
|
$array[$rule->action_value][] = ['id' => $rule->id, 'title' => $rule->title, 'active' => $rule->active];
|
2018-04-08 17:36:37 +02:00
|
|
|
}
|
|
|
|
$return = [];
|
|
|
|
foreach ($collection as $bill) {
|
|
|
|
$return[$bill->id] = $array[$bill->name] ?? [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2016-07-23 21:37:06 +02:00
|
|
|
/**
|
|
|
|
* @param Bill $bill
|
|
|
|
* @param Carbon $date
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getYearAverage(Bill $bill, Carbon $date): string
|
|
|
|
{
|
2018-02-25 19:09:05 +01:00
|
|
|
/** @var JournalRepositoryInterface $repos */
|
|
|
|
$repos = app(JournalRepositoryInterface::class);
|
|
|
|
$repos->setUser($this->user);
|
|
|
|
|
2016-08-26 09:30:52 +02:00
|
|
|
$journals = $bill->transactionJournals()
|
2018-02-04 09:22:52 +01:00
|
|
|
->where('date', '>=', $date->year . '-01-01 00:00:00')
|
2018-02-25 19:09:05 +01:00
|
|
|
->where('date', '<=', $date->year . '-12-31 23:59:59')
|
2016-07-23 21:37:06 +02:00
|
|
|
->get();
|
|
|
|
$sum = '0';
|
2018-04-02 15:10:40 +02:00
|
|
|
$count = (string)$journals->count();
|
2016-07-23 21:37:06 +02:00
|
|
|
/** @var TransactionJournal $journal */
|
|
|
|
foreach ($journals as $journal) {
|
2018-02-25 19:09:05 +01:00
|
|
|
$sum = bcadd($sum, $repos->getJournalTotal($journal));
|
2016-07-23 21:37:06 +02:00
|
|
|
}
|
|
|
|
$avg = '0';
|
|
|
|
if ($journals->count() > 0) {
|
|
|
|
$avg = bcdiv($sum, $count);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $avg;
|
|
|
|
}
|
|
|
|
|
2018-04-14 09:59:04 +02:00
|
|
|
/**
|
|
|
|
* Link a set of journals to a bill.
|
|
|
|
*
|
|
|
|
* @param Bill $bill
|
2019-07-21 17:15:06 +02:00
|
|
|
* @param array $transactions
|
2018-04-14 09:59:04 +02:00
|
|
|
*/
|
2019-07-21 17:15:06 +02:00
|
|
|
public function linkCollectionToBill(Bill $bill, array $transactions): void
|
2018-04-14 09:59:04 +02:00
|
|
|
{
|
2018-04-14 23:25:28 +02:00
|
|
|
/** @var Transaction $transaction */
|
|
|
|
foreach ($transactions as $transaction) {
|
|
|
|
$journal = $transaction->transactionJournal;
|
2018-04-14 23:23:47 +02:00
|
|
|
$journal->bill_id = $bill->id;
|
|
|
|
$journal->save();
|
|
|
|
Log::debug(sprintf('Linked journal #%d to bill #%d', $journal->id, $bill->id));
|
|
|
|
}
|
2018-04-14 09:59:04 +02:00
|
|
|
}
|
|
|
|
|
2015-02-25 15:19:14 +01:00
|
|
|
/**
|
2016-10-21 06:26:12 +02: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()) {
|
2017-03-04 11:19:44 +01:00
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
2016-10-21 06:26:12 +02:00
|
|
|
}
|
|
|
|
// find the most recent date for this bill NOT in the future. Cache this date:
|
|
|
|
$start = clone $bill->date;
|
2019-08-01 06:21:44 +02:00
|
|
|
//Log::debug('nextDateMatch: Start is ' . $start->format('Y-m-d'));
|
2016-10-21 06:26:12 +02:00
|
|
|
|
2016-10-21 13:20:51 +02:00
|
|
|
while ($start < $date) {
|
2019-08-01 06:21:44 +02:00
|
|
|
//Log::debug(sprintf('$start (%s) < $date (%s)', $start->format('Y-m-d'), $date->format('Y-m-d')));
|
2018-02-22 20:07:14 +01:00
|
|
|
$start = app('navigation')->addPeriod($start, $bill->repeat_freq, $bill->skip);
|
2019-08-01 06:21:44 +02:00
|
|
|
//Log::debug('Start is now ' . $start->format('Y-m-d'));
|
2016-10-21 06:26:12 +02:00
|
|
|
}
|
|
|
|
|
2018-02-22 20:07:14 +01:00
|
|
|
$end = app('navigation')->addPeriod($start, $bill->repeat_freq, $bill->skip);
|
2016-10-21 13:20:51 +02:00
|
|
|
|
|
|
|
Log::debug('nextDateMatch: Final start is ' . $start->format('Y-m-d'));
|
|
|
|
Log::debug('nextDateMatch: Matching end is ' . $end->format('Y-m-d'));
|
|
|
|
|
2016-10-21 06:26:12 +02:00
|
|
|
$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 21:40:45 +02:00
|
|
|
* @param Bill $bill
|
|
|
|
* @param Carbon $date
|
2015-02-25 15:19:14 +01:00
|
|
|
*
|
2016-10-20 21:40:45 +02:00
|
|
|
* @return Carbon
|
2015-02-25 15:19:14 +01:00
|
|
|
*/
|
2016-10-20 21:40:45 +02:00
|
|
|
public function nextExpectedMatch(Bill $bill, Carbon $date): Carbon
|
2015-02-25 15:19:14 +01:00
|
|
|
{
|
2016-10-20 21:40:45 +02:00
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty($bill->id);
|
|
|
|
$cache->addProperty('nextExpectedMatch');
|
2016-10-21 06:26:12 +02:00
|
|
|
$cache->addProperty($date);
|
2016-10-20 21:40:45 +02:00
|
|
|
if ($cache->has()) {
|
2017-03-04 11:19:44 +01:00
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
2015-02-25 15:19:14 +01:00
|
|
|
}
|
2016-10-20 21:40:45 +02:00
|
|
|
// find the most recent date for this bill NOT in the future. Cache this date:
|
|
|
|
$start = clone $bill->date;
|
2016-10-21 06:41:33 +02:00
|
|
|
Log::debug('nextExpectedMatch: Start is ' . $start->format('Y-m-d'));
|
2016-10-20 21:40:45 +02:00
|
|
|
|
2016-10-21 07:29:25 +02:00
|
|
|
while ($start < $date) {
|
|
|
|
Log::debug(sprintf('$start (%s) < $date (%s)', $start->format('Y-m-d'), $date->format('Y-m-d')));
|
2018-02-22 20:07:14 +01:00
|
|
|
$start = app('navigation')->addPeriod($start, $bill->repeat_freq, $bill->skip);
|
2016-10-20 21:40:45 +02:00
|
|
|
Log::debug('Start is now ' . $start->format('Y-m-d'));
|
2016-10-21 07:29:25 +02:00
|
|
|
}
|
2016-10-20 21:40:45 +02:00
|
|
|
|
2018-02-22 20:07:14 +01:00
|
|
|
$end = app('navigation')->addPeriod($start, $bill->repeat_freq, $bill->skip);
|
2016-10-20 21:40:45 +02:00
|
|
|
|
|
|
|
// 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-21 06:26:12 +02:00
|
|
|
Log::debug(sprintf('Journal count is %d, so start becomes %s', $journalCount, $end->format('Y-m-d')));
|
2016-10-20 21:40:45 +02:00
|
|
|
$start = clone $end;
|
2018-02-22 20:07:14 +01:00
|
|
|
$end = app('navigation')->addPeriod($start, $bill->repeat_freq, $bill->skip);
|
2015-02-25 15:19:14 +01:00
|
|
|
}
|
2016-10-21 07:29:25 +02: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 21:40:45 +02:00
|
|
|
$cache->store($start);
|
2015-02-25 15:19:14 +01:00
|
|
|
|
2016-10-20 21:40:45 +02:00
|
|
|
return $start;
|
2015-02-25 15:19:14 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 14:12:09 +01:00
|
|
|
/**
|
|
|
|
* @param string $query
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function searchBill(string $query): Collection
|
|
|
|
{
|
|
|
|
$query = sprintf('%%%s%%', $query);
|
|
|
|
|
|
|
|
return $this->user->bills()->where('name', 'LIKE', $query)->get();
|
|
|
|
}
|
|
|
|
|
2017-01-30 16:46:30 +01:00
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
*/
|
2018-07-22 18:50:27 +02:00
|
|
|
public function setUser(User $user): void
|
2017-01-30 16:46:30 +01:00
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
2015-02-25 15:19:14 +01:00
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*
|
2018-04-03 19:12:59 +02:00
|
|
|
* @return Bill|null
|
2015-02-25 15:19:14 +01:00
|
|
|
*/
|
2018-04-03 19:12:59 +02:00
|
|
|
public function store(array $data): ?Bill
|
2015-02-25 15:19:14 +01:00
|
|
|
{
|
2018-02-22 20:07:14 +01:00
|
|
|
/** @var BillFactory $factory */
|
|
|
|
$factory = app(BillFactory::class);
|
|
|
|
$factory->setUser($this->user);
|
2015-02-25 15:19:14 +01:00
|
|
|
|
2018-04-02 15:10:40 +02:00
|
|
|
return $factory->create($data);
|
2015-02-25 15:19:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Bill $bill
|
|
|
|
* @param array $data
|
|
|
|
*
|
2015-05-26 18:57:31 +00:00
|
|
|
* @return Bill
|
2015-02-25 15:19:14 +01:00
|
|
|
*/
|
2016-02-06 18:59:48 +01:00
|
|
|
public function update(Bill $bill, array $data): Bill
|
2015-02-25 15:19:14 +01:00
|
|
|
{
|
2018-02-22 20:07:14 +01:00
|
|
|
/** @var BillUpdateService $service */
|
|
|
|
$service = app(BillUpdateService::class);
|
2017-11-25 20:54:42 +01:00
|
|
|
|
2018-02-22 20:07:14 +01:00
|
|
|
return $service->update($bill, $data);
|
2015-02-25 15:19:14 +01:00
|
|
|
}
|
|
|
|
}
|