| 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 |
|
| 27374 |
amit.gupta |
77 |
List<Long> PHYSICAL_WAREHOUSE_IDS = Arrays.asList(7573L, 7681L, 7678L, 8468L, 7720L, 8889L);
|
| 14751 |
manish.sha |
78 |
Map<Long, Warehouse> allWarehousesMap = new HashMap<Long, Warehouse>();
|
|
|
79 |
|
|
|
80 |
List<InventoryAvailability> totalInventoryList;
|
|
|
81 |
|
|
|
82 |
for(Warehouse wh : allWarehouses){
|
|
|
83 |
allWarehousesMap.put(wh.getId(), wh);
|
|
|
84 |
}
|
|
|
85 |
|
| 14754 |
manish.sha |
86 |
for(Item item: allAliveItems){
|
|
|
87 |
allAliveItemsMap.put(item.getId(), item);
|
|
|
88 |
}
|
|
|
89 |
|
| 14751 |
manish.sha |
90 |
for(Long physicalWarehouseId : PHYSICAL_WAREHOUSE_IDS){
|
|
|
91 |
totalInventoryList = new ArrayList<InventoryAvailability>();
|
|
|
92 |
|
|
|
93 |
List<InventoryAvailability> serializedInventoryList = scanHandler.getCurrentSerializedInventoryByScans(physicalWarehouseId);
|
|
|
94 |
List<InventoryAvailability> nonSerializedInventoryList = scanHandler.getCurrentNonSerializedInventoryByScans(physicalWarehouseId);
|
|
|
95 |
|
|
|
96 |
totalInventoryList.addAll(serializedInventoryList);
|
|
|
97 |
totalInventoryList.addAll(nonSerializedInventoryList);
|
|
|
98 |
|
| 14753 |
manish.sha |
99 |
Map<Long, InventoryAvailability> totalInventoryMap = new HashMap<Long, InventoryAvailability>();
|
|
|
100 |
|
| 14751 |
manish.sha |
101 |
for(InventoryAvailability availability : totalInventoryList){
|
| 14754 |
manish.sha |
102 |
totalInventoryMap.put(availability.getItemId(), availability);
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
for(InventoryAvailability availability : totalInventoryList){
|
| 14751 |
manish.sha |
106 |
long totalInventoryAsCatalog = 0;
|
| 14754 |
manish.sha |
107 |
if(itemInventoryMap.containsKey(availability.getItemId())){
|
|
|
108 |
ItemInventory itemInvObj = itemInventoryMap.get(availability.getItemId());
|
| 14753 |
manish.sha |
109 |
Map<Long, Long> itemAvailibility = itemInvObj.getAvailability();
|
|
|
110 |
for(Long whId : itemAvailibility.keySet()){
|
|
|
111 |
Warehouse warehouse = allWarehousesMap.get(whId);
|
|
|
112 |
if(warehouse.getBillingType() == BillingType.OURS && warehouse.getBillingWarehouseId()==physicalWarehouseId &&
|
|
|
113 |
warehouse.getInventoryType()==InventoryType.GOOD && warehouse.getWarehouseType() == WarehouseType.OURS){
|
|
|
114 |
totalInventoryAsCatalog = totalInventoryAsCatalog + itemAvailibility.get(whId);
|
|
|
115 |
}
|
| 14751 |
manish.sha |
116 |
}
|
| 14753 |
manish.sha |
117 |
InventoryMismatchUnit invMismatchUnit = new InventoryMismatchUnit();
|
|
|
118 |
invMismatchUnit.setItemId(availability.getItemId());
|
|
|
119 |
invMismatchUnit.setBrand(availability.getBrand());
|
|
|
120 |
invMismatchUnit.setModelName(availability.getModelName());
|
|
|
121 |
invMismatchUnit.setModelNumber(availability.getModelNumber());
|
|
|
122 |
invMismatchUnit.setColor(availability.getColor());
|
|
|
123 |
invMismatchUnit.setQuantityAsPerScans(availability.getQuantity());
|
|
|
124 |
invMismatchUnit.setQuantityAsPerCatalog(totalInventoryAsCatalog);
|
| 14755 |
manish.sha |
125 |
invMismatchUnit.setPhysicalWarehouseId(physicalWarehouseId);
|
| 14753 |
manish.sha |
126 |
inventoryMismatches.add(invMismatchUnit);
|
|
|
127 |
}else{
|
|
|
128 |
InventoryMismatchUnit invMismatchUnit = new InventoryMismatchUnit();
|
|
|
129 |
invMismatchUnit.setItemId(availability.getItemId());
|
|
|
130 |
invMismatchUnit.setBrand(availability.getBrand());
|
|
|
131 |
invMismatchUnit.setModelName(availability.getModelName());
|
|
|
132 |
invMismatchUnit.setModelNumber(availability.getModelNumber());
|
|
|
133 |
invMismatchUnit.setColor(availability.getColor());
|
|
|
134 |
invMismatchUnit.setQuantityAsPerScans(availability.getQuantity());
|
|
|
135 |
invMismatchUnit.setQuantityAsPerCatalog(totalInventoryAsCatalog);
|
| 14755 |
manish.sha |
136 |
invMismatchUnit.setPhysicalWarehouseId(physicalWarehouseId);
|
| 14753 |
manish.sha |
137 |
inventoryMismatches.add(invMismatchUnit);
|
| 14751 |
manish.sha |
138 |
}
|
|
|
139 |
}
|
|
|
140 |
|
| 14754 |
manish.sha |
141 |
for(Long itemId : itemInventoryMap.keySet()){
|
|
|
142 |
Item it = null;
|
|
|
143 |
if(!totalInventoryMap.containsKey(itemId)){
|
|
|
144 |
if(allAliveItemsMap.containsKey(itemId)){
|
|
|
145 |
it = allAliveItemsMap.get(itemId);
|
|
|
146 |
}else{
|
|
|
147 |
continue;
|
|
|
148 |
}
|
|
|
149 |
}else{
|
|
|
150 |
continue;
|
|
|
151 |
}
|
|
|
152 |
long totalInventoryAsCatalog = 0;
|
|
|
153 |
ItemInventory itemInvObj = itemInventoryMap.get(itemId);
|
|
|
154 |
Map<Long, Long> itemAvailibility = itemInvObj.getAvailability();
|
|
|
155 |
for(Long whId : itemAvailibility.keySet()){
|
|
|
156 |
Warehouse warehouse = allWarehousesMap.get(whId);
|
|
|
157 |
if(warehouse.getBillingType() == BillingType.OURS && warehouse.getBillingWarehouseId()==physicalWarehouseId &&
|
|
|
158 |
warehouse.getInventoryType()==InventoryType.GOOD && warehouse.getWarehouseType() == WarehouseType.OURS){
|
|
|
159 |
totalInventoryAsCatalog = totalInventoryAsCatalog + itemAvailibility.get(whId);
|
|
|
160 |
}
|
|
|
161 |
}
|
|
|
162 |
InventoryMismatchUnit invMismatchUnit = new InventoryMismatchUnit();
|
|
|
163 |
invMismatchUnit.setItemId(it.getId());
|
|
|
164 |
invMismatchUnit.setBrand(it.getBrand());
|
|
|
165 |
invMismatchUnit.setModelName(it.getModelName());
|
|
|
166 |
invMismatchUnit.setModelNumber(it.getModelNumber());
|
|
|
167 |
invMismatchUnit.setColor(it.getColor());
|
|
|
168 |
invMismatchUnit.setQuantityAsPerScans(0);
|
|
|
169 |
invMismatchUnit.setQuantityAsPerCatalog(totalInventoryAsCatalog);
|
| 14755 |
manish.sha |
170 |
invMismatchUnit.setPhysicalWarehouseId(physicalWarehouseId);
|
|
|
171 |
inventoryMismatches.add(invMismatchUnit);
|
| 14754 |
manish.sha |
172 |
}
|
|
|
173 |
|
| 14751 |
manish.sha |
174 |
}
|
|
|
175 |
|
|
|
176 |
/*
|
|
|
177 |
|
|
|
178 |
|
|
|
179 |
|
| 6484 |
amar.kumar |
180 |
List<Map<String, Integer>> serializedInventorybyScansinList = inventoryItemhandler.getCurrentSerializedInventory();
|
|
|
181 |
List<Map<String, Integer>> unSerializedInventorybyScansinList = inventoryItemhandler.getCurrentNonSerializedInventory();
|
| 14751 |
manish.sha |
182 |
|
|
|
183 |
|
| 6484 |
amar.kumar |
184 |
|
|
|
185 |
Map<Long, Long> itemAvailabilityByScans = new HashMap<Long, Long>();
|
|
|
186 |
|
|
|
187 |
for (Map<String,Integer> inventoryForItem : serializedInventorybyScansinList) {
|
|
|
188 |
Object sumObj = inventoryForItem.get("sum");
|
|
|
189 |
String sumString = sumObj.toString();
|
|
|
190 |
Long availability = Long.parseLong(sumString);
|
|
|
191 |
itemAvailabilityByScans.put(Long.parseLong(inventoryForItem.get("itemId").toString()), availability);
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
for (Map<String,Integer> inventoryForItem : unSerializedInventorybyScansinList) {
|
|
|
195 |
Object sumObj = inventoryForItem.get("sum");
|
|
|
196 |
String sumString = sumObj.toString();
|
|
|
197 |
Long availability = Long.parseLong(sumString);
|
|
|
198 |
itemAvailabilityByScans.put(Long.parseLong(inventoryForItem.get("itemId").toString()), availability);
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
|
|
|
202 |
|
|
|
203 |
Map<Long, Long> itemAvailabilitiesOnSite = null;
|
|
|
204 |
|
|
|
205 |
try {
|
|
|
206 |
in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = new CatalogClient().getClient();
|
| 6493 |
amar.kumar |
207 |
List<Item> activeItems = catalogClient.getAllItems(false);
|
| 6484 |
amar.kumar |
208 |
List<Long> itemIds = new ArrayList<Long>();
|
|
|
209 |
for(Item item : activeItems) {
|
|
|
210 |
itemIds.add(item.getId());
|
|
|
211 |
}
|
| 14751 |
manish.sha |
212 |
Client inventoryClient = new InventoryClient().getClient();
|
|
|
213 |
itemAvailabilitiesOnSite = inventoryClient.getItemAvailabilitiesAtOurWarehouses(itemIds);
|
| 6484 |
amar.kumar |
214 |
} catch (TException e) {
|
|
|
215 |
e.printStackTrace();
|
|
|
216 |
return;
|
|
|
217 |
} catch (CatalogServiceException e) {
|
|
|
218 |
// TODO Auto-generated catch block
|
|
|
219 |
e.printStackTrace();
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
|
|
|
223 |
for( Long itemId : itemAvailabilitiesOnSite.keySet()) {
|
|
|
224 |
if(itemAvailabilityByScans.containsKey(itemId)) {
|
|
|
225 |
if(itemAvailabilitiesOnSite.get(itemId)!=itemAvailabilityByScans.get(itemId)) {
|
|
|
226 |
Long[] mismatch = new Long[2];
|
|
|
227 |
mismatch[0] = itemAvailabilityByScans.get(itemId);
|
|
|
228 |
mismatch[1] = itemAvailabilitiesOnSite.get(itemId);
|
|
|
229 |
availabilityMismatchMap.put(itemId, mismatch);
|
|
|
230 |
}
|
|
|
231 |
} else {
|
|
|
232 |
if(itemAvailabilitiesOnSite.get(itemId)!=0) {
|
|
|
233 |
Long[] mismatch = new Long[2];
|
|
|
234 |
mismatch[0] = 0L;
|
|
|
235 |
mismatch[1] = itemAvailabilitiesOnSite.get(itemId);
|
|
|
236 |
availabilityMismatchMap.put(itemId, mismatch);
|
|
|
237 |
}
|
|
|
238 |
}
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
for( Long itemId : itemAvailabilityByScans.keySet()) {
|
|
|
242 |
if(!availabilityMismatchMap.containsKey(itemId)) {
|
|
|
243 |
if(itemAvailabilitiesOnSite.containsKey(itemId)) {
|
|
|
244 |
if(itemAvailabilitiesOnSite.get(itemId)!=0 && (itemAvailabilitiesOnSite.get(itemId)!=itemAvailabilityByScans.get(itemId))) {
|
|
|
245 |
Long[] mismatch = new Long[2];
|
|
|
246 |
mismatch[0] = itemAvailabilityByScans.get(itemId);
|
|
|
247 |
mismatch[1] = itemAvailabilitiesOnSite.get(itemId);
|
|
|
248 |
availabilityMismatchMap.put(itemId, mismatch);
|
|
|
249 |
}
|
| 9849 |
amar.kumar |
250 |
} else {
|
| 6484 |
amar.kumar |
251 |
if(itemAvailabilityByScans.get(itemId)!=0) {
|
|
|
252 |
Long[] mismatch = new Long[2];
|
|
|
253 |
mismatch[0] = itemAvailabilityByScans.get(itemId);
|
|
|
254 |
mismatch[1] = 0L;
|
|
|
255 |
availabilityMismatchMap.put(itemId, mismatch);
|
|
|
256 |
}
|
|
|
257 |
}
|
|
|
258 |
}
|
| 14751 |
manish.sha |
259 |
}*/
|
|
|
260 |
int mismatchRecordsNumber = 0;
|
| 9851 |
amar.kumar |
261 |
//String subject = availabilityMismatchMap.size() + " mismatches in inventory compared to scan Records: ";
|
| 6484 |
amar.kumar |
262 |
File file = new File("inv_mismatches.xls");
|
|
|
263 |
|
|
|
264 |
try {
|
|
|
265 |
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
|
|
|
266 |
bufferedWriter.write(StringUtils.join(new String[] { "Item Id",
|
|
|
267 |
"Brand", "Model Name", "Model Number",
|
| 14755 |
manish.sha |
268 |
"Color", "Inventory shown", "Inventory through Scans", "Physical Warehouse Id" }, '\t'));
|
| 14751 |
manish.sha |
269 |
for (InventoryMismatchUnit invUnit : inventoryMismatches) {
|
|
|
270 |
if(invUnit.getQuantityAsPerScans()==invUnit.getQuantityAsPerCatalog()){
|
| 9217 |
amar.kumar |
271 |
continue;
|
|
|
272 |
}
|
| 6484 |
amar.kumar |
273 |
bufferedWriter.newLine();
|
| 14751 |
manish.sha |
274 |
bufferedWriter.write(StringUtils.join(new String[] { String.valueOf(invUnit.getItemId()), invUnit.getBrand(), invUnit.getModelName(),
|
| 14755 |
manish.sha |
275 |
invUnit.getModelNumber(), invUnit.getColor(),invUnit.getQuantityAsPerCatalog()+"", invUnit.getQuantityAsPerScans()+"", invUnit.getPhysicalWarehouseId()+"" }, "\t"));
|
| 14751 |
manish.sha |
276 |
mismatchRecordsNumber++;
|
| 6484 |
amar.kumar |
277 |
}
|
|
|
278 |
bufferedWriter.close();
|
|
|
279 |
} catch (IOException e) {
|
| 14751 |
manish.sha |
280 |
|
| 6484 |
amar.kumar |
281 |
}
|
| 9851 |
amar.kumar |
282 |
String subject = mismatchRecordsNumber + " mismatches in inventory compared to scan Records: ";
|
| 6484 |
amar.kumar |
283 |
GmailUtils g = new GmailUtils();
|
| 25983 |
amit.gupta |
284 |
g.sendSSLMessage(new String[]{ "sunny.yadav@smartdukaan.com","amit.gupta@shop2020.in"/*,"rajveer.singh@shop2020.in"*/ }, subject,
|
| 14751 |
manish.sha |
285 |
"", "adwords@shop2020.in", "adwords_shop2020", file.getAbsolutePath());
|
| 6484 |
amar.kumar |
286 |
}
|
|
|
287 |
}
|