| 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;
|
| 8645 |
vikram.rag |
43 |
import org.slf4j.Logger;
|
|
|
44 |
import org.slf4j.LoggerFactory;
|
| 8616 |
vikram.rag |
45 |
|
|
|
46 |
import order.SaholicAPI;
|
|
|
47 |
import order.ObjectFactory;
|
|
|
48 |
import order.SaholicAPI.Order;
|
|
|
49 |
|
|
|
50 |
public class ProcessSnapdealOrder {
|
| 8627 |
vikram.rag |
51 |
//private static final int SNAPDEAL_SOURCE_ID = 3;
|
| 8616 |
vikram.rag |
52 |
private static final int SNAPDEAL_SOURCE_ID = 7;
|
|
|
53 |
private static final String FIRSTFLIGHT = "First Flight";
|
| 8627 |
vikram.rag |
54 |
private static final String DELHIVERY = "DELHIVERY";
|
|
|
55 |
private static final String BLUEDART = "Bluedart";
|
| 8642 |
vikram.rag |
56 |
private static final String CHHOTU = "CHHOTU";
|
| 8627 |
vikram.rag |
57 |
private static String transactionId;
|
|
|
58 |
private static final int SNAPDEAL_GATEWAY_ID = 18;
|
| 8646 |
vikram.rag |
59 |
private static Logger logger;
|
| 8627 |
vikram.rag |
60 |
|
| 8616 |
vikram.rag |
61 |
public static void processSnapdealOrders(File file) throws JAXBException{
|
| 8646 |
vikram.rag |
62 |
logger = LoggerFactory.getLogger(ProcessSnapdealOrder.class);
|
| 8616 |
vikram.rag |
63 |
JAXBContext jc = JAXBContext.newInstance(SaholicAPI.class);
|
|
|
64 |
Unmarshaller unmarshaller = jc.createUnmarshaller();
|
|
|
65 |
SaholicAPI orders = (SaholicAPI) unmarshaller.unmarshal(file);
|
|
|
66 |
Marshaller marshaller = jc.createMarshaller();
|
|
|
67 |
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
|
|
68 |
marshaller.marshal(orders, System.out);
|
|
|
69 |
SourceDetail sourceDetail = null;
|
|
|
70 |
User user = null;
|
|
|
71 |
TransactionClient tsc = null;
|
|
|
72 |
try {
|
|
|
73 |
tsc = new TransactionClient();
|
|
|
74 |
sourceDetail = tsc.getClient().getSourceDetail(SNAPDEAL_SOURCE_ID);
|
|
|
75 |
} catch (Exception e) {
|
| 8645 |
vikram.rag |
76 |
logger.error("Unable to establish connection to the transaction service while getting Snapdeal Source Detail", e);
|
| 8616 |
vikram.rag |
77 |
}
|
|
|
78 |
try {
|
|
|
79 |
in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient().getClient();
|
|
|
80 |
user = userClient.getUserByEmail(sourceDetail.getEmail());
|
|
|
81 |
} catch (Exception e) {
|
| 8649 |
vikram.rag |
82 |
logger.error("Unable to establish connection to the User service", e);
|
| 8616 |
vikram.rag |
83 |
}
|
|
|
84 |
for(Order order:orders.getOrder()){
|
| 8627 |
vikram.rag |
85 |
long subOrderId = (long) order.getSuborderId();
|
|
|
86 |
Client transaction_client = null;
|
|
|
87 |
try {
|
|
|
88 |
transaction_client = new TransactionClient().getClient();
|
|
|
89 |
//transaction_client.snapdealOrderExists(subOrderId);
|
|
|
90 |
System.out.println("Suborder id " + subOrderId);
|
|
|
91 |
if(transaction_client.snapdealOrderExists(subOrderId)) {
|
|
|
92 |
System.out.println("Suborder id exists");
|
|
|
93 |
continue;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
} catch (TTransportException e1) {
|
|
|
97 |
// TODO Auto-generated catch block
|
|
|
98 |
e1.printStackTrace();
|
|
|
99 |
} catch (TException e) {
|
|
|
100 |
// TODO Auto-generated catch block
|
|
|
101 |
e.printStackTrace();
|
|
|
102 |
}
|
|
|
103 |
System.out.println("Inside Order Parsing");
|
| 8616 |
vikram.rag |
104 |
Transaction txn = new Transaction();
|
| 8627 |
vikram.rag |
105 |
System.out.println("User details are " + user.getActiveCartId());
|
| 8616 |
vikram.rag |
106 |
txn.setShoppingCartid(user.getActiveCartId());
|
| 8627 |
vikram.rag |
107 |
System.out.println("transaction created");
|
| 8616 |
vikram.rag |
108 |
txn.setCustomer_id(user.getUserId());
|
| 8627 |
vikram.rag |
109 |
System.out.println("User Id is " + user.getUserId());
|
| 8616 |
vikram.rag |
110 |
txn.setCreatedOn(new Date().getTime());
|
|
|
111 |
txn.setTransactionStatus(TransactionStatus.INIT);
|
|
|
112 |
txn.setStatusDescription("Order for Snapdeal ");
|
|
|
113 |
List<in.shop2020.model.v1.order.Order> orderlist = new ArrayList<in.shop2020.model.v1.order.Order>();
|
|
|
114 |
LineItem lineItem = null;
|
|
|
115 |
try {
|
|
|
116 |
lineItem = createLineItem(order.getSKUCode(),order.getSellingPricePerItem());
|
| 8627 |
vikram.rag |
117 |
System.out.println("Line item created");
|
| 8616 |
vikram.rag |
118 |
} catch (CatalogServiceException e) {
|
| 8645 |
vikram.rag |
119 |
logger.error("Unable to create order for suborderid " + order.getSuborderId());
|
| 8616 |
vikram.rag |
120 |
e.printStackTrace();
|
|
|
121 |
} catch (TException e) {
|
| 8645 |
vikram.rag |
122 |
logger.error("Unable to create order for suborderid " + order.getSuborderId());
|
| 8616 |
vikram.rag |
123 |
e.printStackTrace();
|
|
|
124 |
}
|
|
|
125 |
in.shop2020.model.v1.order.Order t_order = new in.shop2020.model.v1.order.Order();
|
| 8627 |
vikram.rag |
126 |
t_order.setCustomer_id(user.getUserId());
|
|
|
127 |
t_order.setCustomer_email(sourceDetail.getEmail());
|
|
|
128 |
t_order.setCustomer_name(order.getCustomerName());
|
|
|
129 |
t_order.setCustomer_address1("");
|
|
|
130 |
t_order.setCustomer_address2("");
|
|
|
131 |
t_order.setCustomer_city(order.getCity());
|
|
|
132 |
t_order.setCustomer_state(order.getState());
|
|
|
133 |
t_order.setCustomer_pincode(String.valueOf(order.getPINCode()));
|
|
|
134 |
t_order.setTotal_amount(lineItem.getTotal_price());
|
|
|
135 |
t_order.setTotal_weight(lineItem.getTotal_weight());
|
|
|
136 |
t_order.setLineitems(Collections.singletonList(lineItem));
|
|
|
137 |
t_order.setStatus(OrderStatus.SUBMITTED_FOR_PROCESSING);
|
|
|
138 |
t_order.setStatusDescription("In Process");
|
|
|
139 |
t_order.setCreated_timestamp(new Date().getTime());
|
|
|
140 |
t_order.setOrderType(OrderType.B2C);
|
|
|
141 |
t_order.setCod(false);
|
|
|
142 |
System.out.println("t order created");
|
|
|
143 |
try {
|
|
|
144 |
Calendar time = Calendar.getInstance();
|
|
|
145 |
time.add(Calendar.DAY_OF_MONTH, 1);
|
|
|
146 |
t_order.setPromised_shipping_time(time.getTimeInMillis());
|
|
|
147 |
t_order.setExpected_shipping_time(time.getTimeInMillis());
|
|
|
148 |
time.add(Calendar.DAY_OF_MONTH, 3);
|
|
|
149 |
t_order.setPromised_delivery_time(time.getTimeInMillis());
|
|
|
150 |
t_order.setExpected_delivery_time(time.getTimeInMillis());
|
|
|
151 |
System.out.println("Dates set in transaction");
|
|
|
152 |
} catch(Exception e) {
|
| 8645 |
vikram.rag |
153 |
logger.error("Error in updating Shipping or Delivery Time for suborderid " + order.getSuborderId());
|
| 8627 |
vikram.rag |
154 |
continue;
|
|
|
155 |
}
|
|
|
156 |
/*InventoryService.Client inventoryClient = null;
|
|
|
157 |
//Warehouse fulfillmentWarehouse= null;
|
| 8616 |
vikram.rag |
158 |
try {
|
| 8627 |
vikram.rag |
159 |
//inventoryClient = new InventoryClient().getClient();
|
|
|
160 |
//VendorItemPricing vendorItemPricing = inventoryClient.getItemPricing(lineItem.getItem_id(), fulfillmentWarehouse.getVendor().getId());
|
|
|
161 |
//t_order.getLineitems().get(0).setTransfer_price(vendorItemPricing.getTransferPrice());
|
|
|
162 |
//t_order.getLineitems().get(0).setNlc(vendorItemPricing.getNlc());
|
| 8616 |
vikram.rag |
163 |
} catch (InventoryServiceException e) {
|
|
|
164 |
continue;
|
|
|
165 |
} catch (TTransportException e) {
|
|
|
166 |
// TODO Auto-generated catch block
|
|
|
167 |
e.printStackTrace();
|
|
|
168 |
} catch (TException e) {
|
|
|
169 |
// TODO Auto-generated catch block
|
|
|
170 |
e.printStackTrace();
|
| 8627 |
vikram.rag |
171 |
}*/
|
| 8616 |
vikram.rag |
172 |
if(order.getCourier().equalsIgnoreCase(FIRSTFLIGHT)) {
|
|
|
173 |
t_order.setLogistics_provider_id(12);
|
|
|
174 |
} else if(order.getCourier().equalsIgnoreCase(DELHIVERY)) {
|
|
|
175 |
t_order.setLogistics_provider_id(13);
|
|
|
176 |
} else if(order.getCourier().equalsIgnoreCase(BLUEDART)) {
|
|
|
177 |
t_order.setLogistics_provider_id(14);
|
| 8642 |
vikram.rag |
178 |
}else if(order.getCourier().equalsIgnoreCase(CHHOTU)) {
|
|
|
179 |
t_order.setLogistics_provider_id(16);
|
| 8616 |
vikram.rag |
180 |
} else {
|
| 8648 |
vikram.rag |
181 |
logger.error("Error No Courier Match please add courier to our system for suborderid " + order.getSuborderId());
|
| 8627 |
vikram.rag |
182 |
continue;
|
| 8616 |
vikram.rag |
183 |
}
|
| 8627 |
vikram.rag |
184 |
|
| 8616 |
vikram.rag |
185 |
t_order.setAirwaybill_no(String.valueOf(order.getAWBNumber()));
|
|
|
186 |
t_order.setTracking_id(String.valueOf(order.getAWBNumber()));
|
| 8627 |
vikram.rag |
187 |
t_order.setTotal_amount(order.getSellingPricePerItem());
|
|
|
188 |
t_order.setOrderType(OrderType.B2C);
|
|
|
189 |
t_order.setSource(SNAPDEAL_SOURCE_ID);
|
|
|
190 |
t_order.setOrderType(OrderType.B2C);
|
|
|
191 |
orderlist.add(t_order);
|
|
|
192 |
txn.setOrders(orderlist);
|
| 8616 |
vikram.rag |
193 |
try {
|
| 8627 |
vikram.rag |
194 |
String referenceCode = order.getReferenceCode();
|
|
|
195 |
String productName = order.getProduct();
|
|
|
196 |
SimpleDateFormat istFormatter = new SimpleDateFormat("dd/mm/yyyy");
|
|
|
197 |
//SimpleDateFormat istFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
|
|
|
198 |
Date snapdealTxnDate = istFormatter.parse(order.getOrderPlacedDate());
|
|
|
199 |
transactionId = String.valueOf(transaction_client.createTransaction(txn));
|
|
|
200 |
createPayment(user, (new Long(subOrderId)).toString(), lineItem.getTotal_price());
|
|
|
201 |
|
|
|
202 |
Transaction transaction = transaction_client.getTransaction(Long.parseLong(transactionId));
|
|
|
203 |
in.shop2020.model.v1.order.Order snapdealorder = transaction.getOrders().get(0);
|
|
|
204 |
|
|
|
205 |
//inventoryClient.reserveItemInWarehouse(lineItem.getItem_id(), fulfillmentWarehouse.getId(), 1,
|
|
|
206 |
// snapdealorder.getId(), snapdealorder.getCreated_timestamp(), snapdealorder.getPromised_shipping_time(), snapdealorder.getLineitems().get(0).getQuantity());
|
|
|
207 |
|
|
|
208 |
|
|
|
209 |
SnapdealOrder snapdealOrder = new SnapdealOrder();
|
|
|
210 |
snapdealOrder.setOrderId(snapdealorder.getId());
|
|
|
211 |
snapdealOrder.setSubOrderId(subOrderId);
|
|
|
212 |
snapdealOrder.setReferenceCode(referenceCode);
|
|
|
213 |
snapdealOrder.setProductName(productName);
|
|
|
214 |
snapdealOrder.setSnapdealTxnDate(snapdealTxnDate.getTime());
|
|
|
215 |
snapdealOrder.setListingPrice(lineItem.getTotal_price());
|
|
|
216 |
transaction_client.createSnapdealOrder(snapdealOrder);
|
|
|
217 |
|
| 8649 |
vikram.rag |
218 |
}
|
| 8645 |
vikram.rag |
219 |
catch (Exception e) {
|
|
|
220 |
logger.error("Error in creating transaction " + order.getSuborderId());
|
|
|
221 |
continue;
|
|
|
222 |
}
|
| 8616 |
vikram.rag |
223 |
}
|
|
|
224 |
|
|
|
225 |
}
|
|
|
226 |
|
| 8649 |
vikram.rag |
227 |
public static void createPayment(User user, String subOrderId, double amount) {
|
|
|
228 |
in.shop2020.payments.PaymentService.Client client = null;
|
|
|
229 |
try {
|
|
|
230 |
client = new PaymentClient().getClient();
|
|
|
231 |
} catch (TTransportException e) {
|
|
|
232 |
logger.error("Could not connect to payment service ");
|
|
|
233 |
e.printStackTrace();
|
|
|
234 |
}
|
|
|
235 |
long paymentId = 0;
|
|
|
236 |
try {
|
|
|
237 |
paymentId = client.createPayment(user.getUserId(), amount, SNAPDEAL_GATEWAY_ID, Long.valueOf(transactionId), false);
|
|
|
238 |
} catch (NumberFormatException e) {
|
|
|
239 |
logger.error("Could not create payment");
|
|
|
240 |
e.printStackTrace();
|
|
|
241 |
} catch (PaymentException e) {
|
|
|
242 |
logger.error("Could not create payment payment exception");
|
|
|
243 |
e.printStackTrace();
|
|
|
244 |
} catch (TException e) {
|
|
|
245 |
logger.error("Could not create payment thrift exception");
|
|
|
246 |
e.printStackTrace();
|
|
|
247 |
}
|
|
|
248 |
try {
|
|
|
249 |
client.updatePaymentDetails(paymentId, null, null, null, null, null, null, subOrderId, null, PaymentStatus.AUTHORIZED, null, null);
|
|
|
250 |
} catch (PaymentException e) {
|
|
|
251 |
logger.error("Could not update payment to order");
|
|
|
252 |
e.printStackTrace();
|
|
|
253 |
} catch (TException e) {
|
|
|
254 |
logger.error("Could not update payment thrift exception");
|
|
|
255 |
e.printStackTrace();
|
|
|
256 |
}
|
| 8627 |
vikram.rag |
257 |
}
|
|
|
258 |
|
| 8616 |
vikram.rag |
259 |
public static LineItem createLineItem(long itemId, double amount) throws CatalogServiceException, TException {
|
| 8627 |
vikram.rag |
260 |
LineItem lineItem = new LineItem();
|
|
|
261 |
CatalogService.Client catalogClient = new CatalogClient().getClient();
|
|
|
262 |
Item item = catalogClient.getItem(itemId);
|
| 8616 |
vikram.rag |
263 |
|
| 8627 |
vikram.rag |
264 |
lineItem.setProductGroup(item.getProductGroup());
|
|
|
265 |
lineItem.setBrand(item.getBrand());
|
|
|
266 |
lineItem.setModel_number(item.getModelNumber());
|
|
|
267 |
lineItem.setModel_name(item.getModelName());
|
|
|
268 |
lineItem.setExtra_info(item.getFeatureDescription());
|
|
|
269 |
lineItem.setQuantity(1);
|
|
|
270 |
lineItem.setItem_id(item.getId());
|
|
|
271 |
lineItem.setUnit_weight(item.getWeight());
|
|
|
272 |
lineItem.setTotal_weight(item.getWeight());
|
|
|
273 |
lineItem.setUnit_price(amount);
|
|
|
274 |
lineItem.setTotal_price(amount);
|
|
|
275 |
|
|
|
276 |
if (item.getColor() == null || "NA".equals(item.getColor())) {
|
|
|
277 |
lineItem.setColor("");
|
|
|
278 |
} else {
|
|
|
279 |
lineItem.setColor(item.getColor());
|
|
|
280 |
}
|
|
|
281 |
return lineItem;
|
| 8616 |
vikram.rag |
282 |
}
|
|
|
283 |
|
|
|
284 |
}
|