Subversion Repositories SmartDukaan

Rev

Rev 9985 | Rev 9988 | 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);
9984 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
9984 vikram.rag 132
		*/
133
		httpclient.getCredentialsProvider().setCredentials(
9975 vikram.rag 134
				new AuthScope("api.flipkart.net", 443),
9974 vikram.rag 135
				new UsernamePasswordCredentials("m2z93iskuj81qiid","0c7ab6a5-98c0-4cdc-8be3-72c591e0add4")); //Prod Server Access Keys
9984 vikram.rag 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;
9985 vikram.rag 157
		long available,reserve,heldforsource,totalheld,allocable,holdinventory,defaultinventory,actualstock,orders;
9983 vikram.rag 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()){
9985 vikram.rag 170
			actualstock=0;
171
			orders=0;
172
			stock=0;
9983 vikram.rag 173
			feedResponse=  new StringBuffer();
9947 vikram.rag 174
			available = entry.getValue().getAvailability();
175
			reserve = entry.getValue().getReserve();
176
			heldforsource = entry.getValue().getHeldForSource();
177
			totalheld = entry.getValue().getTotalHeldInventory();
178
			holdinventory = entry.getValue().getHoldInventory();
179
			defaultinventory = entry.getValue().getDefaultInventory();
9987 vikram.rag 180
			System.out.println("Item ID  " + entry.getKey());
181
			System.out.println("Available " + available);
182
			System.out.println("Reserve " + reserve);
183
			System.out.println("Held For Source " + heldforsource);
184
			System.out.println("Total Held " + totalheld);
185
			System.out.println("General Hold " + holdinventory );
186
			System.out.println("Default Inventory " + defaultinventory);
9947 vikram.rag 187
			if(!entry.getValue().getStatus().equals(status.PAUSED)){
188
				if(entry.getValue().getRisky()){
189
					if((available - reserve) < totalheld && totalheld!=0){
190
						stock = (long) Math.floor(((available - reserve)*heldforsource/totalheld));
191
					}
192
					else{
193
						allocable = available - reserve - totalheld - holdinventory;
194
						if(allocable < 0){
195
							allocable = 0;
196
						}
197
						stock = Math.round(allocable*1/3) + heldforsource;
198
					}
199
 
200
				}
201
				else{
202
					allocable = available - reserve - totalheld;
203
					if(allocable < 0){
204
						allocable = 0;
205
					}
206
					if((allocable + heldforsource ) > defaultinventory){
207
						stock = Math.round(allocable*1/3) +  heldforsource;
208
					}
209
					else{
210
						stock = defaultinventory;
211
					}
212
				}
213
				System.out.println("Item ID " + entry.getKey()+" Stock "+stock +" Orders " + itemIdOrdersMap.get(entry.getKey()));
9971 vikram.rag 214
				if(itemIdOrdersMap!=null && itemIdOrdersMap.containsKey(entry.getKey())){
9977 vikram.rag 215
					stock = stock + itemIdOrdersMap.get(entry.getKey());  
9983 vikram.rag 216
					orders = itemIdOrdersMap.get(entry.getKey());
9947 vikram.rag 217
				}
9985 vikram.rag 218
				actualstock = stock;
9947 vikram.rag 219
				if(stock<0){
220
					stock =0;
221
				}
9977 vikram.rag 222
				else{
223
					if(stock > 25){
224
						stock=25;
225
					}
226
				}
9947 vikram.rag 227
			}
228
			else{
229
				stock =0;
230
			}
231
			try {
232
				System.out.println("Item Id " + entry.getValue().getId());
233
				logisticinfo = logisticsClient.getLogisticsEstimation(entry.getValue().getId(),"110001",DeliveryType.COD);
234
			} catch (LogisticsServiceException e) {
235
				e.printStackTrace();
236
				continue;
237
			}
238
			jsonRequest.append("{\"skuId\":"+"\""+entry.getValue().getSkuAtflipkart()+
239
					"\","+"\"attributeValues\""+":"+
240
					"{\"procurement_sla\""+":"+"\""+logisticinfo.getShippingTime()+
241
					"\","+"\"stock_count\""+":"+"\""+stock+"\""+"}}");
9983 vikram.rag 242
			item = aliveItemsMap.get(entry.getValue().getId());
243
			inventoryItems.append("<tr>" 
244
					+ "<td>" +getProductName(item)+"</td>"
245
					+"<td>" + item.getId() + "</td>"
246
					+"<td>" + actualstock + "</td>"
247
					+"<td>" + orders + "</td>"
248
					+"<td>" + stock + "</td>"
249
					+"</tr>");
9947 vikram.rag 250
			if(flipkartItemsInventoryMap.entrySet().size()!=1 && (i!=flipkartItemsInventoryMap.entrySet().size() -1 ) ){
251
				jsonRequest.append(",");	
252
			}
253
			i++;
254
			if(i%10==0 || i==flipkartItemsInventoryMap.entrySet().size()){
9968 vikram.rag 255
				StringEntity input = null;
256
				try {
9983 vikram.rag 257
					input = new StringEntity(jsonStart+jsonRequest.toString()+jsonEnd);
258
					System.out.println("Json input " + jsonStart+jsonRequest.toString()+jsonEnd);
259
					jsonRequest = new StringBuffer();
9968 vikram.rag 260
				} catch (UnsupportedEncodingException e) {
261
					// TODO Auto-generated catch block
262
					e.printStackTrace();
263
				}
9947 vikram.rag 264
				input.setContentType("application/json");
9983 vikram.rag 265
 
9947 vikram.rag 266
				httppost.setEntity(input);
9968 vikram.rag 267
				HttpResponse response = null;
268
				try {
9970 vikram.rag 269
					System.out.println("Before send " );
9968 vikram.rag 270
					response = httpclient.execute(httppost);
9970 vikram.rag 271
					System.out.println("After send " );
9968 vikram.rag 272
				} catch (IOException e) {
273
					// TODO Auto-generated catch block
274
					e.printStackTrace();
275
				}
9969 vikram.rag 276
				BufferedReader rd = null;
9968 vikram.rag 277
				try {
278
					rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
279
				} catch (IllegalStateException e) {
280
					// TODO Auto-generated catch block
281
					e.printStackTrace();
282
				} catch (IOException e) {
283
					// TODO Auto-generated catch block
284
					e.printStackTrace();
285
				}
9947 vikram.rag 286
				String line;
9969 vikram.rag 287
				try {
288
					while ((line = rd.readLine()) != null) {
289
						System.out.println("Response " + line);
9983 vikram.rag 290
						if(line.equalsIgnoreCase("{\"status\":\"success\"}")){
291
						}
292
						else{
293
							feedResponse.append(line);
294
						}
295
 
9969 vikram.rag 296
					}
297
				} catch (IOException e) {
298
					// TODO Auto-generated catch block
299
					e.printStackTrace();
9947 vikram.rag 300
				}
301
			}
302
		}
9983 vikram.rag 303
		System.out.println("Before sending email");
304
		String text = tableHeader+inventoryItems.toString()+tableFooter;
305
		try {
306
			mailer.sendSSLMessage(sendTo,"Flipkart Inventory Sent Details ", emailFromAddress, password, text);
307
		} catch (MessagingException e) {
308
			e.printStackTrace();
309
		}
310
		if(feedResponse.toString().length()>0){
311
			try {
312
				mailer.sendSSLMessage(sendTo, "Flipkart Inventory could not be updated ", feedResponse.toString(), emailFromAddress, password,new ArrayList<File>());
313
			} catch (MessagingException e) {
314
				e.printStackTrace();
315
			}
316
		}
317
		else{
318
			try {
319
				mailer.sendSSLMessage(sendTo, "Flipkart Inventory updated successfully ", feedResponse.toString(), emailFromAddress, password,new ArrayList<File>());
320
			} catch (MessagingException e) {
321
				e.printStackTrace();
322
			}
323
		}
9947 vikram.rag 324
	}
9983 vikram.rag 325
 
9947 vikram.rag 326
	private static void calculateinventory(){
327
		CatalogClient catalogServiceClient = null;
328
		Map<Long,ItemInventory> availability= new HashMap<Long,ItemInventory>();
329
		try {
330
			catalogServiceClient = new CatalogClient();
331
		} catch (TTransportException e) {
332
			// TODO Auto-generated catch block
333
			e.printStackTrace();
334
		}
335
		in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = catalogServiceClient.getClient();
336
		try {
337
			List<Item> aliveItems = catalogClient.getAllAliveItems();
338
			for(in.shop2020.model.v1.catalog.Item thriftItem:aliveItems){
339
				aliveItemsMap.put(thriftItem.getId(), thriftItem);
340
 
341
			}
342
			InventoryClient inventoryServiceClient = new InventoryClient();
343
			try {
344
				inventoryServiceClient = new InventoryClient();
345
			} catch (TTransportException e) {
346
				e.printStackTrace();
347
			}
348
			in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
349
			List<in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems> ignoreItems = inventoryClient.getIgnoredWarehouseidsAndItemids();
350
			availability = inventoryClient.getInventorySnapshot(0);
351
			List<Warehouse> allwarehouses = inventoryClient.getWarehouses(null,null,0,0,0);
352
			Map<Long,Warehouse> allWarehousesMap = new HashMap<Long,Warehouse>();
353
			for(Warehouse warehouse:allwarehouses){
354
				allWarehousesMap.put(warehouse.getId(),warehouse);
355
			}
356
			List<Warehouse> warehouses = inventoryClient.getWarehouses(WarehouseType.OURS_THIRDPARTY,null,0,0,0);
357
			List<Warehouse> ours_warehouses = inventoryClient.getWarehouses(WarehouseType.OURS, InventoryType.GOOD, 0, 0, 0);
358
			List<Long> thirdpartywarehouseids = new ArrayList<Long>();
359
			List<Long> oursGoodWarehouse = new ArrayList<Long>();
360
			for(Warehouse warehouse:warehouses){
361
				thirdpartywarehouseids.add(warehouse.getId());
362
			}
363
			for (Warehouse warehouse:ours_warehouses){
364
				oursGoodWarehouse.add(warehouse.getId());
365
			}
366
			long available=0;
367
			long reserve=0;
368
			long total_warehouse_held=0;
369
			long heldForSource=0;
370
			long total_held=0;
371
			double nlc=0;
372
			double maxnlc=0;
373
			Item thriftItem;
374
			for(FlipkartItem flipkartItem:flipkartInventoryItems){
375
				if(aliveItemsMap.get(flipkartItem.getItem_id()) != null){
376
					thriftItem = aliveItemsMap.get(flipkartItem.getItem_id());	
377
				}
378
				else{
379
					continue;
380
				}
381
				available=0;
382
				reserve=0;
383
				total_warehouse_held=0;
384
				heldForSource=0;
385
				total_held=0;
386
				nlc=0;
387
				List<Warehouse> vendor_warehouses=null;
388
				FlipkartInventoryItem item;
389
				if(availability.get(thriftItem.getId())!=null){ 
390
					ItemInventory iteminventory = availability.get(thriftItem.getId());
391
					Map<Long,Long> itemavailability = new HashMap<Long,Long>();
392
					itemavailability = iteminventory.getAvailability();
393
					if (thriftItem.isIsWarehousePreferenceSticky() && thriftItem.getPreferredVendor()!=0){
394
						//System.out.println("Item id "+thriftItem.getId()+".Found prefered vendor and warehouse is marked sticky (Calculating availability)"+thriftItem.getPreferredVendor());						
395
						vendor_warehouses = inventoryClient.getWarehouses(WarehouseType.THIRD_PARTY,InventoryType.GOOD , thriftItem.getPreferredVendor(), 0, 0);
396
						vendor_warehouses.addAll(ours_warehouses);
397
						for (Warehouse warehouse:vendor_warehouses){
398
							if(warehouse.getBillingWarehouseId()!=7){
399
								if(warehouse.getLogisticsLocation().equals(WarehouseLocation.Delhi) && warehouse.getWarehouseType().equals(WarehouseType.THIRD_PARTY) && warehouse.getVendor().getId()!=1){
400
 
401
								}
402
								else{
403
									continue;
404
								}
405
							}
406
							in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
407
							ignoredItem.setItemId(thriftItem.getId());
408
							ignoredItem.setWarehouseId(warehouse.getId());
409
							if (itemavailability.get(warehouse.getId())!=null && !thirdpartywarehouseids.contains(warehouse.getId()) && !ignoreItems.contains(ignoredItem)){
410
								try{
411
									nlc = inventoryClient.getNlcForWarehouse(warehouse.getId(),thriftItem.getId());
412
								}
413
								catch(TTransportException e){
414
									inventoryClient = inventoryServiceClient.getClient(); 
415
									nlc = inventoryClient.getNlcForWarehouse(warehouse.getId(),thriftItem.getId());
416
								}
417
								maxnlc = flipkartItem.getMaxNlc();
418
 
419
								//System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + warehouse.getId() );
420
								if(nlc !=0 && (maxnlc >= nlc)){
421
									total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 0);
422
									heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 8);
423
									available = available + itemavailability.get(warehouse.getId());
424
									total_held = total_held + total_warehouse_held; 
425
									//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
426
								}
427
								else if(maxnlc==0){
428
									total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 0);
429
									heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 8);
430
									available = available + itemavailability.get(warehouse.getId());
431
									total_held = total_held + total_warehouse_held;
432
									//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
433
								}
434
							}
435
						}
436
					}
437
					else{
438
						for (Map.Entry<Long,Long> entry :  itemavailability.entrySet()) {
439
							if(allWarehousesMap.get(entry.getKey()).getBillingWarehouseId()!=7){
440
								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){
441
 
442
								}
443
								else{
444
									continue;
445
								}
446
							}
447
							in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
448
							ignoredItem.setItemId(thriftItem.getId());
449
							ignoredItem.setWarehouseId(entry.getKey());
450
							if(!thirdpartywarehouseids.contains(entry.getKey()) && !ignoreItems.contains(ignoredItem)){
451
 
452
								nlc = inventoryClient.getNlcForWarehouse(entry.getKey(),thriftItem.getId());
453
								maxnlc = flipkartItem.getMaxNlc();
454
								//System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + entry.getKey() );
455
								if(nlc !=0 && (maxnlc >= nlc)){
456
									total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 0);
457
									heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 8);
458
									available =  available + entry.getValue();
459
									total_held = total_held + total_warehouse_held;
460
									//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
461
								}
462
								else if(maxnlc==0){
463
									total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 0);
464
									heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 8);
465
									available = available + itemavailability.get(entry.getKey());
466
									total_held = total_held + total_warehouse_held;
467
									//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
468
								}
469
 
470
							}
471
						}
472
					}
473
					Map<Long,Long> itemreserve = new HashMap<Long,Long>();
474
					itemreserve = iteminventory.getReserved();
475
					if (thriftItem.isIsWarehousePreferenceSticky() && thriftItem.getPreferredVendor()!=0){
476
						//System.out.println("Item id "+thriftItem.getId()+".Found prefered vendor and warehouse is marked sticky (Calculating Reserve)"+thriftItem.getPreferredVendor());
477
						for (Warehouse warehouse:vendor_warehouses){
478
							if(warehouse.getBillingWarehouseId()!=7){
479
								if(warehouse.getLogisticsLocation().equals(WarehouseLocation.Delhi) && warehouse.getWarehouseType().equals(WarehouseType.THIRD_PARTY) && warehouse.getVendor().getId()!=1){
480
 
481
								}
482
								else{
483
									continue;
484
								}
485
							}
486
							in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
487
							ignoredItem.setItemId(thriftItem.getId());
488
							ignoredItem.setWarehouseId(warehouse.getId());
489
							if (itemreserve.get(warehouse.getId())!=null && !thirdpartywarehouseids.contains(warehouse.getId()) && !ignoreItems.contains(warehouse.getId())){
490
								nlc = inventoryClient.getNlcForWarehouse(warehouse.getId(),thriftItem.getId());
491
								maxnlc = flipkartItem.getMaxNlc();
492
								//System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + warehouse.getId() );
493
								if(nlc !=0 && (maxnlc >= nlc)){
494
									reserve = reserve + itemreserve.get(warehouse.getId());
495
									//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
496
								}
497
								else if(maxnlc==0){
498
									reserve = reserve + itemreserve.get(warehouse.getId());
499
									//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
500
								}
501
 
502
							}
503
						}
504
					}else{
505
						for (Map.Entry<Long,Long> entry : itemreserve.entrySet()) {
506
							if(allWarehousesMap.get(entry.getKey()).getBillingWarehouseId()!=7){
507
								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){
508
 
509
								}
510
								else{
511
									continue;
512
								}
513
							}
514
							in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
515
							ignoredItem.setItemId(thriftItem.getId());
516
							ignoredItem.setWarehouseId(entry.getKey());
517
							if(!thirdpartywarehouseids.contains(entry.getKey()) && !ignoreItems.contains(ignoredItem)){
518
								nlc = inventoryClient.getNlcForWarehouse(entry.getKey(),thriftItem.getId());
519
								maxnlc = flipkartItem.getMaxNlc();
520
								//System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + entry.getKey() );
521
								if(nlc !=0 && (maxnlc >= nlc)){
522
									reserve =  reserve + entry.getValue();
523
									//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
524
								}
525
								else if(maxnlc==0){
526
									reserve =  reserve + entry.getValue();
527
									//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
528
								}
529
 
530
							}
531
 
532
							else{
533
								//System.out.println("skipping inventory for warehouse id " + entry.getKey());
534
							}
535
						}
536
					}
537
					item= new FlipkartInventoryItem(thriftItem.getId(),available,reserve,heldForSource,thriftItem.getHoldInventory(),thriftItem.getDefaultInventory(),total_held,thriftItem.isRisky(),thriftItem.getItemStatus(),flipkartItem.getSkuAtFlipkart());
538
					//System.out.println("itemId:" + thriftItem.getId() + "\tavailable: " + available + "\treserve" + reserve + "\theldForSource:" + heldForSource + "\twebsite_hold:" + thriftItem.getHoldInventory() + "\tdefault_inv:" +thriftItem.getDefaultInventory());
539
				}
540
				else{
541
					item = new FlipkartInventoryItem(thriftItem.getId(),0,0,0,thriftItem.getHoldInventory(),thriftItem.getDefaultInventory(),0,thriftItem.isRisky(),thriftItem.getItemStatus(),flipkartItem.getSkuAtFlipkart());
542
					//System.out.println("itemId:" + thriftItem.getId() + "\tavailable: " + available + "\treserve" + reserve + "\theldForSource:" + heldForSource + "\twebsite_hold:" + thriftItem.getHoldInventory() + "\tdefault_inv:" +thriftItem.getDefaultInventory());
543
				}
544
 
545
				//System.out.println(" Item details are " + thriftItem.getId() +" " + available + " " + reserve + " " + thriftItem.getHoldInventory() + " "+ thriftItem.getDefaultInventory() + " " + thriftItem.isRisky());
546
				//System.out.println("+++++++++++++++++++++++");
547
				flipkartItemsInventoryMap.put(thriftItem.getId(),item);
548
 
549
			}
550
 
551
		} catch (TException e) {
552
			// TODO Auto-generated catch block
553
			e.printStackTrace();
554
		}
555
	}
556
 
557
	static List<String> FetchOrders() throws ClientProtocolException, IOException{
558
		HttpClient client = new DefaultHttpClient();
559
		HttpPost post = new HttpPost("https://seller.flipkart.com/login");
560
		BufferedReader rd= null;
561
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
562
		nameValuePairs.add(new BasicNameValuePair("authName",
563
		"flipkart"));
564
		nameValuePairs.add(new BasicNameValuePair("username",
565
		"flipkart-support@saholic.com"));
566
		nameValuePairs.add(new BasicNameValuePair("password",
567
		"076c27ee24d7596b06608a8ed2559f87"));
568
		post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
569
		HttpResponse response = client.execute(post);
570
		rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
571
		String line = "";
572
		Gson gson;
573
		List<Order> orders = new ArrayList<Order>(); 
574
		OrderItems new_orders;
575
		OrderItems conf_orders;
576
		HttpGet get_new;
577
		HttpGet get_conf; 
578
		long time;
579
		int i=1;
580
		while ((line = rd.readLine()) != null) {
581
			System.out.println(line);
582
		}
583
		time = System.currentTimeMillis();
584
		while(true){
585
			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);
586
			response = client.execute(get_new);
587
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
588
			line = "";
589
			gson = new Gson();
590
			new_orders = (OrderItems) gson.fromJson(rd, OrderItems.class);
591
			if(new_orders.getOrder_items().size()>0){
592
				orders.addAll(new_orders.getOrder_items());
593
			}
594
			else{
595
				break;
596
			}
597
			i++;
598
		}
599
		i=1;
600
		time = System.currentTimeMillis();
601
		while(true){
602
			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);
603
			response = client.execute(get_conf);
604
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
605
			line = "";
606
			gson = new Gson();
607
			conf_orders = (OrderItems) gson.fromJson(rd, OrderItems.class);
608
			if(conf_orders.getOrder_items().size()>0){
609
				orders.addAll(conf_orders.getOrder_items());
610
			}
611
			else{
612
				break;
613
			}
614
			i++;
615
		}
616
		List<String> orderIdList = new ArrayList<String>();
617
		for(Order order:orders){
618
			System.out.println("Order  " + order.getExternalId() +" "+order.getOrderItemId() + " : "+ order.getStatus() + " "+order.getStatusDateMessage() + " "
619
					+ order.getStatusLabel() + " " + order.getPickup_by_date() + " "+ order.getTrackingId());
620
			orderIdList.add(order.getExternalId());
621
		}
622
		return orderIdList; 
623
	}
9983 vikram.rag 624
	static String getProductName(Item item){
625
		return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor()); 
626
 
627
	}
628
	static String getName(String name){
629
		if(name==null || name.length()==0){
630
			return "";
631
		}
632
		else{
633
			return name;
634
		}
635
	}
9947 vikram.rag 636
}