Subversion Repositories SmartDukaan

Rev

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

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