mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-17 01:42:19 +00:00
Fix #4113
This commit is contained in:
@@ -23,7 +23,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace FireflyIII\Api\V1\Requests;
|
namespace FireflyIII\Api\V1\Requests;
|
||||||
|
|
||||||
use FireflyIII\Models\PiggyBank;
|
|
||||||
use FireflyIII\Rules\IsAssetAccountId;
|
use FireflyIII\Rules\IsAssetAccountId;
|
||||||
use FireflyIII\Rules\LessThanPiggyTarget;
|
use FireflyIII\Rules\LessThanPiggyTarget;
|
||||||
use FireflyIII\Support\Request\ChecksLogin;
|
use FireflyIII\Support\Request\ChecksLogin;
|
||||||
@@ -46,16 +45,31 @@ class PiggyBankUpdateRequest extends FormRequest
|
|||||||
*/
|
*/
|
||||||
public function getAll(): array
|
public function getAll(): array
|
||||||
{
|
{
|
||||||
return [
|
// if the value isn't present, dont return it at all.
|
||||||
'name' => $this->string('name'),
|
// TODO this should be the way to collect fields for all API things.
|
||||||
'account_id' => $this->integer('account_id'),
|
// TODO make sure piggy bank uses 'start_date' etc. until right up to DB update.
|
||||||
'targetamount' => $this->string('target_amount'),
|
// TODO can we configure this and return it from config?
|
||||||
'current_amount' => $this->string('current_amount'),
|
$return = [];
|
||||||
'startdate' => $this->date('start_date'),
|
$fields = [
|
||||||
'targetdate' => $this->date('target_date'),
|
'name' => ['name', 'string'],
|
||||||
'notes' => $this->nlString('notes'),
|
'account_id' => ['account_id', 'integer'],
|
||||||
'order' => $this->integer('order'),
|
'targetamount' => ['target_amount', 'string'],
|
||||||
|
'current_amount' => ['current_amount', 'string'],
|
||||||
|
'startdate' => ['start_date', 'date'],
|
||||||
|
'targetdate' => ['target_date', 'string'],
|
||||||
|
'notes' => ['notes', 'nlString'],
|
||||||
|
'order' => ['order', 'integer'],
|
||||||
|
'object_group' => ['object_group', 'string'],
|
||||||
|
'object_group_id' => ['object_group_id', 'integer'],
|
||||||
];
|
];
|
||||||
|
foreach ($fields as $field => $info) {
|
||||||
|
if ($this->has($info[0])) {
|
||||||
|
$method = $info[1];
|
||||||
|
$return[$field] = $this->$method($info[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -65,15 +79,16 @@ class PiggyBankUpdateRequest extends FormRequest
|
|||||||
*/
|
*/
|
||||||
public function rules(): array
|
public function rules(): array
|
||||||
{
|
{
|
||||||
$piggyBank = $this->route()->parameter('piggyBank');
|
$piggyBank = $this->route()->parameter('piggyBank');
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'name' => 'between:1,255|uniquePiggyBankForUser:' . $piggyBank->id,
|
'name' => 'between:1,255|uniquePiggyBankForUser:' . $piggyBank->id,
|
||||||
'current_amount' => ['numeric', 'gte:0', new LessThanPiggyTarget],
|
'current_amount' => ['numeric', 'gte:0', new LessThanPiggyTarget],
|
||||||
'target_amount' => 'numeric|gt:0',
|
'target_amount' => 'numeric|gt:0',
|
||||||
'start_date' => 'date|nullable',
|
'start_date' => 'date|nullable',
|
||||||
'target_date' => 'date|nullable|after:start_date',
|
'target_date' => 'date|nullable|after:start_date',
|
||||||
'notes' => 'max:65000',
|
'notes' => 'max:65000',
|
||||||
'account_id' => ['belongsToUser:accounts', new IsAssetAccountId],
|
'account_id' => ['belongsToUser:accounts', new IsAssetAccountId],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -45,6 +45,7 @@ use Log;
|
|||||||
trait ModifiesPiggyBanks
|
trait ModifiesPiggyBanks
|
||||||
{
|
{
|
||||||
use CreatesObjectGroups;
|
use CreatesObjectGroups;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param PiggyBank $piggyBank
|
* @param PiggyBank $piggyBank
|
||||||
* @param string $amount
|
* @param string $amount
|
||||||
@@ -102,9 +103,9 @@ trait ModifiesPiggyBanks
|
|||||||
public function canAddAmount(PiggyBank $piggyBank, string $amount): bool
|
public function canAddAmount(PiggyBank $piggyBank, string $amount): bool
|
||||||
{
|
{
|
||||||
$leftOnAccount = $this->leftOnAccount($piggyBank, today(config('app.timezone')));
|
$leftOnAccount = $this->leftOnAccount($piggyBank, today(config('app.timezone')));
|
||||||
$savedSoFar = (string) $this->getRepetition($piggyBank)->currentamount;
|
$savedSoFar = (string)$this->getRepetition($piggyBank)->currentamount;
|
||||||
$leftToSave = bcsub($piggyBank->targetamount, $savedSoFar);
|
$leftToSave = bcsub($piggyBank->targetamount, $savedSoFar);
|
||||||
$maxAmount = (string) min(round($leftOnAccount, 12), round($leftToSave, 12));
|
$maxAmount = (string)min(round($leftOnAccount, 12), round($leftToSave, 12));
|
||||||
$compare = bccomp($amount, $maxAmount);
|
$compare = bccomp($amount, $maxAmount);
|
||||||
$result = $compare <= 0;
|
$result = $compare <= 0;
|
||||||
|
|
||||||
@@ -142,7 +143,7 @@ trait ModifiesPiggyBanks
|
|||||||
$set = $this->user->piggyBanks()->orderBy('order', 'ASC')->get();
|
$set = $this->user->piggyBanks()->orderBy('order', 'ASC')->get();
|
||||||
$current = 1;
|
$current = 1;
|
||||||
foreach ($set as $piggyBank) {
|
foreach ($set as $piggyBank) {
|
||||||
if ((int) $piggyBank->order !== $current) {
|
if ((int)$piggyBank->order !== $current) {
|
||||||
$piggyBank->order = $current;
|
$piggyBank->order = $current;
|
||||||
$piggyBank->save();
|
$piggyBank->save();
|
||||||
}
|
}
|
||||||
@@ -161,6 +162,7 @@ trait ModifiesPiggyBanks
|
|||||||
if (0 === bccomp('0', $amount)) {
|
if (0 === bccomp('0', $amount)) {
|
||||||
return new PiggyBankEvent;
|
return new PiggyBankEvent;
|
||||||
}
|
}
|
||||||
|
|
||||||
return PiggyBankEvent::create(['date' => Carbon::now(), 'amount' => $amount, 'piggy_bank_id' => $piggyBank->id]);
|
return PiggyBankEvent::create(['date' => Carbon::now(), 'amount' => $amount, 'piggy_bank_id' => $piggyBank->id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,8 +187,8 @@ trait ModifiesPiggyBanks
|
|||||||
/**
|
/**
|
||||||
* @param PiggyBank $piggyBank
|
* @param PiggyBank $piggyBank
|
||||||
*
|
*
|
||||||
* @throws \Exception
|
|
||||||
* @return bool
|
* @return bool
|
||||||
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public function destroy(PiggyBank $piggyBank): bool
|
public function destroy(PiggyBank $piggyBank): bool
|
||||||
{
|
{
|
||||||
@@ -278,8 +280,8 @@ trait ModifiesPiggyBanks
|
|||||||
/**
|
/**
|
||||||
* @param array $data
|
* @param array $data
|
||||||
*
|
*
|
||||||
* @throws FireflyException
|
|
||||||
* @return PiggyBank
|
* @return PiggyBank
|
||||||
|
* @throws FireflyException
|
||||||
*/
|
*/
|
||||||
public function store(array $data): PiggyBank
|
public function store(array $data): PiggyBank
|
||||||
{
|
{
|
||||||
@@ -310,7 +312,7 @@ trait ModifiesPiggyBanks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// try also with ID:
|
// try also with ID:
|
||||||
$objectGroupId = (int) ($data['object_group_id'] ?? 0);
|
$objectGroupId = (int)($data['object_group_id'] ?? 0);
|
||||||
if (0 !== $objectGroupId) {
|
if (0 !== $objectGroupId) {
|
||||||
$objectGroup = $this->findObjectGroupById($objectGroupId);
|
$objectGroup = $this->findObjectGroupById($objectGroupId);
|
||||||
if (null !== $objectGroup) {
|
if (null !== $objectGroup) {
|
||||||
@@ -328,12 +330,13 @@ trait ModifiesPiggyBanks
|
|||||||
*
|
*
|
||||||
* @return PiggyBank
|
* @return PiggyBank
|
||||||
*/
|
*/
|
||||||
private function updateProperties(PiggyBank $piggyBank, array $data): PiggyBank {
|
private function updateProperties(PiggyBank $piggyBank, array $data): PiggyBank
|
||||||
|
{
|
||||||
if (array_key_exists('name', $data) && '' !== $data['name']) {
|
if (array_key_exists('name', $data) && '' !== $data['name']) {
|
||||||
$piggyBank->name = $data['name'];
|
$piggyBank->name = $data['name'];
|
||||||
}
|
}
|
||||||
if (array_key_exists('account_id', $data) && 0 !== $data['account_id']) {
|
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']) {
|
if (array_key_exists('targetamount', $data) && '' !== $data['targetamount']) {
|
||||||
$piggyBank->targetamount = $data['targetamount'];
|
$piggyBank->targetamount = $data['targetamount'];
|
||||||
@@ -343,6 +346,7 @@ trait ModifiesPiggyBanks
|
|||||||
}
|
}
|
||||||
$piggyBank->startdate = $data['startdate'] ?? $piggyBank->startdate;
|
$piggyBank->startdate = $data['startdate'] ?? $piggyBank->startdate;
|
||||||
$piggyBank->save();
|
$piggyBank->save();
|
||||||
|
|
||||||
return $piggyBank;
|
return $piggyBank;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -358,8 +362,8 @@ trait ModifiesPiggyBanks
|
|||||||
$this->updateNote($piggyBank, $data['notes'] ?? '');
|
$this->updateNote($piggyBank, $data['notes'] ?? '');
|
||||||
|
|
||||||
// update the order of the piggy bank:
|
// update the order of the piggy bank:
|
||||||
$oldOrder = (int) $piggyBank->order;
|
$oldOrder = (int)$piggyBank->order;
|
||||||
$newOrder = (int) ($data['order'] ?? $oldOrder);
|
$newOrder = (int)($data['order'] ?? $oldOrder);
|
||||||
if ($oldOrder !== $newOrder) {
|
if ($oldOrder !== $newOrder) {
|
||||||
$this->updateOrder($piggyBank, $oldOrder, $newOrder);
|
$this->updateOrder($piggyBank, $oldOrder, $newOrder);
|
||||||
}
|
}
|
||||||
@@ -376,30 +380,36 @@ trait ModifiesPiggyBanks
|
|||||||
}
|
}
|
||||||
|
|
||||||
// update using name:
|
// update using name:
|
||||||
$objectGroupTitle = $data['object_group'] ?? '';
|
if (array_key_exists('object_group', $data)) {
|
||||||
if ('' !== $objectGroupTitle) {
|
$objectGroupTitle = (string)$data['object_group'];
|
||||||
$objectGroup = $this->findOrCreateObjectGroup($objectGroupTitle);
|
if ('' !== $objectGroupTitle) {
|
||||||
if (null !== $objectGroup) {
|
$objectGroup = $this->findOrCreateObjectGroup($objectGroupTitle);
|
||||||
$piggyBank->objectGroups()->sync([$objectGroup->id]);
|
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([]);
|
||||||
$piggyBank->save();
|
$piggyBank->save();
|
||||||
}
|
}
|
||||||
return $piggyBank;
|
|
||||||
}
|
|
||||||
// remove if name is empty. Should be overruled by ID.
|
|
||||||
if ('' === $objectGroupTitle) {
|
|
||||||
$piggyBank->objectGroups()->sync([]);
|
|
||||||
$piggyBank->save();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// try also with ID:
|
// try also with ID:
|
||||||
$objectGroupId = (int) ($data['object_group_id'] ?? 0);
|
if (array_key_exists('object_group_id', $data)) {
|
||||||
if (0 !== $objectGroupId) {
|
$objectGroupId = (int)($data['object_group_id'] ?? 0);
|
||||||
$objectGroup = $this->findObjectGroupById($objectGroupId);
|
if (0 !== $objectGroupId) {
|
||||||
if (null !== $objectGroup) {
|
$objectGroup = $this->findObjectGroupById($objectGroupId);
|
||||||
$piggyBank->objectGroups()->sync([$objectGroup->id]);
|
if (null !== $objectGroup) {
|
||||||
$piggyBank->save();
|
$piggyBank->objectGroups()->sync([$objectGroup->id]);
|
||||||
|
$piggyBank->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $piggyBank;
|
||||||
}
|
}
|
||||||
return $piggyBank;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $piggyBank;
|
return $piggyBank;
|
||||||
|
Reference in New Issue
Block a user