This commit is contained in:
James Cole 2020-12-02 06:41:42 +01:00
parent 822432712b
commit e27e0a97a7
No known key found for this signature in database
GPG Key ID: B5669F9493CDE38D
2 changed files with 66 additions and 41 deletions

View File

@ -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],

View File

@ -45,6 +45,7 @@ use Log;
trait ModifiesPiggyBanks
{
use CreatesObjectGroups;
/**
* @param PiggyBank $piggyBank
* @param string $amount
@ -102,9 +103,9 @@ trait ModifiesPiggyBanks
public function canAddAmount(PiggyBank $piggyBank, string $amount): bool
{
$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);
$maxAmount = (string) min(round($leftOnAccount, 12), round($leftToSave, 12));
$maxAmount = (string)min(round($leftOnAccount, 12), round($leftToSave, 12));
$compare = bccomp($amount, $maxAmount);
$result = $compare <= 0;
@ -142,7 +143,7 @@ trait ModifiesPiggyBanks
$set = $this->user->piggyBanks()->orderBy('order', 'ASC')->get();
$current = 1;
foreach ($set as $piggyBank) {
if ((int) $piggyBank->order !== $current) {
if ((int)$piggyBank->order !== $current) {
$piggyBank->order = $current;
$piggyBank->save();
}
@ -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
{
@ -310,7 +312,7 @@ trait ModifiesPiggyBanks
}
}
// 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) {
@ -328,12 +330,13 @@ 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'];
}
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'];
@ -343,6 +346,7 @@ trait ModifiesPiggyBanks
}
$piggyBank->startdate = $data['startdate'] ?? $piggyBank->startdate;
$piggyBank->save();
return $piggyBank;
}
@ -358,8 +362,8 @@ trait ModifiesPiggyBanks
$this->updateNote($piggyBank, $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->updateOrder($piggyBank, $oldOrder, $newOrder);
}
@ -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,17 +396,21 @@ trait ModifiesPiggyBanks
$piggyBank->objectGroups()->sync([]);
$piggyBank->save();
}
}
// try also with ID:
$objectGroupId = (int) ($data['object_group_id'] ?? 0);
if (array_key_exists('object_group_id', $data)) {
$objectGroupId = (int)($data['object_group_id'] ?? 0);
if (0 !== $objectGroupId) {
$objectGroup = $this->findObjectGroupById($objectGroupId);
if (null !== $objectGroup) {
$piggyBank->objectGroups()->sync([$objectGroup->id]);
$piggyBank->save();
}
return $piggyBank;
}
}
return $piggyBank;
}