Subversion Repositories SmartDukaan

Rev

Rev 21612 | Rev 21654 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.CustomCurrentInventorySnapshot;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.dao.entity.FofoItemId;
import com.spice.profitmandi.dao.entity.InventoryItem;
import com.spice.profitmandi.dao.repository.CurrentInventorySnapshotRepository;
import com.spice.profitmandi.dao.repository.InventoryItemRepository;
import com.spice.profitmandi.web.model.FofoDetails;
import com.spice.profitmandi.web.response.GrnHistory;
import com.spice.profitmandi.web.util.CookiesFetcher;
import com.spice.profitmandi.web.util.MVCResponseSender;

@Controller
public class InventoryController {
        
        private static final Logger LOGGER = LoggerFactory.getLogger(InventoryController.class);
        
        @Autowired
        CurrentInventorySnapshotRepository  currentInventorySnapshotRepository;
        
        @Autowired
        InventoryItemRepository inventoryItemRepository;
        
        @Autowired
        MVCResponseSender mvcResponseSender;
        
        @Autowired
        CookiesFetcher cookiesFetcher;
        
        @RequestMapping(value = "/checkCurrentAvailability")
        public String getCurrentAvailability(HttpServletRequest request, @ModelAttribute ModelMap model) throws Exception{
                FofoDetails fofoDetails;
                try {
                        fofoDetails = cookiesFetcher.getCookiesObject(request);
                } catch (ProfitMandiBusinessException e) {
                        model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_1009", false, "/login"));
                        return "response";
                }
                List<Object[]> rows = currentInventorySnapshotRepository.selectCustomByFofoId(fofoDetails.getFofoId());
                
                model.addAttribute("currentInventorySnapshots", this.getCustomCurrentInventorySnapshots(rows));
                return "dashboard";
        }
        
        private Set<CustomCurrentInventorySnapshot> getCustomCurrentInventorySnapshots(List<Object[]> rows){
                Set<CustomCurrentInventorySnapshot> currentInventorySnapshots = new HashSet<>();
                for(Object[] row : rows){
                        currentInventorySnapshots.add(this.createCustomCurrentInventorySnapshop(row));
                }
                return currentInventorySnapshots;
        }
        
        private CustomCurrentInventorySnapshot createCustomCurrentInventorySnapshop(Object[] row)
        {
                CustomCurrentInventorySnapshot currentInventorySnapshot = new CustomCurrentInventorySnapshot();
                currentInventorySnapshot.setItemId((Integer)row[0]);
                currentInventorySnapshot.setAvailability((Integer)row[1]);
                currentInventorySnapshot.setBrand((String)row[2]);
                currentInventorySnapshot.setModelName((String)row[3]);
                currentInventorySnapshot.setModelNumber((String)row[4]);
                currentInventorySnapshot.setColor((String)row[5]);
                return currentInventorySnapshot;
        }
        @RequestMapping(value = "/checkItemAvailability")
        public String getItemAvailability(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ITEM_ID) int itemId, @ModelAttribute Model model) throws Exception{
                FofoDetails fofoDetails;
                try {
                        fofoDetails = cookiesFetcher.getCookiesObject(request);
                } catch (ProfitMandiBusinessException e) {
                        model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_1009", false, "/login"));
                        return "response";
                }
                try{
                        FofoItemId fofoItemId = new FofoItemId();
                        fofoItemId.setFofoId(fofoDetails.getFofoId());
                        fofoItemId.setItemId(itemId);
                        Object[] row = currentInventorySnapshotRepository.selectCustomByFofoItemId(fofoItemId);
                        model.addAttribute("currentInventorySnapshots", this.createCustomCurrentInventorySnapshop(row));
                        return "dashboard";
                }catch(ProfitMandiBusinessException profitMandiBusinessException){
                        model.addAttribute("loginResponse", mvcResponseSender.createResponseString("INVNTRY_SNPSHT", false, "/dashboard"));
                        return "response";
                }
        }
        
        private List<GrnHistory> grnHistory(List<InventoryItem> inventoryItems){
                List<GrnHistory> grnHistories = new ArrayList<>();
                for(InventoryItem inventoryItem : inventoryItems){
                        GrnHistory searchablePurchaseReference = new GrnHistory();
                        searchablePurchaseReference.setPurchaseReference(inventoryItem.getPurchaseId());
                        if(!grnHistories.contains(searchablePurchaseReference)){
                                GrnHistory grnHistory = new GrnHistory();
                                grnHistory.setPurchaseReference(inventoryItem.getPurchaseId());
                                if(null != inventoryItem.getSerialNumber() || !inventoryItem.getSerialNumber().equals("")){
                                        grnHistory.setSerializedQuantity(inventoryItem.getInitialQuantity());
                                }else{
                                        grnHistory.setNonSerializedQuantity(inventoryItem.getInitialQuantity());
                                }
                                grnHistory.setTotalQuantity(grnHistory.getSerializedQuantity() + grnHistory.getNonSerializedQuantity());
                                grnHistories.add(grnHistory);
                        }else{
                                GrnHistory grnHistory = grnHistories.get(grnHistories.indexOf(searchablePurchaseReference));
                                grnHistory.setSerializedQuantity(grnHistory.getSerializedQuantity() + 1);
                                grnHistory.setNonSerializedQuantity(grnHistory.getNonSerializedQuantity() + 1);
                                grnHistory.setTotalQuantity(grnHistory.getSerializedQuantity() + grnHistory.getNonSerializedQuantity());
                        }
                }
                return grnHistories;
        }
        
        @RequestMapping(value = "/grnHistory")
        public String grnHistory(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber, @RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize, Model model) throws Exception{
                FofoDetails fofoDetails;
                try {
                        fofoDetails = cookiesFetcher.getCookiesObject(request);
                } catch (ProfitMandiBusinessException e) {
                        model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_1009", false, "/login"));
                        return "response";
                }
                List<InventoryItem> inventoryItems = inventoryItemRepository.selectByFofoId(fofoDetails.getFofoId(), pageNumber, pageSize);
                model.addAttribute("grnHistories", this.grnHistory(inventoryItems));
                return "";
        }
        
}