Subversion Repositories SmartDukaan

Rev

Rev 1023 | Rev 3125 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
676 chandransh 1
package in.shop2020.support.services;
2
 
3
import java.io.ByteArrayOutputStream;
744 chandransh 4
import java.io.File;
5
import java.io.FileOutputStream;
676 chandransh 6
import java.io.IOException;
7
import java.net.MalformedURLException;
8
import java.text.DateFormat;
744 chandransh 9
import java.text.DecimalFormat;
676 chandransh 10
import java.util.Date;
11
import java.util.List;
12
 
13
import org.apache.thrift.TException;
3062 chandransh 14
import org.slf4j.Logger;
15
import org.slf4j.LoggerFactory;
676 chandransh 16
 
17
import com.itextpdf.text.Document;
18
import com.itextpdf.text.DocumentException;
19
import com.itextpdf.text.Element;
20
import com.itextpdf.text.Font;
21
import com.itextpdf.text.FontFactory;
22
import com.itextpdf.text.Image;
23
import com.itextpdf.text.Paragraph;
24
import com.itextpdf.text.Phrase;
25
import com.itextpdf.text.Rectangle;
26
import com.itextpdf.text.Font.FontFamily;
27
import com.itextpdf.text.pdf.PdfPCell;
28
import com.itextpdf.text.pdf.PdfPTable;
29
import com.itextpdf.text.pdf.PdfWriter;
30
 
31
import in.shop2020.logistics.LogisticsServiceException;
32
import in.shop2020.logistics.Provider;
33
import in.shop2020.model.v1.catalog.InventoryServiceException;
34
import in.shop2020.model.v1.catalog.Warehouse;
35
import in.shop2020.model.v1.order.LineItem;
36
import in.shop2020.model.v1.order.Order;
37
import in.shop2020.model.v1.order.OrderStatus;
38
import in.shop2020.model.v1.order.TransactionServiceException;
39
import in.shop2020.thrift.clients.CatalogServiceClient;
40
import in.shop2020.thrift.clients.LogisticsServiceClient;
41
import in.shop2020.thrift.clients.TransactionServiceClient;
42
 
43
public class ManifestGenerator {
3062 chandransh 44
 
45
    private static Logger logger = LoggerFactory.getLogger(ManifestGenerator.class);
46
 
676 chandransh 47
	private TransactionServiceClient tsc = null;
48
	private CatalogServiceClient csc = null;
49
	private LogisticsServiceClient lsc = null;
744 chandransh 50
	private DecimalFormat weightFormat = new DecimalFormat("0.000");
676 chandransh 51
	public ManifestGenerator() {
52
		try {
53
			tsc = new TransactionServiceClient();
54
			csc = new CatalogServiceClient();
55
			lsc = new LogisticsServiceClient();
56
		} catch (Exception e) {
3062 chandransh 57
		    logger.error("Error while initializing one of the thrift clients", e);
676 chandransh 58
		}
59
	}
60
 
3062 chandransh 61
	public ByteArrayOutputStream generateManifestFile(long warehouseId,	long providerId, boolean isCod) {
676 chandransh 62
		ByteArrayOutputStream baosPDF = null;
63
		in.shop2020.model.v1.order.TransactionService.Client txnClient = tsc.getClient();
64
		in.shop2020.model.v1.catalog.InventoryService.Client inventoryClient = csc.getClient();
65
		in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();
66
 
67
		List<Order> orders = null;
68
		Warehouse warehouse = null;
69
		Provider provider = null;
70
		try {
71
			orders = txnClient.getAllOrders(OrderStatus.BILLED, 0L, new Date().getTime(), warehouseId);
72
			warehouse = inventoryClient.getWarehouse(warehouseId);
73
			provider = logisticsClient.getProvider(providerId);
3062 chandransh 74
		} catch (TException e) {
75
		    logger.error("Error getting information from one of the Thrift Services: ", e);
676 chandransh 76
			return baosPDF;
77
		} catch (InventoryServiceException e) {
3062 chandransh 78
		    logger.error("Error getting warehouse info from the catalog service: ", e);
676 chandransh 79
			return baosPDF;
80
		} catch (LogisticsServiceException e) {
3062 chandransh 81
		    logger.error("Error getting provider info from the logistics service: ", e);
676 chandransh 82
			return baosPDF;
83
		} catch (TransactionServiceException e) {
3062 chandransh 84
		    logger.error("Error getting orders from the transaction service: ", e);
676 chandransh 85
			return baosPDF;
86
		}
87
 
88
		try {
89
			baosPDF = new ByteArrayOutputStream();
90
			Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);
91
			Document document = new Document();
92
			PdfWriter.getInstance(document, baosPDF);
93
			document.addAuthor("shop2020");
94
			document.addTitle("Manifest for warehouse " + warehouseId + " provider " + providerId);
95
			document.open();
96
			PdfPTable table = new PdfPTable(1);
97
			table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
98
			table.getDefaultCell().setPaddingBottom(10.0f);
99
 
744 chandransh 100
			String logoPath = ManifestGenerator.class.getResource("/logo.jpg").getPath();
676 chandransh 101
			PdfPCell logoCell = new PdfPCell(Image.getInstance(logoPath), false);
102
			logoCell.setBorder(Rectangle.NO_BORDER);
103
 
104
			String addressString =  warehouse.getLocation() + "\nPIN " + warehouse.getPincode()+ "\n\n";
105
			Paragraph addressParagraph = new Paragraph(addressString, new Font(FontFamily.TIMES_ROMAN,8f));
106
			PdfPCell addressCell = new PdfPCell();
107
			addressCell.addElement(addressParagraph);
108
			addressCell.setHorizontalAlignment(Element.ALIGN_LEFT);
109
			addressCell.setBorder(Rectangle.NO_BORDER);
110
 
111
			PdfPTable ordersTable = new PdfPTable(8);
112
			ordersTable.addCell(new Phrase("Sl No", helvetica8));
113
			ordersTable.addCell(new Phrase("Order No", helvetica8));
114
			ordersTable.addCell(new Phrase("AWB No", helvetica8));
115
			ordersTable.addCell(new Phrase("Packet Wt.", helvetica8));
116
			ordersTable.addCell(new Phrase("Name", helvetica8));
117
			ordersTable.addCell(new Phrase("Destination City", helvetica8));
118
			ordersTable.addCell(new Phrase("Pincode", helvetica8));
119
			ordersTable.addCell(new Phrase("State", helvetica8));
120
 
121
			int serialNo = 0;
122
			for(int i=0; i < orders.size();i++){
123
				Order order = orders.get(i);
124
				if(order.getLogistics_provider_id()!=providerId)
125
					continue;
3062 chandransh 126
				if(order.isCod() != isCod)
127
	                continue;
676 chandransh 128
				//TODO: These are exactly the orders which will be shipped now. Shouldn't we change their status to be SHIPPED?
129
				serialNo++;
130
				List<LineItem> lineItems = order.getLineitems();
131
				double weight = 0;
132
				for(LineItem lineItem: lineItems)
133
					weight += lineItem.getTotal_weight();
134
 
135
				ordersTable.addCell(new Phrase(serialNo + "", helvetica8));
136
				ordersTable.addCell(new Phrase(order.getId() + "", helvetica8));
137
				ordersTable.addCell(new Phrase(order.getAirwaybill_no(), helvetica8));
919 rajveer 138
				ordersTable.addCell(new Phrase(weightFormat.format(weight), helvetica8));
676 chandransh 139
				ordersTable.addCell(new Phrase(order.getCustomer_name(), helvetica8));
140
				ordersTable.addCell(new Phrase(order.getCustomer_city(), helvetica8));
141
				ordersTable.addCell(new Phrase(order.getCustomer_pincode(), helvetica8));
142
				ordersTable.addCell(new Phrase(order.getCustomer_state(), helvetica8));				
143
			}
144
 
145
			table.addCell(logoCell);
146
			table.addCell(addressCell);
3062 chandransh 147
			if(isCod)
148
			    table.addCell(new Phrase("PAYMODE: COD", helvetica8));
149
			else
150
			    table.addCell(new Phrase("PAYMODE: Prepaid", helvetica8));
676 chandransh 151
			table.addCell(new Phrase("Courier Name: " + provider.getName(), helvetica8));
152
			table.addCell(new Phrase("Pick up Date: " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()), helvetica8));
153
			table.addCell(ordersTable);
154
			table.setWidthPercentage(90.0f);
155
 
156
			document.add(table);  
157
			document.close();
158
			baosPDF.close();
159
		} catch (DocumentException e) {
3062 chandransh 160
		    logger.error("Error while creating the manifest file", e);
676 chandransh 161
		} catch (MalformedURLException e) {
3062 chandransh 162
		    logger.error("Error while creating the manifest file", e);
676 chandransh 163
		} catch (IOException e) {
3062 chandransh 164
			logger.error("Error while creating the manifest file", e);
676 chandransh 165
		}
166
 
167
		return baosPDF;
168
	}
744 chandransh 169
 
170
	public static void main(String[] args) throws IOException {
171
		ManifestGenerator manifestGenerator = new ManifestGenerator();
3062 chandransh 172
		ByteArrayOutputStream baos = manifestGenerator.generateManifestFile(1, 1, true);
744 chandransh 173
		File f = new File("/home/ashish/Downloads/manifest-1-2.pdf");
174
		FileOutputStream fos = new FileOutputStream(f);
175
		baos.writeTo(fos);
176
	}
676 chandransh 177
}