Subversion Repositories SmartDukaan

Rev

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