2020-03-21 14:20:40 +01:00
|
|
|
<?php
|
2020-06-30 19:05:35 +02:00
|
|
|
|
2020-03-21 14:20:40 +01:00
|
|
|
/**
|
|
|
|
* ModifiesPiggyBanks.php
|
|
|
|
* Copyright (c) 2020 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/>.
|
|
|
|
*/
|
|
|
|
|
2020-06-30 19:05:35 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-03-21 14:20:40 +01:00
|
|
|
namespace FireflyIII\Repositories\PiggyBank;
|
2021-07-04 07:58:11 +02:00
|
|
|
|
2023-09-21 11:29:09 +02:00
|
|
|
use FireflyIII\Events\Model\PiggyBank\ChangedAmount;
|
2020-03-21 14:20:40 +01:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2024-12-01 18:16:48 +01:00
|
|
|
use FireflyIII\Factory\PiggyBankFactory;
|
2020-03-21 14:20:40 +01:00
|
|
|
use FireflyIII\Models\Note;
|
|
|
|
use FireflyIII\Models\PiggyBank;
|
|
|
|
use FireflyIII\Models\PiggyBankRepetition;
|
2024-12-01 18:16:48 +01:00
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
2020-03-21 14:20:40 +01:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2020-06-07 11:31:01 +02:00
|
|
|
use FireflyIII\Repositories\ObjectGroup\CreatesObjectGroups;
|
2024-12-01 18:16:48 +01:00
|
|
|
use FireflyIII\Support\Facades\Amount;
|
2020-03-21 14:20:40 +01:00
|
|
|
use Illuminate\Database\QueryException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trait ModifiesPiggyBanks
|
|
|
|
*/
|
|
|
|
trait ModifiesPiggyBanks
|
|
|
|
{
|
2020-06-07 11:31:01 +02:00
|
|
|
use CreatesObjectGroups;
|
2020-12-02 06:41:42 +01:00
|
|
|
|
2023-06-21 12:34:58 +02:00
|
|
|
public function addAmountToRepetition(PiggyBankRepetition $repetition, string $amount, TransactionJournal $journal): void
|
|
|
|
{
|
2024-12-01 18:32:05 +01:00
|
|
|
throw new FireflyException('[a] Piggy bank repetitions are EOL.');
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('addAmountToRepetition: %s', $amount));
|
2023-06-21 12:34:58 +02:00
|
|
|
if (-1 === bccomp($amount, '0')) {
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug('Remove amount.');
|
2023-06-21 12:34:58 +02:00
|
|
|
$this->removeAmount($repetition->piggyBank, bcmul($amount, '-1'), $journal);
|
|
|
|
}
|
|
|
|
if (1 === bccomp($amount, '0')) {
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug('Add amount.');
|
2023-06-21 12:34:58 +02:00
|
|
|
$this->addAmount($repetition->piggyBank, $amount, $journal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function removeAmount(PiggyBank $piggyBank, string $amount, ?TransactionJournal $journal = null): bool
|
|
|
|
{
|
2024-01-01 14:43:56 +01:00
|
|
|
$repetition = $this->getRepetition($piggyBank);
|
2023-06-21 12:34:58 +02:00
|
|
|
if (null === $repetition) {
|
|
|
|
return false;
|
|
|
|
}
|
2024-11-30 16:02:30 +01:00
|
|
|
$repetition->current_amount = bcsub($repetition->current_amount, $amount);
|
2023-06-21 12:34:58 +02:00
|
|
|
$repetition->save();
|
|
|
|
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug('addAmount [a]: Trigger change for negative amount.');
|
2023-09-21 11:29:09 +02:00
|
|
|
event(new ChangedAmount($piggyBank, bcmul($amount, '-1'), $journal, null));
|
2023-06-21 12:34:58 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-12-29 19:42:26 +01:00
|
|
|
public function addAmount(PiggyBank $piggyBank, string $amount, ?TransactionJournal $journal = null): bool
|
2020-03-21 14:20:40 +01:00
|
|
|
{
|
2024-01-01 14:43:56 +01:00
|
|
|
$repetition = $this->getRepetition($piggyBank);
|
2022-12-29 19:42:26 +01:00
|
|
|
if (null === $repetition) {
|
|
|
|
return false;
|
2022-12-11 07:17:59 +01:00
|
|
|
}
|
2024-11-30 16:02:30 +01:00
|
|
|
$currentAmount = $repetition->current_amount ?? '0';
|
|
|
|
$repetition->current_amount = bcadd($currentAmount, $amount);
|
2022-12-29 19:42:26 +01:00
|
|
|
$repetition->save();
|
|
|
|
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug('addAmount [b]: Trigger change for positive amount.');
|
2023-09-21 11:29:09 +02:00
|
|
|
event(new ChangedAmount($piggyBank, $amount, $journal, null));
|
2022-12-29 19:42:26 +01:00
|
|
|
|
|
|
|
return true;
|
2020-03-21 14:20:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function canAddAmount(PiggyBank $piggyBank, string $amount): bool
|
|
|
|
{
|
2021-09-18 10:26:12 +02:00
|
|
|
$today = today(config('app.timezone'));
|
2021-09-11 06:52:49 +02:00
|
|
|
$leftOnAccount = $this->leftOnAccount($piggyBank, $today);
|
2024-11-30 16:02:30 +01:00
|
|
|
$savedSoFar = $this->getRepetition($piggyBank)->current_amount;
|
2022-03-27 18:30:46 +02:00
|
|
|
$maxAmount = $leftOnAccount;
|
|
|
|
$leftToSave = null;
|
2024-11-30 16:02:30 +01:00
|
|
|
if (0 !== bccomp($piggyBank->target_amount, '0')) {
|
|
|
|
$leftToSave = bcsub($piggyBank->target_amount, $savedSoFar);
|
2022-03-27 18:30:46 +02:00
|
|
|
$maxAmount = 1 === bccomp($leftOnAccount, $leftToSave) ? $leftToSave : $leftOnAccount;
|
|
|
|
}
|
|
|
|
|
2024-01-01 14:43:56 +01:00
|
|
|
$compare = bccomp($amount, $maxAmount);
|
|
|
|
$result = $compare <= 0;
|
2020-03-21 14:20:40 +01:00
|
|
|
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('Left on account: %s on %s', $leftOnAccount, $today->format('Y-m-d')));
|
|
|
|
app('log')->debug(sprintf('Saved so far: %s', $savedSoFar));
|
|
|
|
app('log')->debug(sprintf('Left to save: %s', $leftToSave));
|
|
|
|
app('log')->debug(sprintf('Maximum amount: %s', $maxAmount));
|
|
|
|
app('log')->debug(sprintf('Compare <= 0? %d, so %s', $compare, var_export($result, true)));
|
2020-03-21 14:20:40 +01:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canRemoveAmount(PiggyBank $piggyBank, string $amount): bool
|
|
|
|
{
|
|
|
|
$repetition = $this->getRepetition($piggyBank);
|
|
|
|
if (null === $repetition) {
|
|
|
|
return false;
|
|
|
|
}
|
2024-11-30 16:02:30 +01:00
|
|
|
$savedSoFar = $repetition->current_amount;
|
2020-03-21 14:20:40 +01:00
|
|
|
|
|
|
|
return bccomp($amount, $savedSoFar) <= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-12-20 19:35:52 +01:00
|
|
|
* @throws \Exception
|
2020-03-21 14:20:40 +01:00
|
|
|
*/
|
|
|
|
public function destroy(PiggyBank $piggyBank): bool
|
|
|
|
{
|
2020-06-07 16:38:15 +02:00
|
|
|
$piggyBank->objectGroups()->sync([]);
|
2020-03-21 14:20:40 +01:00
|
|
|
$piggyBank->delete();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-29 14:59:58 +02:00
|
|
|
public function removeObjectGroup(PiggyBank $piggyBank): PiggyBank
|
2021-03-21 09:15:40 +01:00
|
|
|
{
|
2022-03-29 14:59:58 +02:00
|
|
|
$piggyBank->objectGroups()->sync([]);
|
|
|
|
|
|
|
|
return $piggyBank;
|
2021-03-21 09:15:40 +01:00
|
|
|
}
|
|
|
|
|
2020-03-21 14:20:40 +01:00
|
|
|
public function setCurrentAmount(PiggyBank $piggyBank, string $amount): PiggyBank
|
|
|
|
{
|
2024-01-01 14:43:56 +01:00
|
|
|
$repetition = $this->getRepetition($piggyBank);
|
2020-03-21 14:20:40 +01:00
|
|
|
if (null === $repetition) {
|
|
|
|
return $piggyBank;
|
|
|
|
}
|
2024-11-30 16:02:30 +01:00
|
|
|
$max = $piggyBank->target_amount;
|
|
|
|
if (1 === bccomp($amount, $max) && 0 !== bccomp($piggyBank->target_amount, '0')) {
|
2020-03-21 14:20:40 +01:00
|
|
|
$amount = $max;
|
|
|
|
}
|
2024-11-30 16:02:30 +01:00
|
|
|
$difference = bcsub($amount, $repetition->current_amount);
|
|
|
|
$repetition->current_amount = $amount;
|
2020-03-21 14:20:40 +01:00
|
|
|
$repetition->save();
|
|
|
|
|
2022-12-11 07:17:59 +01:00
|
|
|
if (-1 === bccomp($difference, '0')) {
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug('addAmount [c]: Trigger change for negative amount.');
|
2023-09-21 11:29:09 +02:00
|
|
|
event(new ChangedAmount($piggyBank, $difference, null, null));
|
2022-12-11 07:17:59 +01:00
|
|
|
}
|
|
|
|
if (1 === bccomp($difference, '0')) {
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug('addAmount [d]: Trigger change for positive amount.');
|
2023-09-21 11:29:09 +02:00
|
|
|
event(new ChangedAmount($piggyBank, $difference, null, null));
|
2022-12-11 07:17:59 +01:00
|
|
|
}
|
2020-03-21 14:20:40 +01:00
|
|
|
|
|
|
|
return $piggyBank;
|
|
|
|
}
|
|
|
|
|
2021-03-12 06:20:01 +01:00
|
|
|
public function setObjectGroup(PiggyBank $piggyBank, string $objectGroupTitle): PiggyBank
|
|
|
|
{
|
|
|
|
$objectGroup = $this->findOrCreateObjectGroup($objectGroupTitle);
|
|
|
|
if (null !== $objectGroup) {
|
|
|
|
$piggyBank->objectGroups()->sync([$objectGroup->id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $piggyBank;
|
|
|
|
}
|
|
|
|
|
2023-05-29 13:56:55 +02:00
|
|
|
/**
|
2020-12-02 06:41:42 +01:00
|
|
|
* @throws FireflyException
|
2020-03-21 14:20:40 +01:00
|
|
|
*/
|
|
|
|
public function store(array $data): PiggyBank
|
|
|
|
{
|
2024-12-01 18:16:48 +01:00
|
|
|
$factory = new PiggyBankFactory();
|
|
|
|
$factory->setUser($this->user);
|
|
|
|
return $factory->store($data);
|
2020-03-21 14:20:40 +01:00
|
|
|
}
|
|
|
|
|
2023-06-21 12:34:58 +02:00
|
|
|
|
|
|
|
public function setOrder(PiggyBank $piggyBank, int $newOrder): bool
|
|
|
|
{
|
2024-01-01 14:43:56 +01:00
|
|
|
$oldOrder = $piggyBank->order;
|
2023-12-20 19:35:52 +01:00
|
|
|
// app('log')->debug(sprintf('Will move piggy bank #%d ("%s") from %d to %d', $piggyBank->id, $piggyBank->name, $oldOrder, $newOrder));
|
2023-06-21 12:34:58 +02:00
|
|
|
if ($newOrder > $oldOrder) {
|
|
|
|
$this->user->piggyBanks()->where('piggy_banks.order', '<=', $newOrder)->where('piggy_banks.order', '>', $oldOrder)
|
2023-12-20 19:35:52 +01:00
|
|
|
->where('piggy_banks.id', '!=', $piggyBank->id)
|
|
|
|
->decrement('piggy_banks.order')
|
|
|
|
;
|
2023-06-21 12:34:58 +02:00
|
|
|
$piggyBank->order = $newOrder;
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('[1] Order of piggy #%d ("%s") from %d to %d', $piggyBank->id, $piggyBank->name, $oldOrder, $newOrder));
|
2023-06-21 12:34:58 +02:00
|
|
|
$piggyBank->save();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->user->piggyBanks()->where('piggy_banks.order', '>=', $newOrder)->where('piggy_banks.order', '<', $oldOrder)
|
2023-12-20 19:35:52 +01:00
|
|
|
->where('piggy_banks.id', '!=', $piggyBank->id)
|
|
|
|
->increment('piggy_banks.order')
|
|
|
|
;
|
2023-06-21 12:34:58 +02:00
|
|
|
$piggyBank->order = $newOrder;
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('[2] Order of piggy #%d ("%s") from %d to %d', $piggyBank->id, $piggyBank->name, $oldOrder, $newOrder));
|
2023-06-21 12:34:58 +02:00
|
|
|
$piggyBank->save();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-12-01 18:16:48 +01:00
|
|
|
public function updateNote(PiggyBank $piggyBank, string $note): void
|
2024-02-22 20:11:09 +01:00
|
|
|
{
|
|
|
|
if ('' === $note) {
|
|
|
|
$dbNote = $piggyBank->notes()->first();
|
|
|
|
$dbNote?->delete();
|
2024-12-01 18:16:48 +01:00
|
|
|
return ;
|
2024-02-22 20:11:09 +01:00
|
|
|
}
|
|
|
|
$dbNote = $piggyBank->notes()->first();
|
|
|
|
if (null === $dbNote) {
|
|
|
|
$dbNote = new Note();
|
|
|
|
$dbNote->noteable()->associate($piggyBank);
|
|
|
|
}
|
|
|
|
$dbNote->text = trim($note);
|
|
|
|
$dbNote->save();
|
|
|
|
}
|
|
|
|
|
2020-10-27 06:53:33 +01:00
|
|
|
public function update(PiggyBank $piggyBank, array $data): PiggyBank
|
|
|
|
{
|
2024-01-01 14:43:56 +01:00
|
|
|
$piggyBank = $this->updateProperties($piggyBank, $data);
|
2021-03-14 06:20:23 +01:00
|
|
|
if (array_key_exists('notes', $data)) {
|
2022-12-11 07:17:59 +01:00
|
|
|
$this->updateNote($piggyBank, (string)$data['notes']);
|
2021-03-14 06:20:23 +01:00
|
|
|
}
|
2020-03-21 14:20:40 +01:00
|
|
|
|
2020-06-30 20:33:08 +02:00
|
|
|
// update the order of the piggy bank:
|
2024-01-01 14:43:56 +01:00
|
|
|
$oldOrder = $piggyBank->order;
|
|
|
|
$newOrder = (int)($data['order'] ?? $oldOrder);
|
2020-06-30 20:33:08 +02:00
|
|
|
if ($oldOrder !== $newOrder) {
|
2021-03-14 06:20:23 +01:00
|
|
|
$this->setOrder($piggyBank, $newOrder);
|
2020-06-30 20:33:08 +02:00
|
|
|
}
|
|
|
|
|
2020-03-21 14:20:40 +01:00
|
|
|
// if the piggy bank is now smaller than the current relevant rep,
|
|
|
|
// remove money from the rep.
|
|
|
|
$repetition = $this->getRepetition($piggyBank);
|
2024-11-30 16:02:30 +01:00
|
|
|
if (null !== $repetition && $repetition->current_amount > $piggyBank->target_amount && 0 !== bccomp($piggyBank->target_amount, '0')) {
|
|
|
|
$difference = bcsub($piggyBank->target_amount, $repetition->current_amount);
|
2022-12-11 07:17:59 +01:00
|
|
|
|
|
|
|
// an amount will be removed, create "negative" event:
|
2023-09-21 11:29:09 +02:00
|
|
|
event(new ChangedAmount($piggyBank, $difference, null, null));
|
2020-03-21 14:20:40 +01:00
|
|
|
|
2024-11-30 16:02:30 +01:00
|
|
|
$repetition->current_amount = $piggyBank->target_amount;
|
2020-03-21 14:20:40 +01:00
|
|
|
$repetition->save();
|
|
|
|
}
|
|
|
|
|
2020-06-30 19:05:35 +02:00
|
|
|
// update using name:
|
2021-03-14 06:20:23 +01:00
|
|
|
if (array_key_exists('object_group_title', $data)) {
|
2022-12-11 07:17:59 +01:00
|
|
|
$objectGroupTitle = (string)$data['object_group_title'];
|
2020-12-02 06:41:42 +01:00
|
|
|
if ('' !== $objectGroupTitle) {
|
|
|
|
$objectGroup = $this->findOrCreateObjectGroup($objectGroupTitle);
|
|
|
|
if (null !== $objectGroup) {
|
|
|
|
$piggyBank->objectGroups()->sync([$objectGroup->id]);
|
|
|
|
$piggyBank->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $piggyBank;
|
|
|
|
}
|
2023-11-05 16:55:16 +01:00
|
|
|
$piggyBank->objectGroups()->sync([]);
|
|
|
|
$piggyBank->save();
|
2020-06-30 19:05:35 +02:00
|
|
|
}
|
2020-10-20 04:42:44 +02:00
|
|
|
|
2020-06-30 19:05:35 +02:00
|
|
|
// try also with ID:
|
2020-12-02 06:41:42 +01:00
|
|
|
if (array_key_exists('object_group_id', $data)) {
|
2022-12-11 07:17:59 +01:00
|
|
|
$objectGroupId = (int)($data['object_group_id'] ?? 0);
|
2020-12-02 06:41:42 +01:00
|
|
|
if (0 !== $objectGroupId) {
|
|
|
|
$objectGroup = $this->findObjectGroupById($objectGroupId);
|
|
|
|
if (null !== $objectGroup) {
|
|
|
|
$piggyBank->objectGroups()->sync([$objectGroup->id]);
|
|
|
|
$piggyBank->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $piggyBank;
|
2020-06-07 11:31:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-21 14:20:40 +01:00
|
|
|
return $piggyBank;
|
|
|
|
}
|
|
|
|
|
2021-03-12 06:20:01 +01:00
|
|
|
private function updateProperties(PiggyBank $piggyBank, array $data): PiggyBank
|
|
|
|
{
|
|
|
|
if (array_key_exists('name', $data) && '' !== $data['name']) {
|
|
|
|
$piggyBank->name = $data['name'];
|
|
|
|
}
|
|
|
|
if (array_key_exists('account_id', $data) && 0 !== $data['account_id']) {
|
2022-12-11 07:17:59 +01:00
|
|
|
$piggyBank->account_id = (int)$data['account_id'];
|
2021-03-12 06:20:01 +01:00
|
|
|
}
|
|
|
|
if (array_key_exists('targetamount', $data) && '' !== $data['targetamount']) {
|
2024-11-30 16:02:30 +01:00
|
|
|
$piggyBank->target_amount = $data['targetamount'];
|
2021-03-12 06:20:01 +01:00
|
|
|
}
|
2022-12-24 05:48:04 +01:00
|
|
|
if (array_key_exists('targetamount', $data) && '' === $data['targetamount']) {
|
2024-11-30 16:02:30 +01:00
|
|
|
$piggyBank->target_amount = '0';
|
2022-12-24 05:48:04 +01:00
|
|
|
}
|
2021-03-12 06:20:01 +01:00
|
|
|
if (array_key_exists('targetdate', $data) && '' !== $data['targetdate']) {
|
2024-11-30 16:02:30 +01:00
|
|
|
$piggyBank->target_date = $data['targetdate'];
|
|
|
|
$piggyBank->target_date_tz = $data['targetdate']?->format('e');
|
2021-03-12 06:20:01 +01:00
|
|
|
}
|
2021-03-14 06:20:23 +01:00
|
|
|
if (array_key_exists('startdate', $data)) {
|
2024-11-30 16:02:30 +01:00
|
|
|
$piggyBank->start_date = $data['startdate'];
|
|
|
|
$piggyBank->start_date_tz = $data['targetdate']?->format('e');
|
2020-06-30 20:33:08 +02:00
|
|
|
}
|
2021-03-14 06:20:23 +01:00
|
|
|
$piggyBank->save();
|
2020-06-30 20:33:08 +02:00
|
|
|
|
2021-03-14 06:20:23 +01:00
|
|
|
return $piggyBank;
|
2020-06-30 20:33:08 +02:00
|
|
|
}
|
2020-05-01 17:47:56 +02:00
|
|
|
}
|