Subversion Repositories SmartDukaan

Rev

Rev 36177 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.common.util;

import com.ibm.icu.text.RuleBasedNumberFormat;
import com.itextpdf.text.*;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.pdf.*;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.util.StringUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class PdfUtils {

    private static final Font FONT_TITLE = new Font(Font.FontFamily.HELVETICA, 18, Font.BOLD);
    private static Font FONT_NORMAL = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL);
    private static Font FONT_BOLD = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD);
    private static Font FONT_MARGIN_HEADER = new Font(Font.FontFamily.HELVETICA, 9, Font.BOLDITALIC);
    public static final String INVOICE_TITLE = "TAX INVOICE";
    public static final String DEBIT_NOTE_TITLE = "DEBIT NOTE";
    public static final String SECURITY_DEPOSIT = "SECURITY DEPOSIT RECEIPT";

    private static float[] igstWidthsWithDiscount = new float[]{.2f, 2.6f, 0.7f, .4f, 0.7f, 0.6f, .7f, .6f, 0.7f};
    private static float[] stateWidthsWithDiscount = new float[]{.2f, 2.1f, 0.7f, .3f, 0.6f, 0.5f, .7f, .6f, .6f, .7f};

    // Column widths without discount column (for Transaction.Order invoices)
    private static float[] igstWidthsNoDiscount = new float[]{.2f, 2.8f, 0.7f, .4f, 0.8f, .7f, .6f, 0.8f};
    private static float[] stateWidthsNoDiscount = new float[]{.2f, 2.3f, 0.7f, .4f, 0.7f, .7f, .6f, .6f, .8f};

    private static float[] igstWidths = new float[]{.6f, 2.6f, 0.7f, .4f, 0.7f, .7f, .6f, 0.6f, 0.9f};
    private static float[] stateWidths = new float[]{.6f, 2.1f, 0.7f, .3f, 0.6f, .7f, .5f, .6f, .5f, .6f, .8f};

    private static float[] igstWidthsCrNote = new float[]{2.6f, 0.7f, .4f, 0.7f, .7f, .6f, 0.6f, 0.9f};
    private static float[] stateWidthsCrNote = new float[]{2.1f, 0.7f, .3f, 0.6f, .7f, .5f, .6f, .5f, .6f, .8f};

    private static final Locale indianLocale = new Locale("en", "IN");
    private static final java.text.NumberFormat indianCurrencyFormat = java.text.NumberFormat.getCurrencyInstance(new Locale("en", "IN"));

    private static final Logger LOGGER = LogManager.getLogger(PdfUtils.class);

    private static final URL iconUrl = PdfUtils.class.getClassLoader().getResource("sdlogo.png");
    private static Image iconImg = null;

    static {
        try {
            iconImg = Image.getInstance(iconUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //Debit Note generation logic has been changed
    //Debit Note considers price drops so amount in debit note is current price of item.
    //From 16Nov 2019 onwards all debit notes will be as per actuall billing value, all pricedrops
    //shall be rolledback or cancelled once the debit note is generated.

    public static void generateAndWrite(List<InvoicePdfModel> pdfModels, PrinterType printerType, ByteArrayOutputStream outputStream) throws ProfitMandiBusinessException {
        if (PrinterType.A4.equals(printerType)) {
            generateAndWrite(pdfModels, outputStream);
        } else {
            if (pdfModels.size() > 1) {
            }
            if (PrinterType.W80.equals(printerType)) {
                InvoiceFormatter.getInvoice(pdfModels.get(0), outputStream, InvoiceFormatter.WIDTH_80MM);
            } else if (PrinterType.W58.equals(printerType)) {
                InvoiceFormatter.getInvoice(pdfModels.get(0), outputStream, InvoiceFormatter.WIDTH_58MM);

            }
        }
    }


    //Standard
    public static void generateAndWrite(List<InvoicePdfModel> pdfModels, ByteArrayOutputStream outputStream) {
        try {
            boolean cancelledPages = false;
            List<Integer> caneclledPageList = new ArrayList<>();
            Document document = new Document();
            document.setMargins(0, 0, 25, 0);
            PdfWriter pdfWriter = PdfWriter.getInstance(document, outputStream);
            document.open();
            for (InvoicePdfModel pdfModel : pdfModels) {
                CustomCustomer customer = pdfModel.getCustomer();
                CustomRetailer retailer = pdfModel.getRetailer();
                boolean stateGst = false;
                if (customer.getAddress().getState().equals(retailer.getAddress().getState())) {
                    stateGst = true;
                }
                List<CustomOrderItem> orderItems = pdfModel.getOrderItems();
                if (pdfModel.isCancelled()) {
                    caneclledPageList.add(1);
                    cancelledPages = true;
                } else {
                    caneclledPageList.add(0);
                }
                document.addTitle(pdfModel.getTitle());
                document.addAuthor(pdfModel.getAuther());

                String titleText = pdfModel.getTitle() != null ? pdfModel.getTitle().toUpperCase() : INVOICE_TITLE;
                Paragraph paragraphTitle = new Paragraph(titleText, FONT_TITLE);
                paragraphTitle.setAlignment(Element.ALIGN_CENTER);

                document.add(paragraphTitle);

                Rectangle rectangle = document.getPageSize();

                if (pdfModel.getIrnModel() != null) {
                    addIrnDetails(pdfModel.getIrnModel(), rectangle, document);
                }


                PdfPTable tableCustomerRetailer = new PdfPTable(2);
                tableCustomerRetailer.setWidthPercentage(90);
                tableCustomerRetailer.getDefaultCell().setBorder(Rectangle.NO_BORDER);
                PdfPCell columnCustomerInfo = new PdfPCell();
                columnCustomerInfo.addElement(new Paragraph("Customer Details", FONT_BOLD));
                columnCustomerInfo.addElement(new Paragraph(
                        StringUtils.capitalize(customer.getAddress().getName() + (customer.getAddress().getLastName() == null ? "" : " " + customer.getAddress().getLastName())), FONT_NORMAL));
                if (customer.getAddress() != null) {
                    if ((customer.getAddress().getLine1() != null && !customer.getAddress().getLine1().trim().isEmpty()) || (customer.getAddress().getLine2() != null && !customer.getAddress().getLine2().trim().isEmpty())) {
                        columnCustomerInfo.addElement(new Paragraph(StringUtils.capitalize(customer.getAddress().getLine1()) + ", " + StringUtils.capitalize(customer.getAddress().getLine2()), FONT_NORMAL));
                    }
                    if ((customer.getAddress().getCity() != null && !customer.getAddress().getCity().trim().isEmpty()) || (customer.getAddress().getState() != null && !customer.getAddress().getState().trim().isEmpty())) {
                        columnCustomerInfo.addElement(new Paragraph(StringUtils.capitalize(customer.getAddress().getCity()) + ", " + StringUtils.capitalize(customer.getAddress().getState()) + "(" + pdfModel.getCustomerAddressStateCode() + ")" + "\n" + customer.getAddress().getPinCode(), FONT_NORMAL));
                    }
                }

                columnCustomerInfo.addElement(new Paragraph("Mobile - " + customer.getAddress().getPhoneNumber(), FONT_NORMAL));
                if (customer.getGstNumber() != null && !customer.getGstNumber().isEmpty()) {
                    columnCustomerInfo.addElement(new Paragraph("GST Number - " + customer.getGstNumber(), FONT_NORMAL));
                }
                columnCustomerInfo.setBorder(Rectangle.NO_BORDER);
                PdfPCell columnRetailerInfo = new PdfPCell();
                columnRetailerInfo.addElement(new Paragraph(StringUtils.capitalize(retailer.getAddress().getName()), FONT_BOLD));
                columnRetailerInfo.addElement(new Paragraph(StringUtils.capitalize(retailer.getAddress().getLine1()) + ", " + StringUtils.capitalize(retailer.getAddress().getLine2()) + ", " + StringUtils.capitalize(retailer.getAddress().getCity()) + "-" + retailer.getAddress().getPinCode() + ", " + retailer.getAddress().getState() + "(" + (stateGst ? pdfModel.getCustomerAddressStateCode() : pdfModel.getPartnerAddressStateCode()) + ")", FONT_BOLD));
                columnRetailerInfo.addElement(new Paragraph("Contact No.- " + retailer.getAddress().getPhoneNumber(), FONT_BOLD));
                columnRetailerInfo.addElement(new Paragraph("GST NO. " + retailer.getGstNumber(), FONT_BOLD));
                columnRetailerInfo.setBorder(Rectangle.NO_BORDER);

                PdfPTable tableInvoiceDateRetailer = new PdfPTable(1);
                tableInvoiceDateRetailer.getDefaultCell().setBorder(Rectangle.NO_BORDER);

                PdfPTable tableInvoiceDate = new PdfPTable(2);
                tableInvoiceDate.getDefaultCell().setBorder(Rectangle.NO_BORDER);

                String invoiceLabel = pdfModel.getTitle() != null && pdfModel.getTitle().toLowerCase().contains("challan") ? "DC No:" : "Invoice No:";
                PdfPCell invoiceNumberKey = new PdfPCell(new Paragraph(invoiceLabel, FONT_NORMAL));
                invoiceNumberKey.setBorder(Rectangle.NO_BORDER);

                PdfPCell invoiceNumberValue = new PdfPCell(new Paragraph(pdfModel.getInvoiceNumber(), FONT_NORMAL));
                invoiceNumberValue.setBorder(Rectangle.NO_BORDER);

                PdfPCell dateKey = new PdfPCell(new Paragraph("Date:", FONT_NORMAL));
                dateKey.setBorder(Rectangle.NO_BORDER);

                PdfPCell dateValue = new PdfPCell(new Paragraph(pdfModel.getInvoiceDate(), FONT_NORMAL));
                dateValue.setBorder(Rectangle.NO_BORDER);
                tableInvoiceDate.addCell(invoiceNumberKey);
                tableInvoiceDate.addCell(invoiceNumberValue);
                tableInvoiceDate.addCell(dateKey);
                tableInvoiceDate.addCell(dateValue);
                tableInvoiceDateRetailer.addCell(tableInvoiceDate);
                tableInvoiceDateRetailer.addCell(columnRetailerInfo);

                tableCustomerRetailer.addCell(columnCustomerInfo);
                tableCustomerRetailer.addCell(tableInvoiceDateRetailer);

                boolean showDiscount = pdfModel.isShowDiscountColumn();
                PdfPTable orders = null;
                if (stateGst) {
                    if (showDiscount) {
                        orders = new PdfPTable(stateWidthsWithDiscount.length);
                        orders.setWidths(stateWidthsWithDiscount);
                    } else {
                        orders = new PdfPTable(stateWidthsNoDiscount.length);
                        orders.setWidths(stateWidthsNoDiscount);
                    }
                } else {
                    if (showDiscount) {
                        orders = new PdfPTable(igstWidthsWithDiscount.length);
                        orders.setWidths(igstWidthsWithDiscount);
                    } else {
                        orders = new PdfPTable(igstWidthsNoDiscount.length);
                        orders.setWidths(igstWidthsNoDiscount);
                    }
                }
                orders.setWidthPercentage(90);
                orders.addCell(new Paragraph("Sl", FONT_BOLD));
                orders.addCell(new Paragraph("Description", FONT_BOLD));
                orders.addCell(new Paragraph("HSN", FONT_BOLD));
                orders.addCell(new Paragraph("Qty", FONT_BOLD));
                orders.addCell(new Paragraph("Rate\n(Per pc)", FONT_BOLD));
                if (showDiscount) {
                    orders.addCell(new Paragraph("Discount", FONT_BOLD));
                }
                orders.addCell(new Paragraph("Total\nTaxable", FONT_BOLD));
                if (!stateGst) {
                    orders.addCell(new Paragraph("IGST", FONT_BOLD));
                } else {
                    orders.addCell(new Paragraph("CGST", FONT_BOLD));
                    orders.addCell(new Paragraph("SGST", FONT_BOLD));
                }
                orders.addCell(new Paragraph("Total", FONT_BOLD));

                orders.setHeaderRows(1);

                float igstTotalAmount = 0, cgstTotalAmount = 0, sgstTotalAmount = 0;
                int index = 1;
                for (CustomOrderItem orderItem : orderItems) {
                    orders.addCell(new Paragraph(String.valueOf(index++), FONT_NORMAL));
                    orders.addCell(new Paragraph(orderItem.getDescription(), FONT_NORMAL));
                    orders.addCell(new Paragraph(orderItem.getHsnCode(), FONT_NORMAL));
                    orders.addCell(new Paragraph(String.valueOf(orderItem.getQuantity()), FONT_NORMAL));

                    orders.addCell(new Paragraph(String.format("%.2f", orderItem.getRate()), FONT_NORMAL));
                    if (showDiscount) {
                        String discountText = orderItem.getDiscount() == 0 ? "-" : String.format("%.2f", orderItem.getDiscount());
                        orders.addCell(new Paragraph(discountText, FONT_NORMAL));
                    }
                    orders.addCell(new Paragraph(String.format("%.2f", orderItem.getAmount()), FONT_NORMAL));
                    if (!stateGst) {
                        orders.addCell(new Paragraph(String.format("%.2f%n(@%.0f%%)", orderItem.getIgstAmount(), orderItem.getIgstRate()), FONT_NORMAL));
                        igstTotalAmount = igstTotalAmount + orderItem.getIgstAmount();
                    } else {
                        orders.addCell(new Paragraph(String.format("%.2f%n(@%.0f%%)", orderItem.getCgstAmount(), orderItem.getCgstRate()), FONT_NORMAL));
                        orders.addCell(new Paragraph(String.format("%.2f%n(@%.0f%%)", orderItem.getSgstAmount(), orderItem.getSgstRate()), FONT_NORMAL));
                        cgstTotalAmount = cgstTotalAmount + orderItem.getCgstAmount();
                        sgstTotalAmount = sgstTotalAmount + orderItem.getSgstAmount();
                    }
                    orders.addCell(new Paragraph(String.format("%.0f", orderItem.getNetAmount()), FONT_NORMAL));
                }
                if (pdfModel.getInsurancePolicies() != null) {
                    for (CustomInsurancePolicy insurancePolicy : pdfModel.getInsurancePolicies()) {
                        orders.addCell(new Paragraph(String.valueOf(index++), FONT_NORMAL));
                        orders.addCell(new Paragraph(insurancePolicy.getDescription(), FONT_NORMAL));
                        orders.addCell(new Paragraph(insurancePolicy.getHsnCode(), FONT_NORMAL));
                        orders.addCell(new Paragraph("1", FONT_NORMAL));
                        orders.addCell(new Paragraph(String.format("%.2f", insurancePolicy.getRate()), FONT_NORMAL));
                        if (showDiscount) {
                            orders.addCell(new Paragraph("-", FONT_NORMAL));
                        }
                        orders.addCell(new Paragraph(String.format("%.2f", insurancePolicy.getRate()), FONT_NORMAL));
                        if (!stateGst) {
                            orders.addCell(new Paragraph(String.format("%.2f%n(@%.0f%%)", insurancePolicy.getIgstAmount(), insurancePolicy.getIgstRate()), FONT_NORMAL));
                            igstTotalAmount = igstTotalAmount + insurancePolicy.getIgstAmount();
                        } else {
                            orders.addCell(new Paragraph(String.format("%.2f%n(@%.0f%%)", insurancePolicy.getCgstAmount(), insurancePolicy.getCgstRate()), FONT_NORMAL));
                            orders.addCell(new Paragraph(String.format("%.2f%n(@%.0f%%)", insurancePolicy.getSgstAmount(), insurancePolicy.getSgstRate()), FONT_NORMAL));
                            cgstTotalAmount = cgstTotalAmount + insurancePolicy.getCgstAmount();
                            sgstTotalAmount = sgstTotalAmount + insurancePolicy.getSgstAmount();
                        }
                        orders.addCell(new Paragraph(String.format("%.0f", insurancePolicy.getNetAmount()), FONT_NORMAL));
                    }
                }

                iconImg.setAbsolutePosition(25, rectangle.getHeight() - 100);
                iconImg.scalePercent(30);

                document.add(iconImg);
                document.add(Chunk.NEWLINE);
                document.add(Chunk.NEWLINE);
                document.add(tableCustomerRetailer);

                document.add(Chunk.NEWLINE);
                document.add(orders);

                PdfPTable grandTotalTable = new PdfPTable(3);
                PdfPTable paymentsTable = new PdfPTable(2);
                paymentsTable.setWidthPercentage(95);
                paymentsTable.setWidths(new float[]{8f, 2f});

                if (stateGst) {
                    grandTotalTable.setWidths(new float[]{6.6f, .6f, .8f});
                } else {
                    grandTotalTable.setWidths(new float[]{6.5f, .6f, .9f});
                }
                grandTotalTable.setWidthPercentage(90);

                Paragraph grandTotalParagraph = new Paragraph("Grand total", FONT_BOLD);
                grandTotalParagraph.setIndentationRight(20);
                grandTotalTable.addCell(grandTotalParagraph);
                Paragraph rsParagraph = new Paragraph("Rs.", FONT_BOLD);
                grandTotalTable.addCell(rsParagraph);
                Paragraph amountParagraph = new Paragraph(String.format("%.2f", pdfModel.getTotalAmount()), FONT_BOLD);
                grandTotalTable.addCell(amountParagraph);

                document.add(grandTotalTable);

                PdfPTable amountInWordsTable = new PdfPTable(3);
                amountInWordsTable.setWidthPercentage(90);
                amountInWordsTable.addCell(new Paragraph("Amount in Words:", FONT_BOLD));
                if (!stateGst) {
                    amountInWordsTable.setWidths(new float[]{2, 5.1f, 0.9f});
                } else {
                    amountInWordsTable.setWidths(new float[]{2, 5.2f, 0.8f});
                }

                String amountInWords = toAmountInWords(pdfModel.getTotalAmount());
                amountInWordsTable.addCell(new Paragraph(amountInWords.toString(), FONT_BOLD));
                amountInWordsTable.addCell(new Paragraph("E & O.E", FONT_NORMAL));
                document.add(amountInWordsTable);

                if (pdfModel.getPaymentOptions() != null) {
                    PdfPTable paidAmountTable = new PdfPTable(2);
                    paidAmountTable.setWidthPercentage(90);
                    if (!stateGst) {
                        paidAmountTable.setWidths(new float[]{7.1f, 0.9f});
                    } else {
                        paidAmountTable.setWidths(new float[]{7.2f, 0.8f});
                    }
                    float totalPaidValue = 0;
                    for (CustomPaymentOption paymentOption : pdfModel.getPaymentOptions()) {
                        LOGGER.info("paymentOption - {}", paymentOption);
                        if (!"CASH DISCOUNT".equals(paymentOption.getPaymentOption())) {
                            PdfPCell cell = new PdfPCell(
                                    new Paragraph(10, "Paid Through " + paymentOption.getPaymentOption(), FONT_BOLD));
                            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                            cell.setPadding(5);
                            paidAmountTable.addCell(cell);

                            PdfPCell cell1 = new PdfPCell(new Paragraph(10, FormattingUtils.formatDecimal(paymentOption.getAmount()), FONT_BOLD));
                            cell1.setPadding(5);
                            paidAmountTable.addCell(cell1);
                            totalPaidValue += paymentOption.getAmount();
                        }
                    }
                    PdfPCell totalPaidCell = new PdfPCell(new Paragraph(10, "Total Paid", FONT_BOLD));
                    totalPaidCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    totalPaidCell.setPadding(5);
                    paidAmountTable.addCell(totalPaidCell);

                    PdfPCell totalPaidValueCell = new PdfPCell(
                            new Paragraph(10, FormattingUtils.formatDecimal(totalPaidValue), FONT_BOLD));
                    totalPaidValueCell.setPadding(5);
                    paidAmountTable.addCell(totalPaidValueCell);

                    document.add(paidAmountTable);
                }
                String docType = pdfModel.getTitle() != null && pdfModel.getTitle().toLowerCase().contains("challan") ? "Delivery Challan" : "Invoice";
                Paragraph autoGenerateParagraph = new Paragraph("Note - This is computer generated " + docType + ", no signature is required", FONT_NORMAL);
                autoGenerateParagraph.setAlignment(Element.ALIGN_CENTER);
                document.add(autoGenerateParagraph);

                if(pdfModel.getCreditTerms()!=null) {
                    Paragraph title = new Paragraph("Credit terms :-\n", FONT_BOLD);
                    title.setIndentationLeft(25);
                    title.setIndentationRight(25);
                    document.add(title);

                    StringBuffer termsBuffer = new StringBuffer();
                    int count = 0;
                    for (String creditTerm : pdfModel.getCreditTerms()) {
                        count++;
                        termsBuffer.append(count).append(". ").append(creditTerm).append(".\n");
                    }

                    Paragraph body = new Paragraph(termsBuffer.toString(), FONT_NORMAL);
                    body.setIndentationLeft(25);
                    body.setIndentationRight(25);
                    document.add(body);

                }

                if (pdfModel.getTncs() != null) {
                    StringBuffer sb = new StringBuffer();
                    for (String tnc : pdfModel.getTncs()) {
                        sb.append(tnc).append("\n");
                    }
                    Paragraph warningParagraph = new Paragraph(sb.toString(), FONT_NORMAL);
                    warningParagraph.setIndentationLeft(40);
                    document.add(Chunk.NEWLINE);
                    document.add(warningParagraph);
                }

                document.newPage();
                if (pdfModel.geteWayBillPdfModel() != null) {

                    EWayBillPDF.generateDocument(document, pdfWriter, pdfModel.geteWayBillPdfModel());
                }
            }
            document.close(); // no need to close PDFwriter?
            if (cancelledPages) {
                stampCancelled(outputStream, caneclledPageList);
            }

        } catch (DocumentException e) {
            LOGGER.error("Unable to write data to pdf file : ", e);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private static void addIrnDetails(IrnModel irnModel, Rectangle rectangle, Document document) throws IOException, DocumentException {
        PdfPTable taxTable = new PdfPTable(1);
        taxTable.setWidthPercentage(45);
        PdfPCell pdfCell = new PdfPCell();
        pdfCell.setBorder(Rectangle.NO_BORDER);
        Image img = Image.getInstance(irnModel.getQrCode().toURI().toURL());
        img.setAbsolutePosition(450f, rectangle.getHeight() - 140);
        document.add(img);
        Paragraph irnParagraph = new Paragraph("IRN No - " + irnModel.getIrnNumber(), FONT_NORMAL);
        irnParagraph.setAlignment(Element.ALIGN_LEFT);
        pdfCell.addElement(irnParagraph);

        Paragraph ackParagraph = new Paragraph("Ack No - " + irnModel.getAcknowledgeNumber(), FONT_NORMAL);
        ackParagraph.setAlignment(Element.ALIGN_LEFT);
        pdfCell.addElement(ackParagraph);

        Paragraph ackDateParagraph = new Paragraph("Ack Date - " + FormattingUtils.format(irnModel.getAcknowledgeDate()), FONT_NORMAL);
        ackDateParagraph.setAlignment(Element.ALIGN_LEFT);
        pdfCell.addElement(ackDateParagraph);
        taxTable.addCell(pdfCell);
        document.add(taxTable);
    }

    private static void addMarginSchemeSeparator(PdfPTable table, int numColumns) {
        PdfPCell separatorCell = new PdfPCell(new Paragraph("Items under Margin Scheme \u2013 Rule 32(5) of CGST Rules, 2017", FONT_MARGIN_HEADER));
        separatorCell.setColspan(numColumns);
        separatorCell.setBackgroundColor(new BaseColor(240, 240, 240));
        separatorCell.setPadding(5);
        separatorCell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(separatorCell);
    }

    private static void addMarginSchemeDeclaration(Document document, InvoicePdfModel pdfModel) throws DocumentException {
        if (pdfModel.getMarginSchemeDeclarations() != null && !pdfModel.getMarginSchemeDeclarations().isEmpty()) {
            document.add(Chunk.NEWLINE);
            Paragraph declarationTitle = new Paragraph("Margin Scheme Declaration:", FONT_BOLD);
            declarationTitle.setIndentationLeft(25);
            document.add(declarationTitle);
            for (String declaration : pdfModel.getMarginSchemeDeclarations()) {
                Paragraph declarationParagraph = new Paragraph("- " + declaration, FONT_NORMAL);
                declarationParagraph.setIndentationLeft(30);
                document.add(declarationParagraph);
            }
        }
    }

    // ── Margin Scheme Invoice Helpers ──────────────────────────────────────
    private static final BaseColor MS_BORDER   = new BaseColor(204, 204, 204);
    private static final BaseColor MS_HEADER_BG = new BaseColor(242, 242, 242);
    private static final BaseColor MS_TOTAL_BG  = new BaseColor(249, 249, 249);
    private static final BaseColor MS_SUMM_BG   = new BaseColor(242, 242, 242);

    private static final Font MS_TITLE      = new Font(Font.FontFamily.HELVETICA, 13, Font.BOLD);
    private static final Font MS_SUBTITLE   = new Font(Font.FontFamily.HELVETICA, 7.5f, Font.ITALIC, new BaseColor(51, 51, 51));
    private static final Font MS_SECTION    = new Font(Font.FontFamily.HELVETICA, 6.5f, Font.BOLD, new BaseColor(85, 85, 85));
    private static final Font MS_NORMAL     = new Font(Font.FontFamily.HELVETICA, 8, Font.NORMAL);
    private static final Font MS_BOLD       = new Font(Font.FontFamily.HELVETICA, 8, Font.BOLD);
    private static final Font MS_SMALL      = new Font(Font.FontFamily.HELVETICA, 7, Font.NORMAL, new BaseColor(68, 68, 68));
    private static final Font MS_SMALL_BOLD = new Font(Font.FontFamily.HELVETICA, 7, Font.BOLD);
    private static final Font MS_ITALIC     = new Font(Font.FontFamily.HELVETICA, 7.5f, Font.ITALIC);
    private static final Font MS_TBL_HDR    = new Font(Font.FontFamily.HELVETICA, 7, Font.BOLD);

    private static Paragraph msSpacer(float spacing) {
        Paragraph p = new Paragraph(" ");
        p.setSpacingAfter(spacing);
        p.setLeading(0f);
        return p;
    }

    private static Paragraph msRightPara(String text, Font font) {
        Paragraph p = new Paragraph(text, font);
        p.setAlignment(Element.ALIGN_RIGHT);
        return p;
    }

    private static void msAddHeader(PdfPTable table, String text, int align) {
        PdfPCell cell = new PdfPCell(new Phrase(text, MS_TBL_HDR));
        cell.setHorizontalAlignment(align);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setBackgroundColor(MS_HEADER_BG);
        cell.setBorderColor(MS_BORDER);
        cell.setPadding(4f);
        table.addCell(cell);
    }

    private static void msAddDataCell(PdfPTable table, String text, int align, boolean bold, boolean totalRow) {
        if (text == null) text = "";
        Font font = bold ? MS_BOLD : MS_NORMAL;
        PdfPCell cell = new PdfPCell();
        cell.setHorizontalAlignment(align);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setBorderColor(MS_BORDER);
        if (totalRow) cell.setBackgroundColor(MS_TOTAL_BG);
        cell.setPadding(4f);
        String[] lines = text.split("\n");
        for (String line : lines) {
            Paragraph p = new Paragraph(line, font);
            p.setAlignment(align);
            p.setLeading(11f);
            cell.addElement(p);
        }
        table.addCell(cell);
    }

    private static void msAddSummaryRow(PdfPTable table, String label, String value, boolean highlight) {
        Font lf = highlight ? MS_BOLD : MS_NORMAL;
        Font vf = highlight ? MS_BOLD : MS_NORMAL;
        BaseColor bg = highlight ? MS_SUMM_BG : BaseColor.WHITE;
        PdfPCell lc = new PdfPCell(new Phrase(label, lf));
        lc.setBorderColor(MS_BORDER);
        lc.setPadding(5f);
        lc.setBackgroundColor(bg);
        table.addCell(lc);
        PdfPCell vc = new PdfPCell(new Phrase(value, vf));
        vc.setHorizontalAlignment(Element.ALIGN_RIGHT);
        vc.setBorderColor(MS_BORDER);
        vc.setPadding(5f);
        vc.setBackgroundColor(bg);
        table.addCell(vc);
    }

    public static void generateMarginSchemeInvoice(List<InvoicePdfModel> pdfModels, ByteArrayOutputStream outputStream) {
        generateInvoiceV2(pdfModels, outputStream);
    }

    public static void generateInvoiceV2(List<InvoicePdfModel> pdfModels, ByteArrayOutputStream outputStream) {
        try {
            boolean cancelledPages = false;
            List<Integer> cancelledPageList = new ArrayList<>();
            Document document = new Document(PageSize.A4, 28, 28, 34, 34);
            PdfWriter pdfWriter = PdfWriter.getInstance(document, outputStream);
            document.open();

            for (InvoicePdfModel pdfModel : pdfModels) {
                CustomCustomer customer = pdfModel.getCustomer();
                CustomRetailer retailer = pdfModel.getRetailer();
                boolean stateGst = customer.getAddress().getState().equals(retailer.getAddress().getState());
                String stateCode = stateGst ? pdfModel.getCustomerAddressStateCode() : pdfModel.getPartnerAddressStateCode();
                List<CustomOrderItem> orderItems = pdfModel.getOrderItems();
                boolean isMargin = pdfModel.isHasMarginSchemeItems();

                if (pdfModel.isCancelled()) {
                    cancelledPageList.add(1);
                    cancelledPages = true;
                } else {
                    cancelledPageList.add(0);
                }

                // ── Header: Logo (left) + Title (center) + QR (right) ─────
                Rectangle rectangle = document.getPageSize();
                String titleText = pdfModel.getTitle() != null ? pdfModel.getTitle().toUpperCase() : "TAX INVOICE";

                PdfPTable headerBanner = new PdfPTable(3);
                headerBanner.setWidthPercentage(100);
                headerBanner.setWidths(new float[]{1.2f, 4f, 1.2f});
                headerBanner.setSpacingAfter(4f);

                // Left: Logo
                PdfPCell logoCell = new PdfPCell();
                logoCell.setBorder(Rectangle.NO_BORDER);
                logoCell.setPadding(4f);
                logoCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                Image logoImg = Image.getInstance(iconImg);
                logoImg.scalePercent(25);
                logoCell.addElement(logoImg);
                headerBanner.addCell(logoCell);

                // Center: Title + subtitle
                PdfPCell titleCell = new PdfPCell();
                titleCell.setBorder(Rectangle.NO_BORDER);
                titleCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                titleCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                Paragraph title = new Paragraph(titleText, MS_TITLE);
                title.setAlignment(Element.ALIGN_CENTER);
                titleCell.addElement(title);
                if (isMargin) {
                    Paragraph subtitle = new Paragraph(
                            "(GST Payable on Margin Scheme Basis \u2014 ITC not admissible to buyer)", MS_SUBTITLE);
                    subtitle.setAlignment(Element.ALIGN_CENTER);
                    titleCell.addElement(subtitle);
                }
                headerBanner.addCell(titleCell);

                // Right: QR code (or empty)
                PdfPCell qrCell = new PdfPCell();
                qrCell.setBorder(Rectangle.NO_BORDER);
                qrCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                qrCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                if (pdfModel.getIrnModel() != null && pdfModel.getIrnModel().getQrCode() != null) {
                    Image qrImg = Image.getInstance(pdfModel.getIrnModel().getQrCode().toURI().toURL());
                    qrImg.scaleAbsolute(80, 80);
                    qrCell.addElement(qrImg);
                }
                headerBanner.addCell(qrCell);

                document.add(headerBanner);

                // ── IRN Details (full width below header) ──────────────────
                if (pdfModel.getIrnModel() != null) {
                    PdfPTable irnTable = new PdfPTable(1);
                    irnTable.setWidthPercentage(100);
                    irnTable.setSpacingAfter(8f);

                    PdfPCell irnCell = new PdfPCell();
                    irnCell.setBorder(Rectangle.BOTTOM);
                    irnCell.setBorderColor(MS_BORDER);
                    irnCell.setPadding(3f);
                    irnCell.addElement(new Paragraph("IRN: " + pdfModel.getIrnModel().getIrnNumber(), MS_SMALL));
                    irnCell.addElement(new Paragraph("Ack No: " + pdfModel.getIrnModel().getAcknowledgeNumber(), MS_SMALL));
                    irnCell.addElement(new Paragraph("Ack Date: " + FormattingUtils.format(pdfModel.getIrnModel().getAcknowledgeDate()), MS_SMALL));
                    irnTable.addCell(irnCell);

                    document.add(irnTable);
                }

                // ── Supplier (left) + Invoice Details (right) ──────────────
                PdfPTable headerTable = new PdfPTable(2);
                headerTable.setWidthPercentage(100);
                headerTable.setWidths(new float[]{1f, 1f});
                headerTable.setSpacingAfter(8f);

                PdfPCell supplierCell = new PdfPCell();
                supplierCell.setBorder(Rectangle.NO_BORDER);
                supplierCell.setPadding(4f);
                supplierCell.setPaddingLeft(0f);
                supplierCell.addElement(new Paragraph("SUPPLIER DETAILS", MS_SECTION));
                supplierCell.addElement(msSpacer(2f));
                supplierCell.addElement(new Paragraph(StringUtils.capitalize(retailer.getAddress().getName()), MS_BOLD));
                supplierCell.addElement(new Paragraph(
                        StringUtils.capitalize(retailer.getAddress().getLine1()) + ", "
                        + StringUtils.capitalize(retailer.getAddress().getLine2()) + ", "
                        + StringUtils.capitalize(retailer.getAddress().getCity()) + " - "
                        + retailer.getAddress().getPinCode(), MS_NORMAL));
                supplierCell.addElement(new Paragraph("GSTIN: " + retailer.getGstNumber(), MS_NORMAL));
                supplierCell.addElement(new Paragraph("State: " + retailer.getAddress().getState()
                        + "  |  Code: " + stateCode, MS_NORMAL));
                headerTable.addCell(supplierCell);

                boolean isChallan = titleText.contains("CHALLAN");
                boolean isCreditNote = titleText.contains("CREDIT");
                String docName = isCreditNote ? "credit note" : (isChallan ? "delivery challan" : "invoice");
                String docLabel = isCreditNote ? "CN No:" : (isChallan ? "Challan No:" : "Invoice No:");
                String sectionLabel = isCreditNote ? "CREDIT NOTE DETAILS" : (isChallan ? "CHALLAN DETAILS" : "INVOICE DETAILS");
                PdfPCell invoiceCell = new PdfPCell();
                invoiceCell.setBorder(Rectangle.NO_BORDER);
                invoiceCell.setPadding(4f);
                invoiceCell.setPaddingRight(0f);
                invoiceCell.addElement(msRightPara(sectionLabel, MS_SECTION));
                invoiceCell.addElement(msSpacer(2f));
                invoiceCell.addElement(msRightPara(docLabel + " " + pdfModel.getInvoiceNumber(), MS_BOLD));
                invoiceCell.addElement(msRightPara("Date: " + pdfModel.getInvoiceDate(), MS_NORMAL));
                String supplyType = stateGst ? "Intra-state" : "Inter-state";
                invoiceCell.addElement(msRightPara("Place of supply: " + customer.getAddress().getState()
                        + " (" + pdfModel.getCustomerAddressStateCode() + ")", MS_NORMAL));
                invoiceCell.addElement(msRightPara("Supply type: " + supplyType, MS_NORMAL));
                headerTable.addCell(invoiceCell);

                document.add(headerTable);

                // ── Buyer Details ──────────────────────────────────────────
                PdfPTable buyerTable = new PdfPTable(1);
                buyerTable.setWidthPercentage(100);
                buyerTable.setSpacingAfter(8f);

                PdfPCell buyerCell = new PdfPCell();
                buyerCell.setBorder(Rectangle.BOX);
                buyerCell.setBorderColor(MS_BORDER);
                buyerCell.setPadding(5f);
                buyerCell.addElement(new Paragraph("BUYER DETAILS", MS_SECTION));
                buyerCell.addElement(msSpacer(2f));
                String buyerName = StringUtils.capitalize(customer.getAddress().getName()
                        + (customer.getAddress().getLastName() == null ? "" : " " + customer.getAddress().getLastName()));
                buyerCell.addElement(new Paragraph(buyerName, MS_BOLD));
                if (customer.getAddress().getLine1() != null || customer.getAddress().getLine2() != null) {
                    String addr = "";
                    if (customer.getAddress().getLine1() != null) addr += StringUtils.capitalize(customer.getAddress().getLine1());
                    if (customer.getAddress().getLine2() != null) addr += ", " + StringUtils.capitalize(customer.getAddress().getLine2());
                    buyerCell.addElement(new Paragraph(addr, MS_NORMAL));
                }
                String buyerLocation = StringUtils.capitalize(customer.getAddress().getCity()) + ", "
                        + customer.getAddress().getState() + " - " + customer.getAddress().getPinCode();
                buyerCell.addElement(new Paragraph(buyerLocation, MS_NORMAL));
                String buyerGstLine = "";
                if (customer.getGstNumber() != null && !customer.getGstNumber().isEmpty()) {
                    buyerGstLine += "GSTIN: " + customer.getGstNumber() + "  |  ";
                }
                buyerGstLine += "State: " + customer.getAddress().getState()
                        + " (Code: " + pdfModel.getCustomerAddressStateCode() + ")";
                buyerCell.addElement(new Paragraph(buyerGstLine, MS_NORMAL));
                if (customer.getAddress().getPhoneNumber() != null) {
                    buyerCell.addElement(new Paragraph("Mobile: " + customer.getAddress().getPhoneNumber(), MS_NORMAL));
                }
                buyerTable.addCell(buyerCell);
                document.add(buyerTable);

                // ── Item Table ─────────────────────────────────────────────
                boolean showOrderId = orderItems.stream().anyMatch(oi -> oi.getOrderId() != 0);
                PdfPTable itemTable;
                if (showOrderId) {
                    itemTable = new PdfPTable(10);
                    itemTable.setWidths(new float[]{0.7f, 1.4f, 3.5f, 1.2f, 0.6f, 1.8f, 1.8f, 2.4f, 1.6f, 1.6f});
                } else {
                    itemTable = new PdfPTable(9);
                    itemTable.setWidths(new float[]{0.7f, 4.9f, 1.2f, 0.6f, 1.8f, 1.8f, 2.4f, 1.6f, 1.6f});
                }
                itemTable.setWidthPercentage(100);
                itemTable.setSpacingAfter(0f);
                itemTable.setHeaderRows(1);

                msAddHeader(itemTable, "S.No.",      Element.ALIGN_CENTER);
                if (showOrderId) msAddHeader(itemTable, "Order Id", Element.ALIGN_CENTER);
                msAddHeader(itemTable, "Description of Goods", Element.ALIGN_LEFT);
                msAddHeader(itemTable, "HSN\nCode",  Element.ALIGN_CENTER);
                msAddHeader(itemTable, "Qty",        Element.ALIGN_CENTER);

                msAddHeader(itemTable, "Rate\n(Rs.)", Element.ALIGN_RIGHT);
                msAddHeader(itemTable, isMargin ? "Taxable Value\n- Margin (Rs.)" : "Taxable\nValue (Rs.)", Element.ALIGN_RIGHT);
                msAddHeader(itemTable, "Tax\nRate",             Element.ALIGN_CENTER);
                msAddHeader(itemTable, "Tax\n(Rs.)",         Element.ALIGN_RIGHT);
                msAddHeader(itemTable, "Total\n(Rs.)",       Element.ALIGN_RIGHT);

                float totalGrossSale = 0, totalTaxable = 0, totalTaxAmount = 0, totalNetAmount = 0;
                float totalCgst = 0, totalSgst = 0, totalIgst = 0;
                int index = 1;
                for (CustomOrderItem orderItem : orderItems) {
                    float taxAmount = orderItem.getCgstAmount() + orderItem.getSgstAmount() + orderItem.getIgstAmount();

                    msAddDataCell(itemTable, String.valueOf(index++),                    Element.ALIGN_CENTER, false, false);
                    if (showOrderId) msAddDataCell(itemTable, String.valueOf(orderItem.getOrderId()), Element.ALIGN_CENTER, false, false);
                    msAddDataCell(itemTable, orderItem.getDescription(),                 Element.ALIGN_LEFT,   false, false);
                    msAddDataCell(itemTable, orderItem.getHsnCode(),                     Element.ALIGN_CENTER, false, false);
                    msAddDataCell(itemTable, String.valueOf(orderItem.getQuantity()),     Element.ALIGN_CENTER, false, false);

                    if (isMargin) {
                        float grossSaleTotal = orderItem.getNetAmount() - taxAmount;
                        float grossSalePerUnit = grossSaleTotal / orderItem.getQuantity();
                        msAddDataCell(itemTable, formatIndianCurrency(grossSalePerUnit),          Element.ALIGN_RIGHT, false, false);
                        totalGrossSale += grossSaleTotal;
                    } else {
                        msAddDataCell(itemTable, formatIndianCurrency(orderItem.getRate()),       Element.ALIGN_RIGHT, false, false);
                    }
                    totalNetAmount += orderItem.getNetAmount();
                    msAddDataCell(itemTable, formatIndianCurrency(orderItem.getAmount()),         Element.ALIGN_RIGHT, false, false);
                    String rateText;
                    if (stateGst) {
                        rateText = String.format("CGST %.1f%%\n+ SGST %.1f%%", orderItem.getCgstRate(), orderItem.getSgstRate());
                    } else {
                        rateText = String.format("IGST %.1f%%", orderItem.getIgstRate());
                    }
                    msAddDataCell(itemTable, rateText,                                            Element.ALIGN_CENTER, false, false);
                    msAddDataCell(itemTable, formatIndianCurrency(taxAmount),                     Element.ALIGN_RIGHT, false, false);
                    msAddDataCell(itemTable, formatIndianCurrency(orderItem.getNetAmount()),      Element.ALIGN_RIGHT, false, false);

                    totalTaxable += orderItem.getAmount();
                    totalTaxAmount += taxAmount;
                    totalCgst += orderItem.getCgstAmount();
                    totalSgst += orderItem.getSgstAmount();
                    totalIgst += orderItem.getIgstAmount();
                }

                // Total row
                int totalPcs = 0;
                for (CustomOrderItem oi : orderItems) totalPcs += oi.getQuantity();
                int totalColSpan = showOrderId ? 5 : 4;
                PdfPCell totalSpan = new PdfPCell(new Phrase("Total " + totalPcs + " pc(s)", MS_BOLD));
                totalSpan.setColspan(totalColSpan);
                totalSpan.setHorizontalAlignment(Element.ALIGN_RIGHT);
                totalSpan.setBorderColor(MS_BORDER);
                totalSpan.setBackgroundColor(MS_TOTAL_BG);
                totalSpan.setPadding(4f);
                itemTable.addCell(totalSpan);

                msAddDataCell(itemTable, isMargin ? formatIndianCurrency(totalGrossSale) : "", Element.ALIGN_RIGHT, true, true);
                msAddDataCell(itemTable, formatIndianCurrency(totalTaxable),      Element.ALIGN_RIGHT, true, true);
                msAddDataCell(itemTable, "\u2014",                                Element.ALIGN_CENTER, false, true);
                msAddDataCell(itemTable, formatIndianCurrency(totalTaxAmount),    Element.ALIGN_RIGHT, true, true);
                msAddDataCell(itemTable, formatIndianCurrency(totalNetAmount),    Element.ALIGN_RIGHT, true, true);

                document.add(itemTable);

                // ── Bottom: Info (left) + Summary (right) + Amount in words ──
                float totalInvoiceValue = isMargin ? (totalGrossSale + totalTaxAmount) : totalNetAmount;

                PdfPTable bottomTable = new PdfPTable(2);
                bottomTable.setWidthPercentage(100);
                bottomTable.setWidths(new float[]{1.15f, 1f});
                bottomTable.setSpacingBefore(0f);
                bottomTable.setSpacingAfter(0f);

                // Left: Declaration (margin) or Tax Summary (regular)
                PdfPCell leftCell = new PdfPCell();
                leftCell.setBorder(Rectangle.BOX);
                leftCell.setBorderColor(MS_BORDER);
                leftCell.setPadding(6f);
                leftCell.setPaddingRight(8f);

                if (isMargin) {
                    leftCell.addElement(new Paragraph("Declaration", MS_SMALL_BOLD));
                    leftCell.addElement(msSpacer(3f));
                    leftCell.addElement(new Paragraph(
                            "GST is payable on margin scheme basis under Section 2(27) read with "
                            + "Rule 32(5) of CGST Rules, 2017.", MS_SMALL));
                    leftCell.addElement(msSpacer(4f));
                    leftCell.addElement(new Paragraph(
                            "The buyer is NOT eligible to claim Input Tax Credit on this invoice.", MS_SMALL_BOLD));
                }
                bottomTable.addCell(leftCell);

                // Right: Summary totals
                PdfPCell summaryCell = new PdfPCell();
                summaryCell.setBorder(Rectangle.BOX);
                summaryCell.setBorderColor(MS_BORDER);
                summaryCell.setPadding(0f);
                PdfPTable sumTable = new PdfPTable(2);
                sumTable.setWidthPercentage(100);
                sumTable.setWidths(new float[]{1.4f, 1f});

                if (isMargin) {
                    msAddSummaryRow(sumTable, "Total Selling Price", "Rs. " + formatIndianCurrency(totalGrossSale), false);
                    msAddSummaryRow(sumTable, "GST on Margin", "Rs. " + formatIndianCurrency(totalTaxAmount), false);
                } else {
                    msAddSummaryRow(sumTable, "Total Taxable Value", "Rs. " + formatIndianCurrency(totalTaxable), false);
                    msAddSummaryRow(sumTable, "Total GST", "Rs. " + formatIndianCurrency(totalTaxAmount), false);
                }
                String totalValueLabel = isCreditNote ? "Total Credit Note Value" : (isChallan ? "Total Challan Value" : "Total Invoice Value");
                msAddSummaryRow(sumTable, totalValueLabel, "Rs. " + formatIndianCurrency(totalInvoiceValue), true);
                summaryCell.addElement(sumTable);
                bottomTable.addCell(summaryCell);

                // Row 2: empty (left) + Amount in words (right) — no gap
                PdfPCell emptyLeft = new PdfPCell(new Phrase("", MS_NORMAL));
                emptyLeft.setBorder(Rectangle.NO_BORDER);
                emptyLeft.setPadding(0f);
                emptyLeft.setFixedHeight(0.1f);
                bottomTable.addCell(emptyLeft);
                PdfPCell wordsCell = new PdfPCell(new Phrase(toAmountInWords(totalInvoiceValue), MS_ITALIC));
                wordsCell.setBorder(Rectangle.BOX);
                wordsCell.setBorderColor(MS_BORDER);
                wordsCell.setPadding(4f);
                wordsCell.setBackgroundColor(MS_TOTAL_BG);
                bottomTable.addCell(wordsCell);

                document.add(bottomTable);

                // ── Payment Options ────────────────────────────────────────
                if (pdfModel.getPaymentOptions() != null) {
                    PdfPTable paidTable = new PdfPTable(2);
                    paidTable.setWidthPercentage(100);
                    paidTable.setWidths(new float[]{7f, 1f});
                    float totalPaidValue = 0;
                    for (CustomPaymentOption po : pdfModel.getPaymentOptions()) {
                        if (!"CASH DISCOUNT".equals(po.getPaymentOption())) {
                            PdfPCell lbl = new PdfPCell(new Phrase("Paid Through " + po.getPaymentOption(), MS_BOLD));
                            lbl.setHorizontalAlignment(Element.ALIGN_RIGHT);
                            lbl.setBorderColor(MS_BORDER);
                            lbl.setPadding(4f);
                            paidTable.addCell(lbl);
                            PdfPCell val = new PdfPCell(new Phrase(FormattingUtils.formatDecimal(po.getAmount()), MS_BOLD));
                            val.setHorizontalAlignment(Element.ALIGN_RIGHT);
                            val.setBorderColor(MS_BORDER);
                            val.setPadding(4f);
                            paidTable.addCell(val);
                            totalPaidValue += po.getAmount();
                        }
                    }
                    PdfPCell tpLbl = new PdfPCell(new Phrase("Total Paid", MS_BOLD));
                    tpLbl.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    tpLbl.setBorderColor(MS_BORDER);
                    tpLbl.setPadding(4f);
                    paidTable.addCell(tpLbl);
                    PdfPCell tpVal = new PdfPCell(new Phrase(FormattingUtils.formatDecimal(totalPaidValue), MS_BOLD));
                    tpVal.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    tpVal.setBorderColor(MS_BORDER);
                    tpVal.setPadding(4f);
                    paidTable.addCell(tpVal);
                    document.add(paidTable);
                }

                // ── Note + Credit Terms ────────────────────────────────────
                Paragraph note = new Paragraph("This is a computer-generated " + docName + " and does not require a physical signature.", MS_SMALL);
                note.setAlignment(Element.ALIGN_CENTER);
                note.setSpacingBefore(8f);
                note.setSpacingAfter(4f);
                document.add(note);

                if (pdfModel.getCreditTerms() != null) {
                    Paragraph ctTitle = new Paragraph("Credit terms:", MS_SMALL_BOLD);
                    ctTitle.setIndentationLeft(25);
                    document.add(ctTitle);
                    int count = 0;
                    for (String ct : pdfModel.getCreditTerms()) {
                        count++;
                        Paragraph line = new Paragraph(count + ". " + ct + ".", MS_SMALL);
                        line.setIndentationLeft(25);
                        line.setIndentationRight(25);
                        document.add(line);
                    }
                }

                document.newPage();

                // ── E-Way Bill ─────────────────────────────────────────────
                if (pdfModel.geteWayBillPdfModel() != null) {
                    EWayBillPDF.generateDocument(document, pdfWriter, pdfModel.geteWayBillPdfModel());
                }
            }

            document.close();
            if (cancelledPages) {
                stampCancelled(outputStream, cancelledPageList);
            }
        } catch (DocumentException e) {
            LOGGER.error("Unable to write data to pdf file : ", e);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void stampCancelled(ByteArrayOutputStream byteStream, List<Integer> cancelledPage) throws IOException, DocumentException {
        ByteArrayInputStream bais = new ByteArrayInputStream(byteStream.toByteArray());
        PdfReader pdfReader = new PdfReader(bais);
        int n = pdfReader.getNumberOfPages();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfStamper pdfStamper = new PdfStamper(pdfReader, baos);
        pdfStamper.setRotateContents(false);
        // text watermark
        Font f = new Font(FontFamily.HELVETICA, 30);
        Phrase p = new Phrase("My watermark (text)", f);
        URL cancelledImgUrl = PdfUtils.class.getClassLoader().getResource("cancelled.png");
        URL waterMarkImgUrl = PdfUtils.class.getClassLoader().getResource("sd1.jpg");
        Image imgCancelled = Image.getInstance(cancelledImgUrl);
        Image imgWatermark = Image.getInstance(waterMarkImgUrl);
        imgWatermark.scaleAbsolute(imgWatermark.getScaledWidth() * 2.5f, imgWatermark.getScaledHeight() * 2.5f);
        float w = imgCancelled.getScaledWidth() / 2;
        float h = imgCancelled.getScaledHeight() / 2;
        float wWaterMark = imgWatermark.getScaledWidth() / 2;
        float hWatermark = imgWatermark.getScaledHeight() / 2;
        // transparency
        PdfGState gs1 = new PdfGState();
        gs1.setFillOpacity(0.4f);
        PdfGState gs2 = new PdfGState();
        gs2.setFillOpacity(0.05f);
        // properties
        PdfContentByte over;
        Rectangle pagesize;
        float x, y;

        // loop over every page
        for (int i = 1; i <= n; i++) {
            pagesize = pdfReader.getPageSize(i);
            x = (pagesize.getLeft() + pagesize.getRight()) / 2;
            y = (pagesize.getTop() + pagesize.getBottom()) / 2;
            over = pdfStamper.getOverContent(i);
            over.saveState();
            if (cancelledPage.get(i - 1) == 1) {
                over.setGState(gs1);
                over.addImage(imgCancelled, w, 0, 0, h, x - (w / 2), y - (h / 2));
                over.restoreState();
            } else {
                over.setGState(gs2);
                over.addImage(imgWatermark, wWaterMark, 0, 0, hWatermark, x - (wWaterMark / 2), y - (hWatermark / 2));
                over.restoreState();
            }
        }
        pdfStamper.close();
        pdfReader.close();
        baos.writeTo(byteStream);

    }

    public static void generateAndWriteDebitNote(List<DebitNotePdfModel> debitNotePdfModels, OutputStream outputStream) {
        Document document = new Document();
        document.setMargins(0, 0, 25, 0);
        try {
            for (DebitNotePdfModel debitNotePdfModel : debitNotePdfModels) {

                InvoicePdfModel pdfModel = debitNotePdfModel.getPdfModel();
                CustomCustomer customer = pdfModel.getCustomer();
                CustomRetailer retailer = pdfModel.getRetailer();
                boolean stateGst = false;

                LOGGER.info("Customer - {}", customer.getAddress().getState());
                LOGGER.info("retailer - {}", retailer.getAddress().getState());


                if (customer.getAddress().getState().equals(retailer.getAddress().getState())) {
                    stateGst = true;
                }
                List<CustomOrderItem> orderItems = pdfModel.getOrderItems();

                PdfWriter.getInstance(document, outputStream);

                document.open();
                document.addTitle(pdfModel.getTitle());
                document.addAuthor(pdfModel.getAuther());

                Paragraph paragraphTitle = new Paragraph(pdfModel.getTitle(), FONT_TITLE);
                paragraphTitle.setAlignment(Element.ALIGN_CENTER);

                PdfPCell blankCell = new PdfPCell();
                blankCell.setBorder(Rectangle.NO_BORDER);
                PdfPTable tableCustomerRetailer = new PdfPTable(3);
                tableCustomerRetailer.setWidthPercentage(95);
                PdfPCell partnerInfo = new PdfPCell();
                partnerInfo.addElement(new Paragraph("From Party:", FONT_BOLD));
                partnerInfo.addElement(
                        new Paragraph(StringUtils.capitalize(customer.getAddress().getName()), FONT_NORMAL));
                partnerInfo.addElement(new Paragraph(
                        (customer.getAddress().getLine1() == null ? "" : StringUtils.capitalize(customer.getAddress().getLine1()) + ", ") + (customer.getAddress().getLine2() == null ? "" : StringUtils.capitalize(customer.getAddress().getLine2()) + ", ") + (customer.getAddress().getCity() == null ? "" : StringUtils.capitalize(customer.getAddress().getCity()) + " - ") + (customer.getAddress().getPinCode() == null ? "" : StringUtils.capitalize(customer.getAddress().getPinCode())), FONT_NORMAL));

                partnerInfo.addElement(new Paragraph(
                        StringUtils.capitalize(customer.getAddress().getState()) + "(" + pdfModel.getCustomerAddressStateCode() + ")", FONT_NORMAL));
                partnerInfo.addElement(new Paragraph("Mobile - " + customer.getMobileNumber(), FONT_NORMAL));
                if (customer.getGstNumber() != null && !customer.getGstNumber().isEmpty()) {
                    partnerInfo.addElement(new Paragraph("GST No - " + customer.getGstNumber(), FONT_BOLD));
                }

                PdfPCell sellerParty = new PdfPCell();
                sellerParty.addElement(new Paragraph("To Party:", FONT_BOLD));
                sellerParty.addElement(
                        new Paragraph(StringUtils.capitalize(retailer.getAddress().getName()), FONT_NORMAL));
                sellerParty.addElement(new Paragraph((retailer.getAddress().getLine1() == null ? "" : StringUtils.capitalize(retailer.getAddress().getLine1()) + ", ") + (retailer.getAddress().getLine2() == null ? "" : StringUtils.capitalize(retailer.getAddress().getLine2()) + ", ") + (retailer.getAddress().getCity() == null ? "" : StringUtils.capitalize(retailer.getAddress().getCity()) + "-") + (retailer.getAddress().getPinCode() == null ? "" : StringUtils.capitalize(retailer.getAddress().getPinCode())), FONT_NORMAL));
                sellerParty.addElement(new Paragraph(
                        retailer.getAddress().getState() + "(" + pdfModel.getPartnerAddressStateCode() + ")", FONT_NORMAL));
                sellerParty.addElement(new Paragraph("Mobile - " + retailer.getAddress().getPhoneNumber(), FONT_NORMAL));
                sellerParty.addElement(new Paragraph("GST No - " + retailer.getGstNumber(), FONT_BOLD));

                PdfPTable tableInvoiceDateRetailer = new PdfPTable(1);
                tableInvoiceDateRetailer.getDefaultCell().setBorder(Rectangle.NO_BORDER);

                PdfPTable tableInvoiceDate = new PdfPTable(2);
                tableInvoiceDate.getDefaultCell().setBorder(Rectangle.NO_BORDER);
                tableInvoiceDate.setWidthPercentage(90);

                PdfPCell debitNoteDetails = new PdfPCell(new Paragraph("Debit Note Details", FONT_BOLD));
                debitNoteDetails.setColspan(2);
                debitNoteDetails.setBorder(Rectangle.NO_BORDER);

                PdfPCell debitNoteNumberKey = new PdfPCell(new Paragraph("Debit Note No:", FONT_NORMAL));
                debitNoteNumberKey.setBorder(Rectangle.NO_BORDER);
                PdfPCell debitNoteNumberValue = new PdfPCell(
                        new Paragraph(debitNotePdfModel.getDebitNoteNumber(), FONT_NORMAL));
                debitNoteNumberValue.setBorder(Rectangle.NO_BORDER);

                PdfPCell debitNoteDateKey = new PdfPCell(new Paragraph("Debit Note Dt:", FONT_NORMAL));
                debitNoteDateKey.setBorder(Rectangle.NO_BORDER);

                PdfPCell debitNoteDateValue = new PdfPCell(
                        new Paragraph(debitNotePdfModel.getDebitNoteDate(), FONT_NORMAL));
                debitNoteDateValue.setBorder(Rectangle.NO_BORDER);

                PdfPCell invoiceNumberKey = new PdfPCell(new Paragraph("Invoice Ref No:", FONT_NORMAL));
                invoiceNumberKey.setBorder(Rectangle.NO_BORDER);
                PdfPCell invoiceNumberValue = new PdfPCell(new Paragraph(pdfModel.getInvoiceNumber(), FONT_NORMAL));
                invoiceNumberValue.setBorder(Rectangle.NO_BORDER);

                PdfPCell dateKey = new PdfPCell(new Paragraph("Invoice Dt:", FONT_NORMAL));
                dateKey.setBorder(Rectangle.NO_BORDER);
                PdfPCell dateValue = new PdfPCell(new Paragraph(pdfModel.getInvoiceDate(), FONT_NORMAL));
                dateValue.setBorder(Rectangle.NO_BORDER);

                tableInvoiceDate.addCell(debitNoteDetails);
                tableInvoiceDate.addCell(debitNoteNumberKey);
                tableInvoiceDate.addCell(debitNoteNumberValue);
                tableInvoiceDate.addCell(debitNoteDateKey);
                tableInvoiceDate.addCell(debitNoteDateValue);
                tableInvoiceDate.addCell(invoiceNumberKey);
                tableInvoiceDate.addCell(invoiceNumberValue);
                tableInvoiceDate.addCell(dateKey);
                tableInvoiceDate.addCell(dateValue);

                tableCustomerRetailer.addCell(partnerInfo);
                tableCustomerRetailer.addCell(tableInvoiceDate);
                tableCustomerRetailer.addCell(sellerParty);

                PdfPTable orders = null;
                if (stateGst) {
                    orders = new PdfPTable(stateWidths.length);
                    orders.setWidths(stateWidths);
                } else {
                    orders = new PdfPTable(igstWidths.length);
                    orders.setWidths(igstWidths);
                }
                orders.setWidthPercentage(95);
                orders.addCell(new Paragraph("Order Id", FONT_BOLD));
                orders.addCell(new Paragraph("Description", FONT_BOLD));
                orders.addCell(new Paragraph("HSN", FONT_BOLD));
                orders.addCell(new Paragraph("Qty", FONT_BOLD));
                orders.addCell(new Paragraph("Rate\n(Per pc)", FONT_BOLD));
                orders.addCell(new Paragraph("Total\nTaxable", FONT_BOLD));
                if (!stateGst) {
                    orders.addCell(new Paragraph("IGST\n%", FONT_BOLD));
                    orders.addCell(new Paragraph("IGST", FONT_BOLD));
                } else {
                    orders.addCell(new Paragraph("CGST %", FONT_BOLD));
                    orders.addCell(new Paragraph("CGST", FONT_BOLD));
                    orders.addCell(new Paragraph("SGST %", FONT_BOLD));
                    orders.addCell(new Paragraph("SGST", FONT_BOLD));
                }
                orders.addCell(new Paragraph("Total", FONT_BOLD));

                orders.setHeaderRows(1);

                float igstTotalAmount = 0, cgstTotalAmount = 0, sgstTotalAmount = 0;
                boolean marginSeparatorAdded = false;
                for (CustomOrderItem orderItem : orderItems) {
                    if (orderItem.isMarginScheme() && !marginSeparatorAdded) {
                        addMarginSchemeSeparator(orders, stateGst ? stateWidths.length : igstWidths.length);
                        marginSeparatorAdded = true;
                    }

                    orders.addCell(new Paragraph(String.valueOf(orderItem.getOrderId()), FONT_NORMAL));
                    orders.addCell(new Paragraph(orderItem.getDescription(), FONT_NORMAL));
                    orders.addCell(new Paragraph(orderItem.getHsnCode(), FONT_NORMAL));
                    orders.addCell(new Paragraph(String.valueOf(orderItem.getQuantity()), FONT_NORMAL));
                    orders.addCell(new Paragraph(String.format("%.2f", orderItem.getRate()), FONT_NORMAL));
                    orders.addCell(new Paragraph(String.format("%.2f", orderItem.getAmount()), FONT_NORMAL));
                    if (!stateGst) {
                        orders.addCell(new Paragraph(String.format("%.2f", orderItem.getIgstRate()), FONT_NORMAL));
                        orders.addCell(new Paragraph(String.format("%.2f", orderItem.getIgstAmount()), FONT_NORMAL));
                        igstTotalAmount = igstTotalAmount + orderItem.getIgstAmount();
                    } else {
                        orders.addCell(new Paragraph(String.format("%.2f", orderItem.getCgstRate()), FONT_NORMAL));
                        orders.addCell(new Paragraph(String.format("%.2f", orderItem.getCgstAmount()), FONT_NORMAL));
                        orders.addCell(new Paragraph(String.format("%.2f", orderItem.getSgstRate()), FONT_NORMAL));
                        orders.addCell(new Paragraph(String.format("%.2f", orderItem.getSgstAmount()), FONT_NORMAL));
                        cgstTotalAmount = cgstTotalAmount + orderItem.getCgstAmount();
                        sgstTotalAmount = sgstTotalAmount + orderItem.getSgstAmount();
                    }
                    orders.addCell(new Paragraph(String.format("%.0f", orderItem.getNetAmount()), FONT_NORMAL));
                    LOGGER.info("IN FOR LOOP");
                }

                document.add(paragraphTitle);

                document.add(Chunk.NEWLINE);
                document.add(tableCustomerRetailer);

                document.add(Chunk.NEWLINE);
                document.add(orders);

                PdfPTable grandTotalTable = new PdfPTable(3);
                if (stateGst) {
                    grandTotalTable.setWidths(new float[]{6.6f, .6f, .8f});
                } else {
                    grandTotalTable.setWidths(new float[]{6.5f, .6f, .9f});
                }
                grandTotalTable.setWidthPercentage(95);

                Paragraph grandTotalParagraph = new Paragraph("Grand total", FONT_BOLD);
                grandTotalParagraph.setIndentationRight(20);
                grandTotalTable.addCell(grandTotalParagraph);
                Paragraph rsParagraph = new Paragraph("Rs.", FONT_BOLD);
                grandTotalTable.addCell(rsParagraph);
                Paragraph amountParagraph = new Paragraph(String.format("%.2f", pdfModel.getTotalAmount()), FONT_BOLD);
                grandTotalTable.addCell(amountParagraph);

                document.add(grandTotalTable);

                PdfPTable amountInWordsTable = new PdfPTable(3);
                if (!stateGst) {
                    amountInWordsTable.setWidths(new float[]{2, 5.1f, 0.9f});
                } else {
                    amountInWordsTable.setWidths(new float[]{2, 5.2f, 0.8f});
                }
                amountInWordsTable.setWidthPercentage(95);
                amountInWordsTable.addCell(new Paragraph("Amount in Words:", FONT_BOLD));

                String amountInWords = toAmountInWords(pdfModel.getTotalAmount());
                amountInWordsTable.addCell(new Paragraph(amountInWords.toString(), FONT_BOLD));
                amountInWordsTable.addCell(new Paragraph("E & O.E", FONT_NORMAL));
                document.add(amountInWordsTable);

                // Margin scheme declaration for debit notes
                addMarginSchemeDeclaration(document, pdfModel);

                document.newPage();
            }
            document.close(); // no need to close PDFwriter?

        } catch (DocumentException e) {
            LOGGER.error("Unable to write data to pdf file : ", e);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static String toAmountInWords(float amount) {
        int rupees = (int) amount;
        int paise = Math.round((amount - rupees) * 100);
        StringBuilder sb = new StringBuilder("Rs. ");
        sb.append(StringUtils.capitalize(indianNumberToWords(rupees)));
        if (paise > 0) {
            sb.append(" and ");
            RuleBasedNumberFormat fmt = new RuleBasedNumberFormat(indianLocale, RuleBasedNumberFormat.SPELLOUT);
            sb.append(StringUtils.capitalize(fmt.format(paise)));
            sb.append(" Paise");
        } else {
            sb.append(" Only");
        }
        return sb.toString();
    }

    /**
     * Convert number to Indian English words (lakh, crore system).
     */
    private static String indianNumberToWords(int number) {
        if (number == 0) return "zero";
        String[] ones = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
                "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
                "eighteen", "nineteen"};
        String[] tens = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};

        if (number < 0) return "minus " + indianNumberToWords(-number);

        StringBuilder words = new StringBuilder();
        if (number / 10000000 > 0) {
            words.append(indianNumberToWords(number / 10000000)).append(" crore ");
            number %= 10000000;
        }
        if (number / 100000 > 0) {
            words.append(indianNumberToWords(number / 100000)).append(" lakh ");
            number %= 100000;
        }
        if (number / 1000 > 0) {
            words.append(indianNumberToWords(number / 1000)).append(" thousand ");
            number %= 1000;
        }
        if (number / 100 > 0) {
            words.append(ones[number / 100]).append(" hundred ");
            number %= 100;
        }
        if (number > 0) {
            if (words.length() > 0) words.append("and ");
            if (number < 20) {
                words.append(ones[number]);
            } else {
                words.append(tens[number / 10]);
                if (number % 10 > 0) words.append("-").append(ones[number % 10]);
            }
        }
        return words.toString().trim();
    }

    /**
     * Format amount in Indian comma style: 1,25,129.00
     */
    static String formatIndianCurrency(double amount) {
        String formatted = indianCurrencyFormat.format(amount);
        // Java 8 en_IN produces "Rs." prefix (e.g. "Rs.17,618.00", "-Rs.500.50")
        return formatted.replace("Rs.", "").replace("\u20B9", "").trim();
    }

    public static void generateAndWriteCustomerCreditNotes(List<CreditNotePdfModel> creditNotes, OutputStream outputStream) {
        Document document = new Document();
        document.setMargins(0, 0, 25, 0);
        try {
            PdfWriter.getInstance(document, outputStream);

            document.open();
            document.addTitle(creditNotes.get(0).getPdfModel().getTitle());
            document.addAuthor(creditNotes.get(0).getPdfModel().getAuther());
            for (CreditNotePdfModel creditNotePdfModel : creditNotes) {
                InvoicePdfModel pdfModel = creditNotePdfModel.getPdfModel();
                CustomCustomer customer = pdfModel.getCustomer();
                CustomRetailer retailer = pdfModel.getRetailer();
                boolean stateGst = false;

                if (customer.getAddress().getState().equals(retailer.getAddress().getState())) {
                    stateGst = true;
                }
                List<CustomOrderItem> orderItems = pdfModel.getOrderItems();

                Paragraph paragraphTitle = new Paragraph(pdfModel.getTitle(), FONT_TITLE);
                paragraphTitle.setAlignment(Element.ALIGN_CENTER);

                PdfPCell blankCell = new PdfPCell();
                blankCell.setBorder(Rectangle.NO_BORDER);
                PdfPTable tableCustomerRetailer = new PdfPTable(3);
                tableCustomerRetailer.setWidthPercentage(95);
                PdfPCell partnerInfo = new PdfPCell();
                partnerInfo.addElement(new Paragraph("To Party:", FONT_BOLD));
                partnerInfo.addElement(
                        new Paragraph(StringUtils.capitalize(customer.getAddress().getName()), FONT_NORMAL));
                partnerInfo.addElement(new Paragraph(
                        StringUtils.capitalize(customer.getAddress().getLine1()) + ", " + (customer.getAddress().getLine2() == null ? "" : StringUtils.capitalize(customer.getAddress().getLine2())) + ", " + customer.getAddress().getCity() + " - " + customer.getAddress().getPinCode(), FONT_NORMAL));

                partnerInfo.addElement(new Paragraph(
                        StringUtils.capitalize(customer.getAddress().getState()) + "(" + pdfModel.getCustomerAddressStateCode() + ")", FONT_NORMAL));
                partnerInfo.addElement(new Paragraph("Mobile - " + customer.getMobileNumber(), FONT_NORMAL));
                if (customer.getGstNumber() != null && !customer.getGstNumber().isEmpty()) {
                    partnerInfo.addElement(new Paragraph("GST No - " + customer.getGstNumber(), FONT_BOLD));
                }

                PdfPCell sellerParty = new PdfPCell();
                sellerParty.addElement(new Paragraph("From Party:", FONT_BOLD));
                sellerParty.addElement(
                        new Paragraph(StringUtils.capitalize(retailer.getAddress().getName()), FONT_NORMAL));
                sellerParty.addElement(new Paragraph((retailer.getAddress().getLine1() == null ? "" : StringUtils.capitalize(retailer.getAddress().getLine1()) + ", ") + (retailer.getAddress().getLine2() == null ? "" : StringUtils.capitalize(retailer.getAddress().getLine2()) + ", ") + StringUtils.capitalize(retailer.getAddress().getCity()) + "-" + retailer.getAddress().getPinCode() + ", ", FONT_NORMAL));
                sellerParty.addElement(new Paragraph(
                        retailer.getAddress().getState() + "(" + pdfModel.getPartnerAddressStateCode() + ")", FONT_NORMAL));
                sellerParty.addElement(new Paragraph("Mobile - " + retailer.getAddress().getPhoneNumber(), FONT_NORMAL));
                sellerParty.addElement(new Paragraph("GST No - " + retailer.getGstNumber(), FONT_BOLD));

                PdfPTable tableInvoiceDateRetailer = new PdfPTable(1);
                tableInvoiceDateRetailer.getDefaultCell().setBorder(Rectangle.NO_BORDER);

                PdfPTable tableInvoiceDate = new PdfPTable(2);
                tableInvoiceDate.getDefaultCell().setBorder(Rectangle.NO_BORDER);
                tableInvoiceDate.setWidthPercentage(90);

                PdfPCell debitNoteDetails = new PdfPCell(new Paragraph("Credit Note Details", FONT_BOLD));
                debitNoteDetails.setColspan(2);
                debitNoteDetails.setBorder(Rectangle.NO_BORDER);

                PdfPCell debitNoteNumberKey = new PdfPCell(new Paragraph("Credit Note No:", FONT_NORMAL));
                debitNoteNumberKey.setBorder(Rectangle.NO_BORDER);
                PdfPCell debitNoteNumberValue = new PdfPCell(
                        new Paragraph(creditNotePdfModel.getCreditNoteNumber(), FONT_NORMAL));
                debitNoteNumberValue.setBorder(Rectangle.NO_BORDER);

                PdfPCell debitNoteDateKey = new PdfPCell(new Paragraph("Credit Note Dt:", FONT_NORMAL));
                debitNoteDateKey.setBorder(Rectangle.NO_BORDER);

                PdfPCell debitNoteDateValue = new PdfPCell(
                        new Paragraph(creditNotePdfModel.getCreditNoteDate(), FONT_NORMAL));
                debitNoteDateValue.setBorder(Rectangle.NO_BORDER);
                tableInvoiceDate.addCell(debitNoteDetails);
                tableInvoiceDate.addCell(debitNoteNumberKey);
                tableInvoiceDate.addCell(debitNoteNumberValue);
                tableInvoiceDate.addCell(debitNoteDateKey);
                tableInvoiceDate.addCell(debitNoteDateValue);
                if (pdfModel.getInvoiceNumber() != null) {
                    PdfPCell dateKey = new PdfPCell(new Paragraph("Invoice Dt:", FONT_NORMAL));
                    dateKey.setBorder(Rectangle.NO_BORDER);
                    PdfPCell dateValue = new PdfPCell(new Paragraph(pdfModel.getInvoiceDate(), FONT_NORMAL));
                    dateValue.setBorder(Rectangle.NO_BORDER);

                    PdfPCell invoiceNumberKey = new PdfPCell(new Paragraph("Invoice Ref No:", FONT_NORMAL));
                    invoiceNumberKey.setBorder(Rectangle.NO_BORDER);
                    PdfPCell invoiceNumberValue = new PdfPCell(new Paragraph(pdfModel.getInvoiceNumber(), FONT_NORMAL));
                    invoiceNumberValue.setBorder(Rectangle.NO_BORDER);
                    tableInvoiceDate.addCell(invoiceNumberKey);
                    tableInvoiceDate.addCell(invoiceNumberValue);
                    tableInvoiceDate.addCell(dateKey);
                    tableInvoiceDate.addCell(dateValue);
                }

                tableCustomerRetailer.addCell(partnerInfo);
                tableCustomerRetailer.addCell(tableInvoiceDate);
                tableCustomerRetailer.addCell(sellerParty);

                PdfPTable orders = null;
                if (stateGst) {
                    orders = new PdfPTable(stateWidthsCrNote.length);
                    orders.setWidths(stateWidthsCrNote);
                } else {
                    orders = new PdfPTable(igstWidthsCrNote.length);
                    orders.setWidths(igstWidthsCrNote);
                }
                orders.setWidthPercentage(95);
                orders.addCell(new Paragraph("Description", FONT_BOLD));
                orders.addCell(new Paragraph("HSN", FONT_BOLD));
                orders.addCell(new Paragraph("Qty", FONT_BOLD));
                orders.addCell(new Paragraph("Rate\n(Per pc)", FONT_BOLD));
                orders.addCell(new Paragraph("Total\nTaxable", FONT_BOLD));
                if (!stateGst) {
                    orders.addCell(new Paragraph("IGST%", FONT_BOLD));
                    orders.addCell(new Paragraph("IGST", FONT_BOLD));
                } else {
                    orders.addCell(new Paragraph("CGST %", FONT_BOLD));
                    orders.addCell(new Paragraph("CGST", FONT_BOLD));
                    orders.addCell(new Paragraph("SGST %", FONT_BOLD));
                    orders.addCell(new Paragraph("SGST", FONT_BOLD));
                }
                orders.addCell(new Paragraph("Total", FONT_BOLD));

                orders.setHeaderRows(1);

                float igstTotalAmount = 0, cgstTotalAmount = 0, sgstTotalAmount = 0;
                boolean marginSeparatorAdded = false;
                for (CustomOrderItem orderItem : orderItems) {
                    if (orderItem.isMarginScheme() && !marginSeparatorAdded) {
                        addMarginSchemeSeparator(orders, stateGst ? stateWidthsCrNote.length : igstWidthsCrNote.length);
                        marginSeparatorAdded = true;
                    }

                    LOGGER.info("Custom Order Item - {}", orderItem);
                    orders.addCell(new Paragraph(orderItem.getDescription(), FONT_NORMAL));
                    orders.addCell(new Paragraph(orderItem.getHsnCode(), FONT_NORMAL));
                    orders.addCell(new Paragraph(String.valueOf(orderItem.getQuantity()), FONT_NORMAL));
                    orders.addCell(new Paragraph(String.format("%.2f", orderItem.getRate()), FONT_NORMAL));
                    orders.addCell(new Paragraph(String.format("%.2f", orderItem.getAmount()), FONT_NORMAL));
                    if (!stateGst) {
                        orders.addCell(new Paragraph(String.format("%.2f", orderItem.getIgstRate()), FONT_NORMAL));
                        orders.addCell(new Paragraph(String.format("%.2f", orderItem.getIgstAmount()), FONT_NORMAL));
                        igstTotalAmount = igstTotalAmount + orderItem.getIgstAmount();
                    } else {
                        orders.addCell(new Paragraph(String.format("%.2f", orderItem.getCgstRate()), FONT_NORMAL));
                        orders.addCell(new Paragraph(String.format("%.2f", orderItem.getCgstAmount()), FONT_NORMAL));
                        orders.addCell(new Paragraph(String.format("%.2f", orderItem.getSgstRate()), FONT_NORMAL));
                        orders.addCell(new Paragraph(String.format("%.2f", orderItem.getSgstAmount()), FONT_NORMAL));
                        cgstTotalAmount = cgstTotalAmount + orderItem.getCgstAmount();
                        sgstTotalAmount = sgstTotalAmount + orderItem.getSgstAmount();
                    }
                    orders.addCell(new Paragraph(String.format("%.0f", orderItem.getNetAmount()), FONT_NORMAL));
                    LOGGER.info("IN FOR LOOP");
                }

                document.add(paragraphTitle);

                document.add(Chunk.NEWLINE);
                document.add(tableCustomerRetailer);

                document.add(Chunk.NEWLINE);
                document.add(orders);

                PdfPTable grandTotalTable = new PdfPTable(3);
                if (stateGst) {
                    grandTotalTable.setWidths(new float[]{6.6f, .6f, .8f});
                } else {
                    grandTotalTable.setWidths(new float[]{6.5f, .6f, .9f});
                }
                grandTotalTable.setWidthPercentage(95);

                Paragraph grandTotalParagraph = new Paragraph("Grand total", FONT_BOLD);
                grandTotalParagraph.setIndentationRight(20);
                grandTotalTable.addCell(grandTotalParagraph);
                Paragraph rsParagraph = new Paragraph("Rs.", FONT_BOLD);
                grandTotalTable.addCell(rsParagraph);
                Paragraph amountParagraph = new Paragraph(String.format("%.2f", pdfModel.getTotalAmount()), FONT_BOLD);
                grandTotalTable.addCell(amountParagraph);

                document.add(grandTotalTable);

                PdfPTable amountInWordsTable = new PdfPTable(3);
                if (!stateGst) {
                    amountInWordsTable.setWidths(new float[]{2, 5.1f, 0.9f});
                } else {
                    amountInWordsTable.setWidths(new float[]{2, 5.2f, 0.8f});
                }
                amountInWordsTable.setWidthPercentage(95);
                amountInWordsTable.addCell(new Paragraph("Amount in Words:", FONT_BOLD));

                String amountInWords = toAmountInWords(pdfModel.getTotalAmount());
                amountInWordsTable.addCell(new Paragraph(amountInWords.toString(), FONT_BOLD));
                amountInWordsTable.addCell(new Paragraph("E & O.E", FONT_NORMAL));
                document.add(amountInWordsTable);

                // Margin scheme declaration for credit notes
                addMarginSchemeDeclaration(document, pdfModel);

                document.newPage();
            }
            document.close(); // no need to close PDFwriter?
        } catch (DocumentException e) {
            LOGGER.error("Unable to write data to pdf file : ", e);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public static byte[] mergePdfFiles(List<byte[]> pdfFiles) throws IOException, DocumentException {
        ByteArrayOutputStream mergedOutputStream = new ByteArrayOutputStream();
        Document document = new Document();
        PdfCopy copy = new PdfCopy(document, mergedOutputStream);
        document.open();

        for (byte[] pdf : pdfFiles) {
            PdfReader reader = new PdfReader(new ByteArrayInputStream(pdf));
            int n = reader.getNumberOfPages();
            for (int i = 1; i <= n; i++) {
                copy.addPage(copy.getImportedPage(reader, i));
            }
            copy.freeReader(reader);
            reader.close();
        }

        document.close();
        return mergedOutputStream.toByteArray();
    }

}