Files
firefly-iii/app/Repositories/UserGroups/Bill/BillRepository.php

249 lines
9.8 KiB
PHP
Raw Normal View History

2023-08-06 07:04:09 +02:00
<?php
2023-09-20 06:36:43 +02:00
2023-08-06 07:04:09 +02:00
/*
* BillRepository.php
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2023-09-20 06:36:43 +02:00
declare(strict_types=1);
namespace FireflyIII\Repositories\UserGroups\Bill;
2023-08-06 07:04:09 +02:00
use Carbon\Carbon;
use FireflyIII\Models\Bill;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Support\CacheProperties;
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait;
2023-08-06 07:04:09 +02:00
use Illuminate\Support\Collection;
/**
* Class BillRepository
*/
class BillRepository implements BillRepositoryInterface
{
use UserGroupTrait;
2023-08-06 07:04:09 +02:00
2023-08-09 14:14:33 +02:00
/**
* Correct order of piggies in case of issues.
*/
public function correctOrder(): void
{
$set = $this->userGroup->bills()->orderBy('order', 'ASC')->get();
$current = 1;
foreach ($set as $bill) {
if ((int)$bill->order !== $current) {
$bill->order = $current;
$bill->save();
}
$current++;
}
}
/**
* @return Collection
*/
public function getBills(): Collection
{
return $this->userGroup->bills()
->orderBy('bills.name', 'ASC')
->get(['bills.*']);
}
2023-08-06 07:04:09 +02:00
/**
* @inheritDoc
*/
public function sumPaidInRange(Carbon $start, Carbon $end): array
{
$bills = $this->getActiveBills();
$default = app('amount')->getDefaultCurrency();
$return = [];
$converter = new ExchangeRateConverter();
/** @var Bill $bill */
foreach ($bills as $bill) {
/** @var Collection $set */
$set = $bill->transactionJournals()->after($start)->before($end)->get(['transaction_journals.*']);
$currency = $bill->transactionCurrency;
2023-11-05 19:41:37 +01:00
$currencyId = $bill->transaction_currency_id;
2023-08-06 07:04:09 +02:00
$return[$currencyId] = $return[$currencyId] ?? [
'currency_id' => (string)$currency->id,
'currency_name' => $currency->name,
'currency_symbol' => $currency->symbol,
'currency_code' => $currency->code,
'currency_decimal_places' => (int)$currency->decimal_places,
'native_id' => (string)$default->id,
'native_name' => $default->name,
'native_symbol' => $default->symbol,
'native_code' => $default->code,
'native_decimal_places' => (int)$default->decimal_places,
'sum' => '0',
'native_sum' => '0',
];
/** @var TransactionJournal $transactionJournal */
foreach ($set as $transactionJournal) {
/** @var Transaction|null $sourceTransaction */
$sourceTransaction = $transactionJournal->transactions()->where('amount', '<', 0)->first();
if (null !== $sourceTransaction) {
2023-11-05 19:41:37 +01:00
$amount = $sourceTransaction->amount;
if ((int)$sourceTransaction->foreign_currency_id === $currency->id) {
2023-08-06 07:04:09 +02:00
// use foreign amount instead!
$amount = (string)$sourceTransaction->foreign_amount;
}
// convert to native currency
$nativeAmount = $amount;
2023-11-05 19:41:37 +01:00
if ($currencyId !== $default->id) {
2023-08-06 07:04:09 +02:00
// get rate and convert.
$nativeAmount = $converter->convert($currency, $default, $transactionJournal->date, $amount);
}
2023-11-05 19:41:37 +01:00
if ((int)$sourceTransaction->foreign_currency_id === $default->id) {
2023-08-06 07:04:09 +02:00
// ignore conversion, use foreign amount
$nativeAmount = (string)$sourceTransaction->foreign_amount;
}
$return[$currencyId]['sum'] = bcadd($return[$currencyId]['sum'], $amount);
$return[$currencyId]['native_sum'] = bcadd($return[$currencyId]['native_sum'], $nativeAmount);
}
}
}
return $return;
}
/**
* @return Collection
*/
public function getActiveBills(): Collection
{
return $this->userGroup->bills()
->where('active', true)
->orderBy('bills.name', 'ASC')
->get(['bills.*']);
}
/**
* @inheritDoc
*/
public function sumUnpaidInRange(Carbon $start, Carbon $end): array
{
$bills = $this->getActiveBills();
$return = [];
$default = app('amount')->getDefaultCurrency();
$converter = new ExchangeRateConverter();
/** @var Bill $bill */
foreach ($bills as $bill) {
$dates = $this->getPayDatesInRange($bill, $start, $end);
$count = $bill->transactionJournals()->after($start)->before($end)->count();
$total = $dates->count() - $count;
if ($total > 0) {
$currency = $bill->transactionCurrency;
2023-11-05 19:41:37 +01:00
$currencyId = $bill->transaction_currency_id;
2023-08-06 07:04:09 +02:00
$average = bcdiv(bcadd($bill->amount_max, $bill->amount_min), '2');
$nativeAverage = $converter->convert($currency, $default, $start, $average);
$return[$currencyId] = $return[$currencyId] ?? [
'currency_id' => (string)$currency->id,
'currency_name' => $currency->name,
'currency_symbol' => $currency->symbol,
'currency_code' => $currency->code,
'currency_decimal_places' => (int)$currency->decimal_places,
'native_id' => (string)$default->id,
'native_name' => $default->name,
'native_symbol' => $default->symbol,
'native_code' => $default->code,
'native_decimal_places' => (int)$default->decimal_places,
'sum' => '0',
'native_sum' => '0',
];
$return[$currencyId]['sum'] = bcadd($return[$currencyId]['sum'], bcmul($average, (string)$total));
$return[$currencyId]['native_sum'] = bcadd($return[$currencyId]['native_sum'], bcmul($nativeAverage, (string)$total));
}
}
return $return;
}
/**
* Between start and end, tells you on which date(s) the bill is expected to hit.
* TODO duplicate of function in other billrepositoryinterface
*
* @param Bill $bill
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getPayDatesInRange(Bill $bill, Carbon $start, Carbon $end): Collection
{
$set = new Collection();
$currentStart = clone $start;
2023-10-29 06:33:43 +01:00
//app('log')->debug(sprintf('Now at bill "%s" (%s)', $bill->name, $bill->repeat_freq));
//app('log')->debug(sprintf('First currentstart is %s', $currentStart->format('Y-m-d')));
2023-08-06 07:04:09 +02:00
while ($currentStart <= $end) {
2023-10-29 06:33:43 +01:00
//app('log')->debug(sprintf('Currentstart is now %s.', $currentStart->format('Y-m-d')));
2023-08-06 07:04:09 +02:00
$nextExpectedMatch = $this->nextDateMatch($bill, $currentStart);
2023-10-29 06:33:43 +01:00
//app('log')->debug(sprintf('Next Date match after %s is %s', $currentStart->format('Y-m-d'), $nextExpectedMatch->format('Y-m-d')));
2023-08-06 07:04:09 +02:00
if ($nextExpectedMatch > $end) {// If nextExpectedMatch is after end, we continue
break;
}
$set->push(clone $nextExpectedMatch);
2023-10-29 06:33:43 +01:00
//app('log')->debug(sprintf('Now %d dates in set.', $set->count()));
2023-08-06 07:04:09 +02:00
$nextExpectedMatch->addDay();
2023-10-29 06:33:43 +01:00
//app('log')->debug(sprintf('Currentstart (%s) has become %s.', $currentStart->format('Y-m-d'), $nextExpectedMatch->format('Y-m-d')));
2023-08-06 07:04:09 +02:00
$currentStart = clone $nextExpectedMatch;
}
return $set;
}
/**
* Given a bill and a date, this method will tell you at which moment this bill expects its next
* transaction. Whether it is there already, is not relevant.
*
* TODO duplicate of other repos
*
* @param Bill $bill
* @param Carbon $date
*
* @return 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;
while ($start < $date) {
$start = app('navigation')->addPeriod($start, $bill->repeat_freq, $bill->skip);
}
$cache->store($start);
return $start;
}
}