| 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;
|
| 31442 |
amit.gupta |
6 |
import com.spice.profitmandi.common.util.Utils;
|
| 30289 |
amit.gupta |
7 |
import com.spice.profitmandi.common.web.client.RestClient;
|
| 30694 |
amit.gupta |
8 |
import com.spice.profitmandi.common.web.util.ResponseSender;
|
|
|
9 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
| 30289 |
amit.gupta |
10 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
| 33311 |
amit.gupta |
11 |
import com.spice.profitmandi.dao.enumuration.fofo.ScanType;
|
| 30694 |
amit.gupta |
12 |
import com.spice.profitmandi.dao.model.ImeiInoviceModel;
|
|
|
13 |
import com.spice.profitmandi.dao.repository.fofo.InventoryItemRepository;
|
| 30715 |
amit.gupta |
14 |
import com.spice.profitmandi.dao.repository.fofo.PurchaseRepository;
|
| 30289 |
amit.gupta |
15 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
|
|
16 |
import com.spice.profitmandi.service.authentication.RoleManager;
|
|
|
17 |
import com.spice.profitmandi.service.inventory.PurchaseService;
|
| 30380 |
amit.gupta |
18 |
import com.spice.profitmandi.service.transaction.invoicing.InvoiceService;
|
| 30289 |
amit.gupta |
19 |
import com.spice.profitmandi.web.model.LoginDetails;
|
|
|
20 |
import com.spice.profitmandi.web.util.CookiesProcessor;
|
| 30394 |
amit.gupta |
21 |
import org.apache.commons.io.IOUtils;
|
|
|
22 |
import org.apache.http.entity.ContentType;
|
| 30289 |
amit.gupta |
23 |
import org.apache.logging.log4j.LogManager;
|
| 23569 |
govind |
24 |
import org.apache.logging.log4j.Logger;
|
| 21574 |
ashik.ali |
25 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 30289 |
amit.gupta |
26 |
import org.springframework.core.io.InputStreamResource;
|
|
|
27 |
import org.springframework.http.HttpHeaders;
|
|
|
28 |
import org.springframework.http.HttpStatus;
|
| 30380 |
amit.gupta |
29 |
import org.springframework.http.MediaType;
|
| 30289 |
amit.gupta |
30 |
import org.springframework.http.ResponseEntity;
|
| 21555 |
kshitij.so |
31 |
import org.springframework.stereotype.Controller;
|
| 21636 |
ashik.ali |
32 |
import org.springframework.transaction.annotation.Transactional;
|
| 21574 |
ashik.ali |
33 |
import org.springframework.ui.Model;
|
| 30694 |
amit.gupta |
34 |
import org.springframework.web.bind.annotation.*;
|
| 21555 |
kshitij.so |
35 |
|
| 30289 |
amit.gupta |
36 |
import javax.servlet.http.HttpServletRequest;
|
| 32797 |
amit.gupta |
37 |
import java.io.*;
|
| 31442 |
amit.gupta |
38 |
import java.time.LocalTime;
|
|
|
39 |
import java.time.YearMonth;
|
| 30380 |
amit.gupta |
40 |
import java.util.*;
|
| 31442 |
amit.gupta |
41 |
import java.util.function.Function;
|
| 30387 |
amit.gupta |
42 |
import java.util.stream.Collectors;
|
| 30380 |
amit.gupta |
43 |
import java.util.zip.ZipEntry;
|
|
|
44 |
import java.util.zip.ZipOutputStream;
|
| 21555 |
kshitij.so |
45 |
|
|
|
46 |
@Controller
|
| 30289 |
amit.gupta |
47 |
@Transactional(rollbackFor = Throwable.class)
|
| 21582 |
kshitij.so |
48 |
public class PurchaseController {
|
| 21555 |
kshitij.so |
49 |
|
| 31437 |
amit.gupta |
50 |
private static final Logger LOGGER = LogManager.getLogger(PurchaseController.class);
|
|
|
51 |
@Autowired
|
|
|
52 |
RestClient restClient;
|
|
|
53 |
@Autowired
|
|
|
54 |
private PurchaseService purchaseService;
|
|
|
55 |
@Autowired
|
|
|
56 |
private PurchaseRepository purchaseRepository;
|
|
|
57 |
@Autowired
|
|
|
58 |
private InventoryItemRepository inventoryItemRepository;
|
|
|
59 |
@Autowired
|
|
|
60 |
private OrderRepository orderRepository;
|
|
|
61 |
@Autowired
|
|
|
62 |
private RoleManager roleManager;
|
| 30289 |
amit.gupta |
63 |
|
| 31437 |
amit.gupta |
64 |
@Autowired
|
|
|
65 |
private ResponseSender responseSender;
|
| 30289 |
amit.gupta |
66 |
|
| 31437 |
amit.gupta |
67 |
@Autowired
|
|
|
68 |
private CookiesProcessor cookiesProcessor;
|
| 30715 |
amit.gupta |
69 |
|
| 31437 |
amit.gupta |
70 |
@Autowired
|
|
|
71 |
private InvoiceService invoiceService;
|
| 30694 |
amit.gupta |
72 |
|
| 31437 |
amit.gupta |
73 |
@RequestMapping(value = "/purchase", method = RequestMethod.GET)
|
|
|
74 |
public String purchase(HttpServletRequest request) throws Exception {
|
|
|
75 |
return "purchase";
|
|
|
76 |
}
|
| 21640 |
kshitij.so |
77 |
|
| 31437 |
amit.gupta |
78 |
@RequestMapping(value = "/pendingGrn", method = RequestMethod.GET)
|
|
|
79 |
public String pendingGrn(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
|
|
|
80 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
81 |
LOGGER.info("Request Received at url {}", request.getRequestURI());
|
|
|
82 |
List<Order> pendingGrns = orderRepository.selectShipmentToDeliveredByRetailerId(fofoDetails.getFofoId());
|
|
|
83 |
model.addAttribute("pendingGrns", pendingGrns);
|
|
|
84 |
return "pending-grn";
|
|
|
85 |
}
|
| 30289 |
amit.gupta |
86 |
|
| 31437 |
amit.gupta |
87 |
@RequestMapping(value = "/purchase/validate-imeis", method = RequestMethod.POST)
|
|
|
88 |
public ResponseEntity<?> validatePurchaseImeis(HttpServletRequest request, @RequestBody ImeiInoviceModel imeiInoviceModel, Model model) throws ProfitMandiBusinessException {
|
|
|
89 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
90 |
return responseSender.ok(this.getImeiValidationMap(fofoDetails.getFofoId(), imeiInoviceModel));
|
|
|
91 |
}
|
| 30694 |
amit.gupta |
92 |
|
| 31437 |
amit.gupta |
93 |
private Map<String, Boolean> getImeiValidationMap(int fofoId, ImeiInoviceModel imeiInoviceModel) throws ProfitMandiBusinessException {
|
|
|
94 |
LOGGER.info("serialNumbers - {}", imeiInoviceModel.getSerialNumbers());
|
|
|
95 |
Set<String> serialNumbers = new HashSet<>(orderRepository.selectSerialNumbers(imeiInoviceModel.getInvoiceNumber(), fofoId, imeiInoviceModel.getSerialNumbers()));
|
|
|
96 |
Map<String, Boolean> imeiValidationMap = new HashMap<>();
|
|
|
97 |
imeiInoviceModel.getSerialNumbers().stream().forEach(x -> {
|
|
|
98 |
imeiValidationMap.put(x, true);
|
|
|
99 |
});
|
|
|
100 |
if (serialNumbers.size() > 0) {
|
|
|
101 |
List<InventoryItem> inventoryItems = inventoryItemRepository.selectByFofoIdSerialNumbers(fofoId, new HashSet<>(serialNumbers), false);
|
| 33311 |
amit.gupta |
102 |
//Should consider sold out imeis from previous purchases as grned as well and considered invalidated.
|
|
|
103 |
Set<String> grnedSerialNumberSet = inventoryItems.stream().filter(x->(x.getGoodQuantity() + x.getBadQuantity())==1 || x.getLastScanType().equals(ScanType.SALE)).map(x->x.getSerialNumber()).collect(Collectors.toSet());
|
| 31437 |
amit.gupta |
104 |
imeiInoviceModel.getSerialNumbers().stream().forEach(imei -> {
|
|
|
105 |
if (grnedSerialNumberSet.contains(imei)) {
|
|
|
106 |
imeiValidationMap.put(imei, false);
|
|
|
107 |
}
|
|
|
108 |
});
|
|
|
109 |
}
|
|
|
110 |
return imeiValidationMap;
|
|
|
111 |
}
|
| 21640 |
kshitij.so |
112 |
|
| 31437 |
amit.gupta |
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 |
}
|
| 30289 |
amit.gupta |
120 |
|
| 31437 |
amit.gupta |
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 |
}
|
| 30289 |
amit.gupta |
131 |
|
| 31437 |
amit.gupta |
132 |
@RequestMapping(value = "/purchase/get-imeis", method = RequestMethod.GET)
|
|
|
133 |
public ResponseEntity<?> getImeis(HttpServletRequest request, @RequestParam String invoiceNumber, @RequestParam int itemId) throws ProfitMandiBusinessException {
|
|
|
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 |
|
| 30715 |
amit.gupta |
143 |
|
| 31437 |
amit.gupta |
144 |
@RequestMapping(value = "/pendingGrnDetails", method = RequestMethod.GET)
|
|
|
145 |
public String pendingGrnDetails(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ORDER_ID) int orderId, Model model) throws ProfitMandiBusinessException {
|
|
|
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 |
}
|
| 30694 |
amit.gupta |
151 |
|
| 31437 |
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 |
}
|
| 30694 |
amit.gupta |
162 |
|
| 31442 |
amit.gupta |
163 |
|
| 31437 |
amit.gupta |
164 |
@RequestMapping(value = "/purchase-invoice/{invoiceNumber}", method = RequestMethod.GET)
|
|
|
165 |
public ResponseEntity<?> downloadInvoice(HttpServletRequest request, @PathVariable String invoiceNumber, Model model) throws Exception {
|
|
|
166 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
| 31442 |
amit.gupta |
167 |
List<String> invoiceNumbers = null;
|
|
|
168 |
try {
|
|
|
169 |
YearMonth ym = YearMonth.parse(invoiceNumber);
|
|
|
170 |
if (!roleManager.isAdmin(fofoDetails.getRoleIds())) {
|
|
|
171 |
List<Order> orders = orderRepository.selectAllByBillingDatesBetween(fofoDetails.getFofoId(), ym.atDay(1).atStartOfDay(),
|
|
|
172 |
ym.atEndOfMonth().atTime(LocalTime.MAX));
|
|
|
173 |
invoiceNumbers = orders.stream().map(x -> x.getInvoiceNumber()).filter(Utils.distinctByKey(Function.identity())).collect(Collectors.toList());
|
|
|
174 |
}
|
|
|
175 |
} catch (Exception e) {
|
|
|
176 |
invoiceNumbers = Arrays.asList(invoiceNumber.replaceAll(" ", "").split(","));
|
|
|
177 |
}
|
| 32989 |
amit.gupta |
178 |
LOGGER.info("Invoice Number - {}", invoiceNumber);
|
| 31437 |
amit.gupta |
179 |
Map<String, List<Order>> invoiceOrdersMap = orderRepository.selectByInvoiceNumbers(invoiceNumbers).stream().collect(Collectors.groupingBy(x -> x.getInvoiceNumber()));
|
|
|
180 |
if (invoiceOrdersMap.size() > 0) {
|
|
|
181 |
if (roleManager.isAdmin(fofoDetails.getRoleIds()) || invoiceOrdersMap.get(invoiceNumbers.get(0)).get(0).getRetailerId() == fofoDetails.getFofoId()) {
|
| 31438 |
amit.gupta |
182 |
if (invoiceOrdersMap.get(invoiceNumbers.get(0)).get(0).getRetailerId() == fofoDetails.getFofoId()) {
|
|
|
183 |
invoiceOrdersMap = invoiceOrdersMap.entrySet().stream().filter(x -> x.getValue().get(0).getRetailerId() == fofoDetails.getFofoId()).collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
|
|
|
184 |
}
|
| 30694 |
amit.gupta |
185 |
|
| 31437 |
amit.gupta |
186 |
final HttpHeaders headers = new HttpHeaders();
|
| 30694 |
amit.gupta |
187 |
|
| 31437 |
amit.gupta |
188 |
if (invoiceNumbers.size() == 1) {
|
| 33006 |
amit.gupta |
189 |
Order order = invoiceOrdersMap.get(invoiceNumbers.get(0)).get(0);
|
| 32988 |
amit.gupta |
190 |
File invoiceFile = invoiceService.getInvoiceFile(order);
|
| 32797 |
amit.gupta |
191 |
|
| 31437 |
amit.gupta |
192 |
headers.setContentType(MediaType.APPLICATION_PDF);
|
|
|
193 |
headers.set("Content-Type", "application/pdf");
|
|
|
194 |
headers.set("Content-disposition", "inline; filename=" + invoiceNumber + ".pdf");
|
| 32797 |
amit.gupta |
195 |
InputStreamResource is = new InputStreamResource(new FileInputStream(invoiceFile));
|
| 31437 |
amit.gupta |
196 |
return new ResponseEntity<>(is, headers, HttpStatus.OK);
|
| 30289 |
amit.gupta |
197 |
|
| 31437 |
amit.gupta |
198 |
} else {
|
| 30380 |
amit.gupta |
199 |
|
| 31437 |
amit.gupta |
200 |
ByteArrayOutputStream fos = new ByteArrayOutputStream();
|
|
|
201 |
ZipOutputStream zipOut = new ZipOutputStream(fos);
|
| 30289 |
amit.gupta |
202 |
|
| 32988 |
amit.gupta |
203 |
for (String singleInvoiceNumber : invoiceNumbers) {
|
| 33311 |
amit.gupta |
204 |
LOGGER.info("Single Invoice Number - {}", singleInvoiceNumber);
|
|
|
205 |
Order order = invoiceOrdersMap.get(singleInvoiceNumber).get(0);
|
| 32988 |
amit.gupta |
206 |
File invoiceFile = invoiceService.getInvoiceFile(order);
|
|
|
207 |
this.addFileToZip(zipOut, new FileInputStream(invoiceFile), singleInvoiceNumber + ".pdf");
|
| 31437 |
amit.gupta |
208 |
}
|
|
|
209 |
zipOut.close();
|
|
|
210 |
byte[] byteArray = fos.toByteArray();
|
|
|
211 |
headers.set("Content-Type", ContentType.APPLICATION_OCTET_STREAM.getMimeType());
|
|
|
212 |
headers.set("Content-disposition", "attachment; filename=invoices.zip");
|
|
|
213 |
headers.setContentLength(byteArray.length);
|
|
|
214 |
final InputStream inputStream = new ByteArrayInputStream(fos.toByteArray());
|
|
|
215 |
return new ResponseEntity<>(new InputStreamResource(inputStream), headers, HttpStatus.OK);
|
|
|
216 |
}
|
| 30380 |
amit.gupta |
217 |
|
| 31437 |
amit.gupta |
218 |
}
|
|
|
219 |
} else {
|
|
|
220 |
throw new ProfitMandiBusinessException("Invalid Invoice", invoiceNumber, "Please check with your manager");
|
|
|
221 |
}
|
|
|
222 |
return null;
|
|
|
223 |
}
|
| 30380 |
amit.gupta |
224 |
|
| 31437 |
amit.gupta |
225 |
private void addFileToZip(ZipOutputStream outZip, InputStream inputStream, String fileName) throws Exception {
|
|
|
226 |
ZipEntry entry = new ZipEntry(fileName);
|
|
|
227 |
outZip.putNextEntry(entry);
|
|
|
228 |
IOUtils.copy(inputStream, outZip);
|
|
|
229 |
outZip.closeEntry();
|
|
|
230 |
}
|
| 30380 |
amit.gupta |
231 |
|
| 21636 |
ashik.ali |
232 |
}
|