Subversion Repositories SmartDukaan

Rev

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