Subversion Repositories SmartDukaan

Rev

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