Subversion Repositories SmartDukaan

Rev

Rev 30289 | Rev 30381 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 30289 Rev 30380
Line 1... Line 1...
1
package com.spice.profitmandi.web.controller;
1
package com.spice.profitmandi.web.controller;
2
 
2
 
3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
-
 
4
import com.spice.profitmandi.common.model.InvoicePdfModel;
4
import com.spice.profitmandi.common.model.ProfitMandiConstants;
5
import com.spice.profitmandi.common.model.ProfitMandiConstants;
-
 
6
import com.spice.profitmandi.common.util.PdfUtils;
5
import com.spice.profitmandi.common.web.client.RestClient;
7
import com.spice.profitmandi.common.web.client.RestClient;
6
import com.spice.profitmandi.dao.entity.transaction.Order;
8
import com.spice.profitmandi.dao.entity.transaction.Order;
7
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
9
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
8
import com.spice.profitmandi.service.authentication.RoleManager;
10
import com.spice.profitmandi.service.authentication.RoleManager;
9
import com.spice.profitmandi.service.inventory.PurchaseService;
11
import com.spice.profitmandi.service.inventory.PurchaseService;
-
 
12
import com.spice.profitmandi.service.transaction.invoicing.InvoiceService;
10
import com.spice.profitmandi.web.model.LoginDetails;
13
import com.spice.profitmandi.web.model.LoginDetails;
11
import com.spice.profitmandi.web.util.CookiesProcessor;
14
import com.spice.profitmandi.web.util.CookiesProcessor;
12
import org.apache.http.HttpResponse;
15
import org.apache.http.HttpResponse;
13
import org.apache.logging.log4j.LogManager;
16
import org.apache.logging.log4j.LogManager;
14
import org.apache.logging.log4j.Logger;
17
import org.apache.logging.log4j.Logger;
15
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.beans.factory.annotation.Autowired;
-
 
19
import org.springframework.core.io.ByteArrayResource;
16
import org.springframework.core.io.InputStreamResource;
20
import org.springframework.core.io.InputStreamResource;
17
import org.springframework.http.HttpHeaders;
21
import org.springframework.http.HttpHeaders;
18
import org.springframework.http.HttpStatus;
22
import org.springframework.http.HttpStatus;
-
 
23
import org.springframework.http.MediaType;
19
import org.springframework.http.ResponseEntity;
24
import org.springframework.http.ResponseEntity;
20
import org.springframework.stereotype.Controller;
25
import org.springframework.stereotype.Controller;
21
import org.springframework.transaction.annotation.Transactional;
26
import org.springframework.transaction.annotation.Transactional;
22
import org.springframework.ui.Model;
27
import org.springframework.ui.Model;
23
import org.springframework.web.bind.annotation.PathVariable;
28
import org.springframework.web.bind.annotation.PathVariable;
24
import org.springframework.web.bind.annotation.RequestMapping;
29
import org.springframework.web.bind.annotation.RequestMapping;
25
import org.springframework.web.bind.annotation.RequestMethod;
30
import org.springframework.web.bind.annotation.RequestMethod;
26
import org.springframework.web.bind.annotation.RequestParam;
31
import org.springframework.web.bind.annotation.RequestParam;
27
 
32
 
28
import javax.servlet.http.HttpServletRequest;
33
import javax.servlet.http.HttpServletRequest;
-
 
34
import java.io.ByteArrayInputStream;
-
 
35
import java.io.ByteArrayOutputStream;
29
import java.io.File;
36
import java.io.File;
-
 
37
import java.io.InputStream;
30
import java.time.LocalDateTime;
38
import java.time.LocalDateTime;
31
import java.util.Base64;
39
import java.util.*;
32
import java.util.HashMap;
40
import java.util.zip.ZipEntry;
33
import java.util.List;
41
import java.util.zip.ZipOutputStream;
34
import java.util.Map;
-
 
35
 
42
 
36
@Controller
43
@Controller
37
@Transactional(rollbackFor = Throwable.class)
44
@Transactional(rollbackFor = Throwable.class)
38
public class PurchaseController {
45
public class PurchaseController {
39
 
46
 
Line 51... Line 58...
51
	private RoleManager roleManager;
58
	private RoleManager roleManager;
52
 
59
 
53
	@Autowired
60
	@Autowired
54
	private CookiesProcessor cookiesProcessor;
61
	private CookiesProcessor cookiesProcessor;
55
 
62
 
56
	@RequestMapping(value = "/purchaseByInvoiceNumber", method = RequestMethod.GET)
-
 
57
	public String purchaseByAirwayBillOrInvoiceNumber(HttpServletRequest request,
-
 
58
													  @RequestParam(name = ProfitMandiConstants.AIRWAY_BILL_OR_INVOICE_NUMBER)
-
 
59
															  String airwayBillOrInvoiceNumber, Model model) throws ProfitMandiBusinessException {
-
 
60
		LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
-
 
61
		LOGGER.info("Request Received at url {} with airwayBillOrInvoiceNumber {}", request.getRequestURI(), airwayBillOrInvoiceNumber);
-
 
62
		Map<String, Object> map = purchaseService.purchaseByInvoiceNumber(airwayBillOrInvoiceNumber, fofoDetails.getFofoId());
-
 
63
		model.addAllAttributes(map);
63
	@Autowired
64
		return "purchase";
64
	private InvoiceService invoiceService;
65
	}
-
 
66
 
65
 
67
	@RequestMapping(value = "/purchase", method = RequestMethod.GET)
66
	@RequestMapping(value = "/purchase", method = RequestMethod.GET)
68
	public String purchase(HttpServletRequest request) throws Exception {
67
	public String purchase(HttpServletRequest request) throws Exception {
69
		return "purchase";
68
		return "purchase";
70
	}
69
	}
Line 84... Line 83...
84
		LOGGER.info("Request Received at url [{}] with orderId [{}]", request.getRequestURI(), orderId);
83
		LOGGER.info("Request Received at url [{}] with orderId [{}]", request.getRequestURI(), orderId);
85
		model.addAttribute("pendingGrnDetails", purchaseService.getShippingDetailByOrderId(orderId, fofoDetails.getFofoId()));
84
		model.addAttribute("pendingGrnDetails", purchaseService.getShippingDetailByOrderId(orderId, fofoDetails.getFofoId()));
86
		return "pending-grn-details";
85
		return "pending-grn-details";
87
	}
86
	}
88
 
87
 
-
 
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
 
89
	@RequestMapping(value = "/purchase-invoice/{invoiceNumber}", method = RequestMethod.GET)
99
	@RequestMapping(value = "/purchase-invoice/{invoiceNumber}", method = RequestMethod.GET)
90
	public ResponseEntity<InputStreamResource> downloadInvoice(HttpServletRequest request, @PathVariable String invoiceNumber, Model model) throws Exception {
100
	public ResponseEntity<?> downloadInvoice(HttpServletRequest request, @PathVariable String invoiceNumber, Model model) throws Exception {
91
		LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
101
		LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
-
 
102
		List<String> invoiceNumbers = Arrays.asList(invoiceNumber.replaceAll(" ", "").split(","));
92
		List<Order> orders = orderRepository.selectByInvoiceNumber(invoiceNumber);
103
		List<Order> orders = orderRepository.selectByInvoiceNumbers(invoiceNumbers);
93
		if (orders.size() > 0) {
104
		if (orders.size() > 0) {
94
			if (roleManager.isAdmin(fofoDetails.getRoleIds()) || orders.get(0).getRetailerId() == fofoDetails.getFofoId()) {
105
			if (roleManager.isAdmin(fofoDetails.getRoleIds()) || orders.get(0).getRetailerId() == fofoDetails.getFofoId()) {
95
				String invoicePath = this.getInvoicePath(orders.get(0));
-
 
96
				Map<String, String> headersMap = new HashMap<>();
-
 
97
				headersMap.put("Authorization", "Basic " + Base64.getEncoder().encodeToString("sd:smart@123".getBytes()));
-
 
98
				HttpResponse response = restClient.getResponse("http://50.116.3.101/" + invoicePath, null, headersMap);
-
 
99
 
-
 
100
 
106
 
101
				HttpHeaders headers = new HttpHeaders();
107
				final HttpHeaders headers = new HttpHeaders();
102
				InputStreamResource is = new InputStreamResource(response.getEntity().getContent());
108
				headers.setContentType(MediaType.APPLICATION_PDF);
103
				headers.set("Content-Type", "application/pdf");
109
				headers.set("Content-Type", "application/pdf");
104
				headers.set("Content-disposition", "inline; filename=" + invoiceNumber + ".pdf");
110
				headers.set("Content-disposition", "inline; filename=" + invoiceNumber + ".pdf");
-
 
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);
105
				headers.setContentLength(response.getEntity().getContentLength());
129
						InputStreamResource is = new InputStreamResource(response.getEntity().getContent());
106
				return new ResponseEntity<>(is, headers, HttpStatus.OK);
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
 
107
			}
159
			}
108
		} else {
160
		} else {
109
			throw new ProfitMandiBusinessException("Invalid Invoice", invoiceNumber, "Please check with your manager");
161
			throw new ProfitMandiBusinessException("Invalid Invoice", invoiceNumber, "Please check with your manager");
110
		}
162
		}
111
		return null;
163
		return null;