Merge pull request #7577 from firefly-iii/fix-billrepos

clean: remove deprecated methods and refactor as necessary.
This commit is contained in:
James Cole 2023-06-02 06:38:49 +02:00 committed by GitHub
commit 1e1497ff4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 252 deletions

View File

@ -269,48 +269,38 @@ class BasicController extends Controller
$return = [];
/**
* @var int $currencyId
* @var array $info
*/
foreach ($paidAmount as $currencyId => $info) {
foreach ($paidAmount as $info) {
$amount = bcmul($info['sum'], '-1');
$currency = $this->currencyRepos->find((int)$currencyId);
if (null === $currency) {
continue;
}
$return[] = [
'key' => sprintf('bills-paid-in-%s', $currency->code),
'title' => trans('firefly.box_bill_paid_in_currency', ['currency' => $currency->symbol]),
'key' => sprintf('bills-paid-in-%s', $info['code']),
'title' => trans('firefly.box_bill_paid_in_currency', ['currency' => $info['symbol']]),
'monetary_value' => $amount,
'currency_id' => $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
'value_parsed' => app('amount')->formatAnything($currency, $amount, false),
'currency_id' => $info['id'],
'currency_code' => $info['code'],
'currency_symbol' => $info['symbol'],
'currency_decimal_places' => $info['decimal_places'],
'value_parsed' => app('amount')->formatFlat($info['symbol'], $info['decimal_places'], $amount, false),
'local_icon' => 'check',
'sub_title' => '',
];
}
/**
* @var int $currencyId
* @var array $info
*/
foreach ($unpaidAmount as $currencyId => $info) {
foreach ($unpaidAmount as $info) {
$amount = bcmul($info['sum'], '-1');
$currency = $this->currencyRepos->find((int)$currencyId);
if (null === $currency) {
continue;
}
$return[] = [
'key' => sprintf('bills-unpaid-in-%s', $currency->code),
'title' => trans('firefly.box_bill_unpaid_in_currency', ['currency' => $currency->symbol]),
'key' => sprintf('bills-unpaid-in-%s', $info['code']),
'title' => trans('firefly.box_bill_unpaid_in_currency', ['currency' => $info['symbol']]),
'monetary_value' => $amount,
'currency_id' => $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places,
'value_parsed' => app('amount')->formatAnything($currency, $amount, false),
'currency_id' => $info['id'],
'currency_code' => $info['code'],
'currency_symbol' => $info['symbol'],
'currency_decimal_places' => $info['decimal_places'],
'value_parsed' => app('amount')->formatFlat($info['symbol'], $info['decimal_places'], $amount, false),
'local_icon' => 'calendar-o',
'sub_title' => '',
];

View File

@ -29,7 +29,6 @@ use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Bill;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\CacheProperties;
use Illuminate\Http\JsonResponse;
@ -38,8 +37,7 @@ use Illuminate\Http\JsonResponse;
*/
class BillController extends Controller
{
/** @var GeneratorInterface Chart generation methods. */
protected $generator;
protected GeneratorInterface $generator;
/**
* BillController constructor.
@ -70,30 +68,33 @@ class BillController extends Controller
if ($cache->has()) {
return response()->json($cache->get());
}
/** @var CurrencyRepositoryInterface $currencyRepository */
$currencyRepository = app(CurrencyRepositoryInterface::class);
$chartData = [];
$currencies = [];
$paid = $repository->getBillsPaidInRangePerCurrency($start, $end); // will be a negative amount.
$unpaid = $repository->getBillsUnpaidInRangePerCurrency($start, $end); // will be a positive amount.
$paid = $repository->sumPaidInRange($start, $end);
$unpaid = $repository->sumUnpaidInRange($start, $end);
foreach ($paid as $currencyId => $amount) {
$currencies[$currencyId] = $currencies[$currencyId] ?? $currencyRepository->find($currencyId);
$label = (string)trans('firefly.paid_in_currency', ['currency' => $currencies[$currencyId]->name]);
/**
* @var array $info
*/
foreach ($paid as $info) {
$amount = $info['sum'];
$label = (string)trans('firefly.paid_in_currency', ['currency' => $info['name']]);
$chartData[$label] = [
'amount' => $amount,
'currency_symbol' => $currencies[$currencyId]->symbol,
'currency_code' => $currencies[$currencyId]->code,
'currency_symbol' => $info['symbol'],
'currency_code' => $info['code'],
];
}
foreach ($unpaid as $currencyId => $amount) {
$currencies[$currencyId] = $currencies[$currencyId] ?? $currencyRepository->find($currencyId);
$label = (string)trans('firefly.unpaid_in_currency', ['currency' => $currencies[$currencyId]->name]);
/**
* @var array $info
*/
foreach ($unpaid as $info) {
$amount = $info['sum'];
$label = (string)trans('firefly.unpaid_in_currency', ['currency' => $info['name']]);
$chartData[$label] = [
'amount' => $amount,
'currency_symbol' => $currencies[$currencyId]->symbol,
'currency_code' => $currencies[$currencyId]->code,
'currency_symbol' => $info['symbol'],
'currency_code' => $info['code'],
];
}

View File

@ -87,27 +87,6 @@ class BillRepository implements BillRepositoryInterface
return $search->take($limit)->get();
}
/**
* @inheritDoc
* @deprecated
*/
public function collectBillsUnpaidInRange(Carbon $start, Carbon $end): Collection
{
$bills = $this->getActiveBills();
$return = new Collection();
/** @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) {
$return->push($bill);
}
}
return $bills;
}
/**
* Correct order of piggies in case of issues.
*/
@ -296,139 +275,6 @@ class BillRepository implements BillRepositoryInterface
->get($fields);
}
/**
* TODO unsure why this is deprecated.
*
* Get the total amount of money paid for the users active bills in the date range given.
* This amount will be negative (they're expenses). This method is equal to
* getBillsUnpaidInRange. So the debug comments are gone.
*
* @param Carbon $start
* @param Carbon $end
* @return string
* @deprecated
*/
public function getBillsPaidInRange(Carbon $start, Carbon $end): string
{
$bills = $this->getActiveBills();
$sum = '0';
/** @var Bill $bill */
foreach ($bills as $bill) {
/** @var Collection $set */
$set = $bill->transactionJournals()->after($start)->before($end)->get(['transaction_journals.*']);
if ($set->count() > 0) {
$journalIds = $set->pluck('id')->toArray();
$amount = (string)Transaction::whereIn('transaction_journal_id', $journalIds)->where('amount', '<', 0)->sum('amount');
$sum = bcadd($sum, $amount);
//Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f', $amount, $sum));
}
}
return $sum;
}
/**
* TODO unsure why this is deprecated.
*
* 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
* @deprecated
*/
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;
}
/**
* TODO unsure why this is deprecated.
*
* 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
* @deprecated
*/
public function getBillsUnpaidInRange(Carbon $start, Carbon $end): string
{
$bills = $this->getActiveBills();
$sum = '0';
/** @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;
//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);
$sum = bcadd($sum, $multi);
//Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f', $multi, $sum));
}
}
return $sum;
}
/**
* TODO unsure why this is deprecated.
*
* 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
* @deprecated
*/
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((string)$bill->amount_max, (string)$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;
}
/**
* Get all bills with these ID's.
*

View File

@ -52,16 +52,6 @@ interface BillRepositoryInterface
*/
public function billStartsWith(string $query, int $limit): Collection;
/**
* Get the total amount of money due for the users active bills in the date range given.
*
* @param Carbon $start
* @param Carbon $end
* @return Collection
* @deprecated
*/
public function collectBillsUnpaidInRange(Carbon $start, Carbon $end): Collection;
/**
* Add correct order to bills.
*/
@ -135,47 +125,6 @@ interface BillRepositoryInterface
*/
public function getBillsForAccounts(Collection $accounts): Collection;
/**
* Get the total amount of money paid for the users active bills in the date range given.
* @param Carbon $start
* @param Carbon $end
*
* @return string
* @deprecated
*/
public function getBillsPaidInRange(Carbon $start, Carbon $end): string;
/**
* 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
* @deprecated
*/
public function getBillsPaidInRangePerCurrency(Carbon $start, Carbon $end): array;
/**
* Get the total amount of money due for the users active bills in the date range given.
* @param Carbon $start
* @param Carbon $end
*
* @return string
* @deprecated
*/
public function getBillsUnpaidInRange(Carbon $start, Carbon $end): string;
/**
* 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
* @deprecated
*/
public function getBillsUnpaidInRangePerCurrency(Carbon $start, Carbon $end): array;
/**
* Get all bills with these ID's.
*