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