mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-16 17:33:45 +00:00
Fix #4113
This commit is contained in:
@@ -23,7 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Api\V1\Requests;
|
||||
|
||||
use FireflyIII\Models\PiggyBank;
|
||||
use FireflyIII\Rules\IsAssetAccountId;
|
||||
use FireflyIII\Rules\LessThanPiggyTarget;
|
||||
use FireflyIII\Support\Request\ChecksLogin;
|
||||
@@ -46,16 +45,31 @@ class PiggyBankUpdateRequest extends FormRequest
|
||||
*/
|
||||
public function getAll(): array
|
||||
{
|
||||
return [
|
||||
'name' => $this->string('name'),
|
||||
'account_id' => $this->integer('account_id'),
|
||||
'targetamount' => $this->string('target_amount'),
|
||||
'current_amount' => $this->string('current_amount'),
|
||||
'startdate' => $this->date('start_date'),
|
||||
'targetdate' => $this->date('target_date'),
|
||||
'notes' => $this->nlString('notes'),
|
||||
'order' => $this->integer('order'),
|
||||
// if the value isn't present, dont return it at all.
|
||||
// TODO this should be the way to collect fields for all API things.
|
||||
// TODO make sure piggy bank uses 'start_date' etc. until right up to DB update.
|
||||
// TODO can we configure this and return it from config?
|
||||
$return = [];
|
||||
$fields = [
|
||||
'name' => ['name', 'string'],
|
||||
'account_id' => ['account_id', 'integer'],
|
||||
'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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,6 +80,7 @@ class PiggyBankUpdateRequest extends FormRequest
|
||||
public function rules(): array
|
||||
{
|
||||
$piggyBank = $this->route()->parameter('piggyBank');
|
||||
|
||||
return [
|
||||
'name' => 'between:1,255|uniquePiggyBankForUser:' . $piggyBank->id,
|
||||
'current_amount' => ['numeric', 'gte:0', new LessThanPiggyTarget],
|
||||
|
@@ -45,6 +45,7 @@ use Log;
|
||||
trait ModifiesPiggyBanks
|
||||
{
|
||||
use CreatesObjectGroups;
|
||||
|
||||
/**
|
||||
* @param PiggyBank $piggyBank
|
||||
* @param string $amount
|
||||
@@ -161,6 +162,7 @@ trait ModifiesPiggyBanks
|
||||
if (0 === bccomp('0', $amount)) {
|
||||
return new PiggyBankEvent;
|
||||
}
|
||||
|
||||
return PiggyBankEvent::create(['date' => Carbon::now(), 'amount' => $amount, 'piggy_bank_id' => $piggyBank->id]);
|
||||
}
|
||||
|
||||
@@ -185,8 +187,8 @@ trait ModifiesPiggyBanks
|
||||
/**
|
||||
* @param PiggyBank $piggyBank
|
||||
*
|
||||
* @throws \Exception
|
||||
* @return bool
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function destroy(PiggyBank $piggyBank): bool
|
||||
{
|
||||
@@ -278,8 +280,8 @@ trait ModifiesPiggyBanks
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @throws FireflyException
|
||||
* @return PiggyBank
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function store(array $data): PiggyBank
|
||||
{
|
||||
@@ -328,7 +330,8 @@ trait ModifiesPiggyBanks
|
||||
*
|
||||
* @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']) {
|
||||
$piggyBank->name = $data['name'];
|
||||
}
|
||||
@@ -343,6 +346,7 @@ trait ModifiesPiggyBanks
|
||||
}
|
||||
$piggyBank->startdate = $data['startdate'] ?? $piggyBank->startdate;
|
||||
$piggyBank->save();
|
||||
|
||||
return $piggyBank;
|
||||
}
|
||||
|
||||
@@ -376,13 +380,15 @@ trait ModifiesPiggyBanks
|
||||
}
|
||||
|
||||
// update using name:
|
||||
$objectGroupTitle = $data['object_group'] ?? '';
|
||||
if (array_key_exists('object_group', $data)) {
|
||||
$objectGroupTitle = (string)$data['object_group'];
|
||||
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.
|
||||
@@ -390,8 +396,10 @@ trait ModifiesPiggyBanks
|
||||
$piggyBank->objectGroups()->sync([]);
|
||||
$piggyBank->save();
|
||||
}
|
||||
}
|
||||
|
||||
// try also with ID:
|
||||
if (array_key_exists('object_group_id', $data)) {
|
||||
$objectGroupId = (int)($data['object_group_id'] ?? 0);
|
||||
if (0 !== $objectGroupId) {
|
||||
$objectGroup = $this->findObjectGroupById($objectGroupId);
|
||||
@@ -399,8 +407,10 @@ trait ModifiesPiggyBanks
|
||||
$piggyBank->objectGroups()->sync([$objectGroup->id]);
|
||||
$piggyBank->save();
|
||||
}
|
||||
|
||||
return $piggyBank;
|
||||
}
|
||||
}
|
||||
|
||||
return $piggyBank;
|
||||
}
|
||||
|
Reference in New Issue
Block a user