. */ declare(strict_types=1); namespace FireflyIII\Repositories\Budget; use Carbon\Carbon; use Exception; use FireflyIII\Models\AvailableBudget; use FireflyIII\Models\TransactionCurrency; use FireflyIII\User; use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Collection; /** * * Class AvailableBudgetRepository */ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface { private User $user; /** * Delete all available budgets. */ public function destroyAll(): void { $this->user->availableBudgets()->delete(); } /** * @param AvailableBudget $availableBudget */ public function destroyAvailableBudget(AvailableBudget $availableBudget): void { try { $availableBudget->delete(); } catch (Exception $e) { // @ignoreException } } /** * @inheritDoc */ public function findById(int $id): ?AvailableBudget { return $this->user->availableBudgets->find($id); } /** * Find existing AB. * * @param TransactionCurrency $currency * @param Carbon $start * @param Carbon $end * * @return AvailableBudget|null */ public function find(TransactionCurrency $currency, Carbon $start, Carbon $end): ?AvailableBudget { return $this->user->availableBudgets() ->where('transaction_currency_id', $currency->id) ->where('start_date', $start->format('Y-m-d')) ->where('end_date', $end->format('Y-m-d')) ->first(); } /** * @param TransactionCurrency $currency * @param Carbon $start * @param Carbon $end * * @return string */ public function getAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end): string { $amount = '0'; $availableBudget = $this->user->availableBudgets() ->where('transaction_currency_id', $currency->id) ->where('start_date', $start->format('Y-m-d')) ->where('end_date', $end->format('Y-m-d'))->first(); if (null !== $availableBudget) { $amount = (string)$availableBudget->amount; } return $amount; } /** * @param Carbon $start * @param Carbon $end * * @return array */ public function getAvailableBudgetWithCurrency(Carbon $start, Carbon $end): array { $return = []; $availableBudgets = $this->user->availableBudgets() ->where('start_date', $start->format('Y-m-d')) ->where('end_date', $end->format('Y-m-d'))->get(); /** @var AvailableBudget $availableBudget */ foreach ($availableBudgets as $availableBudget) { $return[$availableBudget->transaction_currency_id] = $availableBudget->amount; } return $return; } /** * Return a list of all available budgets (in all currencies) (for the selected period). * * @param Carbon|null $start * @param Carbon|null $end * * @return Collection */ public function get(?Carbon $start = null, ?Carbon $end = null): Collection { $query = $this->user->availableBudgets()->with(['transactionCurrency']); if (null !== $start && null !== $end) { $query->where( static function (Builder $q1) use ($start, $end) { $q1->where('start_date', '=', $start->format('Y-m-d')); $q1->where('end_date', '=', $end->format('Y-m-d')); } ); } return $query->get(['available_budgets.*']); } /** * Returns all available budget objects. * * @param TransactionCurrency $currency * * @return Collection */ public function getAvailableBudgetsByCurrency(TransactionCurrency $currency): Collection { return $this->user->availableBudgets()->where('transaction_currency_id', $currency->id)->get(); } /** * Returns all available budget objects. * * @param Carbon|null $start * @param Carbon|null $end * * @return Collection * */ public function getAvailableBudgetsByDate(?Carbon $start, ?Carbon $end): Collection { $query = $this->user->availableBudgets(); if (null !== $start) { $query->where('start_date', '>=', $start->format('Y-m-d')); } if (null !== $end) { $query->where('end_date', '<=', $end->format('Y-m-d')); } return $query->get(); } /** * @inheritDoc */ public function getByCurrencyDate(Carbon $start, Carbon $end, TransactionCurrency $currency): ?AvailableBudget { return $this->user ->availableBudgets() ->where('transaction_currency_id', $currency->id) ->where('start_date', $start->format('Y-m-d')) ->where('end_date', $end->format('Y-m-d'))->first(); } /** * @param TransactionCurrency $currency * @param Carbon $start * @param Carbon $end * @param string $amount * * @return AvailableBudget * @deprecated */ 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')) ->where('end_date', $end->format('Y-m-d'))->first(); if (null === $availableBudget) { $availableBudget = new AvailableBudget(); $availableBudget->user()->associate($this->user); $availableBudget->transactionCurrency()->associate($currency); $availableBudget->start_date = $start->format('Y-m-d'); $availableBudget->end_date = $end->format('Y-m-d'); } $availableBudget->amount = $amount; $availableBudget->save(); return $availableBudget; } /** * @param User $user */ public function setUser(User $user): void { $this->user = $user; } /** * @param array $data * * @return AvailableBudget|null */ public function store(array $data): ?AvailableBudget { $start = $data['start']; if ($start instanceof Carbon) { $start = $data['start']->startOfDay(); } $end = $data['end']; if ($end instanceof Carbon) { $end = $data['end']->endOfDay(); } return AvailableBudget::create( [ 'user_id' => $this->user->id, 'transaction_currency_id' => $data['currency_id'], 'amount' => $data['amount'], 'start_date' => $start, 'end_date' => $end, ] ); } /** * @param AvailableBudget $availableBudget * @param array $data * * @return AvailableBudget */ public function update(AvailableBudget $availableBudget, array $data): AvailableBudget { if (array_key_exists('amount', $data)) { $availableBudget->amount = $data['amount']; } $availableBudget->save(); return $availableBudget; } /** * @param AvailableBudget $availableBudget * @param array $data * * @return AvailableBudget */ public function updateAvailableBudget(AvailableBudget $availableBudget, array $data): AvailableBudget { if (array_key_exists('start', $data)) { $start = $data['start']; if ($start instanceof Carbon) { $start = $data['start']->startOfDay(); $availableBudget->start_date = $start; $availableBudget->save(); } } if (array_key_exists('end', $data)) { $end = $data['end']; if ($end instanceof Carbon) { $end = $data['end']->endOfDay(); $availableBudget->end_date = $end; $availableBudget->save(); } } if (array_key_exists('currency_id', $data)) { $availableBudget->transaction_currency_id = $data['currency_id']; $availableBudget->save(); } if (array_key_exists('amount', $data)) { $availableBudget->amount = $data['amount']; $availableBudget->save(); } return $availableBudget; } }