1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.tklm.leave.accrual.bucket;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.joda.time.DateTime;
22 import org.joda.time.LocalDate;
23 import org.kuali.kpme.core.accrualcategory.AccrualCategory;
24 import org.kuali.kpme.core.earncode.EarnCode;
25 import org.kuali.kpme.core.principal.PrincipalHRAttributes;
26 import org.kuali.kpme.core.service.HrServiceLocator;
27 import org.kuali.kpme.core.util.HrConstants;
28 import org.kuali.kpme.tklm.api.leave.accrual.bucket.AvailableLeaveBalanceContract;
29 import org.kuali.kpme.tklm.leave.accrual.bucket.exception.MaximumBalanceException;
30 import org.kuali.kpme.tklm.leave.accrual.bucket.exception.NegativeBalanceException;
31 import org.kuali.kpme.tklm.leave.accrual.bucket.exception.UsageLimitException;
32 import org.kuali.kpme.tklm.leave.block.LeaveBlock;
33
34 public class AvailableLeaveBalance extends LeaveBalance implements AvailableLeaveBalanceContract {
35
36 private List<LeaveBlock> leaveBlocks;
37
38 private PendingLeaveBalance pendingBalance;
39 private YearToDateUsageLeaveBalance ytdUsage;
40
41 public AvailableLeaveBalance(AccrualCategory accrualCategory, PrincipalHRAttributes principalCalendar) {
42 super(accrualCategory, principalCalendar);
43 asOfDate = LocalDate.now();
44 pendingBalance = new PendingLeaveBalance(accrualCategory,principalCalendar);
45 ytdUsage = new YearToDateUsageLeaveBalance(accrualCategory,principalCalendar);
46 leaveBlocks = new ArrayList<LeaveBlock>();
47 }
48
49 private final static String BALANCE_TYPE = "AVAILABLE_BALANCE";
50
51 @Override
52 public void add(LeaveBlock leaveBlock) throws NegativeBalanceException {
53
54 DateTime rolloverDate = HrServiceLocator.getLeavePlanService().getFirstDayOfLeavePlan(principalCalendar.getLeavePlan(), asOfDate);
55
56 if((leaveBlock.getLeaveDate().compareTo(asOfDate.toDate()) <= 0)
57 || leaveBlock.getLeaveAmount().signum() < 0 ) {
58 try {
59
60 ytdUsage.add(leaveBlock);
61
62 EarnCode earnCode = HrServiceLocator.getEarnCodeService().getEarnCode(leaveBlock.getEarnCode(), LocalDate.fromDateFields(leaveBlock.getLeaveDate()));
63 if(earnCode != null) {
64 if(earnCode.getAccrualBalanceAction().equals(HrConstants.ACCRUAL_BALANCE_ACTION.USAGE)){
65
66 add(leaveBlock.getLeaveAmount());
67 leaveBlocks.add(leaveBlock);
68 }
69 else if(earnCode.getAccrualBalanceAction().equals(HrConstants.ACCRUAL_BALANCE_ACTION.ADJUSTMENT)) {
70
71 add(leaveBlock.getLeaveAmount());
72 leaveBlocks.add(leaveBlock);
73 }
74 else if(earnCode.getAccrualBalanceAction().equals(HrConstants.ACCRUAL_BALANCE_ACTION.NONE)) {
75
76 leaveBlocks.add(leaveBlock);
77 }
78
79 try {
80
81
82 pendingBalance.add(leaveBlock);
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 } catch (MaximumBalanceException e) {
108 e.printStackTrace();
109 } catch (NegativeBalanceException e) {
110 e.printStackTrace();
111 }
112 }
113 } catch (UsageLimitException e) {
114 e.printStackTrace();
115 }
116 }
117 }
118
119 @Override
120 public void remove(LeaveBlock leaveBlock) {
121
122 if((leaveBlock.getLeaveDate().compareTo(asOfDate.toDate()) <= 0)
123 || leaveBlock.getLeaveAmount().signum() < 0 ) {
124 try {
125
126 ytdUsage.remove(leaveBlock);
127
128 EarnCode earnCode = HrServiceLocator.getEarnCodeService().getEarnCode(leaveBlock.getEarnCode(), LocalDate.fromDateFields(leaveBlock.getLeaveDate()));
129 if(earnCode != null) {
130 if(earnCode.getAccrualBalanceAction().equals(HrConstants.ACCRUAL_BALANCE_ACTION.USAGE)){
131
132 remove(leaveBlock.getLeaveAmount());
133 leaveBlocks.remove(leaveBlock);
134 }
135 else if(earnCode.getAccrualBalanceAction().equals(HrConstants.ACCRUAL_BALANCE_ACTION.ADJUSTMENT)) {
136
137 remove(leaveBlock.getLeaveAmount());
138 leaveBlocks.remove(leaveBlock);
139 }
140 else if(earnCode.getAccrualBalanceAction().equals(HrConstants.ACCRUAL_BALANCE_ACTION.NONE)) {
141
142 leaveBlocks.remove(leaveBlock);
143 }
144
145 try {
146
147
148 pendingBalance.remove(leaveBlock);
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173 } catch (MaximumBalanceException e) {
174 e.printStackTrace();
175 } catch (NegativeBalanceException e) {
176 e.printStackTrace();
177 }
178 }
179 } catch (UsageLimitException e) {
180 e.printStackTrace();
181 } catch (NegativeBalanceException e1) {
182 e1.printStackTrace();
183 }
184 }
185 }
186
187 @Override
188 public String getBalanceType() {
189 return BALANCE_TYPE;
190 }
191
192 @Override
193 public void adjust(LeaveBlock leaveBlock) throws UsageLimitException,
194 MaximumBalanceException, NegativeBalanceException {
195
196
197 }
198
199 @Override
200 public void clear() {
201
202 pendingBalance.clear();
203 ytdUsage.clear();
204 leaveBlocks.clear();
205 super.clear();
206 }
207
208 }