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