1 package org.kuali.ole.select.service.impl;
2
3 import org.apache.log4j.Logger;
4 import org.apache.ojb.broker.query.Criteria;
5 import org.apache.ojb.broker.query.QueryFactory;
6 import org.apache.ojb.broker.query.ReportQueryByCriteria;
7 import org.kuali.ole.select.businessobject.OleInvoiceItem;
8 import org.kuali.ole.sys.util.TransactionalServiceUtils;
9 import org.kuali.rice.core.api.util.type.KualiDecimal;
10 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
11
12 import java.math.BigDecimal;
13 import java.util.Iterator;
14
15
16
17
18 public class OLEInvoiceDaoOjb extends PlatformAwareDaoBaseOjb {
19 private static final Logger LOG = Logger.getLogger(OLEInvoiceDaoOjb.class);
20
21 public BigDecimal getInvoiceTotal(Integer purapDocumentIdentifier,String itemTypeCode){
22 BigDecimal debitResult = BigDecimal.ZERO;
23 BigDecimal creditResult = BigDecimal.ZERO;
24 Criteria criteria = new Criteria();
25 criteria.addEqualTo("purapDocumentIdentifier", purapDocumentIdentifier);
26 criteria.addEqualTo("debitItem","Y");
27 if(itemTypeCode!=null){
28 criteria.addEqualTo("itemTypeCode",itemTypeCode);
29 }
30 ReportQueryByCriteria query = QueryFactory.newReportQuery(OleInvoiceItem.class, criteria);
31 query.setAttributes(new String[] { "sum(ITM_EXTND_PRC)" });
32 Iterator results= getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
33 if (results.hasNext()) {
34 debitResult = (BigDecimal) ((Object[]) TransactionalServiceUtils.retrieveFirstAndExhaustIterator(results))[0];
35 LOG.info("debitResult" + debitResult);
36 if(debitResult==null){
37 debitResult = BigDecimal.ZERO;
38 }
39 }
40 criteria = new Criteria();
41 criteria.addEqualTo("purapDocumentIdentifier", purapDocumentIdentifier);
42 criteria.addEqualTo("debitItem","N");
43 if(itemTypeCode!=null){
44 criteria.addEqualTo("itemTypeCode",itemTypeCode);
45 }
46 query = QueryFactory.newReportQuery(OleInvoiceItem.class, criteria);
47 query.setAttributes(new String[] { "sum(ITM_EXTND_PRC)" });
48 results= getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
49 if (results.hasNext()) {
50 creditResult = (BigDecimal) ((Object[]) TransactionalServiceUtils.retrieveFirstAndExhaustIterator(results))[0];
51 LOG.info("creditResult" + creditResult);
52 if(creditResult==null){
53 creditResult = BigDecimal.ZERO;
54 }
55 }
56 return debitResult.subtract(creditResult).setScale(KualiDecimal.SCALE, KualiDecimal.ROUND_BEHAVIOR);
57 }
58
59 public BigDecimal getForeignInvoiceTotal(Integer purapDocumentIdentifier,String itemTypeCode){
60 BigDecimal debitResult = BigDecimal.ZERO;
61 BigDecimal creditResult = BigDecimal.ZERO;
62 Criteria criteria = new Criteria();
63 criteria.addEqualTo("purapDocumentIdentifier", purapDocumentIdentifier);
64 criteria.addEqualTo("debitItem","Y");
65 if(itemTypeCode!=null){
66 criteria.addEqualTo("itemTypeCode",itemTypeCode);
67 }
68 ReportQueryByCriteria query = QueryFactory.newReportQuery(OleInvoiceItem.class, criteria);
69 query.setAttributes(new String[] { "sum(ITM_INV_QTY * OLE_FOR_UNT_CST)" } );
70
71 Iterator results= getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
72 if (results.hasNext()) {
73 debitResult = (BigDecimal) ((Object[]) TransactionalServiceUtils.retrieveFirstAndExhaustIterator(results))[0];
74 LOG.info("debitResult for foreign currency" + debitResult);
75 if(debitResult==null){
76 debitResult = BigDecimal.ZERO;
77 }
78 }
79
80 criteria = new Criteria();
81 criteria.addEqualTo("purapDocumentIdentifier", purapDocumentIdentifier);
82 criteria.addEqualTo("debitItem","N");
83 if(itemTypeCode!=null){
84 criteria.addEqualTo("itemTypeCode",itemTypeCode);
85 }
86 query = QueryFactory.newReportQuery(OleInvoiceItem.class, criteria);
87 query.setAttributes(new String[] { "sum(ITM_INV_QTY * OLE_FOR_UNT_CST)" });
88 results= getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
89 if (results.hasNext()) {
90 creditResult = (BigDecimal) ((Object[]) TransactionalServiceUtils.retrieveFirstAndExhaustIterator(results))[0];
91 LOG.info("creditResult for foreign currency" + creditResult);
92 if(creditResult==null){
93 creditResult = BigDecimal.ZERO;
94 }
95 }
96
97 return debitResult.subtract(creditResult).setScale(KualiDecimal.SCALE, KualiDecimal.ROUND_BEHAVIOR);
98 }
99 }