View Javadoc

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   * Created with IntelliJ IDEA.
25   * User: ?
26   * Date: 9/7/12
27   * Time: 11:32 AM
28   * To change this template use File | Settings | File Templates.
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       *  Gets the businessObjectService attribute.
42       * @return  Returns the businessObjectService
43       */
44      private BusinessObjectService getBusinessObjectService() {
45          if (null == businessObjectService) {
46              businessObjectService = KRADServiceLocator.getBusinessObjectService();
47          }
48          return businessObjectService;
49      }
50  
51      /**
52       *  This method creates new PatronBill form
53       * @param request
54       * @return  PatronBillForm
55       */
56      @Override
57      protected PatronBillForm createInitialForm(HttpServletRequest request) {
58          return new PatronBillForm();
59      }
60  
61  
62  
63      /**
64       * This method takes the initial request when click(Patron Bill) on Patron Screen.
65       * @param form
66       * @param result
67       * @param request
68       * @param response
69       * @return  ModelAndView
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       * This method will accept payment from user and update the record in Patron bill payment table.
85       * @param form
86       * @param result
87       * @param request
88       * @param response
89       * @return  ModelAndView
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          PatronBillForm patronBillForm = (PatronBillForm) form;
96          List<PatronBill> patronBillList = patronBillForm.getPatronBillList();
97          Iterator itr = patronBillList.iterator();
98          PatronBill patronBill;
99          int count = 0;
100         String billNumber="";
101         String id="";
102         BigDecimal unPaidAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
103         BigDecimal feeAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
104         BigDecimal totalAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
105         BigDecimal paidAmount = patronBillForm.getPaymentAmount() != null ?  patronBillForm.getPaymentAmount() : new BigDecimal(0.0);
106         if(patronBillForm.getPaymentAmount()==null){
107             GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.PAY_AMT_EMPTY );
108             return getUIFModelAndView(patronBillForm);
109         }
110         for(int i=0;i<patronBillList.size();i++){
111             totalAmount=totalAmount.add(patronBillList.get(i).getFeeAmount());
112         }
113         while(itr.hasNext())  {
114             patronBill = (PatronBill)itr.next();
115             boolean isActive = patronBill.isActive();
116             if(isActive){
117                 count +=1;
118                 id= patronBill.getId();
119                 billNumber = patronBill.getBillNumber();
120                 BigDecimal billPaidAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
121                 if(paidAmount.doubleValue() >= totalAmount.doubleValue()){
122                     PatronBillHelperService patronBillHelperService = new PatronBillHelperService();
123                     feeAmount = patronBill.getFeeAmount();
124                     Map billNoMap = new HashMap();
125                     billNoMap.put("billNumber",billNumber);
126                     List<PatronBillPayment> patronBillPaymentList = (List<PatronBillPayment>)KRADServiceLocator.getBusinessObjectService().findMatching(PatronBillPayment.class,billNoMap);
127                     BigDecimal billTotalAmount = patronBillPaymentList.get(0).getTotalAmount();
128                     if(paidAmount.doubleValue() >= totalAmount.doubleValue()){
129                         unPaidAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
130                         billPaidAmount = billPaidAmount.add(feeAmount);
131                     }else{
132                         unPaidAmount = billTotalAmount.subtract(paidAmount);
133                         billPaidAmount = paidAmount.add(paidAmount);
134                     }
135                     billTotalAmount = billTotalAmount.subtract(feeAmount);
136                    // String paymentStatus = patronBillForm.getPaymentStatus().equals("")? patronBill.getPaymentStatus() : patronBillForm.getPaymentStatus();
137                     String paymentStatus ="";
138                     String paymentMethod = patronBillForm.getPaymentMethod().equals("") ? patronBill.getPaymentMethod() : patronBillForm.getPaymentMethod();
139                     feeAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
140                     if(billTotalAmount.doubleValue() ==0)
141                     {
142                         paymentStatus = getPaymentStatus(OLEConstants.FULL_PAID);
143                     } else{
144                         paymentStatus = getPaymentStatus(OLEConstants.PAR_PAID);
145                     }
146                     patronBillHelperService.updatePatronFeeAmount(id,feeAmount);
147                     patronBillHelperService.updatePatronBillForAccept(billNumber,billPaidAmount,unPaidAmount,billTotalAmount,paymentStatus,paymentMethod);
148                     BigDecimal balanceAmount = paidAmount.subtract(totalAmount);
149                     patronBillForm.setMessage(OLEConstants.BAL_AMT+balanceAmount);
150                 }else{
151                     Map billNoMap = new HashMap();
152                     billNoMap.put("billNumber",billNumber);
153                     List<PatronBillPayment> patronBillPaymentList = (List<PatronBillPayment>)KRADServiceLocator.getBusinessObjectService().findMatching(PatronBillPayment.class,billNoMap);
154                     BigDecimal billTotalAmount = patronBillPaymentList.get(0).getTotalAmount();
155                     feeAmount = patronBill.getFeeAmount();
156                     if(paidAmount.doubleValue()>feeAmount.doubleValue()){
157                         paidAmount = paidAmount.subtract(feeAmount);
158                         billTotalAmount = billTotalAmount.subtract(feeAmount);
159                         billPaidAmount = billPaidAmount.add(feeAmount);
160                         unPaidAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
161                         feeAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
162                     }else{
163                         feeAmount = feeAmount.subtract(paidAmount);
164                         unPaidAmount = billTotalAmount.subtract(paidAmount);
165                         billTotalAmount = billTotalAmount.subtract(paidAmount);
166                         billPaidAmount = paidAmount;
167                     }
168                     PatronBillHelperService patronBillHelperService = new PatronBillHelperService();
169                     //String paymentStatus = patronBillForm.getPaymentStatus().equals("")? patronBill.getPaymentStatus() : patronBillForm.getPaymentStatus();
170                     String paymentStatus ="";
171                     if(billTotalAmount.doubleValue() ==0)
172                     {
173                         paymentStatus = getPaymentStatus(OLEConstants.FULL_PAID);
174                     } else{
175                         paymentStatus = getPaymentStatus(OLEConstants.PAR_PAID);
176                     }
177                     String paymentMethod = patronBillForm.getPaymentMethod().equals("") ? patronBill.getPaymentMethod() : patronBillForm.getPaymentMethod();
178                     patronBillHelperService.updatePatronFeeAmount(id,feeAmount);
179                     patronBillHelperService.updatePatronBillForAccept(billNumber,billPaidAmount,unPaidAmount,billTotalAmount,paymentStatus,paymentMethod);
180                 }
181             }
182         }
183         if(count ==0){
184             GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.SELECT );
185         }
186         return start(patronBillForm, result, request, response);
187     }
188 
189     private String getPaymentStatus(String paymentStatus){
190         LOG.debug("Inside the getPaymentStatus method");
191         Map statusMap = new HashMap();
192         statusMap.put("paymentStatusName", paymentStatus);
193         List<OlePaymentStatus> olePaymentStatusList = (List<OlePaymentStatus>)getBusinessObjectService().findMatching(OlePaymentStatus.class,statusMap);
194         return olePaymentStatusList!=null && olePaymentStatusList.size()>0 ? olePaymentStatusList.get(0).getPaymentStatusId(): null;
195     }
196 
197     /**
198      * This method will allow library operator to forgive payment for user
199      * @param form
200      * @param result
201      * @param request
202      * @param response
203      * @return  ModelAndView
204      */
205     @RequestMapping(params = "methodToCall=forgive")
206     public ModelAndView forgivePayment(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
207                                        HttpServletRequest request, HttpServletResponse response) {
208         LOG.debug("Initialized forgivePayment Method");
209         PatronBillForm patronBillForm = (PatronBillForm) form;
210         List<PatronBill> patronBillList = patronBillForm.getPatronBillList();
211         Iterator itr = patronBillList.iterator();
212         PatronBill patronBill;
213         int count = 0;
214         String billNumber="";
215         String id="";
216         BigDecimal unPaidAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
217         BigDecimal feeAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
218         BigDecimal totalAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
219         BigDecimal paidAmount = patronBillForm.getPaymentAmount() != null ?  patronBillForm.getPaymentAmount() : new BigDecimal(0.0);
220         if(patronBillForm.getPaymentAmount()==null){
221             GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.PAY_AMT_EMPTY );
222             return getUIFModelAndView(patronBillForm);
223         }
224         for(int i=0;i<patronBillList.size();i++){
225             totalAmount=totalAmount.add(patronBillList.get(i).getFeeAmount());
226         }
227         while(itr.hasNext())  {
228             patronBill = (PatronBill)itr.next();
229             boolean isActive = patronBill.isActive();
230             if(isActive){
231                 count +=1;
232                 id= patronBill.getId();
233                 billNumber = patronBill.getBillNumber();
234                 if(patronBillForm.getPaymentAmount().doubleValue() > totalAmount.doubleValue()){
235                     GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.PAID_AMT_EXC);
236                 }
237                 BigDecimal billPaidAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
238                 if(paidAmount.doubleValue() >= totalAmount.doubleValue()){
239                     PatronBillHelperService patronBillHelperService = new PatronBillHelperService();
240                     feeAmount = patronBill.getFeeAmount();
241                     Map billNoMap = new HashMap();
242                     billNoMap.put("billNumber",billNumber);
243                     List<PatronBillPayment> patronBillPaymentList = (List<PatronBillPayment>)KRADServiceLocator.getBusinessObjectService().findMatching(PatronBillPayment.class,billNoMap);
244                     BigDecimal billTotalAmount = patronBillPaymentList.get(0).getTotalAmount();
245                     if(paidAmount.doubleValue() >= totalAmount.doubleValue()){
246                         unPaidAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
247                         billPaidAmount = billPaidAmount.add(feeAmount);
248                     }else{
249                         unPaidAmount = billTotalAmount.subtract(paidAmount);
250                         billPaidAmount = paidAmount.add(paidAmount);
251                     }
252                     billTotalAmount = billTotalAmount.subtract(feeAmount);
253                     //String paymentStatus = patronBillForm.getPaymentStatus().equals("")? patronBill.getPaymentStatus() : patronBillForm.getPaymentStatus();
254                     String paymentStatus ="";
255                     if(billTotalAmount.doubleValue() ==0)
256                     {
257                         paymentStatus = getPaymentStatus(OLEConstants.FULL_PAID);
258                     } else{
259                         paymentStatus = getPaymentStatus(OLEConstants.PAR_PAID);
260                     }
261                     String paymentMethod = patronBillForm.getPaymentMethod().equals("") ? patronBill.getPaymentMethod() : patronBillForm.getPaymentMethod();
262                     feeAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
263                     patronBillHelperService.updatePatronFeeAmount(id,feeAmount);
264                     patronBillHelperService.updatePatronBillForAccept(billNumber,billPaidAmount,unPaidAmount,billTotalAmount,paymentStatus,paymentMethod);
265                 }else{
266                     Map billNoMap = new HashMap();
267                     billNoMap.put("billNumber",billNumber);
268                     List<PatronBillPayment> patronBillPaymentList = (List<PatronBillPayment>)KRADServiceLocator.getBusinessObjectService().findMatching(PatronBillPayment.class,billNoMap);
269                     BigDecimal billTotalAmount = patronBillPaymentList.get(0).getTotalAmount();
270                     feeAmount = patronBill.getFeeAmount();
271                     if(paidAmount.doubleValue()>feeAmount.doubleValue()){
272                         paidAmount = paidAmount.subtract(feeAmount);
273                         billTotalAmount = billTotalAmount.subtract(feeAmount);
274                         unPaidAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
275                         feeAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
276                     }else{
277                         feeAmount = feeAmount.subtract(paidAmount);
278                         unPaidAmount = billTotalAmount.subtract(paidAmount);
279                         billTotalAmount = billTotalAmount.subtract(paidAmount);
280                         billPaidAmount = paidAmount;
281                     }
282                     PatronBillHelperService patronBillHelperService = new PatronBillHelperService();
283                     //String paymentStatus = patronBillForm.getPaymentStatus().equals("")? patronBill.getPaymentStatus() : patronBillForm.getPaymentStatus();
284                     String paymentStatus ="";
285                     if(billTotalAmount.doubleValue() ==0)
286                     {
287                         paymentStatus = getPaymentStatus(OLEConstants.FULL_PAID);
288                     } else{
289                         paymentStatus = getPaymentStatus(OLEConstants.PAR_PAID);
290                     }
291                     patronBillHelperService.updatePatronFeeAmount(id,feeAmount);
292                     patronBillHelperService.updatePatronBillForForgive(billNumber,unPaidAmount,billTotalAmount,paymentStatus);
293                 }
294             }
295         }
296         if(count ==0){
297             GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.SELECT );
298         }
299         return start(patronBillForm, result, request, response);
300     }
301 
302     /**
303      * This method will allow library operator to forgive payment for user
304      * @param form
305      * @param result
306      * @param request
307      * @param response
308      * @return  ModelAndView
309      */
310     @RequestMapping(params = "methodToCall=print")
311     public ModelAndView printBill(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,HttpServletRequest request, HttpServletResponse response) throws Exception {
312         LOG.debug("Initialized printBill Method");
313         PatronBillForm patronBillForm = (PatronBillForm) form;
314         PatronBillPayment patronBillPayment = new PatronBillPayment();
315         List<PatronBill> patronBillList = patronBillForm.getPatronBillList();
316         Iterator itr = patronBillList.iterator();
317         PatronBill patronBill;
318         int count = 0;
319         String billNumber="";
320         while(itr.hasNext())  {
321             patronBill = (PatronBill)itr.next();
322             boolean isPrint = patronBill.isActive();
323             if(isPrint){
324                 count += 1;
325                 if(patronBill.isReviewed())   {
326                 billNumber = patronBill.getBillNumber();
327                 Map billNumberMap = new HashMap();
328                 billNumberMap.put("billNumber", billNumber);
329                 List<PatronBillPayment> patronBillPaymentList = (List<PatronBillPayment>) KRADServiceLocator.getBusinessObjectService().findMatching(PatronBillPayment.class,billNumberMap);
330                 List<FeeType> feeTypeList = (List<FeeType>) KRADServiceLocator.getBusinessObjectService().findMatching(FeeType.class, billNumberMap);
331                 for(int i=0;i< patronBillPaymentList.size();i++){
332                     billNumber = patronBillPaymentList.get(i).getBillNumber();
333                     String firstName = patronBillPaymentList.get(i).getFirstName();
334                     String lastName = patronBillPaymentList.get(i).getLastName();
335                     BigDecimal totalAmount = patronBillPaymentList.get(i).getTotalAmount();
336                 }
337                 PrintBill printBill = new PrintBill();
338                 printBill.generatePdf(patronBillPaymentList,feeTypeList,response);
339                 } else{
340                     GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.NOT_REVIEWED );
341                     //return getUIFModelAndView(patronBillForm,"BillViewPage");
342                 }
343             }
344         }
345         if(count ==0){
346             GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.SELECT );
347         }
348         return getUIFModelAndView(patronBillForm,"BillViewPage");
349     }
350 
351     /**
352      * Just returns as if return with no value was selected.
353      */
354     @Override
355     @RequestMapping(params = "methodToCall=close")
356     public ModelAndView close(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
357                               HttpServletRequest request, HttpServletResponse response) {
358         return super.close(form, result, request, response);
359     }
360 
361 }