Subversion Repositories SmartDukaan

Rev

Rev 10330 | Rev 14753 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 10330 Rev 14751
Line 1... Line 1...
1
package in.shop2020.warehouse.util;
1
package in.shop2020.warehouse.util;
2
 
2
 
-
 
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
 
3
import java.io.BufferedWriter;
15
import java.io.BufferedWriter;
4
import java.io.File;
16
import java.io.File;
5
import java.io.FileWriter;
17
import java.io.FileWriter;
6
import java.io.IOException;
18
import java.io.IOException;
7
import java.math.BigDecimal;
-
 
8
import java.util.ArrayList;
19
import java.util.ArrayList;
-
 
20
import java.util.Arrays;
9
import java.util.HashMap;
21
import java.util.HashMap;
10
import java.util.List;
22
import java.util.List;
11
import java.util.Map;
23
import java.util.Map;
12
 
24
 
13
import javax.mail.MessagingException;
25
import javax.mail.MessagingException;
14
 
26
 
15
import org.apache.commons.lang.StringUtils;
27
import org.apache.commons.lang.StringUtils;
16
import org.apache.thrift.TException;
28
import org.apache.thrift.TException;
17
import org.apache.thrift.transport.TTransportException;
-
 
18
import org.springframework.context.ApplicationContext;
29
import org.springframework.context.ApplicationContext;
19
import org.springframework.context.support.ClassPathXmlApplicationContext;
30
import org.springframework.context.support.ClassPathXmlApplicationContext;
20
 
31
 
21
import in.shop2020.model.v1.catalog.CatalogServiceException;
-
 
22
import in.shop2020.model.v1.catalog.Item;
-
 
23
import in.shop2020.model.v1.inventory.InventoryService.Client;
-
 
24
import in.shop2020.thrift.clients.CatalogClient;
-
 
25
import in.shop2020.thrift.clients.InventoryClient;
-
 
26
import in.shop2020.utils.GmailUtils;
-
 
27
import in.shop2020.warehouse.handler.InventoryItemHandler;
-
 
28
 
-
 
29
public class InventoryMismatchGenerator {
32
public class InventoryMismatchGenerator {
30
 
33
 
31
	private InventoryItemHandler inventoryItemhandler;
34
	private ScanHandler scanHandler;
-
 
35
	
32
	/**
36
	/**
33
	 * @param args
37
	 * @param args
34
	 */
38
	 */
35
	
39
	
36
	public InventoryMismatchGenerator() {
40
	public InventoryMismatchGenerator() {
37
		ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
41
		ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
38
		inventoryItemhandler = context.getBean(InventoryItemHandler.class);
42
		scanHandler = context.getBean(ScanHandler.class);
39
	}
43
	}
40
       
44
       
41
	public static void main(String[] args) {
45
	public static void main(String[] args) {
42
		InventoryMismatchGenerator mismatchGenerator = new InventoryMismatchGenerator();
46
		InventoryMismatchGenerator mismatchGenerator = new InventoryMismatchGenerator();
43
		try {
47
		try {
44
			mismatchGenerator.sendMailForMismatches();
48
			mismatchGenerator.sendMailForMismatches();
45
		} catch (MessagingException e) {
49
		} catch (Exception e) {
46
			// TODO Auto-generated catch block
50
			// TODO Auto-generated catch block
47
			e.printStackTrace();
51
			e.printStackTrace();
48
		}
52
		}
49
	}
53
	}
50
 
54
 
51
	public void sendMailForMismatches() throws MessagingException {
55
	public void sendMailForMismatches() throws MessagingException, TException, InventoryServiceException {
-
 
56
 
-
 
57
		List<InventoryMismatchUnit> inventoryMismatches = new ArrayList<InventoryMismatchUnit>();
-
 
58
		
-
 
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);
52
		Map<Long, Long[]> availabilityMismatchMap = new HashMap<Long, Long[]>();
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
			
-
 
84
			for(InventoryAvailability availability : totalInventoryList){
-
 
85
				ItemInventory itemInvObj = itemInventoryMap.get(availability.getItemId());
-
 
86
				Map<Long, Long> itemAvailibility = itemInvObj.getAvailability();
-
 
87
				long totalInventoryAsCatalog = 0;
-
 
88
				for(Long whId : itemAvailibility.keySet()){
-
 
89
					Warehouse warehouse = allWarehousesMap.get(whId);
-
 
90
					if(warehouse.getBillingType() == BillingType.OURS && warehouse.getBillingWarehouseId()==physicalWarehouseId &&
-
 
91
					   warehouse.getInventoryType()==InventoryType.GOOD && warehouse.getWarehouseType() == WarehouseType.OURS){
-
 
92
						totalInventoryAsCatalog = totalInventoryAsCatalog + itemAvailibility.get(whId);
-
 
93
					}
-
 
94
				}
-
 
95
				InventoryMismatchUnit invMismatchUnit = new InventoryMismatchUnit();
-
 
96
				invMismatchUnit.setItemId(availability.getItemId());
-
 
97
				invMismatchUnit.setBrand(availability.getBrand());
-
 
98
				invMismatchUnit.setModelName(availability.getModelName());
-
 
99
				invMismatchUnit.setModelNumber(availability.getModelNumber());
-
 
100
				invMismatchUnit.setColor(availability.getColor());
-
 
101
				invMismatchUnit.setQuantityAsPerScans(availability.getQuantity());
-
 
102
				invMismatchUnit.setQuantityAsPerCatalog(totalInventoryAsCatalog);
-
 
103
				inventoryMismatches.add(invMismatchUnit);
-
 
104
			}
-
 
105
			
-
 
106
		}
-
 
107
		
-
 
108
		/*
-
 
109
		
-
 
110
		
53
		
111
		
54
		List<Map<String, Integer>> serializedInventorybyScansinList = inventoryItemhandler.getCurrentSerializedInventory();
112
		List<Map<String, Integer>> serializedInventorybyScansinList = inventoryItemhandler.getCurrentSerializedInventory();
55
		List<Map<String, Integer>> unSerializedInventorybyScansinList = inventoryItemhandler.getCurrentNonSerializedInventory();
113
		List<Map<String, Integer>> unSerializedInventorybyScansinList = inventoryItemhandler.getCurrentNonSerializedInventory();
-
 
114
		
-
 
115
		
56
	
116
	
57
		Map<Long, Long> itemAvailabilityByScans = new HashMap<Long, Long>();
117
		Map<Long, Long> itemAvailabilityByScans = new HashMap<Long, Long>();
58
		
118
		
59
		for (Map<String,Integer> inventoryForItem : serializedInventorybyScansinList) {
119
		for (Map<String,Integer> inventoryForItem : serializedInventorybyScansinList) {
60
			Object sumObj = inventoryForItem.get("sum");
120
			Object sumObj = inventoryForItem.get("sum");
Line 79... Line 139...
79
			List<Item> activeItems = 	catalogClient.getAllItems(false);
139
			List<Item> activeItems = 	catalogClient.getAllItems(false);
80
			List<Long> itemIds = new ArrayList<Long>();
140
			List<Long> itemIds = new ArrayList<Long>();
81
			for(Item item : activeItems) {
141
			for(Item item : activeItems) {
82
				itemIds.add(item.getId());
142
				itemIds.add(item.getId());
83
			}
143
			}
84
			Client inventoryClient = new InventoryClient().getClient();
144
			 Client inventoryClient = new InventoryClient().getClient();
85
			itemAvailabilitiesOnSite = inventoryClient.getItemAvailabilitiesAtOurWarehouses(itemIds);
145
			itemAvailabilitiesOnSite = inventoryClient.getItemAvailabilitiesAtOurWarehouses(itemIds); 
86
		} catch (TException e) {
146
		} catch (TException e) {
87
			e.printStackTrace();
147
			e.printStackTrace();
88
			return;
148
			return;
89
		} catch (CatalogServiceException e) {
149
		} catch (CatalogServiceException e) {
90
			// TODO Auto-generated catch block
150
			// TODO Auto-generated catch block
Line 126... Line 186...
126
						mismatch[1] = 0L;
186
						mismatch[1] = 0L;
127
						availabilityMismatchMap.put(itemId, mismatch);
187
						availabilityMismatchMap.put(itemId, mismatch);
128
					}
188
					}
129
				}
189
				}
130
			}
190
			}
131
		}
191
		}*/
132
		int mismatchRecordsNumber = availabilityMismatchMap.size();
192
		int mismatchRecordsNumber = 0;
133
        //String subject = availabilityMismatchMap.size() + " mismatches in inventory compared to scan Records: ";
193
        //String subject = availabilityMismatchMap.size() + " mismatches in inventory compared to scan Records: ";
134
        File file = new File("inv_mismatches.xls");
194
        File file = new File("inv_mismatches.xls");
135
 
195
 
136
        try {
196
        try {
137
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
197
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
138
            bufferedWriter.write(StringUtils.join(new String[] { "Item Id",
198
            bufferedWriter.write(StringUtils.join(new String[] { "Item Id",
139
                    "Brand", "Model Name", "Model Number",
199
                    "Brand", "Model Name", "Model Number",
140
                    "Color", "Inventory shown", "Inventory through Scans" }, '\t'));
200
                    "Color", "Inventory shown", "Inventory through Scans" }, '\t'));
141
            for (Long itemId : availabilityMismatchMap.keySet()) {
201
            for (InventoryMismatchUnit invUnit : inventoryMismatches) {
142
            	if(availabilityMismatchMap.get(itemId)[0].equals(availabilityMismatchMap.get(itemId)[1])){
202
            	if(invUnit.getQuantityAsPerScans()==invUnit.getQuantityAsPerCatalog()){
143
            		mismatchRecordsNumber--;
-
 
144
            		continue;
203
            		continue;
145
            	}
204
            	}
146
            	Item item = null;
-
 
147
            	try {
-
 
148
	            	in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = new CatalogClient().getClient();
-
 
149
	            	item = catalogClient.getItem(itemId);
-
 
150
				} catch (CatalogServiceException e) {
-
 
151
					// TODO Auto-generated catch block
-
 
152
					e.printStackTrace();
-
 
153
				} catch (TException e) {
-
 
154
					// TODO Auto-generated catch block
-
 
155
					e.printStackTrace();
-
 
156
				} 
-
 
157
                bufferedWriter.newLine();
205
                bufferedWriter.newLine();
158
                bufferedWriter.write(StringUtils.join(new String[] { String.valueOf(item.getId()), item.getBrand(), item.getModelName(),
206
                bufferedWriter.write(StringUtils.join(new String[] { String.valueOf(invUnit.getItemId()), invUnit.getBrand(), invUnit.getModelName(),
159
                        item.getModelNumber(), item.getColor(),availabilityMismatchMap.get(itemId)[1].toString(), availabilityMismatchMap.get(itemId)[0].toString() }, "\t"));
207
                		invUnit.getModelNumber(), invUnit.getColor(),invUnit.getQuantityAsPerCatalog()+"", invUnit.getQuantityAsPerScans()+"" }, "\t"));
-
 
208
                mismatchRecordsNumber++;
160
            }
209
            }
161
            bufferedWriter.close();
210
            bufferedWriter.close();
162
        } catch (IOException e) {
211
        } catch (IOException e) {
163
//TODO            logger.error("Could not write mismatches to file", e);
-
 
-
 
212
 
164
        }
213
        }
165
        String subject = mismatchRecordsNumber + " mismatches in inventory compared to scan Records: ";
214
        String subject = mismatchRecordsNumber + " mismatches in inventory compared to scan Records: ";
166
        GmailUtils g = new GmailUtils();
215
        GmailUtils g = new GmailUtils();
167
        g.sendSSLMessage(new String[]{ "manish.sharma@shop2020.in","sandeep.sachdeva@shop2020.in","rajveer.singh@shop2020.in" }, subject, 
216
        g.sendSSLMessage(new String[]{ "manish.sharma@shop2020.in"/*,"sandeep.sachdeva@shop2020.in","rajveer.singh@shop2020.in"*/ }, subject, 
168
                "", "cnc.center@shop2020.in", "5h0p2o2o", file.getAbsolutePath());
217
                "", "adwords@shop2020.in", "adwords_shop2020", file.getAbsolutePath());
169
	}
218
	}
170
}
219
}