Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
679 chandransh 1
package in.shop2020.support.services;
2
 
3
import in.shop2020.logistics.LogisticsServiceException;
4
import in.shop2020.logistics.Provider;
5
import in.shop2020.model.v1.catalog.InventoryServiceException;
6
import in.shop2020.model.v1.catalog.Warehouse;
7
import in.shop2020.model.v1.order.LineItem;
8
import in.shop2020.model.v1.order.Order;
9
import in.shop2020.model.v1.order.OrderStatus;
10
import in.shop2020.model.v1.order.TransactionServiceException;
11
import in.shop2020.thrift.clients.CatalogServiceClient;
12
import in.shop2020.thrift.clients.LogisticsServiceClient;
13
import in.shop2020.thrift.clients.TransactionServiceClient;
14
 
15
import java.io.ByteArrayOutputStream;
16
import java.io.FileNotFoundException;
17
import java.io.FileOutputStream;
18
import java.io.IOException;
19
import java.util.Date;
20
import java.util.List;
21
 
22
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
23
import org.apache.poi.ss.usermodel.Cell;
24
import org.apache.poi.ss.usermodel.CellStyle;
25
import org.apache.poi.ss.usermodel.CreationHelper;
26
import org.apache.poi.ss.usermodel.Row;
27
import org.apache.poi.ss.usermodel.Sheet;
28
import org.apache.poi.ss.usermodel.Workbook;
29
import org.apache.thrift.TException;
30
 
31
public class CourierDetailsGenerator {
32
	private TransactionServiceClient tsc = null;
33
	private CatalogServiceClient csc = null;
34
	private LogisticsServiceClient lsc = null;
35
 
36
	public CourierDetailsGenerator(){
37
		try {
38
			tsc = new TransactionServiceClient();
39
			csc = new CatalogServiceClient();
40
			lsc = new LogisticsServiceClient();
41
		} catch (Exception e) {
42
			e.printStackTrace();
43
		}
44
	}
45
 
46
	public ByteArrayOutputStream generateCourierDetails(long warehouseId, long providerId){
47
		ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
48
		in.shop2020.model.v1.order.TransactionService.Client txnClient = tsc.getClient();
49
		in.shop2020.model.v1.catalog.InventoryService.Client inventoryClient = csc.getClient();
50
		in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();
51
 
52
		List<Order> orders = null;
53
		Warehouse warehouse = null;
54
		Provider provider = null;
55
 
56
		try {
57
			orders = txnClient.getAllOrders(OrderStatus.BILLED, 0L, new Date().getTime(), warehouseId);
58
			warehouse = inventoryClient.getWarehouse(warehouseId);
59
			provider = logisticsClient.getProvider(providerId);
60
		} catch (TException e1) {
61
			e1.printStackTrace();
62
			return baosXLS;
63
		} catch (InventoryServiceException e) {
64
			e.printStackTrace();
65
			return baosXLS;
66
		} catch (LogisticsServiceException e) {
67
			e.printStackTrace();
68
			return baosXLS;
69
		} catch (TransactionServiceException e) {
70
			e.printStackTrace();
71
			return baosXLS;
72
		}
73
 
74
	    Workbook wb = new HSSFWorkbook();
75
	    CreationHelper createHelper = wb.getCreationHelper();
76
	    Sheet sheet = wb.createSheet("new sheet");
77
 
78
	    CellStyle dateCellStyle = wb.createCellStyle();
79
	    dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("d/m/yyyy"));
80
 
81
	    CellStyle weightStyle = wb.createCellStyle();
82
	    weightStyle.setDataFormat(createHelper.createDataFormat().getFormat("0.000"));
83
 
84
	    // Create the header row and put all the titles in it. Rows are 0 based.
85
	    Row headerRow = sheet.createRow((short)0);
86
	    headerRow.createCell(0).setCellValue("Sl No");
87
	    headerRow.createCell(1).setCellValue("AWB No");
88
	    headerRow.createCell(2).setCellValue("AWB Date");
89
	    headerRow.createCell(3).setCellValue("Order No");
90
	    headerRow.createCell(4).setCellValue("Name");
91
	    headerRow.createCell(5).setCellValue("Address 1");
92
	    headerRow.createCell(6).setCellValue("Address 2");
93
	    headerRow.createCell(7).setCellValue("City");
94
	    headerRow.createCell(8).setCellValue("State");
95
	    headerRow.createCell(9).setCellValue("Pin Code");
96
	    headerRow.createCell(10).setCellValue("Telephone No 1");
97
	    headerRow.createCell(11).setCellValue("Telephone No 2");
98
	    headerRow.createCell(12).setCellValue("Paymode");
99
	    headerRow.createCell(13).setCellValue("Amount to be Collected");
100
	    headerRow.createCell(14).setCellValue("Shipment Value");
101
	    headerRow.createCell(15).setCellValue("Item ID");
102
	    headerRow.createCell(16).setCellValue("Packet Weight(in Kg)");
103
	    headerRow.createCell(17).setCellValue("Product Name");
104
	    headerRow.createCell(18).setCellValue("Pickup Location");
105
	    headerRow.createCell(19).setCellValue("Customer A/C Code");
106
 
107
	    Date awbDate = new Date();
108
	    int serialNo = 0;
109
	    for(int i = 0; i<orders.size(); i++){
110
	    	Order order = orders.get(i);
111
			if(order.getLogistics_provider_id()!=providerId)
112
				continue;
113
			serialNo++;
114
	    	Row contentRow = sheet.createRow((short)serialNo);
115
		    contentRow.createCell(0).setCellValue(serialNo);
116
		    contentRow.createCell(1).setCellValue(order.getAirwaybill_no());
117
		    Cell awbDateCell = contentRow.createCell(2);
118
		    awbDateCell.setCellValue(awbDate);
119
		    awbDateCell.setCellStyle(dateCellStyle);		    
120
		    contentRow.createCell(3).setCellValue(order.getId());
744 chandransh 121
		    contentRow.createCell(4).setCellValue(getValueForEmptyString(order.getCustomer_name()));
122
		    contentRow.createCell(5).setCellValue(getValueForEmptyString(order.getCustomer_address1()));
123
		    contentRow.createCell(6).setCellValue(getValueForEmptyString(order.getCustomer_address2()));
124
		    contentRow.createCell(7).setCellValue(getValueForEmptyString(order.getCustomer_city()));
125
		    contentRow.createCell(8).setCellValue(getValueForEmptyString(order.getCustomer_state()));
126
		    contentRow.createCell(9).setCellValue(getValueForEmptyString(order.getCustomer_pincode()));
127
		    contentRow.createCell(10).setCellValue(getValueForEmptyString(order.getCustomer_mobilenumber()));
734 chandransh 128
		    contentRow.createCell(11).setCellValue("-");
679 chandransh 129
		    contentRow.createCell(12).setCellValue("Prepaid");
130
		    contentRow.createCell(13).setCellValue(0);
131
		    List<LineItem> lineItems = order.getLineitems();
132
		    LineItem lineItem = lineItems.get(0);
744 chandransh 133
		    contentRow.createCell(14).setCellValue(order.getTotal_amount());
679 chandransh 134
		    contentRow.createCell(15).setCellValue(lineItem.getId());
135
		    Cell weightCell = contentRow.createCell(16);
919 rajveer 136
		    weightCell.setCellValue(lineItem.getTotal_weight());
679 chandransh 137
		    weightCell.setCellStyle(weightStyle);
138
		    contentRow.createCell(17).setCellValue(lineItem.getBrand() + " " + lineItem.getModel_number() + " " + lineItem.getModel_name() + " " + lineItem.getColor());
139
		    contentRow.createCell(18).setCellValue(warehouse.getLocation());
140
		    contentRow.createCell(19).setCellValue(provider.getAccountNo());
141
	    }
142
 
143
		// Write the workbook to the output stream
144
		try {
145
			wb.write(baosXLS);
146
			baosXLS.close();
147
		} catch (IOException e) {
148
			e.printStackTrace();
149
		}
150
 
151
		return baosXLS;
152
	}
744 chandransh 153
 
154
	private String getValueForEmptyString(String s){
155
		if(s==null || s.equals(""))
156
			return "-";
157
		else
158
			return s; 
159
	}
160
 
679 chandransh 161
	/**
162
	 * @param args
163
	 */
164
	public static void main(String[] args) {
165
		System.out.println("Hey There");
166
		CourierDetailsGenerator g = new CourierDetailsGenerator();
167
		try {
744 chandransh 168
			FileOutputStream f = new FileOutputStream("/home/ashish/Downloads/courier-details.xls");
679 chandransh 169
			ByteArrayOutputStream baosXLS = g.generateCourierDetails(1, 1);
170
			baosXLS.writeTo(f);
171
			f.close();
172
		} catch (FileNotFoundException e) {
173
			e.printStackTrace();
174
		} catch (IOException e) {
175
			e.printStackTrace();
176
		}
744 chandransh 177
		System.out.println("Successfully generated the detailed courier report");
679 chandransh 178
	}
179
 
180
}