| 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;
|
| 30380 |
amit.gupta |
10 |
import com.spice.profitmandi.service.transaction.invoicing.InvoiceService;
|
| 30289 |
amit.gupta |
11 |
import com.spice.profitmandi.web.model.LoginDetails;
|
|
|
12 |
import com.spice.profitmandi.web.util.CookiesProcessor;
|
|
|
13 |
import org.apache.http.HttpResponse;
|
|
|
14 |
import org.apache.logging.log4j.LogManager;
|
| 23569 |
govind |
15 |
import org.apache.logging.log4j.Logger;
|
| 21574 |
ashik.ali |
16 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 30380 |
amit.gupta |
17 |
import org.springframework.core.io.ByteArrayResource;
|
| 30289 |
amit.gupta |
18 |
import org.springframework.core.io.InputStreamResource;
|
|
|
19 |
import org.springframework.http.HttpHeaders;
|
|
|
20 |
import org.springframework.http.HttpStatus;
|
| 30380 |
amit.gupta |
21 |
import org.springframework.http.MediaType;
|
| 30289 |
amit.gupta |
22 |
import org.springframework.http.ResponseEntity;
|
| 21555 |
kshitij.so |
23 |
import org.springframework.stereotype.Controller;
|
| 21636 |
ashik.ali |
24 |
import org.springframework.transaction.annotation.Transactional;
|
| 21574 |
ashik.ali |
25 |
import org.springframework.ui.Model;
|
| 30289 |
amit.gupta |
26 |
import org.springframework.web.bind.annotation.PathVariable;
|
| 21555 |
kshitij.so |
27 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
28 |
import org.springframework.web.bind.annotation.RequestMethod;
|
| 21583 |
ashik.ali |
29 |
import org.springframework.web.bind.annotation.RequestParam;
|
| 21555 |
kshitij.so |
30 |
|
| 30289 |
amit.gupta |
31 |
import javax.servlet.http.HttpServletRequest;
|
| 30380 |
amit.gupta |
32 |
import java.io.ByteArrayOutputStream;
|
| 30289 |
amit.gupta |
33 |
import java.io.File;
|
|
|
34 |
import java.time.LocalDateTime;
|
| 30380 |
amit.gupta |
35 |
import java.util.*;
|
| 30387 |
amit.gupta |
36 |
import java.util.stream.Collectors;
|
| 30380 |
amit.gupta |
37 |
import java.util.zip.ZipEntry;
|
|
|
38 |
import java.util.zip.ZipOutputStream;
|
| 21555 |
kshitij.so |
39 |
|
|
|
40 |
@Controller
|
| 30289 |
amit.gupta |
41 |
@Transactional(rollbackFor = Throwable.class)
|
| 21582 |
kshitij.so |
42 |
public class PurchaseController {
|
| 21555 |
kshitij.so |
43 |
|
| 23569 |
govind |
44 |
private static final Logger LOGGER = LogManager.getLogger(PurchaseController.class);
|
| 30289 |
amit.gupta |
45 |
|
| 21574 |
ashik.ali |
46 |
@Autowired
|
| 22927 |
ashik.ali |
47 |
private PurchaseService purchaseService;
|
| 30289 |
amit.gupta |
48 |
|
| 23076 |
ashik.ali |
49 |
@Autowired
|
|
|
50 |
private OrderRepository orderRepository;
|
| 21640 |
kshitij.so |
51 |
|
| 21574 |
ashik.ali |
52 |
@Autowired
|
| 30289 |
amit.gupta |
53 |
RestClient restClient;
|
|
|
54 |
@Autowired
|
|
|
55 |
private RoleManager roleManager;
|
|
|
56 |
|
|
|
57 |
@Autowired
|
| 22927 |
ashik.ali |
58 |
private CookiesProcessor cookiesProcessor;
|
| 21640 |
kshitij.so |
59 |
|
| 30380 |
amit.gupta |
60 |
@Autowired
|
|
|
61 |
private InvoiceService invoiceService;
|
| 21640 |
kshitij.so |
62 |
|
| 21587 |
kshitij.so |
63 |
@RequestMapping(value = "/purchase", method = RequestMethod.GET)
|
| 30289 |
amit.gupta |
64 |
public String purchase(HttpServletRequest request) throws Exception {
|
| 21587 |
kshitij.so |
65 |
return "purchase";
|
|
|
66 |
}
|
| 30289 |
amit.gupta |
67 |
|
| 23076 |
ashik.ali |
68 |
@RequestMapping(value = "/pendingGrn", method = RequestMethod.GET)
|
| 30289 |
amit.gupta |
69 |
public String pendingGrn(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
|
| 23076 |
ashik.ali |
70 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
71 |
LOGGER.info("Request Received at url {}", request.getRequestURI());
|
|
|
72 |
List<Order> pendingGrns = orderRepository.selectShipmentToDeliveredByRetailerId(fofoDetails.getFofoId());
|
|
|
73 |
model.addAttribute("pendingGrns", pendingGrns);
|
|
|
74 |
return "pending-grn";
|
|
|
75 |
}
|
| 30289 |
amit.gupta |
76 |
|
| 23076 |
ashik.ali |
77 |
@RequestMapping(value = "/pendingGrnDetails", method = RequestMethod.GET)
|
| 30289 |
amit.gupta |
78 |
public String pendingGrnDetails(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ORDER_ID) int orderId, Model model) throws ProfitMandiBusinessException {
|
| 23076 |
ashik.ali |
79 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
80 |
LOGGER.info("Request Received at url [{}] with orderId [{}]", request.getRequestURI(), orderId);
|
|
|
81 |
model.addAttribute("pendingGrnDetails", purchaseService.getShippingDetailByOrderId(orderId, fofoDetails.getFofoId()));
|
|
|
82 |
return "pending-grn-details";
|
|
|
83 |
}
|
| 30289 |
amit.gupta |
84 |
|
| 30380 |
amit.gupta |
85 |
@RequestMapping(value = "/purchaseByInvoiceNumber", method = RequestMethod.GET)
|
|
|
86 |
public String purchaseByAirwayBillOrInvoiceNumber(HttpServletRequest request,
|
|
|
87 |
@RequestParam(name = ProfitMandiConstants.AIRWAY_BILL_OR_INVOICE_NUMBER)
|
|
|
88 |
String airwayBillOrInvoiceNumber, Model model) throws ProfitMandiBusinessException {
|
|
|
89 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
90 |
LOGGER.info("Request Received at url {} with airwayBillOrInvoiceNumber {}", request.getRequestURI(), airwayBillOrInvoiceNumber);
|
|
|
91 |
Map<String, Object> map = purchaseService.purchaseByInvoiceNumber(airwayBillOrInvoiceNumber, fofoDetails.getFofoId());
|
|
|
92 |
model.addAllAttributes(map);
|
|
|
93 |
return "purchase";
|
|
|
94 |
}
|
|
|
95 |
|
| 30289 |
amit.gupta |
96 |
@RequestMapping(value = "/purchase-invoice/{invoiceNumber}", method = RequestMethod.GET)
|
| 30380 |
amit.gupta |
97 |
public ResponseEntity<?> downloadInvoice(HttpServletRequest request, @PathVariable String invoiceNumber, Model model) throws Exception {
|
| 30289 |
amit.gupta |
98 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
| 30380 |
amit.gupta |
99 |
List<String> invoiceNumbers = Arrays.asList(invoiceNumber.replaceAll(" ", "").split(","));
|
| 30387 |
amit.gupta |
100 |
Map<String, List<Order>> invoiceOrdersMap = orderRepository.selectByInvoiceNumbers(invoiceNumbers).stream().collect(Collectors.groupingBy(x -> x.getInvoiceNumber()));
|
|
|
101 |
if (invoiceOrdersMap.size() > 0) {
|
|
|
102 |
if (roleManager.isAdmin(fofoDetails.getRoleIds()) || invoiceOrdersMap.get(invoiceNumbers.get(0)).get(0).getRetailerId() == fofoDetails.getFofoId()) {
|
| 30289 |
amit.gupta |
103 |
|
| 30380 |
amit.gupta |
104 |
final HttpHeaders headers = new HttpHeaders();
|
|
|
105 |
headers.setContentType(MediaType.APPLICATION_PDF);
|
| 30289 |
amit.gupta |
106 |
headers.set("Content-Type", "application/pdf");
|
|
|
107 |
headers.set("Content-disposition", "inline; filename=" + invoiceNumber + ".pdf");
|
| 30380 |
amit.gupta |
108 |
|
| 30381 |
amit.gupta |
109 |
if (false) {
|
|
|
110 |
/*InvoicePdfModel invoicePdfModel = invoiceService.getInvoicePdfModel(orders);
|
| 30380 |
amit.gupta |
111 |
|
|
|
112 |
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
113 |
List<InvoicePdfModel> pdfModels = new ArrayList<>();
|
|
|
114 |
pdfModels.add(invoicePdfModel);
|
|
|
115 |
PdfUtils.generateAndWrite(pdfModels, byteArrayOutputStream);
|
|
|
116 |
final InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
|
|
|
117 |
final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
|
|
|
118 |
LOGGER.info("Before stream - {}", pdfModels);
|
| 30381 |
amit.gupta |
119 |
return new ResponseEntity<>(inputStreamResource, headers, HttpStatus.OK);*/
|
| 30380 |
amit.gupta |
120 |
} else {
|
|
|
121 |
Map<String, String> headersMap = new HashMap<>();
|
|
|
122 |
headersMap.put("Authorization", "Basic " + Base64.getEncoder().encodeToString("sd:smart@123".getBytes()));
|
|
|
123 |
if (invoiceNumbers.size() == 1) {
|
| 30387 |
amit.gupta |
124 |
String invoicePath = this.getInvoicePath(invoiceOrdersMap.get(invoiceNumbers.get(0)).get(0));
|
| 30380 |
amit.gupta |
125 |
HttpResponse response = restClient.getResponse("http://50.116.3.101/" + invoicePath, null, headersMap);
|
|
|
126 |
InputStreamResource is = new InputStreamResource(response.getEntity().getContent());
|
|
|
127 |
return new ResponseEntity<>(is, headers, HttpStatus.OK);
|
|
|
128 |
|
|
|
129 |
} else {
|
|
|
130 |
|
|
|
131 |
ByteArrayOutputStream fos = new ByteArrayOutputStream();
|
|
|
132 |
ZipOutputStream zipOut = new ZipOutputStream(fos);
|
|
|
133 |
|
|
|
134 |
for (String invoice : invoiceNumbers) {
|
| 30387 |
amit.gupta |
135 |
List<Order> orders = invoiceOrdersMap.get(invoice);
|
| 30380 |
amit.gupta |
136 |
String invoicePath = this.getInvoicePath(orders.get(0));
|
|
|
137 |
HttpResponse response = restClient.getResponse("http://50.116.3.101/" + invoicePath, null, headersMap);
|
|
|
138 |
ZipEntry zipEntry = new ZipEntry(invoice + ".pdf");
|
|
|
139 |
zipOut.putNextEntry(zipEntry);
|
| 30388 |
amit.gupta |
140 |
byte[] bytes = new byte[(int) response.getEntity().getContentLength()];
|
|
|
141 |
response.getEntity().getContent().read(bytes);
|
|
|
142 |
zipOut.write(bytes, 0, (int) response.getEntity().getContentLength());
|
| 30380 |
amit.gupta |
143 |
}
|
|
|
144 |
byte[] byteArray = fos.toByteArray();
|
| 30382 |
amit.gupta |
145 |
headers.set("Content-Type", "application/octet-stream");
|
| 30380 |
amit.gupta |
146 |
headers.set("Content-disposition", "inline; filename=invoices.zip");
|
|
|
147 |
headers.setContentLength(byteArray.length);
|
|
|
148 |
zipOut.close();
|
|
|
149 |
fos.close();
|
|
|
150 |
return new ResponseEntity<>(new ByteArrayResource(byteArray), headers, HttpStatus.OK);
|
|
|
151 |
}
|
|
|
152 |
}
|
|
|
153 |
|
| 30289 |
amit.gupta |
154 |
}
|
|
|
155 |
} else {
|
|
|
156 |
throw new ProfitMandiBusinessException("Invalid Invoice", invoiceNumber, "Please check with your manager");
|
|
|
157 |
}
|
|
|
158 |
return null;
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
private String getInvoicePath(Order order) {
|
|
|
162 |
LocalDateTime billTime = order.getBillingTimestamp();
|
|
|
163 |
String dirPath = billTime.getYear() + "-" + (billTime.getMonthValue() - 1) + File.separator + order.getRetailerId();
|
|
|
164 |
String filename = dirPath + File.separator + order.getInvoiceNumber() + ".pdf";
|
|
|
165 |
return filename;
|
|
|
166 |
}
|
| 21636 |
ashik.ali |
167 |
}
|