Subversion Repositories SmartDukaan

Rev

Rev 11876 | Rev 11884 | 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
						}
11882 manish.sha 312
						snapdealLeftInvDataMap.put(snapdealItem.getSkuAtSnapdeal(), nlcLoiSetSD);
313
						flipkartLeftInvDataMap.put(flipkartItem.getSkuAtFlipkart(), nlcLoiSetFK);
11874 manish.sha 314
					}
315
					else{
316
						for (Map.Entry<Long,Long> entry :  itemavailability.entrySet()) {
317
							if(allWarehousesMap.get(entry.getKey()).getBillingWarehouseId()!=7){
318
								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){
319
 
320
								}
321
								else{
322
									continue;
323
								}
324
							}
325
							in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
326
							ignoredItem.setItemId(thriftItem.getId());
327
							ignoredItem.setWarehouseId(entry.getKey());
328
							if(!thirdpartywarehouseids.contains(entry.getKey()) && !ignoreItems.contains(ignoredItem)){
329
 
330
								nlc = inventoryClient.getNlcForWarehouse(entry.getKey(),thriftItem.getId());
331
 
332
								//System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + entry.getKey() );
333
								if(snapdealItem!=null){
334
									if(nlc !=0 && (maxnlcSD >= nlc)){
335
										/*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 0);
336
										heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 7);
337
										available =  available + entry.getValue();
338
										total_held = total_held + total_warehouse_held;*/
11876 manish.sha 339
										//continue;
11874 manish.sha 340
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
341
									}
342
									else if(maxnlcSD==0){
343
										/*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 0);
344
										heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 7);
345
										available = available + itemavailability.get(entry.getKey());
346
										total_held = total_held + total_warehouse_held;*/
347
 
11876 manish.sha 348
										//continue;
11874 manish.sha 349
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
350
									}
351
									else{
11875 manish.sha 352
										long available = itemavailability.get(entry.getKey());
353
										if(available>0){
354
											nlcLOISD = new NlcLeftOutInventory();
355
											nlcLOISD.setItemId(snapdealItem.getItem_id());
356
											nlcLOISD.setProductName(snapdealItem.getBrand()+" " +snapdealItem.getModel_name() + " "+ snapdealItem.getModel_number() + " " +  snapdealItem.getColor());
357
											nlcLOISD.setOurNlc(maxnlcSD);
358
											nlcLOISD.setSkuAtSource(snapdealItem.getSkuAtSnapdeal());
11882 manish.sha 359
											nlcLOISD.setVendorName(vendorDataMap.get(allWarehousesMap.get(entry.getKey()).getVendor().getId()));
11875 manish.sha 360
											nlcLOISD.setVendorAvailability(available);
361
											nlcLOISD.setVendorNlc(nlc);
11882 manish.sha 362
											nlcLoiSetSD.add(nlcLOISD);
11875 manish.sha 363
										}
11874 manish.sha 364
									}
365
								}
366
 
367
								if(flipkartItem!=null){
368
									if(nlc !=0 && (maxnlcFK >= nlc)){
369
										/*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 0);
370
										heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 8);
371
										available =  available + entry.getValue();
372
										total_held = total_held + total_warehouse_held;*/
11876 manish.sha 373
										//continue;
11874 manish.sha 374
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
375
									}
376
									else if(maxnlcFK==0){
377
										/*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 0);
378
										heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 8);
379
										available = available + itemavailability.get(entry.getKey());
380
										total_held = total_held + total_warehouse_held;*/
11876 manish.sha 381
										//continue;
11874 manish.sha 382
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
383
									}
384
									else{
11875 manish.sha 385
										long available = itemavailability.get(entry.getKey());
386
										if(available>0){
387
											nlcLOIFK = new NlcLeftOutInventory();
388
											nlcLOIFK.setItemId(flipkartItem.getItem_id());
389
											nlcLOIFK.setProductName(thriftItem.getBrand()+" " +thriftItem.getModelName() + " "+ thriftItem.getModelNumber() + " " +  thriftItem.getColor());
390
											nlcLOIFK.setOurNlc(maxnlcFK);
391
											nlcLOIFK.setSkuAtSource(flipkartItem.getSkuAtFlipkart());
11882 manish.sha 392
											nlcLOIFK.setVendorName(vendorDataMap.get(allWarehousesMap.get(entry.getKey()).getVendor().getId()));
11875 manish.sha 393
											nlcLOIFK.setVendorAvailability(available);
394
											nlcLOIFK.setVendorNlc(nlc);
11882 manish.sha 395
											nlcLoiSetFK.add(nlcLOIFK);
11875 manish.sha 396
										}
11874 manish.sha 397
									}
398
								}
399
 
400
 
401
							}
402
						}
11882 manish.sha 403
						snapdealLeftInvDataMap.put(snapdealItem.getSkuAtSnapdeal(), nlcLoiSetSD);
404
						flipkartLeftInvDataMap.put(flipkartItem.getSkuAtFlipkart(), nlcLoiSetFK);
11874 manish.sha 405
					}
406
 
407
					Map<Long,Long> itemreserve = new HashMap<Long,Long>();
408
					itemreserve = iteminventory.getReserved();
409
					if (thriftItem.isIsWarehousePreferenceSticky() && thriftItem.getPreferredVendor()!=0){
410
						//System.out.println("Item id "+thriftItem.getId()+".Found prefered vendor and warehouse is marked sticky (Calculating Reserve)"+thriftItem.getPreferredVendor());
411
						for (Warehouse warehouse:vendor_warehouses){
412
							if(warehouse.getBillingWarehouseId()!=7){
413
								if(warehouse.getLogisticsLocation().equals(WarehouseLocation.Delhi) && warehouse.getWarehouseType().equals(WarehouseType.THIRD_PARTY) && warehouse.getVendor().getId()!=1){
414
 
415
								}
416
								else{
417
									continue;
418
								}
419
							}
420
							in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
421
							ignoredItem.setItemId(thriftItem.getId());
422
							ignoredItem.setWarehouseId(warehouse.getId());
423
							if (itemreserve.get(warehouse.getId())!=null && !thirdpartywarehouseids.contains(warehouse.getId()) && !ignoreItems.contains(warehouse.getId())){
424
								nlc = inventoryClient.getNlcForWarehouse(warehouse.getId(),thriftItem.getId());
425
								//maxnlc = catalogClient.getSnapdealItem(thriftItem.getId()).getMaxNlc();
426
								//System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + warehouse.getId() );
427
								if(snapdealItem!=null){
428
									if(nlc !=0 && (maxnlcSD >= nlc)){
429
										//reserve = reserve + itemreserve.get(warehouse.getId());
430
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
11876 manish.sha 431
										//continue;
11874 manish.sha 432
									}
433
									else if(maxnlcSD==0){
434
										//reserve = reserve + itemreserve.get(warehouse.getId());
435
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
11876 manish.sha 436
										//continue;
11874 manish.sha 437
									}
438
									else{
11882 manish.sha 439
										if(snapdealLeftInvDataMap.containsKey(snapdealItem.getSkuAtSnapdeal())){
440
											Set<NlcLeftOutInventory> newNlcObjSet = snapdealLeftInvDataMap.get(snapdealItem.getSkuAtSnapdeal());
441
											Iterator<NlcLeftOutInventory> it = newNlcObjSet.iterator();
442
											int i=0;
443
											while(it.hasNext()){
444
												NlcLeftOutInventory newObj = it.next();
445
												if(newObj.getVendorWarehouseId()==warehouse.getId()){
446
													newObj.setReserved(itemreserve.get(warehouse.getId()));
447
													newObj.setTotalAvailability(newObj.getVendorAvailability()-itemreserve.get(warehouse.getId()));
448
													newNlcObjSet.remove(i);
449
													newNlcObjSet.add(newObj);
450
												}
451
												i++;
452
											}
453
 
454
											nlcLoiSetSD = newNlcObjSet;
11874 manish.sha 455
										}
456
									}
457
								}
458
 
459
								if(flipkartItem!=null){
460
									if(nlc !=0 && (maxnlcFK >= nlc)){
461
										//reserve = reserve + itemreserve.get(warehouse.getId());
462
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
11876 manish.sha 463
										//continue;
11874 manish.sha 464
									}
465
									else if(maxnlcFK==0){
466
										//reserve = reserve + itemreserve.get(warehouse.getId());
467
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
11876 manish.sha 468
										//continue;
11874 manish.sha 469
									}
470
									else{
11882 manish.sha 471
										if(flipkartLeftInvDataMap.containsKey(flipkartItem.getSkuAtFlipkart())){
472
											Set<NlcLeftOutInventory> newNlcObjSet = flipkartLeftInvDataMap.get(flipkartItem.getSkuAtFlipkart());
473
											Iterator<NlcLeftOutInventory> it = newNlcObjSet.iterator();
474
											int i=0;
475
											while(it.hasNext()){
476
												NlcLeftOutInventory newObj = it.next();
477
												if(newObj.getVendorWarehouseId()==warehouse.getId()){
478
													newObj.setReserved(itemreserve.get(warehouse.getId()));
479
													newObj.setTotalAvailability(newObj.getVendorAvailability()-itemreserve.get(warehouse.getId()));
480
													newNlcObjSet.remove(i);
481
													newNlcObjSet.add(newObj);
482
												}
483
												i++;
484
											}
485
 
486
											nlcLoiSetFK = newNlcObjSet;
11874 manish.sha 487
										}
488
									}
489
								}
490
 
491
 
492
							}
493
						}
11882 manish.sha 494
 
495
						snapdealLeftInvDataMap.put(snapdealItem.getSkuAtSnapdeal(), nlcLoiSetSD);
496
						flipkartLeftInvDataMap.put(flipkartItem.getSkuAtFlipkart(), nlcLoiSetFK);
497
 
11874 manish.sha 498
					}else{
499
						for (Map.Entry<Long,Long> entry : itemreserve.entrySet()) {
500
							if(allWarehousesMap.get(entry.getKey()).getBillingWarehouseId()!=7){
501
								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){
502
 
503
								}
504
								else{
505
									continue;
506
								}
507
							}
508
							in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
509
							ignoredItem.setItemId(thriftItem.getId());
510
							ignoredItem.setWarehouseId(entry.getKey());
511
							if(!thirdpartywarehouseids.contains(entry.getKey()) && !ignoreItems.contains(ignoredItem)){
512
								nlc = inventoryClient.getNlcForWarehouse(entry.getKey(),thriftItem.getId());
513
								//System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + entry.getKey() );
514
								if(snapdealItem!=null){
515
									if(nlc !=0 && (maxnlcSD >= nlc)){
516
										//reserve =  reserve + entry.getValue();
517
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
11876 manish.sha 518
										//continue;
11874 manish.sha 519
									}
520
									else if(maxnlcSD==0){
521
										//reserve =  reserve + entry.getValue();
522
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
11876 manish.sha 523
										//continue;
11874 manish.sha 524
									}
525
									else{
11882 manish.sha 526
										if(snapdealLeftInvDataMap.containsKey(snapdealItem.getSkuAtSnapdeal())){
527
											Set<NlcLeftOutInventory> newNlcObjSet = snapdealLeftInvDataMap.get(snapdealItem.getSkuAtSnapdeal());
528
											Iterator<NlcLeftOutInventory> it = newNlcObjSet.iterator();
529
											int i=0;
530
											while(it.hasNext()){
531
												NlcLeftOutInventory newObj = it.next();
532
												if(newObj.getVendorWarehouseId()==entry.getKey()){
533
													newObj.setReserved(itemreserve.get(entry.getKey()));
534
													newObj.setTotalAvailability(newObj.getVendorAvailability()-itemreserve.get(entry.getKey()));
535
													newNlcObjSet.remove(i);
536
													newNlcObjSet.add(newObj);
537
												}
538
												i++;
539
											}
540
 
541
											nlcLoiSetSD = newNlcObjSet;
11874 manish.sha 542
										}
543
									}
544
								}
545
								if(flipkartItem!=null){
546
									if(nlc !=0 && (maxnlcFK >= nlc)){
547
										//reserve =  reserve + entry.getValue();
548
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
11876 manish.sha 549
										//continue;
11874 manish.sha 550
									}
551
									else if(maxnlcFK==0){
552
										//reserve =  reserve + entry.getValue();
553
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
11876 manish.sha 554
										//continue;
11874 manish.sha 555
									}
556
									else{
11882 manish.sha 557
										if(flipkartLeftInvDataMap.containsKey(flipkartItem.getSkuAtFlipkart())){
558
											Set<NlcLeftOutInventory> newNlcObjSet = flipkartLeftInvDataMap.get(flipkartItem.getSkuAtFlipkart());
559
											Iterator<NlcLeftOutInventory> it = newNlcObjSet.iterator();
560
											int i=0;
561
											while(it.hasNext()){
562
												NlcLeftOutInventory newObj = it.next();
563
												if(newObj.getVendorWarehouseId()==entry.getKey()){
564
													newObj.setReserved(itemreserve.get(entry.getKey()));
565
													newObj.setTotalAvailability(newObj.getVendorAvailability()-itemreserve.get(entry.getKey()));
566
													newNlcObjSet.remove(i);
567
													newNlcObjSet.add(newObj);
568
												}
569
												i++;
570
											}
571
 
572
											nlcLoiSetFK = newNlcObjSet;
11874 manish.sha 573
										}
574
									}
575
								}
576
 
577
							}
578
 
579
							else{
580
								//System.out.println("skipping inventory for warehouse id " + entry.getKey());
581
							}
582
						}
11882 manish.sha 583
 
584
						snapdealLeftInvDataMap.put(snapdealItem.getSkuAtSnapdeal(), nlcLoiSetSD);
585
						flipkartLeftInvDataMap.put(flipkartItem.getSkuAtFlipkart(), nlcLoiSetFK);
11874 manish.sha 586
					}
587
 
588
 
589
				}
590
 
11882 manish.sha 591
				for (Map.Entry<String,Set<NlcLeftOutInventory>> entry : snapdealLeftInvDataMap.entrySet()){
592
					for(NlcLeftOutInventory nlcLOI : entry.getValue()){
593
						nlcLeftOutInventoryItemsSD.add(nlcLOI);
594
					}
11874 manish.sha 595
				}
11882 manish.sha 596
				for (Map.Entry<String,Set<NlcLeftOutInventory>> entry : flipkartLeftInvDataMap.entrySet()){
597
					for(NlcLeftOutInventory nlcLOI : entry.getValue()){
598
						nlcLeftOutInventoryItemsFK.add(nlcLOI);
599
					}
11874 manish.sha 600
				}
601
 
602
			}
603
		} catch (TException e) {
604
			// TODO Auto-generated catch block
605
			e.printStackTrace();
606
		}
607
 
608
	}
609
 
610
	private static void createAndSendLeftOutInventoryFile(List<NlcLeftOutInventory> nlcLeftoutInventoryItems, String fileName){
611
		try {
612
			File file = new File("/tmp/" + fileName +"-leftOutInv.xls");
613
			BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
614
			bufferedWriter.write(StringUtils.join(new String[] {
615
					"ItemId", 
616
					"Product Name",
617
					"SkuAtSource",
618
					"Vendor Name",
619
					"Max Our NLC",
620
					"Vendor NLC",
621
					"Availability Count",
622
					"Reserved Count",
623
					"Total Availabilty"
624
			}, '\t'));
625
 
626
			for(NlcLeftOutInventory nlcLOI : nlcLeftoutInventoryItems){
627
				bufferedWriter.newLine();
628
 
629
				bufferedWriter.write(StringUtils.join(
630
						new String[] {
631
								String.valueOf(nlcLOI.getItemId()),
632
								nlcLOI.getProductName(),
633
								nlcLOI.getSkuAtSource(),
634
								nlcLOI.getVendorName(),
635
								String.valueOf(nlcLOI.getOurNlc()),
636
								String.valueOf(nlcLOI.getVendorNlc()),
637
								String.valueOf(nlcLOI.getVendorAvailability()),
638
								String.valueOf(nlcLOI.getReserved()),
639
								String.valueOf(nlcLOI.getTotalAvailability())}, '\t'));
640
 
641
			}
642
			bufferedWriter.close();
643
 
644
			List<File> files = new ArrayList<File>();
645
			files.add(file);
646
 
11882 manish.sha 647
			//mailer.sendSSLMessage(sendTo, "Inventory not Sent due to NLC Limitaion in case of - "+ fileName , "Please find attached file.", emailFromAddress, password, files);
11874 manish.sha 648
		} catch (Exception e) {
649
			e.printStackTrace();
11882 manish.sha 650
			/*try {
651
				//mailer.sendSSLMessage(sendTo,"Error while creating file from data ", emailFromAddress, password, "");
11874 manish.sha 652
			} catch (MessagingException e1) {
653
				e1.printStackTrace();
11882 manish.sha 654
			}*/
11874 manish.sha 655
		}
656
	}
657
 
658
	//private static DefaultHttpClient client = new DefaultHttpClient();
659
 
660
	public static void main(String[] args) throws Exception {
661
		calculateinventory();
662
		createAndSendLeftOutInventoryFile(nlcLeftOutInventoryItemsSD, "SNAPDEAL");
663
		createAndSendLeftOutInventoryFile(nlcLeftOutInventoryItemsFK, "FLIPKART");
664
 
665
 
666
		/*HttpPost post = new HttpPost("http://selleraccounts.snapdeal.com/keymaker/login");
667
		HttpGet get = new HttpGet("http://seller.snapdeal.com/pricing/search?gridType=normal&_search=false&nd=1392188583467&rows=30&page=1&sidx=&sord=asc");
668
		BufferedReader rd= null;
669
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
670
		nameValuePairs.add(new BasicNameValuePair("username",
671
		"khushal.bhatia@saholic.com"));
672
		nameValuePairs.add(new BasicNameValuePair("password",
673
		"sonline"));
674
		nameValuePairs.add(new BasicNameValuePair("lt",
675
		"LT-14717-oFCLyRTRMWfMDeczgXm0aaufUjfwPV"));
676
		nameValuePairs.add(new BasicNameValuePair("execution",
677
		"e1s1"));
678
		nameValuePairs.add(new BasicNameValuePair("_eventId",
679
		"submit"));
680
		nameValuePairs.add(new BasicNameValuePair("submit",
681
		"LOGIN"));
682
		//post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
683
 
684
		HttpResponse response = null;
685
		try {
686
			response = client.execute(post);
687
		} catch (ClientProtocolException e) {
688
			// TODO Auto-generated catch block
689
			e.printStackTrace();
690
		} catch (IOException e) {
691
			// TODO Auto-generated catch block
692
			e.printStackTrace();
693
		}
694
 
695
		try {
696
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
697
		} catch (IllegalStateException e1) {
698
			// TODO Auto-generated catch block
699
			e1.printStackTrace();
700
		} catch (IOException e1) {
701
			// TODO Auto-generated catch block
702
			e1.printStackTrace();
703
		}
704
		String line = "";
705
		try {
706
			while ((line = rd.readLine()) != null) {
707
				System.out.println(line);
708
			}
709
		} catch (IOException e) {
710
			// TODO Auto-generated catch block
711
			e.printStackTrace();
712
		}
713
 
714
		for(Header h: response.getAllHeaders()){
715
			System.out.println(h.getName()+" "+h.getValue());
716
		}
717
 
718
 
719
		try {
720
			response = client.execute(get);
721
		} catch (ClientProtocolException e) {
722
			// TODO Auto-generated catch block
723
			e.printStackTrace();
724
		} catch (IOException e) {
725
			// TODO Auto-generated catch block
726
			e.printStackTrace();
727
		}
728
 
729
		try {
730
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
731
		} catch (IllegalStateException e1) {
732
			// TODO Auto-generated catch block
733
			e1.printStackTrace();
734
		} catch (IOException e1) {
735
			// TODO Auto-generated catch block
736
			e1.printStackTrace();
737
		}
738
 
739
		System.out.println("Get Response for Get Request");
740
 
741
		try {
742
			while ((line = rd.readLine()) != null) {
743
				System.out.println(line);
744
			}
745
		} catch (IOException e) {
746
			// TODO Auto-generated catch block
747
			e.printStackTrace();
748
		}*/
749
 
750
	}
751
}