Subversion Repositories SmartDukaan

Rev

Rev 13938 | Rev 14073 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 13938 Rev 14072
Line 1... Line 1...
1
package in.shop2020.inventory.service;
1
package in.shop2020.inventory.service;
2
 
2
 
-
 
3
import in.shop2020.inventory.controllers.PurchaseOrderController;
-
 
4
import in.shop2020.model.v1.catalog.CatalogServiceException;
-
 
5
import in.shop2020.model.v1.inventory.StateInfo;
-
 
6
import in.shop2020.model.v1.inventory.VatType;
-
 
7
import in.shop2020.model.v1.inventory.Warehouse;
3
import in.shop2020.purchase.PurchaseOrder;
8
import in.shop2020.purchase.PurchaseOrder;
4
import in.shop2020.purchase.Supplier;
9
import in.shop2020.purchase.Supplier;
5
import in.shop2020.purchase.TaxType;
10
import in.shop2020.purchase.TaxType;
6
 
11
 
7
import java.io.ByteArrayOutputStream;
12
import java.io.ByteArrayOutputStream;
Line 9... Line 14...
9
import java.io.FileOutputStream;
14
import java.io.FileOutputStream;
10
import java.io.IOException;
15
import java.io.IOException;
11
import java.math.BigDecimal;
16
import java.math.BigDecimal;
12
import java.text.DateFormat;
17
import java.text.DateFormat;
13
import java.util.Date;
18
import java.util.Date;
-
 
19
import java.util.Map;
14
 
20
 
-
 
21
import org.apache.thrift.TException;
-
 
22
import org.apache.thrift.transport.TTransportException;
15
import org.slf4j.Logger;
23
import org.slf4j.Logger;
16
import org.slf4j.LoggerFactory;
24
import org.slf4j.LoggerFactory;
17
 
25
 
18
import com.itextpdf.text.Document;
26
import com.itextpdf.text.Document;
19
import com.itextpdf.text.Element;
27
import com.itextpdf.text.Element;
Line 24... Line 32...
24
import com.itextpdf.text.Phrase;
32
import com.itextpdf.text.Phrase;
25
import com.itextpdf.text.Rectangle;
33
import com.itextpdf.text.Rectangle;
26
import com.itextpdf.text.pdf.PdfPCell;
34
import com.itextpdf.text.pdf.PdfPCell;
27
import com.itextpdf.text.pdf.PdfPTable;
35
import com.itextpdf.text.pdf.PdfPTable;
28
import com.itextpdf.text.pdf.PdfWriter;
36
import com.itextpdf.text.pdf.PdfWriter;
-
 
37
import in.shop2020.purchase.PurchaseService.Client;
-
 
38
import in.shop2020.thrift.clients.CatalogClient;
-
 
39
import in.shop2020.thrift.clients.InventoryClient;
-
 
40
import in.shop2020.thrift.clients.PurchaseClient;
29
 
41
 
30
public class PdfPoSheetGenerator {
42
public class PdfPoSheetGenerator {
31
 
43
 
32
    private static Logger logger = LoggerFactory
44
    private static Logger logger = LoggerFactory
33
            .getLogger(PdfPoSheetGenerator.class);
45
            .getLogger(PdfPoSheetGenerator.class);
Line 62... Line 74...
62
 
74
 
63
    private static final Font helveticaBold8 = FontFactory.getFont(
75
    private static final Font helveticaBold8 = FontFactory.getFont(
64
            FontFactory.HELVETICA_BOLD, 8);
76
            FontFactory.HELVETICA_BOLD, 8);
65
    private static final Font helveticaBold12 = FontFactory.getFont(
77
    private static final Font helveticaBold12 = FontFactory.getFont(
66
            FontFactory.HELVETICA_BOLD, 12);
78
            FontFactory.HELVETICA_BOLD, 12);
-
 
79
    
-
 
80
    public static Map<Long, StateInfo> stateIdMap = null; 
-
 
81
 
-
 
82
	static{
-
 
83
		try{
-
 
84
			in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = new InventoryClient().getClient();
-
 
85
			stateIdMap = inventoryClient.getStateMaster();
-
 
86
			
-
 
87
		} catch(Exception e){
-
 
88
			logger.error("Unable to get State Info Map", e);
-
 
89
		}
-
 
90
	}
67
 
91
 
68
    public static String generatePdfSheet(PurchaseOrder purchaseOrder,
92
    public static String generatePdfSheet(PurchaseOrder purchaseOrder,
69
            Supplier supplier) throws IOException {
93
            Supplier supplier) throws IOException {
70
        ByteArrayOutputStream baosPDF = null;
94
        ByteArrayOutputStream baosPDF = null;
71
        try {
95
        try {
72
            baosPDF = new ByteArrayOutputStream();
96
        	baosPDF = new ByteArrayOutputStream();
73
 
97
 
74
            Document document = new Document();
98
            Document document = new Document();
75
            PdfWriter.getInstance(document, baosPDF);
99
            PdfWriter.getInstance(document, baosPDF);
76
            document.addAuthor("shop2020");
100
            document.addAuthor("shop2020");
77
            document.addTitle("Purchase Order No: "
101
            document.addTitle("Purchase Order No: "
Line 119... Line 143...
119
 
143
 
120
        poTable.addCell(poTitleCell);
144
        poTable.addCell(poTitleCell);
121
        poTable.addCell(getAddressCell(purchaseOrder.getWarehouseId(), purchaseOrder.getShippingWarehouseId()));
145
        poTable.addCell(getAddressCell(purchaseOrder.getWarehouseId(), purchaseOrder.getShippingWarehouseId()));
122
        poTable.addCell(poSummaryTable);
146
        poTable.addCell(poSummaryTable);
123
        poTable.addCell(getSalutationTable(supplier));
147
        poTable.addCell(getSalutationTable(supplier));
124
        poTable.addCell(getPoDetailsTable(purchaseOrder));
148
        poTable.addCell(getPoDetailsTable(purchaseOrder, supplier));
125
        poTable.addCell(getBillToTable(purchaseOrder.getWarehouseId(), purchaseOrder.getTaxType()));
149
        poTable.addCell(getBillToTable(purchaseOrder.getWarehouseId(), purchaseOrder.getTaxType()));
126
        //poTable.addCell(getCFormCell(purchaseOrder.getTaxType()));
150
        //poTable.addCell(getCFormCell(purchaseOrder.getTaxType()));
127
 
151
 
128
        return poTable;
152
        return poTable;
129
    }
153
    }
Line 293... Line 317...
293
                        helvetica8));
317
                        helvetica8));
294
        salutationTable.addCell(new Phrase(" "));
318
        salutationTable.addCell(new Phrase(" "));
295
        return salutationTable;
319
        return salutationTable;
296
    }
320
    }
297
 
321
 
298
    private static PdfPTable getPoDetailsTable(PurchaseOrder purchaseOrder) {
322
    private static PdfPTable getPoDetailsTable(PurchaseOrder purchaseOrder,Supplier supplier) throws CatalogServiceException, TException {
299
        PdfPTable detailsTable = new PdfPTable(new float[] { 0.1f, 0.5f, 0.1f,
323
        PdfPTable detailsTable = new PdfPTable(new float[] { 0.1f, 0.3f, 0.1f,
300
                0.1f, 0.15f, 0.15f });
324
                0.1f, 0.1f, 0.1f, 0.15f, 0.15f });
301
        detailsTable.addCell(new Phrase("Sl. No.", helveticaBold8));
325
        detailsTable.addCell(new Phrase("Sl. No.", helveticaBold8));
302
        detailsTable.addCell(new Phrase("Description", helveticaBold8));
326
        detailsTable.addCell(new Phrase("Description", helveticaBold8));
303
        detailsTable.addCell(new Phrase("Quantity", helveticaBold8));
327
        detailsTable.addCell(new Phrase("Quantity", helveticaBold8));
304
        detailsTable.addCell(new Phrase("Mrp", helveticaBold8));
328
        detailsTable.addCell(new Phrase("Mrp", helveticaBold8));
-
 
329
        if(purchaseOrder.getTaxType()==TaxType.CFORM){
-
 
330
        	detailsTable.addCell(new Phrase("CST Amount",helveticaBold8));
-
 
331
        } else {
-
 
332
        	detailsTable.addCell(new Phrase("VAT Amount",helveticaBold8));
-
 
333
        }
-
 
334
        detailsTable.addCell(new Phrase("Rate Excl. (Rs)", helveticaBold8));
305
        detailsTable.addCell(new Phrase("Rate (Rs)", helveticaBold8));
335
        detailsTable.addCell(new Phrase("Rate Incl. (Rs)", helveticaBold8));
306
        detailsTable.addCell(new Phrase("Amount (Rs)", helveticaBold8));
336
        detailsTable.addCell(new Phrase("Amount (Rs)", helveticaBold8));
307
 
337
 
308
        int slNo = 0;
338
        int slNo = 0;
309
        double total = 0;
339
        double total = 0;
-
 
340
        double vatFactor = 0;
-
 
341
        double totalUnits = 0;
-
 
342
        boolean isMRPVat = false;
-
 
343
        in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = new CatalogClient().getClient();
-
 
344
        if(stateIdMap.get(supplier.getStateId()).getVatType().equals(VatType.MRP)) {
-
 
345
			isMRPVat = true;
-
 
346
		}
310
        for (in.shop2020.purchase.LineItem lineitem : purchaseOrder
347
        for (in.shop2020.purchase.LineItem lineitem : purchaseOrder
311
                .getLineitems()) {
348
                .getLineitems()) {
312
            slNo++;
349
            slNo++;
313
            detailsTable.addCell(new Phrase(slNo + "", helvetica8));
350
            detailsTable.addCell(new Phrase(slNo + "", helvetica8));
314
            detailsTable.addCell(getProductNameCell(lineitem));
351
            detailsTable.addCell(getProductNameCell(lineitem));
315
            detailsTable.addCell(new Phrase(lineitem.getQuantity() + "",
352
            detailsTable.addCell(new Phrase(lineitem.getQuantity() + "",
316
                    helvetica8));
353
                    helvetica8));
317
            detailsTable.addCell(new Phrase(lineitem.getMrp() + "",
354
            detailsTable.addCell(new Phrase(lineitem.getMrp() + "",
318
                    helvetica8));
355
                    helvetica8));
-
 
356
            if(purchaseOrder.getTaxType()==TaxType.CFORM){
-
 
357
            	vatFactor = 2.0;
-
 
358
            } else {
-
 
359
            	if(isMRPVat){
-
 
360
            		vatFactor = catalogClient.getVatPercentageForItem(lineitem.getItemId(), supplier.getStateId(), lineitem.getMrp());
-
 
361
            	}else{
-
 
362
            		vatFactor = catalogClient.getVatPercentageForItem(lineitem.getItemId(), supplier.getStateId(), lineitem.getUnitPrice());
-
 
363
            	}
-
 
364
            }
-
 
365
            double taxAmount = (lineitem.getUnitPrice() * vatFactor)/100;
-
 
366
            detailsTable.addCell(new Phrase(taxAmount + "",
-
 
367
                    helvetica8));
-
 
368
            detailsTable.addCell(new Phrase((lineitem.getUnitPrice()-taxAmount) + "",
-
 
369
                    helvetica8));
319
            detailsTable.addCell(new Phrase(lineitem.getUnitPrice() + "",
370
            detailsTable.addCell(new Phrase(lineitem.getUnitPrice() + "",
320
                    helvetica8));
371
                    helvetica8));
321
            double lineTotal = lineitem.getQuantity() * lineitem.getUnitPrice();
372
            double lineTotal = lineitem.getQuantity() * lineitem.getUnitPrice();
-
 
373
            totalUnits += lineitem.getQuantity();
322
            total += lineTotal;
374
            total += lineTotal;
323
            detailsTable.addCell(new Phrase("" + lineTotal, helvetica8));
375
            detailsTable.addCell(new Phrase("" + lineTotal, helvetica8));
324
        }
376
        }
325
        detailsTable.addCell(getTotalCell(5));
377
        detailsTable.addCell(getTotalCell(2));
326
        detailsTable.addCell(new Phrase(BigDecimal.valueOf(total).toPlainString(), helvetica8));
378
        detailsTable.addCell(new Phrase(BigDecimal.valueOf(totalUnits).toPlainString(), helvetica8));
-
 
379
        detailsTable.addCell(getTotalAmountCell(5,total));
327
        return detailsTable;
380
        return detailsTable;
328
    }
381
    }
329
 
382
 
330
    private static PdfPTable getBillToTable(long warehouseId, TaxType taxType) {
383
    private static PdfPTable getBillToTable(long warehouseId, TaxType taxType) {
331
    	//TODO Write this code in a proper configurable way
384
    	//TODO Write this code in a proper configurable way
Line 450... Line 503...
450
        PdfPCell totalCell = new PdfPCell(new Phrase("Total", helveticaBold8));
503
        PdfPCell totalCell = new PdfPCell(new Phrase("Total", helveticaBold8));
451
        totalCell.setColspan(colspan);
504
        totalCell.setColspan(colspan);
452
        totalCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
505
        totalCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
453
        return totalCell;
506
        return totalCell;
454
    }
507
    }
-
 
508
    
-
 
509
    private static PdfPCell getTotalAmountCell(int colspan, double totalAmount){
-
 
510
    	PdfPCell totalCell = new PdfPCell(new Phrase(BigDecimal.valueOf(totalAmount).toPlainString(), helvetica8));
-
 
511
        totalCell.setColspan(colspan);
-
 
512
        return totalCell; 
-
 
513
    }
455
 
514
 
456
}
515
}