mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Unify piggy bank events. Does not save a link to the journal (yet)
This commit is contained in:
@@ -24,17 +24,15 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Repositories\PiggyBank;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Exception;
|
||||
use FireflyIII\Events\ChangedPiggyBankAmount;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\PiggyBank;
|
||||
use FireflyIII\Models\PiggyBankEvent;
|
||||
use FireflyIII\Models\PiggyBankRepetition;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Repositories\ObjectGroup\CreatesObjectGroups;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Log;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Trait ModifiesPiggyBanks
|
||||
@@ -44,8 +42,8 @@ trait ModifiesPiggyBanks
|
||||
use CreatesObjectGroups;
|
||||
|
||||
/**
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param string $amount
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param string $amount
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -59,30 +57,34 @@ trait ModifiesPiggyBanks
|
||||
$repetition->currentamount = bcadd($currentAmount, $amount);
|
||||
$repetition->save();
|
||||
|
||||
// create event
|
||||
//$this->createEvent($piggyBank, $amount);
|
||||
Log::debug('addAmount: Trigger change for positive amount.');
|
||||
event(new ChangedPiggyBankAmount($piggyBank, $amount, null, null));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PiggyBankRepetition $repetition
|
||||
* @param string $amount
|
||||
* @param PiggyBankRepetition $repetition
|
||||
* @param string $amount
|
||||
*
|
||||
* @return string
|
||||
* @return void
|
||||
*/
|
||||
public function addAmountToRepetition(PiggyBankRepetition $repetition, string $amount): string
|
||||
public function addAmountToRepetition(PiggyBankRepetition $repetition, string $amount): void
|
||||
{
|
||||
$newAmount = bcadd($repetition->currentamount, $amount);
|
||||
$repetition->currentamount = $newAmount;
|
||||
$repetition->save();
|
||||
|
||||
return $newAmount;
|
||||
Log::debug(sprintf('addAmountToRepetition: %s', $amount));
|
||||
if (-1 === bccomp($amount, '0')) {
|
||||
Log::debug('Remove amount.');
|
||||
$this->removeAmount($repetition->piggyBank, bcmul($amount, '-1'));
|
||||
}
|
||||
if (1 === bccomp($amount, '0')) {
|
||||
Log::debug('Add amount.');
|
||||
$this->addAmount($repetition->piggyBank, $amount);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param string $amount
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param string $amount
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -90,10 +92,10 @@ trait ModifiesPiggyBanks
|
||||
{
|
||||
$today = today(config('app.timezone'));
|
||||
$leftOnAccount = $this->leftOnAccount($piggyBank, $today);
|
||||
$savedSoFar = (string) $this->getRepetition($piggyBank)->currentamount;
|
||||
$savedSoFar = (string)$this->getRepetition($piggyBank)->currentamount;
|
||||
$maxAmount = $leftOnAccount;
|
||||
$leftToSave = null;
|
||||
if (0.0 !== (float) $piggyBank->targetamount) {
|
||||
if (0.0 !== (float)$piggyBank->targetamount) {
|
||||
$leftToSave = bcsub($piggyBank->targetamount, $savedSoFar);
|
||||
$maxAmount = 1 === bccomp($leftOnAccount, $leftToSave) ? $leftToSave : $leftOnAccount;
|
||||
}
|
||||
@@ -111,8 +113,8 @@ trait ModifiesPiggyBanks
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param string $amount
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param string $amount
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -128,25 +130,7 @@ trait ModifiesPiggyBanks
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param string $amount
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return PiggyBankEvent
|
||||
*/
|
||||
public function createEventWithJournal(PiggyBank $piggyBank, string $amount, TransactionJournal $journal): PiggyBankEvent
|
||||
{
|
||||
return PiggyBankEvent::create(
|
||||
[
|
||||
'piggy_bank_id' => $piggyBank->id,
|
||||
'transaction_journal_id' => $journal->id,
|
||||
'date' => $journal->date->format('Y-m-d'),
|
||||
'amount' => $amount]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param PiggyBank $piggyBank
|
||||
*
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
@@ -160,8 +144,8 @@ trait ModifiesPiggyBanks
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param string $amount
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param string $amount
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -174,27 +158,12 @@ trait ModifiesPiggyBanks
|
||||
$repetition->currentamount = bcsub($repetition->currentamount, $amount);
|
||||
$repetition->save();
|
||||
|
||||
// create event
|
||||
$this->createEvent($piggyBank, bcmul($amount, '-1'));
|
||||
Log::debug('addAmount: Trigger change for negative amount.');
|
||||
event(new ChangedPiggyBankAmount($piggyBank, bcmul($amount, '-1'), null, null));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param string $amount
|
||||
*
|
||||
* @return PiggyBankEvent
|
||||
*/
|
||||
public function createEvent(PiggyBank $piggyBank, string $amount): PiggyBankEvent
|
||||
{
|
||||
if (0 === bccomp('0', $amount)) {
|
||||
return new PiggyBankEvent;
|
||||
}
|
||||
|
||||
return PiggyBankEvent::create(['date' => Carbon::now(), 'amount' => $amount, 'piggy_bank_id' => $piggyBank->id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
@@ -206,8 +175,8 @@ trait ModifiesPiggyBanks
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param string $amount
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param string $amount
|
||||
*
|
||||
* @return PiggyBank
|
||||
*/
|
||||
@@ -225,8 +194,14 @@ trait ModifiesPiggyBanks
|
||||
$repetition->currentamount = $amount;
|
||||
$repetition->save();
|
||||
|
||||
// create event
|
||||
$this->createEvent($piggyBank, $difference);
|
||||
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));
|
||||
}
|
||||
|
||||
return $piggyBank;
|
||||
}
|
||||
@@ -242,11 +217,10 @@ trait ModifiesPiggyBanks
|
||||
}
|
||||
|
||||
return $piggyBank;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @param array $data
|
||||
*
|
||||
* @return PiggyBank
|
||||
* @throws FireflyException
|
||||
@@ -263,7 +237,7 @@ trait ModifiesPiggyBanks
|
||||
unset($piggyData['object_group_title'], $piggyData['object_group_id'], $piggyData['notes'], $piggyData['current_amount']);
|
||||
|
||||
// validate amount:
|
||||
if (array_key_exists('targetamount', $piggyData) && '' === (string) $piggyData['targetamount']) {
|
||||
if (array_key_exists('targetamount', $piggyData) && '' === (string)$piggyData['targetamount']) {
|
||||
$piggyData['targetamount'] = '0';
|
||||
}
|
||||
|
||||
@@ -295,10 +269,9 @@ trait ModifiesPiggyBanks
|
||||
$piggyBank->objectGroups()->sync([$objectGroup->id]);
|
||||
$piggyBank->save();
|
||||
}
|
||||
|
||||
}
|
||||
// try also with ID
|
||||
$objectGroupId = (int) ($data['object_group_id'] ?? 0);
|
||||
$objectGroupId = (int)($data['object_group_id'] ?? 0);
|
||||
if (0 !== $objectGroupId) {
|
||||
$objectGroup = $this->findObjectGroupById($objectGroupId);
|
||||
if (null !== $objectGroup) {
|
||||
@@ -318,7 +291,7 @@ trait ModifiesPiggyBanks
|
||||
$set = $this->user->piggyBanks()->orderBy('piggy_banks.order', 'ASC')->get(['piggy_banks.*']);
|
||||
$current = 1;
|
||||
foreach ($set as $piggyBank) {
|
||||
if ((int) $piggyBank->order !== $current) {
|
||||
if ((int)$piggyBank->order !== $current) {
|
||||
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();
|
||||
@@ -332,7 +305,7 @@ trait ModifiesPiggyBanks
|
||||
*/
|
||||
public function setOrder(PiggyBank $piggyBank, int $newOrder): bool
|
||||
{
|
||||
$oldOrder = (int) $piggyBank->order;
|
||||
$oldOrder = (int)$piggyBank->order;
|
||||
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)
|
||||
@@ -356,8 +329,8 @@ trait ModifiesPiggyBanks
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param string $note
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param string $note
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -387,8 +360,8 @@ trait ModifiesPiggyBanks
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param array $data
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param array $data
|
||||
*
|
||||
* @return PiggyBank
|
||||
*/
|
||||
@@ -396,12 +369,12 @@ trait ModifiesPiggyBanks
|
||||
{
|
||||
$piggyBank = $this->updateProperties($piggyBank, $data);
|
||||
if (array_key_exists('notes', $data)) {
|
||||
$this->updateNote($piggyBank, (string) $data['notes']);
|
||||
$this->updateNote($piggyBank, (string)$data['notes']);
|
||||
}
|
||||
|
||||
// update the order of the piggy bank:
|
||||
$oldOrder = (int) $piggyBank->order;
|
||||
$newOrder = (int) ($data['order'] ?? $oldOrder);
|
||||
$oldOrder = (int)$piggyBank->order;
|
||||
$newOrder = (int)($data['order'] ?? $oldOrder);
|
||||
if ($oldOrder !== $newOrder) {
|
||||
$this->setOrder($piggyBank, $newOrder);
|
||||
}
|
||||
@@ -409,9 +382,11 @@ trait ModifiesPiggyBanks
|
||||
// if the piggy bank is now smaller than the current relevant rep,
|
||||
// remove money from the rep.
|
||||
$repetition = $this->getRepetition($piggyBank);
|
||||
if (null !== $repetition && $repetition->currentamount > $piggyBank->targetamount && 0.0 !== (float) $piggyBank->targetamount) {
|
||||
$diff = bcsub($piggyBank->targetamount, $repetition->currentamount);
|
||||
$this->createEvent($piggyBank, $diff);
|
||||
if (null !== $repetition && $repetition->currentamount > $piggyBank->targetamount && 0.0 !== (float)$piggyBank->targetamount) {
|
||||
$difference = bcsub($piggyBank->targetamount, $repetition->currentamount);
|
||||
|
||||
// an amount will be removed, create "negative" event:
|
||||
event(new ChangedPiggyBankAmount($piggyBank, $difference, null, null));
|
||||
|
||||
$repetition->currentamount = $piggyBank->targetamount;
|
||||
$repetition->save();
|
||||
@@ -419,7 +394,7 @@ trait ModifiesPiggyBanks
|
||||
|
||||
// update using name:
|
||||
if (array_key_exists('object_group_title', $data)) {
|
||||
$objectGroupTitle = (string) $data['object_group_title'];
|
||||
$objectGroupTitle = (string)$data['object_group_title'];
|
||||
if ('' !== $objectGroupTitle) {
|
||||
$objectGroup = $this->findOrCreateObjectGroup($objectGroupTitle);
|
||||
if (null !== $objectGroup) {
|
||||
@@ -438,7 +413,7 @@ trait ModifiesPiggyBanks
|
||||
|
||||
// try also with ID:
|
||||
if (array_key_exists('object_group_id', $data)) {
|
||||
$objectGroupId = (int) ($data['object_group_id'] ?? 0);
|
||||
$objectGroupId = (int)($data['object_group_id'] ?? 0);
|
||||
if (0 !== $objectGroupId) {
|
||||
$objectGroup = $this->findObjectGroupById($objectGroupId);
|
||||
if (null !== $objectGroup) {
|
||||
@@ -454,8 +429,8 @@ trait ModifiesPiggyBanks
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param array $data
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param array $data
|
||||
*
|
||||
* @return PiggyBank
|
||||
*/
|
||||
@@ -465,7 +440,7 @@ trait ModifiesPiggyBanks
|
||||
$piggyBank->name = $data['name'];
|
||||
}
|
||||
if (array_key_exists('account_id', $data) && 0 !== $data['account_id']) {
|
||||
$piggyBank->account_id = (int) $data['account_id'];
|
||||
$piggyBank->account_id = (int)$data['account_id'];
|
||||
}
|
||||
if (array_key_exists('targetamount', $data) && '' !== $data['targetamount']) {
|
||||
$piggyBank->targetamount = $data['targetamount'];
|
||||
@@ -475,7 +450,6 @@ trait ModifiesPiggyBanks
|
||||
}
|
||||
if (array_key_exists('startdate', $data)) {
|
||||
$piggyBank->startdate = $data['startdate'];
|
||||
|
||||
}
|
||||
$piggyBank->save();
|
||||
|
||||
|
@@ -47,10 +47,8 @@ interface PiggyBankRepositoryInterface
|
||||
/**
|
||||
* @param PiggyBankRepetition $repetition
|
||||
* @param string $amount
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function addAmountToRepetition(PiggyBankRepetition $repetition, string $amount): string;
|
||||
public function addAmountToRepetition(PiggyBankRepetition $repetition, string $amount): void;
|
||||
|
||||
/**
|
||||
* @param PiggyBank $piggyBank
|
||||
@@ -68,25 +66,6 @@ interface PiggyBankRepositoryInterface
|
||||
*/
|
||||
public function canRemoveAmount(PiggyBank $piggyBank, string $amount): bool;
|
||||
|
||||
/**
|
||||
* Create a new event.
|
||||
*
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param string $amount
|
||||
*
|
||||
* @return PiggyBankEvent
|
||||
*/
|
||||
public function createEvent(PiggyBank $piggyBank, string $amount): PiggyBankEvent;
|
||||
|
||||
/**
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param string $amount
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return PiggyBankEvent
|
||||
*/
|
||||
public function createEventWithJournal(PiggyBank $piggyBank, string $amount, TransactionJournal $journal): PiggyBankEvent;
|
||||
|
||||
/**
|
||||
* Destroy piggy bank.
|
||||
*
|
||||
|
Reference in New Issue
Block a user