Subversion Repositories SmartDukaan

Rev

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