use of org.kuali.kfs.module.purap.businessobject.PurchaseOrderItem 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.PurchaseOrderItem in project cu-kfs by CU-CommunityApps.
the class PurchaseOrderDocument method customPrepareForSave.
@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 (Object anItem : this.getItems()) {
PurchaseOrderItem item = (PurchaseOrderItem) anItem;
// Set quantities
item.setItemOutstandingEncumberedQuantity(item.getItemQuantity());
if (item.getItemInvoicedTotalQuantity() == null) {
item.setItemInvoicedTotalQuantity(KualiDecimal.ZERO);
}
if (item.getItemInvoicedTotalAmount() == null) {
item.setItemInvoicedTotalAmount(KualiDecimal.ZERO);
}
item.setItemOutstandingEncumberedAmount(item.getTotalAmount() == null ? KualiDecimal.ZERO : item.getTotalAmount());
List accounts = item.getSourceAccountingLines();
Collections.sort(accounts);
for (Object anAcct : accounts) {
PurchaseOrderAccount account = (PurchaseOrderAccount) anAcct;
if (!account.isEmpty()) {
account.setItemAccountOutstandingEncumbranceAmount(account.getAmount());
}
}
}
this.setSourceAccountingLines(SpringContext.getBean(PurapAccountingService.class).generateSummaryWithNoZeroTotals(this.getItems()));
}
use of org.kuali.kfs.module.purap.businessobject.PurchaseOrderItem 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<PurchaseOrderItem> returnList = new ArrayList<>();
for (Object anItem : getItems()) {
PurchaseOrderItem item = (PurchaseOrderItem) anItem;
if (item.isItemActiveIndicator()) {
for (PurApAccountingLine purApAccountingLine : item.getSourceAccountingLines()) {
PurchaseOrderAccount account = (PurchaseOrderAccount) purApAccountingLine;
account.setAlternateAmountForGLEntryCreation(account.getItemAccountOutstandingEncumbranceAmount());
}
returnList.add(item);
}
}
return returnList;
}
use of org.kuali.kfs.module.purap.businessobject.PurchaseOrderItem in project cu-kfs by CU-CommunityApps.
the class PurchaseOrderDocument method getTotalPreTaxDollarAmount.
/**
* Gets the pre tax total dollar amount for this Purchase Order.
*
* @param includeInactive indicates whether inactive items shall be included.
* @param includeBelowTheLine indicates whether below the line items shall be included.
* @return the total dollar amount for this Purchase Order.
*/
public KualiDecimal getTotalPreTaxDollarAmount(boolean includeInactive, boolean includeBelowTheLine) {
KualiDecimal total = new KualiDecimal(BigDecimal.ZERO);
for (PurchaseOrderItem item : (List<PurchaseOrderItem>) getItems()) {
ItemType it = item.getItemType();
if ((includeBelowTheLine || it.isLineItemIndicator()) && (includeInactive || item.isItemActiveIndicator())) {
KualiDecimal extendedPrice = item.getExtendedPrice();
KualiDecimal itemTotal = (extendedPrice != null) ? extendedPrice : KualiDecimal.ZERO;
total = total.add(itemTotal);
}
}
return total;
}
use of org.kuali.kfs.module.purap.businessobject.PurchaseOrderItem in project cu-kfs by CU-CommunityApps.
the class PurchaseOrderDocument method populatePurchaseOrderFromRequisition.
/**
* Populates this Purchase Order from the related Requisition Document.
*
* @param requisitionDocument the Requisition Document from which field values are copied.
*/
public void populatePurchaseOrderFromRequisition(RequisitionDocument requisitionDocument) {
this.setPurchaseOrderCreateTimestamp(SpringContext.getBean(DateTimeService.class).getCurrentTimestamp());
this.getDocumentHeader().setOrganizationDocumentNumber(requisitionDocument.getDocumentHeader().getOrganizationDocumentNumber());
this.getDocumentHeader().setDocumentDescription(requisitionDocument.getDocumentHeader().getDocumentDescription());
this.getDocumentHeader().setExplanation(requisitionDocument.getDocumentHeader().getExplanation());
this.setBillingName(requisitionDocument.getBillingName());
this.setBillingLine1Address(requisitionDocument.getBillingLine1Address());
this.setBillingLine2Address(requisitionDocument.getBillingLine2Address());
this.setBillingCityName(requisitionDocument.getBillingCityName());
this.setBillingStateCode(requisitionDocument.getBillingStateCode());
this.setBillingPostalCode(requisitionDocument.getBillingPostalCode());
this.setBillingCountryCode(requisitionDocument.getBillingCountryCode());
this.setBillingPhoneNumber(requisitionDocument.getBillingPhoneNumber());
this.setBillingEmailAddress(requisitionDocument.getBillingEmailAddress());
this.setReceivingName(requisitionDocument.getReceivingName());
this.setReceivingCityName(requisitionDocument.getReceivingCityName());
this.setReceivingLine1Address(requisitionDocument.getReceivingLine1Address());
this.setReceivingLine2Address(requisitionDocument.getReceivingLine2Address());
this.setReceivingStateCode(requisitionDocument.getReceivingStateCode());
this.setReceivingPostalCode(requisitionDocument.getReceivingPostalCode());
this.setReceivingCountryCode(requisitionDocument.getReceivingCountryCode());
this.setAddressToVendorIndicator(requisitionDocument.getAddressToVendorIndicator());
this.setDeliveryBuildingCode(requisitionDocument.getDeliveryBuildingCode());
this.setDeliveryBuildingRoomNumber(requisitionDocument.getDeliveryBuildingRoomNumber());
this.setDeliveryBuildingName(requisitionDocument.getDeliveryBuildingName());
this.setDeliveryCampusCode(requisitionDocument.getDeliveryCampusCode());
this.setDeliveryCityName(requisitionDocument.getDeliveryCityName());
this.setDeliveryCountryCode(requisitionDocument.getDeliveryCountryCode());
this.setDeliveryInstructionText(requisitionDocument.getDeliveryInstructionText());
this.setDeliveryBuildingLine1Address(requisitionDocument.getDeliveryBuildingLine1Address());
this.setDeliveryBuildingLine2Address(requisitionDocument.getDeliveryBuildingLine2Address());
this.setDeliveryPostalCode(requisitionDocument.getDeliveryPostalCode());
this.setDeliveryRequiredDate(requisitionDocument.getDeliveryRequiredDate());
this.setDeliveryRequiredDateReasonCode(requisitionDocument.getDeliveryRequiredDateReasonCode());
this.setDeliveryStateCode(requisitionDocument.getDeliveryStateCode());
this.setDeliveryToEmailAddress(requisitionDocument.getDeliveryToEmailAddress());
this.setDeliveryToName(requisitionDocument.getDeliveryToName());
this.setDeliveryToPhoneNumber(requisitionDocument.getDeliveryToPhoneNumber());
this.setDeliveryBuildingOtherIndicator(requisitionDocument.isDeliveryBuildingOtherIndicator());
this.setPurchaseOrderBeginDate(requisitionDocument.getPurchaseOrderBeginDate());
this.setPurchaseOrderCostSourceCode(requisitionDocument.getPurchaseOrderCostSourceCode());
this.setPostingYear(requisitionDocument.getPostingYear());
this.setPurchaseOrderEndDate(requisitionDocument.getPurchaseOrderEndDate());
this.setChartOfAccountsCode(requisitionDocument.getChartOfAccountsCode());
this.setDocumentFundingSourceCode(requisitionDocument.getDocumentFundingSourceCode());
this.setInstitutionContactEmailAddress(requisitionDocument.getInstitutionContactEmailAddress());
this.setInstitutionContactName(requisitionDocument.getInstitutionContactName());
this.setInstitutionContactPhoneNumber(requisitionDocument.getInstitutionContactPhoneNumber());
this.setNonInstitutionFundAccountNumber(requisitionDocument.getNonInstitutionFundAccountNumber());
this.setNonInstitutionFundChartOfAccountsCode(requisitionDocument.getNonInstitutionFundChartOfAccountsCode());
this.setNonInstitutionFundOrgChartOfAccountsCode(requisitionDocument.getNonInstitutionFundOrgChartOfAccountsCode());
this.setNonInstitutionFundOrganizationCode(requisitionDocument.getNonInstitutionFundOrganizationCode());
this.setOrganizationCode(requisitionDocument.getOrganizationCode());
this.setRecurringPaymentTypeCode(requisitionDocument.getRecurringPaymentTypeCode());
this.setRequestorPersonEmailAddress(requisitionDocument.getRequestorPersonEmailAddress());
this.setRequestorPersonName(requisitionDocument.getRequestorPersonName());
this.setRequestorPersonPhoneNumber(requisitionDocument.getRequestorPersonPhoneNumber());
this.setRequisitionIdentifier(requisitionDocument.getPurapDocumentIdentifier());
this.setPurchaseOrderTotalLimit(requisitionDocument.getPurchaseOrderTotalLimit());
this.setPurchaseOrderTransmissionMethodCode(requisitionDocument.getPurchaseOrderTransmissionMethodCode());
this.setUseTaxIndicator(requisitionDocument.isUseTaxIndicator());
this.setVendorCityName(requisitionDocument.getVendorCityName());
this.setVendorContractGeneratedIdentifier(requisitionDocument.getVendorContractGeneratedIdentifier());
this.setVendorCountryCode(requisitionDocument.getVendorCountryCode());
this.setVendorCustomerNumber(requisitionDocument.getVendorCustomerNumber());
this.setVendorAttentionName(requisitionDocument.getVendorAttentionName());
this.setVendorDetailAssignedIdentifier(requisitionDocument.getVendorDetailAssignedIdentifier());
this.setVendorFaxNumber(requisitionDocument.getVendorFaxNumber());
this.setVendorHeaderGeneratedIdentifier(requisitionDocument.getVendorHeaderGeneratedIdentifier());
this.setVendorLine1Address(requisitionDocument.getVendorLine1Address());
this.setVendorLine2Address(requisitionDocument.getVendorLine2Address());
this.setVendorAddressInternationalProvinceName(requisitionDocument.getVendorAddressInternationalProvinceName());
this.setVendorName(requisitionDocument.getVendorName());
this.setVendorNoteText(requisitionDocument.getVendorNoteText());
this.setVendorPhoneNumber(requisitionDocument.getVendorPhoneNumber());
this.setVendorPostalCode(requisitionDocument.getVendorPostalCode());
this.setVendorStateCode(requisitionDocument.getVendorStateCode());
this.setVendorRestrictedIndicator(requisitionDocument.getVendorRestrictedIndicator());
this.setJustification(requisitionDocument.getJustification());
// KFSPTS-1458, KFSPTS-16990: Also copy vendorEmailAddress from Requisition to PO
this.setVendorEmailAddress(requisitionDocument.getVendorEmailAddress());
this.setExternalOrganizationB2bSupplierIdentifier(requisitionDocument.getExternalOrganizationB2bSupplierIdentifier());
this.setRequisitionSourceCode(requisitionDocument.getRequisitionSourceCode());
this.setAccountsPayablePurchasingDocumentLinkIdentifier(requisitionDocument.getAccountsPayablePurchasingDocumentLinkIdentifier());
this.setReceivingDocumentRequiredIndicator(requisitionDocument.isReceivingDocumentRequiredIndicator());
this.setPaymentRequestPositiveApprovalIndicator(requisitionDocument.isPaymentRequestPositiveApprovalIndicator());
setApplicationDocumentStatus(PurchaseOrderStatuses.APPDOC_IN_PROCESS);
this.setAccountDistributionMethod(requisitionDocument.getAccountDistributionMethod());
// Copy items from requisition (which will copy the item's accounts and capital assets)
List<PurchaseOrderItem> items = new ArrayList<>();
for (PurApItem reqItem : ((PurchasingAccountsPayableDocument) requisitionDocument).getItems()) {
RequisitionCapitalAssetItem reqCamsItem = (RequisitionCapitalAssetItem) requisitionDocument.getPurchasingCapitalAssetItemByItemIdentifier(reqItem.getItemIdentifier());
items.add(new PurchaseOrderItem((RequisitionItem) reqItem, this, reqCamsItem));
}
this.setItems(items);
// Copy capital asset information that is directly off the document.
this.setCapitalAssetSystemTypeCode(requisitionDocument.getCapitalAssetSystemTypeCode());
this.setCapitalAssetSystemStateCode(requisitionDocument.getCapitalAssetSystemStateCode());
for (CapitalAssetSystem capitalAssetSystem : requisitionDocument.getPurchasingCapitalAssetSystems()) {
this.getPurchasingCapitalAssetSystems().add(new PurchaseOrderCapitalAssetSystem(capitalAssetSystem));
}
this.fixItemReferences();
// KFSUPGRADE-346/KITI-727
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());
}
// KFSUPGRADE-337/KITI-2414 : checking for duplicate sensitive data types
Set<String> currentSensitiveDataCodes = new HashSet<String>();
if (purchaseOrderSensitiveData != null) {
for (PurchaseOrderSensitiveData sensitiveData : purchaseOrderSensitiveData) {
currentSensitiveDataCodes.add(sensitiveData.getSensitiveDataCode());
}
}
if (requisitionDocument.isSensitive()) {
for (PurchasingItemBase pib : (List<PurchasingItemBase>) requisitionDocument.getItems()) {
CommodityCode cc = pib.getCommodityCode();
if (cc != null && cc.getSensitiveDataCode() != null && currentSensitiveDataCodes.add(cc.getSensitiveDataCode())) {
if (purchaseOrderSensitiveData == null) {
boolean addNewData = true;
purchaseOrderSensitiveData = getPurchaseOrderSensitiveData();
for (PurchaseOrderSensitiveData sensitiveData : purchaseOrderSensitiveData) {
if (!currentSensitiveDataCodes.add(sensitiveData.getSensitiveDataCode())) {
addNewData = false;
}
}
if (addNewData) {
purchaseOrderSensitiveData.add(new PurchaseOrderSensitiveData(getPurapDocumentIdentifier(), getRequisitionIdentifier(), cc.getSensitiveDataCode()));
}
} else {
purchaseOrderSensitiveData.add(new PurchaseOrderSensitiveData(getPurapDocumentIdentifier(), getRequisitionIdentifier(), cc.getSensitiveDataCode()));
}
}
}
}
if (purchaseOrderSensitiveData != null) {
SpringContext.getBean(BusinessObjectService.class).save(purchaseOrderSensitiveData);
}
}
Aggregations