Subversion Repositories SmartDukaan

Rev

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