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