Subversion Repositories SmartDukaan

Rev

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