Thare are multiple solutions to this : You may make use of popular commercial products like BusinessObjects CrystalReports or Oracle Discoverer.
Another option is to choose Open Source Jasper Reports.
This post is all about developing reports using Jasper APIs for Java. We will also talk about the iReport, the free Jasper designer tool.
Acquiring the Jasper Engine
The latest version of Jasper Reports can be downloaded from here. And here is the direct link to download jasperreports-2.0.5 which I am using currently.
Unzip the file, and add all the jar files in the lib folder to your classpath or the Java Build Path in yor IDE inorder to work with Jasper.
Designing the Report XML (JRXML)
The Jasper engine, like any other report engine uses a report defenition file, containing the report parameter defenitions and formatting instructions, to generate the report at runtime. For Jasper, this design is an XML file, known as the JRXML.
A JRXML file is devided into different sections - title, pageheader, columnheader, detail, columnfooter, pagefooter and summary - generally used to display the title and header and footer informations. Each of these sections may have one or more bands, which are used for displaying the content. Bands contain textfields-formatting instructions and textfieldexpressions-for parameter expressions.
The parameters can be passed from the calling java class. They can also come from a resultset obtained as a result of executing a query when the report is loaded. This query and its parameter set needs to be defined in the JRXML. The database connection needs to be passed as a parameter to the Report engine. The defined parameters needs to be wrapped in a java.util.Map object and passes to the engine.
Given below, is a sample JRXML file, used to generate a small Sales Bill.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport
name="BasicReport"
columnCount="1"
printOrder="Vertical"
orientation="Portrait"
pageWidth="595"
pageHeight="842"
columnWidth="555"
columnSpacing="0"
leftMargin="20"
rightMargin="20"
topMargin="30"
bottomMargin="30"
whenNoDataType="NoPages"
isTitleNewPage="false"
isSummaryNewPage="false"
isFloatColumnFooter="true">
<property name="ireport.scriptlethandling" value="0" />
<property name="ireport.encoding" value="UTF-8" />
<import value="java.util.*" />
<import value="net.sf.jasperreports.engine.*" />
<import value="net.sf.jasperreports.engine.data.*" />
<style
name="Arial_Normal"
isDefault="true"
fontName="Arial"
fontSize="12"
isBold="false"
isItalic="false"
isUnderline="false"
isStrikeThrough="false"
pdfFontName="Helvetica"
pdfEncoding="Cp1252"
isPdfEmbedded="false"
/>
<style
name="Arial_Bold"
isDefault="false"
fontName="Arial"
fontSize="12"
isBold="true"
isItalic="false"
isUnderline="false"
isStrikeThrough="false"
pdfFontName="Helvetica-Bold"
pdfEncoding="Cp1252"
isPdfEmbedded="false"
/>
<style
name="Arial_Italic"
isDefault="false"
fontName="Arial"
fontSize="12"
isBold="false"
isItalic="true"
isUnderline="false"
isStrikeThrough="false"
pdfFontName="Helvetica-Oblique"
pdfEncoding="Cp1252"
isPdfEmbedded="false"
/>
<parameter name="NetAmountText" isForPrompting="true" class="java.lang.String"/>
<parameter name="DiscountText" isForPrompting="true" class="java.lang.String"/>
<parameter name="TotalAmountText" isForPrompting="true" class="java.lang.String"/>
<parameter name="DateFieldText" isForPrompting="true" class="java.lang.String"/>
<parameter name="BuyerFieldText" isForPrompting="true" class="java.lang.String"/>
<parameter name="BillNumberFieldText" isForPrompting="true" class="java.lang.String"/>
<parameter name="ReportTitle" isForPrompting="true" class="java.lang.String"/>
<parameter name="MyBillNumber" isForPrompting="true" class="java.lang.String"/>
<parameter name="SOLDON" isForPrompting="true" class="java.lang.String"/>
<parameter name="SOLDBY" isForPrompting="true" class="java.lang.String"/>
<parameter name="BUYERNAME" isForPrompting="true" class="java.lang.String"/>
<parameter name="SALESTOTALS" isForPrompting="true" class="java.lang.Double"/>
<parameter name="DISCOUNT" isForPrompting="true" class="java.lang.Double"/>
<parameter name="NETAMOUNT" isForPrompting="true" class="java.lang.Double"/>
<parameter name="CASHAMTS" isForPrompting="true" class="java.lang.Double"/>
<parameter name="CHECKAMTS" isForPrompting="true" class="java.lang.Double"/>
<parameter name="CREDITAMTS" isForPrompting="true" class="java.lang.Double"/>
<queryString><![CDATA[select itemcode, quantitybox, salespricebox, quantitypiece, salespricepiece, salestotal from salesregister WHERE billnumber=$P{MyBillNumber}]]></queryString>
<field name="ITEMCODE" class="java.lang.String"/>
<field name="QUANTITYBOX" class="java.lang.Long"/>
<field name="SALESPRICEBOX" class="java.lang.Double"/>
<field name="QUANTITYPIECE" class="java.lang.Long"/>
<field name="SALESPRICEPIECE" class="java.lang.Double"/>
<field name="SALESTOTAL" class="java.lang.Double"/>
<background>
<band height="0" isSplitAllowed="true" >
</band>
</background>
<title>
<band height="140" isSplitAllowed="true" >
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
style="Arial_Bold"
x="20"
y="0"
width="508"
height="30"
key="textField"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Center">
<font size="15"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$P{ReportTitle}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="20"
y="40"
width="150"
height="20"
key="textField"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Left">
<font/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$P{DateFieldText}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="false" pattern="EEE, MMMMM dd yyyy, h:mm:ss a" isBlankWhenNull="false" evaluationTime="Report" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="200"
y="40"
width="300"
height="20"
key="textField"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement>
<font/>
</textElement>
<textFieldExpression class="java.util.Date"><![CDATA[new java.util.Date()]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="20"
y="70"
width="150"
height="20"
key="textField"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Left">
<font/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$P{BuyerFieldText}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="200"
y="70"
width="300"
height="20"
key="textField"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement>
<font/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$P{BUYERNAME}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="20"
y="100"
width="150"
height="20"
key="textField"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Left">
<font/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$P{BillNumberFieldText}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="200"
y="100"
width="300"
height="20"
key="textField"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement>
<font/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$P{MyBillNumber}]]></textFieldExpression>
</textField>
</band>
</title>
<pageHeader>
<band height="0" isSplitAllowed="true" >
</band>
</pageHeader>
<columnHeader>
<band height="20" isSplitAllowed="true" >
<staticText>
<reportElement
x="25"
y="0"
width="100"
height="20"
key="staticText"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Center">
<font isUnderline="true"/>
</textElement>
<text><![CDATA[Item Code]]></text>
</staticText>
<staticText>
<reportElement
x="125"
y="0"
width="100"
height="20"
key="staticText"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Center">
<font isUnderline="true"/>
</textElement>
<text><![CDATA[Boxes]]></text>
</staticText>
<staticText>
<reportElement
x="225"
y="0"
width="67"
height="20"
key="staticText"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Center">
<font isUnderline="true"/>
</textElement>
<text><![CDATA[Rs./Box]]></text>
</staticText>
<staticText>
<reportElement
x="292"
y="0"
width="72"
height="20"
key="staticText"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Center">
<font isUnderline="true"/>
</textElement>
<text><![CDATA[Pieces]]></text>
</staticText>
<staticText>
<reportElement
x="364"
y="0"
width="61"
height="20"
key="staticText"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Center">
<font isUnderline="true"/>
</textElement>
<text><![CDATA[Rs./Piece]]></text>
</staticText>
<staticText>
<reportElement
x="425"
y="0"
width="85"
height="20"
key="staticText"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Center">
<font isUnderline="true"/>
</textElement>
<text><![CDATA[Item Total]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" isSplitAllowed="true" >
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="25"
y="0"
width="100"
height="20"
key="textField"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Left">
<font/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{ITEMCODE}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="false" pattern="0" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="125"
y="0"
width="100"
height="20"
key="textField"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Center">
<font/>
</textElement>
<textFieldExpression class="java.lang.Long"><![CDATA[$F{QUANTITYBOX}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="false" pattern="0.00" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="225"
y="0"
width="67"
height="20"
key="textField"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Center">
<font/>
</textElement>
<textFieldExpression class="java.lang.Double"><![CDATA[$F{SALESPRICEBOX}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="false" pattern="0" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="292"
y="0"
width="72"
height="20"
key="textField"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Center">
<font/>
</textElement>
<textFieldExpression class="java.lang.Long"><![CDATA[$F{QUANTITYPIECE}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="false" pattern="0.00" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="364"
y="0"
width="61"
height="20"
key="textField"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Right">
<font/>
</textElement>
<textFieldExpression class="java.lang.Double"><![CDATA[$F{SALESPRICEPIECE}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="false" pattern="0.00" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="425"
y="0"
width="85"
height="20"
key="textField"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Right">
<font/>
</textElement>
<textFieldExpression class="java.lang.Double"><![CDATA[$F{SALESTOTAL}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="100" isSplitAllowed="true" >
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="292"
y="20"
width="133"
height="20"
key="textField"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Center">
<font/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$P{TotalAmountText}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="false" pattern="0.00" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="425"
y="20"
width="85"
height="20"
key="textField"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Right">
<font/>
</textElement>
<textFieldExpression class="java.lang.Double"><![CDATA[$P{SALESTOTALS}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="292"
y="50"
width="133"
height="20"
key="textField"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Center">
<font/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$P{DiscountText}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="false" pattern="0.00" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="425"
y="50"
width="85"
height="20"
key="textField"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Right">
<font/>
</textElement>
<textFieldExpression class="java.lang.Double"><![CDATA[$P{DISCOUNT}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="292"
y="80"
width="133"
height="20"
key="textField"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Center">
<font/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$P{NetAmountText}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="false" pattern="0.00" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="425"
y="80"
width="85"
height="20"
key="textField"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Right">
<font/>
</textElement>
<textFieldExpression class="java.lang.Double"><![CDATA[$P{NETAMOUNT}]]></textFieldExpression>
</textField>
</band>
</columnFooter>
<pageFooter>
<band height="15" isSplitAllowed="true" >
<staticText>
<reportElement
x="0"
y="0"
width="40"
height="15"
key="staticText"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement>
<font/>
</textElement>
<text><![CDATA[Page:]]></text>
</staticText>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="40"
y="0"
width="100"
height="15"
key="textField"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Center">
<font/>
</textElement>
<textFieldExpression class="java.lang.Integer"><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
<summary>
<band height="0" isSplitAllowed="true" >
</band>
</summary>
</jasperReport>
The XML might look huge. It is. And it's not so easy to write this XML. In fact, I generated this using another freeware called iReport, which is a designer tool for generating Jasper XML. You can drag and drop fields and change the alignments and formatting of fields and sections using this tool. The corresponding XML code gets generated dynamically. Here is a screenshot of a JRXML file in design using iReports.
I downloaded iReport version 2.0.5 from here.More about iReport and information on new releases can be found here.
Running the Report
Now you have the JRXML file at hand, which contain instructions for the Jasper engine to generate the report. How will you make Jasper engine read this XML and return you the report? This requires a bit of Java coding.
Here is the code snippet that does the job for you.
JasperDesign jasperDesign = JasperManager.loadXmlDesign(Thread.currentThread ().getContextClassLoader ().getResourceAsStream("SalesBill.JRXML"));
JasperPrint jasperPrint = JasperManager.fillReport(jasperReport,parameters, conn);
JasperExportManager.exportReportToPdfFile(jasperPrint,"SalesBill.pdf");
This report is generated as a PDF file. You may opt to generate the report in HTML, PDF, Excel or RTF formats. You may even view the report in JasperViewer, which is a Java based Report Viewer tool from Jasper.
Here is a complete listing of a struts action class that processes a sales order and generates a PDF Sales Bill.
package com.dj.sales.action;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.view.JasperViewer;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.sql.Connection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.classic.account.db.*;
import com.classic.account.util.*;
import com.classic.addr.forms.*;
import com.classic.account.constants.*;
import com.classic.purch.forms.PurchBillForm;
import com.classic.sales.forms.SalesForm;
import com.classic.sales.util.BilledItem;
import com.classic.sales.util.SaleItem;
public class AddSales extends Action
{
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception
{
SalesForm lForm = (SalesForm)form;
String txtDate = lForm.getTxtDate();
String billnumber = lForm.getBillnumber();
String soldby = lForm.getSoldby();
String salestype= lForm.getSalestype();
String oldbuyer= lForm.getOldbuyer();
String newbuyer= lForm.getNewbuyer();
double checkamt= lForm.getCheckamt();
String checkdetails= lForm.getCheckdetails();
double cashamt= lForm.getCashamt();
double creditamt= lForm.getCreditamt();
double discount = lForm.getDiscount();
double netsaleamount = lForm.getNetsaleamount();
String buyername = "";
double initcheckamt=checkamt;
double initcashamt=cashamt;
String initcheckdetails=checkdetails;
if(checkdetails==null)
checkdetails="";
if(oldbuyer==null)
oldbuyer="";
if(newbuyer==null)
newbuyer="";
if(oldbuyer.equals("Select Buyer") || oldbuyer.equals("") )
buyername = newbuyer;
else
buyername = oldbuyer;
User user = (User)request.getSession().getAttribute("AuthUser");
request.getSession().setAttribute("SalesReportGenerated","");
String userId = user.getUserId();
SaleItem item=null;
try
{
HashMap itemMap = request.getSession().getAttribute("ItemsAdded")==null? null : (HashMap)request.getSession().getAttribute("ItemsAdded");
if(itemMap != null)
{
Iterator itkeys = itemMap.keySet().iterator();
int cnt=0;
int primary = 1;
while(itkeys.hasNext())
{
//Insert Each Item into itemRegister
item=(SaleItem)itemMap.get(itkeys.next());
AccountDAO.getDAO().insertSales(AccUtil.formatYyyyMmDd(txtDate),soldby,billnumber,item.getItemcode(),item.getQuantitybox(),item.getSalespricebox(),item.getUnitpricebox(),item.getQuantitypiece(),item.getSalespricepiece(),item.getUnitpricepiece(),item.getNetamount(),item.getNetprofit(),salestype,buyername,checkamt,checkdetails,cashamt,creditamt,AccUtil.formatYyyyMmDd(new Date()),userId,primary,discount,netsaleamount);
cnt++;
if(cnt > 0)
{
checkamt=0;
cashamt=0;
creditamt=0;
discount =0;
netsaleamount =0;
checkdetails="";
primary=0;
}
}
}
//Insert Payment Details to PaymentHistory
AccountDAO.getDAO().addToPaymentHistory(billnumber, "Sales Bill", initcheckamt, initcashamt, "CR", initcheckdetails);
}
catch(Exception e)
{
lForm.reset(mapping,request);
ActionErrors actionErrors = new ActionErrors();
actionErrors.add("GlblSalesErrMsg", new ActionError("global.sales.exception.message"));
saveErrors(request,actionErrors);
return mapping.findForward("failure");
}
// First, load JasperDesign from XML and compile it into JasperReport
try
{
response.setContentType("application/pdf");
Map parameters = new HashMap();
JasperDesign jasperDesign = JasperManager.loadXmlDesign(Thread.currentThread ().getContextClassLoader ().getResourceAsStream("SalesBill.JRXML"));
//ClassLoader.getSystemClassLoader());
JasperReport jasperReport = JasperManager.compileReport(jasperDesign);
// Now, create a map of parameters to pass to the report.
parameters.put("ReportTitle", "Classic Ceramics - Sales Bill");
parameters.put("MyBillNumber", billnumber);
parameters.put("TotalAmountText", "Total Amt (Rs)");
parameters.put("DiscountText", "Discount (Rs)");
parameters.put("NetAmountText", "Net Amt (Rs)");
parameters.put("BuyerFieldText", "To M/s. ");
parameters.put("DateFieldText", "Date : ");
parameters.put("BillNumberFieldText", "Bill Number ");
//
BilledItem billedItem = AccountDAO.getDAO().getBilledEntry(billnumber);
parameters.put("SOLDON", billedItem.getSoldon());
parameters.put("SOLDBY", billedItem.getSoldby());
parameters.put("BUYERNAME", billedItem.getBuyername());
parameters.put("SALESTOTALS", new Double(billedItem.getItemtotals()));
parameters.put("DISCOUNT", new Double(billedItem.getDiscount()));
parameters.put("NETAMOUNT", new Double(billedItem.getSalestotal()));
parameters.put("CASHAMTS", new Double(billedItem.getCashamt()));
parameters.put("CHECKAMTS", new Double(billedItem.getCheckamt()));
parameters.put("CREDITAMTS", new Double(billedItem.getCreditamt()));
// Get a database connection
Connection conn = AccountDAO.getDAO().getConnection();
// Create JasperPrint using fillReport() method
JasperPrint jasperPrint = JasperManager.fillReport(jasperReport,
parameters, conn);
// Generate the PDF using JasperPrint
JasperExportManager.exportReportToPdfFile(jasperPrint,this.getServlet().getServletContext().getRealPath("/")+"/SalesBill.pdf");
request.getSession().setAttribute("SalesReportGenerated","YES");
request.getSession().setAttribute("EstimateReportGenerated","");
// Or View the report in the JasperViewer
// JasperViewer.viewReport(jasperPrint);
}
catch(Exception e){
e.printStackTrace();
}
lForm.reset(mapping,request);
request.getSession().setAttribute("ItemsAdded",null);
request.getSession().setAttribute("SessionTotal",null);
ActionErrors actionErrors = new ActionErrors();
actionErrors.add("GlblSalesErrMsg", new ActionError("global.sales.success.message"));
saveErrors(request,actionErrors);
return mapping.findForward("success");
//return null;
}
public AddSales()
{
super();
}
}
And finally, here is a screenshot of the generated report.
No comments:
Post a Comment