change office transactions capability to support intra office transactions and external transactions (money in or out of office from external entity).

This commit is contained in:
Keith Woodlock
2012-07-12 11:58:53 +01:00
parent 47c5f33d23
commit 1a29bbe97e
9 changed files with 202 additions and 73 deletions
@@ -0,0 +1,22 @@
-- create office transaction table
DROP TABLE IF EXISTS `mifosngprovider`.`portfolio_office_monetary_transfers`;
DROP TABLE IF EXISTS `mifosngprovider`.`portfolio_office_transaction`;
CREATE TABLE `portfolio_office_transaction` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`from_office_id` bigint(20) DEFAULT NULL,
`to_office_id` bigint(20) DEFAULT NULL,
`currency_code` varchar(3) NOT NULL,
`currency_digits` int(11) NOT NULL,
`transaction_amount` decimal(19,6) NOT NULL,
`transaction_date` date NOT NULL,
`description` varchar(100) DEFAULT NULL,
`created_date` datetime DEFAULT NULL,
`createdby_id` bigint(20) DEFAULT NULL,
`lastmodified_date` datetime DEFAULT NULL,
`lastmodifiedby_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK1E37728B93C6C1B6` (`to_office_id`),
KEY `FK1E37728B783C5C25` (`from_office_id`),
CONSTRAINT `FK1E37728B783C5C25` FOREIGN KEY (`from_office_id`) REFERENCES `org_office` (`id`),
CONSTRAINT `FK1E37728B93C6C1B6` FOREIGN KEY (`to_office_id`) REFERENCES `org_office` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
@@ -1,8 +1,6 @@
package org.mifosng.platform.api;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Locale;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
@@ -17,15 +15,12 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.apache.commons.lang.StringUtils;
import org.joda.time.LocalDate;
import org.mifosng.platform.api.commands.BranchMoneyTransferCommand;
import org.mifosng.platform.api.commands.OfficeCommand;
import org.mifosng.platform.api.data.EntityIdentifier;
import org.mifosng.platform.api.data.OfficeData;
import org.mifosng.platform.api.infrastructure.ApiDataConversionService;
import org.mifosng.platform.api.infrastructure.ApiJSONFormattingService;
import org.mifosng.platform.exceptions.UnrecognizedQueryParamException;
import org.mifosng.platform.organisation.service.OfficeReadPlatformService;
import org.mifosng.platform.organisation.service.OfficeWritePlatformService;
import org.springframework.beans.factory.annotation.Autowired;
@@ -130,41 +125,4 @@ public class OfficeApiResource {
return Response.ok().entity(new EntityIdentifier(entityId)).build();
}
@POST
@Path("{officeId}")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public Response transferMoneyFrom(@PathParam("officeId") final Long officeId,
@QueryParam("command") final String commandParam,
final BranchMoneyTransferCommand command) {
command.setFromOfficeId(officeId);
LocalDate transactionLocalDate = apiDataConversionService.convertFrom(command.getTransactionDate(), "transactionDate", command.getDateFormat());
command.setTransactionLocalDate(transactionLocalDate);
Locale clientLocale = this.apiDataConversionService.localeFromString(command.getLocale());
BigDecimal transactionAmountValue = apiDataConversionService.convertFrom(command.getTransactionAmount(), "transactionAmount", clientLocale);
command.setTransactionAmountValue(transactionAmountValue);
Response response = null;
if (is(commandParam, "transfer")) {
Long id = this.writePlatformService.transferMoney(command);
response = Response.ok().entity(new EntityIdentifier(id)).build();
}
if (response == null) {
throw new UnrecognizedQueryParamException("command", commandParam);
}
return response;
}
private boolean is(final String commandParam, final String commandValue) {
return StringUtils.isNotBlank(commandParam)
&& commandParam.trim().equalsIgnoreCase(commandValue);
}
}
@@ -0,0 +1,82 @@
package org.mifosng.platform.api;
import java.math.BigDecimal;
import java.util.Locale;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.commons.lang.StringUtils;
import org.joda.time.LocalDate;
import org.mifosng.platform.api.commands.BranchMoneyTransferCommand;
import org.mifosng.platform.api.data.EntityIdentifier;
import org.mifosng.platform.api.infrastructure.ApiDataConversionService;
import org.mifosng.platform.api.infrastructure.ApiJSONFormattingService;
import org.mifosng.platform.exceptions.UnrecognizedQueryParamException;
import org.mifosng.platform.organisation.service.OfficeReadPlatformService;
import org.mifosng.platform.organisation.service.OfficeWritePlatformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Path("/officetransactions")
@Component
@Scope("singleton")
public class OfficeTransactionsApiResource {
private String defaultFieldList = "openingDate";
private String allowedFieldList = "allowedParents";
private String filterName = "myFilter";
@Autowired
private OfficeReadPlatformService readPlatformService;
@Autowired
private OfficeWritePlatformService writePlatformService;
@Autowired
private ApiDataConversionService apiDataConversionService;
@Autowired
private ApiJSONFormattingService jsonFormattingService;
@POST
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public Response transferMoneyFrom(@QueryParam("command") final String commandParam, final BranchMoneyTransferCommand command) {
LocalDate transactionLocalDate = apiDataConversionService.convertFrom(command.getTransactionDate(), "transactionDate", command.getDateFormat());
command.setTransactionLocalDate(transactionLocalDate);
Locale clientLocale = this.apiDataConversionService.localeFromString(command.getLocale());
BigDecimal transactionAmountValue = apiDataConversionService.convertFrom(command.getTransactionAmount(), "transactionAmount", clientLocale);
command.setTransactionAmountValue(transactionAmountValue);
Response response = null;
if (is(commandParam, "intra-transfer")) {
Long id = this.writePlatformService.interBranchMoneyTransfer(command);
response = Response.ok().entity(new EntityIdentifier(id)).build();
} else if (is(commandParam, "external-transfer")) {
Long id = this.writePlatformService.externalBranchMoneyTransfer(command);
response = Response.ok().entity(new EntityIdentifier(id)).build();
}
if (response == null) {
throw new UnrecognizedQueryParamException("command", commandParam);
}
return response;
}
private boolean is(final String commandParam, final String commandValue) {
return StringUtils.isNotBlank(commandParam)
&& commandParam.trim().equalsIgnoreCase(commandValue);
}
}
@@ -25,6 +25,8 @@ public class BranchMoneyTransferCommand {
private String transactionAmount;
private BigDecimal transactionAmountValue;
private String description;
protected BranchMoneyTransferCommand() {
//
}
@@ -108,4 +110,12 @@ public class BranchMoneyTransferCommand {
public void setCurrencyCode(String currencyCode) {
this.currencyCode = currencyCode;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
@@ -20,8 +20,8 @@ import org.mifosng.platform.infrastructure.AbstractAuditableCustom;
import org.mifosng.platform.user.domain.AppUser;
@Entity
@Table(name = "portfolio_office_monetary_transfers")
public class OfficeMonetaryTransfer extends AbstractAuditableCustom<AppUser, Long> {
@Table(name = "portfolio_office_transaction")
public class OfficeTransaction extends AbstractAuditableCustom<AppUser, Long> {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "from_office_id")
@@ -40,22 +40,25 @@ public class OfficeMonetaryTransfer extends AbstractAuditableCustom<AppUser, Lon
@Column(name = "transaction_amount", scale = 6, precision = 19, nullable = false)
private BigDecimal transactionAmount;
@Column(name = "description", nullable=true, length=100)
private String description;
protected OfficeMonetaryTransfer() {
protected OfficeTransaction() {
this.transactionDate = null;
}
public static OfficeMonetaryTransfer create(Office fromOffice, Office toOffice, LocalDate transactionLocalDate, Money amount) {
public static OfficeTransaction create(Office fromOffice, Office toOffice, LocalDate transactionLocalDate, Money amount) {
Date transactionDate = null;
if (transactionLocalDate != null) {
transactionDate = transactionLocalDate.toDate();
}
return new OfficeMonetaryTransfer(fromOffice, toOffice, transactionDate, amount);
return new OfficeTransaction(fromOffice, toOffice, transactionDate, amount);
}
private OfficeMonetaryTransfer(final Office fromOffice,final Office toOffice, final Date transactionDate, final Money amount) {
private OfficeTransaction(final Office fromOffice,final Office toOffice, final Date transactionDate, final Money amount) {
this.from = fromOffice;
this.to = toOffice;
this.transactionDate = transactionDate;
@@ -3,8 +3,8 @@ package org.mifosng.platform.organisation.domain;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface OfficeMonetaryTransferRepository extends
JpaRepository<OfficeMonetaryTransfer, Long>,
JpaSpecificationExecutor<OfficeMonetaryTransfer> {
public interface OfficeTransactionRepository extends
JpaRepository<OfficeTransaction, Long>,
JpaSpecificationExecutor<OfficeTransaction> {
// no added behaviour
}
@@ -16,7 +16,7 @@ public class BranchMoneyTransferCommandValidator {
this.command = command;
}
public void validate() {
public void validateInterBranchTransfer() {
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
@@ -27,6 +27,31 @@ public class BranchMoneyTransferCommandValidator {
baseDataValidator.reset().parameter("transactionDate").value(command.getTransactionLocalDate()).notNull();
baseDataValidator.reset().parameter("currencyCode").value(command.getCurrencyCode()).notBlank();
baseDataValidator.reset().parameter("transactionAmount").value(command.getTransactionAmountValue()).notNull();
baseDataValidator.reset().parameter("description").value(command.getDescription()).notExceedingLengthOf(100);
if (!dataValidationErrors.isEmpty()) {
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors);
}
}
public void validateExternalBranchTransfer() {
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("office.money.transfer");
if (command.getFromOfficeId() == null) {
baseDataValidator.reset().parameter("toOfficeId").value(command.getToOfficeId()).notNull();
}
if (command.getToOfficeId() == null) {
baseDataValidator.reset().parameter("fromOfficeId").value(command.getFromOfficeId()).notNull();
}
baseDataValidator.reset().parameter("transactionDate").value(command.getTransactionLocalDate()).notNull();
baseDataValidator.reset().parameter("currencyCode").value(command.getCurrencyCode()).notBlank();
baseDataValidator.reset().parameter("transactionAmount").value(command.getTransactionAmountValue()).notNull();
baseDataValidator.reset().parameter("description").value(command.getDescription()).notExceedingLengthOf(100);
if (!dataValidationErrors.isEmpty()) {
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors);
@@ -13,5 +13,8 @@ public interface OfficeWritePlatformService {
Long updateOffice(final OfficeCommand command);
@PreAuthorize(value = "hasRole('ORGANISATION_ADMINISTRATION_SUPER_USER_ROLE')")
Long transferMoney(BranchMoneyTransferCommand command);
Long interBranchMoneyTransfer(BranchMoneyTransferCommand command);
@PreAuthorize(value = "hasRole('ORGANISATION_ADMINISTRATION_SUPER_USER_ROLE')")
Long externalBranchMoneyTransfer(BranchMoneyTransferCommand command);
}
@@ -13,8 +13,8 @@ import org.mifosng.platform.exceptions.NoAuthorizationException;
import org.mifosng.platform.exceptions.OfficeNotFoundException;
import org.mifosng.platform.exceptions.PlatformDataIntegrityException;
import org.mifosng.platform.organisation.domain.Office;
import org.mifosng.platform.organisation.domain.OfficeMonetaryTransfer;
import org.mifosng.platform.organisation.domain.OfficeMonetaryTransferRepository;
import org.mifosng.platform.organisation.domain.OfficeTransaction;
import org.mifosng.platform.organisation.domain.OfficeTransactionRepository;
import org.mifosng.platform.organisation.domain.OfficeRepository;
import org.mifosng.platform.security.PlatformSecurityContext;
import org.mifosng.platform.user.domain.AppUser;
@@ -32,13 +32,13 @@ public class OfficeWritePlatformServiceJpaRepositoryImpl implements OfficeWriteP
private final PlatformSecurityContext context;
private final OfficeRepository officeRepository;
private final OfficeMonetaryTransferRepository officeMonetaryTransferRepository;
private final OfficeTransactionRepository officeMonetaryTransferRepository;
private final ApplicationCurrencyRepository applicationCurrencyRepository;
@Autowired
public OfficeWritePlatformServiceJpaRepositoryImpl(final PlatformSecurityContext context,
final OfficeRepository officeRepository,
final OfficeMonetaryTransferRepository officeMonetaryTransferRepository,
final OfficeTransactionRepository officeMonetaryTransferRepository,
final ApplicationCurrencyRepository applicationCurrencyRepository) {
this.context = context;
this.officeRepository = officeRepository;
@@ -104,26 +104,21 @@ public class OfficeWritePlatformServiceJpaRepositoryImpl implements OfficeWriteP
@Transactional
@Override
public Long transferMoney(final BranchMoneyTransferCommand command) {
public Long interBranchMoneyTransfer(final BranchMoneyTransferCommand command) {
AppUser currentUser = context.authenticatedUser();
BranchMoneyTransferCommandValidator validator = new BranchMoneyTransferCommandValidator(command);
validator.validate();
validator.validateInterBranchTransfer();
Office fromOffice = validateUserPriviledgeOnOfficeAndRetrieve(currentUser, command.getFromOfficeId());
Office toOffice = null;
if (fromOffice.isHeadOffice()) {
toOffice = validateUserPriviledgeOnOfficeAndRetrieve(currentUser, command.getToOfficeId());
} else {
toOffice = this.officeRepository.findOne(command.getToOfficeId());
if (toOffice == null) {
throw new OfficeNotFoundException(command.getToOfficeId());
}
if (!fromOffice.hasParentOf(toOffice)) {
throw new NoAuthorizationException("User does not have sufficient priviledges to act on the provided office.");
}
Office fromOffice = this.officeRepository.findOne(officesThatMatch(currentUser.getOrganisation(), command.getFromOfficeId()));
if (fromOffice == null) {
throw new OfficeNotFoundException(command.getFromOfficeId());
}
Office toOffice = this.officeRepository.findOne(officesThatMatch(currentUser.getOrganisation(), command.getToOfficeId()));
if (toOffice == null) {
throw new OfficeNotFoundException(command.getToOfficeId());
}
final String currencyCode = command.getCurrencyCode();
@@ -135,7 +130,38 @@ public class OfficeWritePlatformServiceJpaRepositoryImpl implements OfficeWriteP
MonetaryCurrency currency = new MonetaryCurrency(appCurrency.getCode(), appCurrency.getDecimalPlaces());
Money amount = Money.of(currency, command.getTransactionAmountValue());
OfficeMonetaryTransfer entity = OfficeMonetaryTransfer.create(fromOffice, toOffice, command.getTransactionLocalDate(), amount);
OfficeTransaction entity = OfficeTransaction.create(fromOffice, toOffice, command.getTransactionLocalDate(), amount);
this.officeMonetaryTransferRepository.save(entity);
return entity.getId();
}
@Transactional
@Override
public Long externalBranchMoneyTransfer(BranchMoneyTransferCommand command) {
AppUser currentUser = context.authenticatedUser();
BranchMoneyTransferCommandValidator validator = new BranchMoneyTransferCommandValidator(command);
validator.validateExternalBranchTransfer();
Office fromOffice = this.officeRepository.findOne(officesThatMatch(currentUser.getOrganisation(), command.getFromOfficeId()));
Office toOffice = this.officeRepository.findOne(officesThatMatch(currentUser.getOrganisation(), command.getToOfficeId()));
if (fromOffice == null && toOffice == null) {
throw new OfficeNotFoundException(command.getToOfficeId());
}
final String currencyCode = command.getCurrencyCode();
ApplicationCurrency appCurrency = this.applicationCurrencyRepository.findOneByCode(currencyCode);
if (appCurrency == null) {
throw new CurrencyNotFoundException(currencyCode);
}
MonetaryCurrency currency = new MonetaryCurrency(appCurrency.getCode(), appCurrency.getDecimalPlaces());
Money amount = Money.of(currency, command.getTransactionAmountValue());
OfficeTransaction entity = OfficeTransaction.create(fromOffice, toOffice, command.getTransactionLocalDate(), amount);
this.officeMonetaryTransferRepository.save(entity);