1 package org.kuali.ole.patron.bill;
2
3 import org.apache.log4j.Logger;
4 import org.kuali.ole.OLEConstants;
5 import org.kuali.rice.krad.service.BusinessObjectService;
6 import org.kuali.rice.krad.service.KRADServiceLocator;
7 import org.kuali.rice.krad.util.GlobalVariables;
8
9 import java.math.BigDecimal;
10 import java.util.*;
11
12
13
14
15
16
17
18
19 public class PatronBillHelperService {
20
21 private static final Logger LOG = Logger.getLogger(PatronBillHelperService.class);
22
23
24
25
26
27
28 private BusinessObjectService getBusinessObjectService() {
29 if (null == businessObjectService) {
30 businessObjectService = KRADServiceLocator.getBusinessObjectService();
31 }
32 return businessObjectService;
33 }
34
35 private BusinessObjectService businessObjectService;
36
37
38
39
40
41 public List<PatronBill> getService(String patronId){
42 LOG.debug("Initialized getService Method");
43 List<PatronBill> paidPatronBillList = new ArrayList<PatronBill>();
44 List<PatronBill> unPaidPatronBillList = new ArrayList<PatronBill>();
45 List<PatronBill> patronBillList = new ArrayList<PatronBill>();
46 Map patronIdMap = new HashMap();
47 patronIdMap.put("patronId", patronId);
48 List<PatronBillPayment> patronBillPaymentList = (List<PatronBillPayment>) KRADServiceLocator.getBusinessObjectService().findMatching(PatronBillPayment.class,patronIdMap);
49 PatronBill patronBill;
50 String billNumber="";
51 for(int i=0;i< patronBillPaymentList.size();i++){
52 billNumber = patronBillPaymentList.get(i).getBillNumber();
53 Map feeTypeMap = new HashMap();
54 feeTypeMap.put("billNumber",billNumber);
55 List<FeeType> feeTypeList = (List<FeeType>) KRADServiceLocator.getBusinessObjectService().findMatching(FeeType.class, feeTypeMap);
56 PatronFeeType patronFeeType;
57 if(patronBillPaymentList.size()>0 && feeTypeList.size()==0){
58 if(patronBillPaymentList.get(i).getPaymentStatusName().equalsIgnoreCase(OLEConstants.FULL_PAID)){
59 patronBill = new PatronBill();
60 patronBill.setBillNumber(patronBillPaymentList.get(i).getBillNumber());
61 patronBill.setPatronId(patronBillPaymentList.get(i).getPatronId());
62 patronBill.setPaymentAmount(patronBillPaymentList.get(i).getPaymentAmount());
63 patronBill.setUnPaidBalance(patronBillPaymentList.get(i).getUnPaidBalance());
64 patronBill.setTotalAmount(patronBillPaymentList.get(i).getTotalAmount());
65 patronBill.setPaymentStatus(patronBillPaymentList.get(i).getPaymentStatus());
66
67 patronBill.setPaymentStatusName(getPaymentStatus(patronBillPaymentList.get(i).getPaymentStatus()));
68 patronBill.setFirstName(patronBillPaymentList.get(i).getFirstName());
69 patronBill.setLastName(patronBillPaymentList.get(i).getLastName());
70 patronBill.setFeeType(OLEConstants.FEE_TYPE_NONE);
71 patronBill.setFeeAmount(OLEConstants.BIGDECIMAL_DEF_VALUE);
72 Date billDate = (Date) patronBillPaymentList.get(i).getBillDate();
73 patronBill.setBillDate(billDate);
74 patronBill.setReviewed(patronBillPaymentList.get(i).isReviewed());
75 paidPatronBillList.add(patronBill);
76 }else{
77 patronBill = new PatronBill();
78 patronBill.setBillNumber(patronBillPaymentList.get(i).getBillNumber());
79 patronBill.setPatronId(patronBillPaymentList.get(i).getPatronId());
80 patronBill.setTotalAmount(patronBillPaymentList.get(i).getTotalAmount());
81
82 patronBill.setPaymentStatus(patronBillPaymentList.get(i).getPaymentStatus());
83 patronBill.setPaymentStatusName(getPaymentStatus(patronBillPaymentList.get(i).getPaymentStatus()));
84 patronBill.setPaymentAmount(patronBillPaymentList.get(i).getPaymentAmount());
85 patronBill.setUnPaidBalance(patronBillPaymentList.get(i).getUnPaidBalance());
86 patronBill.setFeeType(OLEConstants.FEE_TYPE_NONE);
87 patronBill.setFeeAmount(OLEConstants.BIGDECIMAL_DEF_VALUE);
88 patronBill.setFirstName(patronBillPaymentList.get(i).getFirstName());
89 patronBill.setLastName(patronBillPaymentList.get(i).getLastName());
90 Date billDate = (Date) patronBillPaymentList.get(i).getBillDate();
91 patronBill.setBillDate(billDate);
92 patronBill.setReviewed(patronBillPaymentList.get(i).isReviewed());
93 unPaidPatronBillList.add(patronBill);
94 }
95 }else{
96 for(int j=0;j<feeTypeList.size();j++){
97 String paymentStatus = patronBillPaymentList.get(i).getPaymentStatusName() != null ? patronBillPaymentList.get(i).getPaymentStatusName() : " ";
98 if(paymentStatus.equalsIgnoreCase(OLEConstants.FULL_PAID)){
99 patronBill = new PatronBill();
100 patronBill.setBillNumber(patronBillPaymentList.get(i).getBillNumber());
101 patronBill.setItemBarcode(feeTypeList.get(j).getItemBarcode());
102 patronBill.setPatronId(patronBillPaymentList.get(i).getPatronId());
103 patronBill.setFeeType(feeTypeList.get(j).getFeeTypeName());
104 patronBill.setFeeAmount(feeTypeList.get(j).getFeeAmount());
105 patronBill.setId(feeTypeList.get(j).getId());
106 patronBill.setTotalAmount(patronBillPaymentList.get(i).getTotalAmount());
107 patronBill.setPaymentStatus(patronBillPaymentList.get(i).getPaymentStatus());
108
109 patronBill.setPaymentStatusName(getPaymentStatus(patronBillPaymentList.get(i).getPaymentStatus()));
110 Date billDate = (Date) patronBillPaymentList.get(i).getBillDate();
111 patronBill.setPaymentAmount(patronBillPaymentList.get(i).getPaymentAmount());
112 patronBill.setUnPaidBalance(patronBillPaymentList.get(i).getUnPaidBalance());
113 patronBill.setBillDate(billDate);
114 patronBill.setId(feeTypeList.get(j).getId());
115 patronBill.setFirstName(patronBillPaymentList.get(i).getFirstName());
116 patronBill.setLastName(patronBillPaymentList.get(i).getLastName());
117 patronBill.setReviewed(patronBillPaymentList.get(i).isReviewed());
118 paidPatronBillList.add(patronBill);
119 }else{
120 patronBill = new PatronBill();
121 patronBill.setBillNumber(patronBillPaymentList.get(i).getBillNumber());
122 patronBill.setItemBarcode(feeTypeList.get(j).getItemBarcode());
123 patronBill.setPatronId(patronBillPaymentList.get(i).getPatronId());
124 patronBill.setFeeType(feeTypeList.get(j).getFeeTypeName());
125 patronBill.setFeeAmount(feeTypeList.get(j).getFeeAmount());
126 patronBill.setTotalAmount(patronBillPaymentList.get(i).getTotalAmount());
127
128 patronBill.setPaymentStatusName(getPaymentStatus(patronBillPaymentList.get(i).getPaymentStatus()));
129 patronBill.setPaymentStatus(patronBillPaymentList.get(i).getPaymentStatus());
130 Date billDate = (Date) patronBillPaymentList.get(i).getBillDate();
131 patronBill.setBillDate(billDate);
132 patronBill.setId(feeTypeList.get(j).getId());
133 patronBill.setPaymentAmount(patronBillPaymentList.get(i).getPaymentAmount());
134 patronBill.setUnPaidBalance(patronBillPaymentList.get(i).getUnPaidBalance());
135 patronBill.setFirstName(patronBillPaymentList.get(i).getFirstName());
136 patronBill.setLastName(patronBillPaymentList.get(i).getLastName());
137 patronBill.setReviewed(patronBillPaymentList.get(i).isReviewed());
138 unPaidPatronBillList.add(patronBill);
139 }
140 }
141 }
142 }
143 sortByDate(unPaidPatronBillList);
144 sortByDate(paidPatronBillList);
145 patronBillList.addAll(unPaidPatronBillList);
146 patronBillList.addAll(paidPatronBillList);
147 return patronBillList;
148 }
149
150
151
152
153
154
155 public List<PatronBill> sortByDate(List<PatronBill> patronBillList){
156 PatronBill patronBill;
157 Collections.sort(patronBillList, new Comparator<PatronBill>() {
158 public int compare(PatronBill patronBill1, PatronBill patronBill2) {
159 return patronBill1.getBillDate().compareTo(patronBill2.getBillDate());
160 }
161 });
162 return patronBillList;
163 }
164
165
166
167
168
169
170 public void updatePatronBillForAccept(String billNumber,BigDecimal paymentAmount,BigDecimal unPaidBalance,BigDecimal totalAmount,String paymentStatus,String paymentMethod){
171 String operatorId = GlobalVariables.getUserSession().getLoggedInUserPrincipalName();
172 Map patronIdMap = new HashMap();
173 patronIdMap.put("billNumber", billNumber);
174 PatronBillPayment patronBillPayment;
175 BigDecimal paidAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
176 Date payDate = new Date();
177 List<PatronBillPayment> patronBillPaymentList = (List<PatronBillPayment>) KRADServiceLocator.getBusinessObjectService().findMatching(PatronBillPayment.class,patronIdMap);
178 for(int i=0;i< patronBillPaymentList.size();i++){
179 patronBillPayment = new PatronBillPayment();
180 if(patronBillPaymentList.get(0).getPaymentAmount() !=null){
181 paidAmount = patronBillPaymentList.get(0).getPaymentAmount();
182 }else{
183 paidAmount = OLEConstants.BIGDECIMAL_DEF_VALUE;
184 }
185 paymentAmount = paymentAmount.add(paidAmount);
186 patronBillPayment = patronBillPaymentList.get(i);
187 patronBillPayment.setPaymentAmount(paymentAmount);
188 patronBillPayment.setPaymentStatus(paymentStatus);
189 patronBillPayment.setUnPaidBalance(unPaidBalance);
190 patronBillPayment.setTotalAmount(totalAmount);
191 patronBillPayment.setPaymentMethod(paymentMethod);
192 patronBillPayment.setPaymentOperatorId(operatorId);
193 patronBillPayment.setPayDate(payDate);
194 getBusinessObjectService().save(patronBillPayment);
195 }
196 }
197
198
199
200
201 private String getPaymentStatus(String paymentStatus){
202 LOG.debug("Inside the getPaymentStatus method");
203 Map statusMap = new HashMap();
204 statusMap.put("paymentStatusId", paymentStatus);
205 List<OlePaymentStatus> olePaymentStatusList = (List<OlePaymentStatus>)getBusinessObjectService().findMatching(OlePaymentStatus.class,statusMap);
206 return olePaymentStatusList!=null && olePaymentStatusList.size()>0 ? olePaymentStatusList.get(0).getPaymentStatusName(): null;
207 }
208
209
210
211
212
213 public void updatePatronBillForForgive(String billNumber ,BigDecimal unPaidBalance,BigDecimal totalAmount,String paymentStatus){
214 String operatorId = GlobalVariables.getUserSession().getLoggedInUserPrincipalName();
215 Map patronIdMap = new HashMap();
216 patronIdMap.put("billNumber", billNumber);
217 PatronBillPayment patronBillPayment;
218 List<PatronBillPayment> patronBillPaymentList = (List<PatronBillPayment>) KRADServiceLocator.getBusinessObjectService().findMatching(PatronBillPayment.class,patronIdMap);
219 for(int i=0;i< patronBillPaymentList.size();i++){
220 patronBillPayment = new PatronBillPayment();
221 patronBillPayment = patronBillPaymentList.get(i);
222 patronBillPayment.setPaymentStatus(paymentStatus);
223 patronBillPayment.setUnPaidBalance(unPaidBalance);
224 patronBillPayment.setTotalAmount(totalAmount);
225 patronBillPayment.setOperatorId(operatorId);
226 getBusinessObjectService().save(patronBillPayment);
227 }
228 }
229
230 public void updatePatronFeeAmount(String id,BigDecimal feeAmount){
231 Map feeTypeId = new HashMap();
232 feeTypeId.put("id",id);
233 FeeType feeType;
234 List<FeeType> feeTypeList = (List<FeeType>)KRADServiceLocator.getBusinessObjectService().findMatching(FeeType.class,feeTypeId);
235 for(int i=0;i<feeTypeList.size();i++){
236 feeType = new FeeType();
237 feeType = feeTypeList.get(i);
238 feeType.setFeeAmount(feeAmount);
239 getBusinessObjectService().save(feeType);
240 }
241 }
242 }