Subversion Repositories SmartDukaan

Rev

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

Rev 35076 Rev 35081
Line 5... Line 5...
5
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
5
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
6
import com.spice.profitmandi.common.model.*;
6
import com.spice.profitmandi.common.model.*;
7
import com.spice.profitmandi.common.solr.SolrService;
7
import com.spice.profitmandi.common.solr.SolrService;
8
import com.spice.profitmandi.common.util.FileUtil;
8
import com.spice.profitmandi.common.util.FileUtil;
9
import com.spice.profitmandi.common.util.PdfUtils;
9
import com.spice.profitmandi.common.util.PdfUtils;
10
import com.spice.profitmandi.common.util.PrinterType;
-
 
11
import com.spice.profitmandi.common.util.StringUtils;
10
import com.spice.profitmandi.common.util.StringUtils;
12
import com.spice.profitmandi.common.web.util.ResponseSender;
11
import com.spice.profitmandi.common.web.util.ResponseSender;
13
import com.spice.profitmandi.dao.entity.auth.AuthUser;
12
import com.spice.profitmandi.dao.entity.auth.AuthUser;
14
import com.spice.profitmandi.dao.entity.catalog.CustomerOffer;
13
import com.spice.profitmandi.dao.entity.catalog.CustomerOffer;
15
import com.spice.profitmandi.dao.entity.catalog.CustomerOfferItem;
14
import com.spice.profitmandi.dao.entity.catalog.CustomerOfferItem;
Line 735... Line 734...
735
 
734
 
736
    }
735
    }
737
 
736
 
738
    @RequestMapping(value = "/generateInvoice")
737
    @RequestMapping(value = "/generateInvoice")
739
    public ResponseEntity<?> generateInvoice(HttpServletRequest request, HttpServletResponse response,
738
    public ResponseEntity<?> generateInvoice(HttpServletRequest request, HttpServletResponse response,
-
 
739
                                             @RequestParam(name = ProfitMandiConstants.ORDER_ID) int orderId) throws ProfitMandiBusinessException {
-
 
740
        LOGGER.info("Request received at url {} with params [{}={}] ", request.getRequestURI(),
-
 
741
                ProfitMandiConstants.ORDER_ID, orderId);
-
 
742
        LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
-
 
743
        InvoicePdfModel pdfModel = null;
-
 
744
        if (roleManager.isAdmin(fofoDetails.getRoleIds())) {
-
 
745
            pdfModel = orderService.getInvoicePdfModel(orderId);
-
 
746
        } else {
-
 
747
            pdfModel = orderService.getInvoicePdfModel(fofoDetails.getFofoId(), orderId);
-
 
748
        }
-
 
749
        FofoOrder fofoOrder = fofoOrderRepository.selectByOrderId(orderId);
-
 
750
        List<InsurancePolicy> insurancePolicies = insurancePolicyRepository.selectByRetailerIdInvoiceNumber(fofoOrder.getInvoiceNumber());
-
 
751
 
-
 
752
        // Step 1: Generate invoice PDF
-
 
753
        ByteArrayOutputStream invoiceOutput = new ByteArrayOutputStream();
-
 
754
        PdfUtils.generateAndWrite(Arrays.asList(pdfModel), invoiceOutput);
-
 
755
        byte[] invoicePdf = invoiceOutput.toByteArray();
-
 
756
 
-
 
757
        // Step 2: Load all policy certificate PDFs
-
 
758
        List<byte[]> pdfFiles = new ArrayList<>();
-
 
759
        pdfFiles.add(invoicePdf); // first add invoice
-
 
760
 
-
 
761
        for (InsurancePolicy insurancePolicy : insurancePolicies) {
-
 
762
            if (insurancePolicy.getProviderId() == 6) {
-
 
763
                String policyNumber = insurancePolicy.getPolicyNumber();
-
 
764
                String safePolicyNo = policyNumber.replace("/", "-");
-
 
765
                String filePath = "/uploads/policy-certificate-" + safePolicyNo + ".pdf";
-
 
766
                File file = new File(filePath);
-
 
767
 
-
 
768
                if (file.exists()) {
-
 
769
                    try {
-
 
770
                        byte[] policyPdf = Files.readAllBytes(file.toPath());
-
 
771
                        pdfFiles.add(policyPdf);
-
 
772
                    } catch (IOException e) {
-
 
773
                        LOGGER.error("Failed to read policy PDF: {}", filePath, e);
-
 
774
                    }
-
 
775
                } else {
-
 
776
                    LOGGER.warn("Policy PDF not found: {}", filePath);
-
 
777
                }
-
 
778
            }
-
 
779
        }
-
 
780
 
-
 
781
        // Step 3: Merge all PDFs
-
 
782
        byte[] mergedPdf;
-
 
783
        try {
-
 
784
            mergedPdf = PdfUtils.mergePdfFiles(pdfFiles);
-
 
785
        } catch (Exception e) {
-
 
786
            LOGGER.error("Error merging PDFs", e);
-
 
787
            throw new ProfitMandiBusinessException("Failed to generate merged PDF", "", "");
-
 
788
        }
-
 
789
 
-
 
790
        // Step 4: Return merged PDF as response
-
 
791
        HttpHeaders headers = new HttpHeaders();
-
 
792
        headers.setContentType(MediaType.APPLICATION_PDF);
-
 
793
        headers.setContentDispositionFormData("inline", "invoice-with-policies-" + pdfModel.getInvoiceNumber() + ".pdf");
-
 
794
        headers.setContentLength(mergedPdf.length);
-
 
795
 
-
 
796
        InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(mergedPdf));
-
 
797
        return new ResponseEntity<>(resource, headers, HttpStatus.OK);
-
 
798
    }
-
 
799
 
-
 
800
   /* @RequestMapping(value = "/generateInvoice")
-
 
801
    public ResponseEntity<?> generateInvoice(HttpServletRequest request, HttpServletResponse response,
740
                                             @RequestParam(name = ProfitMandiConstants.ORDER_ID) int orderId, @RequestParam(required = false) PrinterType printerType) throws ProfitMandiBusinessException {
802
                                             @RequestParam(name = ProfitMandiConstants.ORDER_ID) int orderId, @RequestParam(required = false) PrinterType printerType) throws ProfitMandiBusinessException {
741
        LOGGER.info("Request received at url {} with params [{}={}] ", request.getRequestURI(),
803
        LOGGER.info("Request received at url {} with params [{}={}] ", request.getRequestURI(),
742
                ProfitMandiConstants.ORDER_ID, orderId);
804
                ProfitMandiConstants.ORDER_ID, orderId);
743
        LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
805
        LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
744
        InvoicePdfModel pdfModel = null;
806
        InvoicePdfModel pdfModel = null;
Line 806... Line 868...
806
        headers.setContentDispositionFormData("inline", "invoice-with-policies-" + pdfModel.getInvoiceNumber() + ".pdf");
868
        headers.setContentDispositionFormData("inline", "invoice-with-policies-" + pdfModel.getInvoiceNumber() + ".pdf");
807
        headers.setContentLength(mergedPdf.length);
869
        headers.setContentLength(mergedPdf.length);
808
 
870
 
809
        InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(mergedPdf));
871
        InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(mergedPdf));
810
        return new ResponseEntity<>(resource, headers, HttpStatus.OK);
872
        return new ResponseEntity<>(resource, headers, HttpStatus.OK);
811
    }
873
    }*/
812
 
874
 
813
    @RequestMapping(value = "/generateInvoices")
875
    @RequestMapping(value = "/generateInvoices")
814
    public ResponseEntity<?> generateInvoice(HttpServletRequest request, HttpServletResponse response,
876
    public ResponseEntity<?> generateInvoice(HttpServletRequest request, HttpServletResponse response,
815
                                             @RequestParam LocalDateTime startDate, @RequestParam LocalDateTime endDate, @RequestParam int partnerId)
877
                                             @RequestParam LocalDateTime startDate, @RequestParam LocalDateTime endDate, @RequestParam int partnerId)
816
            throws ProfitMandiBusinessException {
878
            throws ProfitMandiBusinessException {