mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-15 08:35:00 +00:00
Expand API and refactor for user groups.
This commit is contained in:
248
app/Repositories/UserGroups/Bill/BillRepository.php
Normal file
248
app/Repositories/UserGroups/Bill/BillRepository.php
Normal file
@@ -0,0 +1,248 @@
|
||||
<?php
|
||||
|
||||
|
||||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Repositories\UserGroups\Bill;
|
||||
|
||||
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;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Class BillRepository
|
||||
*/
|
||||
class BillRepository implements BillRepositoryInterface
|
||||
{
|
||||
use UserGroupTrait;
|
||||
|
||||
/**
|
||||
* 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.*']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
$currencyId = (int)$bill->transaction_currency_id;
|
||||
|
||||
$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) {
|
||||
$amount = (string)$sourceTransaction->amount;
|
||||
if ((int)$sourceTransaction->foreign_currency_id === (int)$currency->id) {
|
||||
// use foreign amount instead!
|
||||
$amount = (string)$sourceTransaction->foreign_amount;
|
||||
}
|
||||
// convert to native currency
|
||||
$nativeAmount = $amount;
|
||||
if ($currencyId !== (int)$default->id) {
|
||||
// get rate and convert.
|
||||
$nativeAmount = $converter->convert($currency, $default, $transactionJournal->date, $amount);
|
||||
}
|
||||
if ((int)$sourceTransaction->foreign_currency_id === (int)$default->id) {
|
||||
// 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;
|
||||
$currencyId = (int)$bill->transaction_currency_id;
|
||||
$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;
|
||||
//Log::debug(sprintf('Now at bill "%s" (%s)', $bill->name, $bill->repeat_freq));
|
||||
//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')));
|
||||
if ($nextExpectedMatch > $end) {// If nextExpectedMatch is after end, we continue
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user