Subversion Repositories SmartDukaan

Rev

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