1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.module.purap.document.dataaccess.impl;
17
18 import org.apache.log4j.Logger;
19 import org.apache.ojb.broker.query.Criteria;
20 import org.apache.ojb.broker.query.QueryByCriteria;
21 import org.apache.ojb.broker.query.ReportQueryByCriteria;
22 import org.kuali.ole.module.purap.PurapPropertyConstants;
23 import org.kuali.ole.module.purap.document.InvoiceDocument;
24 import org.kuali.ole.module.purap.document.dataaccess.InvoiceDao;
25 import org.kuali.ole.module.purap.util.VendorGroupingHelper;
26 import org.kuali.ole.sys.OLEPropertyConstants;
27 import org.kuali.rice.core.api.util.type.KualiDecimal;
28 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
29 import org.springframework.transaction.annotation.Transactional;
30
31 import java.sql.Date;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.List;
35
36
37
38
39 @Transactional
40 public class InvoiceDaoOjb extends PlatformAwareDaoBaseOjb implements InvoiceDao {
41 private static Logger LOG = Logger.getLogger(InvoiceDaoOjb.class);
42
43
44
45
46
47
48
49
50
51 public List<InvoiceDocument> getInvoicesToExtract(boolean onlySpecialPayments, String chartCode, Date onOrBeforeInvoicePayDate) {
52 LOG.debug("getInvoicesToExtract() started");
53
54 Criteria criteria = new Criteria();
55 if (chartCode != null) {
56 criteria.addEqualTo("processingCampusCode", chartCode);
57 }
58
59 criteria.addIsNull("extractedTimestamp");
60 criteria.addEqualTo("holdIndicator", Boolean.FALSE);
61
62 if (onlySpecialPayments) {
63 Criteria a = new Criteria();
64
65 Criteria c1 = new Criteria();
66 c1.addNotNull("specialHandlingInstructionLine1Text");
67 Criteria c2 = new Criteria();
68 c2.addNotNull("specialHandlingInstructionLine2Text");
69 Criteria c3 = new Criteria();
70 c3.addNotNull("specialHandlingInstructionLine3Text");
71 Criteria c4 = new Criteria();
72 c4.addEqualTo("paymentAttachmentIndicator", Boolean.TRUE);
73
74 c1.addOrCriteria(c2);
75 c1.addOrCriteria(c3);
76 c1.addOrCriteria(c4);
77
78 a.addAndCriteria(c1);
79 a.addLessOrEqualThan("invoicePayDate", onOrBeforeInvoicePayDate);
80
81 Criteria c5 = new Criteria();
82 c5.addEqualTo("immediatePaymentIndicator", Boolean.TRUE);
83 c5.addOrCriteria(a);
84
85 criteria.addAndCriteria(a);
86 } else {
87 Criteria c1 = new Criteria();
88 c1.addLessOrEqualThan("invoicePayDate", onOrBeforeInvoicePayDate);
89
90 Criteria c2 = new Criteria();
91 c2.addEqualTo("immediatePaymentIndicator", Boolean.TRUE);
92
93 c1.addOrCriteria(c2);
94 criteria.addAndCriteria(c1);
95 }
96
97 return (List<InvoiceDocument>) getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(InvoiceDocument.class, criteria));
98 }
99
100
101
102
103 public List<InvoiceDocument> getImmediateInvoicesToExtract(String chartCode) {
104 LOG.debug("getImmediateInvoicesToExtract() started");
105
106 Criteria criteria = new Criteria();
107 if (chartCode != null) {
108 criteria.addEqualTo("processingCampusCode", chartCode);
109 }
110
111
112 criteria.addIsNull("extractedTimestamp");
113 criteria.addEqualTo("immediatePaymentIndicator", Boolean.TRUE);
114
115 return (List<InvoiceDocument>) getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(InvoiceDocument.class, criteria));
116 }
117
118
119
120
121 @Deprecated
122 public List<InvoiceDocument> getInvoicesToExtract(String campusCode, Integer invoiceIdentifier, Integer purchaseOrderIdentifier, Integer vendorHeaderGeneratedIdentifier, Integer vendorDetailAssignedIdentifier, Date currentSqlDateMidnight) {
123 LOG.debug("getInvoicesToExtract() started");
124
125 Criteria criteria = new Criteria();
126 criteria.addEqualTo("processingCampusCode", campusCode);
127
128 criteria.addIsNull("extractedTimestamp");
129 criteria.addEqualTo("holdIndicator", Boolean.FALSE);
130
131 Criteria c1 = new Criteria();
132 c1.addLessOrEqualThan("invoicePayDate", currentSqlDateMidnight);
133
134 Criteria c2 = new Criteria();
135 c2.addEqualTo("immediatePaymentIndicator", Boolean.TRUE);
136
137 c1.addOrCriteria(c2);
138 criteria.addAndCriteria(c1);
139
140 if (invoiceIdentifier != null) {
141 criteria.addEqualTo("purapDocumentIdentifier", invoiceIdentifier);
142 }
143 if (purchaseOrderIdentifier != null) {
144 criteria.addEqualTo("purchaseOrderIdentifier", purchaseOrderIdentifier);
145 }
146 criteria.addEqualTo("vendorHeaderGeneratedIdentifier", vendorHeaderGeneratedIdentifier);
147 criteria.addEqualTo("vendorDetailAssignedIdentifier", vendorDetailAssignedIdentifier);
148
149 return (List<InvoiceDocument>) getPersistenceBrokerTemplate().getIteratorByQuery(new QueryByCriteria(InvoiceDocument.class, criteria));
150 }
151
152
153
154
155
156 public Collection<InvoiceDocument> getInvoicesToExtractForVendor(String campusCode, VendorGroupingHelper vendor, Date onOrBeforeInvoicePayDate) {
157 LOG.debug("getInvoicesToExtract() started");
158
159 Criteria criteria = new Criteria();
160 criteria.addEqualTo("processingCampusCode", campusCode);
161
162 criteria.addIsNull("extractedTimestamp");
163 criteria.addEqualTo("holdIndicator", Boolean.FALSE);
164
165 Criteria c1 = new Criteria();
166 c1.addLessOrEqualThan("invoicePayDate", onOrBeforeInvoicePayDate);
167
168 Criteria c2 = new Criteria();
169 c2.addEqualTo("immediatePaymentIndicator", Boolean.TRUE);
170
171 c1.addOrCriteria(c2);
172 criteria.addAndCriteria(c1);
173
174 criteria.addEqualTo("vendorHeaderGeneratedIdentifier", vendor.getVendorHeaderGeneratedIdentifier());
175 criteria.addEqualTo("vendorDetailAssignedIdentifier", vendor.getVendorDetailAssignedIdentifier());
176 criteria.addEqualTo("vendorCountryCode", vendor.getVendorCountry());
177 criteria.addLike("vendorPostalCode", vendor.getVendorPostalCode() + "%");
178
179 return getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(InvoiceDocument.class, criteria));
180 }
181
182
183
184
185 public List<String> getEligibleForAutoApproval(Date todayAtMidnight) {
186
187 Criteria criteria = new Criteria();
188 criteria.addLessOrEqualThan(PurapPropertyConstants.PAYMENT_REQUEST_PAY_DATE, todayAtMidnight);
189 criteria.addNotEqualTo("holdIndicator", "Y");
190 criteria.addNotEqualTo("invoiceCancelIndicator", "Y");
191
192 List<String> returnList = getDocumentNumbersOfInvoiceByCriteria(criteria, false);
193
194 return returnList;
195 }
196
197
198
199
200
201 public String getDocumentNumberByInvoiceId(Integer id) {
202 Criteria criteria = new Criteria();
203 criteria.addEqualTo(PurapPropertyConstants.PURAP_DOC_ID, id);
204 return getDocumentNumberOfInvoiceByCriteria(criteria);
205 }
206
207
208
209
210 public List<String> getDocumentNumbersByPurchaseOrderId(Integer poPurApId) {
211 List<String> returnList = new ArrayList<String>();
212 Criteria criteria = new Criteria();
213 criteria.addEqualTo(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, poPurApId);
214 returnList = getDocumentNumbersOfInvoiceByCriteria(criteria, false);
215
216 return returnList;
217 }
218
219
220
221
222
223
224
225 protected String getDocumentNumberOfInvoiceByCriteria(Criteria criteria) {
226 LOG.debug("getDocumentNumberOfInvoiceByCriteria() started");
227 List<String> returnList = getDocumentNumbersOfInvoiceByCriteria(criteria, false);
228
229 if (returnList.isEmpty()) {
230 return null;
231 }
232
233 if (returnList.size() > 1) {
234
235 String errorMsg = "Expected single document number for given criteria but multiple (at least 2) were returned";
236 LOG.error(errorMsg);
237 throw new RuntimeException();
238
239 } else {
240 return (returnList.get(0));
241 }
242 }
243
244
245
246
247
248
249
250
251
252 protected List<String> getDocumentNumbersOfInvoiceByCriteria(Criteria criteria, boolean orderByAscending) {
253 LOG.debug("getDocumentNumberOfInvoiceByCriteria() started");
254 ReportQueryByCriteria rqbc = new ReportQueryByCriteria(InvoiceDocument.class, criteria);
255 if (orderByAscending) {
256 rqbc.addOrderByAscending(OLEPropertyConstants.DOCUMENT_NUMBER);
257 } else {
258 rqbc.addOrderByDescending(OLEPropertyConstants.DOCUMENT_NUMBER);
259 }
260
261 List<String> returnList = new ArrayList<String>();
262
263 List<InvoiceDocument> prDocs = (List<InvoiceDocument>) getPersistenceBrokerTemplate().getCollectionByQuery(rqbc);
264 for (InvoiceDocument prDoc : prDocs) {
265 returnList.add(prDoc.getDocumentNumber());
266 }
267
268 return returnList;
269 }
270
271
272
273
274
275
276
277 protected List<InvoiceDocument> getInvoicesByQueryByCriteria(QueryByCriteria qbc) {
278 LOG.debug("getInvoicesByQueryByCriteria() started");
279 return (List<InvoiceDocument>) getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
280 }
281
282
283
284
285
286
287
288
289
290 public List<InvoiceDocument> getActiveInvoicesByVendorNumberInvoiceNumber(Integer vendorHeaderGeneratedId, Integer vendorDetailAssignedId, String invoiceNumber) {
291 LOG.debug("getActiveInvoicesByVendorNumberInvoiceNumber() started");
292 Criteria criteria = new Criteria();
293 criteria.addEqualTo("vendorHeaderGeneratedIdentifier", vendorHeaderGeneratedId);
294 criteria.addEqualTo("vendorDetailAssignedIdentifier", vendorDetailAssignedId);
295 criteria.addEqualTo("invoiceNumber", invoiceNumber);
296 QueryByCriteria qbc = new QueryByCriteria(InvoiceDocument.class, criteria);
297 return this.getInvoicesByQueryByCriteria(qbc);
298 }
299
300
301
302
303
304
305
306
307 public List<InvoiceDocument> getActiveInvoicesByVendorNumber(Integer vendorHeaderGeneratedId, Integer vendorDetailAssignedId) {
308 LOG.debug("getActiveInvoicesByVendorNumber started");
309 Criteria criteria = new Criteria();
310 criteria.addEqualTo("vendorHeaderGeneratedIdentifier", vendorHeaderGeneratedId);
311 criteria.addEqualTo("vendorDetailAssignedIdentifier", vendorDetailAssignedId);
312 QueryByCriteria qbc = new QueryByCriteria(InvoiceDocument.class, criteria);
313 return this.getInvoicesByQueryByCriteria(qbc);
314 }
315
316
317
318
319
320
321 public List<InvoiceDocument> getActiveInvoicesByPOIdInvoiceAmountInvoiceDate(Integer poId, KualiDecimal vendorInvoiceAmount, Date invoiceDate) {
322 LOG.debug("getActiveInvoicesByVendorNumberInvoiceNumber() started");
323 Criteria criteria = new Criteria();
324 criteria.addEqualTo("purchaseOrderIdentifier", poId);
325 criteria.addEqualTo("vendorInvoiceAmount", vendorInvoiceAmount);
326 criteria.addEqualTo("invoiceDate", invoiceDate);
327 QueryByCriteria qbc = new QueryByCriteria(InvoiceDocument.class, criteria);
328 return this.getInvoicesByQueryByCriteria(qbc);
329 }
330
331 public List<String> getActiveInvoiceDocumentNumbersForPurchaseOrder(Integer purchaseOrderId) {
332 LOG.debug("getActiveInvoicesByVendorNumberInvoiceNumber() started");
333
334 List<String> returnList = new ArrayList<String>();
335 Criteria criteria = new Criteria();
336
337 criteria.addEqualTo(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, purchaseOrderId);
338 returnList = getDocumentNumbersOfInvoiceByCriteria(criteria, false);
339
340 return returnList;
341 }
342
343 public List<String> getInvoiceInReceivingStatus() {
344 Criteria criteria = new Criteria();
345 criteria.addNotEqualTo("holdIndicator", "Y");
346 criteria.addNotEqualTo("invoiceCancelIndicator", "Y");
347
348 List<String> returnList = new ArrayList<String>();
349 returnList = getDocumentNumbersOfInvoiceByCriteria(criteria, false);
350
351 return returnList;
352
353 }
354 }
355