/*
 * Copyright 2014 The Kuali Foundation.
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.opensource.org/licenses/ecl2.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.kfs.module.ar.document.validation.impl;

import org.apache.commons.lang.StringUtils;
import org.kuali.kfs.module.ar.ArConstants;
import org.kuali.kfs.module.ar.businessobject.ContractsGrantsInvoiceDetail;
import org.kuali.kfs.module.ar.businessobject.InvoiceAccountDetail;
import org.kuali.kfs.module.ar.document.ContractsGrantsInvoiceDocument;
import org.kuali.kfs.module.ar.document.validation.SuspensionCategoryBase;
import org.kuali.rice.core.api.util.type.KualiDecimal;
import org.kuali.rice.krad.util.ObjectUtils;

/**
 * Suspension Category that checks to see if cost category codes are setup correctly. An object code might not
 * be assigned or could be assigned to more than one Cost Category. Check by comparing total current expenditure
 * to the sum of account current expenditure.
 */
public class CostCategoryCodesSuspensionCategory extends SuspensionCategoryBase {

    /**
     * @see org.kuali.kfs.module.ar.document.validation.SuspensionCategory#shouldSuspend(org.kuali.kfs.module.ar.document.ContractsGrantsInvoiceDocument)
     */
    @Override
    public boolean shouldSuspend(ContractsGrantsInvoiceDocument contractsGrantsInvoiceDocument) {
        String billingFrequencyCode = contractsGrantsInvoiceDocument.getAward().getBillingFrequencyCode();

        if (!StringUtils.equals(ArConstants.MILESTONE_BILLING_SCHEDULE_CODE, billingFrequencyCode) &&
                !StringUtils.equals(ArConstants.PREDETERMINED_BILLING_SCHEDULE_CODE, billingFrequencyCode)) {
            ContractsGrantsInvoiceDetail totalCostInvoiceDetail = contractsGrantsInvoiceDocument.getTotalCostInvoiceDetail();
            if (ObjectUtils.isNotNull(totalCostInvoiceDetail)) {
                KualiDecimal categoryCumulativeExpenditure = totalCostInvoiceDetail.getCumulative();
                KualiDecimal accountDetailsCumulativeExpenditure = KualiDecimal.ZERO;

                for (InvoiceAccountDetail invoiceAccountDetail : contractsGrantsInvoiceDocument.getAccountDetails()) {
                    accountDetailsCumulativeExpenditure = accountDetailsCumulativeExpenditure.add(invoiceAccountDetail.getCumulativeAmount());
                }

                return !categoryCumulativeExpenditure.equals(accountDetailsCumulativeExpenditure);
            }
            return true;
        }

        return false;
    }

}

@@ -1106,53 +1106,8 @@
 mal;
-%0Aimport org.kuali.rice.krad.util.ObjectUtils;
 %0A%0A/*
@@ -2197,77 +2197,8 @@
 ();%0A
-            if (ObjectUtils.isNotNull(totalCostInvoiceDetail)) %7B%0A    
     
@@ -2294,36 +2294,32 @@
 ();%0A            
-   
- 
 KualiDecimal acc
@@ -2373,20 +2373,16 @@
 .ZERO;%0A%0A
-    
         
@@ -2504,20 +2504,16 @@
         
-   
- 
 accountD
@@ -2635,35 +2635,27 @@
             
-    %7D%0A%0A    
+%7D%0A%0A
             
@@ -2741,47 +2741,8 @@
 e);%0A
-            %7D%0A            return true;%0A
     

remove unnecessary conditional in suspension category