Subversion Repositories SmartDukaan

Rev

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

Rev 32797 Rev 32988
Line 16... Line 16...
16
import com.spice.profitmandi.service.inventory.PurchaseService;
16
import com.spice.profitmandi.service.inventory.PurchaseService;
17
import com.spice.profitmandi.service.transaction.invoicing.InvoiceService;
17
import com.spice.profitmandi.service.transaction.invoicing.InvoiceService;
18
import com.spice.profitmandi.web.model.LoginDetails;
18
import com.spice.profitmandi.web.model.LoginDetails;
19
import com.spice.profitmandi.web.util.CookiesProcessor;
19
import com.spice.profitmandi.web.util.CookiesProcessor;
20
import org.apache.commons.io.IOUtils;
20
import org.apache.commons.io.IOUtils;
21
import org.apache.http.HttpResponse;
-
 
22
import org.apache.http.entity.ContentType;
21
import org.apache.http.entity.ContentType;
23
import org.apache.logging.log4j.LogManager;
22
import org.apache.logging.log4j.LogManager;
24
import org.apache.logging.log4j.Logger;
23
import org.apache.logging.log4j.Logger;
25
import org.springframework.beans.factory.annotation.Autowired;
24
import org.springframework.beans.factory.annotation.Autowired;
26
import org.springframework.core.io.InputStreamResource;
25
import org.springframework.core.io.InputStreamResource;
Line 33... Line 32...
33
import org.springframework.ui.Model;
32
import org.springframework.ui.Model;
34
import org.springframework.web.bind.annotation.*;
33
import org.springframework.web.bind.annotation.*;
35
 
34
 
36
import javax.servlet.http.HttpServletRequest;
35
import javax.servlet.http.HttpServletRequest;
37
import java.io.*;
36
import java.io.*;
38
import java.time.LocalDateTime;
-
 
39
import java.time.LocalTime;
37
import java.time.LocalTime;
40
import java.time.YearMonth;
38
import java.time.YearMonth;
41
import java.util.*;
39
import java.util.*;
42
import java.util.function.Function;
40
import java.util.function.Function;
43
import java.util.stream.Collectors;
41
import java.util.stream.Collectors;
Line 184... Line 182...
184
                    invoiceOrdersMap = invoiceOrdersMap.entrySet().stream().filter(x -> x.getValue().get(0).getRetailerId() == fofoDetails.getFofoId()).collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
182
                    invoiceOrdersMap = invoiceOrdersMap.entrySet().stream().filter(x -> x.getValue().get(0).getRetailerId() == fofoDetails.getFofoId()).collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
185
                }
183
                }
186
 
184
 
187
                final HttpHeaders headers = new HttpHeaders();
185
                final HttpHeaders headers = new HttpHeaders();
188
 
186
 
189
 
-
 
190
                Map<String, String> headersMap = new HashMap<>();
187
                Order order = invoiceOrdersMap.get(invoiceNumbers.get(0)).get(0);
191
                headersMap.put("Authorization", "Basic " + Base64.getEncoder().encodeToString("smartdukaan:$smart@123#".getBytes()));
-
 
192
                if (invoiceNumbers.size() == 1) {
188
                if (invoiceNumbers.size() == 1) {
193
                    Order order = invoiceOrdersMap.get(invoiceNumbers.get(0)).get(0);
-
 
194
                    File invoiceFile = this.getInvoiceFile(invoiceOrdersMap.get(invoiceNumbers.get(0)).get(0));
189
                    File invoiceFile = invoiceService.getInvoiceFile(order);
195
 
190
 
196
                    headers.setContentType(MediaType.APPLICATION_PDF);
191
                    headers.setContentType(MediaType.APPLICATION_PDF);
197
                    headers.set("Content-Type", "application/pdf");
192
                    headers.set("Content-Type", "application/pdf");
198
                    headers.set("Content-disposition", "inline; filename=" + invoiceNumber + ".pdf");
193
                    headers.set("Content-disposition", "inline; filename=" + invoiceNumber + ".pdf");
199
                    InputStreamResource is = new InputStreamResource(new FileInputStream(invoiceFile));
194
                    InputStreamResource is = new InputStreamResource(new FileInputStream(invoiceFile));
Line 202... Line 197...
202
                } else {
197
                } else {
203
 
198
 
204
                    ByteArrayOutputStream fos = new ByteArrayOutputStream();
199
                    ByteArrayOutputStream fos = new ByteArrayOutputStream();
205
                    ZipOutputStream zipOut = new ZipOutputStream(fos);
200
                    ZipOutputStream zipOut = new ZipOutputStream(fos);
206
 
201
 
207
                    for (String invoice : invoiceNumbers) {
202
                    for (String singleInvoiceNumber : invoiceNumbers) {
208
                        Order order = invoiceOrdersMap.get(invoiceNumbers.get(0)).get(0);
-
 
209
                        File invoiceFile = this.getInvoiceFile(order);
203
                        File invoiceFile = invoiceService.getInvoiceFile(order);
210
                        this.addFileToZip(zipOut, new FileInputStream(invoiceFile), invoice + ".pdf");
204
                        this.addFileToZip(zipOut, new FileInputStream(invoiceFile), singleInvoiceNumber + ".pdf");
211
                    }
205
                    }
212
                    zipOut.close();
206
                    zipOut.close();
213
                    byte[] byteArray = fos.toByteArray();
207
                    byte[] byteArray = fos.toByteArray();
214
                    headers.set("Content-Type", ContentType.APPLICATION_OCTET_STREAM.getMimeType());
208
                    headers.set("Content-Type", ContentType.APPLICATION_OCTET_STREAM.getMimeType());
215
                    headers.set("Content-disposition", "attachment; filename=invoices.zip");
209
                    headers.set("Content-disposition", "attachment; filename=invoices.zip");
Line 230... Line 224...
230
        outZip.putNextEntry(entry);
224
        outZip.putNextEntry(entry);
231
        IOUtils.copy(inputStream, outZip);
225
        IOUtils.copy(inputStream, outZip);
232
        outZip.closeEntry();
226
        outZip.closeEntry();
233
    }
227
    }
234
 
228
 
235
    private File getInvoiceFile(Order order) {
-
 
236
        LocalDateTime billTime = order.getBillingTimestamp();
-
 
237
        String dirPath = billTime.getYear() + "-" + (billTime.getMonthValue() - 1) + File.separator + order.getRetailerId();
-
 
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);
-
 
246
        return f;
-
 
247
    }
-
 
248
 
-
 
249
    private String getInvoicePathLastMonth(Order order) {
-
 
250
        LocalDateTime billTime = order.getBillingTimestamp();
-
 
251
        billTime = billTime.plusMonths(1);
-
 
252
        String dirPath = billTime.getYear() + "-" + (billTime.getMonthValue() - 1) + File.separator + order.getRetailerId();
-
 
253
        String filename = dirPath + File.separator + order.getInvoiceNumber() + ".pdf";
-
 
254
        return filename;
-
 
255
    }
-
 
256
}
229
}
257
230