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