Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4370 anupam.sin 1
package in.shop2020.support.services;
2
 
4497 mandeep.dh 3
import in.shop2020.purchase.PurchaseOrder;
4
import in.shop2020.purchase.Supplier;
5
 
4370 anupam.sin 6
import java.io.ByteArrayOutputStream;
7
import java.io.File;
8
import java.io.FileOutputStream;
9
import java.io.IOException;
10
import java.text.DateFormat;
11
import java.util.Date;
12
 
13
import org.slf4j.Logger;
14
import org.slf4j.LoggerFactory;
15
 
16
import com.itextpdf.text.Document;
17
import com.itextpdf.text.Element;
18
import com.itextpdf.text.Font;
4497 mandeep.dh 19
import com.itextpdf.text.Font.FontFamily;
4370 anupam.sin 20
import com.itextpdf.text.FontFactory;
21
import com.itextpdf.text.Paragraph;
22
import com.itextpdf.text.Phrase;
23
import com.itextpdf.text.Rectangle;
24
import com.itextpdf.text.pdf.PdfPCell;
25
import com.itextpdf.text.pdf.PdfPTable;
26
import com.itextpdf.text.pdf.PdfWriter;
27
 
28
public class PdfPoSheetGenerator {
29
 
30
    private static Logger logger = LoggerFactory.getLogger(PdfPoSheetGenerator.class);
31
 
32
    //private static final Properties properties = readProperties();
33
    private static final String ourAddress = "Spice Online Retail Pvt. Ltd.\nKhasra No. 819, Block-K\nMahipalpur, New Delhi-110037\n";
34
    private static final String tinNo = "07250399732";
35
 
36
    private static final Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);
37
 
38
    private static final Font helveticaBold8 = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 8);
39
    private static final Font helveticaBold12 = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 12);
40
 
41
    public static String generatePdfSheet(PurchaseOrder purchaseOrder, Supplier supplier) throws IOException{
42
        ByteArrayOutputStream baosPDF = null;
43
        try {
44
            baosPDF = new ByteArrayOutputStream();
45
 
46
            Document document = new Document();
47
            PdfWriter.getInstance(document, baosPDF);
48
            document.addAuthor("shop2020");
49
            document.addTitle("Purchase Order No: " + purchaseOrder.getPoNumber());
50
            document.open();
51
 
52
            PdfPTable poTable = getPoTable(purchaseOrder, supplier);
53
            poTable.setSpacingAfter(10.0f);
54
            poTable.setWidthPercentage(90.0f);
55
 
56
            document.add(poTable);
57
            document.close();
58
            baosPDF.close();
59
        } catch (Exception e) {
60
            logger.error("Error while generating Invoice: ", e);
61
        }
62
 
63
        String tmpDir = System.getProperty("java.io.tmpdir");
64
        String filename = tmpDir + "/po-" + purchaseOrder.getId() + ".pdf";
65
        File f = new File(filename);
66
        FileOutputStream fos = new FileOutputStream(f);
67
        baosPDF.writeTo(fos);
68
        return filename;
69
    }
70
 
71
    private static PdfPTable getPoTable(PurchaseOrder purchaseOrder, Supplier supplier) throws Exception{
72
        PdfPTable poTable = new PdfPTable(1);
73
        poTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
74
        poTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
75
 
76
        PdfPCell poTitleCell = new PdfPCell(new Phrase("Purchase Order", helveticaBold12));
77
        poTitleCell.setHorizontalAlignment(Element.ALIGN_CENTER);
78
        poTitleCell.setBorder(Rectangle.NO_BORDER);
79
 
80
        Date poDate = new Date(purchaseOrder.getCreatedAt());
81
        PdfPTable poSummaryTable = new PdfPTable(new float[]{0.5f, 0.5f});
82
        poSummaryTable.addCell(new PdfPCell(new Phrase("PO No: " + purchaseOrder.getPoNumber())));
83
        poSummaryTable.addCell(new PdfPCell(new Phrase("Date: " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(poDate))));
84
        poSummaryTable.setSpacingBefore(10.0f);
85
 
86
        poTable.addCell(poTitleCell);
87
        poTable.addCell(getAddressCell());
88
        poTable.addCell(poSummaryTable);
89
        poTable.addCell(getSalutationTable(supplier));
90
        poTable.addCell(getPoDetailsTable(purchaseOrder));
91
        poTable.addCell(getBillToTable());
92
 
93
        return poTable;
94
    }
95
 
96
    private static PdfPCell getAddressCell() {
97
        Paragraph addressParagraph = new Paragraph(ourAddress + "\nTIN NO. " + tinNo, new Font(FontFamily.TIMES_ROMAN, 8f));
98
        PdfPCell addressCell = new PdfPCell();
99
        addressCell.addElement(addressParagraph);
100
        addressCell.setHorizontalAlignment(Element.ALIGN_CENTER);
101
        addressCell.setBorder(Rectangle.NO_BORDER);
102
        return addressCell;
103
    }
104
 
105
    private static PdfPTable getSalutationTable(Supplier supplier) throws Exception{
106
        PdfPTable salutationTable = new PdfPTable(1);
107
        salutationTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
108
        salutationTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
109
        salutationTable.addCell(new Phrase("To", helvetica8));
110
        salutationTable.addCell(new Paragraph(supplier.getCommunicationAddress(), helvetica8));
111
        salutationTable.addCell(new Phrase("Dear Sir/Madam", helveticaBold8));
112
        salutationTable.addCell(new Phrase("Please supply the following stocks as per the details given below:", helvetica8));
113
        salutationTable.addCell(new Phrase(" "));
114
        return salutationTable;
115
    }
116
 
117
    private static PdfPTable getPoDetailsTable(PurchaseOrder purchaseOrder){
118
        PdfPTable detailsTable = new PdfPTable(new float[]{0.1f, 0.5f, 0.1f, 0.2f, 0.2f});
119
        detailsTable.addCell(new Phrase("Sl. No.", helveticaBold8));
120
        detailsTable.addCell(new Phrase("Description", helveticaBold8));
121
        detailsTable.addCell(new Phrase("Quantity", helveticaBold8));
122
        detailsTable.addCell(new Phrase("Rate (Rs)", helveticaBold8));
123
        detailsTable.addCell(new Phrase("Amount (Rs)", helveticaBold8));
124
 
125
        int slNo = 0;
126
        double total = 0;
4497 mandeep.dh 127
        for(in.shop2020.purchase.LineItem lineitem : purchaseOrder.getLineitems()){
4370 anupam.sin 128
            slNo++;
129
            detailsTable.addCell(new Phrase(slNo + "", helvetica8));
130
            detailsTable.addCell(getProductNameCell(lineitem));
131
            detailsTable.addCell(new Phrase(lineitem.getQuantity() + "", helvetica8));
132
            detailsTable.addCell(new Phrase(lineitem.getUnitPrice() + "", helvetica8));
133
            double lineTotal = lineitem.getQuantity() * lineitem.getUnitPrice();
134
            total += lineTotal;
135
            detailsTable.addCell(new Phrase("" + lineTotal, helvetica8));
136
        }
137
        detailsTable.addCell(getTotalCell(4));
138
        detailsTable.addCell(new Phrase("" + total, helvetica8));
139
        return detailsTable;
140
    }
141
 
142
    private static PdfPTable getBillToTable(){
143
        PdfPTable billToTable = new PdfPTable(new float[]{0.2f, 0.8f});
144
        billToTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
145
 
146
        billToTable.addCell(new Phrase("Bill To and Ship To:", helvetica8));
147
        billToTable.addCell(new PdfPCell(new Paragraph(ourAddress + "\nTIN NO. " + tinNo, helvetica8)));
148
 
149
        billToTable.addCell(new Phrase("Contact Person:", helvetica8));
150
        billToTable.addCell(new Phrase("Mr. Pramod Kumar, Contact No. +91 9971573026", helvetica8));
151
 
152
        billToTable.addCell(new Phrase("Taxes:", helvetica8));
153
        billToTable.addCell(new Phrase("Prices inclusive of all taxes", helvetica8));
154
 
155
        return billToTable;
156
    }
157
 
4497 mandeep.dh 158
    private static PdfPCell getProductNameCell(in.shop2020.purchase.LineItem lineitem) {
4370 anupam.sin 159
        String itemName = getItemDisplayName(lineitem);
160
        PdfPCell productNameCell = new PdfPCell(new Phrase(itemName, helvetica8));
161
        productNameCell.setHorizontalAlignment(Element.ALIGN_LEFT);
162
        return productNameCell;
163
    }
164
 
4497 mandeep.dh 165
    private static String getItemDisplayName(in.shop2020.purchase.LineItem lineitem){
4370 anupam.sin 166
        StringBuffer itemName = new StringBuffer();
167
        if(lineitem.getBrand()!= null)
168
            itemName.append(lineitem.getBrand() + " ");
169
        if(lineitem.getModelName() != null)
170
            itemName.append(lineitem.getModelName() + " ");
171
        if(lineitem.getModelNumber() != null )
172
            itemName.append(lineitem.getModelNumber() + " ");
173
        if(lineitem.getColor() != null && !lineitem.getColor().trim().equals("NA"))
174
            itemName.append("("+lineitem.getColor()+")");
175
 
176
        return itemName.toString();
177
    }
178
 
179
    private static PdfPCell getTotalCell(int colspan) {
180
        PdfPCell totalCell = new PdfPCell(new Phrase("Total", helveticaBold8));
181
        totalCell.setColspan(colspan);
182
        totalCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
183
        return totalCell;
184
    }
185
 
186
    /*public static void main(String[] args) throws Exception {
187
        long purchaseOrderId = 82;
188
 
189
        if (args != null && args.length != 0) {
190
            purchaseOrderId = Long.parseLong(args[0]);
191
        }
192
 
193
        WarehouseClient warehouseServiceClient = new WarehouseClient();
194
        Client client = warehouseServiceClient.getClient();
195
        in.shop2020.warehouse.PurchaseOrder purchaseOrder = client.getPurchaseOrder(purchaseOrderId);
196
        Supplier supplier = client.getSupplier(purchaseOrder.getSupplierId());
197
        String filename = generatePdfSheet(purchaseOrder, supplier);
198
        System.out.println("PO generated in: " + filename);
199
    }*/
200
}