Subversion Repositories SmartDukaan

Rev

Rev 23886 | Rev 24106 | 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.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.InputStreamResource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.CustomCurrentInventorySnapshot;
import com.spice.profitmandi.common.model.InventoryItemAgingModel;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.util.ExcelUtils;
import com.spice.profitmandi.common.util.Utils;
import com.spice.profitmandi.service.inventory.InventoryService;
import com.spice.profitmandi.web.model.LoginDetails;
import com.spice.profitmandi.web.util.CookiesProcessor;

@Controller
@Transactional(rollbackFor=Throwable.class)
public class InventoryController {

        private static final Logger LOGGER = LogManager.getLogger(InventoryController.class);

        @Autowired
        private CookiesProcessor cookiesProcessor;
        
        @Autowired
        @Qualifier("fofoInventoryService")
        private InventoryService inventoryService;

        @Value("${saholic.api.host}")
        private String host;

        @Value("${saholic.api.port}")
        private int port;

        @Value("${saholic.api.webapp}")
        private String webapp;

        @RequestMapping(value = "/getCurrentInventorySnapshot")
        public String getCurrentAvailability(HttpServletRequest request,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit,
                        @RequestParam(name = "searchTerm", required = false, defaultValue = "") String searchTerm, 
                        Model model)
                        throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                Map<String, Object> map = inventoryService.getCurrentInventorySnapshot(loginDetails.getFofoId(), offset, limit,
                                searchTerm);
                model.addAllAttributes(map);
                return "inventory-snapshot";
        }

        @RequestMapping(value = "/getBadInventorySnapshot")
        public String getBadAvailability(HttpServletRequest request,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit,
                        @RequestParam(name = "searchTerm", required = false, defaultValue = "") String searchTerm, Model model)
                        throws ProfitMandiBusinessException {
                if(searchTerm==null) {
                        searchTerm="";
                }
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                Map<String, Object> map = inventoryService.getBadInventorySnapshot(loginDetails.getFofoId(), offset, limit,
                                searchTerm);
                model.addAllAttributes(map);
                return "bad-inventory-snapshot";
        }

        @RequestMapping(value = "/getPaginatedCurrentInventorySnapshot")
        public String getPaginatedCurrentInventorySnapshot(HttpServletRequest request,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit,
                        @RequestParam(name = "searchTerm", required = false, defaultValue = "") String searchTerm, Model model)
                        throws ProfitMandiBusinessException {
                if(searchTerm==null) {
                        searchTerm="";
                }
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                Map<String, Object> map = inventoryService.getPaginatedCurrentInventorySnapshot(loginDetails.getFofoId(),
                                offset, limit, searchTerm);
                model.addAllAttributes(map);
                return "inventory-snapshot-paginated";
        }

        @RequestMapping(value = "/getCatalog")
        public String getCatalog(HttpServletRequest request, @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit,
                        @RequestParam(name = "searchTerm", required = false, defaultValue = "") String searchTerm, Model model)
                        throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                if(searchTerm==null) {
                        searchTerm="";
                }
                Map<String, Object> map = inventoryService.getCatalog(loginDetails.getFofoId(), offset, limit, searchTerm);
                model.addAllAttributes(map);
                return "catalog";
        }

        @RequestMapping(value = "/getPaginatedCatalog")
        public String getCatalogPaginated(HttpServletRequest request,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit,
                        @RequestParam(name = "searchTerm", required = false, defaultValue = "") String searchTerm, Model model)
                        throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                if(searchTerm==null) {
                        searchTerm="";
                }
                Map<String, Object> map = inventoryService.getPaginatedCatalog(loginDetails.getFofoId(), offset, limit,
                                searchTerm);
                model.addAllAttributes(map);
                return "catalog-paginated";
        }

        @RequestMapping(value = "/checkItemAvailability")
        public String getItemAvailability(HttpServletRequest request,
                        @RequestParam(name = ProfitMandiConstants.ITEM_ID) int itemId, Model model)
                        throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                CustomCurrentInventorySnapshot customCurrentInventorySnapshot = inventoryService.checkItemAvailability(itemId,
                                loginDetails.getFofoId());
                customCurrentInventorySnapshot
                                .setIconUrl(Utils.getIconUrl(customCurrentInventorySnapshot.getCatalogItemId(), host, port, webapp));
                model.addAttribute("currentInventorySnapshot", new Gson().toJson(customCurrentInventorySnapshot));
                return "current-item-availability";

        }

        @RequestMapping(value = "/cart", method = RequestMethod.POST)
        public String addToCart(HttpServletRequest request, @RequestParam(name = "cartData") String cartData, Model model)
                        throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);

                Map<String, Object> map = inventoryService.addToCart(cartData, loginDetails.getFofoId());
                model.addAllAttributes(map);
                return "cart";
        }

        @RequestMapping(value = "/validate-cart", method = RequestMethod.POST)
        public String validateCart(HttpServletRequest request, HttpServletResponse response,
                        @RequestParam(name = "cartData") String cartData, Model model) throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);

                Map<String, Object> map = inventoryService.validateCart(cartData, loginDetails.getFofoId());
                model.addAllAttributes(map);
                return "validate-cart";
        }

        @RequestMapping(value = "/grnHistory")
        public String getGrnHistory(HttpServletRequest request,
                        @RequestParam(required = false) LocalDateTime startTime,
                        @RequestParam(required = false) LocalDateTime endTime,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit,
                        @RequestParam(name = ProfitMandiConstants.PURCHASE_REFERENCE, defaultValue = "") String purchaseReference,
                        @RequestParam(name = "searchType", defaultValue = "") String searchType, Model model)
                        throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                Map<String, Object> map = inventoryService.getGrnHistory(loginDetails.getFofoId(), startTime,
                                endTime, offset, limit, purchaseReference, searchType);
                model.addAllAttributes(map);
                return "grn-history";
        }

        @RequestMapping(value = "/getPaginatedGrnHistory")
        public String getPaginatedGrnHistory(HttpServletRequest request,
                        @RequestParam(required = false) LocalDateTime startTime,
                        @RequestParam(required = false) LocalDateTime endTime,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit,
                        @RequestParam(name = ProfitMandiConstants.PURCHASE_REFERENCE, defaultValue = "") String purchaseReference,
                        @RequestParam(name = "searchType", defaultValue = "") String searchType, Model model)
                        throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                Map<String, Object> map = inventoryService.getPaginatedGrnHistory(loginDetails.getFofoId(), startTime,
                                endTime, offset, limit);
                model.addAllAttributes(map);
                return "grn-history-paginated";
        }

        @RequestMapping(value = "/grnHistoryDetailByPurchaseId")
        public String grnHistoryByPurchaseId(HttpServletRequest request,
                        @RequestParam(name = ProfitMandiConstants.PURCHASE_ID) int purchaseId, Model model)
                        throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                Map<String, Object> map = inventoryService.getGrnHistoryDetail(loginDetails.getFofoId(), purchaseId, host, port,
                                webapp);
                model.addAllAttributes(map);
                return "grn-details";
        }

        @RequestMapping(value = "/grnHistoryDetailByPurchaseReference")
        public String grnHistoryByPurchaseReference(HttpServletRequest request,
                        @RequestParam(name = ProfitMandiConstants.PURCHASE_REFERENCE) String purchaseReference, Model model)
                        throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                Map<String, Object> map = inventoryService.getGrnHistoryDetail(loginDetails.getFofoId(), purchaseReference,
                                host, port, webapp);
                model.addAllAttributes(map);
                return "grn-details";
        }

        @RequestMapping(value = "/getInventoryItemAgingByInterval", method = RequestMethod.POST)
        public String getInventoryItemAgingByInterval(HttpServletRequest request, @RequestBody List<Integer> intervals,
                        Model model, @RequestParam(name = "searchContent", defaultValue = "") String searchContent,
                        @RequestParam(name = "offset", defaultValue = "0") int offset,
                        @RequestParam(name = "limit", defaultValue = "10") int limit) throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                Map<String, Object> map = inventoryService.getPaginatedItemAgingByInterval(loginDetails.getFofoId(), intervals,
                                searchContent, offset, limit);
                model.addAllAttributes(map);
                return "item-aging";
        }

        @RequestMapping(value = "/downloadInventoryItemAgingByInterval", method = RequestMethod.POST)
        public ResponseEntity<?> downloadInventoryItemAgingByInterval(HttpServletRequest request,
                        @RequestBody List<Integer> intervals, Model model) throws ProfitMandiBusinessException {
                LOGGER.info("Request received at url{} with body {}", request.getRequestURI(), intervals);
                LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);

                List<InventoryItemAgingModel> inventoryItemAgingModels = inventoryService
                                .getItemAgingByInterval(fofoDetails.getFofoId(), intervals);

                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                ExcelUtils.writeInventoryItemAgingModels(inventoryItemAgingModels, intervals, byteArrayOutputStream);

                final HttpHeaders headers = new HttpHeaders();
                // private static final String CONTENT_TYPE_XLSX =
                // "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                // headers.set("Content-Type", "application/vnd.ms-excel");
                headers.set("Content-disposition", "inline; filename=InventoryItemAging.xlsx");
                headers.setContentLength(byteArrayOutputStream.toByteArray().length);
                final InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
                final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
                return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);

                // return responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
        }

}