Subversion Repositories SmartDukaan

Rev

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