package custom.energypro.vinnica.gp9;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRDefaultScriptlet;
import net.sf.jasperreports.engine.JRScriptletException;
import net.sf.jasperreports.engine.data.JRMapCollectionDataSource;
import system.LangLogic;
import system.PeriodLogic;
public class GP9Scriplet extends JRDefaultScriptlet {
private static final String F_PCCODE = "PCCODE";
private static final String F_PCNAME = "PCNAME";
private static final String F_SUM = "SUMGEN";
private static final String F_VAT = "VAT";
private static final String F_SUMVAT = "SUMVAT";
private Map<Integer, Map<String, ?>> totalData;
@Override
public void beforeReportInit() throws JRScriptletException {
super.beforeReportInit();
totalData = new HashMap<Integer, Map<String, ?>>();
}
@Override
public void afterDetailEval() throws JRScriptletException {
super.afterDetailEval();
Integer pcCode = (Integer) getFieldValue(F_PCCODE);
String pcName = (String) getFieldValue(F_PCNAME);
BigDecimal sum = (BigDecimal) getFieldValue(F_SUM);
BigDecimal vat = (BigDecimal) getFieldValue(F_VAT);
BigDecimal sumvat = (BigDecimal) getFieldValue(F_SUMVAT);
@SuppressWarnings("unchecked")
Map<String, Object> pcData = (Map<String, Object>) totalData
.get(pcCode);
if (pcData == null) {
pcData = new HashMap<String, Object>();
pcData.put(F_PCCODE, pcCode);
pcData.put(F_PCNAME, pcName);
pcData.put(F_SUM, BigDecimal.ZERO);
pcData.put(F_VAT, BigDecimal.ZERO);
pcData.put(F_SUMVAT, BigDecimal.ZERO);
totalData.put(pcCode, pcData);
}
accum(pcData, F_SUM, sum);
accum(pcData, F_VAT, vat);
accum(pcData, F_SUMVAT, sumvat);
}
private void accum(Map<String, Object> pcData, String fName, BigDecimal sum) {
BigDecimal pcSum = (BigDecimal) pcData.get(fName);
pcSum = pcSum.add(sum);
pcData.put(fName, pcSum);
}
public JRDataSource getTotalDS() {
return new JRMapCollectionDataSource(totalData.values());
}
public String getMonthYearUA(int periodCode) throws JRScriptletException {
PeriodLogic pl = new PeriodLogic(
(Connection) getParameterValue("REPORT_CONNECTION"));
pl.loadPeriodByCode(periodCode);
return LangLogic.getMonthYearUA(pl.getStartDate());
}
public static Integer getVirtCustomer(Connection conn)
throws JRScriptletException {
String sql = "select cs.code from config c inner join epcustomer cs on cs.ACCOUNTNUMBER = substr(value, 1, posstr(value, '|') - 1) where c.NAME = 'Energypro.BankExtraction.VirtualCustomer' and cs.ISLASTSTATE = 1";
PreparedStatement st = null;
ResultSet rs = null;
try {
st = conn.prepareStatement(sql);
rs = st.executeQuery();
if (rs.next()) {
return rs.getInt(1);
}
return null;
} catch (SQLException e) {
throw new JRScriptletException(e.getMessage(), e);
} finally {
try {
rs.close();
} catch (SQLException e) {
}
try {
st.close();
} catch (SQLException e) {
}
}
}
}