| Line 1... |
Line 1... |
| 1 |
package com.spice.profitmandi.web.controller;
|
1 |
package com.spice.profitmandi.web.controller;
|
| 2 |
|
2 |
|
| - |
|
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| - |
|
4 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| - |
|
5 |
import com.spice.profitmandi.common.web.client.RestClient;
|
| 3 |
import java.util.List;
|
6 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
| - |
|
7 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
| - |
|
8 |
import com.spice.profitmandi.service.authentication.RoleManager;
|
| - |
|
9 |
import com.spice.profitmandi.service.inventory.PurchaseService;
|
| 4 |
import java.util.Map;
|
10 |
import com.spice.profitmandi.web.model.LoginDetails;
|
| 5 |
|
- |
|
| 6 |
import javax.servlet.http.HttpServletRequest;
|
11 |
import com.spice.profitmandi.web.util.CookiesProcessor;
|
| 7 |
|
- |
|
| 8 |
import org.apache.logging.log4j.Logger;
|
12 |
import org.apache.http.HttpResponse;
|
| 9 |
import org.apache.logging.log4j.LogManager;
|
13 |
import org.apache.logging.log4j.LogManager;
|
| - |
|
14 |
import org.apache.logging.log4j.Logger;
|
| 10 |
import org.springframework.beans.factory.annotation.Autowired;
|
15 |
import org.springframework.beans.factory.annotation.Autowired;
|
| - |
|
16 |
import org.springframework.core.io.InputStreamResource;
|
| - |
|
17 |
import org.springframework.http.HttpHeaders;
|
| - |
|
18 |
import org.springframework.http.HttpStatus;
|
| - |
|
19 |
import org.springframework.http.ResponseEntity;
|
| 11 |
import org.springframework.stereotype.Controller;
|
20 |
import org.springframework.stereotype.Controller;
|
| 12 |
import org.springframework.transaction.annotation.Transactional;
|
21 |
import org.springframework.transaction.annotation.Transactional;
|
| 13 |
import org.springframework.ui.Model;
|
22 |
import org.springframework.ui.Model;
|
| - |
|
23 |
import org.springframework.web.bind.annotation.PathVariable;
|
| 14 |
import org.springframework.web.bind.annotation.RequestMapping;
|
24 |
import org.springframework.web.bind.annotation.RequestMapping;
|
| 15 |
import org.springframework.web.bind.annotation.RequestMethod;
|
25 |
import org.springframework.web.bind.annotation.RequestMethod;
|
| 16 |
import org.springframework.web.bind.annotation.RequestParam;
|
26 |
import org.springframework.web.bind.annotation.RequestParam;
|
| 17 |
|
27 |
|
| 18 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
28 |
import javax.servlet.http.HttpServletRequest;
|
| 19 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
29 |
import java.io.File;
|
| 20 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
30 |
import java.time.LocalDateTime;
|
| 21 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
31 |
import java.util.Base64;
|
| 22 |
import com.spice.profitmandi.service.inventory.PurchaseService;
|
32 |
import java.util.HashMap;
|
| 23 |
import com.spice.profitmandi.web.model.LoginDetails;
|
33 |
import java.util.List;
|
| 24 |
import com.spice.profitmandi.web.util.CookiesProcessor;
|
34 |
import java.util.Map;
|
| 25 |
|
35 |
|
| 26 |
@Controller
|
36 |
@Controller
|
| 27 |
@Transactional(rollbackFor=Throwable.class)
|
37 |
@Transactional(rollbackFor = Throwable.class)
|
| 28 |
public class PurchaseController {
|
38 |
public class PurchaseController {
|
| 29 |
|
39 |
|
| 30 |
private static final Logger LOGGER = LogManager.getLogger(PurchaseController.class);
|
40 |
private static final Logger LOGGER = LogManager.getLogger(PurchaseController.class);
|
| 31 |
|
41 |
|
| 32 |
@Autowired
|
42 |
@Autowired
|
| 33 |
private PurchaseService purchaseService;
|
43 |
private PurchaseService purchaseService;
|
| 34 |
|
44 |
|
| 35 |
@Autowired
|
45 |
@Autowired
|
| 36 |
private OrderRepository orderRepository;
|
46 |
private OrderRepository orderRepository;
|
| 37 |
|
47 |
|
| 38 |
@Autowired
|
48 |
@Autowired
|
| - |
|
49 |
RestClient restClient;
|
| - |
|
50 |
@Autowired
|
| - |
|
51 |
private RoleManager roleManager;
|
| - |
|
52 |
|
| - |
|
53 |
@Autowired
|
| 39 |
private CookiesProcessor cookiesProcessor;
|
54 |
private CookiesProcessor cookiesProcessor;
|
| 40 |
|
55 |
|
| 41 |
@RequestMapping(value = "/purchaseByInvoiceNumber", method = RequestMethod.GET)
|
56 |
@RequestMapping(value = "/purchaseByInvoiceNumber", method = RequestMethod.GET)
|
| 42 |
public String purchaseByAirwayBillOrInvoiceNumber(HttpServletRequest request,
|
57 |
public String purchaseByAirwayBillOrInvoiceNumber(HttpServletRequest request,
|
| 43 |
@RequestParam(name = ProfitMandiConstants.AIRWAY_BILL_OR_INVOICE_NUMBER)
|
58 |
@RequestParam(name = ProfitMandiConstants.AIRWAY_BILL_OR_INVOICE_NUMBER)
|
| 44 |
String airwayBillOrInvoiceNumber, Model model) throws ProfitMandiBusinessException{
|
59 |
String airwayBillOrInvoiceNumber, Model model) throws ProfitMandiBusinessException {
|
| 45 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
60 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
| 46 |
LOGGER.info("Request Received at url {} with airwayBillOrInvoiceNumber {}", request.getRequestURI(), airwayBillOrInvoiceNumber);
|
61 |
LOGGER.info("Request Received at url {} with airwayBillOrInvoiceNumber {}", request.getRequestURI(), airwayBillOrInvoiceNumber);
|
| 47 |
Map<String, Object> map = purchaseService.purchaseByInvoiceNumber(airwayBillOrInvoiceNumber, fofoDetails.getFofoId());
|
62 |
Map<String, Object> map = purchaseService.purchaseByInvoiceNumber(airwayBillOrInvoiceNumber, fofoDetails.getFofoId());
|
| 48 |
model.addAllAttributes(map);
|
63 |
model.addAllAttributes(map);
|
| 49 |
return "purchase";
|
64 |
return "purchase";
|
| 50 |
}
|
65 |
}
|
| 51 |
|
66 |
|
| 52 |
@RequestMapping(value = "/purchase", method = RequestMethod.GET)
|
67 |
@RequestMapping(value = "/purchase", method = RequestMethod.GET)
|
| 53 |
public String purchase(HttpServletRequest request) throws Exception{
|
68 |
public String purchase(HttpServletRequest request) throws Exception {
|
| 54 |
return "purchase";
|
69 |
return "purchase";
|
| 55 |
}
|
70 |
}
|
| 56 |
|
71 |
|
| 57 |
@RequestMapping(value = "/pendingGrn", method = RequestMethod.GET)
|
72 |
@RequestMapping(value = "/pendingGrn", method = RequestMethod.GET)
|
| 58 |
public String pendingGrn(HttpServletRequest request, Model model) throws ProfitMandiBusinessException{
|
73 |
public String pendingGrn(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
|
| 59 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
74 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
| 60 |
LOGGER.info("Request Received at url {}", request.getRequestURI());
|
75 |
LOGGER.info("Request Received at url {}", request.getRequestURI());
|
| 61 |
List<Order> pendingGrns = orderRepository.selectShipmentToDeliveredByRetailerId(fofoDetails.getFofoId());
|
76 |
List<Order> pendingGrns = orderRepository.selectShipmentToDeliveredByRetailerId(fofoDetails.getFofoId());
|
| 62 |
model.addAttribute("pendingGrns", pendingGrns);
|
77 |
model.addAttribute("pendingGrns", pendingGrns);
|
| 63 |
return "pending-grn";
|
78 |
return "pending-grn";
|
| 64 |
}
|
79 |
}
|
| 65 |
|
80 |
|
| 66 |
@RequestMapping(value = "/pendingGrnDetails", method = RequestMethod.GET)
|
81 |
@RequestMapping(value = "/pendingGrnDetails", method = RequestMethod.GET)
|
| 67 |
public String pendingGrnDetails(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ORDER_ID) int orderId, Model model) throws ProfitMandiBusinessException{
|
82 |
public String pendingGrnDetails(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ORDER_ID) int orderId, Model model) throws ProfitMandiBusinessException {
|
| 68 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
83 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
| 69 |
LOGGER.info("Request Received at url [{}] with orderId [{}]", request.getRequestURI(), orderId);
|
84 |
LOGGER.info("Request Received at url [{}] with orderId [{}]", request.getRequestURI(), orderId);
|
| 70 |
model.addAttribute("pendingGrnDetails", purchaseService.getShippingDetailByOrderId(orderId, fofoDetails.getFofoId()));
|
85 |
model.addAttribute("pendingGrnDetails", purchaseService.getShippingDetailByOrderId(orderId, fofoDetails.getFofoId()));
|
| 71 |
return "pending-grn-details";
|
86 |
return "pending-grn-details";
|
| 72 |
}
|
87 |
}
|
| - |
|
88 |
|
| - |
|
89 |
@RequestMapping(value = "/purchase-invoice/{invoiceNumber}", method = RequestMethod.GET)
|
| - |
|
90 |
public ResponseEntity<InputStreamResource> downloadInvoice(HttpServletRequest request, @PathVariable String invoiceNumber, Model model) throws Exception {
|
| - |
|
91 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
| - |
|
92 |
List<Order> orders = orderRepository.selectByInvoiceNumber(invoiceNumber);
|
| - |
|
93 |
if (orders.size() > 0) {
|
| - |
|
94 |
if (roleManager.isAdmin(fofoDetails.getRoleIds()) || orders.get(0).getRetailerId() == fofoDetails.getFofoId()) {
|
| - |
|
95 |
String invoicePath = this.getInvoicePath(orders.get(0));
|
| - |
|
96 |
Map<String, String> headersMap = new HashMap<>();
|
| - |
|
97 |
headersMap.put("Authorization", "Basic " + Base64.getEncoder().encodeToString("sd:smart@123".getBytes()));
|
| - |
|
98 |
HttpResponse response = restClient.getResponse("http://50.116.3.101/" + invoicePath, null, headersMap);
|
| - |
|
99 |
|
| - |
|
100 |
|
| - |
|
101 |
HttpHeaders headers = new HttpHeaders();
|
| - |
|
102 |
InputStreamResource is = new InputStreamResource(response.getEntity().getContent());
|
| - |
|
103 |
headers.set("Content-Type", "application/pdf");
|
| - |
|
104 |
headers.set("Content-disposition", "inline; filename=" + invoiceNumber + ".pdf");
|
| - |
|
105 |
headers.setContentLength(response.getEntity().getContentLength());
|
| - |
|
106 |
return new ResponseEntity<>(is, headers, HttpStatus.OK);
|
| - |
|
107 |
}
|
| - |
|
108 |
} else {
|
| - |
|
109 |
throw new ProfitMandiBusinessException("Invalid Invoice", invoiceNumber, "Please check with your manager");
|
| - |
|
110 |
}
|
| - |
|
111 |
return null;
|
| 73 |
|
112 |
}
|
| - |
|
113 |
|
| - |
|
114 |
private String getInvoicePath(Order order) {
|
| - |
|
115 |
LocalDateTime billTime = order.getBillingTimestamp();
|
| - |
|
116 |
String dirPath = billTime.getYear() + "-" + (billTime.getMonthValue() - 1) + File.separator + order.getRetailerId();
|
| - |
|
117 |
String filename = dirPath + File.separator + order.getInvoiceNumber() + ".pdf";
|
| - |
|
118 |
return filename;
|
| - |
|
119 |
}
|
| 74 |
}
|
120 |
}
|
| 75 |
|
121 |
|