Files
firefly-iii/app/Api/V1/Requests/Data/Bulk/MoveTransactionsRequest.php

110 lines
3.8 KiB
PHP
Raw Normal View History

<?php
2021-08-10 19:31:55 +02:00
/*
* MoveTransactionsRequest.php
* Copyright (c) 2021 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2021-04-04 08:31:15 +02:00
declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests\Data\Bulk;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Support\Request\ChecksLogin;
use FireflyIII\Support\Request\ConvertsDataTypes;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
/**
* Class MoveTransactionsRequest
*/
class MoveTransactionsRequest extends FormRequest
{
2022-10-30 14:23:00 +01:00
use ChecksLogin;
use ConvertsDataTypes;
public function getAll(): array
{
return [
2022-10-01 05:29:42 +02:00
'original_account' => $this->convertInteger('original_account'),
'destination_account' => $this->convertInteger('destination_account'),
];
}
/**
* @return string[]
*/
public function rules(): array
{
return [
'original_account' => 'required|different:destination_account|belongsToUser:accounts,id',
'destination_account' => 'required|different:original_account|belongsToUser:accounts,id',
];
}
/**
* Configure the validator instance with special rules for after the basic validation rules.
2024-01-04 07:44:52 +01:00
* TODO this is duplicate.
*/
public function withValidator(Validator $validator): void
{
$validator->after(
2023-12-21 04:59:23 +01:00
function (Validator $validator): void {
// validate start before end only if both are there.
$data = $validator->getData();
if (array_key_exists('original_account', $data) && array_key_exists('destination_account', $data)) {
2022-03-30 06:54:59 +02:00
$this->validateMove($validator);
}
}
);
}
2022-03-30 06:54:59 +02:00
2022-10-30 14:23:00 +01:00
private function validateMove(Validator $validator): void
{
2024-01-04 07:48:51 +01:00
$data = $validator->getData();
$repository = app(AccountRepositoryInterface::class);
2022-03-30 06:54:59 +02:00
$repository->setUser(auth()->user());
2024-01-04 07:48:51 +01:00
$original = $repository->find((int) $data['original_account']);
$destination = $repository->find((int) $data['destination_account']);
2022-03-30 06:54:59 +02:00
// not the same type:
if ($original->accountType->type !== $destination->accountType->type) {
2024-01-04 07:44:52 +01:00
$validator->errors()->add('title', (string) trans('validation.same_account_type'));
2022-03-30 06:54:59 +02:00
return;
}
// get currency pref:
$originalCurrency = $repository->getAccountCurrency($original);
$destinationCurrency = $repository->getAccountCurrency($destination);
// check different scenario's.
if (null === $originalCurrency xor null === $destinationCurrency) {
2024-01-04 07:44:52 +01:00
$validator->errors()->add('title', (string) trans('validation.same_account_currency'));
2022-03-30 06:54:59 +02:00
return;
}
if (null === $originalCurrency && null === $destinationCurrency) {
// this is OK
return;
}
if ($originalCurrency->code !== $destinationCurrency->code) {
2024-01-04 07:44:52 +01:00
$validator->errors()->add('title', (string) trans('validation.same_account_currency'));
2022-03-30 06:54:59 +02:00
}
}
2021-04-04 08:31:15 +02:00
}