package com.gpc.client.pointofsale.invoice.line; import java.math.BigDecimal; import java.util.LinkedList; import java.util.Vector; import org.apache.log4j.Logger; import com.gpc.client.common.ClientApplicationContext; import com.gpc.common.Profile; import com.gpc.common.TsoConstant; import com.gpc.common.constants.refvalues.RefInvoiceLineItemType; import com.gpc.common.constants.refvalues.RefInvoiceSpecialFunction; import com.gpc.common.constants.refvalues.RefState; import com.gpc.common.exception.ApplicationException; import com.gpc.common.pointofsale.invoice.line.CommonInvoiceDeliveryFeeLineBL; import com.gpc.common.util.MathUtil; import com.gpc.valueobjects.customer.CustomerVO; import com.gpc.valueobjects.invoice.InvoiceVO; import com.gpc.valueobjects.invoice.line.BaseLineItemVO; import com.gpc.valueobjects.invoice.line.InvoiceLineItemVO; import com.gpc.valueobjects.profile.StoreProfileVO; /** * This class contains business logic for handling delivery fee line item . * * @author $Author: * * @version $Revision: $, $Date: $ */ public class InvoiceDeliveryFeeLineBL extends CommonInvoiceDeliveryFeeLineBL { private static final Logger logger = Logger.getLogger(InvoiceDeliveryFeeLineBL.class); public static BaseLineItemVO createDeliveryFeeLineItem(final InvoiceVO invoiceVO, final CustomerVO customerVO, final String lineAbbrev, final int invoiceLineItemType) { final String methodSpec = "[DeliveryFee] InvoiceDeliveryFeeLineBL.createDeliveryFeeLineItem - "; logger.debug(methodSpec); ClientApplicationContext clientApplicationContext = ClientApplicationContext .getClientApplicationContext(); final Profile profile = clientApplicationContext.getProfile(Profile.POINT_OF_SALE_CLIENT, clientApplicationContext.getLocation()); final String storeLanguageCd = profile.getStoreProfile().getRefLanguageCd(); String deliveryFeeLineItemDescription = null; final Integer employeeId = clientApplicationContext.getCurrentUser().getEmployeeId(); try { deliveryFeeLineItemDescription = clientApplicationContext.getRefTableDAO() .getSpecialFuntionDescription(lineAbbrev, storeLanguageCd); } catch (ApplicationException applicationException) { logger.error("Error occured while retrieving line description" + applicationException.toString(), applicationException); } BaseLineItemVO deliveryFeeLineItem = null; if (invoiceVO != null && customerVO != null && deliveryFeeLineItemDescription != null && employeeId != null) { deliveryFeeLineItem = createDeliveryFeeLineItem(lineAbbrev, deliveryFeeLineItemDescription, invoiceLineItemType, employeeId, invoiceVO.getLOC()); deliveryFeeLineItem.setLOC(customerVO.getLOC()); deliveryFeeLineItem.setGroupCode(new Short("0")); deliveryFeeLineItem.setLoyaltyEligible(false); deliveryFeeLineItem.setNonStocking(false); InvoiceLineBL.setDisplayPricesForLineItem(deliveryFeeLineItem, customerVO, invoiceVO); invoiceVO.setCoroladoFeeLineAdded( invoiceLineItemType == RefInvoiceLineItemType.COLORADO_DELIVERY_FEE); invoiceVO.setDeliveryFeeLineItemAdded( invoiceLineItemType == RefInvoiceLineItemType.MINNESOTA_DELIVERY_FEE); } return deliveryFeeLineItem; } public static boolean isDeliveryFeeApplicableForColorado(final CustomerVO customerVO, final StoreProfileVO storeprofile) { final String methodSpec = "[DeliveryFee] InvoiceDeliveryFeeLineBL.isDeliveryFeeApplicableForColorado - "; logger.debug(methodSpec); return storeprofile != null && customerVO != null && customerVO.getState().equalsIgnoreCase(RefState.COLORADO) && storeprofile.getRetailDeliveryFee().equalsIgnoreCase(customerVO.getState()) && customerVO.getCustomerDeliveryVO() != null && customerVO.getCustomerDeliveryVO().getDeliveryRefOptionCD() .equalsIgnoreCase(TsoConstant.DELIVERY_OPTION_ALWAYS); } public static boolean isDeliveryFeeApplicableForInvoice(final InvoiceVO invoice, final CustomerVO customer, final StoreProfileVO storeProfile, final BaseLineItemVO baseLineItem, final LinkedList autoGeneratedItems) { final String methodSpec = "[DeliveryFee] InvoiceDeliveryFeeLineBL.isDeliveryFeeApplicableForInvoice - "; logger.debug(methodSpec); boolean isDeliveryFeeApplicableForInvoice = false; if (isDeliveryFeeApplicableForCustomer(customer, storeProfile, RefState.MINNESOTA)) { final BigDecimal taxableSales = calculateInvoiceTaxableSales(invoice, customer, baseLineItem, autoGeneratedItems); logger.debug(methodSpec + "Invoice Taxable Sales = " + taxableSales); if (taxableSales != null && taxableSales.compareTo(new BigDecimal(100)) > 0) { isDeliveryFeeApplicableForInvoice = true; } } return isDeliveryFeeApplicableForInvoice; } public static boolean isDeliveryFeeApplicableForInvoice(final InvoiceVO invoice, final CustomerVO customer, final StoreProfileVO storeProfile) { final String methodSpec = "[DeliveryFee] InvoiceDeliveryFeeLineBL.isDeliveryFeeApplicableForMinnesota - "; logger.debug(methodSpec); boolean isDeliveryFeeApplicableForInvoice = false; if (isDeliveryFeeApplicableForState(customer, storeProfile)) { if(storeProfile.getRetailDeliveryFee().equalsIgnoreCase(RefState.MINNESOTA)) { final BigDecimal taxableSales = calculateInvoiceTaxableSales(invoice, customer); logger.debug(methodSpec + "Invoice Taxable Sales = " + taxableSales); if (taxableSales != null && taxableSales.compareTo(new BigDecimal(100)) > 0) { isDeliveryFeeApplicableForInvoice = true; } } } return isDeliveryFeeApplicableForInvoice; } public static boolean isDeliveryFeeApplicable(final InvoiceVO invoice, final CustomerVO customer, final StoreProfileVO storeProfile) { final String methodSpec = "[DeliveryFee] FinalizeInvoiceBaseController.isDeliveryFeeApplicable - "; logger.debug(methodSpec); boolean isDeliveryFeeApplicable = false; if (storeProfile != null && customer != null && storeProfile.getRetailDeliveryFee() != null && storeProfile.getRetailDeliveryFee().equalsIgnoreCase(customer.getState())) { if (storeProfile.getRetailDeliveryFee().equalsIgnoreCase(RefState.MINNESOTA)) { if (invoice != null && (invoice.getTaxableSales1().compareTo(new BigDecimal(100)) > 0 || invoice.getTaxableSales2().compareTo(new BigDecimal(100)) > 0)) { isDeliveryFeeApplicable = true; } } } logger.debug(methodSpec + "isDeliveryFeeApplicable = " + isDeliveryFeeApplicable); return isDeliveryFeeApplicable; } public static boolean isDeliveryFeeApplicableForCustomer(final CustomerVO customer, final StoreProfileVO storeProfile, final String customerState) { return storeProfile != null && customer != null && customer.getState().equalsIgnoreCase(customerState) && storeProfile.getRetailDeliveryFee().equalsIgnoreCase(customer.getState()) && customer.getCustomerDeliveryVO() != null && customer.getCustomerDeliveryVO().getDeliveryRefOptionCD() .equalsIgnoreCase(TsoConstant.DELIVERY_OPTION_ALWAYS); } public static BigDecimal calculateInvoiceTaxableSales(final InvoiceVO invoice, final CustomerVO customer, final BaseLineItemVO lineItem, final LinkedList autoGeneratedItems) { final String methodSpec = "[DeliveryFee] InvoiceDeliveryFeeLineBL.calculateInvoiceTaxableSubTotal - "; logger.debug(methodSpec); BigDecimal invoiceTaxableAmount = calculateInvoiceTaxableSubTotal(invoice, customer, lineItem); invoiceTaxableAmount = invoiceTaxableAmount .add(calculateAutoGeneratedItemsTaxableAmount(autoGeneratedItems, customer)); logger.debug(methodSpec + "InvoiceTaxableAmount = " + invoiceTaxableAmount); return invoiceTaxableAmount; } public static boolean isDeliveryFeeApplicableForMinnesota(final CustomerVO customerVO, final StoreProfileVO storeprofile) { final String methodSpec = "[DeliveryFee] InvoiceDeliveryFeeLineBL.isDeliveryFeeApplicableForMinnesota - "; logger.debug(methodSpec); return storeprofile != null && customerVO != null && customerVO.getState().equalsIgnoreCase(RefState.MINNESOTA) && storeprofile.getRetailDeliveryFee().equalsIgnoreCase(customerVO.getState()) && customerVO.getCustomerDeliveryVO() != null && customerVO.getCustomerDeliveryVO().getDeliveryRefOptionCD() .equalsIgnoreCase(TsoConstant.DELIVERY_OPTION_ALWAYS); } public static boolean isDeliveryFeeApplicableForState(final CustomerVO customerVO, final StoreProfileVO storeprofile) { final String methodSpec = "[DeliveryFee] InvoiceDeliveryFeeLineBL.isDeliveryFeeApplicableForState - "; logger.debug(methodSpec); boolean isDeliveryFeeApplicableForState = false; if (storeprofile != null && customerVO != null && storeprofile.getRetailDeliveryFee() != null && storeprofile.getRetailDeliveryFee().equalsIgnoreCase(customerVO.getState()) && customerVO.getCustomerDeliveryVO() != null && customerVO.getCustomerDeliveryVO().getDeliveryRefOptionCD() .equalsIgnoreCase(TsoConstant.DELIVERY_OPTION_ALWAYS)) { if (customerVO.getState().equalsIgnoreCase(RefState.MINNESOTA)) { isDeliveryFeeApplicableForState = true; } } return isDeliveryFeeApplicableForState; } public static void removeDeliveryFeeLineItem(final InvoiceVO invoiceVO, final int invoiceLineItemType) { final String methodSpec = "[DeliveryFee] InvoiceDeliveryFeeLineBL.removeDeliveryFeeLineItem - "; logger.debug(methodSpec); Vector itemList = invoiceVO.getLineItems(); if (itemList != null) { int listSize = itemList.size(); InvoiceLineItemVO lineItem = null; for (int index = 0; index < listSize; index++) { lineItem = (InvoiceLineItemVO) itemList.elementAt(index); if (lineItem instanceof BaseLineItemVO) { InvoiceLineItemVO invoiceLineItemVO = (InvoiceLineItemVO) lineItem; if (invoiceLineItemVO.getGeneratedByLineItem() == null && invoiceLineItemVO.getRefInvoiceLineItemTypeID() != null && invoiceLineItemVO.getRefInvoiceLineItemTypeID() .intValue() == invoiceLineItemType) { itemList.remove(index); listSize = itemList.size(); index--; if (invoiceLineItemType == RefInvoiceLineItemType.COLORADO_DELIVERY_FEE) { invoiceVO.setCoroladoFeeLineAdded(false); } else if (invoiceLineItemType == RefInvoiceLineItemType.MINNESOTA_DELIVERY_FEE) { invoiceVO.setDeliveryFeeLineItemAdded(false); } } } } } } public static boolean isDeliveryFeeApplicableForMinnesota(final InvoiceVO invoice, final CustomerVO customer, final BaseLineItemVO baseLineItem) { final String methodSpec = "[DeliveryFee] InvoiceDeliveryFeeLineBL.isDeliveryFeeApplicableForMinnesota - "; logger.debug(methodSpec); final BigDecimal invoiceTaxableSales = calculateInvoiceTaxableSubTotal(invoice, customer, baseLineItem); boolean isDeliveryFeeApplicableForMinnesota = invoiceTaxableSales != null && invoiceTaxableSales.compareTo(new BigDecimal(100)) > 0; logger.debug(methodSpec + "isDeliveryFeeApplicableForMinnesota = " + isDeliveryFeeApplicableForMinnesota); return isDeliveryFeeApplicableForMinnesota; } public static BigDecimal calculateInvoiceTaxableSubTotal(final InvoiceVO invoice, final CustomerVO customer, final BaseLineItemVO baseLineItem) { final String methodSpec = "[DeliveryFee] InvoiceDeliveryFeeLineBL.calculateInvoiceTaxableSubTotal - "; logger.debug(methodSpec); BigDecimal invoiceTaxableAmount = MathUtil.BD_ZERO; invoiceTaxableAmount = calculateInvoiceTaxableSales(invoice, customer); invoiceTaxableAmount = invoiceTaxableAmount .add(calculateLineItemTaxableAmount(baseLineItem, customer)) .setScale(2, BigDecimal.ROUND_HALF_UP); logger.debug(methodSpec + "Invoice Taxable Amount = " + invoiceTaxableAmount); return invoiceTaxableAmount; } public static BigDecimal calculateInvoiceTaxableSales(final InvoiceVO invoice, CustomerVO customer) { final String methodSpec = "[DeliveryFee] InvoiceDeliveryFeeLineBL.calculateInvoiceTaxableSales - "; logger.debug(methodSpec); BigDecimal invoiceTaxableAmount = MathUtil.BD_ZERO; invoiceTaxableAmount = invoiceTaxableAmount.setScale(2, BigDecimal.ROUND_HALF_UP); if (invoice != null) { final Vector invoiceLineItems = invoice.getLineItems(); if (invoiceLineItems != null) { final int invoiceLineItemsSize = invoiceLineItems.size(); InvoiceLineItemVO invoiceLineItem = null; BaseLineItemVO baseLineItem = null; for (int lineItemIndex = 0; lineItemIndex < invoiceLineItemsSize; lineItemIndex++) { invoiceLineItem = (InvoiceLineItemVO) invoiceLineItems.elementAt(lineItemIndex); if (invoiceLineItem instanceof BaseLineItemVO) { baseLineItem = (BaseLineItemVO) invoiceLineItem; if (canExcludeCoreItem(baseLineItem, customer)) { continue; } if (baseLineItem.getTaxable1().booleanValue() || baseLineItem.getTaxable2().booleanValue()) { invoiceTaxableAmount = invoiceTaxableAmount .add(baseLineItem.getExtendedPrice()) .setScale(2, BigDecimal.ROUND_HALF_UP); } } } } } return invoiceTaxableAmount; } private static BigDecimal calculateLineItemTaxableAmount(final BaseLineItemVO baseLineItem, final CustomerVO customer) { final String methodSpec = "[DeliveryFee] InvoiceDeliveryFeeLineBL.calculateLineItemTaxableAmount - "; logger.debug(methodSpec); BigDecimal lineItemTaxableAmount = MathUtil.BD_ZERO; lineItemTaxableAmount = lineItemTaxableAmount.setScale(2, BigDecimal.ROUND_HALF_UP); if (baseLineItem != null && (baseLineItem.getTaxable1().booleanValue() || baseLineItem.getTaxable2().booleanValue()) && !canExcludeCoreItem(baseLineItem, customer)) { lineItemTaxableAmount = lineItemTaxableAmount.add(baseLineItem.getExtendedPrice()) .setScale(2, BigDecimal.ROUND_HALF_UP); } return lineItemTaxableAmount; } public static BigDecimal calculateAutoGeneratedItemsTaxableAmount( final LinkedList autogeneratedItems, final CustomerVO customer) { final String methodSpec = "[DeliveryFee] InvoiceDeliveryFeeLineBL.calculateAutoGeneratedItemsTaxableAmount - "; logger.debug(methodSpec); BigDecimal taxableAmount = MathUtil.BD_ZERO; taxableAmount = taxableAmount.setScale(2, BigDecimal.ROUND_HALF_UP); if (autogeneratedItems != null) { int autogeneratedItemsSize = autogeneratedItems.size(); InvoiceLineItemVO invoiceLineItem = null; BaseLineItemVO baseLineItem = null; for (int index = 0; index < autogeneratedItemsSize; index++) { invoiceLineItem = (InvoiceLineItemVO) autogeneratedItems.get(index); if (invoiceLineItem instanceof BaseLineItemVO) { baseLineItem = (BaseLineItemVO) invoiceLineItem; if (baseLineItem != null && (baseLineItem.getTaxable1().booleanValue() || baseLineItem.getTaxable2().booleanValue()) && !canExcludeCoreItem(baseLineItem, customer)) { taxableAmount = taxableAmount.add(baseLineItem.getExtendedPrice()) .setScale(2, BigDecimal.ROUND_HALF_UP); } } } } logger.debug(methodSpec + "taxableAmount = " + taxableAmount); return taxableAmount; } private static boolean canExcludeCoreItem(final BaseLineItemVO baseLineItem, final CustomerVO customer) { final String methodSpec = "[DeliveryFee] InvoiceDeliveryFeeLineBL.canExcludeCoreItems - "; logger.debug(methodSpec); return isAlternateCoreCustomer(customer) && isCoreItem(baseLineItem); } public static BigDecimal calculateInvoiceTaxableSubTotal(final InvoiceVO invoice, final CustomerVO customer) { final String methodSpec = "[DeliveryFee] InvoiceDeliveryFeeLineBL.calculateInvoiceTaxableSubTotal - "; logger.debug(methodSpec); final boolean hasAlternateCoreCustomer = isAlternateCoreCustomer(customer); BigDecimal invoiceTaxableAmount = MathUtil.BD_ZERO; invoiceTaxableAmount = invoiceTaxableAmount.setScale(2, BigDecimal.ROUND_HALF_UP); final Vector invoiceLineItems = invoice.getLineItems(); final int invoiceLineItemsSize = invoiceLineItems.size(); InvoiceLineItemVO invoiceLineItem = null; BaseLineItemVO baseLineItem = null; for (int lineItemIndex = 0; lineItemIndex < invoiceLineItemsSize; lineItemIndex++) { invoiceLineItem = (InvoiceLineItemVO) invoiceLineItems.elementAt(lineItemIndex); if (invoiceLineItem instanceof BaseLineItemVO) { baseLineItem = (BaseLineItemVO) invoiceLineItem; if (hasAlternateCoreCustomer && isCoreItem(baseLineItem)) { continue; } if (baseLineItem.getTaxable1().booleanValue() || baseLineItem.getTaxable2().booleanValue()) { invoiceTaxableAmount = invoiceTaxableAmount.add(baseLineItem.getExtendedPrice()) .setScale(2, BigDecimal.ROUND_HALF_UP); } } } return invoiceTaxableAmount; } public static String getLineAbbrev(final String stateCD) { final String methodSpec = "[DeliveryFee] InvoiceDeliveryFeeLineBL.getLineAbbrev - "; logger.debug(methodSpec); String lineAbbrev = null; if (stateCD.equalsIgnoreCase(RefState.MINNESOTA)) { lineAbbrev = RefInvoiceSpecialFunction.MINNESOTA_DELIVERY_FEE; } else if (stateCD.equalsIgnoreCase(RefState.COLORADO)) { lineAbbrev = RefInvoiceSpecialFunction.COLORADO_DELIVERY_FEE; } return lineAbbrev; } public static int getLineItemType(final String stateCD) { final String methodSpec = "[DeliveryFee] InvoiceDeliveryFeeLineBL.getLineItemType - "; logger.debug(methodSpec); int lineItemType = -1; if (stateCD.equalsIgnoreCase(RefState.MINNESOTA)) { lineItemType = RefInvoiceLineItemType.MINNESOTA_DELIVERY_FEE; } else if (stateCD.equalsIgnoreCase(RefState.COLORADO)) { lineItemType = RefInvoiceLineItemType.COLORADO_DELIVERY_FEE; } return lineItemType; } public static boolean isDeliveryFeeApplicableForInvoice(final InvoiceVO invoiceVO, final StoreProfileVO storeProfile) { final String methodSpec = "[DeliveryFee] InvoiceDeliveryFeeLineBL.isDeliveryFeeApplicableForInvoice - "; logger.debug(methodSpec); boolean isDeliveryFeeApplicableForInvoice = false; if (storeProfile != null && storeProfile.getRetailDeliveryFee() != null) { String stateCD = storeProfile.getRetailDeliveryFee(); if (stateCD.equalsIgnoreCase(RefState.MINNESOTA)) { if ((invoiceVO.getTaxableSales1() != null && invoiceVO.getTaxableSales1().signum() > 0 && invoiceVO.getTaxableSales1().compareTo(new BigDecimal(100)) > 0) || (invoiceVO.getTaxableSales2() != null && invoiceVO.getTaxableSales2().signum() > 0 && invoiceVO .getTaxableSales2().compareTo(new BigDecimal(100)) > 0)) { isDeliveryFeeApplicableForInvoice = true; } } } logger.debug(methodSpec + "isDeliveryFeeApplicableForInvoice = " + isDeliveryFeeApplicableForInvoice); return isDeliveryFeeApplicableForInvoice; } private static boolean isCoreItem(BaseLineItemVO baseLineItem) { final String methodSpec = "[DeliveryFee] InvoiceDeliveryFeeLineBL.isCoreItem - "; logger.debug(methodSpec); return baseLineItem.isCoreItem() || baseLineItem.isCoreTransaction(); } private static boolean isAlternateCoreCustomer(final CustomerVO customer) { final String methodSpec = "[DeliveryFee] InvoiceDeliveryFeeLineBL.isAlternateCoreCustomer - "; logger.debug(methodSpec); return customer != null && customer.getAlternateCoreCustomerID() != null; } }