Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
6484 amar.kumar 1
package in.shop2020.warehouse.util;
2
 
14754 manish.sha 3
import in.shop2020.model.v1.catalog.CatalogService;
4
import in.shop2020.model.v1.catalog.Item;
14751 manish.sha 5
import in.shop2020.model.v1.inventory.BillingType;
6
import in.shop2020.model.v1.inventory.InventoryService;
7
import in.shop2020.model.v1.inventory.InventoryServiceException;
8
import in.shop2020.model.v1.inventory.InventoryType;
9
import in.shop2020.model.v1.inventory.ItemInventory;
10
import in.shop2020.model.v1.inventory.Warehouse;
11
import in.shop2020.model.v1.inventory.WarehouseType;
14754 manish.sha 12
import in.shop2020.thrift.clients.CatalogClient;
14751 manish.sha 13
import in.shop2020.thrift.clients.InventoryClient;
14
import in.shop2020.utils.GmailUtils;
15
import in.shop2020.warehouse.InventoryAvailability;
16
import in.shop2020.warehouse.handler.ScanHandler;
17
 
6484 amar.kumar 18
import java.io.BufferedWriter;
19
import java.io.File;
20
import java.io.FileWriter;
21
import java.io.IOException;
22
import java.util.ArrayList;
14751 manish.sha 23
import java.util.Arrays;
6484 amar.kumar 24
import java.util.HashMap;
25
import java.util.List;
26
import java.util.Map;
27
 
28
import javax.mail.MessagingException;
29
 
30
import org.apache.commons.lang.StringUtils;
31
import org.apache.thrift.TException;
32
import org.springframework.context.ApplicationContext;
33
import org.springframework.context.support.ClassPathXmlApplicationContext;
34
 
35
public class InventoryMismatchGenerator {
36
 
14751 manish.sha 37
	private ScanHandler scanHandler;
38
 
6484 amar.kumar 39
	/**
40
	 * @param args
41
	 */
42
 
43
	public InventoryMismatchGenerator() {
44
		ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
14751 manish.sha 45
		scanHandler = context.getBean(ScanHandler.class);
6484 amar.kumar 46
	}
47
 
48
	public static void main(String[] args) {
49
		InventoryMismatchGenerator mismatchGenerator = new InventoryMismatchGenerator();
50
		try {
51
			mismatchGenerator.sendMailForMismatches();
14751 manish.sha 52
		} catch (Exception e) {
6484 amar.kumar 53
			// TODO Auto-generated catch block
54
			e.printStackTrace();
55
		}
56
	}
57
 
14751 manish.sha 58
	public void sendMailForMismatches() throws MessagingException, TException, InventoryServiceException {
59
 
60
		List<InventoryMismatchUnit> inventoryMismatches = new ArrayList<InventoryMismatchUnit>();
6484 amar.kumar 61
 
14751 manish.sha 62
		InventoryClient inventoryClient = new InventoryClient();
63
 
14754 manish.sha 64
		CatalogClient catalogClient = new CatalogClient();
65
 
66
		CatalogService.Client catalogServiceClient = catalogClient.getClient();
67
 
14751 manish.sha 68
		InventoryService.Client invClient = inventoryClient.getClient();
69
 
14754 manish.sha 70
		List<Item> allAliveItems = catalogServiceClient.getAllAliveItems();
71
 
72
		Map<Long, Item> allAliveItemsMap = new HashMap<Long, Item>();
73
 
14751 manish.sha 74
		Map<Long, ItemInventory> itemInventoryMap = invClient.getInventorySnapshot(0);
75
		List<Warehouse> allWarehouses = invClient.getAllWarehouses(false);
76
 
22622 amit.gupta 77
		//List<Long> PHYSICAL_WAREHOUSE_IDS = Arrays.asList(7441L, 7480L);
22725 amit.gupta 78
		List<Long> PHYSICAL_WAREHOUSE_IDS = Arrays.asList(7573L, 7678L, 7681L, 7720L);
14751 manish.sha 79
		Map<Long, Warehouse> allWarehousesMap = new HashMap<Long, Warehouse>();
80
 
81
		List<InventoryAvailability> totalInventoryList;
82
 
83
		for(Warehouse wh : allWarehouses){
84
			allWarehousesMap.put(wh.getId(), wh);
85
		}
86
 
14754 manish.sha 87
		for(Item item: allAliveItems){
88
			allAliveItemsMap.put(item.getId(), item);
89
		}
90
 
14751 manish.sha 91
		for(Long physicalWarehouseId : PHYSICAL_WAREHOUSE_IDS){
92
			totalInventoryList = new ArrayList<InventoryAvailability>();
93
 
94
			List<InventoryAvailability> serializedInventoryList = scanHandler.getCurrentSerializedInventoryByScans(physicalWarehouseId);
95
			List<InventoryAvailability> nonSerializedInventoryList = scanHandler.getCurrentNonSerializedInventoryByScans(physicalWarehouseId);
96
 
97
			totalInventoryList.addAll(serializedInventoryList);
98
			totalInventoryList.addAll(nonSerializedInventoryList);
99
 
14753 manish.sha 100
			Map<Long, InventoryAvailability> totalInventoryMap = new HashMap<Long, InventoryAvailability>();
101
 
14751 manish.sha 102
			for(InventoryAvailability availability : totalInventoryList){
14754 manish.sha 103
				totalInventoryMap.put(availability.getItemId(), availability);
104
			}
105
 
106
			for(InventoryAvailability availability : totalInventoryList){
14751 manish.sha 107
				long totalInventoryAsCatalog = 0;
14754 manish.sha 108
				if(itemInventoryMap.containsKey(availability.getItemId())){
109
					ItemInventory itemInvObj = itemInventoryMap.get(availability.getItemId());
14753 manish.sha 110
					Map<Long, Long> itemAvailibility = itemInvObj.getAvailability();
111
					for(Long whId : itemAvailibility.keySet()){
112
						Warehouse warehouse = allWarehousesMap.get(whId);
113
						if(warehouse.getBillingType() == BillingType.OURS && warehouse.getBillingWarehouseId()==physicalWarehouseId &&
114
						   warehouse.getInventoryType()==InventoryType.GOOD && warehouse.getWarehouseType() == WarehouseType.OURS){
115
							totalInventoryAsCatalog = totalInventoryAsCatalog + itemAvailibility.get(whId);
116
						}
14751 manish.sha 117
					}
14753 manish.sha 118
					InventoryMismatchUnit invMismatchUnit = new InventoryMismatchUnit();
119
					invMismatchUnit.setItemId(availability.getItemId());
120
					invMismatchUnit.setBrand(availability.getBrand());
121
					invMismatchUnit.setModelName(availability.getModelName());
122
					invMismatchUnit.setModelNumber(availability.getModelNumber());
123
					invMismatchUnit.setColor(availability.getColor());
124
					invMismatchUnit.setQuantityAsPerScans(availability.getQuantity());
125
					invMismatchUnit.setQuantityAsPerCatalog(totalInventoryAsCatalog);
14755 manish.sha 126
					invMismatchUnit.setPhysicalWarehouseId(physicalWarehouseId);
14753 manish.sha 127
					inventoryMismatches.add(invMismatchUnit);
128
				}else{
129
					InventoryMismatchUnit invMismatchUnit = new InventoryMismatchUnit();
130
					invMismatchUnit.setItemId(availability.getItemId());
131
					invMismatchUnit.setBrand(availability.getBrand());
132
					invMismatchUnit.setModelName(availability.getModelName());
133
					invMismatchUnit.setModelNumber(availability.getModelNumber());
134
					invMismatchUnit.setColor(availability.getColor());
135
					invMismatchUnit.setQuantityAsPerScans(availability.getQuantity());
136
					invMismatchUnit.setQuantityAsPerCatalog(totalInventoryAsCatalog);
14755 manish.sha 137
					invMismatchUnit.setPhysicalWarehouseId(physicalWarehouseId);
14753 manish.sha 138
					inventoryMismatches.add(invMismatchUnit);
14751 manish.sha 139
				}
140
			}
141
 
14754 manish.sha 142
			for(Long itemId : itemInventoryMap.keySet()){
143
				Item it = null;
144
				if(!totalInventoryMap.containsKey(itemId)){
145
					if(allAliveItemsMap.containsKey(itemId)){
146
						it = allAliveItemsMap.get(itemId);
147
					}else{
148
						continue;
149
					}
150
				}else{
151
					continue;
152
				}
153
				long totalInventoryAsCatalog = 0;
154
				ItemInventory itemInvObj = itemInventoryMap.get(itemId);
155
				Map<Long, Long> itemAvailibility = itemInvObj.getAvailability();
156
				for(Long whId : itemAvailibility.keySet()){
157
					Warehouse warehouse = allWarehousesMap.get(whId);
158
					if(warehouse.getBillingType() == BillingType.OURS && warehouse.getBillingWarehouseId()==physicalWarehouseId &&
159
					   warehouse.getInventoryType()==InventoryType.GOOD && warehouse.getWarehouseType() == WarehouseType.OURS){
160
						totalInventoryAsCatalog = totalInventoryAsCatalog + itemAvailibility.get(whId);
161
					}
162
				}
163
				InventoryMismatchUnit invMismatchUnit = new InventoryMismatchUnit();
164
				invMismatchUnit.setItemId(it.getId());
165
				invMismatchUnit.setBrand(it.getBrand());
166
				invMismatchUnit.setModelName(it.getModelName());
167
				invMismatchUnit.setModelNumber(it.getModelNumber());
168
				invMismatchUnit.setColor(it.getColor());
169
				invMismatchUnit.setQuantityAsPerScans(0);
170
				invMismatchUnit.setQuantityAsPerCatalog(totalInventoryAsCatalog);
14755 manish.sha 171
				invMismatchUnit.setPhysicalWarehouseId(physicalWarehouseId);
172
				inventoryMismatches.add(invMismatchUnit);				
14754 manish.sha 173
			}
174
 
14751 manish.sha 175
		}
176
 
177
		/*
178
 
179
 
180
 
6484 amar.kumar 181
		List<Map<String, Integer>> serializedInventorybyScansinList = inventoryItemhandler.getCurrentSerializedInventory();
182
		List<Map<String, Integer>> unSerializedInventorybyScansinList = inventoryItemhandler.getCurrentNonSerializedInventory();
14751 manish.sha 183
 
184
 
6484 amar.kumar 185
 
186
		Map<Long, Long> itemAvailabilityByScans = new HashMap<Long, Long>();
187
 
188
		for (Map<String,Integer> inventoryForItem : serializedInventorybyScansinList) {
189
			Object sumObj = inventoryForItem.get("sum");
190
			String sumString = sumObj.toString();
191
			Long availability = Long.parseLong(sumString);
192
			itemAvailabilityByScans.put(Long.parseLong(inventoryForItem.get("itemId").toString()), availability);
193
		}
194
 
195
		for (Map<String,Integer> inventoryForItem : unSerializedInventorybyScansinList) {
196
			Object sumObj = inventoryForItem.get("sum");
197
			String sumString = sumObj.toString();
198
			Long availability = Long.parseLong(sumString);
199
			itemAvailabilityByScans.put(Long.parseLong(inventoryForItem.get("itemId").toString()), availability);
200
		}
201
 
202
 
203
 
204
		Map<Long, Long> itemAvailabilitiesOnSite = null;
205
 
206
		try {
207
			in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = new CatalogClient().getClient();
6493 amar.kumar 208
			List<Item> activeItems = 	catalogClient.getAllItems(false);
6484 amar.kumar 209
			List<Long> itemIds = new ArrayList<Long>();
210
			for(Item item : activeItems) {
211
				itemIds.add(item.getId());
212
			}
14751 manish.sha 213
			 Client inventoryClient = new InventoryClient().getClient();
214
			itemAvailabilitiesOnSite = inventoryClient.getItemAvailabilitiesAtOurWarehouses(itemIds); 
6484 amar.kumar 215
		} catch (TException e) {
216
			e.printStackTrace();
217
			return;
218
		} catch (CatalogServiceException e) {
219
			// TODO Auto-generated catch block
220
			e.printStackTrace();
221
		}
222
 
223
 
224
		for( Long itemId : itemAvailabilitiesOnSite.keySet()) {
225
			if(itemAvailabilityByScans.containsKey(itemId)) {
226
				if(itemAvailabilitiesOnSite.get(itemId)!=itemAvailabilityByScans.get(itemId)) {
227
					Long[] mismatch = new Long[2];
228
					mismatch[0] = itemAvailabilityByScans.get(itemId);
229
					mismatch[1] = itemAvailabilitiesOnSite.get(itemId);
230
					availabilityMismatchMap.put(itemId, mismatch);
231
				}
232
			} else  {
233
				if(itemAvailabilitiesOnSite.get(itemId)!=0) {
234
					Long[] mismatch = new Long[2];
235
					mismatch[0] = 0L;
236
					mismatch[1] = itemAvailabilitiesOnSite.get(itemId);
237
					availabilityMismatchMap.put(itemId, mismatch);
238
				}
239
			}
240
		}
241
 
242
		for( Long itemId : itemAvailabilityByScans.keySet()) {
243
			if(!availabilityMismatchMap.containsKey(itemId)) {
244
				if(itemAvailabilitiesOnSite.containsKey(itemId)) {
245
					if(itemAvailabilitiesOnSite.get(itemId)!=0 && (itemAvailabilitiesOnSite.get(itemId)!=itemAvailabilityByScans.get(itemId))) {
246
						Long[] mismatch = new Long[2];
247
						mismatch[0] = itemAvailabilityByScans.get(itemId);
248
						mismatch[1] = itemAvailabilitiesOnSite.get(itemId);
249
						availabilityMismatchMap.put(itemId, mismatch);
250
					}
9849 amar.kumar 251
				} else {
6484 amar.kumar 252
					if(itemAvailabilityByScans.get(itemId)!=0) {
253
						Long[] mismatch = new Long[2];
254
						mismatch[0] = itemAvailabilityByScans.get(itemId);
255
						mismatch[1] = 0L;
256
						availabilityMismatchMap.put(itemId, mismatch);
257
					}
258
				}
259
			}
14751 manish.sha 260
		}*/
261
		int mismatchRecordsNumber = 0;
9851 amar.kumar 262
        //String subject = availabilityMismatchMap.size() + " mismatches in inventory compared to scan Records: ";
6484 amar.kumar 263
        File file = new File("inv_mismatches.xls");
264
 
265
        try {
266
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
267
            bufferedWriter.write(StringUtils.join(new String[] { "Item Id",
268
                    "Brand", "Model Name", "Model Number",
14755 manish.sha 269
                    "Color", "Inventory shown", "Inventory through Scans", "Physical Warehouse Id" }, '\t'));
14751 manish.sha 270
            for (InventoryMismatchUnit invUnit : inventoryMismatches) {
271
            	if(invUnit.getQuantityAsPerScans()==invUnit.getQuantityAsPerCatalog()){
9217 amar.kumar 272
            		continue;
273
            	}
6484 amar.kumar 274
                bufferedWriter.newLine();
14751 manish.sha 275
                bufferedWriter.write(StringUtils.join(new String[] { String.valueOf(invUnit.getItemId()), invUnit.getBrand(), invUnit.getModelName(),
14755 manish.sha 276
                		invUnit.getModelNumber(), invUnit.getColor(),invUnit.getQuantityAsPerCatalog()+"", invUnit.getQuantityAsPerScans()+"", invUnit.getPhysicalWarehouseId()+"" }, "\t"));
14751 manish.sha 277
                mismatchRecordsNumber++;
6484 amar.kumar 278
            }
279
            bufferedWriter.close();
280
        } catch (IOException e) {
14751 manish.sha 281
 
6484 amar.kumar 282
        }
9851 amar.kumar 283
        String subject = mismatchRecordsNumber + " mismatches in inventory compared to scan Records: ";
6484 amar.kumar 284
        GmailUtils g = new GmailUtils();
22657 amit.gupta 285
        g.sendSSLMessage(new String[]{ "deena.nath@profitmandi.com","amit.gupta@shop2020.in"/*,"rajveer.singh@shop2020.in"*/ }, subject, 
14751 manish.sha 286
                "", "adwords@shop2020.in", "adwords_shop2020", file.getAbsolutePath());
6484 amar.kumar 287
	}
288
}