use of org.kuali.kfs.coa.businessobject.ObjectCodeCurrent in project cu-kfs by CU-CommunityApps.
the class ContractsGrantsInvoiceDocumentServiceImpl method assignCurrentExpenditureToNonExistingAccountObjectCode.
protected void assignCurrentExpenditureToNonExistingAccountObjectCode(ContractsGrantsInvoiceDocument contractsGrantsInvoiceDocument, ContractsGrantsInvoiceDetail invoiceDetail) {
String categoryCode = invoiceDetail.getCategoryCode();
if (StringUtils.isBlank(categoryCode)) {
throw new IllegalArgumentException("Category Code can not be null during recalculation of account object code for Contracts & Grants Invoice Document.");
}
// get the category that matches this category code.
final CostCategory category = businessObjectService.findBySinglePrimaryKey(CostCategory.class, categoryCode);
// got the category now.
if (!ObjectUtils.isNull(category)) {
final KualiDecimal oneCent = new KualiDecimal(0.01);
int size = contractsGrantsInvoiceDocument.getAccountDetails().size();
KualiDecimal amount = new KualiDecimal(invoiceDetail.getInvoiceAmount().bigDecimalValue().divide(new BigDecimal(size), 2, RoundingMode.HALF_UP));
KualiDecimal remainder = invoiceDetail.getInvoiceAmount().subtract(amount.multiply(new KualiDecimal(size)));
for (InvoiceAccountDetail invoiceAccountDetail : contractsGrantsInvoiceDocument.getAccountDetails()) {
InvoiceDetailAccountObjectCode invoiceDetailAccountObjectCode = new InvoiceDetailAccountObjectCode();
invoiceDetailAccountObjectCode.setDocumentNumber(contractsGrantsInvoiceDocument.getDocumentNumber());
invoiceDetailAccountObjectCode.setProposalNumber(contractsGrantsInvoiceDocument.getInvoiceGeneralDetail().getProposalNumber());
invoiceDetailAccountObjectCode.setCategoryCode(categoryCode);
invoiceDetailAccountObjectCode.setAccountNumber(invoiceAccountDetail.getAccountNumber());
invoiceDetailAccountObjectCode.setChartOfAccountsCode(invoiceAccountDetail.getChartOfAccountsCode());
// it's 0.00 that's why we are in this section to begin with.
invoiceDetailAccountObjectCode.setCumulativeExpenditures(KualiDecimal.ZERO);
// this is also 0.00 because it has never been billed before
invoiceDetailAccountObjectCode.setTotalBilled(KualiDecimal.ZERO);
final ObjectCodeCurrent objectCode = getCostCategoryService().findObjectCodeForChartAndCategory(invoiceAccountDetail.getChartOfAccountsCode(), categoryCode);
if (!ObjectUtils.isNull(objectCode)) {
invoiceDetailAccountObjectCode.setFinancialObjectCode(objectCode.getFinancialObjectCode());
}
// tack on or remove one penny until the remainder is 0 - take a penny, leave a penny!
if (remainder.isGreaterThan(KualiDecimal.ZERO)) {
amount = amount.add(oneCent);
remainder = remainder.subtract(oneCent);
} else if (remainder.isLessThan(KualiDecimal.ZERO)) {
amount = amount.subtract(oneCent);
remainder = remainder.add(oneCent);
}
invoiceDetailAccountObjectCode.setCurrentExpenditures(amount);
List<InvoiceDetailAccountObjectCode> invoiceDetailAccountObjectCodes = contractsGrantsInvoiceDocument.getInvoiceDetailAccountObjectCodes();
if (invoiceDetailAccountObjectCodes.contains(invoiceDetailAccountObjectCode)) {
// update existing code
InvoiceDetailAccountObjectCode original = invoiceDetailAccountObjectCodes.get(invoiceDetailAccountObjectCodes.indexOf(invoiceDetailAccountObjectCode));
original.setCurrentExpenditures(amount);
original.setCategoryCode(categoryCode);
} else {
// add this single account object code item to the list in the Map
contractsGrantsInvoiceDocument.getInvoiceDetailAccountObjectCodes().add(invoiceDetailAccountObjectCode);
}
}
} else {
LOG.error("Category Code cannot be found from the category list during recalculation of account object " + "code for Contracts & Grants Invoice Document.");
}
}
Aggregations