1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.sys.service.impl;
17
18 import java.util.Calendar;
19 import java.util.HashMap;
20
21 import org.kuali.ole.coa.businessobject.Account;
22 import org.kuali.ole.coa.businessobject.ObjectCode;
23 import org.kuali.ole.coa.businessobject.OffsetDefinition;
24 import org.kuali.ole.coa.service.AccountService;
25 import org.kuali.ole.coa.service.ObjectCodeService;
26 import org.kuali.ole.fp.businessobject.OffsetAccount;
27 import org.kuali.ole.gl.businessobject.FlexibleAccountUpdateable;
28 import org.kuali.ole.sys.OLEConstants;
29 import org.kuali.ole.sys.exception.InvalidFlexibleOffsetException;
30 import org.kuali.ole.sys.service.FlexibleOffsetAccountService;
31 import org.kuali.rice.core.api.datetime.DateTimeService;
32 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
33 import org.kuali.rice.krad.service.BusinessObjectService;
34
35
36
37
38 public class FlexibleOffsetAccountServiceImpl implements FlexibleOffsetAccountService {
39 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(FlexibleOffsetAccountServiceImpl.class);
40
41 private BusinessObjectService businessObjectService;
42 private AccountService accountService;
43 private ObjectCodeService objectCodeService;
44 private DateTimeService dateTimeService;
45 private ParameterService parameterService;
46
47
48
49
50
51
52
53
54
55
56
57
58 public OffsetAccount getByPrimaryIdIfEnabled(String chartOfAccountsCode, String accountNumber, String financialOffsetObjectCode) {
59 LOG.debug("getByPrimaryIdIfEnabled() started");
60
61 if (!getEnabled()) {
62 return null;
63 }
64 HashMap<String,Object> keys = new HashMap();
65 keys.put("chartOfAccountsCode", chartOfAccountsCode);
66 keys.put("accountNumber", accountNumber);
67 keys.put("financialOffsetObjectCode", financialOffsetObjectCode);
68 return (OffsetAccount) businessObjectService.findByPrimaryKey(OffsetAccount.class, keys);
69 }
70
71
72
73
74
75
76
77
78 public boolean getEnabled() {
79 LOG.debug("getEnabled() started");
80 return parameterService.getParameterValueAsBoolean(OffsetDefinition.class, OLEConstants.SystemGroupParameterNames.FLEXIBLE_OFFSET_ENABLED_FLAG);
81 }
82
83
84
85
86
87
88
89
90
91
92 public boolean updateOffset(FlexibleAccountUpdateable transaction) {
93 LOG.debug("setBusinessObjectService() started");
94
95 if (!getEnabled()) {
96 return false;
97 }
98 String keyOfErrorMessage = "";
99
100 Integer fiscalYear = transaction.getUniversityFiscalYear();
101 String chartOfAccountsCode = transaction.getChartOfAccountsCode();
102 String accountNumber = transaction.getAccountNumber();
103
104 String balanceTypeCode = transaction.getFinancialBalanceTypeCode();
105 String documentTypeCode = transaction.getFinancialDocumentTypeCode();
106
107
108
109 OffsetAccount flexibleOffsetAccount = getByPrimaryIdIfEnabled(chartOfAccountsCode, accountNumber, transaction.getFinancialObjectCode());
110 if (flexibleOffsetAccount == null) {
111 return false;
112 }
113
114 String offsetAccountNumber = flexibleOffsetAccount.getFinancialOffsetAccountNumber();
115 String offsetChartOfAccountsCode = flexibleOffsetAccount.getFinancialOffsetChartOfAccountCode();
116
117 Account offsetAccount = accountService.getByPrimaryId(offsetChartOfAccountsCode, offsetAccountNumber);
118 if (offsetAccount == null) {
119 throw new InvalidFlexibleOffsetException("Invalid Flexible Offset Account " + offsetChartOfAccountsCode + "-" + offsetAccountNumber);
120 }
121
122
123 if (!offsetAccount.isActive()) {
124 throw new InvalidFlexibleOffsetException("Closed Flexible Offset Account " + offsetChartOfAccountsCode + "-" + offsetAccountNumber);
125 }
126 if ((offsetAccount.getAccountExpirationDate() != null) && isExpired(offsetAccount, dateTimeService.getCurrentCalendar())) {
127 throw new InvalidFlexibleOffsetException("Expired Flexible Offset Account " + offsetChartOfAccountsCode + "-" + offsetAccountNumber);
128 }
129
130
131 if (!chartOfAccountsCode.equals(offsetChartOfAccountsCode)) {
132 ObjectCode objectCode = objectCodeService.getByPrimaryId(fiscalYear, offsetChartOfAccountsCode, transaction.getFinancialObjectCode());
133 if (objectCode == null) {
134 throw new InvalidFlexibleOffsetException("Invalid Object Code for flexible offset " + fiscalYear + "-" + offsetChartOfAccountsCode + "-" + transaction.getFinancialObjectCode());
135 }
136 }
137
138
139 transaction.setAccount(offsetAccount);
140 transaction.setAccountNumber(offsetAccountNumber);
141 transaction.setChartOfAccountsCode(offsetChartOfAccountsCode);
142
143
144 transaction.setSubAccountNumber(OLEConstants.getDashSubAccountNumber());
145 transaction.setFinancialSubObjectCode(OLEConstants.getDashFinancialSubObjectCode());
146 return true;
147 }
148
149
150
151
152
153
154
155
156
157 protected boolean isExpired(Account account, Calendar runCalendar) {
158
159 Calendar expirationDate = Calendar.getInstance();
160 expirationDate.setTimeInMillis(account.getAccountExpirationDate().getTime());
161
162 int expirationYear = expirationDate.get(Calendar.YEAR);
163 int runYear = runCalendar.get(Calendar.YEAR);
164 int expirationDay = expirationDate.get(Calendar.DAY_OF_YEAR);
165 int runDay = runCalendar.get(Calendar.DAY_OF_YEAR);
166
167 return (expirationYear < runYear) || (expirationYear == runYear && expirationDay < runDay);
168 }
169
170
171
172
173
174 public void setDateTimeService(DateTimeService dateTimeService) {
175 this.dateTimeService = dateTimeService;
176 }
177
178
179
180
181
182 public void setAccountService(AccountService accountService) {
183 this.accountService = accountService;
184 }
185
186
187
188
189
190 public void setObjectCodeService(ObjectCodeService objectCodeService) {
191 this.objectCodeService = objectCodeService;
192 }
193
194
195
196
197
198 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
199 this.businessObjectService = businessObjectService;
200 }
201
202
203
204
205
206 public void setParameterService(ParameterService parameterService) {
207 this.parameterService = parameterService;
208 }
209 }