mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Fix for #2446
This commit is contained in:
parent
ecc37b2ed3
commit
21a9e981a2
@ -226,7 +226,14 @@ class PiggyBankController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function update(PiggyBankRequest $request, PiggyBank $piggyBank): JsonResponse
|
public function update(PiggyBankRequest $request, PiggyBank $piggyBank): JsonResponse
|
||||||
{
|
{
|
||||||
$piggyBank = $this->repository->update($piggyBank, $request->getAll());
|
$data = $request->getAll();
|
||||||
|
$piggyBank = $this->repository->update($piggyBank, $data);
|
||||||
|
|
||||||
|
if ('' !== $data['current_amount']) {
|
||||||
|
$this->repository->setCurrentAmount($piggyBank, $data['current_amount']);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
$manager = new Manager();
|
$manager = new Manager();
|
||||||
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';
|
||||||
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
$manager->setSerializer(new JsonApiSerializer($baseUrl));
|
||||||
|
@ -25,6 +25,7 @@ namespace FireflyIII\Api\V1\Requests;
|
|||||||
|
|
||||||
use FireflyIII\Models\PiggyBank;
|
use FireflyIII\Models\PiggyBank;
|
||||||
use FireflyIII\Rules\IsAssetAccountId;
|
use FireflyIII\Rules\IsAssetAccountId;
|
||||||
|
use FireflyIII\Rules\LessThanPiggyTarget;
|
||||||
use FireflyIII\Rules\ZeroOrMore;
|
use FireflyIII\Rules\ZeroOrMore;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -53,14 +54,11 @@ class PiggyBankRequest extends Request
|
|||||||
*/
|
*/
|
||||||
public function getAll(): array
|
public function getAll(): array
|
||||||
{
|
{
|
||||||
$current = $this->string('current_amount');
|
|
||||||
$current = '' === $current ? '0' : $current;
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'name' => $this->string('name'),
|
'name' => $this->string('name'),
|
||||||
'account_id' => $this->integer('account_id'),
|
'account_id' => $this->integer('account_id'),
|
||||||
'targetamount' => $this->string('target_amount'),
|
'targetamount' => $this->string('target_amount'),
|
||||||
'current_amount' => $current,
|
'current_amount' => $this->string('current_amount'),
|
||||||
'startdate' => $this->date('start_date'),
|
'startdate' => $this->date('start_date'),
|
||||||
'targetdate' => $this->date('target_date'),
|
'targetdate' => $this->date('target_date'),
|
||||||
'notes' => $this->string('notes'),
|
'notes' => $this->string('notes'),
|
||||||
@ -76,8 +74,6 @@ class PiggyBankRequest extends Request
|
|||||||
{
|
{
|
||||||
$rules = [
|
$rules = [
|
||||||
'name' => 'required|between:1,255|uniquePiggyBankForUser',
|
'name' => 'required|between:1,255|uniquePiggyBankForUser',
|
||||||
'account_id' => ['required', 'belongsToUser:accounts', new IsAssetAccountId],
|
|
||||||
'target_amount' => 'required|numeric|more:0',
|
|
||||||
'current_amount' => ['numeric', new ZeroOrMore, 'lte:target_amount'],
|
'current_amount' => ['numeric', new ZeroOrMore, 'lte:target_amount'],
|
||||||
'start_date' => 'date|nullable',
|
'start_date' => 'date|nullable',
|
||||||
'target_date' => 'date|nullable|after:start_date',
|
'target_date' => 'date|nullable|after:start_date',
|
||||||
@ -91,7 +87,10 @@ class PiggyBankRequest extends Request
|
|||||||
case 'PATCH':
|
case 'PATCH':
|
||||||
/** @var PiggyBank $piggyBank */
|
/** @var PiggyBank $piggyBank */
|
||||||
$piggyBank = $this->route()->parameter('piggyBank');
|
$piggyBank = $this->route()->parameter('piggyBank');
|
||||||
$rules['name'] = 'required|between:1,255|uniquePiggyBankForUser:' . $piggyBank->id;
|
$rules['name'] = 'between:1,255|uniquePiggyBankForUser:' . $piggyBank->id;
|
||||||
|
$rules['account_id'] = ['belongsToUser:accounts', new IsAssetAccountId];
|
||||||
|
$rules['target_amount'] = 'numeric|more:0';
|
||||||
|
$rules['current_amount'] = ['numeric', new ZeroOrMore, new LessThanPiggyTarget];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -534,20 +534,27 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function update(PiggyBank $piggyBank, array $data): PiggyBank
|
public function update(PiggyBank $piggyBank, array $data): PiggyBank
|
||||||
{
|
{
|
||||||
|
if (isset($data['name']) && '' !== $data['name']) {
|
||||||
$piggyBank->name = $data['name'];
|
$piggyBank->name = $data['name'];
|
||||||
|
}
|
||||||
|
if (isset($data['account_id']) && 0 !== $data['account_id']) {
|
||||||
$piggyBank->account_id = (int)$data['account_id'];
|
$piggyBank->account_id = (int)$data['account_id'];
|
||||||
|
}
|
||||||
|
if (isset($data['targetamount']) && '' !== $data['targetamount']) {
|
||||||
$piggyBank->targetamount = $data['targetamount'];
|
$piggyBank->targetamount = $data['targetamount'];
|
||||||
|
}
|
||||||
|
if (isset($data['targetdate']) && '' !== $data['targetdate']) {
|
||||||
$piggyBank->targetdate = $data['targetdate'];
|
$piggyBank->targetdate = $data['targetdate'];
|
||||||
|
}
|
||||||
$piggyBank->startdate = $data['startdate'] ?? $piggyBank->startdate;
|
$piggyBank->startdate = $data['startdate'] ?? $piggyBank->startdate;
|
||||||
|
|
||||||
$piggyBank->save();
|
$piggyBank->save();
|
||||||
|
|
||||||
$this->updateNote($piggyBank, $data['notes']);
|
$this->updateNote($piggyBank, $data['notes'] ?? '');
|
||||||
|
|
||||||
// if the piggy bank is now smaller than the current relevant rep,
|
// if the piggy bank is now smaller than the current relevant rep,
|
||||||
// remove money from the rep.
|
// remove money from the rep.
|
||||||
$repetition = $this->getRepetition($piggyBank);
|
$repetition = $this->getRepetition($piggyBank);
|
||||||
if ($repetition->currentamount > $piggyBank->targetamount) {
|
if (null !== $repetition && $repetition->currentamount > $piggyBank->targetamount) {
|
||||||
$diff = bcsub($piggyBank->targetamount, $repetition->currentamount);
|
$diff = bcsub($piggyBank->targetamount, $repetition->currentamount);
|
||||||
$this->createEvent($piggyBank, $diff);
|
$this->createEvent($piggyBank, $diff);
|
||||||
|
|
||||||
@ -558,12 +565,36 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
|
|||||||
return $piggyBank;
|
return $piggyBank;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param PiggyBank $piggyBank
|
||||||
|
* @param string $amount
|
||||||
|
*
|
||||||
|
* @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;
|
||||||
|
}
|
||||||
|
$repetition->currentamount = $amount;
|
||||||
|
$repetition->save();
|
||||||
|
|
||||||
|
// create event
|
||||||
|
$this->createEvent($piggyBank, $amount);
|
||||||
|
|
||||||
|
return $piggyBank;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param PiggyBank $piggyBank
|
* @param PiggyBank $piggyBank
|
||||||
* @param string $note
|
* @param string $note
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws \Exception
|
|
||||||
*/
|
*/
|
||||||
private function updateNote(PiggyBank $piggyBank, string $note): bool
|
private function updateNote(PiggyBank $piggyBank, string $note): bool
|
||||||
{
|
{
|
||||||
|
@ -35,6 +35,14 @@ use Illuminate\Support\Collection;
|
|||||||
*/
|
*/
|
||||||
interface PiggyBankRepositoryInterface
|
interface PiggyBankRepositoryInterface
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @param PiggyBank $piggyBank
|
||||||
|
* @param string $amount
|
||||||
|
*
|
||||||
|
* @return PiggyBank
|
||||||
|
*/
|
||||||
|
public function setCurrentAmount(PiggyBank $piggyBank, string $amount): PiggyBank;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param PiggyBank $piggyBank
|
* @param PiggyBank $piggyBank
|
||||||
* @param string $amount
|
* @param string $amount
|
||||||
|
34
app/Rules/LessThanPiggyTarget.php
Normal file
34
app/Rules/LessThanPiggyTarget.php
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace FireflyIII\Rules;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Validation\Rule;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class LessThanPiggyTarget
|
||||||
|
*/
|
||||||
|
class LessThanPiggyTarget implements Rule
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Get the validation error message.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function message(): string
|
||||||
|
{
|
||||||
|
return (string)trans('validation.current_target_amount');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if the validation rule passes.
|
||||||
|
*
|
||||||
|
* @param string $attribute
|
||||||
|
* @param mixed $value
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function passes($attribute, $value): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@ -127,6 +127,7 @@ return [
|
|||||||
'in_array' => 'The :attribute field does not exist in :other.',
|
'in_array' => 'The :attribute field does not exist in :other.',
|
||||||
'present' => 'The :attribute field must be present.',
|
'present' => 'The :attribute field must be present.',
|
||||||
'amount_zero' => 'The total amount cannot be zero.',
|
'amount_zero' => 'The total amount cannot be zero.',
|
||||||
|
'current_target_amount' => 'The current amount must be less than the target amount.',
|
||||||
'unique_piggy_bank_for_user' => 'The name of the piggy bank must be unique.',
|
'unique_piggy_bank_for_user' => 'The name of the piggy bank must be unique.',
|
||||||
'secure_password' => 'This is not a secure password. Please try again. For more information, visit https://bit.ly/FF3-password-security',
|
'secure_password' => 'This is not a secure password. Please try again. For more information, visit https://bit.ly/FF3-password-security',
|
||||||
'valid_recurrence_rep_type' => 'Invalid repetition type for recurring transactions.',
|
'valid_recurrence_rep_type' => 'Invalid repetition type for recurring transactions.',
|
||||||
|
Loading…
Reference in New Issue
Block a user