use of org.kuali.kfs.module.purap.businessobject.PurchaseOrderAccount in project cu-kfs by CU-CommunityApps.
the class PurchaseOrderDocument method customPrepareForSave.
/**
* @see org.kuali.kfs.module.purap.document.PurchasingAccountsPayableDocumentBase#customPrepareForSave(KualiDocumentEvent)
*/
@Override
public void customPrepareForSave(KualiDocumentEvent event) {
super.customPrepareForSave(event);
if (ObjectUtils.isNull(getPurapDocumentIdentifier())) {
// need retrieve the next available PO id to save in GL entries (only do if purap id is null which should be on first
// save)
SequenceAccessorService sas = SpringContext.getBean(SequenceAccessorService.class);
Long poSequenceNumber = sas.getNextAvailableSequenceNumber("PO_ID", this.getClass());
setPurapDocumentIdentifier(poSequenceNumber.intValue());
}
// Set outstanding encumbered quantity/amount on items
for (Iterator items = this.getItems().iterator(); items.hasNext(); ) {
PurchaseOrderItem item = (PurchaseOrderItem) items.next();
// Set quantities
item.setItemOutstandingEncumberedQuantity(item.getItemQuantity());
if (item.getItemInvoicedTotalQuantity() == null) {
item.setItemInvoicedTotalQuantity(ZERO);
}
if (item.getItemInvoicedTotalAmount() == null) {
item.setItemInvoicedTotalAmount(ZERO);
}
// Set amount
item.setItemOutstandingEncumberedAmount(item.getTotalAmount() == null ? ZERO : item.getTotalAmount());
List accounts = item.getSourceAccountingLines();
Collections.sort(accounts);
for (Iterator iterator = accounts.iterator(); iterator.hasNext(); ) {
PurchaseOrderAccount account = (PurchaseOrderAccount) iterator.next();
if (!account.isEmpty()) {
account.setItemAccountOutstandingEncumbranceAmount(account.getAmount());
}
}
// endfor accounts
}
// endfor items
this.setSourceAccountingLines(SpringContext.getBean(PurapAccountingService.class).generateSummaryWithNoZeroTotals(this.getItems()));
}
use of org.kuali.kfs.module.purap.businessobject.PurchaseOrderAccount in project cu-kfs by CU-CommunityApps.
the class PurchaseOrderDocument method getItemsActiveOnlySetupAlternateAmount.
/**
* Gets the active items in this Purchase Order, and sets up the alternate amount for GL entry creation.
*
* @return the list of all active items in this Purchase Order.
*/
public List getItemsActiveOnlySetupAlternateAmount() {
List returnList = new ArrayList();
for (Iterator iter = getItems().iterator(); iter.hasNext(); ) {
PurchaseOrderItem item = (PurchaseOrderItem) iter.next();
if (item.isItemActiveIndicator()) {
for (Iterator iterator = item.getSourceAccountingLines().iterator(); iterator.hasNext(); ) {
PurchaseOrderAccount account = (PurchaseOrderAccount) iterator.next();
account.setAlternateAmountForGLEntryCreation(account.getItemAccountOutstandingEncumbranceAmount());
}
returnList.add(item);
}
}
return returnList;
}
use of org.kuali.kfs.module.purap.businessobject.PurchaseOrderAccount in project cu-kfs by CU-CommunityApps.
the class CuPurapGeneralLedgerServiceImpl method reencumberEncumbrance.
@Override
protected List<SourceAccountingLine> reencumberEncumbrance(PaymentRequestDocument preq) {
LOG.debug("reencumberEncumbrance() started");
PurchaseOrderDocument po = purchaseOrderService.getCurrentPurchaseOrder(preq.getPurchaseOrderIdentifier());
Map encumbranceAccountMap = new HashMap();
// Get each item one by one
for (Iterator items = preq.getItems().iterator(); items.hasNext(); ) {
PaymentRequestItem payRequestItem = (PaymentRequestItem) items.next();
PurchaseOrderItem poItem = getPoItem(po, payRequestItem.getItemLineNumber(), payRequestItem.getItemType());
// Amount to reencumber for this item
KualiDecimal itemReEncumber = null;
String logItmNbr = "Item # " + payRequestItem.getItemLineNumber();
if (LOG.isDebugEnabled()) {
LOG.debug("reencumberEncumbrance() " + logItmNbr);
}
// If there isn't a PO item or the total amount is 0, we don't need encumbrances
final KualiDecimal preqItemTotalAmount = (payRequestItem.getTotalAmount() == null) ? KualiDecimal.ZERO : payRequestItem.getTotalAmount();
if ((poItem == null) || (preqItemTotalAmount.doubleValue() == 0)) {
if (poItem != null) {
// KFSUPGRADE-893 recumber $0 item too
if (poItem.getItemType().isQuantityBasedGeneralLedgerIndicator()) {
if (LOG.isDebugEnabled()) {
LOG.debug("reencumberEncumbrance() " + logItmNbr + " Calculate encumbrance based on quantity");
}
// Do disencumbrance calculations based on quantity
KualiDecimal preqQuantity = payRequestItem.getItemQuantity() == null ? ZERO : payRequestItem.getItemQuantity();
KualiDecimal outstandingEncumberedQuantity = poItem.getItemOutstandingEncumberedQuantity() == null ? ZERO : poItem.getItemOutstandingEncumberedQuantity();
KualiDecimal invoicedTotal = poItem.getItemInvoicedTotalQuantity() == null ? ZERO : poItem.getItemInvoicedTotalQuantity();
poItem.setItemInvoicedTotalQuantity(invoicedTotal.subtract(preqQuantity));
poItem.setItemOutstandingEncumberedQuantity(outstandingEncumberedQuantity.add(preqQuantity));
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("reencumberEncumbrance() " + logItmNbr + " No encumbrances required");
}
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("reencumberEncumbrance() " + logItmNbr + " Calculate encumbrance GL entries");
}
// Do we calculate the encumbrance amount based on quantity or amount?
if (poItem.getItemType().isQuantityBasedGeneralLedgerIndicator()) {
if (LOG.isDebugEnabled()) {
LOG.debug("reencumberEncumbrance() " + logItmNbr + " Calculate encumbrance based on quantity");
}
// Do disencumbrance calculations based on quantity
KualiDecimal preqQuantity = payRequestItem.getItemQuantity() == null ? ZERO : payRequestItem.getItemQuantity();
KualiDecimal outstandingEncumberedQuantity = poItem.getItemOutstandingEncumberedQuantity() == null ? ZERO : poItem.getItemOutstandingEncumberedQuantity();
KualiDecimal invoicedTotal = poItem.getItemInvoicedTotalQuantity() == null ? ZERO : poItem.getItemInvoicedTotalQuantity();
poItem.setItemInvoicedTotalQuantity(invoicedTotal.subtract(preqQuantity));
poItem.setItemOutstandingEncumberedQuantity(outstandingEncumberedQuantity.add(preqQuantity));
// do math as big decimal as doing it as a KualiDecimal will cause the item price to round to 2 digits
itemReEncumber = new KualiDecimal(preqQuantity.bigDecimalValue().multiply(poItem.getItemUnitPrice()));
// add tax for encumbrance
KualiDecimal itemTaxAmount = poItem.getItemTaxAmount() == null ? ZERO : poItem.getItemTaxAmount();
KualiDecimal encumbranceTaxAmount = preqQuantity.divide(poItem.getItemQuantity()).multiply(itemTaxAmount);
itemReEncumber = itemReEncumber.add(encumbranceTaxAmount);
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("reencumberEncumbrance() " + logItmNbr + " Calculate encumbrance based on amount");
}
itemReEncumber = preqItemTotalAmount;
// this prevents negative encumbrance
if ((poItem.getTotalAmount() != null) && (poItem.getTotalAmount().bigDecimalValue().signum() < 0)) {
// po item extended cost is negative
if ((poItem.getTotalAmount().compareTo(itemReEncumber)) > 0) {
itemReEncumber = poItem.getTotalAmount();
}
} else if ((poItem.getTotalAmount() != null) && (poItem.getTotalAmount().bigDecimalValue().signum() >= 0)) {
// po item extended cost is positive
if ((poItem.getTotalAmount().compareTo(itemReEncumber)) < 0) {
itemReEncumber = poItem.getTotalAmount();
}
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("reencumberEncumbrance() " + logItmNbr + " Amount to reencumber: " + itemReEncumber);
}
KualiDecimal outstandingEncumberedAmount = poItem.getItemOutstandingEncumberedAmount() == null ? ZERO : poItem.getItemOutstandingEncumberedAmount();
if (LOG.isDebugEnabled()) {
LOG.debug("reencumberEncumbrance() " + logItmNbr + " PO Item Outstanding Encumbrance Amount set to: " + outstandingEncumberedAmount);
}
KualiDecimal newOutstandingEncumberedAmount = outstandingEncumberedAmount.add(itemReEncumber);
if (LOG.isDebugEnabled()) {
LOG.debug("reencumberEncumbrance() " + logItmNbr + " New PO Item Outstanding Encumbrance Amount to set: " + newOutstandingEncumberedAmount);
}
poItem.setItemOutstandingEncumberedAmount(newOutstandingEncumberedAmount);
KualiDecimal invoicedTotalAmount = poItem.getItemInvoicedTotalAmount() == null ? ZERO : poItem.getItemInvoicedTotalAmount();
if (LOG.isDebugEnabled()) {
LOG.debug("reencumberEncumbrance() " + logItmNbr + " PO Item Invoiced Total Amount set to: " + invoicedTotalAmount);
}
KualiDecimal newInvoicedTotalAmount = invoicedTotalAmount.subtract(preqItemTotalAmount);
if (LOG.isDebugEnabled()) {
LOG.debug("reencumberEncumbrance() " + logItmNbr + " New PO Item Invoiced Total Amount to set: " + newInvoicedTotalAmount);
}
poItem.setItemInvoicedTotalAmount(newInvoicedTotalAmount);
// make the list of accounts for the reencumbrance entry
PurchaseOrderAccount lastAccount = null;
KualiDecimal accountTotal = ZERO;
// Sort accounts
Collections.sort((List) poItem.getSourceAccountingLines());
for (Iterator accountIter = poItem.getSourceAccountingLines().iterator(); accountIter.hasNext(); ) {
PurchaseOrderAccount account = (PurchaseOrderAccount) accountIter.next();
if (!account.isEmpty()) {
SourceAccountingLine acctString = account.generateSourceAccountingLine();
// amount = item reencumber * account percent / 100
KualiDecimal reencumbranceAmount = itemReEncumber.multiply(new KualiDecimal(account.getAccountLinePercent().toString())).divide(HUNDRED);
account.setItemAccountOutstandingEncumbranceAmount(account.getItemAccountOutstandingEncumbranceAmount().add(reencumbranceAmount));
// For rounding check at the end
accountTotal = accountTotal.add(reencumbranceAmount);
lastAccount = account;
if (LOG.isDebugEnabled()) {
LOG.debug("reencumberEncumbrance() " + logItmNbr + " " + acctString + " = " + reencumbranceAmount);
}
if (encumbranceAccountMap.containsKey(acctString)) {
KualiDecimal currentAmount = (KualiDecimal) encumbranceAccountMap.get(acctString);
encumbranceAccountMap.put(acctString, reencumbranceAmount.add(currentAmount));
} else {
encumbranceAccountMap.put(acctString, reencumbranceAmount);
}
}
}
// account for rounding by adjusting last account as needed
if (lastAccount != null) {
KualiDecimal difference = itemReEncumber.subtract(accountTotal);
if (LOG.isDebugEnabled()) {
LOG.debug("reencumberEncumbrance() difference: " + logItmNbr + " " + difference);
}
SourceAccountingLine acctString = lastAccount.generateSourceAccountingLine();
KualiDecimal amount = (KualiDecimal) encumbranceAccountMap.get(acctString);
if (amount == null) {
encumbranceAccountMap.put(acctString, difference);
} else {
encumbranceAccountMap.put(acctString, amount.add(difference));
}
lastAccount.setItemAccountOutstandingEncumbranceAmount(lastAccount.getItemAccountOutstandingEncumbranceAmount().add(difference));
}
}
}
SpringContext.getBean(BusinessObjectService.class).save(po);
List<SourceAccountingLine> encumbranceAccounts = new ArrayList<SourceAccountingLine>();
for (Iterator<SourceAccountingLine> iter = encumbranceAccountMap.keySet().iterator(); iter.hasNext(); ) {
SourceAccountingLine acctString = iter.next();
KualiDecimal amount = (KualiDecimal) encumbranceAccountMap.get(acctString);
if (amount.doubleValue() != 0) {
acctString.setAmount(amount);
encumbranceAccounts.add(acctString);
}
}
return encumbranceAccounts;
}
use of org.kuali.kfs.module.purap.businessobject.PurchaseOrderAccount in project cu-kfs by CU-CommunityApps.
the class CuPendingTransactionServiceImpl method reencumberEncumbrance.
/**
* Re-encumber the Encumbrance on a PO based on values in a PREQ. This is used when a PREQ is cancelled. Note: This modifies the
* encumbrance values on the PO and saves the PO
*
* @param preq PREQ for invoice
* @return List of accounting lines to use to create the pending general ledger entries
*/
protected List<SourceAccountingLine> reencumberEncumbrance(PaymentRequestDocument preq) {
LOG.debug("reencumberEncumbrance() started");
PurchaseOrderDocument po = SpringContext.getBean(PurchaseOrderService.class).getCurrentPurchaseOrder(preq.getPurchaseOrderIdentifier());
Map encumbranceAccountMap = new HashMap();
// Get each item one by one
for (Iterator items = preq.getItems().iterator(); items.hasNext(); ) {
PaymentRequestItem payRequestItem = (PaymentRequestItem) items.next();
PurchaseOrderItem poItem = getPoItem(po, payRequestItem.getItemLineNumber(), payRequestItem.getItemType());
// Amount to reencumber for this item
KualiDecimal itemReEncumber = null;
String logItmNbr = "Item # " + payRequestItem.getItemLineNumber();
LOG.debug("reencumberEncumbrance() " + logItmNbr);
// If there isn't a PO item or the total amount is 0, we don't need encumbrances
final KualiDecimal preqItemTotalAmount = (payRequestItem.getTotalAmount() == null) ? KualiDecimal.ZERO : payRequestItem.getTotalAmount();
if ((poItem == null) || (preqItemTotalAmount.doubleValue() == 0)) {
LOG.debug("reencumberEncumbrance() " + logItmNbr + " No encumbrances required");
} else {
LOG.debug("reencumberEncumbrance() " + logItmNbr + " Calculate encumbrance GL entries");
// Do we calculate the encumbrance amount based on quantity or amount?
if (poItem.getItemType().isQuantityBasedGeneralLedgerIndicator()) {
LOG.debug("reencumberEncumbrance() " + logItmNbr + " Calculate encumbrance based on quantity");
// Do disencumbrance calculations based on quantity
KualiDecimal preqQuantity = payRequestItem.getItemQuantity() == null ? ZERO : payRequestItem.getItemQuantity();
KualiDecimal outstandingEncumberedQuantity = poItem.getItemOutstandingEncumberedQuantity() == null ? ZERO : poItem.getItemOutstandingEncumberedQuantity();
KualiDecimal invoicedTotal = poItem.getItemInvoicedTotalQuantity() == null ? ZERO : poItem.getItemInvoicedTotalQuantity();
poItem.setItemInvoicedTotalQuantity(invoicedTotal.subtract(preqQuantity));
poItem.setItemOutstandingEncumberedQuantity(outstandingEncumberedQuantity.add(preqQuantity));
itemReEncumber = preqQuantity.multiply(new KualiDecimal(poItem.getItemUnitPrice()));
// add tax for encumbrance
KualiDecimal itemTaxAmount = poItem.getItemTaxAmount() == null ? ZERO : poItem.getItemTaxAmount();
KualiDecimal encumbranceTaxAmount = preqQuantity.divide(poItem.getItemQuantity()).multiply(itemTaxAmount);
itemReEncumber = itemReEncumber.add(encumbranceTaxAmount);
} else {
LOG.debug("reencumberEncumbrance() " + logItmNbr + " Calculate encumbrance based on amount");
itemReEncumber = preqItemTotalAmount;
// this prevents negative encumbrance
if ((poItem.getTotalAmount() != null) && (poItem.getTotalAmount().bigDecimalValue().signum() < 0)) {
// po item extended cost is negative
if ((poItem.getTotalAmount().compareTo(itemReEncumber)) > 0) {
itemReEncumber = poItem.getTotalAmount();
}
} else if ((poItem.getTotalAmount() != null) && (poItem.getTotalAmount().bigDecimalValue().signum() >= 0)) {
// po item extended cost is positive
if ((poItem.getTotalAmount().compareTo(itemReEncumber)) < 0) {
itemReEncumber = poItem.getTotalAmount();
}
}
}
LOG.debug("reencumberEncumbrance() " + logItmNbr + " Amount to reencumber: " + itemReEncumber);
KualiDecimal outstandingEncumberedAmount = poItem.getItemOutstandingEncumberedAmount() == null ? ZERO : poItem.getItemOutstandingEncumberedAmount();
LOG.debug("reencumberEncumbrance() " + logItmNbr + " PO Item Outstanding Encumbrance Amount set to: " + outstandingEncumberedAmount);
KualiDecimal newOutstandingEncumberedAmount = outstandingEncumberedAmount.add(itemReEncumber);
LOG.debug("reencumberEncumbrance() " + logItmNbr + " New PO Item Outstanding Encumbrance Amount to set: " + newOutstandingEncumberedAmount);
poItem.setItemOutstandingEncumberedAmount(newOutstandingEncumberedAmount);
KualiDecimal invoicedTotalAmount = poItem.getItemInvoicedTotalAmount() == null ? ZERO : poItem.getItemInvoicedTotalAmount();
LOG.debug("reencumberEncumbrance() " + logItmNbr + " PO Item Invoiced Total Amount set to: " + invoicedTotalAmount);
KualiDecimal newInvoicedTotalAmount = invoicedTotalAmount.subtract(preqItemTotalAmount);
LOG.debug("reencumberEncumbrance() " + logItmNbr + " New PO Item Invoiced Total Amount to set: " + newInvoicedTotalAmount);
poItem.setItemInvoicedTotalAmount(newInvoicedTotalAmount);
// make the list of accounts for the reencumbrance entry
PurchaseOrderAccount lastAccount = null;
KualiDecimal accountTotal = ZERO;
// Sort accounts
Collections.sort((List) poItem.getSourceAccountingLines());
for (Iterator accountIter = poItem.getSourceAccountingLines().iterator(); accountIter.hasNext(); ) {
PurchaseOrderAccount account = (PurchaseOrderAccount) accountIter.next();
if (!account.isEmpty()) {
SourceAccountingLine acctString = account.generateSourceAccountingLine();
// amount = item reencumber * account percent / 100
KualiDecimal reencumbranceAmount = itemReEncumber.multiply(new KualiDecimal(account.getAccountLinePercent().toString())).divide(HUNDRED);
account.setItemAccountOutstandingEncumbranceAmount(account.getItemAccountOutstandingEncumbranceAmount().add(reencumbranceAmount));
// For rounding check at the end
accountTotal = accountTotal.add(reencumbranceAmount);
lastAccount = account;
LOG.debug("reencumberEncumbrance() " + logItmNbr + " " + acctString + " = " + reencumbranceAmount);
if (encumbranceAccountMap.containsKey(acctString)) {
KualiDecimal currentAmount = (KualiDecimal) encumbranceAccountMap.get(acctString);
encumbranceAccountMap.put(acctString, reencumbranceAmount.add(currentAmount));
} else {
encumbranceAccountMap.put(acctString, reencumbranceAmount);
}
}
}
// account for rounding by adjusting last account as needed
if (lastAccount != null) {
KualiDecimal difference = itemReEncumber.subtract(accountTotal);
LOG.debug("reencumberEncumbrance() difference: " + logItmNbr + " " + difference);
SourceAccountingLine acctString = lastAccount.generateSourceAccountingLine();
KualiDecimal amount = (KualiDecimal) encumbranceAccountMap.get(acctString);
if (amount == null) {
encumbranceAccountMap.put(acctString, difference);
} else {
encumbranceAccountMap.put(acctString, amount.add(difference));
}
lastAccount.setItemAccountOutstandingEncumbranceAmount(lastAccount.getItemAccountOutstandingEncumbranceAmount().add(difference));
}
}
}
List<SourceAccountingLine> encumbranceAccounts = new ArrayList<SourceAccountingLine>();
for (Iterator<SourceAccountingLine> iter = encumbranceAccountMap.keySet().iterator(); iter.hasNext(); ) {
SourceAccountingLine acctString = (SourceAccountingLine) iter.next();
KualiDecimal amount = (KualiDecimal) encumbranceAccountMap.get(acctString);
if (amount.doubleValue() != 0) {
acctString.setAmount(amount);
encumbranceAccounts.add(acctString);
}
}
return encumbranceAccounts;
}
use of org.kuali.kfs.module.purap.businessobject.PurchaseOrderAccount in project cu-kfs by CU-CommunityApps.
the class FavoriteAccountLineBuilderTest method testLineBuilderFailureForNonexistentLine.
/*
* Test that line creation will fail if no favorite account exists for the given ID.
*/
@Test
public void testLineBuilderFailureForNonexistentLine() throws Exception {
Integer badId = Integer.valueOf(-1);
reqsItem.setFavoriteAccountLineIdentifier(badId);
poDoc.setFavoriteAccountLineIdentifier(badId);
iwntDoc.setFavoriteAccountLineIdentifier(badId);
PurchasingFavoriteAccountLineBuilderForLineItem<RequisitionAccount> reqsBuilder = createBuilderForLineItem(reqsItem, 0, new RequisitionAccount());
PurchasingFavoriteAccountLineBuilderForDistribution<PurchaseOrderAccount> poBuilder = createBuilderForDistribution(poDoc, poAccounts, new PurchaseOrderAccount());
PurApFavoriteAccountLineBuilderForIWantDocument iwntBuilder = createBuilderForIWant(iwntDoc);
assertUnsuccessfulAccountLineCreation(reqsBuilder);
clearMessageMapErrors();
assertUnsuccessfulAccountLineCreation(poBuilder);
clearMessageMapErrors();
assertUnsuccessfulAccountLineCreation(iwntBuilder);
clearMessageMapErrors();
assertUnsuccessfulAccountLineAdditionToList(reqsBuilder);
clearMessageMapErrors();
assertUnsuccessfulAccountLineAdditionToList(poBuilder);
clearMessageMapErrors();
assertUnsuccessfulAccountLineAdditionToList(iwntBuilder);
}
Aggregations