Subversion Repositories SmartDukaan

Rev

Rev 23779 | Rev 23785 | 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.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.spice.profitmandi.common.enumuration.IndentStatus;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.StockAllocationModel;
import com.spice.profitmandi.dao.entity.catalog.TagListing;
import com.spice.profitmandi.dao.entity.dtr.Role;
import com.spice.profitmandi.dao.entity.fofo.CurrentInventorySnapshot;
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
import com.spice.profitmandi.dao.entity.fofo.IndentItem;
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
import com.spice.profitmandi.dao.repository.catalog.CategoryRepository;
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
import com.spice.profitmandi.dao.repository.dtr.IndentItemRepository;
import com.spice.profitmandi.dao.repository.dtr.IndentRepository;
import com.spice.profitmandi.dao.repository.dtr.RoleRepository;
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
import com.spice.profitmandi.service.indent.IndentService;
import com.spice.profitmandi.service.inventory.StockAllocationService;
import com.spice.profitmandi.service.user.RetailerService;
import com.spice.profitmandi.web.model.LoginDetails;
import com.spice.profitmandi.web.util.CookiesProcessor;
import com.spice.profitmandi.web.util.MVCResponseSender;

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

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

        @Autowired
        private CookiesProcessor cookiesProcessor;
        
        @Autowired
        FofoStoreRepository fofoStoreRepository;

        @Autowired
        private CategoryRepository categoryRepository;

        @Autowired
        @Qualifier("catalogItemRepository")
        private ItemRepository itemRepository;

        @Autowired
        private IndentRepository indentRepository;

        @Autowired
        private StockAllocationService stockAllocationService;

        @Autowired
        private IndentItemRepository indentItemRepository;
        
        @Autowired
        private RetailerService retailerService;

        @Autowired
        private IndentService indentService;

        @Autowired
        private TagListingRepository tagListingRepository;

        @Autowired
        private FofoOrderRepository fofoOrderRepository;

        @Autowired
        @Qualifier("fofoCurrentInventorySnapshotRepository")
        private CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
        
        @Autowired
        private RoleRepository roleRepository;

        @Autowired
        private MVCResponseSender mvcResponseSender;

        /*@RequestMapping(value = "/indent-item/save", method = RequestMethod.PUT)
        public String saveIndentItem(HttpServletRequest request, @RequestBody ItemIdQuantity itemIdQuantity, Model model)
                        throws Exception {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                LOGGER.info("Item id is {}, And quantity is {}", itemIdQuantity.getItemId(), itemIdQuantity.getItemId());
                int fofoId = loginDetails.getFofoId();
                boolean response = indentService.updateOpenIndentItem(fofoId, itemIdQuantity.getItemId(),
                                itemIdQuantity.getQuantity());
                model.addAttribute("response", mvcResponseSender.createResponseString(response));
                return "response";
        }*/

        @RequestMapping(value = "/open-indent/save", method = RequestMethod.POST)
        public String saveOpenIndent(HttpServletRequest request, Model model, @RequestBody List<StockAllocationModel> stockAllocationModelList) throws Exception {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                boolean response = false;
                Role roleFofoAdmin = roleRepository.selectByName(RoleType.FOFO_ADMIN.toString());
                response = stockAllocationService.addToAllocation(stockAllocationModelList);
                model.addAttribute("response", mvcResponseSender.createResponseString(response));
                return "response";
        }

        @RequestMapping(value = "/migrate", method = RequestMethod.PUT)
        public String migrate(HttpServletRequest request, Model model) throws Exception {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                int fofoId = loginDetails.getFofoId();
                List<TagListing> tagListings = tagListingRepository.selectAll();
                for (TagListing tagListing : tagListings) {
                        int itemId = tagListing.getItemId();
                }
                return "";
        }

        @RequestMapping(value = "/indent/inProcess")
        public String loadInProcessIndents(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                int fofoId = loginDetails.getFofoId();

                List<Integer> pendingIndentIds = indentRepository.selectIndentByStatus(fofoId, IndentStatus.PENDING).stream()
                                .map(x -> x.getId()).collect(Collectors.toList());
                List<Integer> allocatedIndentIds = indentRepository.selectIndentByStatus(fofoId, IndentStatus.ALLOCATED)
                                .stream().map(x -> x.getId()).collect(Collectors.toList());
                pendingIndentIds.addAll(allocatedIndentIds);

                if (pendingIndentIds.size() > 0) {
                        Map<Integer, IndentItem> pendingIndentItemIdMap = indentItemRepository.selectIndentItems(pendingIndentIds)
                                        .stream().collect(Collectors.toMap(x -> x.getItemId(), x -> x));
                        List<TagListing> tagListings = tagListingRepository
                                        .selectByItemIdsAndTagIds(pendingIndentItemIdMap.keySet(), new HashSet<>(Arrays.asList(4)));

                        List<FofoOrderItem> fofoOrderItems = fofoOrderRepository.selectByFofoItemIds(fofoId, pendingIndentIds,
                                        LocalDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT).minusDays(30), LocalDateTime.now());
                        Map<Integer, Integer> itemQuantity = fofoOrderItems.stream().collect(
                                        Collectors.groupingBy(FofoOrderItem::getItemId, Collectors.summingInt(FofoOrderItem::getQuantity)));

                        List<CurrentInventorySnapshot> cis = currentInventorySnapshotRepository.selectByFofoId(fofoId);
                        Map<Integer, CurrentInventorySnapshot> itemIdSnapshotMap = cis.stream()
                                        .collect(Collectors.toMap(x -> x.getItemId(), x -> x));

                        for (TagListing tagListing : tagListings) {
                                Integer itemId = tagListing.getItemId();
                                tagListing.setItemDescription(itemRepository.selectById(itemId).getItemDescription());
                                if (itemQuantity.containsKey(itemId)) {
                                        tagListing.setLast30DaysSale(itemQuantity.get(itemId));
                                }
                                if (itemIdSnapshotMap.containsKey(itemId)) {
                                        tagListing.setStockInHand(itemIdSnapshotMap.get(itemId).getAvailability());
                                }
                        }

                        model.addAttribute("pendingIndentItemIdMap", pendingIndentItemIdMap);
                        model.addAttribute("tagListings", tagListings);
                }
                return "pending-indent";
        }

        @RequestMapping(value = "/indent/loadIndent")
        public String loadOpenIndent(HttpServletRequest request, Model model, @RequestParam(required=false, defaultValue="0") int fofoId ) throws ProfitMandiBusinessException {
                LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
                if (loginDetails.isAdmin()) {
                        List<StockAllocationModel> stockAllocationList;
                        if(fofoId > 0) {
                                stockAllocationList = stockAllocationService.getStockAllocation(fofoId, true);
                        } else {
                                stockAllocationList = stockAllocationService.getStockAllocation(true);
                        }

                        Map<Integer, StockAllocationModel> itemStockAllocationMap = stockAllocationList.stream()
                                        .collect(Collectors.toMap(x -> x.getItemId(), x -> x));

                        List<TagListing> tagListings = tagListingRepository.selectAll();

                        for (TagListing tagListing : tagListings) {
                                StockAllocationModel stockAllocationModel = itemStockAllocationMap.get(tagListing.getItemId());
                                if(stockAllocationModel!= null ) {
                                        tagListing.setAllocatedQuantity(stockAllocationModel.getQuantity());
                                }
                                tagListing.setItemDescription(itemRepository.selectById(tagListing.getItemId()).getItemDescription());
                        }

                        List<Integer> fofoIds = fofoStoreRepository.selectAll().stream().map(x->x.getId()).collect(Collectors.toList());
                        model.addAttribute("tagListings", tagListings);
                        String customRetailers = JSONObject.valueToString(retailerService.getFofoRetailers(fofoIds).values());
                        LOGGER.info("Custom retailesr {}", customRetailers);
                        model.addAttribute("customRetailers", customRetailers);
                        return "open-indent";
                } else {
                        throw new ProfitMandiBusinessException("Unauthorised", loginDetails.getEmailId(),
                                        "Can't access the resource");
                }
        }
}