Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
2674 vikas 1
package in.shop2020.serving.controllers;
2
 
7065 kshitij.so 3
import in.shop2020.config.ConfigException;
4689 anupam.sin 4
import in.shop2020.crm.Activity;
5
import in.shop2020.crm.ActivityType;
3578 mandeep.dh 6
import in.shop2020.crm.SearchFilter;
7
import in.shop2020.crm.Ticket;
8
import in.shop2020.crm.TicketCategory;
4689 anupam.sin 9
import in.shop2020.crm.TicketPriority;
3578 mandeep.dh 10
import in.shop2020.crm.TicketStatus;
7826 manish.sha 11
import in.shop2020.logistics.LogisticsServiceException;
5845 mandeep.dh 12
import in.shop2020.logistics.PickupStore;
7826 manish.sha 13
import in.shop2020.logistics.Provider;
7190 amar.kumar 14
import in.shop2020.model.v1.catalog.CatalogService;
15
import in.shop2020.model.v1.catalog.CatalogServiceException;
16
import in.shop2020.model.v1.catalog.Item;
7826 manish.sha 17
import in.shop2020.model.v1.order.AmazonOrder;
7399 anupam.sin 18
import in.shop2020.model.v1.order.HotspotStore;
2674 vikas 19
import in.shop2020.model.v1.order.LineItem;
20
import in.shop2020.model.v1.order.Order;
3578 mandeep.dh 21
import in.shop2020.model.v1.order.OrderStatus;
7393 anupam.sin 22
import in.shop2020.model.v1.order.StoreOrderDetail;
4689 anupam.sin 23
import in.shop2020.model.v1.order.TransactionServiceException;
4142 mandeep.dh 24
import in.shop2020.model.v1.user.Address;
4689 anupam.sin 25
import in.shop2020.model.v1.user.UserContextException;
2728 vikas 26
import in.shop2020.payments.Attribute;
27
import in.shop2020.payments.Constants;
2674 vikas 28
import in.shop2020.payments.Payment;
2728 vikas 29
import in.shop2020.payments.PaymentException;
4142 mandeep.dh 30
import in.shop2020.payments.PaymentService.Client;
4689 anupam.sin 31
import in.shop2020.serving.auth.CRMAuthorizingRealm;
3090 mandeep.dh 32
import in.shop2020.serving.model.ShipmentUpdate;
5845 mandeep.dh 33
import in.shop2020.serving.services.AramexTrackingService;
3090 mandeep.dh 34
import in.shop2020.serving.services.BlueDartTrackingService;
5303 phani.kuma 35
import in.shop2020.serving.services.DelhiveryTrackingService;
7942 manish.sha 36
import in.shop2020.serving.services.FedExTrackingService;
7200 kshitij.so 37
import in.shop2020.serving.services.RedExpressTrackingService;
3578 mandeep.dh 38
import in.shop2020.thrift.clients.CRMClient;
7190 amar.kumar 39
import in.shop2020.thrift.clients.CatalogClient;
6322 amar.kumar 40
import in.shop2020.thrift.clients.HelperClient;
5845 mandeep.dh 41
import in.shop2020.thrift.clients.LogisticsClient;
3128 rajveer 42
import in.shop2020.thrift.clients.PaymentClient;
43
import in.shop2020.thrift.clients.TransactionClient;
4142 mandeep.dh 44
import in.shop2020.thrift.clients.UserClient;
7065 kshitij.so 45
import in.shop2020.thrift.clients.config.ConfigClient;
7942 manish.sha 46
import in.shop2020.utils.FedExShipAccountInfo;
3546 mandeep.dh 47
import in.shop2020.utils.ModelUtils;
6322 amar.kumar 48
import in.shop2020.warehouse.WarehouseService;
2674 vikas 49
 
6322 amar.kumar 50
import java.io.BufferedInputStream;
51
import java.io.File;
52
import java.io.FileInputStream;
53
import java.io.FileWriter;
54
import java.io.IOException;
55
import java.io.InputStream;
6912 anupam.sin 56
import java.text.SimpleDateFormat;
2674 vikas 57
import java.util.ArrayList;
6912 anupam.sin 58
import java.util.Calendar;
4416 mandeep.dh 59
import java.util.Collections;
6912 anupam.sin 60
import java.util.Date;
7836 anupam.sin 61
import java.util.HashMap;
4689 anupam.sin 62
import java.util.HashSet;
2674 vikas 63
import java.util.List;
7836 anupam.sin 64
import java.util.Map;
4689 anupam.sin 65
import java.util.Set;
4416 mandeep.dh 66
import java.util.concurrent.Callable;
67
import java.util.concurrent.Executors;
68
import java.util.concurrent.TimeUnit;
2674 vikas 69
 
6322 amar.kumar 70
import javax.servlet.ServletOutputStream;
71
 
4689 anupam.sin 72
import net.htmlparser.jericho.Source;
73
 
5845 mandeep.dh 74
import org.apache.commons.lang.StringUtils;
2674 vikas 75
import org.apache.log4j.Logger;
2728 vikas 76
import org.apache.thrift.TException;
4142 mandeep.dh 77
import org.apache.thrift.transport.TTransportException;
2674 vikas 78
 
7942 manish.sha 79
import com.ShipWebServiceClient;
80
import com.fedex.ship.stub.CompletedPackageDetail;
81
import com.fedex.ship.stub.CompletedShipmentDetail;
82
import com.fedex.ship.stub.ProcessShipmentReply;
83
import com.fedex.ship.stub.TrackingId;
84
 
2674 vikas 85
/**
86
 * @author vikas
3578 mandeep.dh 87
 * 
2674 vikas 88
 */
89
@SuppressWarnings("serial")
90
public class UserOrderInfoController extends BaseController {
7372 kshitij.so 91
	private static Logger                  log                     = Logger.getLogger(Class.class);
92
	private static BlueDartTrackingService blueDartTrackingService = new BlueDartTrackingService();
93
	private static AramexTrackingService aramexTrackingService     = new AramexTrackingService();
94
	private static DelhiveryTrackingService delhiveryTrackingService     = new DelhiveryTrackingService();
95
	private static RedExpressTrackingService redexpressTrackingService = new RedExpressTrackingService();
7942 manish.sha 96
	//Start:- Added by Manish Sharma for fedex Integration- Shipment Tracking on 31-Jul-2013
97
	private static FedExTrackingService fedexTrackingService = new FedExTrackingService();
98
	//End:- Added by Manish Sharma for fedex Integration- Shipment Tracking on 31-Jul-2013
99
 
7372 kshitij.so 100
	private long                 orderId;
101
	private Order                order;
102
	private List<Payment>        payments;
103
	private List<ShipmentUpdate> shipmentUpdates = new ArrayList<ShipmentUpdate>();
104
	private Long                 codTicketId;
105
	private List<Address>        addresses;
106
	private Set<OrderStatus>     setOfcancellableStates;
107
	private String               cancellationInitiator;
108
	private String               cancelReason;
109
	private String               body;
110
	private String               line1;
111
	private String               line2;
112
	private String               city;
113
	private String               state;
114
	private String               pin;
115
	private String				 freebieItem;
116
	private String				 dealText;
117
	private String 				 parentOrderIdForFreebieOrder;
118
	private String				 freebieOrderId;
7393 anupam.sin 119
	private double               cashAmount;
120
	private double               cardAmount;
121
    private StoreOrderDetail     storeOrderDetail;
7826 manish.sha 122
    //Start:- Added by Manish Sharma for Displaying Logistics Provider Id and Amazon Order Id on 25-Jul-2013
7830 anupam.sin 123
    private AmazonOrder          amazonOrder = null;
7836 anupam.sin 124
    private String               providerName = "N/A";
7826 manish.sha 125
    //End:- Added by Manish Sharma for Displaying Logistics Provider Id and Amazon Order Id on 25-Jul-2013
2674 vikas 126
 
7372 kshitij.so 127
	public UserOrderInfoController() {
128
		super();
129
		setOfcancellableStates = new HashSet<OrderStatus>();
130
		setOfcancellableStates.add(OrderStatus.SUBMITTED_FOR_PROCESSING);
131
		setOfcancellableStates.add(OrderStatus.INVENTORY_LOW);
132
		setOfcancellableStates.add(OrderStatus.LOW_INV_PO_RAISED);
133
		setOfcancellableStates.add(OrderStatus.LOW_INV_REVERSAL_IN_PROCESS);
134
		setOfcancellableStates.add(OrderStatus.LOW_INV_NOT_AVAILABLE_AT_HOTSPOT);
135
		setOfcancellableStates.add(OrderStatus.ACCEPTED);
136
		setOfcancellableStates.add(OrderStatus.BILLED);
137
	}
7836 anupam.sin 138
 
139
	public static final Map<Long, String> providerNameMap = new HashMap<Long, String>() {
140
        {
141
            put(1l, "BlueDart");
142
            put(2l, "Aramex");
143
            put(3l, "Delhivery");
144
            put(4l, "SelfPickup");
145
            put(5l, "Runner");
146
            put(6l, "RedExpress");
147
        }
148
	};
149
 
7372 kshitij.so 150
	public String index() {
151
		try {
152
			PaymentClient paymentServiceClient = new PaymentClient();
153
			TransactionClient transactionServiceClient = new TransactionClient();
7826 manish.sha 154
			LogisticsClient logisticsServiceClient = new LogisticsClient();
155
 
7372 kshitij.so 156
			order = transactionServiceClient.getClient().getOrder(orderId);
7826 manish.sha 157
			//Start:- Added by Manish Sharma for Displaying Logistics Provider Id and Amazon Order Id on 25-Jul-2013
7830 anupam.sin 158
			try {
159
			    amazonOrder = transactionServiceClient.getClient().getAmazonOrder(orderId);
160
			} catch (Exception e) {
161
			    log.error("No Amazon order found with orderId " + orderId, e);
162
			}
7836 anupam.sin 163
			if (order.getLogistics_provider_id() > 0) {
164
			    setProviderName(providerNameMap.get(order.getLogistics_provider_id()));
165
			}
7826 manish.sha 166
			//End:- Added by Manish Sharma for Displaying Logistics Provider Id and Amazon Order Id on 25-Jul-2013
7393 anupam.sin 167
			if(order.getSource() == 2) {
168
    			storeOrderDetail = transactionServiceClient.getClient().getStoreOrderDetail(order.getId(),order.getStoreId());
7399 anupam.sin 169
    			if(order.getStatus().getValue() == 15 || order.getStatus().getValue() == 34) {
170
    			    setCashAmount(storeOrderDetail.getCashRefundAmount());
171
                    setCardAmount(storeOrderDetail.getCardRefundAmount()); 
172
    			} else {
173
        			setCashAmount(storeOrderDetail.getCashAmount());
174
        			setCardAmount(storeOrderDetail.getCardAmount());
175
    			}
7393 anupam.sin 176
			}
7372 kshitij.so 177
			if(order.getFreebieItemId()>0) {
178
				CatalogService.Client catalogClient = new CatalogClient().getClient();
179
				Item item = catalogClient.getItem(order.getFreebieItemId());
180
				freebieItem = item.getBrand() + " " + item.getModelName() + " " + item.getModelNumber() + " " + item.getColor();
181
			}
3090 mandeep.dh 182
 
7372 kshitij.so 183
			if(order.getLineitems().get(0).getDealText()!=null && !order.getLineitems().get(0).getDealText().isEmpty()) {
184
				dealText = order.getLineitems().get(0).getDealText();
185
			}
186
			List<in.shop2020.model.v1.order.Attribute> attributes = 
187
				transactionServiceClient.getClient().getAllAttributesForOrderId(orderId);
3578 mandeep.dh 188
 
7372 kshitij.so 189
			for(in.shop2020.model.v1.order.Attribute attribute: attributes){
190
				if(attribute.getName().equals("parentOrderIdForFreebie")){
191
					parentOrderIdForFreebieOrder = attribute.getValue();
192
				} else if (attribute.getName().equals("freebieOrderId")){
193
					freebieOrderId = attribute.getValue();
194
				}
195
			}
4689 anupam.sin 196
 
7372 kshitij.so 197
			payments = paymentServiceClient.getClient()
198
			.getPaymentForTxnId(order.getTransactionId());
4689 anupam.sin 199
 
4416 mandeep.dh 200
 
3578 mandeep.dh 201
 
7372 kshitij.so 202
			// Spawning a thread to capture shipment updates from Bluedart
203
			// This is done to ensure that response from Crm web app is sent
204
			// within given time limits. Also, we wont be affected in the cases 
205
			// where bluedart site is down or slow
206
			Executors.newSingleThreadExecutor().invokeAll(Collections.singletonList(new Callable<Boolean>() {
207
				public Boolean call() throws Exception {
208
					if (order.getLogistics_provider_id() == 1)
209
						shipmentUpdates = blueDartTrackingService.getUpdates(order.getAirwaybill_no());
210
					else if (order.getLogistics_provider_id() == 2) {
211
						shipmentUpdates = aramexTrackingService.getUpdates(order.getAirwaybill_no());
212
					}
213
					else if (order.getLogistics_provider_id() == 3) {
214
						shipmentUpdates = delhiveryTrackingService.getUpdates(order.getAirwaybill_no());
215
					}
216
					else if (order.getLogistics_provider_id() == 6) {
217
						shipmentUpdates = redexpressTrackingService.getUpdates(order.getAirwaybill_no());
218
					}
7942 manish.sha 219
					//Start:- Added by Manish Sharma for fedex Integration- Shipment Tracking on 31-Jul-2013
220
					else if (order.getLogistics_provider_id() == 7) {
221
						shipmentUpdates = fedexTrackingService.getUpdates(order.getAirwaybill_no());
222
					}
223
					//End:- Added by Manish Sharma for fedex Integration- Shipment Tracking on 31-Jul-2013
7372 kshitij.so 224
					else {
225
						shipmentUpdates = new ArrayList<ShipmentUpdate>();
226
						log.error("Error : providerId = " + order.getLogistics_provider_id() + "for orderId : " + order.getId());
227
					}
228
					return true;
229
				}
230
			}), 60, TimeUnit.SECONDS);
2674 vikas 231
 
7372 kshitij.so 232
			if (order.isCod() && OrderStatus.COD_VERIFICATION_PENDING.equals(order.getStatus())) {
233
				populateCODTicketId(order.getCustomer_id());
234
			}
4142 mandeep.dh 235
 
7372 kshitij.so 236
			if (canEditOrderAddress()) {
237
				userContextServiceClient = new UserClient().getClient();
238
				addresses = userContextServiceClient.getAllAddressesForUser(order.getCustomer_id());
239
			}
3578 mandeep.dh 240
 
7372 kshitij.so 241
		} catch (TTransportException e) {
242
			log.error("Unable to create thrift Client", e);
243
		} catch (TransactionServiceException e) {
244
			addActionError("Invalid order id or no order selected.");
245
		} catch (TException e) {
246
			log.error("Unable to get thrift Client", e);
247
		} catch (PaymentException e) {
248
			log.error("Unable to get payments for transctionId : " + order.getTransactionId(), e);
249
		} catch (InterruptedException e) {
250
			log.error("Thread was interrupted", e);
251
		} catch (UserContextException e) {
252
			log.error("Unable to get addresses for user : " + order.getCustomer_id(), e);
253
		} catch(CatalogServiceException csex) {
254
			log.error("Unable to get item details for itemId: " + order.getFreebieItemId(), csex);
255
		}
256
		return INDEX;
257
	}
4689 anupam.sin 258
 
7372 kshitij.so 259
	private boolean canEditOrderAddress() {
260
		return false;
261
	}
4689 anupam.sin 262
 
7372 kshitij.so 263
	private void populateCODTicketId(long customerId) {
264
		try {
265
			SearchFilter searchFilter = new SearchFilter();
266
			searchFilter.setTicketCategory(TicketCategory.COD_VERIFICATION);
267
			searchFilter.setTicketStatuses(new ArrayList<TicketStatus>());
268
			searchFilter.getTicketStatuses().add(TicketStatus.OPEN);
269
			searchFilter.getTicketStatuses().add(TicketStatus.REOPEN);
270
			searchFilter.setCustomerId(customerId);
271
			crmServiceClient = new CRMClient().getClient();
272
			List<Ticket> tickets = crmServiceClient.getTickets(searchFilter);
273
			if (tickets != null && !tickets.isEmpty()) {
274
				codTicketId = tickets.get(0).getId();
275
			}
276
		} catch (TException e) {
277
			log.error("Error fetching tickets for customerId: " + customerId, e);
278
		}
279
	}
4689 anupam.sin 280
 
7372 kshitij.so 281
	public String markOrderForCancellation() {
282
		try{
283
			TransactionClient transactionServiceClient = new TransactionClient();
284
			log.info("URL = " + request.getRequestURI());
285
			log.info("Initiator = " + request.getParameter("cancellationInitiator"));
286
			log.info("orderId = " + request.getParameter("orderId"));
7393 anupam.sin 287
 
288
			order = transactionServiceClient.getClient().getOrder(orderId);
289
 
7499 anupam.sin 290
			/**
291
			 * If the order is from store then refund the order directly.
292
			 */
7393 anupam.sin 293
			if (order.getSource() == 2) {
294
			    String plainTextbody = "";
295
			    if(body!=null && !body.isEmpty()){
296
                    plainTextbody = new Source(body).getTextExtractor().toString();
297
                }
7612 anupam.sin 298
 
299
			    if(cashAmount + cardAmount > order.getAdvanceAmount()) {
300
			        log.error("Could not mark order for Cancellation, OrderId : " + orderId);
301
			        return "index";
302
			    }
303
 
304
			    transactionServiceClient.getClient().saveRefundAmountsForStoreOrder(orderId, order.getStoreId(), cashAmount, cardAmount);
7399 anupam.sin 305
			    if (cancellationInitiator.equals("CUSTOMER")) {
306
			        transactionServiceClient.getClient().markOrderCancellationRequestReceived(orderId);
307
			        transactionServiceClient.getClient().markOrderCancellationRequestConfirmed(orderId);
308
			    }
7393 anupam.sin 309
			    transactionServiceClient.getClient().refundOrder(orderId, currentAgentEmailId, cancelReason + " : " + plainTextbody);
7499 anupam.sin 310
			} else {
311
		         if (cancellationInitiator.equals("CUSTOMER")) {
312
	                transactionServiceClient.getClient().markOrderCancellationRequestReceived(orderId);
313
	                long creatorId = CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId();
314
	                Ticket ticket = new Ticket();
315
	                String plainTextbody = "";
316
	                if(body!=null && !body.isEmpty()){
317
	                    plainTextbody = new Source(body).getTextExtractor().toString();
318
	                }
319
	                ticket.setDescription("Creating ticket for Cancellation Request Received, Reason : " + cancelReason + " : " + plainTextbody);
320
	                ticket.setCreatorId(creatorId);
321
	                ticket.setStatus(TicketStatus.OPEN);
322
	                ticket.setPriority(TicketPriority.HIGH);
323
	                ticket.setCategory(TicketCategory.ORDER_CANCELLATION);
324
	                ticket.setOrderId(orderId);
4689 anupam.sin 325
 
7499 anupam.sin 326
	                Activity activity = new Activity();
327
	                activity.setDescription("Creating Ticket");
328
	                activity.setType(ActivityType.OTHER);
329
	                activity.setTicketPriority(TicketPriority.HIGH);
330
	                activity.setTicketStatus(TicketStatus.OPEN);
331
	                activity.setCreatorId(creatorId);
332
	                activity.setTicketDescription("Creating ticket for Cancellation Request Received, Reason : " + cancelReason + " : " + plainTextbody);
333
	                activity.setTicketCategory(TicketCategory.ORDER_CANCELLATION);
4689 anupam.sin 334
 
7499 anupam.sin 335
	                ticket.setCustomerId(order.getCustomer_id());
336
	                activity.setCustomerId(order.getCustomer_id());
337
	                ticket.setCustomerName(order.getCustomer_name());
338
	                activity.setCustomerName(order.getCustomer_name());
339
	                ticket.setCustomerEmailId(order.getCustomer_email());
340
	                activity.setCustomerEmailId(order.getCustomer_email());
341
	                ticket.setCustomerMobileNumber(order.getCustomer_mobilenumber());
342
	                activity.setCustomerMobileNumber(order.getCustomer_mobilenumber());
4689 anupam.sin 343
 
7499 anupam.sin 344
	                crmServiceClient = new CRMClient().getClient();
345
	                crmServiceClient.insertTicket(ticket, activity);
346
	            } 
347
	            else if (cancellationInitiator.equals("INTERNAL")) {
348
	                String plainTextbody = "";
349
	                if(body!=null && !body.isEmpty()){
350
	                    plainTextbody = new Source(body).getTextExtractor().toString();
351
	                }
352
	                boolean status_returned = transactionServiceClient.getClient().refundOrder(orderId, currentAgentEmailId, cancelReason + " : " + plainTextbody);
353
	                if (status_returned) {
354
	                    order = transactionServiceClient.getClient().getOrder(orderId);
355
	                    long creatorId = CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId();
356
	                    Ticket ticket = new Ticket();
357
	                    ticket.setDescription("Creating ticket for Order Cancellation due to Low inventory");
358
	                    ticket.setCreatorId(creatorId);
359
	                    ticket.setAssigneeId(36);
360
	                    ticket.setStatus(TicketStatus.OPEN);
361
	                    ticket.setPriority(TicketPriority.MEDIUM);
362
	                    ticket.setCategory(TicketCategory.LOW_INVENTORY_CANCELLED_ORDERS);
363
	                    ticket.setOrderId(orderId);
364
	                    ticket.setCustomerId(order.getCustomer_id());
365
	                    ticket.setCustomerName(order.getCustomer_name());
366
	                    ticket.setCustomerEmailId(order.getCustomer_email());
367
	                    ticket.setCustomerMobileNumber(order.getCustomer_mobilenumber());
6322 amar.kumar 368
 
7499 anupam.sin 369
	                    Activity activity = new Activity();
370
	                    activity.setDescription("Creating Ticket");
371
	                    activity.setType(ActivityType.OTHER);
372
	                    activity.setTicketPriority(TicketPriority.MEDIUM);
373
	                    activity.setTicketStatus(TicketStatus.OPEN);
374
	                    activity.setCreatorId(creatorId);
375
	                    activity.setTicketAssigneeId(36);
376
	                    activity.setTicketCategory(TicketCategory.LOW_INVENTORY_CANCELLED_ORDERS);
377
	                    activity.setTicketDescription("Creating ticket for Order Cancellation due to Low inventory");
378
	                    activity.setCustomerId(order.getCustomer_id());
379
	                    activity.setCustomerName(order.getCustomer_name());
380
	                    activity.setCustomerEmailId(order.getCustomer_email());
381
	                    activity.setCustomerMobileNumber(order.getCustomer_mobilenumber());
6322 amar.kumar 382
 
7499 anupam.sin 383
	                    crmServiceClient = new CRMClient().getClient();
384
	                    crmServiceClient.insertTicket(ticket, activity);
385
	                }
386
	            }
7372 kshitij.so 387
			}
388
		} catch(Exception e) {
389
			log.error("Could not mark order for Cancellation, OrderId : " + orderId, e);
390
		}
391
		return index();
392
	}
7221 kshitij.so 393
 
7372 kshitij.so 394
	public void getOrderConfirmationMail() throws IOException, TException, ConfigException, TransactionServiceException {
395
		TransactionClient transactionServiceClient = new TransactionClient();
396
		order = transactionServiceClient.getClient().getOrder(orderId);
397
		long source = order.getTransactionId();
398
		HelperClient helperClient = new HelperClient("helper_service_server","helper_service_server_port");
399
		String mail = helperClient.getClient().getOrderConfirmationMail(source);
400
		File file = new File("/tmp/temp");
7643 manish.sha 401
		//Start:- Added by Manish Sharma for resolving Exception for getting order delivery and confirmation mail on 27-Jun-2013
402
		if(mail.isEmpty() || mail==null || mail=="")
403
			mail="<html><body><p>Sorry, Required e-mail for mentioned order does not exist in the system !!!</p></body></html>";
404
		//End:- Added by Manish Sharma for resolving Exception for getting order delivery and confirmation mail on 27-Jun-2013
7221 kshitij.so 405
 
7372 kshitij.so 406
		FileWriter writer = new FileWriter(file);
407
		writer.append(mail);
408
		writer.close();
4689 anupam.sin 409
 
7372 kshitij.so 410
		byte[] buffer = new byte[(int)file.length()];
411
		InputStream input = null;
412
		try {
413
			int totalBytesRead = 0;
414
			input = new BufferedInputStream(new FileInputStream(file));
415
			while(totalBytesRead < buffer.length){
416
				int bytesRemaining = buffer.length - totalBytesRead;
417
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
418
				if (bytesRead > 0){
419
					totalBytesRead = totalBytesRead + bytesRead;
420
				}
421
			}
422
		}
423
		finally {
424
			input.close();
425
			file.delete();
426
		}
3578 mandeep.dh 427
 
4142 mandeep.dh 428
 
7372 kshitij.so 429
		response.setHeader("Content-disposition", "inline; filename=" + "OrderDetail_"+orderId );
2674 vikas 430
 
7372 kshitij.so 431
		ServletOutputStream sos;
432
		try {
433
			sos = response.getOutputStream();
434
			sos.write(buffer);
435
			sos.flush();
436
		} catch (IOException e) {
437
			System.out.println("Unable to stream the manifest file");
438
		}   
439
	}
4142 mandeep.dh 440
 
7372 kshitij.so 441
	public void getOrderDeliveryMail() throws IOException, TException, ConfigException, TransactionServiceException {
442
		HelperClient helperClient = new HelperClient("helper_service_server","helper_service_server_port");
443
		String mail = helperClient.getClient().getOrderDeliveryMail(orderId);
444
		File file = new File("/tmp/temp");
7643 manish.sha 445
		//Start:- Added by Manish Sharma for resolving Exception for getting order delivery and confirmation mail on 27-Jun-2013
446
		if(mail.isEmpty() || mail==null || mail=="")
447
			mail="<html><body><p>Sorry, Required e-mail for mentioned order does not exist in the system anymore !!!</p></body></html>";
448
		//End:- Added by Manish Sharma for resolving Exception for getting order delivery and confirmation mail on 27-Jun-2013
7372 kshitij.so 449
		FileWriter writer = new FileWriter(file);
450
		writer.append(mail);
451
		writer.close();
2674 vikas 452
 
7372 kshitij.so 453
		byte[] buffer = new byte[(int)file.length()];
454
		InputStream input = null;
455
		try {
456
			int totalBytesRead = 0;
457
			input = new BufferedInputStream(new FileInputStream(file));
458
			while(totalBytesRead < buffer.length){
459
				int bytesRemaining = buffer.length - totalBytesRead;
460
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
461
				if (bytesRead > 0){
462
					totalBytesRead = totalBytesRead + bytesRead;
463
				}
464
			}
465
		}
466
		finally {
467
			input.close();
468
			file.delete();
469
		}
3578 mandeep.dh 470
 
5845 mandeep.dh 471
 
7372 kshitij.so 472
		response.setHeader("Content-disposition", "inline; filename=" + "OrderDetail_"+orderId );
4142 mandeep.dh 473
 
7372 kshitij.so 474
		ServletOutputStream sos;
475
		try {
476
			sos = response.getOutputStream();
477
			sos.write(buffer);
478
			sos.flush();
479
		} catch (IOException e) {
480
			System.out.println("Unable to stream the manifest file");
481
		}   
482
	}
2674 vikas 483
 
7372 kshitij.so 484
	public boolean canOrderBeCancelled() {
485
		if (setOfcancellableStates.contains(order.getStatus())) {
486
			return true;
487
		}
488
		return false;
489
	}
2674 vikas 490
 
7372 kshitij.so 491
	public String getPaymentGateway(Payment payment) {
492
		String gatewayName = "";
2674 vikas 493
 
7372 kshitij.so 494
		try {
495
			Client paymentServiceClient = new PaymentClient().getClient();
496
			gatewayName = paymentServiceClient.getPaymentGateway(payment.getGatewayId()).getName();
497
		} catch (TTransportException e) {
498
		} catch (PaymentException e) {
499
		} catch (TException e) {
500
		}
3090 mandeep.dh 501
 
7372 kshitij.so 502
		return gatewayName;
503
	}
3499 mandeep.dh 504
 
7372 kshitij.so 505
	public int convertDouble(double value) {
506
		return (int)value;
507
	}
4142 mandeep.dh 508
 
7372 kshitij.so 509
	public String getInsuranceExpiryDate(long DeliveryDate) {
510
		if (DeliveryDate == 0) {
511
			return "N/A";
512
		}
513
		Calendar cal = Calendar.getInstance();
514
		cal.setTimeInMillis(DeliveryDate);
515
		cal.add(Calendar.YEAR, 1);
516
		SimpleDateFormat sdf = new SimpleDateFormat("dd MMM, yyyy");
517
		return sdf.format(cal.getTime());
518
	}
4142 mandeep.dh 519
 
7372 kshitij.so 520
	public String getShippingAddressOfStore(long storeId) {
521
		try {
522
			in.shop2020.logistics.LogisticsService.Client client = new LogisticsClient().getClient();
523
			PickupStore store = client.getPickupStore(storeId);
524
			return StringUtils.join(new String[] {
525
					store.getName(),
526
					store.getLine1(),
527
					store.getLine2(),
528
					store.getPin(),
529
					store.getCity(),
530
					store.getState(),
531
					store.getPhone()}, ",");
532
		} catch (Exception e) {
533
			return "";
534
		}
535
	}
4241 anupam.sin 536
 
7372 kshitij.so 537
	public String getAddress(Order order) {
538
		return ModelUtils.extractAddressFromOrder(order);
539
	}
4241 anupam.sin 540
 
7372 kshitij.so 541
	public String changeShippingAddress() {
542
		try {
543
			TransactionClient transactionServiceClient = new TransactionClient();
7942 manish.sha 544
			in.shop2020.model.v1.order.TransactionService.Client transactionClient
545
		    = transactionServiceClient.getClient();
546
			transactionClient.changeShippingAddress(orderId, line1, line2, city, state, pin);
547
			in.shop2020.model.v1.order.Order t_order = transactionClient.getOrder(orderId);
548
			if(t_order.getLogistics_provider_id()==7L){
549
				FedExShipAccountInfo fedexAccountInfo = FedExShipAccountInfo.getFedExInfo();
550
				ProcessShipmentReply  processShipmentReply = ShipWebServiceClient.getShipmentCreationReply(t_order,fedexAccountInfo.getClientDetail(),fedexAccountInfo.getWad(),fedexAccountInfo.getEndPointAddress());
551
				CompletedShipmentDetail completedShipmentDetails = processShipmentReply.getCompletedShipmentDetail();
552
				CompletedPackageDetail completedPackageDetails =  completedShipmentDetails.getCompletedPackageDetails(0);
553
				TrackingId  trackId= completedPackageDetails.getTrackingIds(0);
554
				t_order = transactionClient.updateOrderAWB(t_order.getId(), trackId.getTrackingNumber());
555
			}
7372 kshitij.so 556
		} catch(Exception e) {
557
			log.error("Unable to update address for orderId : " + orderId + "and address : " + 
558
					line1 + ", " + line2+ ", " + city + ", " + state + ", " + pin, e);
559
		}
560
		return null;
561
	}
7399 anupam.sin 562
 
7730 anupam.sin 563
	public String convertStoreToNormal() {
564
	    try{
565
	        TransactionClient transactionServiceClient = new TransactionClient();
566
	        transactionServiceClient.getClient().convertStoreToNormal(orderId);
567
	    } catch(Exception e) {
568
	        log.error("Unable to convert to normal order", e);
569
	    }
570
	    return null;
571
	}
572
 
7399 anupam.sin 573
	public String getStoreDetails(long storeId) {
574
	    try {
575
	        TransactionClient tcl = new TransactionClient();
576
	        HotspotStore store = tcl.getClient().getHotspotStore(storeId, "");
577
	        return store.getHotspotId() + " - " + store.getCity(); 
578
	    } catch (Exception e) {
579
	        log.error("Unable to get hostspotStore for id : " + storeId, e);
580
	    }
581
	    return "N/A";
582
	}
4689 anupam.sin 583
 
7372 kshitij.so 584
	public Address getShippingAddress(Order order) {
585
		Address address = new Address();
586
		address.setLine1(order.getCustomer_address1());
587
		address.setLine2(order.getCustomer_address2());
588
		address.setCity(order.getCustomer_city());
589
		address.setState(order.getCustomer_state());
590
		address.setPin(order.getCustomer_pincode());
591
		return address;
592
	}
4689 anupam.sin 593
 
7372 kshitij.so 594
	public String getPaymentMethod(List<Attribute> paymentAttributes) {
595
		String paymentMethod = null;
596
		if (paymentAttributes == null || paymentAttributes.isEmpty()) {
597
			return "N/A";
598
		}
599
		for (Attribute a : paymentAttributes) {
600
			if ("payMethod".equals(a.getName())) {
601
				paymentMethod = Constants.PAYMENT_METHOD.get(a.getValue());
602
				break;
603
			}
604
		}
605
		return paymentMethod != null ? paymentMethod : "N/A";
606
	}
4689 anupam.sin 607
 
7372 kshitij.so 608
	public void setOrderId(String orderId) {
609
		try {
610
			this.orderId = Long.parseLong(orderId);
611
		} catch (NumberFormatException e) {
612
			log.error(e);
613
		}
614
	}
4689 anupam.sin 615
 
7372 kshitij.so 616
	public List<Payment> getPayments() {
617
		return payments;
618
	}
4689 anupam.sin 619
 
7372 kshitij.so 620
	public List<ShipmentUpdate> getShipmentUpdates() {
621
		return shipmentUpdates;
622
	}
4689 anupam.sin 623
 
7372 kshitij.so 624
	public void setShipmentUpdates(List<ShipmentUpdate> shipmentUpdates) {
625
		this.shipmentUpdates = shipmentUpdates;
626
	}
6985 anupam.sin 627
 
7372 kshitij.so 628
	public Order getOrder() {
629
		return order;
630
	}
6985 anupam.sin 631
 
7372 kshitij.so 632
	public void setOrder(Order order) {
633
		this.order = order;
634
	}
6985 anupam.sin 635
 
7372 kshitij.so 636
	public Long getCodTicketId() {
637
		return codTicketId;
638
	}
6985 anupam.sin 639
 
7372 kshitij.so 640
	public void setCodTicketId(Long codTicketId) {
641
		this.codTicketId = codTicketId;
642
	}
6985 anupam.sin 643
 
7372 kshitij.so 644
	public List<Address> getAddresses() {
645
		return addresses;
646
	}
6985 anupam.sin 647
 
7372 kshitij.so 648
	public void setAddresses(List<Address> addresses) {
649
		this.addresses = addresses;
650
	}
6985 anupam.sin 651
 
7372 kshitij.so 652
	public String getOrderStatusDescription(Order order) {
653
		String status = order.getStatus().getDescription();
6985 anupam.sin 654
 
7372 kshitij.so 655
		if (order.getStatus() == OrderStatus.DELIVERY_SUCCESS) {
656
			status = "Completed";
657
		}
6985 anupam.sin 658
 
7372 kshitij.so 659
		return status;
660
	}
6985 anupam.sin 661
 
7372 kshitij.so 662
	public String getCancellationInitiator() {
663
		return cancellationInitiator;
664
	}
7190 amar.kumar 665
 
7372 kshitij.so 666
	public void setCancellationInitiator(String cancellationInitiator) {
667
		this.cancellationInitiator = cancellationInitiator;
668
	}
669
 
670
	public String getCancelReason() {
671
		return cancelReason;
672
	}
673
 
674
	public void setCancelReason(String cancelReason) {
675
		this.cancelReason = cancelReason;
676
	}
677
 
678
	public String getBody() {
679
		return body;
680
	}
681
 
682
	public void setBody(String body) {
683
		this.body = body;
684
	}
685
 
686
	public String getLine1() {
687
		return line1;
688
	}
689
 
690
	public void setLine1(String line1) {
691
		this.line1 = line1;
692
	}
693
 
694
	public String getLine2() {
695
		return line2;
696
	}
697
 
698
	public void setLine2(String line2) {
699
		this.line2 = line2;
700
	}
701
 
702
	public String getCity() {
703
		return city;
704
	}
705
 
706
	public void setCity(String city) {
707
		this.city = city;
708
	}
709
 
710
	public String getState() {
711
		return state;
712
	}
713
 
714
	public void setState(String state) {
715
		this.state = state;
716
	}
717
 
718
	public String getPin() {
719
		return pin;
720
	}
721
 
722
	public void setPin(String pin) {
723
		this.pin = pin;
724
	}
725
 
7190 amar.kumar 726
	public String getFreebieItem() {
727
		return freebieItem;
728
	}
729
 
730
	public void setFreebieItem(String freebieItem) {
731
		this.freebieItem = freebieItem;
732
	}
733
 
734
	public String getDealText() {
735
		return dealText;
736
	}
737
 
738
	public void setDealText(String dealText) {
739
		this.dealText = dealText;
740
	}
741
 
742
	public String getParentOrderIdForFreebieOrder() {
743
		return parentOrderIdForFreebieOrder;
744
	}
745
 
746
	public void setParentOrderIdForFreebieOrder(String parentOrderIdForFreebieOrder) {
747
		this.parentOrderIdForFreebieOrder = parentOrderIdForFreebieOrder;
748
	}
749
 
750
	public String getFreebieOrderId() {
751
		return freebieOrderId;
752
	}
753
 
754
	public void setFreebieOrderId(String freebieOrderId) {
755
		this.freebieOrderId = freebieOrderId;
756
	}
7372 kshitij.so 757
 
7393 anupam.sin 758
    public void setCashAmount(double cashAmount) {
759
        this.cashAmount = cashAmount;
760
    }
761
 
762
    public double getCashAmount() {
763
        return cashAmount;
764
    }
765
 
766
    public void setCardAmount(double cardAmount) {
767
        this.cardAmount = cardAmount;
768
    }
769
 
770
    public double getCardAmount() {
771
        return cardAmount;
772
    }
773
 
774
    public long getOrderId() {
775
        return orderId;
776
    }
777
 
778
    public void setOrderId(long orderId) {
779
        this.orderId = orderId;
780
    }
781
 
782
    public StoreOrderDetail getStoreOrderDetail() {
783
        return storeOrderDetail;
784
    }
785
 
786
    public void setStoreOrderDetail(StoreOrderDetail storeOrderDetail) {
787
        this.storeOrderDetail = storeOrderDetail;
788
    }
7826 manish.sha 789
 
790
  //Start:- Added by Manish Sharma for Displaying Logistics Provider Id and Amazon Order Id on 25-Jul-2013
791
    public AmazonOrder getAmazonOrder() {
792
		return amazonOrder;
793
	}
7393 anupam.sin 794
 
7826 manish.sha 795
	public void setAmazonOrder(AmazonOrder amazonOrder) {
796
		this.amazonOrder = amazonOrder;
797
	}
798
 
799
	//End:- Added by Manish Sharma for Displaying Logistics Provider Id and Amazon Order Id on 25-Jul-2013
800
 
7836 anupam.sin 801
    public void setProviderName(String providerName) {
802
        this.providerName = providerName;
803
    }
804
 
805
    public String getProviderName() {
806
        return providerName;
807
    }
808
 
809
    public static void main(String[] args) {
810
        System.out.println(providerNameMap.get(5l));
811
        System.out.println(providerNameMap.get(10l));
812
    }
813
 
2674 vikas 814
}