Subversion Repositories SmartDukaan

Rev

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

package com.spice.profitmandi.web.controller;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.util.FormattingUtils;
import com.spice.profitmandi.common.util.Utils;
import com.spice.profitmandi.dao.entity.transaction.Order;
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
import com.spice.profitmandi.service.authentication.RoleManager;
import com.spice.profitmandi.web.model.LoginDetails;
import com.spice.profitmandi.web.util.CookiesProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@Transactional(rollbackFor = Throwable.class)
public class InvoiceController {
    @Autowired
    OrderRepository orderRepository;

    @Autowired
    CookiesProcessor cookiesProcessor;

    @Autowired
    RoleManager roleManager;

    @RequestMapping(value = {"invoice-summary", "/invoice-summary/{yearMonth}" })
    public String addAddress(HttpServletRequest request, @PathVariable(required = false) YearMonth yearMonth, Model model, @RequestParam(defaultValue = "0") int fofoId) throws ProfitMandiBusinessException {
        if (yearMonth == null) {
            yearMonth = YearMonth.now();
        }
        LocalDateTime startDate = yearMonth.atDay(1).atStartOfDay();
        LocalDateTime endDate = yearMonth.atEndOfMonth().atTime(LocalTime.MAX);
        LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
        boolean isAdmin = false;
        if (roleManager.isAdmin(loginDetails.getRoleIds())) {
            isAdmin = true;
        } else {
            fofoId = loginDetails.getFofoId();
        }
        List<Order> orders;
        if (fofoId == 0) {
            orders = new ArrayList<>();
        } else {
            orders = orderRepository.selectAllByBillingDatesBetween(fofoId, startDate,
                    endDate);
        }
        Map<String, LocalDateTime> invoiceBillingDateMap = orders.stream().filter(Utils.distinctByKey(x -> x.getInvoiceNumber())).collect(Collectors.toMap(x -> x.getInvoiceNumber(), x -> x.getBillingTimestamp()));
        String yearMonthString = FormattingUtils.formatYearMonth(startDate);
        model.addAttribute("yearMonth", yearMonth);
        model.addAttribute("invoiceMap", invoiceBillingDateMap);
        model.addAttribute("isAdmin", isAdmin);

        return "invoice-summary";
    }

}