Subversion Repositories SmartDukaan

Rev

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

Rev 30382 Rev 30387
Line 29... Line 29...
29
import org.springframework.web.bind.annotation.RequestParam;
29
import org.springframework.web.bind.annotation.RequestParam;
30
 
30
 
31
import javax.servlet.http.HttpServletRequest;
31
import javax.servlet.http.HttpServletRequest;
32
import java.io.ByteArrayOutputStream;
32
import java.io.ByteArrayOutputStream;
33
import java.io.File;
33
import java.io.File;
-
 
34
import java.io.InputStream;
34
import java.time.LocalDateTime;
35
import java.time.LocalDateTime;
35
import java.util.*;
36
import java.util.*;
-
 
37
import java.util.stream.Collectors;
36
import java.util.zip.ZipEntry;
38
import java.util.zip.ZipEntry;
37
import java.util.zip.ZipOutputStream;
39
import java.util.zip.ZipOutputStream;
38
 
40
 
39
@Controller
41
@Controller
40
@Transactional(rollbackFor = Throwable.class)
42
@Transactional(rollbackFor = Throwable.class)
Line 94... Line 96...
94
 
96
 
95
	@RequestMapping(value = "/purchase-invoice/{invoiceNumber}", method = RequestMethod.GET)
97
	@RequestMapping(value = "/purchase-invoice/{invoiceNumber}", method = RequestMethod.GET)
96
	public ResponseEntity<?> downloadInvoice(HttpServletRequest request, @PathVariable String invoiceNumber, Model model) throws Exception {
98
	public ResponseEntity<?> downloadInvoice(HttpServletRequest request, @PathVariable String invoiceNumber, Model model) throws Exception {
97
		LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
99
		LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
98
		List<String> invoiceNumbers = Arrays.asList(invoiceNumber.replaceAll(" ", "").split(","));
100
		List<String> invoiceNumbers = Arrays.asList(invoiceNumber.replaceAll(" ", "").split(","));
99
		List<Order> orders = orderRepository.selectByInvoiceNumbers(invoiceNumbers);
101
		Map<String, List<Order>> invoiceOrdersMap = orderRepository.selectByInvoiceNumbers(invoiceNumbers).stream().collect(Collectors.groupingBy(x -> x.getInvoiceNumber()));
100
		if (orders.size() > 0) {
102
		if (invoiceOrdersMap.size() > 0) {
101
			if (roleManager.isAdmin(fofoDetails.getRoleIds()) || orders.get(0).getRetailerId() == fofoDetails.getFofoId()) {
103
			if (roleManager.isAdmin(fofoDetails.getRoleIds()) || invoiceOrdersMap.get(invoiceNumbers.get(0)).get(0).getRetailerId() == fofoDetails.getFofoId()) {
102
 
104
 
103
				final HttpHeaders headers = new HttpHeaders();
105
				final HttpHeaders headers = new HttpHeaders();
104
				headers.setContentType(MediaType.APPLICATION_PDF);
106
				headers.setContentType(MediaType.APPLICATION_PDF);
105
				headers.set("Content-Type", "application/pdf");
107
				headers.set("Content-Type", "application/pdf");
106
				headers.set("Content-disposition", "inline; filename=" + invoiceNumber + ".pdf");
108
				headers.set("Content-disposition", "inline; filename=" + invoiceNumber + ".pdf");
Line 118... Line 120...
118
					return new ResponseEntity<>(inputStreamResource, headers, HttpStatus.OK);*/
120
					return new ResponseEntity<>(inputStreamResource, headers, HttpStatus.OK);*/
119
				} else {
121
				} else {
120
					Map<String, String> headersMap = new HashMap<>();
122
					Map<String, String> headersMap = new HashMap<>();
121
					headersMap.put("Authorization", "Basic " + Base64.getEncoder().encodeToString("sd:smart@123".getBytes()));
123
					headersMap.put("Authorization", "Basic " + Base64.getEncoder().encodeToString("sd:smart@123".getBytes()));
122
					if (invoiceNumbers.size() == 1) {
124
					if (invoiceNumbers.size() == 1) {
123
						String invoicePath = this.getInvoicePath(orders.get(0));
125
						String invoicePath = this.getInvoicePath(invoiceOrdersMap.get(invoiceNumbers.get(0)).get(0));
124
						HttpResponse response = restClient.getResponse("http://50.116.3.101/" + invoicePath, null, headersMap);
126
						HttpResponse response = restClient.getResponse("http://50.116.3.101/" + invoicePath, null, headersMap);
125
						InputStreamResource is = new InputStreamResource(response.getEntity().getContent());
127
						InputStreamResource is = new InputStreamResource(response.getEntity().getContent());
126
						return new ResponseEntity<>(is, headers, HttpStatus.OK);
128
						return new ResponseEntity<>(is, headers, HttpStatus.OK);
127
 
129
 
128
					} else {
130
					} else {
129
 
131
 
130
						ByteArrayOutputStream fos = new ByteArrayOutputStream();
132
						ByteArrayOutputStream fos = new ByteArrayOutputStream();
131
						ZipOutputStream zipOut = new ZipOutputStream(fos);
133
						ZipOutputStream zipOut = new ZipOutputStream(fos);
132
 
134
 
133
						for (String invoice : invoiceNumbers) {
135
						for (String invoice : invoiceNumbers) {
-
 
136
							List<Order> orders = invoiceOrdersMap.get(invoice);
134
							String invoicePath = this.getInvoicePath(orders.get(0));
137
							String invoicePath = this.getInvoicePath(orders.get(0));
135
							HttpResponse response = restClient.getResponse("http://50.116.3.101/" + invoicePath, null, headersMap);
138
							HttpResponse response = restClient.getResponse("http://50.116.3.101/" + invoicePath, null, headersMap);
136
							ZipEntry zipEntry = new ZipEntry(invoice + ".pdf");
139
							ZipEntry zipEntry = new ZipEntry(invoice + ".pdf");
137
							zipOut.putNextEntry(zipEntry);
140
							zipOut.putNextEntry(zipEntry);
138
							byte[] bytes = new byte[1024];
141
							byte[] bytes = new byte[1024];
139
							int length;
142
							int length;
-
 
143
							InputStream is = response.getEntity().getContent();
140
							while ((length = response.getEntity().getContent().read(bytes)) >= 0) {
144
							while ((length = is.read(bytes)) >= 0) {
141
								zipOut.write(bytes, 0, length);
145
								zipOut.write(bytes, 0, length);
142
							}
146
							}
143
							response.getEntity().getContent().close();
147
							is.close();
144
						}
148
						}
145
						byte[] byteArray = fos.toByteArray();
149
						byte[] byteArray = fos.toByteArray();
146
						headers.set("Content-Type", "application/octet-stream");
150
						headers.set("Content-Type", "application/octet-stream");
147
						headers.set("Content-disposition", "inline; filename=invoices.zip");
151
						headers.set("Content-disposition", "inline; filename=invoices.zip");
148
						headers.setContentLength(byteArray.length);
152
						headers.setContentLength(byteArray.length);