Files
firefly-iii/app/Repositories/PiggyBank/ModifiesPiggyBanks.php

463 lines
15 KiB
PHP
Raw Normal View History

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
2020-03-21 14:20:40 +01:00
use Exception;
use FireflyIII\Events\ChangedPiggyBankAmount;
2020-03-21 14:20:40 +01:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Note;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\PiggyBankRepetition;
use FireflyIII\Models\TransactionJournal;
2020-06-07 11:31:01 +02:00
use FireflyIII\Repositories\ObjectGroup\CreatesObjectGroups;
2020-03-21 14:20:40 +01:00
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Log;
2020-03-21 14:20:40 +01:00
/**
* Trait ModifiesPiggyBanks
*/
trait ModifiesPiggyBanks
{
2020-06-07 11:31:01 +02:00
use CreatesObjectGroups;
2020-12-02 06:41:42 +01:00
2020-03-21 14:20:40 +01:00
/**
* @param PiggyBank $piggyBank
* @param string $amount
2022-12-11 07:29:06 +01:00
* @param TransactionJournal|null $journal
2020-03-21 14:20:40 +01:00
* @return bool
*/
2022-12-11 07:29:06 +01:00
public function addAmount(PiggyBank $piggyBank, string $amount, ?TransactionJournal $journal = null): bool
2020-03-21 14:20:40 +01:00
{
$repetition = $this->getRepetition($piggyBank);
if (null === $repetition) {
return false;
}
$currentAmount = $repetition->currentamount ?? '0';
$repetition->currentamount = bcadd($currentAmount, $amount);
$repetition->save();
Log::debug('addAmount: Trigger change for positive amount.');
2022-12-11 07:29:06 +01:00
event(new ChangedPiggyBankAmount($piggyBank, $amount, $journal, null));
2020-03-21 14:20:40 +01:00
return true;
}
/**
* @param PiggyBankRepetition $repetition
* @param string $amount
2020-03-21 14:20:40 +01:00
*
* @return void
2020-03-21 14:20:40 +01:00
*/
2022-12-11 07:29:06 +01:00
public function addAmountToRepetition(PiggyBankRepetition $repetition, string $amount, TransactionJournal $journal): void
2020-03-21 14:20:40 +01:00
{
Log::debug(sprintf('addAmountToRepetition: %s', $amount));
if (-1 === bccomp($amount, '0')) {
Log::debug('Remove amount.');
2022-12-11 07:29:06 +01:00
$this->removeAmount($repetition->piggyBank, bcmul($amount, '-1'), $journal);
}
if (1 === bccomp($amount, '0')) {
Log::debug('Add amount.');
2022-12-11 07:29:06 +01:00
$this->addAmount($repetition->piggyBank, $amount, $journal);
}
2020-03-21 14:20:40 +01:00
}
/**
* @param PiggyBank $piggyBank
* @param string $amount
2020-03-21 14:20:40 +01:00
*
* @return bool
*/
public function canAddAmount(PiggyBank $piggyBank, string $amount): bool
{
$today = today(config('app.timezone'));
2021-09-11 06:52:49 +02:00
$leftOnAccount = $this->leftOnAccount($piggyBank, $today);
$savedSoFar = (string)$this->getRepetition($piggyBank)->currentamount;
2022-03-27 18:30:46 +02:00
$maxAmount = $leftOnAccount;
$leftToSave = null;
2022-12-24 05:06:39 +01:00
if (0 !== bccomp($piggyBank->targetamount, '0')) {
2022-03-27 18:30:46 +02:00
$leftToSave = bcsub($piggyBank->targetamount, $savedSoFar);
$maxAmount = 1 === bccomp($leftOnAccount, $leftToSave) ? $leftToSave : $leftOnAccount;
}
$compare = bccomp($amount, $maxAmount);
$result = $compare <= 0;
2020-03-21 14:20:40 +01:00
2021-09-11 06:52:49 +02:00
Log::debug(sprintf('Left on account: %s on %s', $leftOnAccount, $today->format('Y-m-d')));
2020-03-21 14:20:40 +01:00
Log::debug(sprintf('Saved so far: %s', $savedSoFar));
Log::debug(sprintf('Left to save: %s', $leftToSave));
Log::debug(sprintf('Maximum amount: %s', $maxAmount));
Log::debug(sprintf('Compare <= 0? %d, so %s', $compare, var_export($result, true)));
return $result;
}
/**
* @param PiggyBank $piggyBank
* @param string $amount
2020-03-21 14:20:40 +01:00
*
* @return bool
*/
public function canRemoveAmount(PiggyBank $piggyBank, string $amount): bool
{
$repetition = $this->getRepetition($piggyBank);
if (null === $repetition) {
return false;
}
$savedSoFar = $repetition->currentamount;
return bccomp($amount, $savedSoFar) <= 0;
}
/**
* @param PiggyBank $piggyBank
2020-03-21 14:20:40 +01:00
*
* @return bool
2021-03-12 06:20:01 +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;
}
/**
* @param PiggyBank $piggyBank
* @param string $amount
2020-03-21 14:20:40 +01:00
*
* @return bool
*/
2022-12-11 07:29:06 +01:00
public function removeAmount(PiggyBank $piggyBank, string $amount, ?TransactionJournal $journal = null): bool
2020-03-21 14:20:40 +01:00
{
$repetition = $this->getRepetition($piggyBank);
if (null === $repetition) {
return false;
}
$repetition->currentamount = bcsub($repetition->currentamount, $amount);
$repetition->save();
Log::debug('addAmount: Trigger change for negative amount.');
2022-12-11 07:29:06 +01:00
event(new ChangedPiggyBankAmount($piggyBank, bcmul($amount, '-1'), $journal, null));
2020-03-21 14:20:40 +01:00
return true;
}
2021-03-21 09:15:40 +01:00
/**
2022-03-29 14:59:58 +02:00
* @inheritDoc
2021-03-21 09:15:40 +01:00
*/
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
/**
* @param PiggyBank $piggyBank
* @param string $amount
2020-03-21 14:20:40 +01:00
*
* @return PiggyBank
*/
public function setCurrentAmount(PiggyBank $piggyBank, string $amount): PiggyBank
{
$repetition = $this->getRepetition($piggyBank);
if (null === $repetition) {
return $piggyBank;
}
$max = $piggyBank->targetamount;
if (1 === bccomp($amount, $max)) {
$amount = $max;
}
2020-05-01 17:47:56 +02:00
$difference = bcsub($amount, $repetition->currentamount);
2020-03-21 14:20:40 +01:00
$repetition->currentamount = $amount;
$repetition->save();
if (-1 === bccomp($difference, '0')) {
Log::debug('addAmount: Trigger change for negative amount.');
event(new ChangedPiggyBankAmount($piggyBank, bcmul($amount, '-1'), null, null));
}
if (1 === bccomp($difference, '0')) {
Log::debug('addAmount: Trigger change for positive amount.');
event(new ChangedPiggyBankAmount($piggyBank, $amount, null, null));
}
2020-03-21 14:20:40 +01:00
return $piggyBank;
}
2021-03-12 06:20:01 +01:00
/**
* @inheritDoc
*/
public function setObjectGroup(PiggyBank $piggyBank, string $objectGroupTitle): PiggyBank
{
$objectGroup = $this->findOrCreateObjectGroup($objectGroupTitle);
if (null !== $objectGroup) {
$piggyBank->objectGroups()->sync([$objectGroup->id]);
}
return $piggyBank;
}
2020-03-21 14:20:40 +01:00
/**
* @param array $data
2020-03-21 14:20:40 +01:00
*
* @return PiggyBank
2020-12-02 06:41:42 +01:00
* @throws FireflyException
2020-03-21 14:20:40 +01:00
*/
public function store(array $data): PiggyBank
{
2021-03-14 06:20:23 +01:00
$order = $this->getMaxOrder() + 1;
if (array_key_exists('order', $data)) {
$order = $data['order'];
}
$data['order'] = 31337; // very high when creating.
2021-02-27 05:59:40 +01:00
$piggyData = $data;
// unset fields just in case.
unset($piggyData['object_group_title'], $piggyData['object_group_id'], $piggyData['notes'], $piggyData['current_amount']);
2022-04-09 07:05:31 +02:00
// validate amount:
if (array_key_exists('targetamount', $piggyData) && '' === (string)$piggyData['targetamount']) {
2022-04-09 07:06:41 +02:00
$piggyData['targetamount'] = '0';
2022-04-09 07:05:31 +02:00
}
2020-03-21 14:20:40 +01:00
try {
/** @var PiggyBank $piggyBank */
2021-02-27 05:59:40 +01:00
$piggyBank = PiggyBank::create($piggyData);
2020-03-21 14:20:40 +01:00
} catch (QueryException $e) {
2021-02-27 05:59:40 +01:00
Log::error(sprintf('Could not store piggy bank: %s', $e->getMessage()), $piggyData);
2021-07-04 07:58:11 +02:00
throw new FireflyException('400005: Could not store new piggy bank.', 0, $e);
2020-03-21 14:20:40 +01:00
}
2021-03-14 06:20:23 +01:00
// reset order then set order:
$this->resetOrder();
$this->setOrder($piggyBank, $order);
2020-03-21 14:20:40 +01:00
$this->updateNote($piggyBank, $data['notes']);
// repetition is auto created.
$repetition = $this->getRepetition($piggyBank);
2021-07-04 07:58:11 +02:00
if (null !== $repetition && array_key_exists('current_amount', $data) && '' !== $data['current_amount']) {
2020-03-21 14:20:40 +01:00
$repetition->currentamount = $data['current_amount'];
$repetition->save();
}
2021-02-27 05:59:40 +01:00
$objectGroupTitle = $data['object_group_title'] ?? '';
2020-06-07 11:31:01 +02:00
if ('' !== $objectGroupTitle) {
2020-06-21 18:28:51 +02:00
$objectGroup = $this->findOrCreateObjectGroup($objectGroupTitle);
if (null !== $objectGroup) {
$piggyBank->objectGroups()->sync([$objectGroup->id]);
$piggyBank->save();
}
}
2021-02-27 05:59:40 +01:00
// try also with ID
$objectGroupId = (int)($data['object_group_id'] ?? 0);
2020-06-21 18:28:51 +02:00
if (0 !== $objectGroupId) {
$objectGroup = $this->findObjectGroupById($objectGroupId);
2020-06-07 11:31:01 +02:00
if (null !== $objectGroup) {
$piggyBank->objectGroups()->sync([$objectGroup->id]);
$piggyBank->save();
}
}
2020-03-21 14:20:40 +01:00
return $piggyBank;
}
2022-03-29 14:59:58 +02:00
/**
* Correct order of piggies in case of issues.
*/
public function resetOrder(): void
{
$set = $this->user->piggyBanks()->orderBy('piggy_banks.order', 'ASC')->get(['piggy_banks.*']);
$current = 1;
foreach ($set as $piggyBank) {
if ((int)$piggyBank->order !== $current) {
2022-03-29 14:59:58 +02:00
Log::debug(sprintf('Piggy bank #%d ("%s") was at place %d but should be on %d', $piggyBank->id, $piggyBank->name, $piggyBank->order, $current));
$piggyBank->order = $current;
$piggyBank->save();
}
$current++;
}
}
/**
* @inheritDoc
*/
public function setOrder(PiggyBank $piggyBank, int $newOrder): bool
{
$oldOrder = (int)$piggyBank->order;
2022-03-29 14:59:58 +02:00
Log::debug(sprintf('Will move piggy bank #%d ("%s") from %d to %d', $piggyBank->id, $piggyBank->name, $oldOrder, $newOrder));
if ($newOrder > $oldOrder) {
$this->user->piggyBanks()->where('piggy_banks.order', '<=', $newOrder)->where('piggy_banks.order', '>', $oldOrder)
->where('piggy_banks.id', '!=', $piggyBank->id)
->decrement('piggy_banks.order');
$piggyBank->order = $newOrder;
Log::debug(sprintf('Order of piggy #%d ("%s") is now %d', $piggyBank->id, $piggyBank->name, $newOrder));
$piggyBank->save();
return true;
}
$this->user->piggyBanks()->where('piggy_banks.order', '>=', $newOrder)->where('piggy_banks.order', '<', $oldOrder)
->where('piggy_banks.id', '!=', $piggyBank->id)
->increment('piggy_banks.order');
$piggyBank->order = $newOrder;
Log::debug(sprintf('Order of piggy #%d ("%s") is now %d', $piggyBank->id, $piggyBank->name, $newOrder));
$piggyBank->save();
return true;
}
/**
* @param PiggyBank $piggyBank
* @param string $note
2022-03-29 14:59:58 +02:00
*
* @return bool
*/
private function updateNote(PiggyBank $piggyBank, string $note): bool
{
if ('' === $note) {
$dbNote = $piggyBank->notes()->first();
if (null !== $dbNote) {
try {
$dbNote->delete();
2022-10-31 05:53:36 +01:00
} catch (Exception $e) {
2022-03-29 14:59:58 +02:00
// @ignoreException
}
}
return true;
}
$dbNote = $piggyBank->notes()->first();
if (null === $dbNote) {
2022-10-30 14:24:28 +01:00
$dbNote = new Note();
2022-03-29 14:59:58 +02:00
$dbNote->noteable()->associate($piggyBank);
}
$dbNote->text = trim($note);
$dbNote->save();
return true;
}
2020-10-27 06:53:33 +01:00
/**
* @param PiggyBank $piggyBank
* @param array $data
2020-10-27 06:53:33 +01:00
*
* @return PiggyBank
*/
public function update(PiggyBank $piggyBank, array $data): PiggyBank
{
$piggyBank = $this->updateProperties($piggyBank, $data);
2021-03-14 06:20:23 +01:00
if (array_key_exists('notes', $data)) {
$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:
$oldOrder = (int)$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);
2022-12-24 05:06:39 +01:00
if (null !== $repetition && $repetition->currentamount > $piggyBank->targetamount && 0 !== bccomp($piggyBank->targetamount, '0')) {
$difference = bcsub($piggyBank->targetamount, $repetition->currentamount);
// an amount will be removed, create "negative" event:
event(new ChangedPiggyBankAmount($piggyBank, $difference, null, null));
2020-03-21 14:20:40 +01:00
$repetition->currentamount = $piggyBank->targetamount;
$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)) {
$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;
}
// remove if name is empty. Should be overruled by ID.
if ('' === $objectGroupTitle) {
$piggyBank->objectGroups()->sync([]);
2020-06-30 19:05:35 +02:00
$piggyBank->save();
}
}
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)) {
$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
/**
* @param PiggyBank $piggyBank
* @param array $data
2021-03-12 06:20:01 +01:00
*
* @return PiggyBank
*/
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']) {
$piggyBank->account_id = (int)$data['account_id'];
2021-03-12 06:20:01 +01:00
}
if (array_key_exists('targetamount', $data) && '' !== $data['targetamount']) {
$piggyBank->targetamount = $data['targetamount'];
}
2022-12-24 05:48:04 +01:00
if (array_key_exists('targetamount', $data) && '' === $data['targetamount']) {
$piggyBank->targetamount = '0';
}
2021-03-12 06:20:01 +01:00
if (array_key_exists('targetdate', $data) && '' !== $data['targetdate']) {
$piggyBank->targetdate = $data['targetdate'];
}
2021-03-14 06:20:23 +01:00
if (array_key_exists('startdate', $data)) {
$piggyBank->startdate = $data['startdate'];
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
}