Subversion Repositories SmartDukaan

Rev

Rev 23918 | Rev 30380 | Go to most recent revision | 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.model.ProfitMandiConstants;
import com.spice.profitmandi.common.web.client.RestClient;
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.service.inventory.PurchaseService;
import com.spice.profitmandi.web.model.LoginDetails;
import com.spice.profitmandi.web.util.CookiesProcessor;
import org.apache.http.HttpResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.time.LocalDateTime;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

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

        @Autowired
        private PurchaseService purchaseService;

        @Autowired
        private OrderRepository orderRepository;

        @Autowired
        RestClient restClient;
        @Autowired
        private RoleManager roleManager;

        @Autowired
        private CookiesProcessor cookiesProcessor;

        @RequestMapping(value = "/purchaseByInvoiceNumber", method = RequestMethod.GET)
        public String purchaseByAirwayBillOrInvoiceNumber(HttpServletRequest request,
                                                                                                          @RequestParam(name = ProfitMandiConstants.AIRWAY_BILL_OR_INVOICE_NUMBER)
                                                                                                                          String airwayBillOrInvoiceNumber, Model model) throws ProfitMandiBusinessException {
                LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
                LOGGER.info("Request Received at url {} with airwayBillOrInvoiceNumber {}", request.getRequestURI(), airwayBillOrInvoiceNumber);
                Map<String, Object> map = purchaseService.purchaseByInvoiceNumber(airwayBillOrInvoiceNumber, fofoDetails.getFofoId());
                model.addAllAttributes(map);
                return "purchase";
        }

        @RequestMapping(value = "/purchase", method = RequestMethod.GET)
        public String purchase(HttpServletRequest request) throws Exception {
                return "purchase";
        }

        @RequestMapping(value = "/pendingGrn", method = RequestMethod.GET)
        public String pendingGrn(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
                LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
                LOGGER.info("Request Received at url {}", request.getRequestURI());
                List<Order> pendingGrns = orderRepository.selectShipmentToDeliveredByRetailerId(fofoDetails.getFofoId());
                model.addAttribute("pendingGrns", pendingGrns);
                return "pending-grn";
        }

        @RequestMapping(value = "/pendingGrnDetails", method = RequestMethod.GET)
        public String pendingGrnDetails(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ORDER_ID) int orderId, Model model) throws ProfitMandiBusinessException {
                LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
                LOGGER.info("Request Received at url [{}] with orderId [{}]", request.getRequestURI(), orderId);
                model.addAttribute("pendingGrnDetails", purchaseService.getShippingDetailByOrderId(orderId, fofoDetails.getFofoId()));
                return "pending-grn-details";
        }

        @RequestMapping(value = "/purchase-invoice/{invoiceNumber}", method = RequestMethod.GET)
        public ResponseEntity<InputStreamResource> downloadInvoice(HttpServletRequest request, @PathVariable String invoiceNumber, Model model) throws Exception {
                LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
                List<Order> orders = orderRepository.selectByInvoiceNumber(invoiceNumber);
                if (orders.size() > 0) {
                        if (roleManager.isAdmin(fofoDetails.getRoleIds()) || orders.get(0).getRetailerId() == fofoDetails.getFofoId()) {
                                String invoicePath = this.getInvoicePath(orders.get(0));
                                Map<String, String> headersMap = new HashMap<>();
                                headersMap.put("Authorization", "Basic " + Base64.getEncoder().encodeToString("sd:smart@123".getBytes()));
                                HttpResponse response = restClient.getResponse("http://50.116.3.101/" + invoicePath, null, headersMap);


                                HttpHeaders headers = new HttpHeaders();
                                InputStreamResource is = new InputStreamResource(response.getEntity().getContent());
                                headers.set("Content-Type", "application/pdf");
                                headers.set("Content-disposition", "inline; filename=" + invoiceNumber + ".pdf");
                                headers.setContentLength(response.getEntity().getContentLength());
                                return new ResponseEntity<>(is, headers, HttpStatus.OK);
                        }
                } else {
                        throw new ProfitMandiBusinessException("Invalid Invoice", invoiceNumber, "Please check with your manager");
                }
                return null;
        }

        private String getInvoicePath(Order order) {
                LocalDateTime billTime = order.getBillingTimestamp();
                String dirPath = billTime.getYear() + "-" + (billTime.getMonthValue() - 1) + File.separator + order.getRetailerId();
                String filename = dirPath + File.separator + order.getInvoiceNumber() + ".pdf";
                return filename;
        }
}