diff --git a/app/Api/V1/Controllers/Models/AvailableBudget/StoreController.php b/app/Api/V1/Controllers/Models/AvailableBudget/StoreController.php index ac60e870e8..e4f57915ee 100644 --- a/app/Api/V1/Controllers/Models/AvailableBudget/StoreController.php +++ b/app/Api/V1/Controllers/Models/AvailableBudget/StoreController.php @@ -57,6 +57,7 @@ class StoreController extends Controller } ); } + /** * Store a newly created resource in storage. * @@ -70,14 +71,14 @@ class StoreController extends Controller $data['start']->startOfDay(); $data['end']->endOfDay(); - /** @var TransactionCurrencyFactory $factory */ - $factory = app(TransactionCurrencyFactory::class); - $currency = $factory->find($data['currency_id'], $data['currency_code']); - - if (null === $currency) { - $currency = app('amount')->getDefaultCurrency(); + // currency is not mandatory: + if (array_key_exists('currency_id', $data) || array_key_exists('currency_code', $data)) { + $factory = app(TransactionCurrencyFactory::class); + $currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null); + $data['currency_id'] = $currency->id; + unset($data['currency_code']); } - $data['currency'] = $currency; + $availableBudget = $this->abRepository->store($data); $manager = $this->getManager(); diff --git a/app/Api/V1/Controllers/Models/AvailableBudget/UpdateController.php b/app/Api/V1/Controllers/Models/AvailableBudget/UpdateController.php index 5196b13bc4..e9c8bb53ad 100644 --- a/app/Api/V1/Controllers/Models/AvailableBudget/UpdateController.php +++ b/app/Api/V1/Controllers/Models/AvailableBudget/UpdateController.php @@ -72,20 +72,15 @@ class UpdateController extends Controller { $data = $request->getAll(); - /** @var TransactionCurrencyFactory $factory */ - $factory = app(TransactionCurrencyFactory::class); - /** @var TransactionCurrency $currency */ - $currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null); - - if (null === $currency) { - // use default currency: - $currency = app('amount')->getDefaultCurrency(); + // find and validate currency ID + if(array_key_exists('currency_id', $data) || array_key_exists('currency_code', $data)) { + $factory = app(TransactionCurrencyFactory::class); + $currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null) ?? app('amount')->getDefaultCurrency(); + $currency->enabled = true; + $currency->save(); + unset($data['currency_code']); + $data['currency_id'] = $currency->id; } - $currency->enabled = true; - $currency->save(); - unset($data['currency_code']); - $data['currency_id'] = $currency->id; - $this->abRepository->updateAvailableBudget($availableBudget, $data); $manager = $this->getManager(); diff --git a/app/Api/V1/Controllers/Models/Bill/StoreController.php b/app/Api/V1/Controllers/Models/Bill/StoreController.php index ef1dbe8106..7cb6c2847f 100644 --- a/app/Api/V1/Controllers/Models/Bill/StoreController.php +++ b/app/Api/V1/Controllers/Models/Bill/StoreController.php @@ -74,7 +74,8 @@ class StoreController extends Controller */ public function store(StoreRequest $request): JsonResponse { - $bill = $this->repository->store($request->getAll()); + $data = $request->getAll(); + $bill = $this->repository->store($data); $manager = $this->getManager(); /** @var BillTransformer $transformer */ diff --git a/app/Api/V1/Requests/Models/AvailableBudget/Request.php b/app/Api/V1/Requests/Models/AvailableBudget/Request.php index 78d8f0f2bb..c421d2a9df 100644 --- a/app/Api/V1/Requests/Models/AvailableBudget/Request.php +++ b/app/Api/V1/Requests/Models/AvailableBudget/Request.php @@ -23,9 +23,11 @@ declare(strict_types=1); namespace FireflyIII\Api\V1\Requests\Models\AvailableBudget; +use Carbon\Carbon; use FireflyIII\Support\Request\ChecksLogin; use FireflyIII\Support\Request\ConvertsDataTypes; use Illuminate\Foundation\Http\FormRequest; +use Illuminate\Validation\Validator; /** * Class Request @@ -43,13 +45,16 @@ class Request extends FormRequest */ public function getAll(): array { - return [ - 'currency_id' => $this->integer('currency_id'), - 'currency_code' => $this->string('currency_code'), - 'amount' => $this->string('amount'), - 'start' => $this->date('start'), - 'end' => $this->date('end'), + // this is the way: + $fields = [ + 'currency_id' => ['currency_id', 'integer'], + 'currency_code' => ['currency_code', 'string'], + 'amount' => ['amount', 'string'], + 'start' => ['start', 'date'], + 'end' => ['end', 'date'], ]; + + return $this->getAllData($fields); } /** @@ -62,11 +67,35 @@ class Request extends FormRequest return [ 'currency_id' => 'numeric|exists:transaction_currencies,id', 'currency_code' => 'min:3|max:3|exists:transaction_currencies,code', - 'amount' => 'required|numeric|gt:0', - 'start' => 'required|date|before:end', - 'end' => 'required|date|after:start', + 'amount' => 'numeric|gt:0', + 'start' => 'date', + 'end' => 'date', ]; } + /** + * Configure the validator instance with special rules for after the basic validation rules. + * + * @param Validator $validator + * + * @return void + */ + public function withValidator(Validator $validator): void + { + $validator->after( + function (Validator $validator) { + // validate start before end only if both are there. + $data = $validator->getData(); + if (array_key_exists('start', $data) && array_key_exists('end', $data)) { + $start = new Carbon($data['start']); + $end = new Carbon($data['end']); + if ($end->isBefore($start)) { + $validator->errors()->add('end', (string)trans('validation.date_after')); + } + } + } + ); + } + } diff --git a/app/Api/V1/Requests/Models/Bill/StoreRequest.php b/app/Api/V1/Requests/Models/Bill/StoreRequest.php index 76571b28ad..f3f0e1ed6c 100644 --- a/app/Api/V1/Requests/Models/Bill/StoreRequest.php +++ b/app/Api/V1/Requests/Models/Bill/StoreRequest.php @@ -29,6 +29,7 @@ use FireflyIII\Support\Request\ChecksLogin; use FireflyIII\Support\Request\ConvertsDataTypes; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Validator; +use Log; /** * Class StoreRequest @@ -46,24 +47,24 @@ class StoreRequest extends FormRequest */ public function getAll(): array { - $active = true; - if (null !== $this->get('active')) { - $active = $this->boolean('active'); - } - - return [ - 'name' => $this->string('name'), - 'amount_min' => $this->string('amount_min'), - 'amount_max' => $this->string('amount_max'), - 'currency_id' => $this->integer('currency_id'), - 'currency_code' => $this->string('currency_code'), - 'date' => $this->date('date'), - 'repeat_freq' => $this->string('repeat_freq'), - 'skip' => $this->integer('skip'), - 'active' => $active, - 'order' => $this->integer('order'), - 'notes' => $this->nlString('notes'), + Log::debug('Raw fields in Bill StoreRequest', $this->all()); + $fields = [ + 'name' => ['name', 'string'], + 'amount_min' => ['amount_min', 'string'], + 'amount_max' => ['amount_max', 'string'], + 'currency_id' => ['currency_id', 'integer'], + 'currency_code' => ['currency_code', 'string'], + 'date' => ['date', 'date'], + 'repeat_freq' => ['repeat_freq', 'string'], + 'skip' => ['skip', 'integer'], + 'active' => ['active', 'boolean'], + 'order' => ['order', 'integer'], + 'notes' => ['notes', 'nlString'], + 'object_group_id' => ['object_group_id', 'integer'], + 'object_group_title' => ['object_group_title', 'string'], ]; + + return $this->getAllData($fields); } /** @@ -99,10 +100,10 @@ class StoreRequest extends FormRequest $validator->after( static function (Validator $validator) { $data = $validator->getData(); - $min = (float) ($data['amount_min'] ?? 0); - $max = (float) ($data['amount_max'] ?? 0); + $min = (float)($data['amount_min'] ?? 0); + $max = (float)($data['amount_max'] ?? 0); if ($min > $max) { - $validator->errors()->add('amount_min', (string) trans('validation.amount_min_over_max')); + $validator->errors()->add('amount_min', (string)trans('validation.amount_min_over_max')); } } ); diff --git a/app/Factory/BillFactory.php b/app/Factory/BillFactory.php index d68c68b68d..88c2ad614e 100644 --- a/app/Factory/BillFactory.php +++ b/app/Factory/BillFactory.php @@ -26,7 +26,6 @@ namespace FireflyIII\Factory; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Bill; -use FireflyIII\Models\TransactionCurrency; use FireflyIII\Repositories\ObjectGroup\CreatesObjectGroups; use FireflyIII\Services\Internal\Support\BillServiceTrait; use FireflyIII\User; @@ -50,15 +49,14 @@ class BillFactory */ public function create(array $data): ?Bill { - /** @var TransactionCurrencyFactory $factory */ - $factory = app(TransactionCurrencyFactory::class); - /** @var TransactionCurrency $currency */ - $currency = $factory->find((int)($data['currency_id'] ?? null), (string)($data['currency_code'] ?? null)); + Log::debug(sprintf('Now in %s', __METHOD__), $data); + $factory = app(TransactionCurrencyFactory::class); + $currency = $factory->find((int)($data['currency_id'] ?? null), (string)($data['currency_code'] ?? null)) ?? + app('amount')->getDefaultCurrencyByUser($this->user); - if (null === $currency) { - $currency = app('amount')->getDefaultCurrencyByUser($this->user); - } try { + $skip = array_key_exists('skip', $data) ? $data['skip'] : 0; + $active = array_key_exists('active', $data) ? $data['active'] : 0; /** @var Bill $bill */ $bill = Bill::create( [ @@ -70,9 +68,9 @@ class BillFactory 'amount_max' => $data['amount_max'], 'date' => $data['date'], 'repeat_freq' => $data['repeat_freq'], - 'skip' => $data['skip'], + 'skip' => $skip, 'automatch' => true, - 'active' => $data['active'] ?? true, + 'active' => $active, ] ); } catch (QueryException $e) { @@ -84,8 +82,7 @@ class BillFactory if (array_key_exists('notes', $data)) { $this->updateNote($bill, (string)$data['notes']); } - - $objectGroupTitle = $data['object_group'] ?? ''; + $objectGroupTitle = $data['object_group_title'] ?? ''; if ('' !== $objectGroupTitle) { $objectGroup = $this->findOrCreateObjectGroup($objectGroupTitle); if (null !== $objectGroup) { diff --git a/app/Repositories/Budget/AvailableBudgetRepository.php b/app/Repositories/Budget/AvailableBudgetRepository.php index df50397925..64854c7c29 100644 --- a/app/Repositories/Budget/AvailableBudgetRepository.php +++ b/app/Repositories/Budget/AvailableBudgetRepository.php @@ -25,7 +25,6 @@ namespace FireflyIII\Repositories\Budget; use Carbon\Carbon; use Exception; -use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\AvailableBudget; use FireflyIII\Models\TransactionCurrency; use FireflyIII\User; @@ -246,7 +245,7 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface return AvailableBudget::create( [ 'user_id' => $this->user->id, - 'transaction_currency_id' => $data['currency']->id, + 'transaction_currency_id' => $data['currency_id'], 'amount' => $data['amount'], 'start_date' => $start, 'end_date' => $end, @@ -276,35 +275,34 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface * @param array $data * * @return AvailableBudget - * @throws FireflyException */ public function updateAvailableBudget(AvailableBudget $availableBudget, array $data): AvailableBudget { - $existing = $this->user->availableBudgets() - ->where('transaction_currency_id', $data['currency_id']) - ->where('start_date', $data['start']->format('Y-m-d')) - ->where('end_date', $data['end']->format('Y-m-d')) - ->where('id', '!=', $availableBudget->id) - ->first(); - - if (null !== $existing) { - throw new FireflyException(sprintf('An entry already exists for these parameters: available budget object with ID #%d', $existing->id)); + if (array_key_exists('start', $data)) { + $start = $data['start']; + if ($start instanceof Carbon) { + $start = $data['start']->startOfDay(); + $availableBudget->start_date = $start; + $availableBudget->save(); + } } - $start = $data['start']; - if ($start instanceof Carbon) { - $start = $data['start']->startOfDay(); + if (array_key_exists('end', $data)) { + $end = $data['end']; + if ($end instanceof Carbon) { + $end = $data['end']->endOfDay(); + $availableBudget->end_date = $end; + $availableBudget->save(); + } } - $end = $data['end']; - if ($end instanceof Carbon) { - $end = $data['end']->endOfDay(); + if (array_key_exists('currency_id', $data)) { + $availableBudget->transaction_currency_id = $data['currency_id']; + $availableBudget->save(); + } + if (array_key_exists('amount', $data)) { + $availableBudget->amount = $data['amount']; + $availableBudget->save(); } - - $availableBudget->transaction_currency_id = $data['currency_id']; - $availableBudget->start_date = $start; - $availableBudget->end_date = $end; - $availableBudget->amount = $data['amount']; - $availableBudget->save(); return $availableBudget; diff --git a/phpunit.xml b/phpunit.xml index 62175fb34e..06b5c1ada3 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -35,9 +35,18 @@ - + ./tests/Api/Models/Account + + ./tests/Api/Models/Attachment + + + ./tests/Api/Models/AvailableBudget + + + ./tests/Api/Models/Bill +