package com.reallinkbilling.download;


import static java.awt.Color.WHITE;
import static org.apache.pdfbox.pdmodel.font.PDType1Font.HELVETICA;
import static org.vandeseer.easytable.settings.HorizontalAlignment.CENTER;
import static org.vandeseer.easytable.settings.HorizontalAlignment.RIGHT;
import static org.vandeseer.easytable.settings.VerticalAlignment.BOTTOM;
import static org.vandeseer.easytable.settings.HorizontalAlignment.LEFT;

import java.awt.Color;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;
import org.vandeseer.easytable.TableDrawer;
import org.vandeseer.easytable.structure.Row;
import org.vandeseer.easytable.structure.Table;
import org.vandeseer.easytable.structure.Table.TableBuilder;
import org.vandeseer.easytable.structure.cell.TextCell;

import com.opensymphony.xwork2.ActionSupport;
import com.reallinkbilling.cipher.EncryptionDecryption;
import com.reallinkbilling.db.GetDBConnection;
import com.reallinkbilling.utils.GetAccountListForDrCr;
import com.reallinkbilling.utils.NumberToWordsConverter;

public class DownloadAccountCreditDebitEntry extends ActionSupport implements SessionAware {

    private static final long serialVersionUID = -488761454485401478L;

    private HttpSession session;

    public HttpServletRequest request;
    public HttpServletResponse response;

    private SessionMap<String, Object> sessionMap;

    private String entryId;
    private String size;
    private String lang;
    private String entryDate;
    private String amount;
    private String entryDetails;
    private String accountId;
    private String accountName;

    Map<String, String> accountList = new LinkedHashMap<String, String>();

    private String typeOfPayment;
    private String entryType;

    private final static Color BLUE_DARK = new Color(76, 129, 190);
    private final static Color BLUE_LIGHT_1 = new Color(186, 206, 230);
    private final static Color BLUE_LIGHT_2 = new Color(218, 230, 242);

    private final static Color GRAY_LIGHT_1 = new Color(245, 245, 245);
    private final static Color GRAY_LIGHT_2 = new Color(240, 240, 240);
    private final static Color GRAY_LIGHT_3 = new Color(216, 216, 216);

    private static final float PADDING10 = 10f;
    private static final float PADDING20 = 20f;
    private static final float PADDING30 = 30f;
    private static final float PADDING40 = 40f;
    private static final float PADDING50 = 50f;


    public void download() {

        this.setSession(ServletActionContext.getRequest().getSession(false));

        if (this.getSession() == null
                || this.getSession().getAttribute("login") == null) {

            addActionMessage("Please Login First !!!");

        } else {

            this.setAccountList(
                    GetAccountListForDrCr.getAccountList(this.getSessionMap())
            );

            Date d = new Date();

            SimpleDateFormat dateFormat =
                    new SimpleDateFormat("dd-MM-yyyy");

            String today = dateFormat.format(d);

            java.util.Date date = new java.util.Date();

            long t = date.getTime();

            java.sql.Time time = new java.sql.Time(t);


            /*
             * ============================================================
             * EASYSBILL SESSION VALUES
             * ============================================================
             */

            String shopId =
                    (String) this.getSessionMap().get("shopId");

            String shopCode =
                    (String) this.getSessionMap().get("shopCode");


            Connection connection = null;
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;


            /*
             * ============================================================
             * DECRYPT ENTRY ID
             * ============================================================
             */

            String accEntryId =
                    EncryptionDecryption.decrypt(this.getEntryId());


            String sql = "";

            String db =
                    (String) this.getSessionMap().get("db");


            try {

                connection =
                        GetDBConnection.getDBConnection(db);


                /*
                 * ========================================================
                 * GET ACCOUNT ENTRY
                 * ========================================================
                 */

                sql =
                        "SELECT "
                        + "account_id,"
                        + "account_number,"
                        + "DATE_FORMAT(entry_date,'%d-%m-%Y'),"
                        + "entry_type,"
                        + "amount,"
                        + "payment_type,"
                        + "entry_details "
                        + "FROM "
                        + "bank_account_credit_debit_master "
                        + "WHERE "
                        + "entry_id = ? "
                        + "AND "
                        + "shop_id = ? "
                        + "AND "
                        + "shop_code = ?";


                preparedStatement =
                        connection.prepareStatement(sql);


                preparedStatement.setString(1, accEntryId);
                preparedStatement.setString(2, shopId);
                preparedStatement.setString(3, shopCode);


                resultSet =
                        preparedStatement.executeQuery();


                if (resultSet.next()) {

                    this.setAccountId(
                            resultSet.getString(1)
                    );

                    this.setAccountName(
                            accountList.get(this.getAccountId())
                    );

                    this.setEntryDate(
                            resultSet.getString(3)
                    );

                    this.setEntryType(
                            resultSet.getString(4)
                    );

                    this.setAmount(
                            resultSet.getString(5)
                    );

                    this.setTypeOfPayment(
                            resultSet.getString(6)
                    );

                    this.setEntryDetails(
                            resultSet.getString(7)
                    );

                } else {

                    addActionError(
                            this.getText(
                                    "message.error.urlTemperedInformationNotFound"
                            )
                    );

                    return;

                }


                /*
                 * ========================================================
                 * ENGLISH A4 PDF
                 * ========================================================
                 */

                if (this.getLang().equalsIgnoreCase("eng")
                        && this.getSize().equals("A4")) {


                    PDDocument document =
                            new PDDocument();


                    PDPage page =
                            new PDPage(PDRectangle.A4);


                    document.addPage(page);


                    response =
                            ServletActionContext.getResponse();

                    request =
                            ServletActionContext.getRequest();


                    String path =
                            request.getSession()
                                   .getServletContext()
                                   .getRealPath("/images/");


                    /*
                     * ====================================================
                     * EASYSBILL SHOP DETAILS
                     * ====================================================
                     *
                     * IMPORTANT:
                     * These session key names must match your EasySBill
                     * login/session code.
                     */

                    String shopName =
                            (String) this.getSessionMap()
                                        .get("shopnameenglish");

                    String address =
                            (String) this.getSessionMap()
                                        .get("shoponelineaddress");

                    String shopCustCareNumber =
                            (String) this.getSessionMap()
                                        .get("shopcustcarenumber");


                    /*
                     * ====================================================
                     * PDF CONTENT STREAM
                     * ====================================================
                     */

                    PDPageContentStream contentStream =
                            new PDPageContentStream(
                                    document,
                                    page,
                                    AppendMode.APPEND,
                                    true
                            );


                    Table printedOn;
                    Table header;
                    Table paymentTable;
                    Table paymentTable2;
                    Table signature;


                    /*
                     * ====================================================
                     * PRINTED ON
                     * ====================================================
                     */

                    TableBuilder tableprintedOnBuilder =
                            Table.builder()
                                 .addColumnsOfWidth(550)
                                 .font(HELVETICA)
                                 .borderColor(Color.BLACK)
                                 .horizontalAlignment(CENTER);


                    Row printedOnDetailsRow =
                            Row.builder()
                                .add(
                                    TextCell.builder()
                                        .text(
                                            "Printed On : "
                                            + today
                                            + " "
                                            + time
                                        )
                                        .borderWidthBottom(0F)
                                        .horizontalAlignment(RIGHT)
                                        .build()
                                )
                                .backgroundColor(WHITE)
                                .textColor(Color.BLACK)
                                .font(HELVETICA)
                                .fontSize(8)
                                .horizontalAlignment(CENTER)
                                .build();


                    tableprintedOnBuilder.addRow(
                            printedOnDetailsRow
                    );


                    printedOn =
                            tableprintedOnBuilder.build();


                    /*
                     * ====================================================
                     * HEADER
                     * ====================================================
                     */

                    TableBuilder tableHeaderBuilder =
                            Table.builder()
                                 .addColumnsOfWidth(550)
                                 .font(HELVETICA)
                                 .borderColor(Color.BLACK)
                                 .horizontalAlignment(CENTER);


                    Row paymentDetailsRow =
                            Row.builder()
                                .add(
                                    TextCell.builder()
                                        .text(
                                            "Account Credit/Debit Entry Receipt"
                                        )
                                        .paddingTop(5F)
                                        .paddingBottom(5F)
                                        .borderWidthBottom(0.5F)
                                        .horizontalAlignment(CENTER)
                                        .build()
                                )
                                .backgroundColor(WHITE)
                                .textColor(Color.BLACK)
                                .font(
                                    PDType1Font.HELVETICA_BOLD
                                )
                                .fontSize(10)
                                .horizontalAlignment(CENTER)
                                .build();


                    Row headerName =
                            Row.builder()
                                .add(
                                    TextCell.builder()
                                        .text(shopName)
                                        .horizontalAlignment(CENTER)
                                        .paddingTop(5F)
                                        .borderWidth(0)
                                        .lineSpacing(10F)
                                        .build()
                                )
                                .backgroundColor(WHITE)
                                .textColor(Color.BLACK)
                                .font(
                                    PDType1Font.HELVETICA_BOLD
                                )
                                .fontSize(20)
                                .horizontalAlignment(CENTER)
                                .build();


                    Row addressRow =
                            Row.builder()
                                .add(
                                    TextCell.builder()
                                        .text(address)
                                        .borderWidthBottom(0F)
                                        .horizontalAlignment(CENTER)
                                        .build()
                                )
                                .backgroundColor(WHITE)
                                .textColor(Color.BLACK)
                                .font(
                                    PDType1Font.HELVETICA_BOLD
                                )
                                .fontSize(9)
                                .horizontalAlignment(CENTER)
                                .build();


                    Row mobileNoRow =
                            Row.builder()
                                .add(
                                    TextCell.builder()
                                        .text(
                                            "Contact : "
                                            + shopCustCareNumber
                                        )
                                        .borderWidthBottom(0.5F)
                                        .paddingBottom(5F)
                                        .horizontalAlignment(CENTER)
                                        .build()
                                )
                                .backgroundColor(WHITE)
                                .textColor(Color.BLACK)
                                .font(
                                    PDType1Font.HELVETICA_BOLD
                                )
                                .fontSize(9)
                                .horizontalAlignment(CENTER)
                                .build();


                    tableHeaderBuilder.addRow(
                            paymentDetailsRow
                    );

                    tableHeaderBuilder.addRow(
                            headerName
                    );

                    tableHeaderBuilder.addRow(
                            addressRow
                    );

                    tableHeaderBuilder.addRow(
                            mobileNoRow
                    );


                    header =
                            tableHeaderBuilder.build();


                    /*
                     * ====================================================
                     * ENTRY DATE / ENTRY NUMBER
                     * ====================================================
                     */

                    TableBuilder paymentDetails =
                            Table.builder()
                                 .addColumnsOfWidth(270, 280)
                                 .font(HELVETICA)
                                 .borderWidth(0.5F)
                                 .borderColor(Color.BLACK);


                    Row receiptNoDateRow1 =
                            Row.builder()

                                .add(
                                    TextCell.builder()
                                        .text(
                                            "Entry No : "
                                            + accEntryId
                                        )
                                        .horizontalAlignment(LEFT)
                                        .fontSize(11)
                                        .lineSpacing(0.5F)
                                        .borderWidthTop(0.5F)
                                        .borderWidthBottom(1.5F)
                                        .borderWidthLeft(0.5F)
                                        .borderWidthRight(0.0F)
                                        .paddingTop(5F)
                                        .paddingBottom(5F)
                                        .font(
                                            PDType1Font.HELVETICA_BOLD
                                        )
                                        .textColor(Color.BLACK)
                                        .backgroundColor(WHITE)
                                        .build()
                                )

                                .add(
                                    TextCell.builder()
                                        .text(
                                            "Entry Date : "
                                            + this.getEntryDate()
                                        )
                                        .horizontalAlignment(RIGHT)
                                        .fontSize(11)
                                        .lineSpacing(0.5F)
                                        .borderWidthTop(0.5F)
                                        .borderWidthBottom(1.5F)
                                        .borderWidthLeft(0.0F)
                                        .borderWidthRight(0.5F)
                                        .paddingTop(5F)
                                        .paddingBottom(5F)
                                        .font(
                                            PDType1Font.HELVETICA_BOLD
                                        )
                                        .textColor(Color.BLACK)
                                        .backgroundColor(WHITE)
                                        .build()
                                )

                                .build();


                    paymentDetails.addRow(
                            receiptNoDateRow1
                    );


                    paymentTable =
                            paymentDetails.build();


                    /*
                     * ====================================================
                     * AMOUNT IN WORDS
                     * ====================================================
                     */

                    Float totalAmounttoConvert =
                            Float.parseFloat(
                                    this.getAmount()
                            );


                    DecimalFormat dfConvert =
                            new DecimalFormat("##.##");


                    String convertedAmount =
                            NumberToWordsConverter.getConverted(
                                    dfConvert.format(
                                            totalAmounttoConvert
                                    )
                            );


                    /*
                     * ====================================================
                     * PAYMENT DETAILS TABLE
                     * ====================================================
                     */

                    TableBuilder paymentDetails2 =
                            Table.builder()
                                 .addColumnsOfWidth(200, 350)
                                 .font(HELVETICA)
                                 .borderWidth(1)
                                 .borderColor(Color.BLACK);


                    /*
                     * ENTRY TYPE
                     */

                    Row customerDetailsRow3 =
                            Row.builder()

                                .add(
                                    TextCell.builder()
                                        .text("Entry Type ")
                                        .horizontalAlignment(LEFT)
                                        .fontSize(11)
                                        .borderWidth(0.5F)
                                        .lineSpacing(0.5F)
                                        .paddingTop(8F)
                                        .paddingBottom(8F)
                                        .font(
                                            PDType1Font.HELVETICA
                                        )
                                        .textColor(Color.BLACK)
                                        .backgroundColor(WHITE)
                                        .build()
                                )

                                .add(
                                    TextCell.builder()
                                        .text(
                                            this.getEntryType()
                                        )
                                        .horizontalAlignment(LEFT)
                                        .fontSize(11)
                                        .borderWidth(0.5F)
                                        .lineSpacing(0.5F)
                                        .paddingTop(8F)
                                        .paddingBottom(8F)
                                        .font(
                                            PDType1Font.HELVETICA_BOLD
                                        )
                                        .textColor(Color.BLACK)
                                        .backgroundColor(WHITE)
                                        .build()
                                )

                                .horizontalAlignment(CENTER)
                                .build();


                    /*
                     * ACCOUNT
                     */

                    Row customerDetailsRow2 =
                            Row.builder()

                                .add(
                                    TextCell.builder()
                                        .text("Account ")
                                        .horizontalAlignment(LEFT)
                                        .fontSize(11)
                                        .borderWidth(0.5F)
                                        .lineSpacing(0.5F)
                                        .paddingTop(8F)
                                        .paddingBottom(8F)
                                        .font(
                                            PDType1Font.HELVETICA
                                        )
                                        .textColor(Color.BLACK)
                                        .backgroundColor(WHITE)
                                        .build()
                                )

                                .add(
                                    TextCell.builder()
                                        .text(
                                            this.getAccountName()
                                        )
                                        .horizontalAlignment(LEFT)
                                        .fontSize(11)
                                        .borderWidth(0.5F)
                                        .lineSpacing(0.5F)
                                        .paddingTop(8F)
                                        .paddingBottom(8F)
                                        .font(
                                            PDType1Font.HELVETICA_BOLD
                                        )
                                        .textColor(Color.BLACK)
                                        .backgroundColor(WHITE)
                                        .build()
                                )

                                .horizontalAlignment(CENTER)
                                .build();


                    /*
                     * AMOUNT IN WORDS
                     */

                    Row amountRow4 =
                            Row.builder()

                                .add(
                                    TextCell.builder()
                                        .text(
                                            "Sum Of Rupees, "
                                        )
                                        .horizontalAlignment(LEFT)
                                        .fontSize(11)
                                        .borderWidth(0.5F)
                                        .lineSpacing(0.5F)
                                        .paddingTop(8F)
                                        .paddingBottom(8F)
                                        .font(
                                            PDType1Font.HELVETICA
                                        )
                                        .textColor(Color.BLACK)
                                        .backgroundColor(WHITE)
                                        .build()
                                )

                                .add(
                                    TextCell.builder()
                                        .text(
                                            convertedAmount
                                        )
                                        .horizontalAlignment(LEFT)
                                        .fontSize(11)
                                        .borderWidth(0.5F)
                                        .lineSpacing(0.5F)
                                        .paddingTop(8F)
                                        .paddingBottom(8F)
                                        .font(
                                            PDType1Font.HELVETICA_BOLD
                                        )
                                        .textColor(Color.BLACK)
                                        .backgroundColor(WHITE)
                                        .build()
                                )

                                .horizontalAlignment(CENTER)
                                .build();


                    /*
                     * AMOUNT
                     */

                    Row byCashRow5 =
                            Row.builder()

                                .add(
                                    TextCell.builder()
                                        .text("Amount, ")
                                        .horizontalAlignment(LEFT)
                                        .fontSize(11)
                                        .borderWidth(0.5F)
                                        .lineSpacing(0.5F)
                                        .paddingTop(8F)
                                        .paddingBottom(8F)
                                        .font(
                                            PDType1Font.HELVETICA
                                        )
                                        .textColor(Color.BLACK)
                                        .backgroundColor(WHITE)
                                        .build()
                                )

                                .add(
                                    TextCell.builder()
                                        .text(
                                            this.getAmount()
                                            + "/-"
                                        )
                                        .horizontalAlignment(LEFT)
                                        .fontSize(11)
                                        .borderWidth(0.5F)
                                        .lineSpacing(0.5F)
                                        .paddingTop(8F)
                                        .paddingBottom(8F)
                                        .font(
                                            PDType1Font.HELVETICA_BOLD
                                        )
                                        .textColor(Color.BLACK)
                                        .backgroundColor(WHITE)
                                        .build()
                                )

                                .horizontalAlignment(CENTER)
                                .build();


                    /*
                     * TYPE OF PAYMENT
                     */

                    Row byCashRow6 =
                            Row.builder()

                                .add(
                                    TextCell.builder()
                                        .text(
                                            "Type Of Payment "
                                        )
                                        .horizontalAlignment(LEFT)
                                        .fontSize(11)
                                        .borderWidth(0.5F)
                                        .lineSpacing(0.5F)
                                        .paddingTop(8F)
                                        .paddingBottom(8F)
                                        .font(
                                            PDType1Font.HELVETICA
                                        )
                                        .textColor(Color.BLACK)
                                        .backgroundColor(WHITE)
                                        .build()
                                )

                                .add(
                                    TextCell.builder()
                                        .text(
                                            this.getTypeOfPayment()
                                        )
                                        .horizontalAlignment(LEFT)
                                        .fontSize(11)
                                        .borderWidth(0.5F)
                                        .lineSpacing(0.5F)
                                        .paddingTop(8F)
                                        .paddingBottom(8F)
                                        .font(
                                            PDType1Font.HELVETICA_BOLD
                                        )
                                        .textColor(Color.BLACK)
                                        .backgroundColor(WHITE)
                                        .build()
                                )

                                .horizontalAlignment(CENTER)
                                .build();


                    /*
                     * ENTRY DETAILS
                     */

                    Row byCashRow7 =
                            Row.builder()

                                .add(
                                    TextCell.builder()
                                        .text(
                                            "Entry Details "
                                        )
                                        .horizontalAlignment(LEFT)
                                        .fontSize(11)
                                        .borderWidth(0.5F)
                                        .lineSpacing(0.5F)
                                        .paddingTop(8F)
                                        .paddingBottom(8F)
                                        .font(
                                            PDType1Font.HELVETICA
                                        )
                                        .textColor(Color.BLACK)
                                        .backgroundColor(WHITE)
                                        .build()
                                )

                                .add(
                                    TextCell.builder()
                                        .text(
                                            this.getEntryDetails()
                                        )
                                        .horizontalAlignment(LEFT)
                                        .fontSize(11)
                                        .borderWidth(0.5F)
                                        .lineSpacing(0.5F)
                                        .paddingTop(8F)
                                        .paddingBottom(8F)
                                        .font(
                                            PDType1Font.HELVETICA_BOLD
                                        )
                                        .textColor(Color.BLACK)
                                        .backgroundColor(WHITE)
                                        .build()
                                )

                                .horizontalAlignment(CENTER)
                                .build();


                    paymentDetails2.addRow(
                            customerDetailsRow3
                    );

                    paymentDetails2.addRow(
                            customerDetailsRow2
                    );

                    paymentDetails2.addRow(
                            amountRow4
                    );

                    paymentDetails2.addRow(
                            byCashRow5
                    );

                    paymentDetails2.addRow(
                            byCashRow6
                    );

                    paymentDetails2.addRow(
                            byCashRow7
                    );


                    paymentTable2 =
                            paymentDetails2.build();


                    /*
                     * ====================================================
                     * SIGNATURE
                     * ====================================================
                     */

                    TableBuilder signatureBuilder =
                            Table.builder()
                                 .addColumnsOfWidth(
                                         200,
                                         100,
                                         200
                                 )
                                 .font(HELVETICA)
                                 .borderColor(Color.BLACK)
                                 .horizontalAlignment(CENTER);


                    Row shopNameRow =
                            Row.builder()

                                .add(
                                    TextCell.builder()
                                        .text("")
                                        .borderWidth(0)
                                        .build()
                                )

                                .add(
                                    TextCell.builder()
                                        .text("")
                                        .borderWidth(0)
                                        .build()
                                )

                                .add(
                                    TextCell.builder()
                                        .text(shopName)
                                        .verticalAlignment(BOTTOM)
                                        .horizontalAlignment(RIGHT)
                                        .borderWidth(0)
                                        .lineSpacing(0.5F)
                                        .build()
                                )

                                .backgroundColor(Color.WHITE)
                                .textColor(Color.BLACK)
                                .font(
                                    PDType1Font.HELVETICA_BOLD
                                )
                                .fontSize(10)
                                .horizontalAlignment(CENTER)
                                .height(20F)
                                .build();


                    Row signRow =
                            Row.builder()

                                .add(
                                    TextCell.builder()
                                        .text("Receiver Signature")
                                        .verticalAlignment(BOTTOM)
                                        .horizontalAlignment(LEFT)
                                        .borderWidth(0)
                                        .lineSpacing(0.5F)
                                        .build()
                                )

                                .add(
                                    TextCell.builder()
                                        .text("")
                                        .borderWidth(0)
                                        .build()
                                )

                                .add(
                                    TextCell.builder()
                                        .text("Authorised Signature")
                                        .verticalAlignment(BOTTOM)
                                        .horizontalAlignment(RIGHT)
                                        .borderWidth(0)
                                        .lineSpacing(0.5F)
                                        .build()
                                )

                                .backgroundColor(Color.WHITE)
                                .textColor(Color.BLACK)
                                .font(
                                    PDType1Font.HELVETICA_BOLD
                                )
                                .fontSize(10)
                                .horizontalAlignment(CENTER)
                                .height(20F)
                                .build();


                    signatureBuilder.addRow(
                            shopNameRow
                    );

                    signatureBuilder.addRow(
                            signRow
                    );


                    signature =
                            signatureBuilder.build();


                    /*
                     * ====================================================
                     * DRAW FIRST RECEIPT
                     * ====================================================
                     */

                    float startY =
                            page.getMediaBox().getHeight()
                            - PADDING20;


                    TableDrawer.builder()
                               .contentStream(contentStream)
                               .table(printedOn)
                               .startX(PADDING20)
                               .startY(startY + 12)
                               .build()
                               .draw();


                    TableDrawer.builder()
                               .contentStream(contentStream)
                               .table(header)
                               .startX(PADDING20)
                               .startY(startY - 5)
                               .build()
                               .draw();


                    TableDrawer.builder()
                               .contentStream(contentStream)
                               .table(paymentTable)
                               .startX(PADDING20)
                               .startY(startY - 75)
                               .build()
                               .draw();


                    TableDrawer.builder()
                               .contentStream(contentStream)
                               .table(paymentTable2)
                               .startX(PADDING20)
                               .startY(startY - 93)
                               .build()
                               .draw();


                    TableDrawer.builder()
                               .contentStream(contentStream)
                               .table(signature)
                               .startX(PADDING20)
                               .startY(startY - 290)
                               .build()
                               .draw();


                    /*
                     * ====================================================
                     * FIRST RECEIPT BORDER
                     * ====================================================
                     */

                    float tableX = 20f;
                    float tableY = 820f;
                    float tableWidth = 550f;
                    float tableHeight = 330f;


                    contentStream.setStrokingColor(
                            Color.BLACK
                    );

                    contentStream.setLineWidth(
                            0.5f
                    );


                    contentStream.addRect(
                            tableX,
                            tableY - tableHeight,
                            tableWidth,
                            tableHeight
                    );


                    contentStream.stroke();


                    /*
                     * ====================================================
                     * SECOND RECEIPT
                     * ====================================================
                     */

                    float currectY =
                            startY - 450;


                    TableDrawer.builder()
                               .contentStream(contentStream)
                               .table(printedOn)
                               .startX(PADDING20)
                               .startY(currectY + 12)
                               .build()
                               .draw();


                    TableDrawer.builder()
                               .contentStream(contentStream)
                               .table(header)
                               .startX(PADDING20)
                               .startY(currectY - 5)
                               .build()
                               .draw();


                    TableDrawer.builder()
                               .contentStream(contentStream)
                               .table(paymentTable)
                               .startX(PADDING20)
                               .startY(currectY - 75)
                               .build()
                               .draw();


                    TableDrawer.builder()
                               .contentStream(contentStream)
                               .table(paymentTable2)
                               .startX(PADDING20)
                               .startY(currectY - 93)
                               .build()
                               .draw();


                    TableDrawer.builder()
                               .contentStream(contentStream)
                               .table(signature)
                               .startX(PADDING20)
                               .startY(currectY - 290)
                               .build()
                               .draw();


                    /*
                     * ====================================================
                     * SECOND RECEIPT BORDER
                     * ====================================================
                     */

                    tableX = 20f;
                    tableY = 370f;
                    tableWidth = 550f;
                    tableHeight = 330f;


                    contentStream.setStrokingColor(
                            Color.BLACK
                    );

                    contentStream.setLineWidth(
                            0.5f
                    );


                    contentStream.addRect(
                            tableX,
                            tableY - tableHeight,
                            tableWidth,
                            tableHeight
                    );


                    contentStream.stroke();


                    /*
                     * ====================================================
                     * CLOSE PDF
                     * ====================================================
                     */

                    contentStream.close();


                    String fileName =
                            "account_credit_debit_entry_slip_EntryDate-"
                            + this.getEntryDate()
                            + ".pdf";


                    String filePath =
                            path + "/" + fileName;


                    document.save(filePath);

                    document.close();


                    /*
                     * ====================================================
                     * SEND PDF TO BROWSER
                     * ====================================================
                     */

                    response.setHeader(
                            "Content-disposition",
                            "inline; filename = " + fileName
                    );


                    response.setContentType(
                            "application/pdf"
                    );


                    FileInputStream fileInputStream =
                            new FileInputStream(filePath);


                    PrintWriter out =
                            response.getWriter();


                    int bytes;


                    while ((bytes =
                            fileInputStream.read()) != -1) {

                        out.write(bytes);

                    }


                    fileInputStream.close();

                    out.flush();

                    out.close();

                }


            } catch (SQLException e) {

                addActionError(
                        e.getErrorCode()
                        + " || "
                        + e.getMessage()
                        + " || Collection details could not be found !"
                );


            } catch (IOException e) {

                addActionError(
                        e.getMessage()
                        + " || "
                        + e.getCause()
                        + " || PDF could not be generated !"
                );


            } catch (Exception e) {

                e.printStackTrace();

                addActionError(
                        e.getMessage()
                );


            } finally {

                if (resultSet != null) {

                    try {

                        resultSet.close();
                        resultSet = null;

                    } catch (SQLException e) {
                    }

                }


                if (preparedStatement != null) {

                    try {

                        preparedStatement.close();
                        preparedStatement = null;

                    } catch (SQLException e) {
                    }

                }


                if (connection != null) {

                    try {

                        connection.close();
                        connection = null;

                    } catch (SQLException e) {
                    }

                }

            }
        }
    }


    /*
     * ================================================================
     * GETTERS / SETTERS
     * ================================================================
     */

    public String getEntryType() {
        return entryType;
    }

    public void setEntryType(String entryType) {
        this.entryType = entryType;
    }


    public String getAccountName() {
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }


    public Map<String, String> getAccountList() {
        return accountList;
    }

    public void setAccountList(Map<String, String> accountList) {
        this.accountList = accountList;
    }


    public HttpSession getSession() {
        return session;
    }

    public void setSession(HttpSession session) {
        this.session = session;
    }


    public SessionMap<String, Object> getSessionMap() {
        return sessionMap;
    }

    public void setSessionMap(
            SessionMap<String, Object> sessionMap) {

        this.sessionMap = sessionMap;
    }


    @Override
    public void setSession(Map<String, Object> map) {

        sessionMap =
                (SessionMap<String, Object>) map;
    }


    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(
            HttpServletRequest request) {

        this.request = request;
    }


    public HttpServletResponse getResponse() {
        return response;
    }

    public void setResponse(
            HttpServletResponse response) {

        this.response = response;
    }


    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }


    public String getLang() {
        return lang;
    }

    public void setLang(String lang) {
        this.lang = lang;
    }


    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }


    public String getAccountId() {
        return accountId;
    }

    public void setAccountId(String accountId) {
        this.accountId = accountId;
    }


    public String getTypeOfPayment() {
        return typeOfPayment;
    }

    public void setTypeOfPayment(String typeOfPayment) {
        this.typeOfPayment = typeOfPayment;
    }


    public String getEntryId() {
        return entryId;
    }

    public void setEntryId(String entryId) {
        this.entryId = entryId;
    }


    public String getEntryDate() {
        return entryDate;
    }

    public void setEntryDate(String entryDate) {
        this.entryDate = entryDate;
    }


    public String getEntryDetails() {
        return entryDetails;
    }

    public void setEntryDetails(String entryDetails) {
        this.entryDetails = entryDetails;
    }

}