Subversion Repositories SmartDukaan

Rev

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