Move method to correct repository.

This commit is contained in:
James Cole
2019-08-30 08:19:55 +02:00
parent 40441de545
commit 5973b94677
12 changed files with 144 additions and 123 deletions

View File

@@ -258,33 +258,6 @@ class BudgetRepository implements BudgetRepositoryInterface
return $search->get();
}
/**
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
* @param string $amount
*
* @return AvailableBudget
*/
public function setAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end, string $amount): AvailableBudget
{
$availableBudget = $this->user->availableBudgets()
->where('transaction_currency_id', $currency->id)
->where('start_date', $start->format('Y-m-d 00:00:00'))
->where('end_date', $end->format('Y-m-d 00:00:00'))->first();
if (null === $availableBudget) {
$availableBudget = new AvailableBudget;
$availableBudget->user()->associate($this->user);
$availableBudget->transactionCurrency()->associate($currency);
$availableBudget->start_date = $start->format('Y-m-d 00:00:00');
$availableBudget->end_date = $end->format('Y-m-d 00:00:00');
}
$availableBudget->amount = $amount;
$availableBudget->save();
return $availableBudget;
}
/**
* @param Budget $budget
* @param int $order
@@ -303,67 +276,6 @@ class BudgetRepository implements BudgetRepositoryInterface
$this->user = $user;
}
/**
* @param Collection $budgets
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* TODO refactor me.
*
* @return array
*/
public function spentInPeriodMc(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): array
{
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setUser($this->user);
$collector->setRange($start, $end)->setBudgets($budgets)->withBudgetInformation();
if ($accounts->count() > 0) {
$collector->setAccounts($accounts);
}
// TODO possible candidate for getExtractedGroups
$set = $collector->getGroups();
$return = [];
$total = [];
$currencies = [];
/** @var array $group */
foreach ($set as $group) {
/** @var array $transaction */
foreach ($group['transactions'] as $transaction) {
$code = $transaction['currency_code'];
if (!isset($currencies[$code])) {
$currencies[$code] = [
'id' => $transaction['currency_id'],
'decimal_places' => $transaction['currency_decimal_places'],
'code' => $transaction['currency_code'],
'name' => $transaction['currency_name'],
'symbol' => $transaction['currency_symbol'],
];
}
$total[$code] = isset($total[$code]) ? bcadd($total[$code], $transaction['amount']) : $transaction['amount'];
}
}
/**
* @var string $code
* @var string $spent
*/
foreach ($total as $code => $spent) {
/** @var TransactionCurrency $currency */
$currency = $currencies[$code];
$return[] = [
'currency_id' => $currency['id'],
'currency_code' => $code,
'currency_name' => $currency['name'],
'currency_symbol' => $currency['symbol'],
'currency_decimal_places' => $currency['decimal_places'],
'amount' => $spent,
];
}
return $return;
}
/**
* @param Collection $accounts
* @param Carbon $start