Untitled

mail@pastecode.io avatar
unknown
java
2 months ago
2.0 kB
3
Indexable
Never
public ResponseEntity<byte[]> generatePaymentReport(
            @RequestParam("startDate") String startDate,
            @RequestParam("endDate") String endDate,
            @RequestParam("brandName") String brandName
    ) throws IOException {
        Account account = orderService.getAccountFromToken();
        Long shopId;
        if (account.getRole().contains(Role.SELLER.name())) {
            shopId = account.getShopId();
        } else if (account.getRole().contains(Role.ADMIN.name())) {
            shopId = null;
        } else {
            ErrorResponse errorResponse = new ErrorResponse();
            errorResponse.setErrorType(String.valueOf(HttpStatus.NOT_FOUND));
            errorResponse.setErrorDescription("ShopId not found for given seller");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        PaymentReportResponse paymentReportResponse = orderService.getPaymentReport(startDate, endDate, brandName);
        List<PaymentReportDto> paymentReportList = paymentReportResponse.getPaymentReportDto();

        byte[] excelData;
        if (!paymentReportList.isEmpty()) {
            excelData = ExcelUtils.generatePaymentReportDownloadExcel(paymentReportList);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentDispositionFormData("attachment", "payments_report.xlsx");
            headers.setContentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
            headers.setContentLength(excelData.length);
            return new ResponseEntity<>(excelData, headers, HttpStatus.OK);
        } else {
            ErrorResponse errorResponse = new ErrorResponse();
            errorResponse.setErrorType(String.valueOf(HttpStatus.BAD_REQUEST));
            errorResponse.setErrorDescription("Data not found for the seller - " + shopId);
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
    }
Leave a Comment