Subversion Repositories SmartDukaan

Rev

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