Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
11874 manish.sha 1
package in.shop2020;
2
 
3
import in.shop2020.model.v1.catalog.FlipkartItem;
4
import in.shop2020.model.v1.catalog.Item;
5
import in.shop2020.model.v1.catalog.SnapdealItemDetails;
6
import in.shop2020.model.v1.inventory.InventoryType;
7
import in.shop2020.model.v1.inventory.ItemInventory;
8
import in.shop2020.model.v1.inventory.Vendor;
9
import in.shop2020.model.v1.inventory.Warehouse;
10
import in.shop2020.model.v1.inventory.WarehouseLocation;
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 in.shop2020.utils.GmailUtils;
16
//import inventory.Inventory;
17
import inventory.FlipkartInventoryItem;
18
import inventory.NlcLeftOutInventory;
19
//import inventory.SnapdealItemForInventory;
20
 
21
import java.io.BufferedReader;
22
import java.io.BufferedWriter;
23
import java.io.File;
24
import java.io.FileWriter;
25
import java.io.IOException;
26
import java.io.InputStreamReader;
27
import java.util.ArrayList;
28
import java.util.HashMap;
11882 manish.sha 29
import java.util.HashSet;
30
import java.util.Iterator;
11874 manish.sha 31
import java.util.List;
32
import java.util.Map;
11882 manish.sha 33
import java.util.Set;
11874 manish.sha 34
 
35
import javax.mail.MessagingException;
36
 
37
import org.apache.commons.lang.StringUtils;
38
import org.apache.http.Header;
39
import org.apache.http.HttpResponse;
40
import org.apache.http.NameValuePair;
41
import org.apache.http.client.ClientProtocolException;
42
import org.apache.http.client.HttpClient;
43
import org.apache.http.client.entity.UrlEncodedFormEntity;
44
import org.apache.http.client.methods.HttpGet;
45
import org.apache.http.client.methods.HttpPost;
46
import org.apache.http.impl.client.DefaultHttpClient;
47
import org.apache.http.message.BasicNameValuePair;
48
import org.apache.http.params.HttpParams;
49
import org.apache.thrift.TException;
50
import org.apache.thrift.transport.TTransportException;
51
 
52
 
53
public class NlcLeftOutInventoryDetailScript{
54
 
55
	//private static ArrayList<Inventory> currentInventoryItemList;
56
	private static List<SnapdealItemDetails> snapdealItems;
57
	private static List<FlipkartItem> flipkartItems;
58
	private static List<FlipkartItem> flipkartInventoryItems = new ArrayList<FlipkartItem>();
59
	static Map<Long,SnapdealItemDetails> snapdealItemMap = new HashMap<Long,SnapdealItemDetails>();
60
	static Map<Long,FlipkartItem> flipkartItemMap = new HashMap<Long,FlipkartItem>();
61
	//private static Map<Long, SnapdealItemForInventory> allItemsInventoryMap = new HashMap<Long, SnapdealItemForInventory>();
62
	private static Map<Long, String> vendorDataMap = new HashMap<Long, String>();
63
	//static java.text.SimpleDateFormat sdf;
64
	static String emailFromAddress;
65
	static String password;
66
	static GmailUtils mailer;
67
	static String sendTo[];
68
	private static List<NlcLeftOutInventory> nlcLeftOutInventoryItemsSD = new ArrayList<NlcLeftOutInventory>();
69
	private static List<NlcLeftOutInventory> nlcLeftOutInventoryItemsFK = new ArrayList<NlcLeftOutInventory>();
70
 
71
	static{
72
		emailFromAddress = "build@shop2020.in";
73
		password = "cafe@nes";
74
		mailer = new GmailUtils();
75
		sendTo = new String[]{"vikram.raghav@shop2020.in", "manish.sharma@shop2020.in"};
76
		/*sendTo = new String[]{ "sandeep.sachdeva@shop2020.in", "vikram.raghav@shop2020.in", "rajneesh.arora@shop2020.in",
77
				"khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","chaitnaya.vats@saholic.com",
78
				"yukti.jain@shop2020.in","manisha.sharma@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in"};*/
79
		CatalogClient catalogServiceClient = null;
80
		try {
81
			catalogServiceClient = new CatalogClient();
82
		} catch (TTransportException e1) {
83
			// TODO Auto-generated catch block
84
			e1.printStackTrace();
85
		}
86
		in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = catalogServiceClient.getClient();
87
 
88
		try {
89
			snapdealItems = catalogClient.getAllSnapdealItems();
90
			flipkartItems = catalogClient.getAllFlipkartItems();
91
		} catch (TException e) {
92
			// TODO Auto-generated catch block
93
			e.printStackTrace();
94
		}
95
		for(FlipkartItem flipkartItem:flipkartItems){
96
			if(flipkartItem.isIsListedOnFlipkart() && !flipkartItem.isSuppressInventoryFeed())
97
				flipkartInventoryItems.add(flipkartItem);
98
		}
99
 
100
		for(FlipkartItem flipkartItem:flipkartItems){
101
			if(flipkartItem.isIsListedOnFlipkart() && !flipkartItem.isSuppressInventoryFeed()){
102
				flipkartItemMap.put(flipkartItem.getItem_id(), flipkartItem);
103
			}
104
		}
105
 
106
		for(SnapdealItemDetails snapdealItem:snapdealItems){
107
			snapdealItemMap.put(snapdealItem.getItem_id(),snapdealItem);
108
		}
109
 
110
		if(flipkartItemMap.size()==0){
111
			String text = "";
112
			try {
113
				mailer.sendSSLMessage(sendTo,"No items to check not sent Inventory Data ", emailFromAddress, password, text);
114
				System.exit(0);
115
			} catch (MessagingException e) {
116
				e.printStackTrace();
117
			}
118
		}
119
 
120
		if(snapdealItemMap.size()==0){
121
			String text = "";
122
			try {
123
				mailer.sendSSLMessage(sendTo,"No items to check not sent Inventory Data ", emailFromAddress, password, text);
124
				System.exit(0);
125
			} catch (MessagingException e) {
126
				e.printStackTrace();
127
			}
128
		}
129
	}
130
 
131
	private static void calculateinventory(){
132
		CatalogClient catalogServiceClient = null;
133
		Map<Long,ItemInventory> availability= new HashMap<Long,ItemInventory>();
134
		try {
135
			catalogServiceClient = new CatalogClient();
136
		} catch (TTransportException e) {
137
			e.printStackTrace();
138
		}
139
		in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = catalogServiceClient.getClient();
140
		try {
141
			List<Item> aliveItems = catalogClient.getAllAliveItems();
142
			Map<Long,Item> aliveItemsMap = new HashMap<Long,Item>(); 
143
			for(in.shop2020.model.v1.catalog.Item thriftItem:aliveItems){
144
				aliveItemsMap.put(thriftItem.getId(), thriftItem);
145
 
146
			}
147
			InventoryClient inventoryServiceClient = new InventoryClient();
148
			try {
149
				inventoryServiceClient = new InventoryClient();
150
			} catch (TTransportException e) {
151
				e.printStackTrace();
152
			}
153
			in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
154
			List<Vendor> allVendors = inventoryClient.getAllVendors();
155
			for(Vendor v: allVendors){
156
				vendorDataMap.put(v.getId(), v.getName());
157
			}
158
			List<in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems> ignoreItems = inventoryClient.getIgnoredWarehouseidsAndItemids();
11882 manish.sha 159
 
11874 manish.sha 160
			availability = inventoryClient.getInventorySnapshot(0);
161
			List<Warehouse> allwarehouses = inventoryClient.getWarehouses(null,null,0,0,0);
162
			Map<Long,Warehouse> allWarehousesMap = new HashMap<Long,Warehouse>();
163
			for(Warehouse warehouse:allwarehouses){
164
				allWarehousesMap.put(warehouse.getId(),warehouse);
165
			}
166
			List<Warehouse> warehouses = inventoryClient.getWarehouses(WarehouseType.OURS_THIRDPARTY,null,0,0,0);
167
			List<Warehouse> ours_warehouses = inventoryClient.getWarehouses(WarehouseType.OURS, InventoryType.GOOD, 0, 0, 0);
168
			List<Long> thirdpartywarehouseids = new ArrayList<Long>();
169
			List<Long> oursGoodWarehouse = new ArrayList<Long>();
170
			for(Warehouse warehouse:warehouses){
171
				thirdpartywarehouseids.add(warehouse.getId());
172
			}
173
			for (Warehouse warehouse:ours_warehouses){
174
				oursGoodWarehouse.add(warehouse.getId());
175
			}
176
			/*long available=0;
177
			long reserve=0;
178
			long total_warehouse_held=0;
179
			long heldForSource=0;
180
			long total_held=0;*/
181
			double nlc=0;
182
			double maxnlcSD=0;
183
			double maxnlcFK=0;
184
 
11882 manish.sha 185
			Map<String, Set<NlcLeftOutInventory>> snapdealLeftInvDataMap = new HashMap<String, Set<NlcLeftOutInventory>>();
186
			Map<String, Set<NlcLeftOutInventory>> flipkartLeftInvDataMap = new HashMap<String, Set<NlcLeftOutInventory>>();
11874 manish.sha 187
 
188
 
189
			for(Item thriftItem : aliveItems){
190
				SnapdealItemDetails snapdealItem = null;
191
				FlipkartItem flipkartItem = null;
192
				NlcLeftOutInventory nlcLOISD;
193
				NlcLeftOutInventory nlcLOIFK;
194
				maxnlcSD=0;
195
				maxnlcFK=0;
11882 manish.sha 196
				Set<NlcLeftOutInventory> nlcLoiSetSD = new HashSet<NlcLeftOutInventory>();
197
				Set<NlcLeftOutInventory> nlcLoiSetFK = new HashSet<NlcLeftOutInventory>();
11874 manish.sha 198
				if(snapdealItemMap.containsKey(thriftItem.getId())){
199
					snapdealItem = snapdealItemMap.get(thriftItem.getId());
200
					maxnlcSD = snapdealItem.getMaxNlc();
201
				}
202
				if(flipkartItemMap.containsKey(thriftItem.getId())){
203
					flipkartItem = flipkartItemMap.get(thriftItem.getId());
204
					maxnlcFK = flipkartItem.getMaxNlc();
205
				}
206
				nlc=0;
207
				List<Warehouse> vendor_warehouses=null; 
208
				if(availability.get(thriftItem.getId())!=null){ 
209
					ItemInventory iteminventory = availability.get(thriftItem.getId());
210
					Map<Long,Long> itemavailability = new HashMap<Long,Long>();
211
					itemavailability = iteminventory.getAvailability();
212
					if (thriftItem.isIsWarehousePreferenceSticky() && thriftItem.getPreferredVendor()!=0){
213
						//System.out.println("Item id "+thriftItem.getId()+".Found prefered vendor and warehouse is marked sticky (Calculating availability)"+thriftItem.getPreferredVendor());						
214
						vendor_warehouses = inventoryClient.getWarehouses(WarehouseType.THIRD_PARTY,InventoryType.GOOD , thriftItem.getPreferredVendor(), 0, 0);
215
						vendor_warehouses.addAll(ours_warehouses);
216
						for (Warehouse warehouse:vendor_warehouses){
217
							if(warehouse.getBillingWarehouseId()!=7){
218
								if(warehouse.getLogisticsLocation().equals(WarehouseLocation.Delhi) && warehouse.getWarehouseType().equals(WarehouseType.THIRD_PARTY) && warehouse.getVendor().getId()!=1){
219
 
220
								}
221
								else{
222
									continue;
223
								}
224
							}
225
							in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
226
							ignoredItem.setItemId(thriftItem.getId());
227
							ignoredItem.setWarehouseId(warehouse.getId());
228
							if (itemavailability.get(warehouse.getId())!=null && !thirdpartywarehouseids.contains(warehouse.getId()) && !ignoreItems.contains(ignoredItem)){
229
								try{
230
									nlc = inventoryClient.getNlcForWarehouse(warehouse.getId(),thriftItem.getId());
231
 
232
								}
233
								catch(TTransportException e){
234
									inventoryClient = inventoryServiceClient.getClient(); 
235
									nlc = inventoryClient.getNlcForWarehouse(warehouse.getId(),thriftItem.getId());
236
								}
237
 
238
								//maxnlc = catalogClient.getSnapdealItem(thriftItem.getId()).getMaxNlc();
239
 
240
								if(snapdealItem!=null){
241
									if(nlc !=0 && (maxnlcSD >= nlc)){
242
										/*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 0);
243
										heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 7);
244
										available = available + itemavailability.get(warehouse.getId());
245
										total_held = total_held + total_warehouse_held;*/ 
11876 manish.sha 246
										//continue;
11874 manish.sha 247
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
248
									}
249
									else if(maxnlcSD==0){
250
										/*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 0);
251
										heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 7);
252
										available = available + itemavailability.get(warehouse.getId());
253
										total_held = total_held + total_warehouse_held;*/
11876 manish.sha 254
										//continue;
11874 manish.sha 255
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
256
									}
257
									else{
11875 manish.sha 258
										long available = itemavailability.get(warehouse.getId());
259
										if(available>0){
260
											nlcLOISD= new NlcLeftOutInventory();
261
											nlcLOISD.setItemId(snapdealItem.getItem_id());
262
											nlcLOISD.setProductName(snapdealItem.getBrand()+" " +snapdealItem.getModel_name() + " "+ snapdealItem.getModel_number() + " " +  snapdealItem.getColor());
263
											nlcLOISD.setOurNlc(maxnlcSD);
264
											nlcLOISD.setSkuAtSource(snapdealItem.getSkuAtSnapdeal());
265
											nlcLOISD.setVendorName(warehouse.getVendor().getName());
266
											nlcLOISD.setVendorAvailability(available);
267
											nlcLOISD.setVendorNlc(nlc);
11882 manish.sha 268
											nlcLOISD.setVendorWarehouseId(warehouse.getId());
269
											nlcLoiSetSD.add(nlcLOISD);
270
 
11875 manish.sha 271
										}
11874 manish.sha 272
									}
273
								}
274
 
275
								if(flipkartItem!=null){
276
									if(nlc !=0 && (maxnlcFK >= nlc)){
277
										/*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 0);
278
										heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 8);
279
										available = available + itemavailability.get(warehouse.getId());
280
										total_held = total_held + total_warehouse_held; */
11876 manish.sha 281
										//continue;
11874 manish.sha 282
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
283
									}
284
									else if(maxnlcFK==0){
285
										/*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 0);
286
										heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 8);
287
										available = available + itemavailability.get(warehouse.getId());
288
										total_held = total_held + total_warehouse_held;*/
11876 manish.sha 289
										//continue;
11874 manish.sha 290
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
291
									}
292
									else{
11875 manish.sha 293
										long available = itemavailability.get(warehouse.getId());
294
										if(available>0){
295
											nlcLOIFK = new NlcLeftOutInventory();
296
											nlcLOIFK.setItemId(flipkartItem.getItem_id());
297
											nlcLOIFK.setProductName(thriftItem.getBrand()+" " +thriftItem.getModelName() + " "+ thriftItem.getModelNumber() + " " +  thriftItem.getColor());
298
											nlcLOIFK.setOurNlc(maxnlcFK);
299
											nlcLOIFK.setSkuAtSource(flipkartItem.getSkuAtFlipkart());
300
											nlcLOIFK.setVendorName(warehouse.getVendor().getName());
301
											nlcLOIFK.setVendorAvailability(available);
302
											nlcLOIFK.setVendorNlc(nlc);
11882 manish.sha 303
											nlcLOIFK.setVendorWarehouseId(warehouse.getId());
304
											nlcLoiSetFK.add(nlcLOIFK);
11875 manish.sha 305
										}
11874 manish.sha 306
									}
307
								}
308
								//System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + warehouse.getId() );
309
 
310
							}
311
						}
11884 manish.sha 312
						if(nlcLoiSetSD!=null && nlcLoiSetSD.size()>0){
313
							snapdealLeftInvDataMap.put(snapdealItem.getSkuAtSnapdeal(), nlcLoiSetSD);
314
						}
315
						if(nlcLoiSetFK!=null && nlcLoiSetFK.size()>0){
316
							flipkartLeftInvDataMap.put(flipkartItem.getSkuAtFlipkart(), nlcLoiSetFK);
317
						}
11874 manish.sha 318
					}
319
					else{
320
						for (Map.Entry<Long,Long> entry :  itemavailability.entrySet()) {
321
							if(allWarehousesMap.get(entry.getKey()).getBillingWarehouseId()!=7){
322
								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){
323
 
324
								}
325
								else{
326
									continue;
327
								}
328
							}
329
							in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
330
							ignoredItem.setItemId(thriftItem.getId());
331
							ignoredItem.setWarehouseId(entry.getKey());
332
							if(!thirdpartywarehouseids.contains(entry.getKey()) && !ignoreItems.contains(ignoredItem)){
333
 
334
								nlc = inventoryClient.getNlcForWarehouse(entry.getKey(),thriftItem.getId());
335
 
336
								//System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + entry.getKey() );
337
								if(snapdealItem!=null){
338
									if(nlc !=0 && (maxnlcSD >= nlc)){
339
										/*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 0);
340
										heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 7);
341
										available =  available + entry.getValue();
342
										total_held = total_held + total_warehouse_held;*/
11876 manish.sha 343
										//continue;
11874 manish.sha 344
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
345
									}
346
									else if(maxnlcSD==0){
347
										/*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 0);
348
										heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 7);
349
										available = available + itemavailability.get(entry.getKey());
350
										total_held = total_held + total_warehouse_held;*/
351
 
11876 manish.sha 352
										//continue;
11874 manish.sha 353
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
354
									}
355
									else{
11875 manish.sha 356
										long available = itemavailability.get(entry.getKey());
357
										if(available>0){
358
											nlcLOISD = new NlcLeftOutInventory();
359
											nlcLOISD.setItemId(snapdealItem.getItem_id());
360
											nlcLOISD.setProductName(snapdealItem.getBrand()+" " +snapdealItem.getModel_name() + " "+ snapdealItem.getModel_number() + " " +  snapdealItem.getColor());
361
											nlcLOISD.setOurNlc(maxnlcSD);
362
											nlcLOISD.setSkuAtSource(snapdealItem.getSkuAtSnapdeal());
11882 manish.sha 363
											nlcLOISD.setVendorName(vendorDataMap.get(allWarehousesMap.get(entry.getKey()).getVendor().getId()));
11875 manish.sha 364
											nlcLOISD.setVendorAvailability(available);
365
											nlcLOISD.setVendorNlc(nlc);
11882 manish.sha 366
											nlcLoiSetSD.add(nlcLOISD);
11875 manish.sha 367
										}
11874 manish.sha 368
									}
369
								}
370
 
371
								if(flipkartItem!=null){
372
									if(nlc !=0 && (maxnlcFK >= nlc)){
373
										/*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 0);
374
										heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 8);
375
										available =  available + entry.getValue();
376
										total_held = total_held + total_warehouse_held;*/
11876 manish.sha 377
										//continue;
11874 manish.sha 378
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
379
									}
380
									else if(maxnlcFK==0){
381
										/*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 0);
382
										heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 8);
383
										available = available + itemavailability.get(entry.getKey());
384
										total_held = total_held + total_warehouse_held;*/
11876 manish.sha 385
										//continue;
11874 manish.sha 386
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
387
									}
388
									else{
11875 manish.sha 389
										long available = itemavailability.get(entry.getKey());
390
										if(available>0){
391
											nlcLOIFK = new NlcLeftOutInventory();
392
											nlcLOIFK.setItemId(flipkartItem.getItem_id());
393
											nlcLOIFK.setProductName(thriftItem.getBrand()+" " +thriftItem.getModelName() + " "+ thriftItem.getModelNumber() + " " +  thriftItem.getColor());
394
											nlcLOIFK.setOurNlc(maxnlcFK);
395
											nlcLOIFK.setSkuAtSource(flipkartItem.getSkuAtFlipkart());
11882 manish.sha 396
											nlcLOIFK.setVendorName(vendorDataMap.get(allWarehousesMap.get(entry.getKey()).getVendor().getId()));
11875 manish.sha 397
											nlcLOIFK.setVendorAvailability(available);
398
											nlcLOIFK.setVendorNlc(nlc);
11882 manish.sha 399
											nlcLoiSetFK.add(nlcLOIFK);
11875 manish.sha 400
										}
11874 manish.sha 401
									}
402
								}
403
 
404
 
405
							}
406
						}
11884 manish.sha 407
						if(nlcLoiSetSD!=null && nlcLoiSetSD.size()>0){
408
							snapdealLeftInvDataMap.put(snapdealItem.getSkuAtSnapdeal(), nlcLoiSetSD);
409
						}
410
						if(nlcLoiSetFK!=null && nlcLoiSetFK.size()>0){
411
							flipkartLeftInvDataMap.put(flipkartItem.getSkuAtFlipkart(), nlcLoiSetFK);
412
						}
11874 manish.sha 413
					}
414
 
415
					Map<Long,Long> itemreserve = new HashMap<Long,Long>();
416
					itemreserve = iteminventory.getReserved();
417
					if (thriftItem.isIsWarehousePreferenceSticky() && thriftItem.getPreferredVendor()!=0){
418
						//System.out.println("Item id "+thriftItem.getId()+".Found prefered vendor and warehouse is marked sticky (Calculating Reserve)"+thriftItem.getPreferredVendor());
419
						for (Warehouse warehouse:vendor_warehouses){
420
							if(warehouse.getBillingWarehouseId()!=7){
421
								if(warehouse.getLogisticsLocation().equals(WarehouseLocation.Delhi) && warehouse.getWarehouseType().equals(WarehouseType.THIRD_PARTY) && warehouse.getVendor().getId()!=1){
422
 
423
								}
424
								else{
425
									continue;
426
								}
427
							}
428
							in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
429
							ignoredItem.setItemId(thriftItem.getId());
430
							ignoredItem.setWarehouseId(warehouse.getId());
431
							if (itemreserve.get(warehouse.getId())!=null && !thirdpartywarehouseids.contains(warehouse.getId()) && !ignoreItems.contains(warehouse.getId())){
432
								nlc = inventoryClient.getNlcForWarehouse(warehouse.getId(),thriftItem.getId());
433
								//maxnlc = catalogClient.getSnapdealItem(thriftItem.getId()).getMaxNlc();
434
								//System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + warehouse.getId() );
435
								if(snapdealItem!=null){
436
									if(nlc !=0 && (maxnlcSD >= nlc)){
437
										//reserve = reserve + itemreserve.get(warehouse.getId());
438
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
11876 manish.sha 439
										//continue;
11874 manish.sha 440
									}
441
									else if(maxnlcSD==0){
442
										//reserve = reserve + itemreserve.get(warehouse.getId());
443
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
11876 manish.sha 444
										//continue;
11874 manish.sha 445
									}
446
									else{
11882 manish.sha 447
										if(snapdealLeftInvDataMap.containsKey(snapdealItem.getSkuAtSnapdeal())){
448
											Set<NlcLeftOutInventory> newNlcObjSet = snapdealLeftInvDataMap.get(snapdealItem.getSkuAtSnapdeal());
449
											Iterator<NlcLeftOutInventory> it = newNlcObjSet.iterator();
450
											int i=0;
451
											while(it.hasNext()){
452
												NlcLeftOutInventory newObj = it.next();
453
												if(newObj.getVendorWarehouseId()==warehouse.getId()){
454
													newObj.setReserved(itemreserve.get(warehouse.getId()));
455
													newObj.setTotalAvailability(newObj.getVendorAvailability()-itemreserve.get(warehouse.getId()));
456
													newNlcObjSet.remove(i);
457
													newNlcObjSet.add(newObj);
458
												}
459
												i++;
460
											}
461
 
462
											nlcLoiSetSD = newNlcObjSet;
11874 manish.sha 463
										}
464
									}
465
								}
466
 
467
								if(flipkartItem!=null){
468
									if(nlc !=0 && (maxnlcFK >= nlc)){
469
										//reserve = reserve + itemreserve.get(warehouse.getId());
470
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
11876 manish.sha 471
										//continue;
11874 manish.sha 472
									}
473
									else if(maxnlcFK==0){
474
										//reserve = reserve + itemreserve.get(warehouse.getId());
475
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
11876 manish.sha 476
										//continue;
11874 manish.sha 477
									}
478
									else{
11882 manish.sha 479
										if(flipkartLeftInvDataMap.containsKey(flipkartItem.getSkuAtFlipkart())){
480
											Set<NlcLeftOutInventory> newNlcObjSet = flipkartLeftInvDataMap.get(flipkartItem.getSkuAtFlipkart());
481
											Iterator<NlcLeftOutInventory> it = newNlcObjSet.iterator();
482
											int i=0;
483
											while(it.hasNext()){
484
												NlcLeftOutInventory newObj = it.next();
485
												if(newObj.getVendorWarehouseId()==warehouse.getId()){
486
													newObj.setReserved(itemreserve.get(warehouse.getId()));
487
													newObj.setTotalAvailability(newObj.getVendorAvailability()-itemreserve.get(warehouse.getId()));
488
													newNlcObjSet.remove(i);
489
													newNlcObjSet.add(newObj);
490
												}
491
												i++;
492
											}
493
 
494
											nlcLoiSetFK = newNlcObjSet;
11874 manish.sha 495
										}
496
									}
497
								}
498
 
499
 
500
							}
501
						}
11882 manish.sha 502
 
11884 manish.sha 503
						if(nlcLoiSetSD!=null && nlcLoiSetSD.size()>0){
504
							snapdealLeftInvDataMap.put(snapdealItem.getSkuAtSnapdeal(), nlcLoiSetSD);
505
						}
506
						if(nlcLoiSetFK!=null && nlcLoiSetFK.size()>0){
507
							flipkartLeftInvDataMap.put(flipkartItem.getSkuAtFlipkart(), nlcLoiSetFK);
508
						}
11882 manish.sha 509
 
11874 manish.sha 510
					}else{
511
						for (Map.Entry<Long,Long> entry : itemreserve.entrySet()) {
512
							if(allWarehousesMap.get(entry.getKey()).getBillingWarehouseId()!=7){
513
								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){
514
 
515
								}
516
								else{
517
									continue;
518
								}
519
							}
520
							in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
521
							ignoredItem.setItemId(thriftItem.getId());
522
							ignoredItem.setWarehouseId(entry.getKey());
523
							if(!thirdpartywarehouseids.contains(entry.getKey()) && !ignoreItems.contains(ignoredItem)){
524
								nlc = inventoryClient.getNlcForWarehouse(entry.getKey(),thriftItem.getId());
525
								//System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + entry.getKey() );
526
								if(snapdealItem!=null){
527
									if(nlc !=0 && (maxnlcSD >= nlc)){
528
										//reserve =  reserve + entry.getValue();
529
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
11876 manish.sha 530
										//continue;
11874 manish.sha 531
									}
532
									else if(maxnlcSD==0){
533
										//reserve =  reserve + entry.getValue();
534
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
11876 manish.sha 535
										//continue;
11874 manish.sha 536
									}
537
									else{
11882 manish.sha 538
										if(snapdealLeftInvDataMap.containsKey(snapdealItem.getSkuAtSnapdeal())){
539
											Set<NlcLeftOutInventory> newNlcObjSet = snapdealLeftInvDataMap.get(snapdealItem.getSkuAtSnapdeal());
540
											Iterator<NlcLeftOutInventory> it = newNlcObjSet.iterator();
541
											int i=0;
542
											while(it.hasNext()){
543
												NlcLeftOutInventory newObj = it.next();
544
												if(newObj.getVendorWarehouseId()==entry.getKey()){
545
													newObj.setReserved(itemreserve.get(entry.getKey()));
546
													newObj.setTotalAvailability(newObj.getVendorAvailability()-itemreserve.get(entry.getKey()));
547
													newNlcObjSet.remove(i);
548
													newNlcObjSet.add(newObj);
549
												}
550
												i++;
551
											}
552
 
553
											nlcLoiSetSD = newNlcObjSet;
11874 manish.sha 554
										}
555
									}
556
								}
557
								if(flipkartItem!=null){
558
									if(nlc !=0 && (maxnlcFK >= nlc)){
559
										//reserve =  reserve + entry.getValue();
560
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
11876 manish.sha 561
										//continue;
11874 manish.sha 562
									}
563
									else if(maxnlcFK==0){
564
										//reserve =  reserve + entry.getValue();
565
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
11876 manish.sha 566
										//continue;
11874 manish.sha 567
									}
568
									else{
11882 manish.sha 569
										if(flipkartLeftInvDataMap.containsKey(flipkartItem.getSkuAtFlipkart())){
570
											Set<NlcLeftOutInventory> newNlcObjSet = flipkartLeftInvDataMap.get(flipkartItem.getSkuAtFlipkart());
571
											Iterator<NlcLeftOutInventory> it = newNlcObjSet.iterator();
572
											int i=0;
573
											while(it.hasNext()){
574
												NlcLeftOutInventory newObj = it.next();
575
												if(newObj.getVendorWarehouseId()==entry.getKey()){
576
													newObj.setReserved(itemreserve.get(entry.getKey()));
577
													newObj.setTotalAvailability(newObj.getVendorAvailability()-itemreserve.get(entry.getKey()));
578
													newNlcObjSet.remove(i);
579
													newNlcObjSet.add(newObj);
580
												}
581
												i++;
582
											}
583
 
584
											nlcLoiSetFK = newNlcObjSet;
11874 manish.sha 585
										}
586
									}
587
								}
588
 
589
							}
590
 
591
							else{
592
								//System.out.println("skipping inventory for warehouse id " + entry.getKey());
593
							}
594
						}
11882 manish.sha 595
 
11884 manish.sha 596
						if(nlcLoiSetSD!=null && nlcLoiSetSD.size()>0){
597
							snapdealLeftInvDataMap.put(snapdealItem.getSkuAtSnapdeal(), nlcLoiSetSD);
598
						}
599
						if(nlcLoiSetFK!=null && nlcLoiSetFK.size()>0){
600
							flipkartLeftInvDataMap.put(flipkartItem.getSkuAtFlipkart(), nlcLoiSetFK);
601
						}
11874 manish.sha 602
					}
603
 
604
 
605
				}
606
 
11882 manish.sha 607
				for (Map.Entry<String,Set<NlcLeftOutInventory>> entry : snapdealLeftInvDataMap.entrySet()){
608
					for(NlcLeftOutInventory nlcLOI : entry.getValue()){
609
						nlcLeftOutInventoryItemsSD.add(nlcLOI);
610
					}
11874 manish.sha 611
				}
11882 manish.sha 612
				for (Map.Entry<String,Set<NlcLeftOutInventory>> entry : flipkartLeftInvDataMap.entrySet()){
613
					for(NlcLeftOutInventory nlcLOI : entry.getValue()){
614
						nlcLeftOutInventoryItemsFK.add(nlcLOI);
615
					}
11874 manish.sha 616
				}
617
 
618
			}
619
		} catch (TException e) {
620
			// TODO Auto-generated catch block
621
			e.printStackTrace();
622
		}
623
 
624
	}
625
 
626
	private static void createAndSendLeftOutInventoryFile(List<NlcLeftOutInventory> nlcLeftoutInventoryItems, String fileName){
627
		try {
628
			File file = new File("/tmp/" + fileName +"-leftOutInv.xls");
629
			BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
630
			bufferedWriter.write(StringUtils.join(new String[] {
631
					"ItemId", 
632
					"Product Name",
633
					"SkuAtSource",
634
					"Vendor Name",
635
					"Max Our NLC",
636
					"Vendor NLC",
637
					"Availability Count",
638
					"Reserved Count",
639
					"Total Availabilty"
640
			}, '\t'));
641
 
642
			for(NlcLeftOutInventory nlcLOI : nlcLeftoutInventoryItems){
643
				bufferedWriter.newLine();
644
 
645
				bufferedWriter.write(StringUtils.join(
646
						new String[] {
647
								String.valueOf(nlcLOI.getItemId()),
648
								nlcLOI.getProductName(),
649
								nlcLOI.getSkuAtSource(),
650
								nlcLOI.getVendorName(),
651
								String.valueOf(nlcLOI.getOurNlc()),
652
								String.valueOf(nlcLOI.getVendorNlc()),
653
								String.valueOf(nlcLOI.getVendorAvailability()),
654
								String.valueOf(nlcLOI.getReserved()),
655
								String.valueOf(nlcLOI.getTotalAvailability())}, '\t'));
656
 
657
			}
658
			bufferedWriter.close();
659
 
660
			List<File> files = new ArrayList<File>();
661
			files.add(file);
662
 
11882 manish.sha 663
			//mailer.sendSSLMessage(sendTo, "Inventory not Sent due to NLC Limitaion in case of - "+ fileName , "Please find attached file.", emailFromAddress, password, files);
11874 manish.sha 664
		} catch (Exception e) {
665
			e.printStackTrace();
11882 manish.sha 666
			/*try {
667
				//mailer.sendSSLMessage(sendTo,"Error while creating file from data ", emailFromAddress, password, "");
11874 manish.sha 668
			} catch (MessagingException e1) {
669
				e1.printStackTrace();
11882 manish.sha 670
			}*/
11874 manish.sha 671
		}
672
	}
673
 
674
	//private static DefaultHttpClient client = new DefaultHttpClient();
675
 
676
	public static void main(String[] args) throws Exception {
677
		calculateinventory();
678
		createAndSendLeftOutInventoryFile(nlcLeftOutInventoryItemsSD, "SNAPDEAL");
679
		createAndSendLeftOutInventoryFile(nlcLeftOutInventoryItemsFK, "FLIPKART");
680
 
681
 
682
		/*HttpPost post = new HttpPost("http://selleraccounts.snapdeal.com/keymaker/login");
683
		HttpGet get = new HttpGet("http://seller.snapdeal.com/pricing/search?gridType=normal&_search=false&nd=1392188583467&rows=30&page=1&sidx=&sord=asc");
684
		BufferedReader rd= null;
685
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
686
		nameValuePairs.add(new BasicNameValuePair("username",
687
		"khushal.bhatia@saholic.com"));
688
		nameValuePairs.add(new BasicNameValuePair("password",
689
		"sonline"));
690
		nameValuePairs.add(new BasicNameValuePair("lt",
691
		"LT-14717-oFCLyRTRMWfMDeczgXm0aaufUjfwPV"));
692
		nameValuePairs.add(new BasicNameValuePair("execution",
693
		"e1s1"));
694
		nameValuePairs.add(new BasicNameValuePair("_eventId",
695
		"submit"));
696
		nameValuePairs.add(new BasicNameValuePair("submit",
697
		"LOGIN"));
698
		//post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
699
 
700
		HttpResponse response = null;
701
		try {
702
			response = client.execute(post);
703
		} catch (ClientProtocolException e) {
704
			// TODO Auto-generated catch block
705
			e.printStackTrace();
706
		} catch (IOException e) {
707
			// TODO Auto-generated catch block
708
			e.printStackTrace();
709
		}
710
 
711
		try {
712
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
713
		} catch (IllegalStateException e1) {
714
			// TODO Auto-generated catch block
715
			e1.printStackTrace();
716
		} catch (IOException e1) {
717
			// TODO Auto-generated catch block
718
			e1.printStackTrace();
719
		}
720
		String line = "";
721
		try {
722
			while ((line = rd.readLine()) != null) {
723
				System.out.println(line);
724
			}
725
		} catch (IOException e) {
726
			// TODO Auto-generated catch block
727
			e.printStackTrace();
728
		}
729
 
730
		for(Header h: response.getAllHeaders()){
731
			System.out.println(h.getName()+" "+h.getValue());
732
		}
733
 
734
 
735
		try {
736
			response = client.execute(get);
737
		} catch (ClientProtocolException e) {
738
			// TODO Auto-generated catch block
739
			e.printStackTrace();
740
		} catch (IOException e) {
741
			// TODO Auto-generated catch block
742
			e.printStackTrace();
743
		}
744
 
745
		try {
746
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
747
		} catch (IllegalStateException e1) {
748
			// TODO Auto-generated catch block
749
			e1.printStackTrace();
750
		} catch (IOException e1) {
751
			// TODO Auto-generated catch block
752
			e1.printStackTrace();
753
		}
754
 
755
		System.out.println("Get Response for Get Request");
756
 
757
		try {
758
			while ((line = rd.readLine()) != null) {
759
				System.out.println(line);
760
			}
761
		} catch (IOException e) {
762
			// TODO Auto-generated catch block
763
			e.printStackTrace();
764
		}*/
765
 
766
	}
767
}