Skip to content

Commit

Permalink
Merge pull request #1511 from younginnovations/1493-remove-requiremen…
Browse files Browse the repository at this point in the history
…t-for-a-revised-activity-budget-to-have-an-equivalent-original-budget

Review: 1493-remove-requirement-for-a-revised-activity-budget-to-have-an-equivalent-original-budget
  • Loading branch information
Sanilblank authored Jul 30, 2024
2 parents 47870f8 + dd205ff commit b248a1f
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 48 deletions.
1 change: 0 additions & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

php artisan command:SetAppDataJsonCache && \
php artisan test && \
npx stylelint "resources/**/*.scss" --fix && \
npm run lint && \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -707,10 +707,6 @@ public function budgetValidation(): void
$this->extend('budgets_identical', function () {
return false;
});

$this->extend('budget_revised_invalid', function () {
return false;
});
}

/**
Expand Down
47 changes: 19 additions & 28 deletions app/Http/Requests/Activity/Budget/BudgetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

use App\Http\Requests\Activity\ActivityBaseRequest;
use App\IATI\Services\Activity\ActivityService;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Validator;
use JsonException;

/**
* Class BudgetRequest.
Expand All @@ -19,15 +21,13 @@ class BudgetRequest extends ActivityBaseRequest
*/
protected array $identicalIds = [];

/**
* @var array
*/
protected array $revisedIds = [];

/**
* Get the validation rules that apply to the request.
*
* @return array
*
* @throws BindingResolutionException
* @throws JsonException
*/
public function rules(): array
{
Expand All @@ -41,6 +41,8 @@ public function rules(): array
* Get the error message as required.
*
* @return array
*
* @throws BindingResolutionException
*/
public function messages(): array
{
Expand All @@ -53,14 +55,16 @@ public function messages(): array
* @param array $formFields
*
* @return array
* @throws BindingResolutionException
* @throws JsonException
*/
public function getErrorsForBudget(array $formFields): array
{
$rules = [];
/** @var ActivityService $activityService */
$activityService = app()->make(ActivityService::class);
$formFields = $activityService->setBudgets($formFields);
$this->identicalIds = $activityService->checkSameMultipleBudgets($formFields);
$this->revisedIds = $activityService->checkRevisedBudgets($formFields);

foreach ($formFields as $budgetIndex => $budget) {
$budgetForm = sprintf('budget.%s', $budgetIndex);
Expand Down Expand Up @@ -100,14 +104,16 @@ public function getErrorsForBudget(array $formFields): array
* @param array $formFields
*
* @return array
*
* @throws BindingResolutionException
*/
public function getWarningForBudget(array $formFields): array
{
$rules = [];
/** @var ActivityService $activityService */
$activityService = app()->make(ActivityService::class);
$formFields = $activityService->setBudgets($formFields);
$this->identicalIds = $activityService->checkSameMultipleBudgets($formFields);
$this->revisedIds = $activityService->checkRevisedBudgets($formFields);

if (count($this->identicalIds)) {
Validator::extend('budgets_identical', function () {
Expand All @@ -121,18 +127,6 @@ public function getWarningForBudget(array $formFields): array
}
}

if (count($this->revisedIds)) {
Validator::extend('budget_revised_invalid', function () {
return false;
});

foreach ($this->revisedIds as $ids) {
foreach ($ids as $id) {
$rules['budget.' . $id . '.budget_type'][] = 'budget_revised_invalid';
}
}
}

foreach ($formFields as $budgetIndex => $budget) {
$budgetForm = sprintf('budget.%s', $budgetIndex);
$diff = 0;
Expand Down Expand Up @@ -179,6 +173,8 @@ public function getWarningForBudget(array $formFields): array
* @param $formBase
*
* @return array
*
* @throws JsonException
*/
protected function getErrorsForValue($formFields, $formBase): array
{
Expand Down Expand Up @@ -286,16 +282,19 @@ public function getCriticalBudgetWarningForPeriodEnd($formFields, $formBase): ar
* Returns messages for related activity validations.
*
* @param array $formFields
* @param bool $fileUpload
*
* @return array
*
* @throws BindingResolutionException
*/
public function getMessagesForBudget(array $formFields, bool $fileUpload = false): array
{
$messages = [];
/** @var ActivityService $activityService */
$activityService = app()->make(ActivityService::class);
$formFields = $activityService->setBudgets($formFields);
$this->identicalIds = $activityService->checkSameMultipleBudgets($formFields);
$this->revisedIds = $activityService->checkRevisedBudgets($formFields);

if (count($this->identicalIds)) {
foreach ($this->identicalIds as $ids) {
Expand All @@ -305,14 +304,6 @@ public function getMessagesForBudget(array $formFields, bool $fileUpload = false
}
}

if (count($this->revisedIds)) {
foreach ($this->revisedIds as $ids) {
foreach ($ids as $id) {
$messages['budget.' . $id . '.budget_type.budget_revised_invalid'] = 'Budget with type revised must have period start and end same to that of one of the budgets having same type original for budgets elements at position ' . $this->getIdenticalIds($ids);
}
}
}

foreach ($formFields as $budgetIndex => $budget) {
$budgetForm = sprintf('budget.%s', $budgetIndex);
$periodStartMessages = $this->getMessagesForPeriodStart($budget['period_start'], $budgetForm);
Expand Down
4 changes: 0 additions & 4 deletions app/XlsImporter/Validator/Traits/RegistersValidationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -687,10 +687,6 @@ public function budgetValidation(): void
$this->extend('budgets_identical', function () {
return false;
});

$this->extend('budget_revised_invalid', function () {
return false;
});
}

/**
Expand Down
4 changes: 0 additions & 4 deletions app/Xml/Validator/Traits/RegistersValidationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,10 +689,6 @@ public function budgetValidation(): void
$this->extend('budgets_identical', function () {
return false;
});

$this->extend('budget_revised_invalid', function () {
return false;
});
}

/**
Expand Down
9 changes: 5 additions & 4 deletions tests/Unit/Csv/BudgetCsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,25 @@ public function get_invalid_budget_period(): array
}

/**
* Revised date not matched with one of the budget type original.
* Do not throw validation if revised not match with budget period.
* Change source: https://github.com/younginnovations/iatipublisher/issues/1493.
*
* @return void
* @test
* @throws \JsonException
*/
public function throw_validation_if_revised_period_do_not_match_one_of_budget_period(): void
public function do_not_throw_validation_if_revised_period_do_not_match_one_of_budget_period(): void
{
$this->signIn();
$rows = $this->get_revised_period_not_matched_date();
$errors = $this->getErrors($rows);
$flattenErrors = Arr::flatten($errors);

$this->assertContains('Budget with type revised must have period start and end same to that of one of the budgets having same type original for budgets elements at position 2', $flattenErrors);
$this->assertEmpty($flattenErrors);
}

/**
* Invalid reviosed period.
* Invalid revised period.
*
* @return array
*/
Expand Down
7 changes: 4 additions & 3 deletions tests/Unit/Xml/BudgetXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,17 @@ public function date_longer_than_one_year_1800_data(): array
}

/**
* Throw validation if revised not match with budget period.
* Do not throw validation if revised not match with budget period.
* Change source: https://github.com/younginnovations/iatipublisher/issues/1493.
*
* @return void
* @test
*/
public function throw_validation_if_revised_period_do_not_match_one_of_budget_period(): void
public function do_not_throw_validation_if_revised_period_do_not_match_one_of_budget_period(): void
{
$rows = $this->get_revised_period_not_matched_date();
$flattenErrors = $this->getErrors($rows);
$this->assertContains('Budget with type revised must have period start and end same to that of one of the budgets having same type original for budgets elements at position 2', $flattenErrors);
$this->assertEmpty($flattenErrors);
}

/**
Expand Down

0 comments on commit b248a1f

Please sign in to comment.