Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
9947 vikram.rag 1
package in.shop2020;
2
 
3
import in.shop2020.logistics.DeliveryType;
4
import in.shop2020.logistics.LogisticsInfo;
5
import in.shop2020.logistics.LogisticsServiceException;
6
import in.shop2020.model.v1.catalog.CatalogService.Client;
7
import in.shop2020.model.v1.catalog.FlipkartItem;
8
import in.shop2020.model.v1.catalog.Item;
9
import in.shop2020.model.v1.catalog.status;
10
import in.shop2020.model.v1.inventory.InventoryType;
11
import in.shop2020.model.v1.inventory.ItemInventory;
12
import in.shop2020.model.v1.inventory.Warehouse;
13
import in.shop2020.model.v1.inventory.WarehouseLocation;
14
import in.shop2020.model.v1.inventory.WarehouseType;
15
import in.shop2020.model.v1.order.OrderSource;
16
import in.shop2020.serving.model.Order;
17
import in.shop2020.serving.model.OrderItems;
18
import in.shop2020.thrift.clients.CatalogClient;
19
import in.shop2020.thrift.clients.InventoryClient;
20
import in.shop2020.thrift.clients.LogisticsClient;
21
import in.shop2020.thrift.clients.TransactionClient;
9983 vikram.rag 22
import in.shop2020.utils.GmailUtils;
9947 vikram.rag 23
import inventory.FlipkartInventoryItem;
24
 
25
import java.io.BufferedReader;
9983 vikram.rag 26
import java.io.File;
9947 vikram.rag 27
import java.io.IOException;
28
import java.io.InputStreamReader;
9968 vikram.rag 29
import java.io.UnsupportedEncodingException;
9947 vikram.rag 30
import java.util.ArrayList;
31
import java.util.HashMap;
32
import java.util.List;
33
import java.util.Map;
34
import java.util.Map.Entry;
35
 
9983 vikram.rag 36
import javax.mail.MessagingException;
37
 
9947 vikram.rag 38
import org.apache.http.HttpResponse;
39
import org.apache.http.NameValuePair;
40
import org.apache.http.auth.AuthScope;
41
import org.apache.http.auth.UsernamePasswordCredentials;
42
import org.apache.http.client.ClientProtocolException;
43
import org.apache.http.client.HttpClient;
44
import org.apache.http.client.entity.UrlEncodedFormEntity;
45
import org.apache.http.client.methods.HttpGet;
46
import org.apache.http.client.methods.HttpPost;
47
import org.apache.http.entity.StringEntity;
48
import org.apache.http.impl.client.DefaultHttpClient;
49
import org.apache.http.message.BasicNameValuePair;
50
import org.apache.thrift.TException;
51
import org.apache.thrift.transport.TTransportException;
52
 
53
import com.google.gson.Gson;
54
 
55
 
56
 
57
public class CreateSubmitFlipkartInventoryFeed {
58
	private static Map<Long, FlipkartInventoryItem> flipkartItemsInventoryMap = new HashMap<Long, FlipkartInventoryItem>();
59
	private static List<FlipkartItem> flipkartItems;
60
	private static List<FlipkartItem> flipkartInventoryItems = new ArrayList<FlipkartItem>();
61
	private static Map<Long,Long> itemIdOrdersMap = new HashMap<Long,Long>();
9983 vikram.rag 62
	private static Map<Long,Item> aliveItemsMap = new HashMap<Long,Item>();
63
	static java.text.SimpleDateFormat sdf;
64
	static String emailFromAddress;
65
	static String password;
66
	static GmailUtils mailer;
67
	static String sendTo[];
9947 vikram.rag 68
	static
69
	{
9983 vikram.rag 70
		sdf = new java.text.SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
71
		emailFromAddress = "build@shop2020.in";
72
		password = "cafe@nes";
73
		mailer = new GmailUtils();
74
		sendTo = new String[]{"vikram.raghav@shop2020.in"};
75
		/*sendTo = new String[]{ "sandeep.sachdeva@shop2020.in", "vikram.raghav@shop2020.in", "rajneesh.arora@shop2020.in",
76
				"khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","chaitnaya.vats@saholic.com",
77
				"yukti.jain@shop2020.in","manisha.sharma@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in","amar.kumar@shop2020.in"};
78
		 */List<String> flipkartorderids = null;
79
		 Client catalogServiceClient = null;
80
		 try {
81
			 catalogServiceClient = new CatalogClient().getClient();
82
		 } catch (TTransportException e) {
83
			 e.printStackTrace();
84
		 }
85
		 try {
86
			 flipkartItems = catalogServiceClient.getAllFlipkartItems();
87
		 } catch (TException e) {
88
			 e.printStackTrace();
89
		 }
90
		 for(FlipkartItem flipkartItem:flipkartItems){
91
			 if(flipkartItem.isIsListedOnFlipkart() && !flipkartItem.isSuppressInventoryFeed())
92
				 flipkartInventoryItems.add(flipkartItem);
93
		 }
94
		 if(flipkartInventoryItems.size()==0){
95
			 String text = "";
96
				try {
97
					mailer.sendSSLMessage(sendTo,"No items to send inventory ", emailFromAddress, password, text);
98
					System.exit(0);
99
				} catch (MessagingException e) {
100
					e.printStackTrace();
101
				}
102
		 }
103
		 in.shop2020.model.v1.order.TransactionService.Client transactionClient;
104
		 try {
105
			 flipkartorderids = FetchOrders();
106
		 } catch (ClientProtocolException e1) {
107
			 e1.printStackTrace();
108
		 } catch (IOException e1) {
109
			 e1.printStackTrace();
110
		 }
111
		 try {
112
			 transactionClient = new TransactionClient("support_transaction_service_server_host","transaction_service_server_port").getClient();
113
			 if(flipkartorderids!=null && flipkartorderids.size()>0){
114
				 itemIdOrdersMap = transactionClient.getCreatedOrdersForFlipkart(flipkartorderids);
115
			 }
116
		 } catch (TTransportException e) {
117
			 e.printStackTrace();
118
		 }catch (TException e) {
119
			 // TODO Auto-generated catch block
120
			 e.printStackTrace();
121
		 }
9947 vikram.rag 122
 
123
	}
124
 
9967 vikram.rag 125
	public static void main(String[] args) throws ClientProtocolException, TException{
9947 vikram.rag 126
		calculateinventory();
127
		DefaultHttpClient httpclient = new DefaultHttpClient();
9973 vikram.rag 128
		httpclient = (DefaultHttpClient) WebClientWrapper.wrapClient(httpclient);
9951 vikram.rag 129
		httpclient.getCredentialsProvider().setCredentials(
9983 vikram.rag 130
				new AuthScope("sandbox-api.flipkart.net", 443),
131
				new UsernamePasswordCredentials("og3yg5994bxbxxc1", "8a137068-417c-4e4f-ae95-9295ab938d88"));/// Test Server Access Keys
132
		/*httpclient.getCredentialsProvider().setCredentials(
9975 vikram.rag 133
				new AuthScope("api.flipkart.net", 443),
9974 vikram.rag 134
				new UsernamePasswordCredentials("m2z93iskuj81qiid","0c7ab6a5-98c0-4cdc-8be3-72c591e0add4")); //Prod Server Access Keys
9983 vikram.rag 135
		*/
136
		//HttpPost httppost = new HttpPost("https://api.flipkart.net/sellers/skus/listings/bulk"); // PROD
137
		HttpPost httppost = new HttpPost("https://sandbox-api.flipkart.net/sellers/skus/listings/bulk"); // TEST
9947 vikram.rag 138
		StringBuffer jsonRequest = new StringBuffer();
9983 vikram.rag 139
		StringBuffer jsonStart = new StringBuffer();
140
		jsonStart = jsonStart.append("{\"listings\":[");
141
		StringBuffer jsonEnd = new StringBuffer();
142
		jsonEnd.append("]}");
9947 vikram.rag 143
		//System.out.println("JSON request  " + jsonRequest);
144
		LogisticsClient logisticsServiceClient;
145
		in.shop2020.logistics.LogisticsService.Client logisticsClient=null;
146
		LogisticsInfo logisticinfo = null;
147
		try {
148
			logisticsServiceClient = new LogisticsClient("logistics_service_prod_server_host","logistics_service_prod_server_port");
149
			logisticsClient = logisticsServiceClient.getClient();
150
 
151
		} catch (TTransportException e) {
152
			// TODO Auto-generated catch block
153
			e.printStackTrace();
154
		}
155
		long stock=0;
156
		int i=0;
9983 vikram.rag 157
		long available,reserve,heldforsource,totalheld,allocable,holdinventory,defaultinventory,actualstock=0,orders=0;
158
		StringBuffer inventoryItems =new StringBuffer();
159
		String tableHeader = "<html><table border=\"1\" align=\"center\"><tr>" 
160
			+ "<td><b>" + "Product Name" +"</b></td>"
161
			+"<td><b>" + "Item ID" + "</b></td>"
162
			+"<td><b>" + "Inventory" + "</b></td>"
163
			+"<td><b>" + "Orders Added to Inventory" + "</b></td>"
164
			+"<td><b>" + "Sent Inventory" + "</b></td>"
165
			+"</tr>";
166
		String tableFooter = "</table></html>";
167
		Item item;
168
		StringBuffer feedResponse = null;
9947 vikram.rag 169
		for(Entry<Long, FlipkartInventoryItem> entry:flipkartItemsInventoryMap.entrySet()){
9983 vikram.rag 170
			feedResponse=  new StringBuffer();
9947 vikram.rag 171
			available = entry.getValue().getAvailability();
172
			reserve = entry.getValue().getReserve();
173
			heldforsource = entry.getValue().getHeldForSource();
174
			totalheld = entry.getValue().getTotalHeldInventory();
175
			holdinventory = entry.getValue().getHoldInventory();
176
			defaultinventory = entry.getValue().getDefaultInventory();
177
			if(!entry.getValue().getStatus().equals(status.PAUSED)){
178
				if(entry.getValue().getRisky()){
179
					if((available - reserve) < totalheld && totalheld!=0){
180
						stock = (long) Math.floor(((available - reserve)*heldforsource/totalheld));
181
					}
182
					else{
183
						allocable = available - reserve - totalheld - holdinventory;
184
						if(allocable < 0){
185
							allocable = 0;
186
						}
187
						stock = Math.round(allocable*1/3) + heldforsource;
188
					}
189
 
190
				}
191
				else{
192
					allocable = available - reserve - totalheld;
193
					if(allocable < 0){
194
						allocable = 0;
195
					}
196
					if((allocable + heldforsource ) > defaultinventory){
197
						stock = Math.round(allocable*1/3) +  heldforsource;
198
					}
199
					else{
200
						stock = defaultinventory;
201
					}
202
				}
203
				System.out.println("Item ID " + entry.getKey()+" Stock "+stock +" Orders " + itemIdOrdersMap.get(entry.getKey()));
9971 vikram.rag 204
				if(itemIdOrdersMap!=null && itemIdOrdersMap.containsKey(entry.getKey())){
9983 vikram.rag 205
					actualstock = stock;
9977 vikram.rag 206
					stock = stock + itemIdOrdersMap.get(entry.getKey());  
9983 vikram.rag 207
					orders = itemIdOrdersMap.get(entry.getKey());
9947 vikram.rag 208
				}
209
				if(stock<0){
210
					stock =0;
211
				}
9977 vikram.rag 212
				else{
213
					if(stock > 25){
214
						stock=25;
215
					}
216
				}
9947 vikram.rag 217
			}
218
			else{
219
				stock =0;
220
			}
221
			try {
222
				System.out.println("Item Id " + entry.getValue().getId());
223
				logisticinfo = logisticsClient.getLogisticsEstimation(entry.getValue().getId(),"110001",DeliveryType.COD);
224
			} catch (LogisticsServiceException e) {
225
				e.printStackTrace();
226
				continue;
227
			}
228
			jsonRequest.append("{\"skuId\":"+"\""+entry.getValue().getSkuAtflipkart()+
229
					"\","+"\"attributeValues\""+":"+
230
					"{\"procurement_sla\""+":"+"\""+logisticinfo.getShippingTime()+
231
					"\","+"\"stock_count\""+":"+"\""+stock+"\""+"}}");
9983 vikram.rag 232
			item = aliveItemsMap.get(entry.getValue().getId());
233
			inventoryItems.append("<tr>" 
234
					+ "<td>" +getProductName(item)+"</td>"
235
					+"<td>" + item.getId() + "</td>"
236
					+"<td>" + actualstock + "</td>"
237
					+"<td>" + orders + "</td>"
238
					+"<td>" + stock + "</td>"
239
					+"</tr>");
9947 vikram.rag 240
			if(flipkartItemsInventoryMap.entrySet().size()!=1 && (i!=flipkartItemsInventoryMap.entrySet().size() -1 ) ){
241
				jsonRequest.append(",");	
242
			}
243
			i++;
244
			if(i%10==0 || i==flipkartItemsInventoryMap.entrySet().size()){
9968 vikram.rag 245
				StringEntity input = null;
246
				try {
9983 vikram.rag 247
					input = new StringEntity(jsonStart+jsonRequest.toString()+jsonEnd);
248
					System.out.println("Json input " + jsonStart+jsonRequest.toString()+jsonEnd);
249
					jsonRequest = new StringBuffer();
9968 vikram.rag 250
				} catch (UnsupportedEncodingException e) {
251
					// TODO Auto-generated catch block
252
					e.printStackTrace();
253
				}
9947 vikram.rag 254
				input.setContentType("application/json");
9983 vikram.rag 255
 
9947 vikram.rag 256
				httppost.setEntity(input);
9968 vikram.rag 257
				HttpResponse response = null;
258
				try {
9970 vikram.rag 259
					System.out.println("Before send " );
9968 vikram.rag 260
					response = httpclient.execute(httppost);
9970 vikram.rag 261
					System.out.println("After send " );
9968 vikram.rag 262
				} catch (IOException e) {
263
					// TODO Auto-generated catch block
264
					e.printStackTrace();
265
				}
9969 vikram.rag 266
				BufferedReader rd = null;
9968 vikram.rag 267
				try {
268
					rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
269
				} catch (IllegalStateException e) {
270
					// TODO Auto-generated catch block
271
					e.printStackTrace();
272
				} catch (IOException e) {
273
					// TODO Auto-generated catch block
274
					e.printStackTrace();
275
				}
9947 vikram.rag 276
				String line;
9969 vikram.rag 277
				try {
278
					while ((line = rd.readLine()) != null) {
279
						System.out.println("Response " + line);
9983 vikram.rag 280
						if(line.equalsIgnoreCase("{\"status\":\"success\"}")){
281
						}
282
						else{
283
							feedResponse.append(line);
284
						}
285
 
9969 vikram.rag 286
					}
287
				} catch (IOException e) {
288
					// TODO Auto-generated catch block
289
					e.printStackTrace();
9947 vikram.rag 290
				}
291
			}
292
		}
9983 vikram.rag 293
		System.out.println("Before sending email");
294
		String text = tableHeader+inventoryItems.toString()+tableFooter;
295
		try {
296
			mailer.sendSSLMessage(sendTo,"Flipkart Inventory Sent Details ", emailFromAddress, password, text);
297
		} catch (MessagingException e) {
298
			e.printStackTrace();
299
		}
300
		if(feedResponse.toString().length()>0){
301
			try {
302
				mailer.sendSSLMessage(sendTo, "Flipkart Inventory could not be updated ", feedResponse.toString(), emailFromAddress, password,new ArrayList<File>());
303
			} catch (MessagingException e) {
304
				e.printStackTrace();
305
			}
306
		}
307
		else{
308
			try {
309
				mailer.sendSSLMessage(sendTo, "Flipkart Inventory updated successfully ", feedResponse.toString(), emailFromAddress, password,new ArrayList<File>());
310
			} catch (MessagingException e) {
311
				e.printStackTrace();
312
			}
313
		}
9947 vikram.rag 314
	}
9983 vikram.rag 315
 
9947 vikram.rag 316
	private static void calculateinventory(){
317
		CatalogClient catalogServiceClient = null;
318
		Map<Long,ItemInventory> availability= new HashMap<Long,ItemInventory>();
319
		try {
320
			catalogServiceClient = new CatalogClient();
321
		} catch (TTransportException e) {
322
			// TODO Auto-generated catch block
323
			e.printStackTrace();
324
		}
325
		in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = catalogServiceClient.getClient();
326
		try {
327
			List<Item> aliveItems = catalogClient.getAllAliveItems();
328
			for(in.shop2020.model.v1.catalog.Item thriftItem:aliveItems){
329
				aliveItemsMap.put(thriftItem.getId(), thriftItem);
330
 
331
			}
332
			InventoryClient inventoryServiceClient = new InventoryClient();
333
			try {
334
				inventoryServiceClient = new InventoryClient();
335
			} catch (TTransportException e) {
336
				e.printStackTrace();
337
			}
338
			in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
339
			List<in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems> ignoreItems = inventoryClient.getIgnoredWarehouseidsAndItemids();
340
			availability = inventoryClient.getInventorySnapshot(0);
341
			List<Warehouse> allwarehouses = inventoryClient.getWarehouses(null,null,0,0,0);
342
			Map<Long,Warehouse> allWarehousesMap = new HashMap<Long,Warehouse>();
343
			for(Warehouse warehouse:allwarehouses){
344
				allWarehousesMap.put(warehouse.getId(),warehouse);
345
			}
346
			List<Warehouse> warehouses = inventoryClient.getWarehouses(WarehouseType.OURS_THIRDPARTY,null,0,0,0);
347
			List<Warehouse> ours_warehouses = inventoryClient.getWarehouses(WarehouseType.OURS, InventoryType.GOOD, 0, 0, 0);
348
			List<Long> thirdpartywarehouseids = new ArrayList<Long>();
349
			List<Long> oursGoodWarehouse = new ArrayList<Long>();
350
			for(Warehouse warehouse:warehouses){
351
				thirdpartywarehouseids.add(warehouse.getId());
352
			}
353
			for (Warehouse warehouse:ours_warehouses){
354
				oursGoodWarehouse.add(warehouse.getId());
355
			}
356
			long available=0;
357
			long reserve=0;
358
			long total_warehouse_held=0;
359
			long heldForSource=0;
360
			long total_held=0;
361
			double nlc=0;
362
			double maxnlc=0;
363
			Item thriftItem;
364
			for(FlipkartItem flipkartItem:flipkartInventoryItems){
365
				if(aliveItemsMap.get(flipkartItem.getItem_id()) != null){
366
					thriftItem = aliveItemsMap.get(flipkartItem.getItem_id());	
367
				}
368
				else{
369
					continue;
370
				}
371
				available=0;
372
				reserve=0;
373
				total_warehouse_held=0;
374
				heldForSource=0;
375
				total_held=0;
376
				nlc=0;
377
				List<Warehouse> vendor_warehouses=null;
378
				FlipkartInventoryItem item;
379
				if(availability.get(thriftItem.getId())!=null){ 
380
					ItemInventory iteminventory = availability.get(thriftItem.getId());
381
					Map<Long,Long> itemavailability = new HashMap<Long,Long>();
382
					itemavailability = iteminventory.getAvailability();
383
					if (thriftItem.isIsWarehousePreferenceSticky() && thriftItem.getPreferredVendor()!=0){
384
						//System.out.println("Item id "+thriftItem.getId()+".Found prefered vendor and warehouse is marked sticky (Calculating availability)"+thriftItem.getPreferredVendor());						
385
						vendor_warehouses = inventoryClient.getWarehouses(WarehouseType.THIRD_PARTY,InventoryType.GOOD , thriftItem.getPreferredVendor(), 0, 0);
386
						vendor_warehouses.addAll(ours_warehouses);
387
						for (Warehouse warehouse:vendor_warehouses){
388
							if(warehouse.getBillingWarehouseId()!=7){
389
								if(warehouse.getLogisticsLocation().equals(WarehouseLocation.Delhi) && warehouse.getWarehouseType().equals(WarehouseType.THIRD_PARTY) && warehouse.getVendor().getId()!=1){
390
 
391
								}
392
								else{
393
									continue;
394
								}
395
							}
396
							in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
397
							ignoredItem.setItemId(thriftItem.getId());
398
							ignoredItem.setWarehouseId(warehouse.getId());
399
							if (itemavailability.get(warehouse.getId())!=null && !thirdpartywarehouseids.contains(warehouse.getId()) && !ignoreItems.contains(ignoredItem)){
400
								try{
401
									nlc = inventoryClient.getNlcForWarehouse(warehouse.getId(),thriftItem.getId());
402
								}
403
								catch(TTransportException e){
404
									inventoryClient = inventoryServiceClient.getClient(); 
405
									nlc = inventoryClient.getNlcForWarehouse(warehouse.getId(),thriftItem.getId());
406
								}
407
								maxnlc = flipkartItem.getMaxNlc();
408
 
409
								//System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + warehouse.getId() );
410
								if(nlc !=0 && (maxnlc >= nlc)){
411
									total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 0);
412
									heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 8);
413
									available = available + itemavailability.get(warehouse.getId());
414
									total_held = total_held + total_warehouse_held; 
415
									//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
416
								}
417
								else if(maxnlc==0){
418
									total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 0);
419
									heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 8);
420
									available = available + itemavailability.get(warehouse.getId());
421
									total_held = total_held + total_warehouse_held;
422
									//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
423
								}
424
							}
425
						}
426
					}
427
					else{
428
						for (Map.Entry<Long,Long> entry :  itemavailability.entrySet()) {
429
							if(allWarehousesMap.get(entry.getKey()).getBillingWarehouseId()!=7){
430
								if(allWarehousesMap.get(entry.getKey()).getLogisticsLocation().equals(WarehouseLocation.Delhi) && allWarehousesMap.get(entry.getKey()).getWarehouseType().equals(WarehouseType.THIRD_PARTY) && allWarehousesMap.get(entry.getKey()).getVendor().getId()!=1){
431
 
432
								}
433
								else{
434
									continue;
435
								}
436
							}
437
							in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
438
							ignoredItem.setItemId(thriftItem.getId());
439
							ignoredItem.setWarehouseId(entry.getKey());
440
							if(!thirdpartywarehouseids.contains(entry.getKey()) && !ignoreItems.contains(ignoredItem)){
441
 
442
								nlc = inventoryClient.getNlcForWarehouse(entry.getKey(),thriftItem.getId());
443
								maxnlc = flipkartItem.getMaxNlc();
444
								//System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + entry.getKey() );
445
								if(nlc !=0 && (maxnlc >= nlc)){
446
									total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 0);
447
									heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 8);
448
									available =  available + entry.getValue();
449
									total_held = total_held + total_warehouse_held;
450
									//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
451
								}
452
								else if(maxnlc==0){
453
									total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 0);
454
									heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 8);
455
									available = available + itemavailability.get(entry.getKey());
456
									total_held = total_held + total_warehouse_held;
457
									//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
458
								}
459
 
460
							}
461
						}
462
					}
463
					Map<Long,Long> itemreserve = new HashMap<Long,Long>();
464
					itemreserve = iteminventory.getReserved();
465
					if (thriftItem.isIsWarehousePreferenceSticky() && thriftItem.getPreferredVendor()!=0){
466
						//System.out.println("Item id "+thriftItem.getId()+".Found prefered vendor and warehouse is marked sticky (Calculating Reserve)"+thriftItem.getPreferredVendor());
467
						for (Warehouse warehouse:vendor_warehouses){
468
							if(warehouse.getBillingWarehouseId()!=7){
469
								if(warehouse.getLogisticsLocation().equals(WarehouseLocation.Delhi) && warehouse.getWarehouseType().equals(WarehouseType.THIRD_PARTY) && warehouse.getVendor().getId()!=1){
470
 
471
								}
472
								else{
473
									continue;
474
								}
475
							}
476
							in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
477
							ignoredItem.setItemId(thriftItem.getId());
478
							ignoredItem.setWarehouseId(warehouse.getId());
479
							if (itemreserve.get(warehouse.getId())!=null && !thirdpartywarehouseids.contains(warehouse.getId()) && !ignoreItems.contains(warehouse.getId())){
480
								nlc = inventoryClient.getNlcForWarehouse(warehouse.getId(),thriftItem.getId());
481
								maxnlc = flipkartItem.getMaxNlc();
482
								//System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + warehouse.getId() );
483
								if(nlc !=0 && (maxnlc >= nlc)){
484
									reserve = reserve + itemreserve.get(warehouse.getId());
485
									//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
486
								}
487
								else if(maxnlc==0){
488
									reserve = reserve + itemreserve.get(warehouse.getId());
489
									//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
490
								}
491
 
492
							}
493
						}
494
					}else{
495
						for (Map.Entry<Long,Long> entry : itemreserve.entrySet()) {
496
							if(allWarehousesMap.get(entry.getKey()).getBillingWarehouseId()!=7){
497
								if(allWarehousesMap.get(entry.getKey()).getLogisticsLocation().equals(WarehouseLocation.Delhi) && allWarehousesMap.get(entry.getKey()).getWarehouseType().equals(WarehouseType.THIRD_PARTY) && allWarehousesMap.get(entry.getKey()).getVendor().getId()!=1){
498
 
499
								}
500
								else{
501
									continue;
502
								}
503
							}
504
							in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
505
							ignoredItem.setItemId(thriftItem.getId());
506
							ignoredItem.setWarehouseId(entry.getKey());
507
							if(!thirdpartywarehouseids.contains(entry.getKey()) && !ignoreItems.contains(ignoredItem)){
508
								nlc = inventoryClient.getNlcForWarehouse(entry.getKey(),thriftItem.getId());
509
								maxnlc = flipkartItem.getMaxNlc();
510
								//System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + entry.getKey() );
511
								if(nlc !=0 && (maxnlc >= nlc)){
512
									reserve =  reserve + entry.getValue();
513
									//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
514
								}
515
								else if(maxnlc==0){
516
									reserve =  reserve + entry.getValue();
517
									//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
518
								}
519
 
520
							}
521
 
522
							else{
523
								//System.out.println("skipping inventory for warehouse id " + entry.getKey());
524
							}
525
						}
526
					}
527
					item= new FlipkartInventoryItem(thriftItem.getId(),available,reserve,heldForSource,thriftItem.getHoldInventory(),thriftItem.getDefaultInventory(),total_held,thriftItem.isRisky(),thriftItem.getItemStatus(),flipkartItem.getSkuAtFlipkart());
528
					//System.out.println("itemId:" + thriftItem.getId() + "\tavailable: " + available + "\treserve" + reserve + "\theldForSource:" + heldForSource + "\twebsite_hold:" + thriftItem.getHoldInventory() + "\tdefault_inv:" +thriftItem.getDefaultInventory());
529
				}
530
				else{
531
					item = new FlipkartInventoryItem(thriftItem.getId(),0,0,0,thriftItem.getHoldInventory(),thriftItem.getDefaultInventory(),0,thriftItem.isRisky(),thriftItem.getItemStatus(),flipkartItem.getSkuAtFlipkart());
532
					//System.out.println("itemId:" + thriftItem.getId() + "\tavailable: " + available + "\treserve" + reserve + "\theldForSource:" + heldForSource + "\twebsite_hold:" + thriftItem.getHoldInventory() + "\tdefault_inv:" +thriftItem.getDefaultInventory());
533
				}
534
 
535
				//System.out.println(" Item details are " + thriftItem.getId() +" " + available + " " + reserve + " " + thriftItem.getHoldInventory() + " "+ thriftItem.getDefaultInventory() + " " + thriftItem.isRisky());
536
				//System.out.println("+++++++++++++++++++++++");
537
				flipkartItemsInventoryMap.put(thriftItem.getId(),item);
538
 
539
			}
540
 
541
		} catch (TException e) {
542
			// TODO Auto-generated catch block
543
			e.printStackTrace();
544
		}
545
	}
546
 
547
	static List<String> FetchOrders() throws ClientProtocolException, IOException{
548
		HttpClient client = new DefaultHttpClient();
549
		HttpPost post = new HttpPost("https://seller.flipkart.com/login");
550
		BufferedReader rd= null;
551
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
552
		nameValuePairs.add(new BasicNameValuePair("authName",
553
		"flipkart"));
554
		nameValuePairs.add(new BasicNameValuePair("username",
555
		"flipkart-support@saholic.com"));
556
		nameValuePairs.add(new BasicNameValuePair("password",
557
		"076c27ee24d7596b06608a8ed2559f87"));
558
		post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
559
		HttpResponse response = client.execute(post);
560
		rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
561
		String line = "";
562
		Gson gson;
563
		List<Order> orders = new ArrayList<Order>(); 
564
		OrderItems new_orders;
565
		OrderItems conf_orders;
566
		HttpGet get_new;
567
		HttpGet get_conf; 
568
		long time;
569
		int i=1;
570
		while ((line = rd.readLine()) != null) {
571
			System.out.println(line);
572
		}
573
		time = System.currentTimeMillis();
574
		while(true){
575
			get_new = new HttpGet("https://seller.flipkart.com/dashboard/som/new_order_items?status=on_hold%2Capproved.payment_approved&sort=confirm_by_date&page="+i+"&page_size=100&_="+time);
576
			response = client.execute(get_new);
577
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
578
			line = "";
579
			gson = new Gson();
580
			new_orders = (OrderItems) gson.fromJson(rd, OrderItems.class);
581
			if(new_orders.getOrder_items().size()>0){
582
				orders.addAll(new_orders.getOrder_items());
583
			}
584
			else{
585
				break;
586
			}
587
			i++;
588
		}
589
		i=1;
590
		time = System.currentTimeMillis();
591
		while(true){
592
			get_conf = new HttpGet("https://seller.flipkart.com/dashboard/som/confirmed_order_items?status=approved.seller_approved&sort=confirm_by_date&page="+i+"&page_size=100&_="+time);
593
			response = client.execute(get_conf);
594
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
595
			line = "";
596
			gson = new Gson();
597
			conf_orders = (OrderItems) gson.fromJson(rd, OrderItems.class);
598
			if(conf_orders.getOrder_items().size()>0){
599
				orders.addAll(conf_orders.getOrder_items());
600
			}
601
			else{
602
				break;
603
			}
604
			i++;
605
		}
606
		List<String> orderIdList = new ArrayList<String>();
607
		for(Order order:orders){
608
			System.out.println("Order  " + order.getExternalId() +" "+order.getOrderItemId() + " : "+ order.getStatus() + " "+order.getStatusDateMessage() + " "
609
					+ order.getStatusLabel() + " " + order.getPickup_by_date() + " "+ order.getTrackingId());
610
			orderIdList.add(order.getExternalId());
611
		}
612
		return orderIdList; 
613
	}
9983 vikram.rag 614
	static String getProductName(Item item){
615
		return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor()); 
616
 
617
	}
618
	static String getName(String name){
619
		if(name==null || name.length()==0){
620
			return "";
621
		}
622
		else{
623
			return name;
624
		}
625
	}
9947 vikram.rag 626
}