@RequestMapping(value = "/invoice")
  public void invoice(
      @RequestParam(required = true) int invoiceId,
      @RequestParam(required = false) String token,
      HttpSession session,
      HttpServletResponse response)
      throws ParseException, IllegalAccessException, InstantiationException,
          InvocationTargetException, NoSuchMethodException {

    Invoice invoice = invoiceService.findById(invoiceId);

    HashMap<String, Object> param = new HashMap<String, Object>();
    ThaiBaht thaiBaht = new ThaiBaht();

    float surTotal = 0;
    float surTax = 0;

    String branchDhipName = "";

    List<InvoiceReport> invoiceReports = new ArrayList<InvoiceReport>();
    for (Claim claim : invoice.getClaims()) {
      if (claim.getBranchDhip() != null) {
        branchDhipName = claim.getBranchDhip().getName();
      }
      float surTotalTemp = ClaimServiceImpl.calcTotalSur(claim);
      surTotal += surTotalTemp;
      float surTaxTemp = ClaimServiceImpl.calcVat(surTotalTemp);
      surTax += surTaxTemp;

      InvoiceReport invoiceReport = new InvoiceReport();
      invoiceReport.setClaimNo(claim.getClaimNo());
      invoiceReport.setDispatchDate(claim.getDispatchDate());
      invoiceReport.setClaimType(
          claim.getClaimType() == null ? "" : claim.getClaimType().getName());
      invoiceReport.setSurInvest(claim.getSurInvest());
      invoiceReport.setSurTrans(claim.getSurTrans());
      invoiceReport.setSurDaily(claim.getSurDaily());
      invoiceReport.setSurPhoto(claim.getSurPhoto());
      invoiceReport.setSurClaim(claim.getSurClaim());
      invoiceReport.setSurTel(claim.getSurTel());
      invoiceReport.setSurInsure(claim.getSurInsure());
      invoiceReport.setSurTowcar(claim.getSurTowcar());
      invoiceReport.setSurOther(claim.getSurOther());
      invoiceReports.add(invoiceReport);
    }

    param.put("branchDhipName", branchDhipName);
    param.put("invoiceNo", invoice.getCode());
    param.put("total", surTotal);
    param.put("tax", surTax);
    param.put("totalAll", surTotal + surTax);
    param.put("totalThai", "(" + thaiBaht.getText(surTotal + surTax) + ")");

    downloadService.download(
        ExporterService.EXTENSION_TYPE_EXCEL,
        invoice.getCode(),
        session.getServletContext().getRealPath("/jasperreport/invoice"),
        param,
        invoiceReports,
        token,
        response);
  }
  @RequestMapping(value = "/paySurvey")
  public void paySurvey(
      @RequestParam(required = true) String paySurveyIds,
      @RequestParam(required = false) String token,
      HttpSession session,
      HttpServletResponse response)
      throws ServletException, IOException, JRException, Exception {

    List<String> fileList = new ArrayList<String>();
    List<InputStream> inputStreams = new ArrayList<InputStream>();
    HashMap<String, Object> param;
    ThaiBaht thaiBaht = new ThaiBaht();

    String[] arrPaySurveyId = paySurveyIds.split(",");
    for (String paySurveyId : arrPaySurveyId) {
      param = new HashMap<String, Object>();

      PaySurvey paySurvey = paySurveyService.findById(Integer.parseInt(paySurveyId));

      float sumSurveyTrans = 0;
      float sumSurveyInvest = 0;
      float sumSurveyDaily = 0;
      float sumSurveyPhoto = 0;
      float sumSurveyTel = 0;
      float sumSurveyClaim = 0;
      float sumSurveyConditionRight = 0;
      float sumSurveyOther = 0;
      float sumSurveyFine = 0;
      float sumSurveyTotal = 0;
      for (Claim claim : paySurvey.getClaims()) {
        sumSurveyTrans += NumberToolsUtil.nullToFloat(claim.getSurveyTrans());
        sumSurveyInvest += NumberToolsUtil.nullToFloat(claim.getSurveyInvest());
        sumSurveyDaily += NumberToolsUtil.nullToFloat(claim.getSurveyDaily());
        sumSurveyPhoto += NumberToolsUtil.nullToFloat(claim.getSurveyPhoto());
        sumSurveyTel += NumberToolsUtil.nullToFloat(claim.getSurveyTel());
        sumSurveyClaim += NumberToolsUtil.nullToFloat(claim.getSurveyClaim());
        sumSurveyConditionRight += NumberToolsUtil.nullToFloat(claim.getSurveyConditionRight());
        sumSurveyOther += NumberToolsUtil.nullToFloat(claim.getSurveyOther());
        sumSurveyFine += NumberToolsUtil.nullToFloat(claim.getSurveyFine());
        sumSurveyTotal += ClaimServiceImpl.calcTotalSurvey(claim);
      }

      param.put("sumSurveyTrans", sumSurveyTrans);
      param.put("sumSurveyInvest", sumSurveyInvest);
      param.put("sumSurveyDaily", sumSurveyDaily);
      param.put("sumSurveyPhoto", sumSurveyPhoto);
      param.put("sumSurveyTel", sumSurveyTel);
      param.put("sumSurveyClaim", sumSurveyClaim);
      param.put("sumSurveyConditionRight", sumSurveyConditionRight);
      param.put("sumSurveyOther", sumSurveyOther);
      param.put("sumSurveyFine", sumSurveyFine);
      param.put("totalThai", "=" + thaiBaht.getText(sumSurveyTotal) + "=");
      param.put("total", sumSurveyTotal);
      param.put(
          "sumSurveyPart1",
          sumSurveyTrans + sumSurveyInvest + sumSurveyDaily + sumSurveyConditionRight);

      SurveyEmployee surveyEmployee = paySurvey.getClaims().get(0).getSurveyEmployee();
      if (surveyEmployee != null) {
        param.put("surveyEmployeeCode", surveyEmployee.getCode());
        param.put("surveyEmployeeName", surveyEmployee.getFullname());
      }

      param.put("paySurveyNo", paySurvey.getCode());

      ByteArrayOutputStream reportOut =
          downloadService.generateReportXLS(
              null,
              session.getServletContext().getRealPath("/jasperreport/paySurvey"),
              ExporterService.EXTENSION_TYPE_EXCEL,
              param,
              "paySurvey",
              paySurvey.getClaims());

      if (reportOut != null) {
        InputStream in = new ByteArrayInputStream(reportOut.toByteArray());
        inputStreams.add(in);
        fileList.add("payment_" + paySurvey.getCode() + ".xls");
      }

      // -----------------------------------------------------

      param = new HashMap<String, Object>();

      param.put("paySurveyNo", paySurvey.getCode());
      if (surveyEmployee != null) {
        param.put("surveyEmployeeCode", surveyEmployee.getCode());
        param.put("surveyEmployeeName", surveyEmployee.getFullname());
      }
      param.put("total", sumSurveyTotal);
      param.put("totalThai", "(" + thaiBaht.getText(sumSurveyTotal) + ")");

      reportOut =
          downloadService.generateReportXLS(
              null,
              session.getServletContext().getRealPath("/jasperreport/paySurveyApprove"),
              ExporterService.EXTENSION_TYPE_EXCEL,
              param,
              "paySurveyApprove",
              paySurvey.getClaims());

      if (reportOut != null) {
        InputStream in = new ByteArrayInputStream(reportOut.toByteArray());
        inputStreams.add(in);
        fileList.add("approveForm_" + paySurvey.getCode() + ".xls");
      }
    }

    OutputStream outs = null;
    outs = response.getOutputStream();

    String header = "attachment; filename=billing.zip";

    header = new String(header.getBytes("UTF-8"), "ISO8859_1");
    response.setHeader("Content-Disposition", header);
    response.setContentType("application/ms-excel");

    downloadService.writeZipFile(fileList, inputStreams, outs, token);
  }