Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
9404 vikram.rag 1
package in.shop2020;
2
 
9456 vikram.rag 3
import in.shop2020.model.v1.catalog.CatalogService;
9404 vikram.rag 4
import in.shop2020.model.v1.catalog.Item;
5
import in.shop2020.model.v1.catalog.SnapdealItem;
6
import in.shop2020.model.v1.inventory.InventoryService.Client;
7
import in.shop2020.model.v1.inventory.InventoryType;
8
import in.shop2020.model.v1.inventory.ItemInventory;
9
import in.shop2020.model.v1.inventory.SnapdealInventoryItem;
10
import in.shop2020.model.v1.inventory.Warehouse;
11
import in.shop2020.model.v1.inventory.WarehouseType;
12
import in.shop2020.thrift.clients.CatalogClient;
13
import in.shop2020.thrift.clients.InventoryClient;
14
import in.shop2020.thrift.clients.TransactionClient;
15
import inventory.Inventory;
9456 vikram.rag 16
import inventory.InventoryHistory;
17
import inventory.InventoryHistoryItems;
9404 vikram.rag 18
import inventory.InventoryItems;
19
import inventory.SnapdealItemForInventory;
20
 
21
import java.io.BufferedReader;
22
import java.io.FileInputStream;
23
import java.io.FileNotFoundException;
24
import java.io.FileOutputStream;
25
import java.io.IOException;
26
import java.io.InputStreamReader;
27
import java.io.UnsupportedEncodingException;
28
import java.util.ArrayList;
29
import java.util.HashMap;
30
import java.util.List;
31
import java.util.Map;
32
 
33
import org.apache.http.HttpResponse;
34
import org.apache.http.NameValuePair;
35
import org.apache.http.client.ClientProtocolException;
36
import org.apache.http.client.HttpClient;
37
import org.apache.http.client.entity.UrlEncodedFormEntity;
38
import org.apache.http.client.methods.HttpGet;
39
import org.apache.http.client.methods.HttpPost;
40
import org.apache.http.impl.client.DefaultHttpClient;
41
import org.apache.http.message.BasicNameValuePair;
42
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
43
import org.apache.poi.ss.usermodel.Row;
44
import org.apache.poi.ss.usermodel.Sheet;
45
import org.apache.poi.ss.usermodel.Workbook;
46
import org.apache.thrift.TException;
47
import org.apache.thrift.transport.TTransportException;
9456 vikram.rag 48
 
9404 vikram.rag 49
import com.google.gson.Gson;
50
 
51
public class CreateSnapDealInventoryFeed {
9456 vikram.rag 52
	private static long time = System.currentTimeMillis();
9415 vikram.rag 53
	private static String SNAPDEAL_INVENTORY_SHEET;
9404 vikram.rag 54
	private static Map<Long, SnapdealItemForInventory> allItemsInventoryMap = new HashMap<Long, SnapdealItemForInventory>();
55
	private static ArrayList<Inventory> inventoryItemList;
56
	static Map<Long,Long> itemIdpendingOrdersMap = new HashMap<Long,Long>();
9456 vikram.rag 57
	static Map<String,Long> skuitemidMap = new HashMap<String,Long>();
58
	private static Map<String,InventoryHistory> inventoryhistoryItemMap;
59
	private static DefaultHttpClient client = new DefaultHttpClient();
9404 vikram.rag 60
	private static void calculateinventory(){
61
		CatalogClient catalogServiceClient = null;
62
		Map<Long,ItemInventory> availability= new HashMap<Long,ItemInventory>();
63
		try {
64
			catalogServiceClient = new CatalogClient();
65
		} catch (TTransportException e) {
66
			// TODO Auto-generated catch block
67
			e.printStackTrace();
68
		}
69
		in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = catalogServiceClient.getClient();
70
		try {
71
			List<Item> aliveItems = catalogClient.getAllAliveItems();
72
			Map<Long,Item> aliveItemsMap = new HashMap<Long,Item>(); 
73
			for(in.shop2020.model.v1.catalog.Item thriftItem:aliveItems){
74
				aliveItemsMap.put(thriftItem.getId(), thriftItem);
75
 
76
			}
77
			InventoryClient inventoryServiceClient = null;
78
			try {
79
				inventoryServiceClient = new InventoryClient();
80
			} catch (TTransportException e) {
81
				e.printStackTrace();
82
			}
83
			in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
84
			List<in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems> ignoreItems = inventoryClient.getIgnoredWarehouseidsAndItemids();
9415 vikram.rag 85
			SNAPDEAL_INVENTORY_SHEET = "/home/snapdeal/snapdeal-inventory-"+time+".xls";
9404 vikram.rag 86
			availability = inventoryClient.getInventorySnapshot(0);
9456 vikram.rag 87
			List<Warehouse> allwarehouses = inventoryClient.getWarehouses(null,null,0,0,0);
88
			Map<Long,Warehouse> allWarehousesMap = new HashMap<Long,Warehouse>();
89
			for(Warehouse warehouse:allwarehouses){
90
				allWarehousesMap.put(warehouse.getId(),warehouse);
91
			}
9404 vikram.rag 92
			List<Warehouse> warehouses = inventoryClient.getWarehouses(WarehouseType.OURS_THIRDPARTY,null,0,0,0);
93
			List<Warehouse> ours_warehouses = inventoryClient.getWarehouses(WarehouseType.OURS, InventoryType.GOOD, 0, 0, 0);
94
			List<Long> thirdpartywarehouseids = new ArrayList<Long>();
95
			List<Long> oursGoodWarehouse = new ArrayList<Long>();
96
			for(Warehouse warehouse:warehouses){
97
				thirdpartywarehouseids.add(warehouse.getId());
98
			}
99
			for (Warehouse warehouse:ours_warehouses){
100
				oursGoodWarehouse.add(warehouse.getId());
101
			}
102
			long available=0;
103
			long reserve=0;
104
			long total_held=0;
105
			long heldForSource=0;
106
			double nlc=0;
107
			double maxnlc=0;
108
			Item thriftItem;
9456 vikram.rag 109
			long sku = 0;
9404 vikram.rag 110
			for(Inventory inventoryItem:inventoryItemList){
9456 vikram.rag 111
				try{
112
					sku = Long.parseLong(inventoryItem.getSellerSku());
9404 vikram.rag 113
				}
9456 vikram.rag 114
				catch(NumberFormatException nfe){
115
					System.out.println(inventoryItem.getSellerSku());
116
					if(skuitemidMap.get(inventoryItem.getSellerSku())!=null){
117
						sku = skuitemidMap.get(inventoryItem.getSellerSku());
118
					}
119
					else{
120
						continue;
121
					}
122
				}
123
				if(aliveItemsMap.get(sku) != null){
124
					thriftItem = aliveItemsMap.get(sku);	
125
				}
9404 vikram.rag 126
				else{
127
					continue;
128
				}
129
				available=0;
130
				reserve=0;
131
				total_held=0;
132
				heldForSource=0;
133
				nlc=0;
134
				List<Warehouse> vendor_warehouses=null;
135
				SnapdealItemForInventory item;
136
				if(availability.get(thriftItem.getId())!=null){ 
137
					ItemInventory iteminventory = availability.get(thriftItem.getId());
138
					Map<Long,Long> itemavailability = new HashMap<Long,Long>();
139
					itemavailability = iteminventory.getAvailability();
140
					if (thriftItem.isIsWarehousePreferenceSticky() && thriftItem.getPreferredVendor()!=0){
141
						//System.out.println("Item id "+thriftItem.getId()+".Found prefered vendor and warehouse is marked sticky (Calculating availability)"+thriftItem.getPreferredVendor());						
142
						vendor_warehouses = inventoryClient.getWarehouses(WarehouseType.THIRD_PARTY,InventoryType.GOOD , thriftItem.getPreferredVendor(), 0, 0);
143
						vendor_warehouses.addAll(ours_warehouses);
144
						for (Warehouse warehouse:vendor_warehouses){
9456 vikram.rag 145
							if(warehouse.getBillingWarehouseId()!=7){
146
								continue;
147
							}
9404 vikram.rag 148
							in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
149
							ignoredItem.setItemId(thriftItem.getId());
150
							ignoredItem.setWarehouseId(warehouse.getId());
151
							if (itemavailability.get(warehouse.getId())!=null && !thirdpartywarehouseids.contains(warehouse.getId()) && !ignoreItems.contains(ignoredItem)){
152
								try{
153
									nlc = inventoryClient.getNlcForWarehouse(warehouse.getId(),thriftItem.getId());
154
								}
155
								catch(TTransportException e){
156
									inventoryClient = inventoryServiceClient.getClient(); 
157
									nlc = inventoryClient.getNlcForWarehouse(warehouse.getId(),thriftItem.getId());
158
								}
159
								maxnlc = catalogClient.getSnapdealItem(thriftItem.getId()).getMaxNlc();
9413 vikram.rag 160
								System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + warehouse.getId() );
9404 vikram.rag 161
								if(nlc !=0 && (maxnlc >= nlc)){
162
									total_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 0);
163
									heldForSource = heldForSource + total_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 7);
164
									available = available + itemavailability.get(warehouse.getId()) - total_held;
9413 vikram.rag 165
									System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
9404 vikram.rag 166
								}
9413 vikram.rag 167
								else if(maxnlc==0){
168
									total_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 0);
169
									heldForSource = heldForSource + total_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 7);
170
									available = available + itemavailability.get(warehouse.getId()) - total_held;
171
									System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
9404 vikram.rag 172
								}
173
							}
174
						}
175
					}
176
					else{
177
						for (Map.Entry<Long,Long> entry :  itemavailability.entrySet()) {
9456 vikram.rag 178
							if(allWarehousesMap.get(entry.getKey()).getBillingWarehouseId()!=7){
179
								continue;
180
							}
9404 vikram.rag 181
							in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
182
							ignoredItem.setItemId(thriftItem.getId());
183
							ignoredItem.setWarehouseId(entry.getKey());
184
							if(!thirdpartywarehouseids.contains(entry.getKey()) && !ignoreItems.contains(ignoredItem)){
185
								nlc = inventoryClient.getNlcForWarehouse(entry.getKey(),thriftItem.getId());
186
								try{
187
									maxnlc = catalogClient.getSnapdealItem(thriftItem.getId()).getMaxNlc();
188
								}
189
								catch(TTransportException e){
190
									catalogClient = catalogServiceClient.getClient();  
191
									maxnlc = catalogClient.getSnapdealItem(thriftItem.getId()).getMaxNlc();
192
								}
193
								if(nlc !=0 && (maxnlc >= nlc)){
194
									total_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 0);
195
									heldForSource = heldForSource + total_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 7);
196
									available =  available + entry.getValue() - total_held;
197
								}
9413 vikram.rag 198
								else if(maxnlc==0){
199
									total_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 0);
200
									heldForSource = heldForSource + total_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 7);
201
									available = available + itemavailability.get(entry.getKey()) - total_held;
202
								}
203
 
9404 vikram.rag 204
							}
205
						}
206
					}
207
					Map<Long,Long> itemreserve = new HashMap<Long,Long>();
208
					itemreserve = iteminventory.getReserved();
209
					if (thriftItem.isIsWarehousePreferenceSticky() && thriftItem.getPreferredVendor()!=0){
210
						//System.out.println("Item id "+thriftItem.getId()+".Found prefered vendor and warehouse is marked sticky (Calculating Reserve)"+thriftItem.getPreferredVendor());
9456 vikram.rag 211
						vendor_warehouses.addAll(ours_warehouses);
9404 vikram.rag 212
						for (Warehouse warehouse:vendor_warehouses){
9456 vikram.rag 213
							if(warehouse.getBillingWarehouseId()!=7){
214
								continue;
215
							}
9404 vikram.rag 216
							in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
217
							ignoredItem.setItemId(thriftItem.getId());
218
							ignoredItem.setWarehouseId(warehouse.getId());
219
							if (itemreserve.get(warehouse.getId())!=null && !thirdpartywarehouseids.contains(warehouse.getId()) && !ignoreItems.contains(warehouse.getId())){
220
								nlc = inventoryClient.getNlcForWarehouse(warehouse.getId(),thriftItem.getId());
221
								maxnlc = catalogClient.getSnapdealItem(thriftItem.getId()).getMaxNlc();
222
								if(nlc !=0 && (maxnlc >= nlc)){
223
									reserve = reserve + itemreserve.get(warehouse.getId());
9413 vikram.rag 224
									System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
9404 vikram.rag 225
								}
9413 vikram.rag 226
								else if(maxnlc==0){
227
									reserve = reserve + itemreserve.get(warehouse.getId());
228
									System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
229
								}
230
 
9404 vikram.rag 231
							}
232
						}
233
					}else{
234
						for (Map.Entry<Long,Long> entry : itemreserve.entrySet()) {
9456 vikram.rag 235
							if(allWarehousesMap.get(entry.getKey()).getBillingWarehouseId()!=7){
236
								continue;
237
							}
9404 vikram.rag 238
							in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
239
							ignoredItem.setItemId(thriftItem.getId());
240
							ignoredItem.setWarehouseId(entry.getKey());
241
							if(!thirdpartywarehouseids.contains(entry.getKey()) && !ignoreItems.contains(ignoredItem)){
242
								nlc = inventoryClient.getNlcForWarehouse(entry.getKey(),thriftItem.getId());
243
								maxnlc = catalogClient.getSnapdealItem(thriftItem.getId()).getMaxNlc();
244
								if(nlc !=0 && (maxnlc >= nlc)){
245
									reserve =  reserve + entry.getValue();
9413 vikram.rag 246
									System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
9404 vikram.rag 247
								}
9413 vikram.rag 248
								else if(maxnlc==0){
249
									reserve =  reserve + entry.getValue();
250
									System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
251
								}
252
 
9404 vikram.rag 253
							}
9413 vikram.rag 254
 
9404 vikram.rag 255
							else{
256
								//System.out.println("skipping inventory for warehouse id " + entry.getKey());
257
							}
258
						}
259
					}
260
					item= new SnapdealItemForInventory(thriftItem.getId(),available,reserve,heldForSource,thriftItem.getHoldInventory(),thriftItem.getDefaultInventory(),thriftItem.isRisky(),thriftItem.getItemStatus());
9413 vikram.rag 261
					System.out.println("itemId:" + thriftItem.getId() + "\tavailable: " + available + "\treserve" + reserve + "\theldForSource:" + heldForSource + "\twebsite_hold:" + thriftItem.getHoldInventory() + "\tdefault_inv:" +thriftItem.getDefaultInventory());
9404 vikram.rag 262
				}
263
				else{
264
					item = new SnapdealItemForInventory(thriftItem.getId(),0,0,0,thriftItem.getHoldInventory(),thriftItem.getDefaultInventory(),thriftItem.isRisky(),thriftItem.getItemStatus());
9413 vikram.rag 265
					System.out.println("itemId:" + thriftItem.getId() + "\tavailable: " + available + "\treserve" + reserve + "\theldForSource:" + heldForSource + "\twebsite_hold:" + thriftItem.getHoldInventory() + "\tdefault_inv:" +thriftItem.getDefaultInventory());
9404 vikram.rag 266
				}
267
 
268
				//System.out.println(" Item details are " + thriftItem.getId() +" " + available + " " + reserve + " " + thriftItem.getHoldInventory() + " "+ thriftItem.getDefaultInventory() + " " + thriftItem.isRisky());
269
				//System.out.println("+++++++++++++++++++++++");
270
				allItemsInventoryMap.put(thriftItem.getId(),item);
271
 
272
			}
273
 
274
		} catch (TException e) {
275
			// TODO Auto-generated catch block
276
			e.printStackTrace();
277
		}
278
 
279
	}
9456 vikram.rag 280
	private static void fetchinventoryhistoryfromsnapdeal() throws UnsupportedEncodingException, TException{
9404 vikram.rag 281
		HttpPost post = new HttpPost("http://seller.snapdeal.com/login_security_check?spring-security-redirect=http://seller.snapdeal.com/inventory&");
282
		HttpGet get; 
283
		BufferedReader rd= null;
284
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
285
		nameValuePairs.add(new BasicNameValuePair("j_username",
286
		"khushal.bhatia@saholic.com"));
287
		nameValuePairs.add(new BasicNameValuePair("j_password",
288
		"sonline"));
289
		post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
290
		HttpResponse response = null;
291
		try {
292
			response = client.execute(post);
293
		} catch (ClientProtocolException e) {
294
			// TODO Auto-generated catch block
295
			e.printStackTrace();
296
		} catch (IOException e) {
297
			// TODO Auto-generated catch block
298
			e.printStackTrace();
299
		}
300
		try {
301
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
302
		} catch (IllegalStateException e1) {
303
			// TODO Auto-generated catch block
304
			e1.printStackTrace();
305
		} catch (IOException e1) {
306
			// TODO Auto-generated catch block
307
			e1.printStackTrace();
308
		}
309
		String line = "";
310
		try {
311
			while ((line = rd.readLine()) != null) {
312
				System.out.println(line);
313
			}
314
		} catch (IOException e) {
315
			// TODO Auto-generated catch block
316
			e.printStackTrace();
317
		}
318
		int i = 1;
319
		Gson gson = new Gson();
9456 vikram.rag 320
		inventoryhistoryItemMap = new HashMap<String,InventoryHistory>();
9404 vikram.rag 321
		Client inventoryClient = null;
9456 vikram.rag 322
		boolean exitfetchinghistory = true;
323
		long lastUpdatedInventoryTime = 0;
9404 vikram.rag 324
		try {
325
			inventoryClient = new InventoryClient().getClient();
9456 vikram.rag 326
			lastUpdatedInventoryTime = inventoryClient.getLastUpdatedTimeForSnapdealInventory();
9404 vikram.rag 327
		} catch (TTransportException e) {
328
			// TODO Auto-generated catch block
329
			e.printStackTrace();
330
		}
9456 vikram.rag 331
 
332
		while(exitfetchinghistory){
333
			System.out.println("Fetching inventory history page " +i);
334
			get = new HttpGet("http://seller.snapdeal.com/inventory/search?gridType=history&_search=false&nd="+time+"&rows=30&page="+i+"&sidx=&sord=dsc");
335
			try {
336
				response = client.execute(get);
337
			} catch (ClientProtocolException e) {
338
				// TODO Auto-generated catch block
339
				e.printStackTrace();
340
			} catch (IOException e) {
341
				// TODO Auto-generated catch block
342
				e.printStackTrace();
343
			}
344
			try {
345
				rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
346
			} catch (IllegalStateException e) {
347
				// TODO Auto-generated catch block
348
				e.printStackTrace();
349
			} catch (IOException e) {
350
				// TODO Auto-generated catch block
351
				e.printStackTrace();
352
			}
353
			InventoryHistoryItems inventoryhistoryitems = (InventoryHistoryItems) gson.fromJson(rd, InventoryHistoryItems.class);
354
			if(inventoryhistoryitems.getRows().size()!=0){
355
				//System.out.println("Parsing page " + i);
356
				for(InventoryHistory inventoryhistory : inventoryhistoryitems.getRows()){
357
					if(inventoryhistory.getUpdateTime() > lastUpdatedInventoryTime){
358
						if(inventoryhistoryItemMap.containsKey(inventoryhistory.getSellerSku())){
359
							if(inventoryhistoryItemMap.get(inventoryhistory.getSellerSku()).getUpdateTime() < inventoryhistory.getUpdateTime()){  
360
								inventoryhistoryItemMap.put(inventoryhistory.getSellerSku(),inventoryhistory);
361
							}
362
						}
363
						else{
364
							inventoryhistoryItemMap.put(inventoryhistory.getSellerSku(),inventoryhistory);
365
						}
366
					}
367
					else{
368
						exitfetchinghistory = false;
369
					}
370
				}
371
			}
372
			else{
373
				exitfetchinghistory = false;
374
			}
375
			i++;
376
		}
377
	}
378
 
379
	private static void fetchinventoryfromsnapdeal() throws UnsupportedEncodingException{
380
		/*client = new DefaultHttpClient();
381
		HttpPost post = new HttpPost("http://seller.snapdeal.com/login_security_check?spring-security-redirect=http://seller.snapdeal.com/inventory&");
382
		BufferedReader rd= null;
383
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
384
		nameValuePairs.add(new BasicNameValuePair("j_username",
385
		"khushal.bhatia@saholic.com"));
386
		nameValuePairs.add(new BasicNameValuePair("j_password",
387
		"sonline"));
388
		post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
389
		HttpResponse response = null;
9404 vikram.rag 390
		try {
9456 vikram.rag 391
			response = client.execute(post);
392
		} catch (ClientProtocolException e) {
9404 vikram.rag 393
			// TODO Auto-generated catch block
394
			e.printStackTrace();
9456 vikram.rag 395
		} catch (IOException e) {
396
			// TODO Auto-generated catch block
397
			e.printStackTrace();
9404 vikram.rag 398
		}
9456 vikram.rag 399
		try {
400
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
401
		} catch (IllegalStateException e1) {
402
			// TODO Auto-generated catch block
403
			e1.printStackTrace();
404
		} catch (IOException e1) {
405
			// TODO Auto-generated catch block
406
			e1.printStackTrace();
407
		}
408
		String line = "";
409
		try {
410
			while ((line = rd.readLine()) != null) {
411
				System.out.println(line);
412
			}
413
		} catch (IOException e) {
414
			// TODO Auto-generated catch block
415
			e.printStackTrace();
416
		}*/
417
		int i = 1;
418
		int items=0;
419
		Gson gson = new Gson();
420
		in.shop2020.model.v1.order.TransactionService.Client transactionClient = null;
9404 vikram.rag 421
		inventoryItemList = new ArrayList<Inventory>();
9456 vikram.rag 422
		HttpGet get;
423
		HttpResponse response = null;
424
		BufferedReader rd= null;
9404 vikram.rag 425
		while(true){
9456 vikram.rag 426
			System.out.println("Fetching current inventory page " +i);
9404 vikram.rag 427
			get = new HttpGet("http://seller.snapdeal.com/inventory/search?gridType=normal&_search=false&nd="+time+"&rows="+30+"&page="+i+"&sidx=&sord=asc");
428
			try {
429
				response = client.execute(get);
430
			} catch (ClientProtocolException e) {
431
				// TODO Auto-generated catch block
432
				e.printStackTrace();
433
			} catch (IOException e) {
434
				// TODO Auto-generated catch block
435
				e.printStackTrace();
436
			}
437
			try {
438
				rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
439
			} catch (IllegalStateException e) {
440
				// TODO Auto-generated catch block
441
				e.printStackTrace();
442
			} catch (IOException e) {
443
				// TODO Auto-generated catch block
444
				e.printStackTrace();
445
			}
446
			InventoryItems inventoryitems = (InventoryItems) gson.fromJson(rd, InventoryItems.class);
447
			if(inventoryitems.getRows().size()!=0){
448
				for(Inventory inventory : inventoryitems.getRows()){
9456 vikram.rag 449
					inventoryItemList.add(inventory);
450
					items++;
451
				}
452
			}
453
			else{
454
				System.out.println("Fetched  " + items);
455
				break;
456
			}
457
			i++;
458
		}
459
		in.shop2020.model.v1.catalog.CatalogService.Client catalogServiceClient = null;
460
		SnapdealItem snapdealitem = null;
461
		long sku = 0;
462
		long created_orders;
463
		long pending_orders;
464
		try {
465
			transactionClient = new TransactionClient("support_transaction_service_server_host","transaction_service_server_port").getClient();
466
		} catch (TTransportException e1) {
467
			// TODO Auto-generated catch block
468
			e1.printStackTrace();
469
		}
470
		try {
471
			catalogServiceClient = new CatalogClient().getClient();
472
		} catch (TTransportException e1) {
473
			// TODO Auto-generated catch block
474
			e1.printStackTrace();
475
		}
476
		List<String> notMappedItems = new ArrayList<String>();
477
		for(Inventory inventory:inventoryItemList){
478
			created_orders = 0;
479
			pending_orders = 0;
480
			if(inventoryhistoryItemMap.get(inventory.getSellerSku())!=null && inventory.getAvailableInventory()!=Long.parseLong(inventoryhistoryItemMap.get(inventory.getSellerSku()).getNewValue())){
481
					try {
482
						try{
483
							sku = Long.parseLong(inventory.getSellerSku());
9413 vikram.rag 484
						}
9456 vikram.rag 485
						catch(NumberFormatException nfe){
9404 vikram.rag 486
							try {
9456 vikram.rag 487
								snapdealitem = catalogServiceClient.getSnapdealItembySkuAtSnapdeal(inventory.getSellerSku());
488
								if(snapdealitem.getItem_id()!=0){
489
									sku = snapdealitem.getItem_id();
490
									skuitemidMap.put(inventory.getSellerSku(),sku);
9413 vikram.rag 491
								}
9456 vikram.rag 492
								else{
493
									notMappedItems.add("inventory.getSellerSku() "+inventory.getProductName());
494
									System.out.println();
495
								}
496
							} catch (TTransportException e) {
9404 vikram.rag 497
								// TODO Auto-generated catch block
498
								e.printStackTrace();
499
							}
500
						}
9456 vikram.rag 501
						if(sku!=0){
502
							System.out.println("Fetching created orders for " + sku);
503
							created_orders = transactionClient.getOrdersCreatedAfterTimestampForSource(inventoryhistoryItemMap.get(inventory.getSellerSku()).getUpdateTime(), 7, sku);
9404 vikram.rag 504
						}
505
						else{
9456 vikram.rag 506
							continue;
9404 vikram.rag 507
						}
9456 vikram.rag 508
					} catch (NumberFormatException e) {
509
						// TODO Auto-generated catch block
510
						e.printStackTrace();
511
					} catch (TException e) {
512
						// TODO Auto-generated catch block
513
						e.printStackTrace();
9404 vikram.rag 514
					}
9456 vikram.rag 515
					pending_orders = (inventory.getAvailableInventory() - Long.parseLong(inventoryhistoryItemMap.get(inventory.getSellerSku()).getNewValue()) - created_orders);
9404 vikram.rag 516
			}
9456 vikram.rag 517
			itemIdpendingOrdersMap.put(sku, pending_orders);
9404 vikram.rag 518
		}
519
	}
520
 
521
	@SuppressWarnings("deprecation")
522
	public static void main(String[] args) throws NumberFormatException, TException, ClientProtocolException, IOException {
9456 vikram.rag 523
		fetchinventoryhistoryfromsnapdeal();
9404 vikram.rag 524
		fetchinventoryfromsnapdeal();
525
		calculateinventory();
526
		in.shop2020.model.v1.catalog.CatalogService.Client catalogServiceClient = null;
527
		SnapdealItem snapdealitem = null;
528
		try {
529
			catalogServiceClient = new CatalogClient().getClient();
530
		} catch (TTransportException e) {
531
			// TODO Auto-generated catch block
532
			e.printStackTrace();
533
		}
534
		Client inventoryClient = new InventoryClient().getClient();
9420 vikram.rag 535
		FileInputStream fis = new FileInputStream("/root/code/trunk/SnapDealFeeds/SellerInventory.xls");
9413 vikram.rag 536
		Workbook hwb = new HSSFWorkbook(fis);
537
		Sheet sheet = hwb.getSheetAt(0);
538
		Row row;
9404 vikram.rag 539
		int iterator=1;
9456 vikram.rag 540
		long availability,reserve,defaultinventory,holdinventory,actualavailability,heldorders=0,sent_inventory;
9404 vikram.rag 541
		SnapdealItemForInventory  inventoryItem;
9456 vikram.rag 542
		StringBuffer outOfStockItems =new StringBuffer();
543
		StringBuffer backInStockItems =new StringBuffer();
544
		long sku = 0;
9404 vikram.rag 545
		for(Inventory inventory:inventoryItemList){
9456 vikram.rag 546
			//System.out.println(inventory.getSupc()+" "+inventory.getSellerSku()+ " " +inventory.getProductName()+" "+inventory.getAvailableInventory()+" "+inventory.isLive()+" "+itemIdpendingOrdersMap.get(Long.parseLong(inventory.getSellerSku())));
9404 vikram.rag 547
			try{
9456 vikram.rag 548
				try{
549
					sku = Long.parseLong(inventory.getSellerSku());
550
				}
551
				catch(NumberFormatException nfe){
552
					if(skuitemidMap.containsKey(inventory.getSellerSku())){
553
						sku = skuitemidMap.get(inventory.getSellerSku()); 
554
					}
555
					else{
556
						continue;
557
					}
558
				}
559
				snapdealitem = catalogServiceClient.getSnapdealItem(sku);
9404 vikram.rag 560
			}
561
			catch(TTransportException e){
562
				catalogServiceClient = new CatalogClient().getClient();
9456 vikram.rag 563
				snapdealitem = catalogServiceClient.getSnapdealItem(sku);
9404 vikram.rag 564
			}
565
			if(snapdealitem.getItem_id()!=0 && snapdealitem.isIsListedOnSnapdeal() && !snapdealitem.isSuppressInventoryFeed()){
566
				inventoryItem = allItemsInventoryMap.get(Long.parseLong(inventory.getSellerSku()));
567
				if(inventoryItem==null){
568
					continue;
569
				}
570
				availability = inventoryItem.getAvailability(); 
571
				reserve = inventoryItem.getReserve();
572
				defaultinventory = inventoryItem.getDefaultInventory();
573
				holdinventory = inventoryItem.getHoldInventory();
574
				actualavailability = availability - reserve;
9456 vikram.rag 575
				if(itemIdpendingOrdersMap.get(Long.parseLong(inventory.getSellerSku()))!=null){
576
					heldorders = itemIdpendingOrdersMap.get(Long.parseLong(inventory.getSellerSku()));
577
				}
9413 vikram.rag 578
				System.out.println("itemId:" + inventoryItem.getId() + "\tavailable: " + inventoryItem.getAvailability() + "\treserve" + inventoryItem.getReserve() + "\theldForSource:" + inventoryItem.getHeldForSource() + "\twebsite_hold:" + inventoryItem.getHoldInventory() + "\tdefault_inv:" +inventoryItem.getDefaultInventory() + "\theldorders" + heldorders);
9404 vikram.rag 579
				row =   sheet.getRow((short) iterator);
580
				long inventoryForRiskyItem = actualavailability - holdinventory;
581
				if(inventoryItem.getRisky()){
582
					if(inventoryForRiskyItem > 0){
9413 vikram.rag 583
						if((Math.round(inventoryForRiskyItem*2/3) - heldorders + inventoryItem.getHeldForSource()) > 0){
9415 vikram.rag 584
							sent_inventory = Math.round(inventoryForRiskyItem*2/3) - heldorders + inventoryItem.getHeldForSource();
9404 vikram.rag 585
						}
586
						else{
9415 vikram.rag 587
							sent_inventory = 0;
9404 vikram.rag 588
						}
589
					}
590
					else{
9415 vikram.rag 591
						sent_inventory = 0;
9404 vikram.rag 592
					}
593
				}
594
				else{
595
					if(actualavailability > defaultinventory){
9413 vikram.rag 596
						if((Math.round(actualavailability*2/3) +  inventoryItem.getHeldForSource()) > heldorders){
597
							System.out.println("actual" + actualavailability + "\t2/3actual" + Math.round(actualavailability*2/3) +  "\theld for source" + inventoryItem.getHeldForSource() + "\theldorders" +  heldorders + "\tFinal Value is : " + (Math.round(actualavailability*2/3) +  inventoryItem.getHeldForSource() - heldorders));
9415 vikram.rag 598
							sent_inventory = Math.round(actualavailability*2/3) +  inventoryItem.getHeldForSource() - heldorders;
9404 vikram.rag 599
						}
600
						else{
9415 vikram.rag 601
							sent_inventory = 0;
9404 vikram.rag 602
						}
603
					}
604
					else{
605
						if(defaultinventory > heldorders){
9415 vikram.rag 606
							sent_inventory = defaultinventory - heldorders;
9404 vikram.rag 607
						}
608
						else{
9415 vikram.rag 609
							sent_inventory = 0;
9404 vikram.rag 610
						}
611
					}
612
				}
9456 vikram.rag 613
				if(inventory.getAvailableInventory()!= sent_inventory){
614
					row.getCell((short) 0).setCellValue(inventory.getSupc());
615
					row.getCell((short) 1).setCellValue(inventory.getSellerSku());
616
					row.getCell((short) 2).setCellValue(inventory.getProductName());
617
					row.getCell((short) 3).setCellValue(inventory.getAvailableInventory());
618
					row.getCell((short) 4).setCellValue(sent_inventory);
619
					if(inventory.getAvailableInventory()!=0 && sent_inventory==0){
620
						outOfStockItems.append(sku+ " " +inventory.getProductName()+" "+inventory.getAvailableInventory()+" "+sent_inventory);
621
					}
622
					if(inventory.getAvailableInventory()==0 && sent_inventory!=0){
623
						backInStockItems.append(sku+ " " +inventory.getProductName()+" "+inventory.getAvailableInventory()+" "+sent_inventory);
624
					}
625
					if(inventory.isLive()){
626
						row.getCell((short) 5).setCellValue("Yes");
627
					}
628
					else{
629
						row.getCell((short) 5).setCellValue("No");
630
					}
631
					SnapdealInventoryItem snapdealinventoryitem = new SnapdealInventoryItem();
632
					snapdealinventoryitem.setAvailability(sent_inventory);
633
					snapdealinventoryitem.setItem_id(Long.parseLong(inventory.getSellerSku()));
634
					System.out.println("Updating inventory snapshot " + inventory.getSellerSku());
635
					snapdealinventoryitem.setLastUpdatedOnSnapdeal(time);
636
					try{
637
						inventoryClient.addOrUpdateSnapdealInventoryForItem(snapdealinventoryitem);
638
					}
639
					catch(TTransportException e){
640
						inventoryClient = new InventoryClient().getClient();
641
						inventoryClient.addOrUpdateSnapdealInventoryForItem(snapdealinventoryitem);
642
					}
643
					iterator++;
9404 vikram.rag 644
				}
645
			}
646
			else{
647
				continue;
648
			}
649
		}
650
		FileOutputStream fileOut = null;
651
		fis.close();
652
		try {
9456 vikram.rag 653
			System.out.println("Before writing file ");
9404 vikram.rag 654
			fileOut = new FileOutputStream(SNAPDEAL_INVENTORY_SHEET);
655
		} catch (FileNotFoundException e) {
656
			// TODO Auto-generated catch block
657
			e.printStackTrace();
658
		}
659
		try {
9456 vikram.rag 660
 
9404 vikram.rag 661
			hwb.write(fileOut);
662
		} catch (IOException e) {
663
			// TODO Auto-generated catch block
664
			e.printStackTrace();
665
		}
9413 vikram.rag 666
		/*		HttpClient client = new DefaultHttpClient();
9404 vikram.rag 667
		HttpPost post = new HttpPost("http://seller.snapdeal.com/login_security_check?spring-security-redirect=http://seller.snapdeal.com/inventory&");
668
		BufferedReader rd= null;
9405 vikram.rag 669
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
9404 vikram.rag 670
		nameValuePairs.add(new BasicNameValuePair("j_username",
671
		"khushal.bhatia@saholic.com"));
672
		nameValuePairs.add(new BasicNameValuePair("j_password",
673
		"sonline"));
9405 vikram.rag 674
		post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
9404 vikram.rag 675
		HttpResponse response = null;
676
		String line;
9405 vikram.rag 677
		try {
9404 vikram.rag 678
			response = client.execute(post);
679
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
680
			while ((line = rd.readLine()) != null) {
681
				System.out.println(line);
682
			}
683
 
684
		} catch (ClientProtocolException e) {
685
			// TODO Auto-generated catch block
686
			e.printStackTrace();
687
		} catch (IOException e) {
688
			// TODO Auto-generated catch block
689
			e.printStackTrace();
690
		}
9405 vikram.rag 691
		post = new HttpPost("http://seller.snapdeal.com/inventory/upload");
9404 vikram.rag 692
		File file = new File("/home/vikram/snapdeal-inventory-1389608325312.xls");
693
		MultipartEntity mpEntity = new MultipartEntity();
694
	    ContentBody cbFile = new FileBody(file,"application/vnd.ms-excel");
695
	    mpEntity.addPart("file", cbFile);
696
	    post.setEntity(mpEntity);
697
		response = client.execute(post);
698
		try {
699
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
700
		} catch (IllegalStateException e1) {
701
			// TODO Auto-generated catch block
702
			e1.printStackTrace();
703
		} catch (IOException e1) {
704
			// TODO Auto-generated catch block
705
			e1.printStackTrace();
706
		}
707
		line = "";
708
		try {
709
			while ((line = rd.readLine()) != null) {
710
				System.out.println(line);
711
			}
712
		} catch (IOException e) {
713
			// TODO Auto-generated catch block
714
			e.printStackTrace();
715
		}
9413 vikram.rag 716
		 */
9404 vikram.rag 717
	}
718
 
719
}