| 21561 |
ashik.ali |
1 |
package com.spice.profitmandi.web.controller;
|
| 21555 |
kshitij.so |
2 |
|
| 30289 |
amit.gupta |
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;
|
|
|
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;
|
|
|
10 |
import com.spice.profitmandi.web.model.LoginDetails;
|
|
|
11 |
import com.spice.profitmandi.web.util.CookiesProcessor;
|
|
|
12 |
import org.apache.http.HttpResponse;
|
|
|
13 |
import org.apache.logging.log4j.LogManager;
|
| 23569 |
govind |
14 |
import org.apache.logging.log4j.Logger;
|
| 21574 |
ashik.ali |
15 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 30289 |
amit.gupta |
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;
|
| 21555 |
kshitij.so |
20 |
import org.springframework.stereotype.Controller;
|
| 21636 |
ashik.ali |
21 |
import org.springframework.transaction.annotation.Transactional;
|
| 21574 |
ashik.ali |
22 |
import org.springframework.ui.Model;
|
| 30289 |
amit.gupta |
23 |
import org.springframework.web.bind.annotation.PathVariable;
|
| 21555 |
kshitij.so |
24 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
25 |
import org.springframework.web.bind.annotation.RequestMethod;
|
| 21583 |
ashik.ali |
26 |
import org.springframework.web.bind.annotation.RequestParam;
|
| 21555 |
kshitij.so |
27 |
|
| 30289 |
amit.gupta |
28 |
import javax.servlet.http.HttpServletRequest;
|
|
|
29 |
import java.io.File;
|
|
|
30 |
import java.time.LocalDateTime;
|
|
|
31 |
import java.util.Base64;
|
|
|
32 |
import java.util.HashMap;
|
|
|
33 |
import java.util.List;
|
|
|
34 |
import java.util.Map;
|
| 21555 |
kshitij.so |
35 |
|
|
|
36 |
@Controller
|
| 30289 |
amit.gupta |
37 |
@Transactional(rollbackFor = Throwable.class)
|
| 21582 |
kshitij.so |
38 |
public class PurchaseController {
|
| 21555 |
kshitij.so |
39 |
|
| 23569 |
govind |
40 |
private static final Logger LOGGER = LogManager.getLogger(PurchaseController.class);
|
| 30289 |
amit.gupta |
41 |
|
| 21574 |
ashik.ali |
42 |
@Autowired
|
| 22927 |
ashik.ali |
43 |
private PurchaseService purchaseService;
|
| 30289 |
amit.gupta |
44 |
|
| 23076 |
ashik.ali |
45 |
@Autowired
|
|
|
46 |
private OrderRepository orderRepository;
|
| 21640 |
kshitij.so |
47 |
|
| 21574 |
ashik.ali |
48 |
@Autowired
|
| 30289 |
amit.gupta |
49 |
RestClient restClient;
|
|
|
50 |
@Autowired
|
|
|
51 |
private RoleManager roleManager;
|
|
|
52 |
|
|
|
53 |
@Autowired
|
| 22927 |
ashik.ali |
54 |
private CookiesProcessor cookiesProcessor;
|
| 21640 |
kshitij.so |
55 |
|
| 23026 |
ashik.ali |
56 |
@RequestMapping(value = "/purchaseByInvoiceNumber", method = RequestMethod.GET)
|
| 30289 |
amit.gupta |
57 |
public String purchaseByAirwayBillOrInvoiceNumber(HttpServletRequest request,
|
|
|
58 |
@RequestParam(name = ProfitMandiConstants.AIRWAY_BILL_OR_INVOICE_NUMBER)
|
|
|
59 |
String airwayBillOrInvoiceNumber, Model model) throws ProfitMandiBusinessException {
|
| 22139 |
amit.gupta |
60 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
| 22927 |
ashik.ali |
61 |
LOGGER.info("Request Received at url {} with airwayBillOrInvoiceNumber {}", request.getRequestURI(), airwayBillOrInvoiceNumber);
|
|
|
62 |
Map<String, Object> map = purchaseService.purchaseByInvoiceNumber(airwayBillOrInvoiceNumber, fofoDetails.getFofoId());
|
|
|
63 |
model.addAllAttributes(map);
|
| 21587 |
kshitij.so |
64 |
return "purchase";
|
| 21555 |
kshitij.so |
65 |
}
|
| 21640 |
kshitij.so |
66 |
|
| 21587 |
kshitij.so |
67 |
@RequestMapping(value = "/purchase", method = RequestMethod.GET)
|
| 30289 |
amit.gupta |
68 |
public String purchase(HttpServletRequest request) throws Exception {
|
| 21587 |
kshitij.so |
69 |
return "purchase";
|
|
|
70 |
}
|
| 30289 |
amit.gupta |
71 |
|
| 23076 |
ashik.ali |
72 |
@RequestMapping(value = "/pendingGrn", method = RequestMethod.GET)
|
| 30289 |
amit.gupta |
73 |
public String pendingGrn(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
|
| 23076 |
ashik.ali |
74 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
75 |
LOGGER.info("Request Received at url {}", request.getRequestURI());
|
|
|
76 |
List<Order> pendingGrns = orderRepository.selectShipmentToDeliveredByRetailerId(fofoDetails.getFofoId());
|
|
|
77 |
model.addAttribute("pendingGrns", pendingGrns);
|
|
|
78 |
return "pending-grn";
|
|
|
79 |
}
|
| 30289 |
amit.gupta |
80 |
|
| 23076 |
ashik.ali |
81 |
@RequestMapping(value = "/pendingGrnDetails", method = RequestMethod.GET)
|
| 30289 |
amit.gupta |
82 |
public String pendingGrnDetails(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ORDER_ID) int orderId, Model model) throws ProfitMandiBusinessException {
|
| 23076 |
ashik.ali |
83 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
84 |
LOGGER.info("Request Received at url [{}] with orderId [{}]", request.getRequestURI(), orderId);
|
|
|
85 |
model.addAttribute("pendingGrnDetails", purchaseService.getShippingDetailByOrderId(orderId, fofoDetails.getFofoId()));
|
|
|
86 |
return "pending-grn-details";
|
|
|
87 |
}
|
| 30289 |
amit.gupta |
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;
|
|
|
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 |
}
|
| 21636 |
ashik.ali |
120 |
}
|