Subversion Repositories SmartDukaan

Rev

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