Subversion Repositories SmartDukaan

Rev

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

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