Subversion Repositories SmartDukaan

Rev

Rev 9984 | Rev 9987 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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