001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.module.ar.document.service.impl;
017    
018    import java.util.HashMap;
019    import java.util.Map;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.kuali.kfs.module.ar.ArConstants;
023    import org.kuali.kfs.module.ar.businessobject.CustomerAddress;
024    import org.kuali.kfs.module.ar.businessobject.CustomerInvoiceDetail;
025    import org.kuali.kfs.module.ar.businessobject.CustomerInvoiceItemCode;
026    import org.kuali.kfs.module.ar.businessobject.OrganizationOptions;
027    import org.kuali.kfs.module.ar.document.CustomerInvoiceDocument;
028    import org.kuali.kfs.module.ar.document.service.AccountsReceivableTaxService;
029    import org.kuali.kfs.module.ar.document.service.CustomerAddressService;
030    import org.kuali.kfs.module.ar.document.service.CustomerInvoiceDetailService;
031    import org.kuali.kfs.sys.context.SpringContext;
032    import org.kuali.kfs.sys.service.impl.KfsParameterConstants;
033    import org.kuali.rice.kns.document.Document;
034    import org.kuali.rice.kns.service.BusinessObjectService;
035    import org.kuali.rice.kns.service.ParameterService;
036    import org.kuali.rice.kns.util.ObjectUtils;
037    
038    public class AccountsReceivableTaxServiceImpl implements AccountsReceivableTaxService {
039        
040        private ParameterService parameterService;
041        private BusinessObjectService businessObjectService;
042        
043        /**
044         * @see org.kuali.kfs.module.ar.document.service.AccountsReceivableTaxService#isCustomerInvoiceDetailTaxable(org.kuali.kfs.module.ar.document.CustomerInvoiceDocument, org.kuali.kfs.module.ar.businessobject.CustomerInvoiceDetail)
045         */
046        public boolean isCustomerInvoiceDetailTaxable(CustomerInvoiceDocument customerInvoiceDocument, CustomerInvoiceDetail customerInvoiceDetail) {
047    
048            //check if sales tax is enabled
049            if( !parameterService.getIndicatorParameter(KfsParameterConstants.ACCOUNTS_RECEIVABLE_DOCUMENT.class, ArConstants.ENABLE_SALES_TAX_IND) ){
050                return false;
051            }
052    
053            //check if customer is tax exempt
054            if( ObjectUtils.isNotNull(customerInvoiceDocument.getCustomer() ) ){
055                if( customerInvoiceDocument.getCustomer().isCustomerTaxExemptIndicator()){
056                    return false;
057                }
058            }
059            
060            //check item if the taxable indicator is checked
061            if (!customerInvoiceDetail.isTaxableIndicator()) {
062                return false;
063            }
064            
065            //check if the shipping address has Postal Code in the same country and state as the Billing Org. If not, the item is not taxable.
066            if (ObjectUtils.isNotNull(customerInvoiceDocument.getShippingZipCode()) && 
067                StringUtils.equals(customerInvoiceDocument.getShippingCountryCode(), customerInvoiceDocument.getBillingCountryCode()) &&
068                !StringUtils.equals(customerInvoiceDocument.getShippingStateCode(), customerInvoiceDocument.getBillingStateCode())) {               
069                return false;
070            }
071            
072            return true;
073        }
074        
075        /**
076         * @see org.kuali.kfs.module.ar.document.service.AccountsReceivableTaxService#getPostalCodeForTaxation(org.kuali.kfs.module.ar.document.CustomerInvoiceDocument)
077         */
078        public String getPostalCodeForTaxation(CustomerInvoiceDocument document) {
079            
080            String postalCode = null;
081            String customerNumber = document.getAccountsReceivableDocumentHeader().getCustomerNumber();
082            Integer shipToAddressIdentifier = document.getCustomerShipToAddressIdentifier();
083            
084            //if customer number or ship to address id isn't provided, go to org options
085            if (ObjectUtils.isNotNull(shipToAddressIdentifier) && StringUtils.isNotEmpty(customerNumber) ) {
086                
087                CustomerAddressService customerAddressService = SpringContext.getBean(CustomerAddressService.class);
088                CustomerAddress customerShipToAddress = customerAddressService.getByPrimaryKey(customerNumber, shipToAddressIdentifier);
089                if( ObjectUtils.isNotNull(customerShipToAddress) ){
090                    postalCode = customerShipToAddress.getCustomerZipCode();
091                }
092            }
093            else {
094                Map<String, String> criteria = new HashMap<String, String>();
095                criteria.put("chartOfAccountsCode", document.getBillByChartOfAccountCode());
096                criteria.put("organizationCode", document.getBilledByOrganizationCode());
097                OrganizationOptions organizationOptions = (OrganizationOptions) businessObjectService.findByPrimaryKey(OrganizationOptions.class, criteria);
098    
099                if (ObjectUtils.isNotNull(organizationOptions)) {
100                    postalCode = organizationOptions.getOrganizationPostalZipCode();
101                }
102    
103               
104            }
105            return postalCode;
106        }        
107    
108        public ParameterService getParameterService() {
109            return parameterService;
110        }
111    
112        public void setParameterService(ParameterService parameterService) {
113            this.parameterService = parameterService;
114        }
115    
116        public BusinessObjectService getBusinessObjectService() {
117            return businessObjectService;
118        }
119    
120        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
121            this.businessObjectService = businessObjectService;
122        }
123    }