Subversion Repositories SmartDukaan

Rev

Rev 11891 | Rev 11940 | 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[];
11889 manish.sha 68
	private static Map<String, Map<Long, NlcLeftOutInventory>> nlcLoiItemsMapSD = new HashMap<String, Map<Long, NlcLeftOutInventory>>();
69
	private static Map<String, Map<Long, NlcLeftOutInventory>> nlcLoiItemsMapFK = new HashMap<String, Map<Long, NlcLeftOutInventory>>();
11874 manish.sha 70
 
71
	static{
11891 manish.sha 72
		emailFromAddress = "build-staging@shop2020.in";
73
		password = "shop2020";
11874 manish.sha 74
		mailer = new GmailUtils();
11894 manish.sha 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",
11874 manish.sha 77
				"khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","chaitnaya.vats@saholic.com",
11894 manish.sha 78
				"yukti.jain@shop2020.in","manisha.sharma@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in"};*/
11874 manish.sha 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();
11889 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
 
11886 manish.sha 185
			Map<String, Map<Long, NlcLeftOutInventory>> snapdealLeftInvDataMap = new HashMap<String, Map<Long, NlcLeftOutInventory>>();
186
			Map<String, Map<Long, NlcLeftOutInventory>> flipkartLeftInvDataMap = new HashMap<String, Map<Long, 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;
11886 manish.sha 196
				Map<Long, NlcLeftOutInventory> nlcLoiMapSD = new HashMap<Long, NlcLeftOutInventory>();
197
				Map<Long, NlcLeftOutInventory> nlcLoiMapFK = new HashMap<Long, NlcLeftOutInventory>();
11882 manish.sha 198
				Set<NlcLeftOutInventory> nlcLoiSetSD = new HashSet<NlcLeftOutInventory>();
199
				Set<NlcLeftOutInventory> nlcLoiSetFK = new HashSet<NlcLeftOutInventory>();
11874 manish.sha 200
				if(snapdealItemMap.containsKey(thriftItem.getId())){
201
					snapdealItem = snapdealItemMap.get(thriftItem.getId());
202
					maxnlcSD = snapdealItem.getMaxNlc();
203
				}
204
				if(flipkartItemMap.containsKey(thriftItem.getId())){
205
					flipkartItem = flipkartItemMap.get(thriftItem.getId());
206
					maxnlcFK = flipkartItem.getMaxNlc();
207
				}
208
				nlc=0;
209
				List<Warehouse> vendor_warehouses=null; 
210
				if(availability.get(thriftItem.getId())!=null){ 
211
					ItemInventory iteminventory = availability.get(thriftItem.getId());
212
					Map<Long,Long> itemavailability = new HashMap<Long,Long>();
213
					itemavailability = iteminventory.getAvailability();
214
					if (thriftItem.isIsWarehousePreferenceSticky() && thriftItem.getPreferredVendor()!=0){
215
						//System.out.println("Item id "+thriftItem.getId()+".Found prefered vendor and warehouse is marked sticky (Calculating availability)"+thriftItem.getPreferredVendor());						
216
						vendor_warehouses = inventoryClient.getWarehouses(WarehouseType.THIRD_PARTY,InventoryType.GOOD , thriftItem.getPreferredVendor(), 0, 0);
217
						vendor_warehouses.addAll(ours_warehouses);
218
						for (Warehouse warehouse:vendor_warehouses){
219
							if(warehouse.getBillingWarehouseId()!=7){
220
								if(warehouse.getLogisticsLocation().equals(WarehouseLocation.Delhi) && warehouse.getWarehouseType().equals(WarehouseType.THIRD_PARTY) && warehouse.getVendor().getId()!=1){
221
 
222
								}
223
								else{
224
									continue;
225
								}
226
							}
227
							in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
228
							ignoredItem.setItemId(thriftItem.getId());
229
							ignoredItem.setWarehouseId(warehouse.getId());
230
							if (itemavailability.get(warehouse.getId())!=null && !thirdpartywarehouseids.contains(warehouse.getId()) && !ignoreItems.contains(ignoredItem)){
231
								try{
232
									nlc = inventoryClient.getNlcForWarehouse(warehouse.getId(),thriftItem.getId());
233
 
234
								}
235
								catch(TTransportException e){
236
									inventoryClient = inventoryServiceClient.getClient(); 
237
									nlc = inventoryClient.getNlcForWarehouse(warehouse.getId(),thriftItem.getId());
238
								}
239
 
240
								//maxnlc = catalogClient.getSnapdealItem(thriftItem.getId()).getMaxNlc();
241
 
242
								if(snapdealItem!=null){
243
									if(nlc !=0 && (maxnlcSD >= nlc)){
244
										/*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 0);
245
										heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 7);
246
										available = available + itemavailability.get(warehouse.getId());
247
										total_held = total_held + total_warehouse_held;*/ 
11876 manish.sha 248
										//continue;
11874 manish.sha 249
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
250
									}
251
									else if(maxnlcSD==0){
252
										/*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 0);
253
										heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 7);
254
										available = available + itemavailability.get(warehouse.getId());
255
										total_held = total_held + total_warehouse_held;*/
11876 manish.sha 256
										//continue;
11874 manish.sha 257
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
258
									}
259
									else{
11875 manish.sha 260
										long available = itemavailability.get(warehouse.getId());
261
										if(available>0){
262
											nlcLOISD= new NlcLeftOutInventory();
263
											nlcLOISD.setItemId(snapdealItem.getItem_id());
264
											nlcLOISD.setProductName(snapdealItem.getBrand()+" " +snapdealItem.getModel_name() + " "+ snapdealItem.getModel_number() + " " +  snapdealItem.getColor());
265
											nlcLOISD.setOurNlc(maxnlcSD);
266
											nlcLOISD.setSkuAtSource(snapdealItem.getSkuAtSnapdeal());
267
											nlcLOISD.setVendorName(warehouse.getVendor().getName());
268
											nlcLOISD.setVendorAvailability(available);
269
											nlcLOISD.setVendorNlc(nlc);
11882 manish.sha 270
											nlcLOISD.setVendorWarehouseId(warehouse.getId());
11894 manish.sha 271
											nlcLOISD.setWarehouseType(warehouse.getWarehouseType().toString());
11886 manish.sha 272
											if(!nlcLoiMapSD.containsKey(warehouse.getId()))
273
												nlcLoiMapSD.put(warehouse.getId(), nlcLOISD);
11889 manish.sha 274
 
11875 manish.sha 275
										}
11874 manish.sha 276
									}
277
								}
278
 
279
								if(flipkartItem!=null){
280
									if(nlc !=0 && (maxnlcFK >= nlc)){
281
										/*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 0);
282
										heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 8);
283
										available = available + itemavailability.get(warehouse.getId());
284
										total_held = total_held + total_warehouse_held; */
11876 manish.sha 285
										//continue;
11874 manish.sha 286
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
287
									}
288
									else if(maxnlcFK==0){
289
										/*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 0);
290
										heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 8);
291
										available = available + itemavailability.get(warehouse.getId());
292
										total_held = total_held + total_warehouse_held;*/
11876 manish.sha 293
										//continue;
11874 manish.sha 294
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
295
									}
296
									else{
11875 manish.sha 297
										long available = itemavailability.get(warehouse.getId());
298
										if(available>0){
299
											nlcLOIFK = new NlcLeftOutInventory();
300
											nlcLOIFK.setItemId(flipkartItem.getItem_id());
301
											nlcLOIFK.setProductName(thriftItem.getBrand()+" " +thriftItem.getModelName() + " "+ thriftItem.getModelNumber() + " " +  thriftItem.getColor());
302
											nlcLOIFK.setOurNlc(maxnlcFK);
303
											nlcLOIFK.setSkuAtSource(flipkartItem.getSkuAtFlipkart());
304
											nlcLOIFK.setVendorName(warehouse.getVendor().getName());
305
											nlcLOIFK.setVendorAvailability(available);
306
											nlcLOIFK.setVendorNlc(nlc);
11882 manish.sha 307
											nlcLOIFK.setVendorWarehouseId(warehouse.getId());
11894 manish.sha 308
											nlcLOIFK.setWarehouseType(warehouse.getWarehouseType().toString());
11886 manish.sha 309
											if(!nlcLoiMapFK.containsKey(warehouse.getId()))
310
												nlcLoiMapFK.put(warehouse.getId(), nlcLOIFK);
11875 manish.sha 311
										}
11874 manish.sha 312
									}
313
								}
314
								//System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + warehouse.getId() );
315
 
316
							}
317
						}
11886 manish.sha 318
						if(nlcLoiMapSD!=null && nlcLoiMapSD.size()>0){
319
							snapdealLeftInvDataMap.put(snapdealItem.getSkuAtSnapdeal(), nlcLoiMapSD);
11884 manish.sha 320
						}
11886 manish.sha 321
						if(nlcLoiMapFK!=null && nlcLoiMapFK.size()>0){
322
							flipkartLeftInvDataMap.put(flipkartItem.getSkuAtFlipkart(), nlcLoiMapFK);
11884 manish.sha 323
						}
11874 manish.sha 324
					}
325
					else{
326
						for (Map.Entry<Long,Long> entry :  itemavailability.entrySet()) {
327
							if(allWarehousesMap.get(entry.getKey()).getBillingWarehouseId()!=7){
328
								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){
329
 
330
								}
331
								else{
332
									continue;
333
								}
334
							}
335
							in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
336
							ignoredItem.setItemId(thriftItem.getId());
337
							ignoredItem.setWarehouseId(entry.getKey());
338
							if(!thirdpartywarehouseids.contains(entry.getKey()) && !ignoreItems.contains(ignoredItem)){
339
 
340
								nlc = inventoryClient.getNlcForWarehouse(entry.getKey(),thriftItem.getId());
341
 
342
								//System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + entry.getKey() );
343
								if(snapdealItem!=null){
344
									if(nlc !=0 && (maxnlcSD >= nlc)){
345
										/*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 0);
346
										heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 7);
347
										available =  available + entry.getValue();
348
										total_held = total_held + total_warehouse_held;*/
11876 manish.sha 349
										//continue;
11874 manish.sha 350
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
351
									}
352
									else if(maxnlcSD==0){
353
										/*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 0);
354
										heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 7);
355
										available = available + itemavailability.get(entry.getKey());
356
										total_held = total_held + total_warehouse_held;*/
357
 
11876 manish.sha 358
										//continue;
11874 manish.sha 359
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
360
									}
361
									else{
11875 manish.sha 362
										long available = itemavailability.get(entry.getKey());
363
										if(available>0){
364
											nlcLOISD = new NlcLeftOutInventory();
365
											nlcLOISD.setItemId(snapdealItem.getItem_id());
366
											nlcLOISD.setProductName(snapdealItem.getBrand()+" " +snapdealItem.getModel_name() + " "+ snapdealItem.getModel_number() + " " +  snapdealItem.getColor());
367
											nlcLOISD.setOurNlc(maxnlcSD);
368
											nlcLOISD.setSkuAtSource(snapdealItem.getSkuAtSnapdeal());
11882 manish.sha 369
											nlcLOISD.setVendorName(vendorDataMap.get(allWarehousesMap.get(entry.getKey()).getVendor().getId()));
11875 manish.sha 370
											nlcLOISD.setVendorAvailability(available);
371
											nlcLOISD.setVendorNlc(nlc);
11894 manish.sha 372
											nlcLOISD.setVendorWarehouseId(entry.getKey());
373
											nlcLOISD.setWarehouseType(allWarehousesMap.get(entry.getKey()).getWarehouseType().toString());
11886 manish.sha 374
											if(!nlcLoiMapSD.containsKey(entry.getKey()))
375
												nlcLoiMapSD.put(entry.getKey(), nlcLOISD);
11875 manish.sha 376
										}
11874 manish.sha 377
									}
378
								}
379
 
380
								if(flipkartItem!=null){
381
									if(nlc !=0 && (maxnlcFK >= nlc)){
382
										/*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 0);
383
										heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 8);
384
										available =  available + entry.getValue();
385
										total_held = total_held + total_warehouse_held;*/
11876 manish.sha 386
										//continue;
11874 manish.sha 387
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
388
									}
389
									else if(maxnlcFK==0){
390
										/*total_warehouse_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 0);
391
										heldForSource = heldForSource + total_warehouse_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 8);
392
										available = available + itemavailability.get(entry.getKey());
393
										total_held = total_held + total_warehouse_held;*/
11876 manish.sha 394
										//continue;
11874 manish.sha 395
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
396
									}
397
									else{
11875 manish.sha 398
										long available = itemavailability.get(entry.getKey());
399
										if(available>0){
400
											nlcLOIFK = new NlcLeftOutInventory();
401
											nlcLOIFK.setItemId(flipkartItem.getItem_id());
402
											nlcLOIFK.setProductName(thriftItem.getBrand()+" " +thriftItem.getModelName() + " "+ thriftItem.getModelNumber() + " " +  thriftItem.getColor());
403
											nlcLOIFK.setOurNlc(maxnlcFK);
404
											nlcLOIFK.setSkuAtSource(flipkartItem.getSkuAtFlipkart());
11882 manish.sha 405
											nlcLOIFK.setVendorName(vendorDataMap.get(allWarehousesMap.get(entry.getKey()).getVendor().getId()));
11875 manish.sha 406
											nlcLOIFK.setVendorAvailability(available);
407
											nlcLOIFK.setVendorNlc(nlc);
11894 manish.sha 408
											nlcLOIFK.setVendorWarehouseId(entry.getKey());
409
											nlcLOIFK.setWarehouseType(allWarehousesMap.get(entry.getKey()).getWarehouseType().toString());
11886 manish.sha 410
											if(!nlcLoiMapFK.containsKey(entry.getKey()))
411
												nlcLoiMapFK.put(entry.getKey(), nlcLOIFK);
11875 manish.sha 412
										}
11874 manish.sha 413
									}
414
								}
415
 
416
 
417
							}
418
						}
11886 manish.sha 419
						if(nlcLoiMapSD!=null && nlcLoiMapSD.size()>0){
420
							snapdealLeftInvDataMap.put(snapdealItem.getSkuAtSnapdeal(), nlcLoiMapSD);
11884 manish.sha 421
						}
11886 manish.sha 422
						if(nlcLoiMapFK!=null && nlcLoiMapFK.size()>0){
423
							flipkartLeftInvDataMap.put(flipkartItem.getSkuAtFlipkart(), nlcLoiMapFK);
11884 manish.sha 424
						}
11874 manish.sha 425
					}
426
 
427
					Map<Long,Long> itemreserve = new HashMap<Long,Long>();
428
					itemreserve = iteminventory.getReserved();
429
					if (thriftItem.isIsWarehousePreferenceSticky() && thriftItem.getPreferredVendor()!=0){
430
						//System.out.println("Item id "+thriftItem.getId()+".Found prefered vendor and warehouse is marked sticky (Calculating Reserve)"+thriftItem.getPreferredVendor());
431
						for (Warehouse warehouse:vendor_warehouses){
432
							if(warehouse.getBillingWarehouseId()!=7){
433
								if(warehouse.getLogisticsLocation().equals(WarehouseLocation.Delhi) && warehouse.getWarehouseType().equals(WarehouseType.THIRD_PARTY) && warehouse.getVendor().getId()!=1){
434
 
435
								}
436
								else{
437
									continue;
438
								}
439
							}
440
							in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
441
							ignoredItem.setItemId(thriftItem.getId());
442
							ignoredItem.setWarehouseId(warehouse.getId());
443
							if (itemreserve.get(warehouse.getId())!=null && !thirdpartywarehouseids.contains(warehouse.getId()) && !ignoreItems.contains(warehouse.getId())){
444
								nlc = inventoryClient.getNlcForWarehouse(warehouse.getId(),thriftItem.getId());
445
								//maxnlc = catalogClient.getSnapdealItem(thriftItem.getId()).getMaxNlc();
446
								//System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + warehouse.getId() );
447
								if(snapdealItem!=null){
448
									if(nlc !=0 && (maxnlcSD >= nlc)){
449
										//reserve = reserve + itemreserve.get(warehouse.getId());
450
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
11876 manish.sha 451
										//continue;
11874 manish.sha 452
									}
453
									else if(maxnlcSD==0){
454
										//reserve = reserve + itemreserve.get(warehouse.getId());
455
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
11876 manish.sha 456
										//continue;
11874 manish.sha 457
									}
458
									else{
11882 manish.sha 459
										if(snapdealLeftInvDataMap.containsKey(snapdealItem.getSkuAtSnapdeal())){
11886 manish.sha 460
											Map<Long, NlcLeftOutInventory> newNlcObjMap = snapdealLeftInvDataMap.get(snapdealItem.getSkuAtSnapdeal());
11889 manish.sha 461
											if(newNlcObjMap.containsKey(warehouse.getId())){
462
												NlcLeftOutInventory newObj = newNlcObjMap.get(warehouse.getId());
463
												newObj.setReserved(itemreserve.get(warehouse.getId()));
464
												newObj.setTotalAvailability(newObj.getVendorAvailability()-itemreserve.get(warehouse.getId()));
465
												newNlcObjMap.remove(warehouse.getId());
466
												newNlcObjMap.put(warehouse.getId(), newObj);
467
 
468
											}
469
											nlcLoiMapSD = newNlcObjMap;
11874 manish.sha 470
										}
471
									}
472
								}
473
 
474
								if(flipkartItem!=null){
475
									if(nlc !=0 && (maxnlcFK >= nlc)){
476
										//reserve = reserve + itemreserve.get(warehouse.getId());
477
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
11876 manish.sha 478
										//continue;
11874 manish.sha 479
									}
480
									else if(maxnlcFK==0){
481
										//reserve = reserve + itemreserve.get(warehouse.getId());
482
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
11876 manish.sha 483
										//continue;
11874 manish.sha 484
									}
485
									else{
11882 manish.sha 486
										if(flipkartLeftInvDataMap.containsKey(flipkartItem.getSkuAtFlipkart())){
11886 manish.sha 487
											Map<Long, NlcLeftOutInventory> newNlcObjMap = flipkartLeftInvDataMap.get(flipkartItem.getSkuAtFlipkart());
11889 manish.sha 488
											if(newNlcObjMap.containsKey(warehouse.getId())){
489
												NlcLeftOutInventory newObj = newNlcObjMap.get(warehouse.getId());
490
												newObj.setReserved(itemreserve.get(warehouse.getId()));
491
												newObj.setTotalAvailability(newObj.getVendorAvailability()-itemreserve.get(warehouse.getId()));
492
												newNlcObjMap.remove(warehouse.getId());
493
												newNlcObjMap.put(warehouse.getId(), newObj);
494
 
495
											}
496
											nlcLoiMapFK = newNlcObjMap;
11874 manish.sha 497
										}
498
									}
499
								}
500
 
501
 
502
							}
503
						}
11889 manish.sha 504
 
11886 manish.sha 505
						if(nlcLoiMapSD!=null && nlcLoiMapSD.size()>0){
506
							snapdealLeftInvDataMap.put(snapdealItem.getSkuAtSnapdeal(), nlcLoiMapSD);
11884 manish.sha 507
						}
11886 manish.sha 508
						if(nlcLoiMapFK!=null && nlcLoiMapFK.size()>0){
509
							flipkartLeftInvDataMap.put(flipkartItem.getSkuAtFlipkart(), nlcLoiMapFK);
11884 manish.sha 510
						}
11889 manish.sha 511
 
11874 manish.sha 512
					}else{
513
						for (Map.Entry<Long,Long> entry : itemreserve.entrySet()) {
514
							if(allWarehousesMap.get(entry.getKey()).getBillingWarehouseId()!=7){
515
								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){
516
 
517
								}
518
								else{
519
									continue;
520
								}
521
							}
522
							in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
523
							ignoredItem.setItemId(thriftItem.getId());
524
							ignoredItem.setWarehouseId(entry.getKey());
525
							if(!thirdpartywarehouseids.contains(entry.getKey()) && !ignoreItems.contains(ignoredItem)){
526
								nlc = inventoryClient.getNlcForWarehouse(entry.getKey(),thriftItem.getId());
527
								//System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + entry.getKey() );
528
								if(snapdealItem!=null){
529
									if(nlc !=0 && (maxnlcSD >= nlc)){
530
										//reserve =  reserve + entry.getValue();
531
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
11876 manish.sha 532
										//continue;
11874 manish.sha 533
									}
534
									else if(maxnlcSD==0){
535
										//reserve =  reserve + entry.getValue();
536
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
11876 manish.sha 537
										//continue;
11874 manish.sha 538
									}
539
									else{
11882 manish.sha 540
										if(snapdealLeftInvDataMap.containsKey(snapdealItem.getSkuAtSnapdeal())){
11886 manish.sha 541
											Map<Long, NlcLeftOutInventory> newNlcObjMap = snapdealLeftInvDataMap.get(snapdealItem.getSkuAtSnapdeal());
11889 manish.sha 542
											if(newNlcObjMap.containsKey(entry.getKey())){
543
												NlcLeftOutInventory newObj = newNlcObjMap.get(entry.getKey());
544
												newObj.setReserved(itemreserve.get(entry.getKey()));
545
												newObj.setTotalAvailability(newObj.getVendorAvailability()-itemreserve.get(entry.getKey()));
546
												newNlcObjMap.remove(entry.getKey());
547
												newNlcObjMap.put(entry.getKey(), newObj);
548
 
549
											}
550
											nlcLoiMapSD = newNlcObjMap;
11874 manish.sha 551
										}
552
									}
553
								}
554
								if(flipkartItem!=null){
555
									if(nlc !=0 && (maxnlcFK >= nlc)){
556
										//reserve =  reserve + entry.getValue();
557
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
11876 manish.sha 558
										//continue;
11874 manish.sha 559
									}
560
									else if(maxnlcFK==0){
561
										//reserve =  reserve + entry.getValue();
562
										//System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
11876 manish.sha 563
										//continue;
11874 manish.sha 564
									}
565
									else{
11882 manish.sha 566
										if(flipkartLeftInvDataMap.containsKey(flipkartItem.getSkuAtFlipkart())){
11886 manish.sha 567
											Map<Long, NlcLeftOutInventory> newNlcObjMap = flipkartLeftInvDataMap.get(flipkartItem.getSkuAtFlipkart());
11889 manish.sha 568
											if(newNlcObjMap.containsKey(entry.getKey())){
569
												NlcLeftOutInventory newObj = newNlcObjMap.get(entry.getKey());
570
												newObj.setReserved(itemreserve.get(entry.getKey()));
571
												newObj.setTotalAvailability(newObj.getVendorAvailability()-itemreserve.get(entry.getKey()));
572
												newNlcObjMap.remove(entry.getKey());
573
												newNlcObjMap.put(entry.getKey(), newObj);
574
 
575
											}
576
											nlcLoiMapFK = newNlcObjMap;
11874 manish.sha 577
										}
578
									}
579
								}
580
 
581
							}
582
 
583
							else{
584
								//System.out.println("skipping inventory for warehouse id " + entry.getKey());
585
							}
586
						}
11889 manish.sha 587
 
11886 manish.sha 588
						if(nlcLoiMapSD!=null && nlcLoiMapSD.size()>0){
589
							snapdealLeftInvDataMap.put(snapdealItem.getSkuAtSnapdeal(), nlcLoiMapSD);
11884 manish.sha 590
						}
11886 manish.sha 591
						if(nlcLoiMapFK!=null && nlcLoiMapFK.size()>0){
592
							flipkartLeftInvDataMap.put(flipkartItem.getSkuAtFlipkart(), nlcLoiMapFK);
11884 manish.sha 593
						}
11874 manish.sha 594
					}
595
 
596
 
597
				}
598
 
11889 manish.sha 599
				nlcLoiItemsMapSD = snapdealLeftInvDataMap;
600
				nlcLoiItemsMapFK = flipkartLeftInvDataMap;
601
				/*for (Map.Entry<String,Map<Long, NlcLeftOutInventory>> entry : snapdealLeftInvDataMap.entrySet()){
11886 manish.sha 602
					for(NlcLeftOutInventory nlcLOI : entry.getValue().values()){
11882 manish.sha 603
						nlcLeftOutInventoryItemsSD.add(nlcLOI);
604
					}
11874 manish.sha 605
				}
11886 manish.sha 606
				for (Map.Entry<String,Map<Long, NlcLeftOutInventory>> entry : flipkartLeftInvDataMap.entrySet()){
607
					for(NlcLeftOutInventory nlcLOI : entry.getValue().values()){
11882 manish.sha 608
						nlcLeftOutInventoryItemsFK.add(nlcLOI);
609
					}
11889 manish.sha 610
				}*/
11874 manish.sha 611
 
612
			}
613
		} catch (TException e) {
614
			// TODO Auto-generated catch block
615
			e.printStackTrace();
616
		}
617
 
618
	}
619
 
11889 manish.sha 620
	private static void createAndSendLeftOutInventoryFile(Map<String, Map<Long, NlcLeftOutInventory>> nlcLeftoutInventoryItems, String fileName){
11874 manish.sha 621
		try {
622
			File file = new File("/tmp/" + fileName +"-leftOutInv.xls");
623
			BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
624
			bufferedWriter.write(StringUtils.join(new String[] {
625
					"ItemId", 
626
					"Product Name",
627
					"SkuAtSource",
628
					"Vendor Name",
11894 manish.sha 629
					"Vendor Warehouse Id",
630
					"Warehouse Type",
11874 manish.sha 631
					"Max Our NLC",
632
					"Vendor NLC",
633
					"Availability Count",
634
					"Reserved Count",
11894 manish.sha 635
					"Net Availabilty"
11874 manish.sha 636
			}, '\t'));
637
 
11891 manish.sha 638
			//System.out.println(fileName+ " Main Map Size "+nlcLeftoutInventoryItems.size());
11889 manish.sha 639
 
640
			for(Map<Long, NlcLeftOutInventory> nlcLOImap : nlcLeftoutInventoryItems.values()){
11891 manish.sha 641
				//System.out.println("Inner Map Size "+nlcLOImap.size());
11889 manish.sha 642
				for(NlcLeftOutInventory nlcLOI : nlcLOImap.values()){
643
					bufferedWriter.newLine();
11874 manish.sha 644
 
11889 manish.sha 645
					bufferedWriter.write(StringUtils.join(
646
							new String[] {
647
									String.valueOf(nlcLOI.getItemId()),
648
									nlcLOI.getProductName(),
649
									nlcLOI.getSkuAtSource(),
650
									nlcLOI.getVendorName(),
11894 manish.sha 651
									String.valueOf(nlcLOI.getVendorWarehouseId()),
652
									nlcLOI.getWarehouseType(),
11889 manish.sha 653
									String.valueOf(nlcLOI.getOurNlc()),
654
									String.valueOf(nlcLOI.getVendorNlc()),
655
									String.valueOf(nlcLOI.getVendorAvailability()),
656
									String.valueOf(nlcLOI.getReserved()),
657
									String.valueOf(nlcLOI.getTotalAvailability())}, '\t'));
11874 manish.sha 658
 
11889 manish.sha 659
				}
11874 manish.sha 660
			}
661
			bufferedWriter.close();
662
 
663
			List<File> files = new ArrayList<File>();
664
			files.add(file);
665
 
11891 manish.sha 666
			mailer.sendSSLMessage(sendTo, "Inventory not Sent due to NLC Limitaion in case of - "+ fileName , "Please find attached file.", emailFromAddress, password, files);
11874 manish.sha 667
		} catch (Exception e) {
668
			e.printStackTrace();
11891 manish.sha 669
			try {
670
				mailer.sendSSLMessage(sendTo,"Error while creating file from data ", emailFromAddress, password, "");
11874 manish.sha 671
			} catch (MessagingException e1) {
672
				e1.printStackTrace();
11891 manish.sha 673
			}
11874 manish.sha 674
		}
675
	}
676
 
677
	//private static DefaultHttpClient client = new DefaultHttpClient();
678
 
679
	public static void main(String[] args) throws Exception {
680
		calculateinventory();
11889 manish.sha 681
		createAndSendLeftOutInventoryFile(nlcLoiItemsMapSD, "SNAPDEAL");
682
		createAndSendLeftOutInventoryFile(nlcLoiItemsMapFK, "FLIPKART");
11874 manish.sha 683
 
684
 
685
		/*HttpPost post = new HttpPost("http://selleraccounts.snapdeal.com/keymaker/login");
686
		HttpGet get = new HttpGet("http://seller.snapdeal.com/pricing/search?gridType=normal&_search=false&nd=1392188583467&rows=30&page=1&sidx=&sord=asc");
687
		BufferedReader rd= null;
688
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
689
		nameValuePairs.add(new BasicNameValuePair("username",
690
		"khushal.bhatia@saholic.com"));
691
		nameValuePairs.add(new BasicNameValuePair("password",
692
		"sonline"));
693
		nameValuePairs.add(new BasicNameValuePair("lt",
694
		"LT-14717-oFCLyRTRMWfMDeczgXm0aaufUjfwPV"));
695
		nameValuePairs.add(new BasicNameValuePair("execution",
696
		"e1s1"));
697
		nameValuePairs.add(new BasicNameValuePair("_eventId",
698
		"submit"));
699
		nameValuePairs.add(new BasicNameValuePair("submit",
700
		"LOGIN"));
701
		//post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
702
 
703
		HttpResponse response = null;
704
		try {
705
			response = client.execute(post);
706
		} catch (ClientProtocolException e) {
707
			// TODO Auto-generated catch block
708
			e.printStackTrace();
709
		} catch (IOException e) {
710
			// TODO Auto-generated catch block
711
			e.printStackTrace();
712
		}
713
 
714
		try {
715
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
716
		} catch (IllegalStateException e1) {
717
			// TODO Auto-generated catch block
718
			e1.printStackTrace();
719
		} catch (IOException e1) {
720
			// TODO Auto-generated catch block
721
			e1.printStackTrace();
722
		}
723
		String line = "";
724
		try {
725
			while ((line = rd.readLine()) != null) {
726
				System.out.println(line);
727
			}
728
		} catch (IOException e) {
729
			// TODO Auto-generated catch block
730
			e.printStackTrace();
731
		}
732
 
733
		for(Header h: response.getAllHeaders()){
734
			System.out.println(h.getName()+" "+h.getValue());
735
		}
736
 
737
 
738
		try {
739
			response = client.execute(get);
740
		} catch (ClientProtocolException e) {
741
			// TODO Auto-generated catch block
742
			e.printStackTrace();
743
		} catch (IOException e) {
744
			// TODO Auto-generated catch block
745
			e.printStackTrace();
746
		}
747
 
748
		try {
749
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
750
		} catch (IllegalStateException e1) {
751
			// TODO Auto-generated catch block
752
			e1.printStackTrace();
753
		} catch (IOException e1) {
754
			// TODO Auto-generated catch block
755
			e1.printStackTrace();
756
		}
757
 
758
		System.out.println("Get Response for Get Request");
759
 
760
		try {
761
			while ((line = rd.readLine()) != null) {
762
				System.out.println(line);
763
			}
764
		} catch (IOException e) {
765
			// TODO Auto-generated catch block
766
			e.printStackTrace();
767
		}*/
768
 
769
	}
770
}