Subversion Repositories SmartDukaan

Rev

Rev 4410 | Rev 4788 | 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 {
4414 rajveer 76
			if(!txnClient.markOrdersAsManifested(warehouseId, providerId, isCod)){
77
				return baosPDF;	
78
			}
4410 rajveer 79
			orders = txnClient.getOrdersInBatch(statuses, 0, 0, warehouseId);
676 chandransh 80
			warehouse = inventoryClient.getWarehouse(warehouseId);
81
			provider = logisticsClient.getProvider(providerId);
3062 chandransh 82
		} catch (TException e) {
83
		    logger.error("Error getting information from one of the Thrift Services: ", e);
676 chandransh 84
			return baosPDF;
85
		} catch (InventoryServiceException e) {
3062 chandransh 86
		    logger.error("Error getting warehouse info from the catalog service: ", e);
676 chandransh 87
			return baosPDF;
88
		} catch (LogisticsServiceException e) {
3062 chandransh 89
		    logger.error("Error getting provider info from the logistics service: ", e);
676 chandransh 90
			return baosPDF;
91
		} catch (TransactionServiceException e) {
3062 chandransh 92
		    logger.error("Error getting orders from the transaction service: ", e);
676 chandransh 93
			return baosPDF;
94
		}
95
 
96
		try {
97
			baosPDF = new ByteArrayOutputStream();
98
			Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);
99
			Document document = new Document();
100
			PdfWriter.getInstance(document, baosPDF);
101
			document.addAuthor("shop2020");
102
			document.addTitle("Manifest for warehouse " + warehouseId + " provider " + providerId);
103
			document.open();
104
			PdfPTable table = new PdfPTable(1);
105
			table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
106
			table.getDefaultCell().setPaddingBottom(10.0f);
107
 
744 chandransh 108
			String logoPath = ManifestGenerator.class.getResource("/logo.jpg").getPath();
676 chandransh 109
			PdfPCell logoCell = new PdfPCell(Image.getInstance(logoPath), false);
110
			logoCell.setBorder(Rectangle.NO_BORDER);
111
 
112
			String addressString =  warehouse.getLocation() + "\nPIN " + warehouse.getPincode()+ "\n\n";
113
			Paragraph addressParagraph = new Paragraph(addressString, new Font(FontFamily.TIMES_ROMAN,8f));
114
			PdfPCell addressCell = new PdfPCell();
115
			addressCell.addElement(addressParagraph);
116
			addressCell.setHorizontalAlignment(Element.ALIGN_LEFT);
117
			addressCell.setBorder(Rectangle.NO_BORDER);
118
 
119
			PdfPTable ordersTable = new PdfPTable(8);
120
			ordersTable.addCell(new Phrase("Sl No", helvetica8));
121
			ordersTable.addCell(new Phrase("Order No", helvetica8));
122
			ordersTable.addCell(new Phrase("AWB No", helvetica8));
123
			ordersTable.addCell(new Phrase("Packet Wt.", helvetica8));
124
			ordersTable.addCell(new Phrase("Name", helvetica8));
125
			ordersTable.addCell(new Phrase("Destination City", helvetica8));
126
			ordersTable.addCell(new Phrase("Pincode", helvetica8));
127
			ordersTable.addCell(new Phrase("State", helvetica8));
128
 
129
			int serialNo = 0;
130
			for(int i=0; i < orders.size();i++){
131
				Order order = orders.get(i);
132
				if(order.getLogistics_provider_id()!=providerId)
133
					continue;
3062 chandransh 134
				if(order.isCod() != isCod)
135
	                continue;
676 chandransh 136
				//TODO: These are exactly the orders which will be shipped now. Shouldn't we change their status to be SHIPPED?
137
				serialNo++;
138
				List<LineItem> lineItems = order.getLineitems();
139
				double weight = 0;
140
				for(LineItem lineItem: lineItems)
141
					weight += lineItem.getTotal_weight();
142
 
143
				ordersTable.addCell(new Phrase(serialNo + "", helvetica8));
144
				ordersTable.addCell(new Phrase(order.getId() + "", helvetica8));
145
				ordersTable.addCell(new Phrase(order.getAirwaybill_no(), helvetica8));
919 rajveer 146
				ordersTable.addCell(new Phrase(weightFormat.format(weight), helvetica8));
676 chandransh 147
				ordersTable.addCell(new Phrase(order.getCustomer_name(), helvetica8));
148
				ordersTable.addCell(new Phrase(order.getCustomer_city(), helvetica8));
149
				ordersTable.addCell(new Phrase(order.getCustomer_pincode(), helvetica8));
150
				ordersTable.addCell(new Phrase(order.getCustomer_state(), helvetica8));				
151
			}
152
 
153
			table.addCell(logoCell);
154
			table.addCell(addressCell);
3062 chandransh 155
			if(isCod)
156
			    table.addCell(new Phrase("PAYMODE: COD", helvetica8));
157
			else
158
			    table.addCell(new Phrase("PAYMODE: Prepaid", helvetica8));
676 chandransh 159
			table.addCell(new Phrase("Courier Name: " + provider.getName(), helvetica8));
160
			table.addCell(new Phrase("Pick up Date: " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()), helvetica8));
161
			table.addCell(ordersTable);
162
			table.setWidthPercentage(90.0f);
163
 
164
			document.add(table);  
165
			document.close();
166
			baosPDF.close();
167
		} catch (DocumentException e) {
3062 chandransh 168
		    logger.error("Error while creating the manifest file", e);
676 chandransh 169
		} catch (MalformedURLException e) {
3062 chandransh 170
		    logger.error("Error while creating the manifest file", e);
676 chandransh 171
		} catch (IOException e) {
3062 chandransh 172
			logger.error("Error while creating the manifest file", e);
676 chandransh 173
		}
174
 
175
		return baosPDF;
176
	}
744 chandransh 177
 
178
	public static void main(String[] args) throws IOException {
179
		ManifestGenerator manifestGenerator = new ManifestGenerator();
3062 chandransh 180
		ByteArrayOutputStream baos = manifestGenerator.generateManifestFile(1, 1, true);
744 chandransh 181
		File f = new File("/home/ashish/Downloads/manifest-1-2.pdf");
182
		FileOutputStream fos = new FileOutputStream(f);
183
		baos.writeTo(fos);
184
	}
676 chandransh 185
}