1 package org.kuali.ole.patron.bill.controller;
2
3 import org.apache.log4j.Logger;
4 import org.kuali.ole.OLEConstants;
5 import org.kuali.ole.patron.bill.*;
6 import org.kuali.rice.krad.service.BusinessObjectService;
7 import org.kuali.rice.krad.service.KRADServiceLocator;
8 import org.kuali.rice.krad.util.GlobalVariables;
9 import org.kuali.rice.krad.util.KRADConstants;
10 import org.kuali.rice.krad.web.controller.UifControllerBase;
11 import org.kuali.rice.krad.web.form.UifFormBase;
12 import org.springframework.stereotype.Controller;
13 import org.springframework.validation.BindingResult;
14 import org.springframework.web.bind.annotation.ModelAttribute;
15 import org.springframework.web.bind.annotation.RequestMapping;
16 import org.springframework.web.servlet.ModelAndView;
17 import org.kuali.ole.patron.bill.form.PatronBillForm;
18 import javax.servlet.http.HttpServletRequest;
19 import javax.servlet.http.HttpServletResponse;
20 import java.math.BigDecimal;
21 import java.util.*;
22
23
24
25
26
27
28
29
30
31 @Controller
32 @RequestMapping(value = "/patronbill")
33 public class PatronBillController extends UifControllerBase {
34
35 private static final Logger LOG = Logger.getLogger(PatronBillController.class);
36
37
38 private BusinessObjectService businessObjectService;
39
40
41
42
43
44 private BusinessObjectService getBusinessObjectService() {
45 if (null == businessObjectService) {
46 businessObjectService = KRADServiceLocator.getBusinessObjectService();
47 }
48 return businessObjectService;
49 }
50
51
52
53
54
55
56 @Override
57 protected PatronBillForm createInitialForm(HttpServletRequest request) {
58 return new PatronBillForm();
59 }
60
61
62
63
64
65
66
67
68
69
70
71 @Override
72 @RequestMapping(params = "methodToCall=start")
73 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
74 HttpServletRequest request, HttpServletResponse response) {
75 PatronBillForm patronBillForm = (PatronBillForm) form;
76 PatronBillHelperService patronBillHelperService = new PatronBillHelperService();
77 patronBillForm.setPatronBillList(patronBillHelperService.getService(patronBillForm.getPatronId()));
78 patronBillForm.setPaymentAmount(null);
79 patronBillForm.setPaymentMethod("");
80 return super.start(patronBillForm, result, request, response);
81 }
82
83
84
85
86
87
88
89
90
91 @RequestMapping(params = "methodToCall=accept")
92 public ModelAndView acceptPayment(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
93 HttpServletRequest request, HttpServletResponse response) {
94 LOG.debug("Initialized acceptPayment Method");
95 String operatorId= GlobalVariables.getUserSession().getLoggedInUserPrincipalName();
96 PatronBillForm patronBillForm = (PatronBillForm) form;
97 List<PatronBill> patronBillList = patronBillForm.getPatronBillList();
98 Iterator itr = patronBillList.iterator();
99 PatronBill patronBill;
100 int count = 0;
101 String billNumber="";
102 String id="";
103 BigDecimal unPaidAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
104 BigDecimal feeAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
105 BigDecimal totalAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
106 BigDecimal paidAmount = patronBillForm.getPaymentAmount() != null ? patronBillForm.getPaymentAmount() : new BigDecimal(0.0);
107 if(patronBillForm.getPaymentAmount()==null){
108 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.PAY_AMT_EMPTY );
109 return getUIFModelAndView(patronBillForm);
110 }
111 for(int i=0;i<patronBillList.size();i++){
112 totalAmount=totalAmount.add(patronBillList.get(i).getFeeAmount());
113 }
114 while(itr.hasNext()) {
115 patronBill = (PatronBill)itr.next();
116 boolean isActive = patronBill.isActive();
117 if(isActive){
118 count +=1;
119 id= patronBill.getId();
120 billNumber = patronBill.getBillNumber();
121 BigDecimal billPaidAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
122 if(paidAmount.doubleValue() >= totalAmount.doubleValue()){
123 PatronBillHelperService patronBillHelperService = new PatronBillHelperService();
124 feeAmount = patronBill.getFeeAmount();
125 Map billNoMap = new HashMap();
126 billNoMap.put("billNumber",billNumber);
127 List<PatronBillPayment> patronBillPaymentList = (List<PatronBillPayment>)KRADServiceLocator.getBusinessObjectService().findMatching(PatronBillPayment.class,billNoMap);
128 BigDecimal billTotalAmount = patronBillPaymentList.get(0).getTotalAmount();
129 if(paidAmount.doubleValue() >= totalAmount.doubleValue()){
130 unPaidAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
131 billPaidAmount = billPaidAmount.add(feeAmount);
132 }else{
133 unPaidAmount = billTotalAmount.subtract(paidAmount);
134 billPaidAmount = paidAmount.add(paidAmount);
135 }
136 billTotalAmount = billTotalAmount.subtract(feeAmount);
137
138 String paymentStatus ="";
139 String paymentMethod = patronBillForm.getPaymentMethod().equals("") ? patronBill.getPaymentMethod() : patronBillForm.getPaymentMethod();
140 feeAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
141 if(billTotalAmount.doubleValue() ==0)
142 {
143 paymentStatus = getPaymentStatus(OLEConstants.FULL_PAID);
144 } else{
145 paymentStatus = getPaymentStatus(OLEConstants.PAR_PAID);
146 }
147 patronBillHelperService.updatePatronFeeAmount(id,feeAmount);
148 patronBillHelperService.updatePatronBillForAccept(billNumber,billPaidAmount,unPaidAmount,billTotalAmount,paymentStatus,paymentMethod,operatorId);
149 BigDecimal balanceAmount = paidAmount.subtract(totalAmount);
150 patronBillForm.setMessage(OLEConstants.BAL_AMT+balanceAmount);
151 }else{
152 Map billNoMap = new HashMap();
153 billNoMap.put("billNumber",billNumber);
154 List<PatronBillPayment> patronBillPaymentList = (List<PatronBillPayment>)KRADServiceLocator.getBusinessObjectService().findMatching(PatronBillPayment.class,billNoMap);
155 BigDecimal billTotalAmount = patronBillPaymentList.get(0).getTotalAmount();
156 feeAmount = patronBill.getFeeAmount();
157 if(paidAmount.doubleValue()>feeAmount.doubleValue()){
158 paidAmount = paidAmount.subtract(feeAmount);
159 billTotalAmount = billTotalAmount.subtract(feeAmount);
160 billPaidAmount = billPaidAmount.add(feeAmount);
161 unPaidAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
162 feeAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
163 }else{
164 feeAmount = feeAmount.subtract(paidAmount);
165 unPaidAmount = billTotalAmount.subtract(paidAmount);
166 billTotalAmount = billTotalAmount.subtract(paidAmount);
167 billPaidAmount = paidAmount;
168 }
169 PatronBillHelperService patronBillHelperService = new PatronBillHelperService();
170
171 String paymentStatus ="";
172 if(billTotalAmount.doubleValue() ==0)
173 {
174 paymentStatus = getPaymentStatus(OLEConstants.FULL_PAID);
175 } else{
176 paymentStatus = getPaymentStatus(OLEConstants.PAR_PAID);
177 }
178 String paymentMethod = patronBillForm.getPaymentMethod().equals("") ? patronBill.getPaymentMethod() : patronBillForm.getPaymentMethod();
179 patronBillHelperService.updatePatronFeeAmount(id,feeAmount);
180 patronBillHelperService.updatePatronBillForAccept(billNumber,billPaidAmount,unPaidAmount,billTotalAmount,paymentStatus,paymentMethod,operatorId);
181 }
182 }
183 }
184 if(count ==0){
185 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.SELECT );
186 }
187 return start(patronBillForm, result, request, response);
188 }
189
190 private String getPaymentStatus(String paymentStatus){
191 LOG.debug("Inside the getPaymentStatus method");
192 Map statusMap = new HashMap();
193 statusMap.put("paymentStatusName", paymentStatus);
194 List<OlePaymentStatus> olePaymentStatusList = (List<OlePaymentStatus>)getBusinessObjectService().findMatching(OlePaymentStatus.class,statusMap);
195 return olePaymentStatusList!=null && olePaymentStatusList.size()>0 ? olePaymentStatusList.get(0).getPaymentStatusId(): null;
196 }
197
198
199
200
201
202
203
204
205
206 @RequestMapping(params = "methodToCall=forgive")
207 public ModelAndView forgivePayment(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
208 HttpServletRequest request, HttpServletResponse response) {
209 LOG.debug("Initialized forgivePayment Method");
210 String operatorId= GlobalVariables.getUserSession().getLoggedInUserPrincipalName();
211 PatronBillForm patronBillForm = (PatronBillForm) form;
212 List<PatronBill> patronBillList = patronBillForm.getPatronBillList();
213 Iterator itr = patronBillList.iterator();
214 PatronBill patronBill;
215 int count = 0;
216 String billNumber="";
217 String id="";
218 BigDecimal unPaidAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
219 BigDecimal feeAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
220 BigDecimal totalAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
221 BigDecimal paidAmount = patronBillForm.getPaymentAmount() != null ? patronBillForm.getPaymentAmount() : new BigDecimal(0.0);
222 if(patronBillForm.getPaymentAmount()==null){
223 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.PAY_AMT_EMPTY );
224 return getUIFModelAndView(patronBillForm);
225 }
226 for(int i=0;i<patronBillList.size();i++){
227 totalAmount=totalAmount.add(patronBillList.get(i).getFeeAmount());
228 }
229 while(itr.hasNext()) {
230 patronBill = (PatronBill)itr.next();
231 boolean isActive = patronBill.isActive();
232 if(isActive){
233 count +=1;
234 id= patronBill.getId();
235 billNumber = patronBill.getBillNumber();
236 if(patronBillForm.getPaymentAmount().doubleValue() > totalAmount.doubleValue()){
237 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.PAID_AMT_EXC);
238 }
239 BigDecimal billPaidAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
240 if(paidAmount.doubleValue() >= totalAmount.doubleValue()){
241 PatronBillHelperService patronBillHelperService = new PatronBillHelperService();
242 feeAmount = patronBill.getFeeAmount();
243 Map billNoMap = new HashMap();
244 billNoMap.put("billNumber",billNumber);
245 List<PatronBillPayment> patronBillPaymentList = (List<PatronBillPayment>)KRADServiceLocator.getBusinessObjectService().findMatching(PatronBillPayment.class,billNoMap);
246 BigDecimal billTotalAmount = patronBillPaymentList.get(0).getTotalAmount();
247 if(paidAmount.doubleValue() >= totalAmount.doubleValue()){
248 unPaidAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
249 billPaidAmount = billPaidAmount.add(feeAmount);
250 }else{
251 unPaidAmount = billTotalAmount.subtract(paidAmount);
252 billPaidAmount = paidAmount.add(paidAmount);
253 }
254 billTotalAmount = billTotalAmount.subtract(feeAmount);
255
256 String paymentStatus ="";
257 if(billTotalAmount.doubleValue() ==0)
258 {
259 paymentStatus = getPaymentStatus(OLEConstants.FULL_PAID);
260 } else{
261 paymentStatus = getPaymentStatus(OLEConstants.PAR_PAID);
262 }
263 String paymentMethod = patronBillForm.getPaymentMethod().equals("") ? patronBill.getPaymentMethod() : patronBillForm.getPaymentMethod();
264 feeAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
265 patronBillHelperService.updatePatronFeeAmount(id,feeAmount);
266 patronBillHelperService.updatePatronBillForAccept(billNumber,billPaidAmount,unPaidAmount,billTotalAmount,paymentStatus,paymentMethod,operatorId);
267 }else{
268 Map billNoMap = new HashMap();
269 billNoMap.put("billNumber",billNumber);
270 List<PatronBillPayment> patronBillPaymentList = (List<PatronBillPayment>)KRADServiceLocator.getBusinessObjectService().findMatching(PatronBillPayment.class,billNoMap);
271 BigDecimal billTotalAmount = patronBillPaymentList.get(0).getTotalAmount();
272 feeAmount = patronBill.getFeeAmount();
273 if(paidAmount.doubleValue()>feeAmount.doubleValue()){
274 paidAmount = paidAmount.subtract(feeAmount);
275 billTotalAmount = billTotalAmount.subtract(feeAmount);
276 unPaidAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
277 feeAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
278 }else{
279 feeAmount = feeAmount.subtract(paidAmount);
280 unPaidAmount = billTotalAmount.subtract(paidAmount);
281 billTotalAmount = billTotalAmount.subtract(paidAmount);
282 billPaidAmount = paidAmount;
283 }
284 PatronBillHelperService patronBillHelperService = new PatronBillHelperService();
285
286 String paymentStatus ="";
287 if(billTotalAmount.doubleValue() ==0)
288 {
289 paymentStatus = getPaymentStatus(OLEConstants.FULL_PAID);
290 } else{
291 paymentStatus = getPaymentStatus(OLEConstants.PAR_PAID);
292 }
293 patronBillHelperService.updatePatronFeeAmount(id,feeAmount);
294 patronBillHelperService.updatePatronBillForForgive(billNumber,unPaidAmount,billTotalAmount,paymentStatus,operatorId);
295 }
296 }
297 }
298 if(count ==0){
299 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.SELECT );
300 }
301 return start(patronBillForm, result, request, response);
302 }
303
304
305
306
307
308
309
310
311
312 @RequestMapping(params = "methodToCall=print")
313 public ModelAndView printBill(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,HttpServletRequest request, HttpServletResponse response) throws Exception {
314 LOG.debug("Initialized printBill Method");
315 PatronBillForm patronBillForm = (PatronBillForm) form;
316 PatronBillPayment patronBillPayment = new PatronBillPayment();
317 List<PatronBill> patronBillList = patronBillForm.getPatronBillList();
318 Iterator itr = patronBillList.iterator();
319 PatronBill patronBill;
320 int count = 0;
321 String billNumber="";
322 while(itr.hasNext()) {
323 patronBill = (PatronBill)itr.next();
324 boolean isPrint = patronBill.isActive();
325 if(isPrint){
326 count += 1;
327 if(patronBill.isReviewed()) {
328 billNumber = patronBill.getBillNumber();
329 Map billNumberMap = new HashMap();
330 billNumberMap.put("billNumber", billNumber);
331 List<PatronBillPayment> patronBillPaymentList = (List<PatronBillPayment>) KRADServiceLocator.getBusinessObjectService().findMatching(PatronBillPayment.class,billNumberMap);
332 List<FeeType> feeTypeList = (List<FeeType>) KRADServiceLocator.getBusinessObjectService().findMatching(FeeType.class, billNumberMap);
333 for(int i=0;i< patronBillPaymentList.size();i++){
334 billNumber = patronBillPaymentList.get(i).getBillNumber();
335 String firstName = patronBillPaymentList.get(i).getFirstName();
336 String lastName = patronBillPaymentList.get(i).getLastName();
337 BigDecimal totalAmount = patronBillPaymentList.get(i).getTotalAmount();
338 }
339 PrintBill printBill = new PrintBill();
340 printBill.generatePdf(patronBillPaymentList,feeTypeList,response);
341 } else{
342 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.NOT_REVIEWED );
343
344 }
345 }
346 }
347 if(count ==0){
348 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.SELECT );
349 }
350 return getUIFModelAndView(patronBillForm,"BillViewPage");
351 }
352
353
354
355
356 @Override
357 @RequestMapping(params = "methodToCall=close")
358 public ModelAndView close(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
359 HttpServletRequest request, HttpServletResponse response) {
360 return super.close(form, result, request, response);
361 }
362
363 }