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