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.purap.businessobject;
017    
018    import java.math.BigDecimal;
019    
020    import org.apache.commons.lang.StringUtils;
021    import org.kuali.kfs.integration.cab.CapitalAssetBuilderAssetTransactionType;
022    import org.kuali.kfs.module.purap.PurapPropertyConstants;
023    import org.kuali.kfs.module.purap.util.PurApItemUtils;
024    import org.kuali.kfs.sys.context.SpringContext;
025    import org.kuali.rice.kns.service.KualiModuleService;
026    import org.kuali.rice.kns.util.KualiDecimal;
027    import org.kuali.rice.kns.util.ObjectUtils;
028    
029    /**
030     * Base class for Accounts Payable Item Business Objects.
031     */
032    public abstract class AccountsPayableItemBase extends PurApItemBase implements AccountsPayableItem {
033        private KualiDecimal extendedPrice;
034        private String capitalAssetTransactionTypeCode;
035        private CapitalAssetBuilderAssetTransactionType capitalAssetTransactionType;
036        
037        /**
038         * Method defaults to {@link #isConsideredEnteredWithZero()}
039         * 
040         * @see org.kuali.module.purap.bo.PurchasingApItem#isConsideredEntered()
041         */
042        public boolean isConsideredEntered() {
043            return isConsideredEnteredWithZero();
044        }
045    
046        public boolean isEligibleDisplay() {
047            return isConsideredEnteredWithZero();
048        }
049    
050        public boolean isConsideredEnteredWithZero() {
051            return isConsideredEntered(true);
052        }
053    
054        public boolean isConsideredEnteredWithoutZero() {
055            return isConsideredEntered(false);
056        }
057    
058        /**
059         * This method is used to determine whether an item has been entered that is we are satisfied there's enough info to continue
060         * processing that particular item. It is currently used by the rules class to determine when it's necessary to run rules on
061         * items (so that lines processors don't touch won't be validated) and to determine when to show items (in combination with the
062         * full entry mode)
063         * 
064         * @param allowsZero if this is true zero will be considered the same as null.
065         * @return true if the item is considered entered false otherwise
066         */
067        private boolean isConsideredEntered(boolean allowsZero) {
068            if (getItemType().isLineItemIndicator()) {
069                if ((getItemType().isQuantityBasedGeneralLedgerIndicator())) {
070                    if ((ObjectUtils.isNull(getItemQuantity())) && (ObjectUtils.isNull(getExtendedPrice()) || (allowsZero && getExtendedPrice().isZero()))) {
071                        return false;
072                    }
073                }
074                else {
075                    if (ObjectUtils.isNull(getExtendedPrice()) || (allowsZero && getExtendedPrice().isZero())) {
076                        return false;
077                    }
078                }
079            }
080            else {
081                if ((ObjectUtils.isNull(getItemUnitPrice()) || (allowsZero && this.getItemUnitPrice().compareTo(new BigDecimal(0)) == 0)) && (StringUtils.isBlank(getItemDescription()))) {
082                    return false;
083                }
084            }
085    
086            return true;
087        }
088    
089        public boolean isNonZeroAmount() {
090            return PurApItemUtils.isNonZeroExtended(this);
091        }
092    
093        /**
094         * Gets the extendedPrice attribute. this override is necessary because extended price needs to be set based on the unit price
095         * for below the line(without this it would always be empty)
096         * 
097         * @return Returns the extendedPrice.
098         */
099        @Override
100        public KualiDecimal getExtendedPrice() {
101            if (ObjectUtils.isNotNull(this.getItemUnitPrice()) && this.getItemType().isAmountBasedGeneralLedgerIndicator()) {
102                if (ObjectUtils.isNotNull(this.getItemUnitPrice())) {
103                    extendedPrice = new KualiDecimal(this.getItemUnitPrice().toString());
104                }else{
105                    extendedPrice = null;
106                }
107            }else if (ObjectUtils.isNull(this.getItemUnitPrice()) && 
108                      this.getItemType().isAmountBasedGeneralLedgerIndicator() &&
109                      this.getItemType().isAdditionalChargeIndicator()){ // This additional charges check is needed since non qty items also dont have unit price
110                // extendedPrice should be null if the unit price is null
111                extendedPrice = null;
112            }
113            return extendedPrice;
114        }
115        
116        public void setExtendedPrice(KualiDecimal extendedPrice) {
117            this.extendedPrice = extendedPrice;
118        }
119        
120        /**
121         * Override the method in PurApItemBase so that if the item is
122         * not eligible to be displayed in the account summary tab,
123         * which is if the item's extended price is null or zero, 
124         * we'll return null and the item won't be added
125         * to the list of account summary.
126         * 
127         * @see org.kuali.kfs.module.purap.businessobject.PurApItemBase#getSummaryItem()
128         */
129        @Override
130        public PurApSummaryItem getSummaryItem() {
131            if (extendedPrice == null || extendedPrice.intValue() == 0) {
132                return null;
133            }
134            else {
135                return super.getSummaryItem();
136            }
137        }
138    
139        public String getCapitalAssetTransactionTypeCode() {
140            return capitalAssetTransactionTypeCode;
141        }
142    
143        public void setCapitalAssetTransactionTypeCode(String capitalAssetTransactionTypeCode) {
144            this.capitalAssetTransactionTypeCode = capitalAssetTransactionTypeCode;
145        }
146    
147        public CapitalAssetBuilderAssetTransactionType getCapitalAssetTransactionType() {
148            return capitalAssetTransactionType = (CapitalAssetBuilderAssetTransactionType) SpringContext.getBean(KualiModuleService.class).getResponsibleModuleService(CapitalAssetBuilderAssetTransactionType.class).retrieveExternalizableBusinessObjectIfNecessary(this, capitalAssetTransactionType, PurapPropertyConstants.ITEM_CAPITAL_ASSET_TRANSACTION_TYPE);
149        }
150        
151        public void setItemDescription(String itemDescription) {
152            if((itemDescription != null) && (itemDescription.length() > 100))
153            {
154               super.setItemDescription(itemDescription.substring(0, 100));
155            } else
156            {
157                super.setItemDescription(itemDescription);
158            }
159            
160        }
161    
162    }