Subversion Repositories SmartDukaan

Rev

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