Search in sources :

Example 1 with ImportedLoanDetail

use of org.mifos.dto.domain.ImportedLoanDetail in project head by mifos.

the class XlsLoansAccountImporter method parse.

/**
     * Parse input stream.
     * @param is input stream containing loan accounts' data
     * @return object containing successfully parsed rows and rows with errors
     */
public ParsedLoansDto parse(InputStream is) {
    //prepare objects: result, lists for rows
    ParsedLoansDto result = null;
    List<String> errors = new ArrayList<String>();
    //temporary list for new accounts numbers, currently not used
    List<String> newAccountsNumbers = new ArrayList<String>();
    List<ImportedLoanDetail> parsedLoanDetails = new ArrayList<ImportedLoanDetail>();
    // open spreadsheet
    try {
        HSSFWorkbook workbook = new HSSFWorkbook(is);
        HSSFSheet sheet = workbook.getSheetAt(0);
        // check first row of data
        HSSFRow row = sheet.getRow(XlsLoansImportTemplateConstants.FIRST_ROW_WITH_DATA.getValue());
        if (row == null) {
            throw new XlsParsingException(getMessage(XlsMessageConstants.NOT_ENOUGH_INPUT_ROW, null));
        }
        Iterator<Row> iterator = sheet.rowIterator();
        // skip to rows with data
        while (iterator.hasNext() && (iterator.next().getRowNum() < XlsLoansImportTemplateConstants.FIRST_ROW_WITH_DATA.getValue() - 1)) ;
        // parse loan account's data
        while (iterator.hasNext()) {
            row = (HSSFRow) iterator.next();
            List<Object> params = new ArrayList<Object>();
            // setup the first cell
            XlsLoansImportTemplateConstants currentCell = XlsLoansImportTemplateConstants.ACCOUNT_NUMBER;
            try {
                // account number
                String accountNumber = getCellStringValue(row, currentCell);
                // TODO: rewrite this account number validation to more universal and extract method
                if (StringUtils.isBlank(accountNumber) && isEdit) {
                    //editing and account number is missing
                    throw new XlsParsingException(getCellError(XlsMessageConstants.MISSING_ACCOUNT_NUMBER, row, currentCell.getValue(), null));
                } else //TODO: validation if account for edition exists
                if (StringUtils.isBlank(accountNumber) && !isEdit) {
                    //not editing, adding with predefined account number and account number is not good
                    accountNumber = null;
                } else //account number is good for creating new account with predefined account number...
                if (!StringUtils.isBlank(accountNumber) && !isEdit) {
                    //...but it's duplicate
                    if (!validateAccountNumber(accountNumber, newAccountsNumbers)) {
                        params.clear();
                        params.add(accountNumber);
                        throw new XlsParsingException(getCellError(XlsMessageConstants.DUPLICATE_GLOBAL_NUM_ERROR, row, currentCell.getValue(), params));
                    }
                }
                //all good, account is either predefined from xls document or null and will be generated 
                //TODO: extract methods that can be shared between loans and savings
                // customer global id
                currentCell = XlsLoansImportTemplateConstants.CUSTOMER_GLOBAL_ID;
                String customerGlobalId = getCellStringValue(row, currentCell);
                if (customerGlobalId.isEmpty()) {
                    throw new XlsParsingException(getCellError(XlsMessageConstants.CUSTOMER_NOT_BLANK, row, currentCell.getValue(), params));
                }
                CustomerBO customerBO = null;
                customerBO = validateCustomerGlobalId(customerGlobalId);
                if (customerBO == null) {
                    params.clear();
                    params.add(customerGlobalId);
                    throw new XlsParsingException(getCellError(XlsMessageConstants.CUSTOMER_NOT_FOUND, row, currentCell.getValue(), params));
                }
                // product name
                currentCell = XlsLoansImportTemplateConstants.PRODUCT_NAME;
                String productName = getCellStringValue(row, currentCell);
                LoanOfferingBO loanOfferingBO = null;
                loanOfferingBO = validateProductName(productName, customerBO, row, currentCell.getValue());
                //TODO: add support for backdated payments
                LoanCreationLoanDetailsDto lcldd = loanAccountServiceFacade.retrieveLoanDetailsForLoanAccountCreation(customerBO.getCustomerId(), loanOfferingBO.getPrdOfferingId(), false);
                // status name
                currentCell = XlsLoansImportTemplateConstants.STATUS_NAME;
                String statusName = getCellStringValue(row, currentCell);
                XlsLoanSavingsAccountStatesConstants statusConstant = null;
                statusConstant = validateStatusName(statusName, customerBO, this.isEdit, row, currentCell.getValue());
                // status reason flag
                currentCell = XlsLoansImportTemplateConstants.CANCEL_FlAG_REASON;
                String cancelReason = getCellStringValue(row, currentCell);
                XlsLoanSavingsFlagsConstants flagConstant = null;
                flagConstant = validateStatusFlagReason(cancelReason, statusName, AccountTypes.LOAN_ACCOUNT, row, currentCell.getValue());
                // loan amount
                currentCell = XlsLoansImportTemplateConstants.LOAN_AMOUNT;
                BigDecimal loanAmount = getCellDecimalValue(row, currentCell);
                validateAmount(loanAmount, loanOfferingBO, customerBO, lcldd, row, currentCell.getValue());
                // Interest rate
                currentCell = XlsLoansImportTemplateConstants.INTEREST_RATE;
                BigDecimal interestRate = getCellDecimalValue(row, currentCell);
                validateInterestRate(interestRate, loanOfferingBO, customerBO, lcldd, row, currentCell.getValue());
                // number of installments
                currentCell = XlsLoansImportTemplateConstants.NO_OF_INSTALLMENTS;
                Integer numberOfInstallments = getCellIntegerValue(row, currentCell);
                validateNumberOfInstallments(numberOfInstallments, customerBO, loanOfferingBO, lcldd, row, currentCell.getValue());
                // disbursal date
                currentCell = XlsLoansImportTemplateConstants.DISBURLSAL_DATE;
                Date disbursalDate = getCellDateValue(row, currentCell);
                validateDisbursalDate(disbursalDate, customerBO, loanOfferingBO, currentCell.getValue(), row, statusName);
                // grace period
                currentCell = XlsLoansImportTemplateConstants.GRACE_PERIOD;
                Integer gracePeriod = getCellIntegerValue(row, currentCell);
                validateGracePeriod(gracePeriod, loanOfferingBO, customerBO, numberOfInstallments, row, currentCell.getValue());
                // source of founds
                currentCell = XlsLoansImportTemplateConstants.SOURCE_OF_FOUNDS;
                List<FundDto> funds = lcldd.getFundDtos();
                String sourceOfFund = getCellStringValue(row, currentCell);
                Integer sourceOfFundId = null;
                sourceOfFundId = validateSourceOfFund(sourceOfFund, funds, row, currentCell.getValue());
                // purpose
                List<ValueListElement> purposes = lcldd.getLoanPurposes();
                currentCell = XlsLoansImportTemplateConstants.PURPOSE;
                String loanPurpose = getCellStringValue(row, currentCell);
                Integer loanPurposeId = null;
                loanPurposeId = validateLoanPurposeId(loanPurpose, purposes, row, currentCell.getValue());
                // collateral type
                currentCell = XlsLoansImportTemplateConstants.COLLATERAL_TYPE;
                Integer collateralTypeId = null;
                String collateralType = getCellStringValue(row, currentCell);
                Map<String, String> collaterals = lcldd.getCollateralOptions();
                collateralTypeId = validateCollateralType(collateralType, collaterals, row, currentCell.getValue());
                // collateral notes
                currentCell = XlsLoansImportTemplateConstants.COLLATERAL_NOTES;
                String collateralNotes = getCellStringValue(row, currentCell);
                collateralNotes = StringUtils.isBlank(collateralNotes) ? null : collateralNotes;
                // external id
                currentCell = XlsLoansImportTemplateConstants.EXTERNAL_ID;
                String externalId = getCellStringValue(row, currentCell);
                externalId = StringUtils.isBlank(externalId) ? null : externalId;
                //...will be used for editing/adding loans with predefined account numbers
                if (accountNumber != null) {
                    newAccountsNumbers.add(accountNumber);
                }
                //create final objects
                //TODO handle backdated payments
                Short flagValue = flagConstant == null ? null : flagConstant.getFlag().getValue();
                ImportedLoanDetail detail = new ImportedLoanDetail(accountNumber, customerBO.getCustomerId(), loanOfferingBO.getPrdOfferingId(), statusConstant.getState().getValue(), flagValue, loanAmount, interestRate, numberOfInstallments, disbursalDate, gracePeriod, sourceOfFundId, loanPurposeId, collateralTypeId, collateralNotes, externalId);
                parsedLoanDetails.add(detail);
            } catch (XlsParsingException xex) {
                if (xex.isMultiple()) {
                    for (String msg : xex.getMessages()) {
                        errors.add(msg);
                    }
                } else {
                    errors.add(xex.getMessage());
                }
            } catch (Exception cex) {
                errors.add(cex.getMessage());
            }
        }
    } catch (Exception ex) {
        errors.add(ex.getMessage());
    }
    result = new ParsedLoansDto(errors, parsedLoanDetails);
    return result;
}
Also used : ArrayList(java.util.ArrayList) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) CustomerBO(org.mifos.customers.business.CustomerBO) ParsedLoansDto(org.mifos.dto.domain.ParsedLoansDto) ImportedLoanDetail(org.mifos.dto.domain.ImportedLoanDetail) FundDto(org.mifos.accounts.fund.servicefacade.FundDto) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) BigDecimal(java.math.BigDecimal) Date(java.util.Date) LocalDate(org.joda.time.LocalDate) LoanOfferingBO(org.mifos.accounts.productdefinition.business.LoanOfferingBO) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) Row(org.apache.poi.ss.usermodel.Row) LoanCreationLoanDetailsDto(org.mifos.dto.screen.LoanCreationLoanDetailsDto) ValueListElement(org.mifos.dto.domain.ValueListElement)

Example 2 with ImportedLoanDetail

use of org.mifos.dto.domain.ImportedLoanDetail in project head by mifos.

the class ImportLoansSavingsFacadeWebTier method saveLoans.

@Override
public ParsedLoansDto saveLoans(ParsedLoansDto parsedLoansDto) {
    List<CreateAccountFeeDto> defaultAccountFees = new ArrayList<CreateAccountFeeDto>();
    List<QuestionGroupDetail> questionGroupDetails = new ArrayList<QuestionGroupDetail>();
    List<CreateAccountPenaltyDto> defaultPenalties = new ArrayList<CreateAccountPenaltyDto>();
    for (ImportedLoanDetail detail : parsedLoansDto.getSuccessfullyParsedRows()) {
        LoanCreationLoanDetailsDto lcldd = loanAccountServiceFacade.retrieveLoanDetailsForLoanAccountCreation(detail.getCustomerId(), detail.getPrdOfferingId(), false);
        RecurringSchedule recurringSchedule = null;
        MeetingDto meetingDto = lcldd.getCustomerMeetingDetail();
        int meetingEvery = (lcldd.isRepaymentIndependentOfMeetingEnabled()) ? lcldd.getLoanOfferingMeetingDetail().getMeetingDetailsDto().getEvery() : meetingDto.getMeetingDetailsDto().getEvery();
        int loanRecurrenceTypeId = (lcldd.isRepaymentIndependentOfMeetingEnabled()) ? lcldd.getLoanOfferingMeetingDetail().getMeetingDetailsDto().getRecurrenceTypeId() : meetingDto.getMeetingDetailsDto().getRecurrenceTypeId();
        if (loanRecurrenceTypeId == 1) {
            recurringSchedule = new WeeklySchedule(meetingEvery, meetingDto.getMeetingDetailsDto().getRecurrenceDetails().getDayOfWeek());
        } else if (loanRecurrenceTypeId == 2) {
            if (meetingDto.getMeetingDetailsDto().getRecurrenceDetails().getWeekOfMonth().equals(0)) {
                recurringSchedule = new MonthlyOnDayOfMonthSchedule(meetingEvery, meetingDto.getMeetingDetailsDto().getRecurrenceDetails().getDayNumber());
            } else {
                recurringSchedule = new MonthlyOnWeekOfMonthSchedule(meetingEvery, meetingDto.getMeetingDetailsDto().getRecurrenceDetails().getWeekOfMonth(), meetingDto.getMeetingDetailsDto().getRecurrenceDetails().getDayOfWeek());
            }
        } else if (loanRecurrenceTypeId == 3) {
            recurringSchedule = new DailySchedule(meetingEvery);
        }
        CreateLoanAccount cla = new CreateLoanAccount(detail.getCustomerId(), new Integer(detail.getPrdOfferingId()), new Integer(detail.getStatus()), detail.getLoanAmount(), lcldd.getMinLoanAmount(), lcldd.getMaxLoanAmount(), detail.getInterestRate().doubleValue(), new LocalDate(detail.getDisbursalDate().getTime()), null, detail.getNumberOfInstallments(), lcldd.getMinNumberOfInstallments(), lcldd.getMaxNumberOfInstallments(), detail.getGracePeriod(), detail.getSourceOfFundId(), detail.getLoanPurposeId(), detail.getCollateralTypeId(), detail.getCollateralNotes(), detail.getExternalId(), lcldd.isRepaymentIndependentOfMeetingEnabled(), recurringSchedule, defaultAccountFees, defaultPenalties);
        cla.setPredefinedAccountNumber(detail.getAccountNumber());
        cla.setFlagId(detail.getFlag());
        this.loanAccountServiceFacade.createLoan(cla, questionGroupDetails, null);
    }
    return null;
}
Also used : QuestionGroupDetail(org.mifos.platform.questionnaire.service.QuestionGroupDetail) ArrayList(java.util.ArrayList) WeeklySchedule(org.mifos.clientportfolio.loan.service.WeeklySchedule) LocalDate(org.joda.time.LocalDate) MeetingDto(org.mifos.dto.domain.MeetingDto) DailySchedule(org.mifos.clientportfolio.loan.service.DailySchedule) RecurringSchedule(org.mifos.clientportfolio.loan.service.RecurringSchedule) CreateLoanAccount(org.mifos.clientportfolio.newloan.applicationservice.CreateLoanAccount) CreateAccountPenaltyDto(org.mifos.dto.domain.CreateAccountPenaltyDto) MonthlyOnDayOfMonthSchedule(org.mifos.clientportfolio.loan.service.MonthlyOnDayOfMonthSchedule) CreateAccountFeeDto(org.mifos.dto.domain.CreateAccountFeeDto) LoanCreationLoanDetailsDto(org.mifos.dto.screen.LoanCreationLoanDetailsDto) MonthlyOnWeekOfMonthSchedule(org.mifos.clientportfolio.loan.service.MonthlyOnWeekOfMonthSchedule) ImportedLoanDetail(org.mifos.dto.domain.ImportedLoanDetail)

Aggregations

ArrayList (java.util.ArrayList)2 LocalDate (org.joda.time.LocalDate)2 ImportedLoanDetail (org.mifos.dto.domain.ImportedLoanDetail)2 LoanCreationLoanDetailsDto (org.mifos.dto.screen.LoanCreationLoanDetailsDto)2 BigDecimal (java.math.BigDecimal)1 Date (java.util.Date)1 HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)1 HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)1 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)1 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)1 Row (org.apache.poi.ss.usermodel.Row)1 FundDto (org.mifos.accounts.fund.servicefacade.FundDto)1 LoanOfferingBO (org.mifos.accounts.productdefinition.business.LoanOfferingBO)1 DailySchedule (org.mifos.clientportfolio.loan.service.DailySchedule)1 MonthlyOnDayOfMonthSchedule (org.mifos.clientportfolio.loan.service.MonthlyOnDayOfMonthSchedule)1 MonthlyOnWeekOfMonthSchedule (org.mifos.clientportfolio.loan.service.MonthlyOnWeekOfMonthSchedule)1 RecurringSchedule (org.mifos.clientportfolio.loan.service.RecurringSchedule)1 WeeklySchedule (org.mifos.clientportfolio.loan.service.WeeklySchedule)1 CreateLoanAccount (org.mifos.clientportfolio.newloan.applicationservice.CreateLoanAccount)1 CustomerBO (org.mifos.customers.business.CustomerBO)1