diff --git a/app/Api/V1/Requests/Models/Account/StoreRequest.php b/app/Api/V1/Requests/Models/Account/StoreRequest.php index 6659baf155..e16b016a6f 100644 --- a/app/Api/V1/Requests/Models/Account/StoreRequest.php +++ b/app/Api/V1/Requests/Models/Account/StoreRequest.php @@ -77,11 +77,11 @@ class StoreRequest extends FormRequest 'interest' => $this->string('interest'), 'interest_period' => $this->string('interest_period'), ]; - // append Location information. + // append location information. $data = $this->appendLocationData($data, null); if ('liability' === $data['account_type_name'] || 'liabilities' === $data['account_type_name']) { - $data['opening_balance'] = bcmul($this->string('liability_amount'), '-1'); + $data['opening_balance'] = app('steam')->negative($this->string('liability_amount')); $data['opening_balance_date'] = $this->date('liability_start_date'); $data['account_type_name'] = $this->string('liability_type'); $data['liability_direction'] = $this->string('liability_direction'); @@ -120,11 +120,11 @@ class StoreRequest extends FormRequest 'credit_card_type' => sprintf('in:%s|required_if:account_role,ccAsset', $ccPaymentTypes), 'monthly_payment_date' => 'date' . '|required_if:account_role,ccAsset|required_if:credit_card_type,monthlyFull', 'liability_type' => 'required_if:type,liability|required_if:type,liabilities|in:loan,debt,mortgage', - 'liability_amount' => 'required_if:type,liability|required_if:type,liabilities|min:0|numeric', + 'liability_amount' => 'required_with:liability_start_date|min:0|numeric', + 'liability_start_date' => 'required_with:liability_amount|date', 'liability_direction' => 'required_if:type,liability|required_if:type,liabilities|in:credit,debit', - 'liability_start_date' => 'required_if:type,liability|required_if:type,liabilities|date', - 'interest' => 'required_if:type,liability|required_if:type,liabilities|between:0,100|numeric', - 'interest_period' => 'required_if:type,liability|required_if:type,liabilities|in:daily,monthly,yearly', + 'interest' => 'between:0,100|numeric', + 'interest_period' => sprintf('in:%s', join(',', config('firefly.interest_periods'))), 'notes' => 'min:0|max:65536', ]; diff --git a/app/Api/V1/Requests/Models/Account/UpdateRequest.php b/app/Api/V1/Requests/Models/Account/UpdateRequest.php index c53f70168a..b8bcf1fa3e 100644 --- a/app/Api/V1/Requests/Models/Account/UpdateRequest.php +++ b/app/Api/V1/Requests/Models/Account/UpdateRequest.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace FireflyIII\Api\V1\Requests\Models\Account; +use FireflyIII\Models\Account; use FireflyIII\Models\Location; use FireflyIII\Rules\IsBoolean; use FireflyIII\Rules\UniqueAccountNumber; @@ -32,6 +33,7 @@ use FireflyIII\Support\Request\AppendsLocationData; use FireflyIII\Support\Request\ChecksLogin; use FireflyIII\Support\Request\ConvertsDataTypes; use Illuminate\Foundation\Http\FormRequest; +use Log; /** * Class UpdateRequest @@ -68,16 +70,23 @@ class UpdateRequest extends FormRequest 'order' => ['order', 'integer'], 'currency_id' => ['currency_id', 'integer'], 'currency_code' => ['currency_code', 'string'], - 'liability_direction' => ['liability_direction', 'string'] + 'liability_direction' => ['liability_direction', 'string'], + 'liability_amount' => ['liability_amount', 'string'], + 'liability_start_date' => ['liability_start_date', 'date'], ]; - $data = $this->getAllData($fields); - $data = $this->appendLocationData($data, null); + /** @var Account $account */ + $account = $this->route()->parameter('account'); + $data = $this->getAllData($fields); + $data = $this->appendLocationData($data, null); + $valid = config('firefly.valid_liabilities'); + if (array_key_exists('liability_amount', $data) && in_array($account->accountType->type, $valid, true)) { + $data['opening_balance'] = app('steam')->negative($data['liability_amount']); + Log::debug(sprintf('Opening balance for liability is "%s".', $data['opening_balance'])); + } - if (array_key_exists('account_type_name', $data) && 'liability' === $data['account_type_name']) { - $data['opening_balance'] = bcmul($this->string('liability_amount'), '-1'); - $data['opening_balance_date'] = $this->date('liability_start_date'); - $data['account_type_name'] = $this->string('liability_type'); - $data['account_type_id'] = null; + if (array_key_exists('liability_start_date', $data) && in_array($account->accountType->type, $valid, true)) { + $data['opening_balance_date'] = $data['liability_start_date']; + Log::debug(sprintf('Opening balance date for liability is "%s".', $data['opening_balance_date'])); } return $data;