Skip to content

Commit

Permalink
feat: Initial design of Topups and some bug fixes
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 322b360f31703ba9ba13e8f79a90404aacdf1d8f
Author: Zikani Nyirenda Mwase <zikaninyirenda@cgatechnologies.org.uk>
Date:   Tue Aug 2 10:38:56 2022 +0200

    fix: Set representative phone number, add yesOrNo for boolean fields.

commit 38af5fcaabf0f9184fc0da985c180123f9cafaef
Author: Zikani Nyirenda Mwase <zikaninyirenda@cgatechnologies.org.uk>
Date:   Mon Aug 1 16:27:36 2022 +0200

    feat: Redirect to index onn failed pre-conditions

    Added a basic function for redirecting when a precondition has failed

commit ba7559c
Author: Zikani Nyirenda Mwase <zikaninyirenda@cgatechnologies.org.uk>
Date:   Mon Aug 1 15:31:45 2022 +0200

    fix: Fix saving of Phone number for Transfer Agencies.

commit f9d3d0b
Author: Zikani Nyirenda Mwase <zikaninyirenda@cgatechnologies.org.uk>
Date:   Mon Aug 1 15:31:18 2022 +0200

    chore: Moved functions from util/form_extras to utils/form

    Removed form_extras.peb

commit 91a453c
Author: Zikani Nyirenda Mwase <zikaninyirenda@cgatechnologies.org.uk>
Date:   Mon Aug 1 15:30:22 2022 +0200

    feat: Add TopUps calculation to TransferCalculator

    Added calculation of the transfer topups to the calculator except for the Admin Fees figure which has to be designed/approached properly

commit 6015763
Author: Zikani Nyirenda Mwase <zikaninyirenda@cgatechnologies.org.uk>
Date:   Wed Jul 27 10:40:16 2022 +0200

    chore: Re-order elements on navigation

commit 32c3f80
Author: Zikani Nyirenda Mwase <zikaninyirenda@cgatechnologies.org.uk>
Date:   Tue Jul 26 16:41:44 2022 +0200

    feat: Implement basic create functionality for TopUps

commit 9b34a6b
Author: Zikani Nyirenda Mwase <zikaninyirenda@cgatechnologies.org.uk>
Date:   Mon Jul 25 16:54:55 2022 +0200

    feat: Add topups migration, entity and base views

commit cced355
Author: Zikani Nyirenda Mwase <zikaninyirenda@cgatechnologies.org.uk>
Date:   Mon Jul 25 16:52:45 2022 +0200

    chore: Make class public for use elsewhere

commit 2ff4d1e
Author: Zikani Nyirenda Mwase <zikaninyirenda@cgatechnologies.org.uk>
Date:   Mon Jul 25 16:51:49 2022 +0200

    fix: Show error messages on new forms
  • Loading branch information
zikani03 committed Aug 2, 2022
1 parent 9c2a590 commit c83c628
Show file tree
Hide file tree
Showing 28 changed files with 1,274 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* BSD 3-Clause License
*
* Copyright (c) 2022, CGATechnologies
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package org.cga.sctp.api.transfers.topups;

public class TopUpController {
}
9 changes: 8 additions & 1 deletion sctp-core/src/main/java/org/cga/sctp/schools/School.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@

package org.cga.sctp.schools;

import org.cga.sctp.schools.educationzone.EducationZone;
import org.cga.sctp.targeting.importation.converters.EducationLevelParameterValueConverter;
import org.cga.sctp.targeting.importation.parameters.EducationLevel;

import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;

@Entity
Expand All @@ -47,15 +48,19 @@ public class School {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@NotEmpty(message = "Name cannot be empty or null")
@Column
private String name;

@NotEmpty(message = "School code cannot be null")
@Column
private String code;

@NotNull(message = "Education Level must be specified")
@Convert(converter = EducationLevelParameterValueConverter.class)
private EducationLevel educationLevel;

@NotNull(message = "Education Zone ID must be specified")
@Column(name="education_zone")
private Long educationZoneId;

Expand All @@ -68,9 +73,11 @@ public class School {
@Column
private Boolean active;

@NotNull(message = "Created At cannot be null")
@Column
private LocalDateTime createdAt;

@NotNull(message = "Modified At cannot be null")
@Column
private LocalDateTime modifiedAt;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

import javax.persistence.AttributeConverter;

abstract class UbrParameterValueConverter implements AttributeConverter<UbrParameterValue, Integer> {
public abstract class UbrParameterValueConverter implements AttributeConverter<UbrParameterValue, Integer> {
private final UbrParameterValue[] values;

public UbrParameterValueConverter(UbrParameterValue[] values) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,6 @@ public void setReviewedBy(Long reviewedBy) {

// TODO: Make this a property in the database ? or pre-compute
public Long getTotalAmountToTransfer() {
return this.basicSubsidyAmount + this.secondaryBonusAmount /* + TODO: this.primaryBonusAmount */ + this.primaryIncentiveAmount;
return this.basicSubsidyAmount + this.secondaryBonusAmount + this.primaryBonusAmount + this.primaryIncentiveAmount + this.topupAmount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import org.cga.sctp.transfers.parameters.EducationTransferParameter;
import org.cga.sctp.transfers.parameters.HouseholdParameterCondition;
import org.cga.sctp.transfers.parameters.HouseholdTransferParameter;
import org.cga.sctp.transfers.parameters.TransferParametersService;
import org.cga.sctp.transfers.periods.TransferPeriod;
import org.cga.sctp.transfers.topups.TopUp;

import java.util.List;
import java.util.Optional;
Expand All @@ -47,9 +47,20 @@ public class TransferCalculator {
private List<EducationTransferParameter> educationTransferParameters;
private List<HouseholdTransferParameter> householdTransferParameters;

public TransferCalculator(List<EducationTransferParameter> educationTransferParameters, List<HouseholdTransferParameter> householdTransferParameters) {
private List<TopUp> topUps;

/**
*
* @param householdTransferParameters Parameters
* @param educationTransferParameters Parameters for education
* @param topUps topups that are applicable to the households to calculate transfers for
*/
public TransferCalculator(List<HouseholdTransferParameter> householdTransferParameters,
List<EducationTransferParameter> educationTransferParameters,
List<TopUp> topUps) {
this.educationTransferParameters = educationTransferParameters;
this.householdTransferParameters = householdTransferParameters;
this.topUps = topUps;
}


Expand Down Expand Up @@ -124,6 +135,40 @@ public Long determineAmountByHouseholdSize(int householdSize, List<HouseholdTran
return -1L;
}

protected long calculateTopUpAmount(Transfer transfer, Long monthlyAmount, List<TopUp> topUpList) {
double totalTopupAmout = 0.0;
for (var topup : topUpList) {
totalTopupAmout += switch (topup.getTopupType()) {
case FIXED_AMOUNT -> topup.getAmount();
case EQUIVALENT_BENEFICIARY_AMOUNT -> monthlyAmount;
case PERCENTAGE_OF_RECIPIENT_AMOUNT -> (monthlyAmount * topup.getPercentage())/100d;
default -> topup.getAmount();
};
}
// TODO: avoid conversion to long, we lose fidelity here..
return (long) totalTopupAmout;
}

protected void calculateTransferAmount(Transfer transfer,Location location, TransferPeriod transferPeriod) {
long monthlyAmount = 0;
final long basicAmount = determineAmountByHouseholdSize(transfer.getHouseholdMemberCount(), householdTransferParameters),
secondaryBonus = transfer.getSecondaryChildrenCount() * getSecondaryBonusAmount(educationTransferParameters),
primaryBonus = transfer.getPrimaryChildrenCount() * getPrimaryBonusAmount(educationTransferParameters),
primaryIncentive = transfer.getPrimaryIncentiveChildrenCount() * getPrimaryIncentiveAmount();

transfer.setAmountDisbursed(0L);
transfer.setNumberOfMonths(transferPeriod.countNoOfMonths());
transfer.setBasicSubsidyAmount(basicAmount);
transfer.setPrimaryIncentiveAmount(primaryIncentive);
transfer.setPrimaryBonusAmount(primaryBonus);
transfer.setSecondaryBonusAmount(secondaryBonus);
// TODO: Should we set this or calculate upon request
monthlyAmount = basicAmount + primaryBonus + secondaryBonus + primaryIncentive;

// Calculate topups
transfer.setTopupAmount(calculateTopUpAmount(transfer, monthlyAmount, topUps));
}

/**
* Calculates transfers for all the households in the transfer period for a given location..
* @param transferPeriod
Expand All @@ -135,21 +180,7 @@ public void calculateTransfersUpdate(Location location, TransferPeriod transferP
if (transfer.getTransferPeriodId() != transferPeriod.getId()) {
continue;
}
basicAmount = determineAmountByHouseholdSize(transfer.getHouseholdMemberCount(), householdTransferParameters);
secondaryBonus = transfer.getSecondaryChildrenCount() * getSecondaryBonusAmount(educationTransferParameters);
primaryBonus = transfer.getPrimaryChildrenCount() * getPrimaryBonusAmount(educationTransferParameters);
// TODO: store the number of children that need/want incentives for primary school...
primaryIncentive = transfer.getPrimaryIncentiveChildrenCount() * getPrimaryIncentiveAmount();

transfer.setNumberOfMonths(transferPeriod.countNoOfMonths());
transfer.setAmountDisbursed(0L);
transfer.setTopupAmount(0L);
transfer.setBasicSubsidyAmount(basicAmount);
transfer.setPrimaryIncentiveAmount(primaryIncentive);
// TODO: transfer.setPrimaryBonusAmount(primaryIncentive);
transfer.setSecondaryBonusAmount(secondaryBonus);
// TODO: Should we set this or calculate upon request
monthlyAmount = basicAmount + primaryBonus + secondaryBonus + primaryIncentive;
this.calculateTransferAmount(transfer, location, transferPeriod);
}
}
}
Loading

0 comments on commit c83c628

Please sign in to comment.