Subversion Repositories SmartDukaan

Rev

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

Rev 32654 Rev 32797
Line 32... Line 32...
32
import org.springframework.transaction.annotation.Transactional;
32
import org.springframework.transaction.annotation.Transactional;
33
import org.springframework.ui.Model;
33
import org.springframework.ui.Model;
34
import org.springframework.web.bind.annotation.*;
34
import org.springframework.web.bind.annotation.*;
35
 
35
 
36
import javax.servlet.http.HttpServletRequest;
36
import javax.servlet.http.HttpServletRequest;
37
import java.io.ByteArrayInputStream;
-
 
38
import java.io.ByteArrayOutputStream;
-
 
39
import java.io.File;
37
import java.io.*;
40
import java.io.InputStream;
-
 
41
import java.time.LocalDateTime;
38
import java.time.LocalDateTime;
42
import java.time.LocalTime;
39
import java.time.LocalTime;
43
import java.time.YearMonth;
40
import java.time.YearMonth;
44
import java.util.*;
41
import java.util.*;
45
import java.util.function.Function;
42
import java.util.function.Function;
Line 192... Line 189...
192
 
189
 
193
                Map<String, String> headersMap = new HashMap<>();
190
                Map<String, String> headersMap = new HashMap<>();
194
                headersMap.put("Authorization", "Basic " + Base64.getEncoder().encodeToString("smartdukaan:$smart@123#".getBytes()));
191
                headersMap.put("Authorization", "Basic " + Base64.getEncoder().encodeToString("smartdukaan:$smart@123#".getBytes()));
195
                if (invoiceNumbers.size() == 1) {
192
                if (invoiceNumbers.size() == 1) {
196
                    Order order = invoiceOrdersMap.get(invoiceNumbers.get(0)).get(0);
193
                    Order order = invoiceOrdersMap.get(invoiceNumbers.get(0)).get(0);
197
                    String invoicePath = this.getInvoicePath(invoiceOrdersMap.get(invoiceNumbers.get(0)).get(0));
194
                    File invoiceFile = this.getInvoiceFile(invoiceOrdersMap.get(invoiceNumbers.get(0)).get(0));
198
                    HttpResponse response;
-
 
199
                    try {
-
 
200
                        response = restClient.getResponse("http://192.168.202.117/" + invoicePath, null, headersMap);
-
 
201
                    } catch (Exception e) {
-
 
202
                        invoicePath = this.getInvoicePathLastMonth(order);
-
 
203
                        response = restClient.getResponse("http://192.168.202.117/" + invoicePath, null, headersMap);
-
 
204
                    }
-
 
-
 
195
 
205
                    headers.setContentType(MediaType.APPLICATION_PDF);
196
                    headers.setContentType(MediaType.APPLICATION_PDF);
206
                    headers.set("Content-Type", "application/pdf");
197
                    headers.set("Content-Type", "application/pdf");
207
                    headers.set("Content-disposition", "inline; filename=" + invoiceNumber + ".pdf");
198
                    headers.set("Content-disposition", "inline; filename=" + invoiceNumber + ".pdf");
208
                    InputStreamResource is = new InputStreamResource(response.getEntity().getContent());
199
                    InputStreamResource is = new InputStreamResource(new FileInputStream(invoiceFile));
209
                    return new ResponseEntity<>(is, headers, HttpStatus.OK);
200
                    return new ResponseEntity<>(is, headers, HttpStatus.OK);
210
 
201
 
211
                } else {
202
                } else {
212
 
203
 
213
                    ByteArrayOutputStream fos = new ByteArrayOutputStream();
204
                    ByteArrayOutputStream fos = new ByteArrayOutputStream();
214
                    ZipOutputStream zipOut = new ZipOutputStream(fos);
205
                    ZipOutputStream zipOut = new ZipOutputStream(fos);
215
 
206
 
216
                    for (String invoice : invoiceNumbers) {
207
                    for (String invoice : invoiceNumbers) {
217
                        Order order = invoiceOrdersMap.get(invoiceNumbers.get(0)).get(0);
208
                        Order order = invoiceOrdersMap.get(invoiceNumbers.get(0)).get(0);
218
                        String invoicePath = this.getInvoicePath(order);
209
                        File invoiceFile = this.getInvoiceFile(order);
219
                        HttpResponse response;
-
 
220
                        try {
-
 
221
                            response = restClient.getResponse("http://192.168.202.117/" + invoicePath, null, headersMap);
-
 
222
                        } catch (Exception e) {
-
 
223
                            invoicePath = this.getInvoicePathLastMonth(order);
-
 
224
                            response = restClient.getResponse("http://192.168.202.117/" + invoicePath, null, headersMap);
-
 
225
                        }
-
 
226
                        this.addFileToZip(zipOut, response.getEntity().getContent(), invoice + ".pdf");
210
                        this.addFileToZip(zipOut, new FileInputStream(invoiceFile), invoice + ".pdf");
227
                    }
211
                    }
228
                    zipOut.close();
212
                    zipOut.close();
229
                    byte[] byteArray = fos.toByteArray();
213
                    byte[] byteArray = fos.toByteArray();
230
                    headers.set("Content-Type", ContentType.APPLICATION_OCTET_STREAM.getMimeType());
214
                    headers.set("Content-Type", ContentType.APPLICATION_OCTET_STREAM.getMimeType());
231
                    headers.set("Content-disposition", "attachment; filename=invoices.zip");
215
                    headers.set("Content-disposition", "attachment; filename=invoices.zip");
Line 246... Line 230...
246
        outZip.putNextEntry(entry);
230
        outZip.putNextEntry(entry);
247
        IOUtils.copy(inputStream, outZip);
231
        IOUtils.copy(inputStream, outZip);
248
        outZip.closeEntry();
232
        outZip.closeEntry();
249
    }
233
    }
250
 
234
 
251
    private String getInvoicePath(Order order) {
235
    private File getInvoiceFile(Order order) {
252
        LocalDateTime billTime = order.getBillingTimestamp();
236
        LocalDateTime billTime = order.getBillingTimestamp();
253
        String dirPath = billTime.getYear() + "-" + (billTime.getMonthValue() - 1) + File.separator + order.getRetailerId();
237
        String dirPath = billTime.getYear() + "-" + (billTime.getMonthValue() - 1) + File.separator + order.getRetailerId();
254
        String filename = dirPath + File.separator + order.getInvoiceNumber() + ".pdf";
238
        String filename = dirPath + File.separator + order.getInvoiceNumber() + ".pdf";
-
 
239
        String invoiceFilePath = "/SaholicInvoices/" + filename;
-
 
240
        File f = new File(invoiceFilePath);
-
 
241
        if(!f.exists()) {
-
 
242
            filename = this.getInvoicePathLastMonth(order);
-
 
243
            invoiceFilePath = "/SaholicInvoices/" + filename;
-
 
244
        }
-
 
245
        f = new File(invoiceFilePath);
255
        return filename;
246
        return f;
256
    }
247
    }
257
 
248
 
258
    private String getInvoicePathLastMonth(Order order) {
249
    private String getInvoicePathLastMonth(Order order) {
259
        LocalDateTime billTime = order.getBillingTimestamp();
250
        LocalDateTime billTime = order.getBillingTimestamp();
260
        billTime = billTime.plusMonths(1);
251
        billTime = billTime.plusMonths(1);