Make some charts multi-currency.

This commit is contained in:
James Cole
2018-08-28 05:21:23 +02:00
parent 46136d94e9
commit e1c829f4fa
12 changed files with 196 additions and 18 deletions

View File

@@ -199,6 +199,36 @@ class BillRepository implements BillRepositoryInterface
return $sum;
}
/**
* 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;
}
/**
* Get the total amount of money due for the users active bills in the date range given. This amount will be positive.
*
@@ -231,6 +261,40 @@ class BillRepository implements BillRepositoryInterface
return $sum;
}
/**
* 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;
}
/**
* Get all bills with these ID's.
*

View File

@@ -88,6 +88,17 @@ interface BillRepositoryInterface
*/
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
*/
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.
*
@@ -98,6 +109,16 @@ interface BillRepositoryInterface
*/
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
*/
public function getBillsUnpaidInRangePerCurrency(Carbon $start, Carbon $end): array;
/**
* Get all bills with these ID's.
*