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.validation.impl;
017
018 import java.math.BigDecimal;
019 import java.util.HashMap;
020 import java.util.Map;
021
022 import org.kuali.kfs.coa.businessobject.ObjectCode;
023 import org.kuali.kfs.coa.service.ObjectTypeService;
024 import org.kuali.kfs.module.ar.ArKeyConstants;
025 import org.kuali.kfs.module.ar.ArPropertyConstants;
026 import org.kuali.kfs.module.ar.businessobject.CustomerInvoiceItemCode;
027 import org.kuali.kfs.module.ar.businessobject.OrganizationOptions;
028 import org.kuali.kfs.sys.context.SpringContext;
029 import org.kuali.kfs.sys.service.UniversityDateService;
030 import org.kuali.rice.kim.util.KimConstants;
031 import org.kuali.rice.kns.document.MaintenanceDocument;
032 import org.kuali.rice.kns.document.authorization.MaintenanceDocumentAuthorizer;
033 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
034 import org.kuali.rice.kns.service.BusinessObjectService;
035 import org.kuali.rice.kns.util.GlobalVariables;
036 import org.kuali.rice.kns.util.KNSConstants;
037 import org.kuali.rice.kns.util.ObjectUtils;
038
039
040 public class CustomerInvoiceItemCodeRule extends MaintenanceDocumentRuleBase {
041
042 protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ArKeyConstants.InvoiceItemCode.class);
043
044 protected CustomerInvoiceItemCode newInvoiceItemCode;
045
046 @Override
047 public void setupConvenienceObjects() {
048 newInvoiceItemCode = (CustomerInvoiceItemCode) super.getNewBo();
049 }
050
051 @Override
052 protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
053
054 boolean success;
055 success = validateItemDefaultPrice(newInvoiceItemCode);
056 success &= validateItemDefaultQuantity(newInvoiceItemCode);
057 success &= validateExistenceOfOrganizationOption(newInvoiceItemCode);
058 success &= isCustomerInvoiceItemCodeObjectValid(newInvoiceItemCode);
059 success &= validateBillingOrg(document);
060
061 return success;
062 }
063
064 @Override
065 protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
066 // always return true even if there are business rule failures.
067 processCustomRouteDocumentBusinessRules(document);
068 return true;
069 }
070
071 public boolean validateBillingOrg(MaintenanceDocument document) {
072 boolean success = true;
073
074 String billingChartCode = newInvoiceItemCode.getChartOfAccountsCode();
075 String billingOrganizationCode = newInvoiceItemCode.getOrganizationCode();
076
077 if (ObjectUtils.isNull(billingChartCode) || ObjectUtils.isNull(billingOrganizationCode))
078 return success;
079
080 // get the documentAuthorizer for this document
081 MaintenanceDocumentAuthorizer documentAuthorizer = (MaintenanceDocumentAuthorizer) getDocumentHelperService().getDocumentAuthorizer(document);
082 success = documentAuthorizer.isAuthorizedByTemplate(document, KNSConstants.KNS_NAMESPACE, KimConstants.PermissionTemplateNames.CREATE_MAINTAIN_RECORDS, GlobalVariables.getUserSession().getPerson().getPrincipalId());
083 if (!success){
084 putFieldError(ArPropertyConstants.CustomerInvoiceItemCodes.CHART_OF_ACCOUNTS_CODE, ArKeyConstants.InvoiceItemCode.ERROR_INVALID_CHART_OF_ACCOUNTS_CODE);
085 putFieldError(ArPropertyConstants.CustomerInvoiceItemCodes.ORGANIZATION_CODE,ArKeyConstants.InvoiceItemCode.ERROR_INVALID_ORGANIZATION_CODE );
086 success = false;
087 }
088 return success;
089 }
090
091 public boolean validateItemDefaultPrice(CustomerInvoiceItemCode customerInvoiceItemCode) {
092
093 boolean validEntry = true;
094 BigDecimal itemDefaultPrice = null;
095 if (customerInvoiceItemCode.getItemDefaultPrice() != null) {
096 itemDefaultPrice = customerInvoiceItemCode.getItemDefaultPrice().bigDecimalValue();
097 }
098
099 if (ObjectUtils.isNotNull(itemDefaultPrice)) {
100 validEntry = itemDefaultPrice.compareTo(BigDecimal.ZERO) == 1;
101 if (!validEntry) {
102 putFieldError("itemDefaultPrice",ArKeyConstants.InvoiceItemCode.NONPOSITIVE_ITEM_DEFAULT_PRICE, "Item Default Price" );
103 }
104 }
105 return validEntry;
106 }
107
108 public boolean validateItemDefaultQuantity(CustomerInvoiceItemCode customerInvoiceItemCode) {
109
110 boolean validEntry = true;
111 BigDecimal itemDefaultQuantity = customerInvoiceItemCode.getItemDefaultQuantity();
112
113 if (ObjectUtils.isNotNull(itemDefaultQuantity)) {
114 if (itemDefaultQuantity.floatValue() <= 0) {
115 putFieldError("itemDefaultQuantity",ArKeyConstants.InvoiceItemCode.NONPOSITIVE_ITEM_DEFAULT_QUANTITY, "Item Default Quantity" );
116 validEntry = false;
117 }
118 }
119 return validEntry;
120 }
121
122 /**
123 * This method returns true of organization option row exists with the same chart of accounts code and organization code
124 * as the customer invoice item code
125 *
126 * @param customerInvoiceItemCode
127 * @return
128 */
129 public boolean validateExistenceOfOrganizationOption(CustomerInvoiceItemCode customerInvoiceItemCode) {
130
131 boolean isValid = true;
132
133 Map<String, String> criteria = new HashMap<String, String>();
134 criteria.put("chartOfAccountsCode", customerInvoiceItemCode.getChartOfAccountsCode());
135 criteria.put("organizationCode", customerInvoiceItemCode.getOrganizationCode());
136
137 BusinessObjectService businessObjectService = SpringContext.getBean(BusinessObjectService.class);
138 if( businessObjectService.countMatching(OrganizationOptions.class, criteria) == 0) {
139 putFieldError("organizationCode",ArKeyConstants.InvoiceItemCode.ORG_OPTIONS_DOES_NOT_EXIST_FOR_CHART_AND_ORG, new String[]{customerInvoiceItemCode.getChartOfAccountsCode(),customerInvoiceItemCode.getOrganizationCode()});
140 isValid = false;
141 }
142
143 return isValid;
144 }
145
146 /**
147 *
148 * This method checks to see if the customer invoice item object code is of type Income
149 *
150 * @return true if it is an income object
151 */
152 protected boolean isCustomerInvoiceItemCodeObjectValid(CustomerInvoiceItemCode customerInvoiceItemCode) {
153 boolean success = true;
154
155 Integer universityFiscalYear = SpringContext.getBean(UniversityDateService.class).getCurrentFiscalYear();
156 ObjectCode defaultInvoiceItemCodeObject = customerInvoiceItemCode.getDefaultInvoiceFinancialObject();
157
158 if (ObjectUtils.isNotNull(universityFiscalYear) && ObjectUtils.isNotNull(defaultInvoiceItemCodeObject)) {
159 ObjectTypeService objectTypeService = SpringContext.getBean(ObjectTypeService.class);
160 success = objectTypeService.getBasicIncomeObjectTypes(universityFiscalYear).contains(defaultInvoiceItemCodeObject.getFinancialObjectTypeCode());
161
162 if (!success) {
163 // RE-USE this OrgAccDef error because it applies here
164 putFieldError("defaultInvoiceFinancialObjectCode", ArKeyConstants.OrganizationAccountingDefaultErrors.DEFAULT_INVOICE_FINANCIAL_OBJECT_CODE_INVALID, defaultInvoiceItemCodeObject.getCode());
165 }
166 }
167
168 return success;
169 }
170 }