Subversion Repositories SmartDukaan

Rev

Rev 8627 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
8616 vikram.rag 1
package in.shop2020;
2
 
3
import in.shop2020.model.v1.catalog.CatalogService;
4
import in.shop2020.model.v1.catalog.CatalogServiceException;
5
import in.shop2020.model.v1.catalog.Item;
6
import in.shop2020.model.v1.inventory.InventoryService;
7
import in.shop2020.model.v1.inventory.InventoryServiceException;
8
import in.shop2020.model.v1.inventory.VendorItemPricing;
9
import in.shop2020.model.v1.inventory.Warehouse;
10
import in.shop2020.model.v1.order.LineItem;
11
import in.shop2020.model.v1.order.OrderStatus;
12
import in.shop2020.model.v1.order.OrderType;
13
import in.shop2020.model.v1.order.SnapdealOrder;
14
import in.shop2020.model.v1.order.SourceDetail;
15
import in.shop2020.model.v1.order.Transaction;
16
import in.shop2020.model.v1.order.TransactionStatus;
17
import in.shop2020.model.v1.order.TransactionService.Client;
18
import in.shop2020.model.v1.user.User;
19
import in.shop2020.payments.PaymentException;
20
import in.shop2020.payments.PaymentStatus;
21
import in.shop2020.thrift.clients.CatalogClient;
22
import in.shop2020.thrift.clients.InventoryClient;
23
import in.shop2020.thrift.clients.PaymentClient;
24
import in.shop2020.thrift.clients.TransactionClient;
25
import in.shop2020.thrift.clients.UserClient;
26
 
27
import java.io.File;
28
import java.text.SimpleDateFormat;
29
import java.util.ArrayList;
30
import java.util.Calendar;
31
import java.util.Collections;
32
import java.util.Date;
33
import java.util.List;
34
import java.util.TimeZone;
35
 
36
import javax.xml.bind.JAXBContext;
37
import javax.xml.bind.JAXBException;
38
import javax.xml.bind.Marshaller;
39
import javax.xml.bind.Unmarshaller;
40
 
41
import org.apache.thrift.TException;
42
import org.apache.thrift.transport.TTransportException;
43
 
44
import order.SaholicAPI;
45
import order.ObjectFactory;
46
import order.SaholicAPI.Order;
47
 
48
public class ProcessSnapdealOrder {
49
	private static final int SNAPDEAL_SOURCE_ID = 7;
50
	private static final String FIRSTFLIGHT = "First Flight";
51
    private static final String DELHIVERY = "DELHIVERY";
52
    private static final String BLUEDART = "Bluedart";
53
    private static String transactionId;
54
    private static final int SNAPDEAL_GATEWAY_ID = 18;
55
 
56
	public static void processSnapdealOrders(File file) throws JAXBException{
57
		JAXBContext jc = JAXBContext.newInstance(SaholicAPI.class);
58
		Unmarshaller unmarshaller = jc.createUnmarshaller();
59
		SaholicAPI orders = (SaholicAPI) unmarshaller.unmarshal(file);
60
		Marshaller marshaller = jc.createMarshaller();
61
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
62
		marshaller.marshal(orders, System.out);
63
 
64
		SourceDetail sourceDetail = null;
65
		User user = null;
66
		TransactionClient tsc = null;
67
		try {
68
			tsc = new TransactionClient();
69
			sourceDetail = tsc.getClient().getSourceDetail(SNAPDEAL_SOURCE_ID);
70
		} catch (Exception e) {
71
		}
72
		try {   
73
			in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient().getClient();
74
			user = userClient.getUserByEmail(sourceDetail.getEmail());
75
		} catch (Exception e) {
76
		}
77
		for(Order order:orders.getOrder()){
78
			Transaction txn = new Transaction();
79
			txn.setShoppingCartid(user.getActiveCartId());
80
			txn.setCustomer_id(user.getUserId());
81
			txn.setCreatedOn(new Date().getTime());
82
			txn.setTransactionStatus(TransactionStatus.INIT);
83
			txn.setStatusDescription("Order for Snapdeal ");
84
			List<in.shop2020.model.v1.order.Order> orderlist = new ArrayList<in.shop2020.model.v1.order.Order>();
85
			LineItem lineItem = null;
86
			try {
87
				lineItem = createLineItem(order.getSKUCode(),order.getSellingPricePerItem());
88
			} catch (CatalogServiceException e) {
89
				// TODO Auto-generated catch block
90
				e.printStackTrace();
91
			} catch (TException e) {
92
				// TODO Auto-generated catch block
93
				e.printStackTrace();
94
			}
95
			in.shop2020.model.v1.order.Order t_order = new in.shop2020.model.v1.order.Order();
96
            t_order.setCustomer_id(user.getUserId());
97
            t_order.setCustomer_email(sourceDetail.getEmail());
98
        	t_order.setCustomer_name(order.getCustomerName());
99
        	t_order.setCustomer_address1("");
100
        	t_order.setCustomer_address2("");
101
        	t_order.setCustomer_city(order.getCity());
102
        	t_order.setCustomer_state(order.getState());
103
        	t_order.setCustomer_pincode(String.valueOf(order.getPINCode()));
104
            t_order.setTotal_amount(lineItem.getTotal_price());            
105
            t_order.setTotal_weight(lineItem.getTotal_weight());
106
            t_order.setLineitems(Collections.singletonList(lineItem));            
107
            t_order.setStatus(OrderStatus.SUBMITTED_FOR_PROCESSING);
108
            t_order.setStatusDescription("In Process");
109
            t_order.setCreated_timestamp(new Date().getTime());
110
            t_order.setOrderType(OrderType.B2C);
111
            t_order.setCod(false);
112
            try {
113
            	Calendar time = Calendar.getInstance();
114
            	time.add(Calendar.DAY_OF_MONTH, 1);
115
            	t_order.setPromised_shipping_time(time.getTimeInMillis());
116
            	t_order.setExpected_shipping_time(time.getTimeInMillis());
117
            	time.add(Calendar.DAY_OF_MONTH, 3);
118
            	t_order.setPromised_delivery_time(time.getTimeInMillis());
119
            	t_order.setExpected_delivery_time(time.getTimeInMillis());
120
            } catch(Exception e) {
121
            	continue;
122
            }
123
            InventoryService.Client inventoryClient = null;
124
            Warehouse fulfillmentWarehouse= null; 
125
            try {
126
            	inventoryClient = new InventoryClient().getClient();
127
        		VendorItemPricing vendorItemPricing = inventoryClient.getItemPricing(lineItem.getItem_id(), fulfillmentWarehouse.getVendor().getId());
128
        		t_order.getLineitems().get(0).setTransfer_price(vendorItemPricing.getTransferPrice());
129
        		t_order.getLineitems().get(0).setNlc(vendorItemPricing.getNlc());
130
			} catch (InventoryServiceException e) {
131
            	continue;
132
			} catch (TTransportException e) {
133
				// TODO Auto-generated catch block
134
				e.printStackTrace();
135
			} catch (TException e) {
136
				// TODO Auto-generated catch block
137
				e.printStackTrace();
138
			}
139
			if(order.getCourier().equalsIgnoreCase(FIRSTFLIGHT)) {
140
				t_order.setLogistics_provider_id(12);
141
			} else if(order.getCourier().equalsIgnoreCase(DELHIVERY)) {
142
				t_order.setLogistics_provider_id(13);
143
			} else if(order.getCourier().equalsIgnoreCase(BLUEDART)) {
144
				t_order.setLogistics_provider_id(14);
145
			} else {
146
            	continue;
147
			}
148
 
149
			t_order.setAirwaybill_no(String.valueOf(order.getAWBNumber()));
150
			t_order.setTracking_id(String.valueOf(order.getAWBNumber()));
151
            t_order.setTotal_amount(order.getSellingPricePerItem());
152
            t_order.setOrderType(OrderType.B2C);
153
            t_order.setSource(SNAPDEAL_SOURCE_ID);
154
            t_order.setOrderType(OrderType.B2C);
155
            orderlist.add(t_order);
156
            txn.setOrders(orderlist);
157
 
158
 
159
            Client transaction_client = null;
160
			try {
161
				transaction_client = new TransactionClient().getClient();
162
			} catch (TTransportException e1) {
163
				// TODO Auto-generated catch block
164
				e1.printStackTrace();
165
			}
166
            try {
167
            	long subOrderId = order.getSuborderId();
168
            	String referenceCode = order.getReferenceCode();
169
            	String productName = order.getProduct();
170
				SimpleDateFormat istFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
171
				Date snapdealTxnDate = istFormatter.parse(order.getOrderPlacedDate());
172
            	if(transaction_client.snapdealOrderExists(subOrderId)) {
173
            		continue;
174
            	}
175
            	transactionId =  String.valueOf(transaction_client.createTransaction(txn));
176
            	createPayment(user, (new Long(subOrderId)).toString(), lineItem.getTotal_price());
177
 
178
                Transaction transaction = transaction_client.getTransaction(Long.parseLong(transactionId));
179
                in.shop2020.model.v1.order.Order snapdealorder = transaction.getOrders().get(0);
180
 
181
                inventoryClient.reserveItemInWarehouse(lineItem.getItem_id(), fulfillmentWarehouse.getId(), 1, 
182
                		snapdealorder.getId(), snapdealorder.getCreated_timestamp(), snapdealorder.getPromised_shipping_time(), snapdealorder.getLineitems().get(0).getQuantity());
183
 
184
 
185
                SnapdealOrder snapdealOrder = new SnapdealOrder();
186
                snapdealOrder.setOrderId(snapdealorder.getId());
187
                snapdealOrder.setSubOrderId(subOrderId);
188
                snapdealOrder.setReferenceCode(referenceCode);
189
                snapdealOrder.setProductName(productName);
190
                snapdealOrder.setSnapdealTxnDate(snapdealTxnDate.getTime());
191
                snapdealOrder.setListingPrice(lineItem.getTotal_price());
192
                transaction_client.createSnapdealOrder(snapdealOrder);
193
 
194
            } catch (Exception e) {
195
                continue;
196
    		}
197
 
198
 
199
		}
200
 
201
	}
202
 
203
	public static void createPayment(User user, String subOrderId, double amount) throws NumberFormatException, PaymentException, TException {
204
        in.shop2020.payments.PaymentService.Client client = new PaymentClient().getClient();
205
        long paymentId = client.createPayment(user.getUserId(), amount, SNAPDEAL_GATEWAY_ID, Long.valueOf(transactionId), false);
206
        client.updatePaymentDetails(paymentId, null, null, null, null, null, null, subOrderId, null, PaymentStatus.AUTHORIZED, null, null);
207
    }   
208
 
209
	public static LineItem createLineItem(long itemId, double amount) throws CatalogServiceException, TException {
210
    	LineItem lineItem = new LineItem();
211
    	CatalogService.Client catalogClient = new CatalogClient().getClient();
212
    	Item item = catalogClient.getItem(itemId);
213
 
214
    	lineItem.setProductGroup(item.getProductGroup());
215
        lineItem.setBrand(item.getBrand());
216
        lineItem.setModel_number(item.getModelNumber());
217
        lineItem.setModel_name(item.getModelName());
218
        lineItem.setExtra_info(item.getFeatureDescription());
219
        lineItem.setQuantity(1);
220
        lineItem.setItem_id(item.getId());
221
        lineItem.setUnit_weight(item.getWeight());
222
        lineItem.setTotal_weight(item.getWeight());
223
        lineItem.setUnit_price(amount);
224
        lineItem.setTotal_price(amount);
225
 
226
        if (item.getColor() == null || "NA".equals(item.getColor())) {
227
            lineItem.setColor("");
228
        } else {
229
            lineItem.setColor(item.getColor());
230
        }
231
    	return lineItem;
232
	}
233
 
234
}