Subversion Repositories SmartDukaan

Rev

Rev 27374 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.warehouse.util;

import in.shop2020.model.v1.catalog.CatalogService;
import in.shop2020.model.v1.catalog.Item;
import in.shop2020.model.v1.inventory.BillingType;
import in.shop2020.model.v1.inventory.InventoryService;
import in.shop2020.model.v1.inventory.InventoryServiceException;
import in.shop2020.model.v1.inventory.InventoryType;
import in.shop2020.model.v1.inventory.ItemInventory;
import in.shop2020.model.v1.inventory.Warehouse;
import in.shop2020.model.v1.inventory.WarehouseType;
import in.shop2020.thrift.clients.CatalogClient;
import in.shop2020.thrift.clients.InventoryClient;
import in.shop2020.utils.GmailUtils;
import in.shop2020.warehouse.InventoryAvailability;
import in.shop2020.warehouse.handler.ScanHandler;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.mail.MessagingException;

import org.apache.commons.lang.StringUtils;
import org.apache.thrift.TException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class InventoryMismatchGenerator {

        private ScanHandler scanHandler;
        
        /**
         * @param args
         */
        
        public InventoryMismatchGenerator() {
                ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
                scanHandler = context.getBean(ScanHandler.class);
        }
       
        public static void main(String[] args) {
                InventoryMismatchGenerator mismatchGenerator = new InventoryMismatchGenerator();
                try {
                        mismatchGenerator.sendMailForMismatches();
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }

        public void sendMailForMismatches() throws MessagingException, TException, InventoryServiceException {

                List<InventoryMismatchUnit> inventoryMismatches = new ArrayList<InventoryMismatchUnit>();
                
                InventoryClient inventoryClient = new InventoryClient();
                
                CatalogClient catalogClient = new CatalogClient();
                
                CatalogService.Client catalogServiceClient = catalogClient.getClient();
                
                InventoryService.Client invClient = inventoryClient.getClient();
                
                List<Item> allAliveItems = catalogServiceClient.getAllAliveItems();
                
                Map<Long, Item> allAliveItemsMap = new HashMap<Long, Item>();
                
                Map<Long, ItemInventory> itemInventoryMap = invClient.getInventorySnapshot(0);
                List<Warehouse> allWarehouses = invClient.getAllWarehouses(false);
                
                List<Long> PHYSICAL_WAREHOUSE_IDS = Arrays.asList(7573L, 7681L, 7678L, 8468L, 7720L, 8889L, 9203L, 9213L);
                Map<Long, Warehouse> allWarehousesMap = new HashMap<Long, Warehouse>();
                
                List<InventoryAvailability> totalInventoryList;
                
                for(Warehouse wh : allWarehouses){
                        allWarehousesMap.put(wh.getId(), wh);
                }
                
                for(Item item: allAliveItems){
                        allAliveItemsMap.put(item.getId(), item);
                }
                
                for(Long physicalWarehouseId : PHYSICAL_WAREHOUSE_IDS){
                        totalInventoryList = new ArrayList<InventoryAvailability>();
                        
                        List<InventoryAvailability> serializedInventoryList = scanHandler.getCurrentSerializedInventoryByScans(physicalWarehouseId);
                        List<InventoryAvailability> nonSerializedInventoryList = scanHandler.getCurrentNonSerializedInventoryByScans(physicalWarehouseId);
                        
                        totalInventoryList.addAll(serializedInventoryList);
                        totalInventoryList.addAll(nonSerializedInventoryList);
                        
                        Map<Long, InventoryAvailability> totalInventoryMap = new HashMap<Long, InventoryAvailability>();
                        
                        for(InventoryAvailability availability : totalInventoryList){
                                totalInventoryMap.put(availability.getItemId(), availability);
                        }
                        
                        for(InventoryAvailability availability : totalInventoryList){
                                long totalInventoryAsCatalog = 0;
                                if(itemInventoryMap.containsKey(availability.getItemId())){
                                        ItemInventory itemInvObj = itemInventoryMap.get(availability.getItemId());
                                        Map<Long, Long> itemAvailibility = itemInvObj.getAvailability();
                                        for(Long whId : itemAvailibility.keySet()){
                                                Warehouse warehouse = allWarehousesMap.get(whId);
                                                if(warehouse.getBillingType() == BillingType.OURS && warehouse.getBillingWarehouseId()==physicalWarehouseId &&
                                                   warehouse.getInventoryType()==InventoryType.GOOD && warehouse.getWarehouseType() == WarehouseType.OURS){
                                                        totalInventoryAsCatalog = totalInventoryAsCatalog + itemAvailibility.get(whId);
                                                }
                                        }
                                        InventoryMismatchUnit invMismatchUnit = new InventoryMismatchUnit();
                                        invMismatchUnit.setItemId(availability.getItemId());
                                        invMismatchUnit.setBrand(availability.getBrand());
                                        invMismatchUnit.setModelName(availability.getModelName());
                                        invMismatchUnit.setModelNumber(availability.getModelNumber());
                                        invMismatchUnit.setColor(availability.getColor());
                                        invMismatchUnit.setQuantityAsPerScans(availability.getQuantity());
                                        invMismatchUnit.setQuantityAsPerCatalog(totalInventoryAsCatalog);
                                        invMismatchUnit.setPhysicalWarehouseId(physicalWarehouseId);
                                        inventoryMismatches.add(invMismatchUnit);
                                }else{
                                        InventoryMismatchUnit invMismatchUnit = new InventoryMismatchUnit();
                                        invMismatchUnit.setItemId(availability.getItemId());
                                        invMismatchUnit.setBrand(availability.getBrand());
                                        invMismatchUnit.setModelName(availability.getModelName());
                                        invMismatchUnit.setModelNumber(availability.getModelNumber());
                                        invMismatchUnit.setColor(availability.getColor());
                                        invMismatchUnit.setQuantityAsPerScans(availability.getQuantity());
                                        invMismatchUnit.setQuantityAsPerCatalog(totalInventoryAsCatalog);
                                        invMismatchUnit.setPhysicalWarehouseId(physicalWarehouseId);
                                        inventoryMismatches.add(invMismatchUnit);
                                }
                        }
                        
                        for(Long itemId : itemInventoryMap.keySet()){
                                Item it = null;
                                if(!totalInventoryMap.containsKey(itemId)){
                                        if(allAliveItemsMap.containsKey(itemId)){
                                                it = allAliveItemsMap.get(itemId);
                                        }else{
                                                continue;
                                        }
                                }else{
                                        continue;
                                }
                                long totalInventoryAsCatalog = 0;
                                ItemInventory itemInvObj = itemInventoryMap.get(itemId);
                                Map<Long, Long> itemAvailibility = itemInvObj.getAvailability();
                                for(Long whId : itemAvailibility.keySet()){
                                        Warehouse warehouse = allWarehousesMap.get(whId);
                                        if(warehouse.getBillingType() == BillingType.OURS && warehouse.getBillingWarehouseId()==physicalWarehouseId &&
                                           warehouse.getInventoryType()==InventoryType.GOOD && warehouse.getWarehouseType() == WarehouseType.OURS){
                                                totalInventoryAsCatalog = totalInventoryAsCatalog + itemAvailibility.get(whId);
                                        }
                                }
                                InventoryMismatchUnit invMismatchUnit = new InventoryMismatchUnit();
                                invMismatchUnit.setItemId(it.getId());
                                invMismatchUnit.setBrand(it.getBrand());
                                invMismatchUnit.setModelName(it.getModelName());
                                invMismatchUnit.setModelNumber(it.getModelNumber());
                                invMismatchUnit.setColor(it.getColor());
                                invMismatchUnit.setQuantityAsPerScans(0);
                                invMismatchUnit.setQuantityAsPerCatalog(totalInventoryAsCatalog);
                                invMismatchUnit.setPhysicalWarehouseId(physicalWarehouseId);
                                inventoryMismatches.add(invMismatchUnit);                               
                        }
                        
                }
                
                /*
                
                
                
                List<Map<String, Integer>> serializedInventorybyScansinList = inventoryItemhandler.getCurrentSerializedInventory();
                List<Map<String, Integer>> unSerializedInventorybyScansinList = inventoryItemhandler.getCurrentNonSerializedInventory();
                
                
        
                Map<Long, Long> itemAvailabilityByScans = new HashMap<Long, Long>();
                
                for (Map<String,Integer> inventoryForItem : serializedInventorybyScansinList) {
                        Object sumObj = inventoryForItem.get("sum");
                        String sumString = sumObj.toString();
                        Long availability = Long.parseLong(sumString);
                        itemAvailabilityByScans.put(Long.parseLong(inventoryForItem.get("itemId").toString()), availability);
                }
                
                for (Map<String,Integer> inventoryForItem : unSerializedInventorybyScansinList) {
                        Object sumObj = inventoryForItem.get("sum");
                        String sumString = sumObj.toString();
                        Long availability = Long.parseLong(sumString);
                        itemAvailabilityByScans.put(Long.parseLong(inventoryForItem.get("itemId").toString()), availability);
                }
                
                
                
                Map<Long, Long> itemAvailabilitiesOnSite = null;
                
                try {
                        in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = new CatalogClient().getClient();
                        List<Item> activeItems =        catalogClient.getAllItems(false);
                        List<Long> itemIds = new ArrayList<Long>();
                        for(Item item : activeItems) {
                                itemIds.add(item.getId());
                        }
                         Client inventoryClient = new InventoryClient().getClient();
                        itemAvailabilitiesOnSite = inventoryClient.getItemAvailabilitiesAtOurWarehouses(itemIds); 
                } catch (TException e) {
                        e.printStackTrace();
                        return;
                } catch (CatalogServiceException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                

                for( Long itemId : itemAvailabilitiesOnSite.keySet()) {
                        if(itemAvailabilityByScans.containsKey(itemId)) {
                                if(itemAvailabilitiesOnSite.get(itemId)!=itemAvailabilityByScans.get(itemId)) {
                                        Long[] mismatch = new Long[2];
                                        mismatch[0] = itemAvailabilityByScans.get(itemId);
                                        mismatch[1] = itemAvailabilitiesOnSite.get(itemId);
                                        availabilityMismatchMap.put(itemId, mismatch);
                                }
                        } else  {
                                if(itemAvailabilitiesOnSite.get(itemId)!=0) {
                                        Long[] mismatch = new Long[2];
                                        mismatch[0] = 0L;
                                        mismatch[1] = itemAvailabilitiesOnSite.get(itemId);
                                        availabilityMismatchMap.put(itemId, mismatch);
                                }
                        }
                }
                
                for( Long itemId : itemAvailabilityByScans.keySet()) {
                        if(!availabilityMismatchMap.containsKey(itemId)) {
                                if(itemAvailabilitiesOnSite.containsKey(itemId)) {
                                        if(itemAvailabilitiesOnSite.get(itemId)!=0 && (itemAvailabilitiesOnSite.get(itemId)!=itemAvailabilityByScans.get(itemId))) {
                                                Long[] mismatch = new Long[2];
                                                mismatch[0] = itemAvailabilityByScans.get(itemId);
                                                mismatch[1] = itemAvailabilitiesOnSite.get(itemId);
                                                availabilityMismatchMap.put(itemId, mismatch);
                                        }
                                } else {
                                        if(itemAvailabilityByScans.get(itemId)!=0) {
                                                Long[] mismatch = new Long[2];
                                                mismatch[0] = itemAvailabilityByScans.get(itemId);
                                                mismatch[1] = 0L;
                                                availabilityMismatchMap.put(itemId, mismatch);
                                        }
                                }
                        }
                }*/
                int mismatchRecordsNumber = 0;
        //String subject = availabilityMismatchMap.size() + " mismatches in inventory compared to scan Records: ";
        File file = new File("inv_mismatches.xls");

        try {
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
            bufferedWriter.write(StringUtils.join(new String[] { "Item Id",
                    "Brand", "Model Name", "Model Number",
                    "Color", "Inventory shown", "Inventory through Scans", "Physical Warehouse Id" }, '\t'));
            for (InventoryMismatchUnit invUnit : inventoryMismatches) {
                if(invUnit.getQuantityAsPerScans()==invUnit.getQuantityAsPerCatalog()){
                        continue;
                }
                bufferedWriter.newLine();
                bufferedWriter.write(StringUtils.join(new String[] { String.valueOf(invUnit.getItemId()), invUnit.getBrand(), invUnit.getModelName(),
                                invUnit.getModelNumber(), invUnit.getColor(),invUnit.getQuantityAsPerCatalog()+"", invUnit.getQuantityAsPerScans()+"", invUnit.getPhysicalWarehouseId()+"" }, "\t"));
                mismatchRecordsNumber++;
            }
            bufferedWriter.close();
        } catch (IOException e) {

        }
        String subject = mismatchRecordsNumber + " mismatches in inventory compared to scan Records: ";
        GmailUtils g = new GmailUtils();
        g.sendSSLMessage(new String[]{ "sunny.yadav@smartdukaan.com","amit.gupta@shop2020.in"/*,"rajveer.singh@shop2020.in"*/ }, subject, 
                "", "adwords@shop2020.in", "adwords_shop2020", file.getAbsolutePath());
        }
}