View Javadoc
1   package org.kuali.ole.deliver.bo;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.log4j.Logger;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.deliver.processor.LoanProcessor;
7   import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
8   import org.kuali.ole.docstore.common.document.content.instance.Item;
9   import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
10  import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
11  import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
12  
13  import org.kuali.ole.sys.context.SpringContext;
14  import org.kuali.rice.core.api.util.type.KualiDecimal;
15  import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
16  import org.kuali.rice.coreservice.api.parameter.Parameter;
17  import org.kuali.rice.coreservice.api.parameter.ParameterKey;
18  import org.kuali.rice.krad.service.BusinessObjectService;
19  import org.kuali.rice.krad.service.KRADServiceLocator;
20  import org.kuali.rice.krad.util.GlobalVariables;
21  
22  import java.math.BigDecimal;
23  import java.sql.*;
24  import java.util.*;
25  
26  /**
27   * Created with IntelliJ IDEA.
28   * User: ?
29   * Date: 10/18/12
30   * Time: 11:18 AM
31   * To change this template use File | Settings | File Templates.
32   */
33  public class PatronBillHelperService {
34  
35      private static final Logger LOG = Logger.getLogger(PatronBillHelperService.class);
36  
37      private DocstoreClientLocator docstoreClientLocator;
38  
39      private List<String> billIds=new ArrayList<String>();
40  
41      public DocstoreClientLocator getDocstoreClientLocator() {
42  
43          if (docstoreClientLocator == null) {
44              docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
45  
46          }
47          return docstoreClientLocator;
48      }
49  
50      public List<String> getBillIds() {
51          return billIds;
52      }
53  
54      /**
55       * Gets the businessObjectService attribute.
56       *
57       * @return Returns the businessObjectService
58       */
59      private BusinessObjectService getBusinessObjectService() {
60          if (null == businessObjectService) {
61              businessObjectService = KRADServiceLocator.getBusinessObjectService();
62          }
63          return businessObjectService;
64      }
65  
66      private BusinessObjectService businessObjectService;
67  
68      /**
69       * This method will retrieve patron bill payment object
70       *
71       * @return Returns the PatronBill List
72       */
73      public List<PatronBillPayment> getBillPayment(String patronId) {
74          LOG.debug("Initialized getService Method");
75          Map patronIdMap = new HashMap();
76          patronIdMap.put("patronId", patronId);
77          List<PatronBillPayment> patronBillPaymentList = (List<PatronBillPayment>) KRADServiceLocator.getBusinessObjectService().findMatching(PatronBillPayment.class, patronIdMap);
78  
79          return sortById(patronBillPaymentList);
80      }
81  
82      public KualiDecimal populateGrandTotal(List<PatronBillPayment> patronBillPaymentList) {
83          KualiDecimal grandTotal = OLEConstants.KUALI_BIGDECIMAL_DEF_VALUE;
84          for (PatronBillPayment patronBillPayment : patronBillPaymentList) {
85              KualiDecimal paidamt = OLEConstants.KUALI_BIGDECIMAL_DEF_VALUE;
86              KualiDecimal unpaidamt = OLEConstants.KUALI_BIGDECIMAL_DEF_VALUE;
87              List<FeeType> feeTypes = patronBillPayment.getFeeType();
88              for (FeeType feeType : feeTypes) {
89                  grandTotal = grandTotal.add(feeType.getBalFeeAmount());
90                  paidamt = paidamt.add(feeType.getPaidAmount());
91                  /*if(feeType.getFeeAmount()!=null && feeType.getBalFeeAmount()!=null && (feeType.getFeeAmount()).compareTo(feeType.getPaidAmount())>0){
92                      unpaidamt = (feeType.getFeeAmount()).subtract(feeType.getPaidAmount());
93                  }*/
94                  unpaidamt = unpaidamt.add(feeType.getBalFeeAmount());
95              }
96              patronBillPayment.setPaidAmount(paidamt);
97              patronBillPayment.setUnPaidBalance(unpaidamt);
98              //getBusinessObjectService().save(patronBillPayment);
99          }
100         return grandTotal;
101     }
102 
103     public List<PatronBillPayment> sortById(List<PatronBillPayment> patronBillPaymentList) {
104         List<PatronBillPayment> outPatronBillPayments = new ArrayList<PatronBillPayment>();
105         List<PatronBillPayment> partPatronBillPayments = new ArrayList<PatronBillPayment>();
106         List<PatronBillPayment> restPatronBillPayments = new ArrayList<PatronBillPayment>();
107         List<PatronBillPayment> categorizedPatronBillPayments = new ArrayList<PatronBillPayment>();
108 
109         for (PatronBillPayment patronBillPayment : patronBillPaymentList) {
110             if (patronBillPayment.getPaymentStatusCode().equalsIgnoreCase(OLEConstants.PAY_STATUS_OUTSTANDING_CODE)) {
111                 outPatronBillPayments.add(patronBillPayment);
112             } else if (patronBillPayment.getPaymentStatusCode().equalsIgnoreCase(OLEConstants.PAY_STATUS_PART_CODE)) {
113                 partPatronBillPayments.add(patronBillPayment);
114             } else {
115                 restPatronBillPayments.add(patronBillPayment);
116             }
117         }
118         categorizedPatronBillPayments.addAll(outPatronBillPayments);
119         categorizedPatronBillPayments.addAll(partPatronBillPayments);
120 
121         Collections.sort(categorizedPatronBillPayments, new Comparator<PatronBillPayment>() {
122             public int compare(PatronBillPayment patronBillPayment1, PatronBillPayment patronBillPayment2) {
123                 return Integer.parseInt(patronBillPayment1.getBillNumber()) > Integer.parseInt(patronBillPayment2.getBillNumber()) ? 1 : -1;
124             }
125         });
126         Collections.sort(restPatronBillPayments, new Comparator<PatronBillPayment>() {
127             public int compare(PatronBillPayment patronBillPayment1, PatronBillPayment patronBillPayment2) {
128                 return Integer.parseInt(patronBillPayment1.getBillNumber()) > Integer.parseInt(patronBillPayment2.getBillNumber()) ? 1 : -1;
129             }
130         });
131         categorizedPatronBillPayments.addAll(restPatronBillPayments);
132         for (PatronBillPayment patronBillPayment : categorizedPatronBillPayments) {
133             List<FeeType> enabledFeeType = new ArrayList<FeeType>();
134             List<FeeType> disableFeeType = new ArrayList<FeeType>();
135             List<FeeType> newFeeTypeList = new ArrayList<FeeType>();
136             for (FeeType feeType : patronBillPayment.getFeeType()) {
137                 KualiDecimal KualiDecimal = OLEConstants.KUALI_BIGDECIMAL_DEF_VALUE;
138 
139                 if (feeType.getBalFeeAmount().intValue() > 0) {
140                     enabledFeeType.add(feeType);
141                 } else {
142                     disableFeeType.add(feeType);
143                 }
144             }
145             newFeeTypeList.addAll(enabledFeeType);
146             newFeeTypeList.addAll(disableFeeType);
147             patronBillPayment.getFeeType().clear();
148             patronBillPayment.setFeeType(newFeeTypeList);
149         }
150 
151         return categorizedPatronBillPayments;
152     }
153 
154     public List<FeeType> getFeeTypeList(List<PatronBillPayment> patronBillPaymentList) {
155         List<FeeType> feeTypes = new ArrayList<FeeType>();
156         List<String> itemUuids = new ArrayList<String>();
157         LoanProcessor loanProcessor = new LoanProcessor();
158         try {
159             for (PatronBillPayment patronBillPayment : patronBillPaymentList) {
160                 feeTypes.addAll(patronBillPayment.getFeeType());
161             }
162             for (FeeType feeType : feeTypes) {
163                 if (feeType.getItemUuid() != null) {
164                     org.kuali.ole.docstore.common.document.Item item = getDocstoreClientLocator().getDocstoreClient().retrieveItem(feeType.getItemUuid());
165                     ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
166                     Item itemContent = itemOlemlRecordProcessor.fromXML(item.getContent());
167                     OleHoldings oleHoldings = new HoldingOlemlRecordProcessor().fromXML(item.getHolding().getContent());
168                     if (feeType.getItemUuid().equals(item.getId())) {
169                         feeType.setItemTitle(item.getHolding().getBib().getTitle());
170                         feeType.setItemAuthor(item.getHolding().getBib().getAuthor());
171                         if(itemContent.getCallNumber()!=null && !StringUtils.isEmpty(itemContent.getCallNumber().getNumber())){
172                             feeType.setItemCallNumber(itemContent.getCallNumber().getNumber());
173                         }else {
174                             feeType.setItemCallNumber(loanProcessor.getItemCallNumber(oleHoldings.getCallNumber()));
175                         }
176                         feeType.setItemCopyNumber(itemContent.getCopyNumber());
177                         feeType.setItemChronologyOwnLocation(itemContent.getChronology());
178                         feeType.setItemEnumeration(itemContent.getEnumeration());
179                         if(itemContent.getTemporaryItemType()!=null && itemContent.getTemporaryItemType().getCodeValue()!=null){
180                             feeType.setItemType(itemContent.getTemporaryItemType().getCodeValue());
181                         }else{
182                             feeType.setItemType(itemContent.getItemType().getCodeValue());
183                         }
184                     }
185                 }
186                 if (feeType.getFeeTypes() != null) {
187                     feeType.getFeeTypes().add(feeType);
188                 }
189             }
190 
191 
192         } catch (Exception e) {
193             LOG.error("Exception while getting fee type list", e);
194         }
195         return feeTypes;
196     }
197 
198     /**
199      * This method will retrieve paymentStatusName based on paymentStatusId
200      *
201      * @return paymentStatusName
202      */
203     private OlePaymentStatus getPaymentStatus(String paymentStatus) {
204         LOG.debug("Inside the getPaymentStatus method");
205         Map statusMap = new HashMap();
206         statusMap.put("paymentStatusCode", paymentStatus);
207         List<OlePaymentStatus> olePaymentStatusList = (List<OlePaymentStatus>) getBusinessObjectService().findMatching(OlePaymentStatus.class, statusMap);
208         return olePaymentStatusList != null && olePaymentStatusList.size() > 0 ? olePaymentStatusList.get(0) : null;
209     }
210 
211 
212     public OlePatronDocument getPatronDetails(String patronId) {
213         OlePatronDocument olePatronDocument = new OlePatronDocument();
214         Map<String, String> criteria = new HashMap<String, String>();
215         criteria.put("olePatronId", patronId);
216         List<OlePatronDocument> olePatronDocumentList = new ArrayList<OlePatronDocument>();
217         olePatronDocumentList = (List<OlePatronDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronDocument.class, criteria);
218         olePatronDocument.setOlePatronId(olePatronDocumentList.get(0).getOlePatronId());
219         olePatronDocument.setBarcode(olePatronDocumentList.get(0).getBarcode());
220         olePatronDocument.setFirstName(olePatronDocumentList.get(0).getEntity().getNames().get(0).getFirstName());
221         olePatronDocument.setLastName(olePatronDocumentList.get(0).getEntity().getNames().get(0).getLastName());
222         olePatronDocument.setBorrowerType(olePatronDocumentList.get(0).getOleBorrowerType().getBorrowerTypeName());
223         return olePatronDocument;
224     }
225 
226     public String getParameter(String name) {
227         ParameterKey parameterKey = ParameterKey.create(OLEConstants.APPL_ID, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, name);
228         Parameter parameter = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameter(parameterKey);
229         return parameter != null ? parameter.getValue() : null;
230     }
231 
232     public KualiDecimal billWiseTransaction(List<PatronBillPayment> patronBillPayments, KualiDecimal paymentAmount, String paymentMethod, String fullyPaidStatus, String partiallyPaidStatus, boolean acceptFlag, String forgiveErrorNote,String transactionNumber,String transactionNote,String paidByUser,List<OleItemLevelBillPayment> currentSessionTransactions) {
233         KualiDecimal payAmt = paymentAmount;
234         LOG.debug("Inside billWiseTransaction");
235         String operatorId = GlobalVariables.getUserSession().getPrincipalId();
236         LOG.debug("Inside billWiseCancellation");
237         List<String> billId = new ArrayList<String>();
238         for (PatronBillPayment patronBillPayment : patronBillPayments) {
239             if (patronBillPayment.isSelectBill() || acceptFlag) {
240                 billId.add(patronBillPayment.getBillNumber());
241             }
242 
243         }
244         List<PatronBillPayment> patronBillPaymentsList = (List<PatronBillPayment>) getBusinessObjectService().findAll(PatronBillPayment.class);
245         KualiDecimal unPaidAmount;
246         for (PatronBillPayment patronBillPayment : patronBillPaymentsList) {
247             if (billId.contains(patronBillPayment.getBillNumber()) || acceptFlag) {
248                 List<FeeType> feeTypes = patronBillPayment.getFeeType();
249                 boolean saveFlag = false;
250                 for (FeeType feeType : feeTypes) {
251                     if (((feeType.getPaymentStatusCode().equalsIgnoreCase(OLEConstants.PAY_OUTSTANDING) || feeType.getPaymentStatusCode().equalsIgnoreCase(OLEConstants.PAY_PARTIALLY))
252                             && paymentAmount.compareTo(OLEConstants.KUALI_BIGDECIMAL_DEF_VALUE) > 0)) {
253                         if (paymentAmount.compareTo(feeType.getBalFeeAmount()) >= 0) {
254                             unPaidAmount = patronBillPayment.getUnPaidBalance().subtract(feeType.getBalFeeAmount());
255                             paymentAmount = paymentAmount.subtract(feeType.getBalFeeAmount());
256                             OlePaymentStatus olePaymentStatus = getPaymentStatus(fullyPaidStatus);
257                             feeType.setOlePaymentStatus(olePaymentStatus);
258                             feeType.setPaymentStatus(olePaymentStatus.getPaymentStatusId());
259                             List<OleItemLevelBillPayment> oleItemLevelBillPayments = feeType.getItemLevelBillPaymentList() != null && feeType.getItemLevelBillPaymentList().size() > 0
260                                     ? feeType.getItemLevelBillPaymentList() : new ArrayList<OleItemLevelBillPayment>();
261                             OleItemLevelBillPayment oleItemLevelBillPayment = new OleItemLevelBillPayment();
262                             oleItemLevelBillPayment.setPaymentDate(new Timestamp(System.currentTimeMillis()));
263                             oleItemLevelBillPayment.setAmount(feeType.getBalFeeAmount());
264                             oleItemLevelBillPayment.setCreatedUser(operatorId);
265                             oleItemLevelBillPayment.setTransactionNote(transactionNote);
266                             oleItemLevelBillPayment.setTransactionNumber(transactionNumber);
267                             oleItemLevelBillPayment.setPaymentMode(paymentMethod);
268                             oleItemLevelBillPayments.add(oleItemLevelBillPayment);
269                             currentSessionTransactions.add(oleItemLevelBillPayment);
270                             feeType.setBalFeeAmount(OLEConstants.KUALI_BIGDECIMAL_DEF_VALUE);
271                             feeType.setItemLevelBillPaymentList(oleItemLevelBillPayments);
272                             if (paymentMethod!=null && paymentMethod.equalsIgnoreCase(OLEConstants.ERROR)) {
273                                 feeType.setErrorNote(forgiveErrorNote);
274                             }
275                             if (paymentMethod!=null && paymentMethod.equalsIgnoreCase(OLEConstants.FORGIVE)) {
276                                 feeType.setForgiveNote(forgiveErrorNote);
277                             }
278                             patronBillPayment.setPaymentAmount(payAmt.subtract(paymentAmount));
279                             payAmt = paymentAmount;
280                         } else {
281                             unPaidAmount = patronBillPayment.getUnPaidBalance().subtract(paymentAmount);
282                             KualiDecimal updatedFeeAmount = feeType.getBalFeeAmount().subtract(paymentAmount);
283                             KualiDecimal transAmount = paymentAmount;
284                             paymentAmount = paymentAmount.subtract(feeType.getBalFeeAmount());
285                             OlePaymentStatus olePaymentStatus = getPaymentStatus(partiallyPaidStatus);
286                             feeType.setOlePaymentStatus(olePaymentStatus);
287                             feeType.setPaymentStatus(olePaymentStatus.getPaymentStatusId());
288                             List<OleItemLevelBillPayment> oleItemLevelBillPayments = feeType.getItemLevelBillPaymentList() != null && feeType.getItemLevelBillPaymentList().size() > 0
289                                     ? feeType.getItemLevelBillPaymentList() : new ArrayList<OleItemLevelBillPayment>();
290                             OleItemLevelBillPayment oleItemLevelBillPayment = new OleItemLevelBillPayment();
291                             oleItemLevelBillPayment.setPaymentDate(new Timestamp(System.currentTimeMillis()));
292                             oleItemLevelBillPayment.setAmount(transAmount.compareTo(OLEConstants.KUALI_BIGDECIMAL_DEF_VALUE) < 0 ? transAmount : transAmount);
293                             oleItemLevelBillPayment.setCreatedUser(operatorId);
294                             oleItemLevelBillPayment.setTransactionNote(transactionNote);
295                             oleItemLevelBillPayment.setTransactionNumber(transactionNumber);
296                             oleItemLevelBillPayment.setPaymentMode(paymentMethod);
297                             oleItemLevelBillPayments.add(oleItemLevelBillPayment);
298                             currentSessionTransactions.add(oleItemLevelBillPayment);
299                             feeType.setBalFeeAmount(updatedFeeAmount);
300                             feeType.setItemLevelBillPaymentList(oleItemLevelBillPayments);
301                             if (paymentMethod!=null && paymentMethod.equalsIgnoreCase(OLEConstants.ERROR)) {
302                                 feeType.setErrorNote(forgiveErrorNote);
303                             }
304                             if (paymentMethod!=null && paymentMethod.equalsIgnoreCase(OLEConstants.FORGIVE)) {
305                                 feeType.setForgiveNote(forgiveErrorNote);
306                             }
307                             patronBillPayment.setPaymentAmount(payAmt);
308                         }
309                         if (unPaidAmount.compareTo(OLEConstants.KUALI_BIGDECIMAL_DEF_VALUE) <= 0) {
310                             patronBillPayment.setSelectBill(false);
311                         }
312                         patronBillPayment.setUnPaidBalance(unPaidAmount);
313                         saveFlag = true;
314                         if (LOG.isDebugEnabled()){
315                             LOG.debug("unPaidAmount" + patronBillPayment.getUnPaidBalance());
316                         }
317                     }
318                 }
319                 if (saveFlag) {
320                     patronBillPayment.setPaymentOperatorId(GlobalVariables.getUserSession().getPrincipalId());
321                     patronBillPayment.setPayDate(new java.sql.Date((new java.util.Date()).getTime()));
322                     patronBillPayment.setPaymentMethod(paymentMethod);
323                     if (patronBillPayment.getPaymentMethod().equals(OLEConstants.FORGIVE) || patronBillPayment.getPaymentMethod().equals(OLEConstants.CANCEL) || patronBillPayment.getPaymentMethod().equals(OLEConstants.ERROR)) {
324                         if(patronBillPayment.getPaymentMethod().equalsIgnoreCase(OLEConstants.FORGIVE)){
325                             patronBillPayment.setFreeTextNote("$"+patronBillPayment.getPaymentAmount() + OLEConstants.FORGIVE_MESSAGE);
326                         }
327                         if(patronBillPayment.getPaymentMethod().equalsIgnoreCase(OLEConstants.CANCEL)){
328                             patronBillPayment.setFreeTextNote(OLEConstants.CANCEL_MESSAGE);
329                         }
330                         if(patronBillPayment.getPaymentMethod().equalsIgnoreCase(OLEConstants.ERROR)){
331                             patronBillPayment.setFreeTextNote("$"+patronBillPayment.getPaymentAmount()+OLEConstants.ERROR_MESSAGE);
332                         }
333                     } else {
334                         patronBillPayment.setFreeTextNote("$"+patronBillPayment.getPaymentAmount() + " paid through " + patronBillPayment.getPaymentMethod());
335                         patronBillPayment.setNote(null);
336                     }
337                     getBusinessObjectService().save(patronBillPayment);
338                 }
339             }
340         }
341         return paymentAmount;
342     }
343 
344     public KualiDecimal itemWiseTransaction(List<FeeType> feeTypes, KualiDecimal paymentAmount, String paymentMethod, String fullyPaidStatus, String partiallyPaidStatus, String forgiveErrorNote,String transactionNumber,String transactionNote,String paidByUser,List<OleItemLevelBillPayment> currentSessionTransactions) {
345         KualiDecimal payAmt = paymentAmount;
346         LOG.debug("Inside itemWiseTransaction");
347         String operatorId = GlobalVariables.getUserSession().getPrincipalId();
348         KualiDecimal unPaidAmount;
349         for (FeeType feeType : feeTypes) {
350             if (feeType.isActiveItem() && ((feeType.getPaymentStatusCode().equalsIgnoreCase(OLEConstants.PAY_OUTSTANDING) || feeType.getPaymentStatusCode().equalsIgnoreCase(OLEConstants.PAY_PARTIALLY))
351                     && paymentAmount.compareTo(OLEConstants.KUALI_BIGDECIMAL_DEF_VALUE) > 0)) {
352                 HashMap<String, String> map = new HashMap<String, String>();
353                 map.put("billNumber", feeType.getBillNumber());
354                 PatronBillPayment patronBillPayment = getBusinessObjectService().findByPrimaryKey(PatronBillPayment.class, map);
355                 List<FeeType> patronFeeTypes = patronBillPayment.getFeeType();
356                 for (FeeType patronFeeType : patronFeeTypes) {
357                     if (patronFeeType.getId().equalsIgnoreCase(feeType.getId())) {
358                         if (paymentAmount.compareTo(patronFeeType.getBalFeeAmount()) >= 0) {
359                             unPaidAmount = patronBillPayment.getUnPaidBalance().subtract(feeType.getBalFeeAmount());
360                             paymentAmount = paymentAmount.subtract(patronFeeType.getBalFeeAmount());
361                             OlePaymentStatus olePaymentStatus = getPaymentStatus(fullyPaidStatus);
362                             patronFeeType.setOlePaymentStatus(olePaymentStatus);
363                             patronFeeType.setPaymentStatus(olePaymentStatus.getPaymentStatusId());
364                             List<OleItemLevelBillPayment> oleItemLevelBillPayments = patronFeeType.getItemLevelBillPaymentList() != null && patronFeeType.getItemLevelBillPaymentList().size() > 0
365                                     ? patronFeeType.getItemLevelBillPaymentList() : new ArrayList<OleItemLevelBillPayment>();
366                             OleItemLevelBillPayment oleItemLevelBillPayment = new OleItemLevelBillPayment();
367                             oleItemLevelBillPayment.setPaymentDate(new Timestamp(System.currentTimeMillis()));
368                             oleItemLevelBillPayment.setAmount(patronFeeType.getBalFeeAmount());
369                             oleItemLevelBillPayment.setCreatedUser(operatorId);
370                             oleItemLevelBillPayment.setTransactionNote(transactionNote);
371                             oleItemLevelBillPayment.setTransactionNumber(transactionNumber);
372                             oleItemLevelBillPayment.setPaymentMode(paymentMethod);
373                             oleItemLevelBillPayments.add(oleItemLevelBillPayment);
374                             currentSessionTransactions.add(oleItemLevelBillPayment);
375                             patronFeeType.setBalFeeAmount(OLEConstants.KUALI_BIGDECIMAL_DEF_VALUE);
376                             patronFeeType.setItemLevelBillPaymentList(oleItemLevelBillPayments);
377                             if (paymentMethod!=null && paymentMethod.equalsIgnoreCase(OLEConstants.ERROR)) {
378                                 patronFeeType.setErrorNote(forgiveErrorNote);
379                             }
380                             if (paymentMethod!=null && paymentMethod.equalsIgnoreCase(OLEConstants.FORGIVE)) {
381                                 patronFeeType.setForgiveNote(forgiveErrorNote);
382                             }
383                             feeType.setActiveItem(false);
384                             patronBillPayment.setPaymentAmount(payAmt.subtract(paymentAmount));
385                             payAmt = paymentAmount;
386                         } else {
387                             unPaidAmount = patronBillPayment.getUnPaidBalance().subtract(paymentAmount);
388                             KualiDecimal updatedFeeAmount = patronFeeType.getBalFeeAmount().subtract(paymentAmount);
389                             KualiDecimal transAmount = paymentAmount;
390                             paymentAmount = paymentAmount.subtract(patronFeeType.getBalFeeAmount());
391                             OlePaymentStatus olePaymentStatus = getPaymentStatus(partiallyPaidStatus);
392                             patronFeeType.setOlePaymentStatus(olePaymentStatus);
393                             patronFeeType.setPaymentStatus(olePaymentStatus.getPaymentStatusId());
394                             List<OleItemLevelBillPayment> oleItemLevelBillPayments = patronFeeType.getItemLevelBillPaymentList() != null && patronFeeType.getItemLevelBillPaymentList().size() > 0
395                                     ? patronFeeType.getItemLevelBillPaymentList() : new ArrayList<OleItemLevelBillPayment>();
396                             OleItemLevelBillPayment oleItemLevelBillPayment = new OleItemLevelBillPayment();
397                             oleItemLevelBillPayment.setPaymentDate(new Timestamp(System.currentTimeMillis()));
398                             oleItemLevelBillPayment.setAmount(transAmount.compareTo(OLEConstants.KUALI_BIGDECIMAL_DEF_VALUE) < 0 ? transAmount : transAmount);
399                             oleItemLevelBillPayment.setCreatedUser(operatorId);
400                             oleItemLevelBillPayment.setTransactionNote(transactionNote);
401                             oleItemLevelBillPayment.setTransactionNumber(transactionNumber);
402                             oleItemLevelBillPayment.setPaymentMode(paymentMethod);
403                             oleItemLevelBillPayments.add(oleItemLevelBillPayment);
404                             currentSessionTransactions.add(oleItemLevelBillPayment);
405                             patronFeeType.setBalFeeAmount(updatedFeeAmount);
406                             patronFeeType.setItemLevelBillPaymentList(oleItemLevelBillPayments);
407                             if (paymentMethod!=null && paymentMethod.equalsIgnoreCase(OLEConstants.ERROR)) {
408                                 patronFeeType.setErrorNote(forgiveErrorNote);
409                             }
410                             if (paymentMethod!=null && paymentMethod.equalsIgnoreCase(OLEConstants.FORGIVE)) {
411                                 patronFeeType.setForgiveNote(forgiveErrorNote);
412                             }
413                             patronBillPayment.setPaymentAmount(payAmt);
414                         }
415                         //feeType = patronFeeType;
416                         patronBillPayment.setUnPaidBalance(unPaidAmount);
417                         if (LOG.isDebugEnabled()){
418                             LOG.debug("unPaidAmount" + patronBillPayment.getUnPaidBalance());
419                         }
420                         patronBillPayment.setPaymentOperatorId(GlobalVariables.getUserSession().getPrincipalId());
421                         patronBillPayment.setPayDate(new java.sql.Date((new java.util.Date()).getTime()));
422                         patronBillPayment.setPaymentMethod(paymentMethod);
423                         if (patronBillPayment.getPaymentMethod().equals(OLEConstants.FORGIVE) || patronBillPayment.getPaymentMethod().equals(OLEConstants.CANCEL) || patronBillPayment.getPaymentMethod().equals(OLEConstants.ERROR)) {
424                             if(patronBillPayment.getPaymentMethod().equalsIgnoreCase(OLEConstants.FORGIVE)){
425                                 patronBillPayment.setFreeTextNote("$"+patronBillPayment.getPaymentAmount() +OLEConstants.FORGIVE_MESSAGE);
426                             }
427                             if(patronBillPayment.getPaymentMethod().equalsIgnoreCase(OLEConstants.CANCEL)){
428                                 patronBillPayment.setFreeTextNote(OLEConstants.CANCEL_MESSAGE_AMT+" $"+patronBillPayment.getPaymentAmount());
429                             }
430                             if(patronBillPayment.getPaymentMethod().equalsIgnoreCase(OLEConstants.ERROR)){
431                                 patronBillPayment.setFreeTextNote("$"+patronBillPayment.getPaymentAmount()+OLEConstants.ERROR_MESSAGE);
432                             }
433                         } else {
434                             patronBillPayment.setFreeTextNote("$" + patronBillPayment.getPaymentAmount() + " paid through " + patronBillPayment.getPaymentMethod());
435                             patronBillPayment.setNote(null);
436                         }
437                         getBusinessObjectService().save(patronBillPayment);
438                     }
439                 }
440             }
441         }
442         return paymentAmount;
443     }
444 
445     public void billWiseCancellation(List<PatronBillPayment> patronBillPayments, String cancellationNote,List<OleItemLevelBillPayment> currentSessionTransactions) {
446         LOG.debug("Inside billWiseCancellation");
447         String billId = null;
448         for (PatronBillPayment patronBillPayment : patronBillPayments) {
449             if (patronBillPayment.isSelectBill()) {
450                 billId = patronBillPayment.getBillNumber();
451             }
452 
453         }
454         String operatorId = GlobalVariables.getUserSession().getPrincipalId();
455         List<PatronBillPayment> patronBillPaymentsList = (List<PatronBillPayment>) getBusinessObjectService().findAll(PatronBillPayment.class);
456         for (PatronBillPayment patronBillPayment : patronBillPaymentsList) {
457             if (patronBillPayment.getBillNumber().equalsIgnoreCase(billId)) {
458                 List<FeeType> feeTypes = patronBillPayment.getFeeType();
459                 for (FeeType feeType : feeTypes) {
460                     OlePaymentStatus olePaymentStatus = getPaymentStatus(OLEConstants.CANCELLED);
461                     feeType.setOlePaymentStatus(olePaymentStatus);
462                     feeType.setPaymentStatus(olePaymentStatus.getPaymentStatusId());
463                     if (cancellationNote != null) {
464                         feeType.setCancellationNote(cancellationNote);
465                     }
466                     List<OleItemLevelBillPayment> oleItemLevelBillPayments = feeType.getItemLevelBillPaymentList() != null && feeType.getItemLevelBillPaymentList().size() > 0
467                             ? feeType.getItemLevelBillPaymentList() : new ArrayList<OleItemLevelBillPayment>();
468                     OleItemLevelBillPayment oleItemLevelBillPayment = new OleItemLevelBillPayment();
469                     oleItemLevelBillPayment.setPaymentDate(new Timestamp(System.currentTimeMillis()));
470                     oleItemLevelBillPayment.setAmount(feeType.getBalFeeAmount());
471                     oleItemLevelBillPayment.setCreatedUser(operatorId);
472                     oleItemLevelBillPayments.add(oleItemLevelBillPayment);
473                     currentSessionTransactions.add(oleItemLevelBillPayment);
474                     feeType.setBalFeeAmount(OLEConstants.KUALI_BIGDECIMAL_DEF_VALUE);
475                     feeType.setItemLevelBillPaymentList(oleItemLevelBillPayments);
476                 }
477                 patronBillPayment.setUnPaidBalance(OLEConstants.KUALI_BIGDECIMAL_DEF_VALUE);
478                 if (LOG.isDebugEnabled()){
479                     LOG.debug("unPaidAmount" + patronBillPayment.getUnPaidBalance());
480                 }
481                 patronBillPayment.setFreeTextNote(OLEConstants.CANCEL_MESSAGE);
482                 patronBillPayment.setNote(null);
483                 patronBillPayment.setSelectBill(false);
484                 patronBillPayment.setPaymentMethod(OLEConstants.CANCEL);
485                 patronBillPayment.setPaymentAmount(OLEConstants.KUALI_BIGDECIMAL_DEF_VALUE);
486                 patronBillPayment.setPaymentOperatorId(GlobalVariables.getUserSession().getPrincipalId());
487                 patronBillPayment.setPayDate(new java.sql.Date((new java.util.Date()).getTime()));
488                 getBusinessObjectService().save(patronBillPaymentsList);
489 
490             }
491 
492         }
493         patronBillPayments.clear();
494         patronBillPayments.addAll(patronBillPaymentsList);
495     }
496 
497     public void itemWiseCancellation(List<FeeType> feeTypes, String cancellationNote,List<OleItemLevelBillPayment> currentSessionTransactions) {
498         LOG.debug("Inside itemWiseCancellation");
499         String operatorId = GlobalVariables.getUserSession().getPrincipalId();
500         for (FeeType feeType : feeTypes) {
501             if (feeType.isActiveItem()) {
502                 HashMap<String, String> map = new HashMap<String, String>();
503                 map.put("billNumber", feeType.getBillNumber());
504                 PatronBillPayment patronBillPayment = getBusinessObjectService().findByPrimaryKey(PatronBillPayment.class, map);
505                 List<FeeType> patronFeeTypes = patronBillPayment.getFeeType();
506                 for (FeeType patronFeeType : patronFeeTypes) {
507                     if (patronFeeType.getId().equalsIgnoreCase(feeType.getId())) {
508                         OlePaymentStatus olePaymentStatus = getPaymentStatus(OLEConstants.CANCELLED);
509                         patronFeeType.setOlePaymentStatus(olePaymentStatus);
510                         patronFeeType.setPaymentStatus(olePaymentStatus.getPaymentStatusId());
511                         if (cancellationNote != null) {
512                             patronFeeType.setCancellationNote(cancellationNote);
513                         }
514                         List<OleItemLevelBillPayment> oleItemLevelBillPayments = patronFeeType.getItemLevelBillPaymentList() != null && patronFeeType.getItemLevelBillPaymentList().size() > 0
515                                 ? patronFeeType.getItemLevelBillPaymentList() : new ArrayList<OleItemLevelBillPayment>();
516                         OleItemLevelBillPayment oleItemLevelBillPayment = new OleItemLevelBillPayment();
517                         oleItemLevelBillPayment.setPaymentDate(new Timestamp(System.currentTimeMillis()));
518                         oleItemLevelBillPayment.setAmount(patronFeeType.getBalFeeAmount());
519                         BigDecimal billValue=patronFeeType.getBalFeeAmount().bigDecimalValue();
520                         if(billValue==null){
521                             billValue=OLEConstants.BIGDECIMAL_DEF_VALUE;
522                         }
523                         oleItemLevelBillPayment.setCreatedUser(operatorId);
524                         oleItemLevelBillPayments.add(oleItemLevelBillPayment);
525                         currentSessionTransactions.add(oleItemLevelBillPayment);
526                         patronFeeType.setBalFeeAmount(OLEConstants.KUALI_BIGDECIMAL_DEF_VALUE);
527                         patronFeeType.setActiveItem(false);
528                         patronFeeType.setItemLevelBillPaymentList(oleItemLevelBillPayments);
529                         patronBillPayment.setUnPaidBalance(OLEConstants.KUALI_BIGDECIMAL_DEF_VALUE);
530                         if (LOG.isDebugEnabled()){
531                             LOG.debug("unPaidAmount" + patronBillPayment.getUnPaidBalance());
532                         }
533                         patronBillPayment.setFreeTextNote(OLEConstants.CANCEL_MESSAGE_AMT+"$"+billValue);
534                         patronBillPayment.setNote(null);
535                         patronBillPayment.setPaymentMethod(OLEConstants.CANCEL);
536                         patronBillPayment.setPaymentOperatorId(GlobalVariables.getUserSession().getPrincipalId());
537                         patronBillPayment.setPaymentAmount(OLEConstants.KUALI_BIGDECIMAL_DEF_VALUE);
538                         patronBillPayment.setPayDate(new java.sql.Date((new java.util.Date()).getTime()));
539                         getBusinessObjectService().save(patronBillPayment);
540                     }
541                 }
542             }
543         }
544     }
545 
546     public boolean isSelectedPaidPatronBillFullyPaid(List<PatronBillPayment> patronBillPayments){
547         billIds.clear();
548         boolean isSuccess=true;
549         for(PatronBillPayment patronBillPayment:patronBillPayments){
550             if(patronBillPayment.getUnPaidBalance().floatValue()==OLEConstants.KUALI_BIGDECIMAL_DEF_VALUE.floatValue() && patronBillPayment.isSelectBill()){
551                isSuccess=false;
552                 if(!billIds.contains(patronBillPayment.getBillNumber()))
553                     billIds.add(patronBillPayment.getBillNumber());
554             }
555 
556         }
557       return isSuccess;
558     }
559 
560     public  boolean isSelectedFeeTypeFullyPaid(List<FeeType> feeTypes){
561        billIds.clear();
562        boolean isSuccess=true;
563         for(FeeType feeType:feeTypes){
564             if(feeType.getBalFeeAmount().floatValue()==OLEConstants.KUALI_BIGDECIMAL_DEF_VALUE.floatValue() && feeType.isActiveItem()){
565                 isSuccess=false;
566                 if(!billIds.contains(feeType.getBillNumber()))
567                    billIds.add(feeType.getBillNumber());
568             }
569         }
570         return isSuccess;
571     }
572 
573     public KualiDecimal getSumOfSelectedPatronBills(List<PatronBillPayment>  patronBillPayments){
574         KualiDecimal sum=OLEConstants.KUALI_BIGDECIMAL_DEF_VALUE;
575         for(PatronBillPayment patronBillPayment:patronBillPayments){
576             if (patronBillPayment.isSelectBill()) {
577                 sum = sum.add(patronBillPayment.getUnPaidBalance());
578             }
579         }
580         return sum;
581     }
582 
583     public KualiDecimal getSumOfSelectedFeeTypes(List<FeeType>  feeTypes){
584         KualiDecimal sum=OLEConstants.KUALI_BIGDECIMAL_DEF_VALUE;
585         for(FeeType feeType:feeTypes){
586             if (feeType.isActiveItem()) {
587                 sum = sum.add(feeType.getBalFeeAmount());
588             }
589         }
590         return sum;
591     }
592 
593     public List<FeeType> getUpdateItemDetailsToFeeTypeList(List<FeeType> feeTypes) {
594         LoanProcessor loanProcessor = new LoanProcessor();
595         try {
596 
597             for (FeeType feeType : feeTypes) {
598                 if (feeType.getItemUuid() != null) {
599                     org.kuali.ole.docstore.common.document.Item item = getDocstoreClientLocator().getDocstoreClient().retrieveItem(feeType.getItemUuid());
600                     ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
601                     Item itemContent = itemOlemlRecordProcessor.fromXML(item.getContent());
602                     OleHoldings oleHoldings = new HoldingOlemlRecordProcessor().fromXML(item.getHolding().getContent());
603                     if (feeType.getItemUuid().equals(item.getId())) {
604                         feeType.setItemTitle(item.getHolding().getBib().getTitle());
605                         feeType.setItemAuthor(item.getHolding().getBib().getAuthor());
606                         if(itemContent.getCallNumber()!=null && !StringUtils.isEmpty(itemContent.getCallNumber().getNumber())){
607                             feeType.setItemCallNumber(itemContent.getCallNumber().getNumber());
608                         }else {
609                             feeType.setItemCallNumber(loanProcessor.getItemCallNumber(oleHoldings.getCallNumber()));
610                         }
611                         feeType.setItemCopyNumber(itemContent.getCopyNumber());
612                         feeType.setItemChronologyOwnLocation(itemContent.getChronology());
613                         feeType.setItemEnumeration(itemContent.getEnumeration());
614                         if(itemContent.getTemporaryItemType()!=null && itemContent.getTemporaryItemType().getCodeValue()!=null){
615                             feeType.setItemType(itemContent.getTemporaryItemType().getCodeValue());
616                         }else{
617                             feeType.setItemType(itemContent.getItemType().getCodeValue());
618                         }
619                     }
620                 }
621                 if(feeType.getFeeTypes()!=null){
622                     feeType.getFeeTypes().add(feeType);
623                 }
624             }
625 
626 
627         } catch (Exception e) {
628             LOG.error("Exception while updating item details to feeTypeList", e);
629         }
630         return feeTypes;
631     }
632 }