Subversion Repositories SmartDukaan

Rev

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