| 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;
|
| 30694 |
amit.gupta |
5 |
import com.spice.profitmandi.common.model.ScanSerializedRequest;
|
| 30289 |
amit.gupta |
6 |
import com.spice.profitmandi.common.web.client.RestClient;
|
| 30694 |
amit.gupta |
7 |
import com.spice.profitmandi.common.web.util.ResponseSender;
|
|
|
8 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
| 30289 |
amit.gupta |
9 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
| 30694 |
amit.gupta |
10 |
import com.spice.profitmandi.dao.model.ImeiInoviceModel;
|
|
|
11 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
| 30289 |
amit.gupta |
12 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
|
|
13 |
import com.spice.profitmandi.service.authentication.RoleManager;
|
|
|
14 |
import com.spice.profitmandi.service.inventory.PurchaseService;
|
| 30380 |
amit.gupta |
15 |
import com.spice.profitmandi.service.transaction.invoicing.InvoiceService;
|
| 30289 |
amit.gupta |
16 |
import com.spice.profitmandi.web.model.LoginDetails;
|
|
|
17 |
import com.spice.profitmandi.web.util.CookiesProcessor;
|
| 30394 |
amit.gupta |
18 |
import org.apache.commons.io.IOUtils;
|
| 30289 |
amit.gupta |
19 |
import org.apache.http.HttpResponse;
|
| 30394 |
amit.gupta |
20 |
import org.apache.http.entity.ContentType;
|
| 30289 |
amit.gupta |
21 |
import org.apache.logging.log4j.LogManager;
|
| 23569 |
govind |
22 |
import org.apache.logging.log4j.Logger;
|
| 21574 |
ashik.ali |
23 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 30289 |
amit.gupta |
24 |
import org.springframework.core.io.InputStreamResource;
|
|
|
25 |
import org.springframework.http.HttpHeaders;
|
|
|
26 |
import org.springframework.http.HttpStatus;
|
| 30380 |
amit.gupta |
27 |
import org.springframework.http.MediaType;
|
| 30289 |
amit.gupta |
28 |
import org.springframework.http.ResponseEntity;
|
| 21555 |
kshitij.so |
29 |
import org.springframework.stereotype.Controller;
|
| 21636 |
ashik.ali |
30 |
import org.springframework.transaction.annotation.Transactional;
|
| 21574 |
ashik.ali |
31 |
import org.springframework.ui.Model;
|
| 30694 |
amit.gupta |
32 |
import org.springframework.web.bind.annotation.*;
|
| 21555 |
kshitij.so |
33 |
|
| 30289 |
amit.gupta |
34 |
import javax.servlet.http.HttpServletRequest;
|
| 30394 |
amit.gupta |
35 |
import java.io.ByteArrayInputStream;
|
| 30380 |
amit.gupta |
36 |
import java.io.ByteArrayOutputStream;
|
| 30289 |
amit.gupta |
37 |
import java.io.File;
|
| 30394 |
amit.gupta |
38 |
import java.io.InputStream;
|
| 30289 |
amit.gupta |
39 |
import java.time.LocalDateTime;
|
| 30380 |
amit.gupta |
40 |
import java.util.*;
|
| 30387 |
amit.gupta |
41 |
import java.util.stream.Collectors;
|
| 30380 |
amit.gupta |
42 |
import java.util.zip.ZipEntry;
|
|
|
43 |
import java.util.zip.ZipOutputStream;
|
| 21555 |
kshitij.so |
44 |
|
| 30395 |
amit.gupta |
45 |
import static org.springframework.util.FileCopyUtils.BUFFER_SIZE;
|
|
|
46 |
|
| 21555 |
kshitij.so |
47 |
@Controller
|
| 30289 |
amit.gupta |
48 |
@Transactional(rollbackFor = Throwable.class)
|
| 21582 |
kshitij.so |
49 |
public class PurchaseController {
|
| 21555 |
kshitij.so |
50 |
|
| 23569 |
govind |
51 |
private static final Logger LOGGER = LogManager.getLogger(PurchaseController.class);
|
| 30289 |
amit.gupta |
52 |
|
| 21574 |
ashik.ali |
53 |
@Autowired
|
| 22927 |
ashik.ali |
54 |
private PurchaseService purchaseService;
|
| 30289 |
amit.gupta |
55 |
|
| 23076 |
ashik.ali |
56 |
@Autowired
|
| 30694 |
amit.gupta |
57 |
private InventoryItemRepository inventoryItemRepository;
|
|
|
58 |
|
|
|
59 |
@Autowired
|
| 23076 |
ashik.ali |
60 |
private OrderRepository orderRepository;
|
| 21640 |
kshitij.so |
61 |
|
| 21574 |
ashik.ali |
62 |
@Autowired
|
| 30289 |
amit.gupta |
63 |
RestClient restClient;
|
|
|
64 |
@Autowired
|
|
|
65 |
private RoleManager roleManager;
|
|
|
66 |
|
|
|
67 |
@Autowired
|
| 30694 |
amit.gupta |
68 |
private ResponseSender responseSender;
|
|
|
69 |
|
|
|
70 |
@Autowired
|
| 22927 |
ashik.ali |
71 |
private CookiesProcessor cookiesProcessor;
|
| 21640 |
kshitij.so |
72 |
|
| 30380 |
amit.gupta |
73 |
@Autowired
|
|
|
74 |
private InvoiceService invoiceService;
|
| 21640 |
kshitij.so |
75 |
|
| 21587 |
kshitij.so |
76 |
@RequestMapping(value = "/purchase", method = RequestMethod.GET)
|
| 30289 |
amit.gupta |
77 |
public String purchase(HttpServletRequest request) throws Exception {
|
| 21587 |
kshitij.so |
78 |
return "purchase";
|
|
|
79 |
}
|
| 30289 |
amit.gupta |
80 |
|
| 23076 |
ashik.ali |
81 |
@RequestMapping(value = "/pendingGrn", method = RequestMethod.GET)
|
| 30289 |
amit.gupta |
82 |
public String pendingGrn(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
|
| 23076 |
ashik.ali |
83 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
84 |
LOGGER.info("Request Received at url {}", request.getRequestURI());
|
|
|
85 |
List<Order> pendingGrns = orderRepository.selectShipmentToDeliveredByRetailerId(fofoDetails.getFofoId());
|
|
|
86 |
model.addAttribute("pendingGrns", pendingGrns);
|
|
|
87 |
return "pending-grn";
|
|
|
88 |
}
|
| 30289 |
amit.gupta |
89 |
|
| 30694 |
amit.gupta |
90 |
@RequestMapping(value = "/purchase/validate-imeis", method = RequestMethod.POST)
|
|
|
91 |
public ResponseEntity<?> validatePurchaseImeis(HttpServletRequest request, @RequestBody ImeiInoviceModel imeiInoviceModel, Model model) throws ProfitMandiBusinessException {
|
|
|
92 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
93 |
return responseSender.ok(this.getImeiValidationMap(fofoDetails.getFofoId(), imeiInoviceModel));
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
private Map<String, Boolean> getImeiValidationMap(int fofoId, ImeiInoviceModel imeiInoviceModel) throws ProfitMandiBusinessException {
|
|
|
97 |
LOGGER.info("serialNumbers - {}", imeiInoviceModel.getSerialNumbers());
|
|
|
98 |
Set<String> serialNumbers = new HashSet<>(orderRepository.selectSerialNumbers(imeiInoviceModel.getInvoiceNumber(), fofoId, imeiInoviceModel.getSerialNumbers()));
|
|
|
99 |
Map<String, Boolean> imeiValidationMap = new HashMap<>();
|
|
|
100 |
imeiInoviceModel.getSerialNumbers().stream().forEach(x -> {
|
|
|
101 |
imeiValidationMap.put(x, null);
|
|
|
102 |
});
|
|
|
103 |
if (serialNumbers.size() > 0) {
|
|
|
104 |
List<InventoryItem> inventoryItems = inventoryItemRepository.selectByFofoIdSerialNumbers(fofoId, new HashSet<>(serialNumbers), false);
|
|
|
105 |
Set<String> grnedSerialNumberSet = inventoryItems.stream().map(x -> x.getSerialNumber()).collect(Collectors.toSet());
|
|
|
106 |
imeiInoviceModel.getSerialNumbers().stream().forEach(x -> {
|
|
|
107 |
imeiValidationMap.put(x, serialNumbers.contains(x) && !grnedSerialNumberSet.contains(x));
|
|
|
108 |
});
|
|
|
109 |
}
|
|
|
110 |
return imeiValidationMap;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
@RequestMapping(value = "/purchase/grn-imeis", method = RequestMethod.POST)
|
|
|
114 |
public ResponseEntity<?> grnImeis(HttpServletRequest request, @RequestBody ImeiInoviceModel imeiInoviceModel, Model model) throws ProfitMandiBusinessException {
|
|
|
115 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
116 |
Map<String, Boolean> imeiValidationMap = this.getImeiValidationMap(fofoDetails.getFofoId(), imeiInoviceModel);
|
|
|
117 |
if (imeiValidationMap.values().stream().filter(x -> !x).findFirst().isPresent()) {
|
|
|
118 |
throw new ProfitMandiBusinessException("Found invalid imeis cant proceed", "invalid imeis", "invalid imeis");
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
Map<Integer, List<String>> itemSerialNumberMap = orderRepository.selectItemSerialNumberMap(imeiInoviceModel.getInvoiceNumber(), fofoDetails.getFofoId(), imeiInoviceModel.getSerialNumbers());
|
|
|
122 |
for (Map.Entry<Integer, List<String>> itemSerialNumberEntry : itemSerialNumberMap.entrySet()) {
|
|
|
123 |
ScanSerializedRequest scanSerializedRequest = new ScanSerializedRequest();
|
|
|
124 |
scanSerializedRequest.setSerialNumbers(itemSerialNumberEntry.getValue());
|
|
|
125 |
scanSerializedRequest.setInvoiceNumber(imeiInoviceModel.getInvoiceNumber());
|
|
|
126 |
scanSerializedRequest.setItemId(itemSerialNumberEntry.getKey());
|
|
|
127 |
purchaseService.scanSerializedItems(scanSerializedRequest, fofoDetails.getFofoId());
|
|
|
128 |
}
|
|
|
129 |
return responseSender.ok(true);
|
|
|
130 |
}
|
|
|
131 |
|
| 30701 |
amit.gupta |
132 |
@RequestMapping(value = "/purchase/get-imeis", method = RequestMethod.GET)
|
| 30702 |
amit.gupta |
133 |
public ResponseEntity<?> getImeis(HttpServletRequest request, @RequestParam String invoiceNumber, @RequestParam int itemId) throws ProfitMandiBusinessException {
|
| 30701 |
amit.gupta |
134 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
135 |
List<String> serialNumbers = orderRepository.selectSerialNumbersByInvoiceItem(invoiceNumber, itemId, fofoDetails.getFofoId());
|
|
|
136 |
ImeiInoviceModel imeiInoviceModel = new ImeiInoviceModel();
|
|
|
137 |
imeiInoviceModel.setInvoiceNumber(invoiceNumber);
|
|
|
138 |
imeiInoviceModel.setSerialNumbers(serialNumbers);
|
|
|
139 |
List<String> validSerialNumbers = this.getImeiValidationMap(fofoDetails.getFofoId(), imeiInoviceModel).entrySet().stream().filter(x -> x.getValue()).map(x -> x.getKey()).collect(Collectors.toList());
|
|
|
140 |
return responseSender.ok(validSerialNumbers);
|
|
|
141 |
}
|
| 30694 |
amit.gupta |
142 |
|
| 30701 |
amit.gupta |
143 |
|
| 23076 |
ashik.ali |
144 |
@RequestMapping(value = "/pendingGrnDetails", method = RequestMethod.GET)
|
| 30289 |
amit.gupta |
145 |
public String pendingGrnDetails(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ORDER_ID) int orderId, Model model) throws ProfitMandiBusinessException {
|
| 23076 |
ashik.ali |
146 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
147 |
LOGGER.info("Request Received at url [{}] with orderId [{}]", request.getRequestURI(), orderId);
|
|
|
148 |
model.addAttribute("pendingGrnDetails", purchaseService.getShippingDetailByOrderId(orderId, fofoDetails.getFofoId()));
|
|
|
149 |
return "pending-grn-details";
|
|
|
150 |
}
|
| 30289 |
amit.gupta |
151 |
|
| 30380 |
amit.gupta |
152 |
@RequestMapping(value = "/purchaseByInvoiceNumber", method = RequestMethod.GET)
|
|
|
153 |
public String purchaseByAirwayBillOrInvoiceNumber(HttpServletRequest request,
|
|
|
154 |
@RequestParam(name = ProfitMandiConstants.AIRWAY_BILL_OR_INVOICE_NUMBER)
|
|
|
155 |
String airwayBillOrInvoiceNumber, Model model) throws ProfitMandiBusinessException {
|
|
|
156 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
157 |
LOGGER.info("Request Received at url {} with airwayBillOrInvoiceNumber {}", request.getRequestURI(), airwayBillOrInvoiceNumber);
|
|
|
158 |
Map<String, Object> map = purchaseService.purchaseByInvoiceNumber(airwayBillOrInvoiceNumber, fofoDetails.getFofoId());
|
|
|
159 |
model.addAllAttributes(map);
|
|
|
160 |
return "purchase";
|
|
|
161 |
}
|
|
|
162 |
|
| 30289 |
amit.gupta |
163 |
@RequestMapping(value = "/purchase-invoice/{invoiceNumber}", method = RequestMethod.GET)
|
| 30380 |
amit.gupta |
164 |
public ResponseEntity<?> downloadInvoice(HttpServletRequest request, @PathVariable String invoiceNumber, Model model) throws Exception {
|
| 30289 |
amit.gupta |
165 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
| 30380 |
amit.gupta |
166 |
List<String> invoiceNumbers = Arrays.asList(invoiceNumber.replaceAll(" ", "").split(","));
|
| 30387 |
amit.gupta |
167 |
Map<String, List<Order>> invoiceOrdersMap = orderRepository.selectByInvoiceNumbers(invoiceNumbers).stream().collect(Collectors.groupingBy(x -> x.getInvoiceNumber()));
|
|
|
168 |
if (invoiceOrdersMap.size() > 0) {
|
|
|
169 |
if (roleManager.isAdmin(fofoDetails.getRoleIds()) || invoiceOrdersMap.get(invoiceNumbers.get(0)).get(0).getRetailerId() == fofoDetails.getFofoId()) {
|
| 30289 |
amit.gupta |
170 |
|
| 30380 |
amit.gupta |
171 |
final HttpHeaders headers = new HttpHeaders();
|
|
|
172 |
headers.setContentType(MediaType.APPLICATION_PDF);
|
| 30289 |
amit.gupta |
173 |
headers.set("Content-Type", "application/pdf");
|
|
|
174 |
headers.set("Content-disposition", "inline; filename=" + invoiceNumber + ".pdf");
|
| 30380 |
amit.gupta |
175 |
|
| 30381 |
amit.gupta |
176 |
if (false) {
|
|
|
177 |
/*InvoicePdfModel invoicePdfModel = invoiceService.getInvoicePdfModel(orders);
|
| 30380 |
amit.gupta |
178 |
|
|
|
179 |
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
180 |
List<InvoicePdfModel> pdfModels = new ArrayList<>();
|
|
|
181 |
pdfModels.add(invoicePdfModel);
|
|
|
182 |
PdfUtils.generateAndWrite(pdfModels, byteArrayOutputStream);
|
|
|
183 |
final InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
|
|
|
184 |
final InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
|
|
|
185 |
LOGGER.info("Before stream - {}", pdfModels);
|
| 30381 |
amit.gupta |
186 |
return new ResponseEntity<>(inputStreamResource, headers, HttpStatus.OK);*/
|
| 30380 |
amit.gupta |
187 |
} else {
|
|
|
188 |
Map<String, String> headersMap = new HashMap<>();
|
| 30566 |
amit.gupta |
189 |
headersMap.put("Authorization", "Basic " + Base64.getEncoder().encodeToString("smartdukaan:$smart@123#".getBytes()));
|
| 30380 |
amit.gupta |
190 |
if (invoiceNumbers.size() == 1) {
|
| 30387 |
amit.gupta |
191 |
String invoicePath = this.getInvoicePath(invoiceOrdersMap.get(invoiceNumbers.get(0)).get(0));
|
| 30570 |
amit.gupta |
192 |
HttpResponse response = restClient.getResponse("http://45.79.106.95/" + invoicePath, null, headersMap);
|
| 30380 |
amit.gupta |
193 |
InputStreamResource is = new InputStreamResource(response.getEntity().getContent());
|
|
|
194 |
return new ResponseEntity<>(is, headers, HttpStatus.OK);
|
|
|
195 |
|
|
|
196 |
} else {
|
|
|
197 |
|
|
|
198 |
ByteArrayOutputStream fos = new ByteArrayOutputStream();
|
|
|
199 |
ZipOutputStream zipOut = new ZipOutputStream(fos);
|
|
|
200 |
|
|
|
201 |
for (String invoice : invoiceNumbers) {
|
| 30387 |
amit.gupta |
202 |
List<Order> orders = invoiceOrdersMap.get(invoice);
|
| 30380 |
amit.gupta |
203 |
String invoicePath = this.getInvoicePath(orders.get(0));
|
| 30570 |
amit.gupta |
204 |
HttpResponse response = restClient.getResponse("http://45.79.106.95/" + invoicePath, null, headersMap);
|
| 30395 |
amit.gupta |
205 |
this.addFileToZip(zipOut, response.getEntity().getContent(), invoice + ".pdf");
|
| 30380 |
amit.gupta |
206 |
}
|
| 30395 |
amit.gupta |
207 |
zipOut.close();
|
| 30380 |
amit.gupta |
208 |
byte[] byteArray = fos.toByteArray();
|
| 30394 |
amit.gupta |
209 |
headers.set("Content-Type", ContentType.APPLICATION_OCTET_STREAM.getMimeType());
|
| 30389 |
amit.gupta |
210 |
headers.set("Content-disposition", "attachment; filename=invoices.zip");
|
| 30380 |
amit.gupta |
211 |
headers.setContentLength(byteArray.length);
|
| 30394 |
amit.gupta |
212 |
final InputStream inputStream = new ByteArrayInputStream(fos.toByteArray());
|
|
|
213 |
return new ResponseEntity<>(new InputStreamResource(inputStream), headers, HttpStatus.OK);
|
| 30380 |
amit.gupta |
214 |
}
|
|
|
215 |
}
|
|
|
216 |
|
| 30289 |
amit.gupta |
217 |
}
|
|
|
218 |
} else {
|
|
|
219 |
throw new ProfitMandiBusinessException("Invalid Invoice", invoiceNumber, "Please check with your manager");
|
|
|
220 |
}
|
|
|
221 |
return null;
|
|
|
222 |
}
|
|
|
223 |
|
| 30395 |
amit.gupta |
224 |
private void addFileToZip(ZipOutputStream outZip, InputStream inputStream, String fileName) throws Exception {
|
|
|
225 |
byte data[] = new byte[BUFFER_SIZE];
|
|
|
226 |
ZipEntry entry = new ZipEntry(fileName);
|
|
|
227 |
outZip.putNextEntry(entry);
|
|
|
228 |
IOUtils.copy(inputStream, outZip);
|
|
|
229 |
outZip.closeEntry();
|
|
|
230 |
}
|
|
|
231 |
|
| 30289 |
amit.gupta |
232 |
private String getInvoicePath(Order order) {
|
|
|
233 |
LocalDateTime billTime = order.getBillingTimestamp();
|
|
|
234 |
String dirPath = billTime.getYear() + "-" + (billTime.getMonthValue() - 1) + File.separator + order.getRetailerId();
|
|
|
235 |
String filename = dirPath + File.separator + order.getInvoiceNumber() + ".pdf";
|
|
|
236 |
return filename;
|
|
|
237 |
}
|
| 21636 |
ashik.ali |
238 |
}
|