Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
19 kB
2
Indexable
Never
import { jsPDF } from 'jspdf';
import {
  getPDFPath,
  savePDF,
  getPDFPathForMail,
  savePDFForMail,
} from '../repository/firebaseStorage/pdfRepository';
import html2canvas from 'html2canvas';
import autoTable from 'jspdf-autotable';
import { saveAs } from 'file-saver';

export async function createPDFReport(
  saveOnDevice = true,
  input,
  width,
  openCashPart,
  startingCapital,
  time,
  pdfContents,
  idFromTable,
  idFromTable2,
  idFromTable3,
  siteElements,
  investmentProductIdArray,
  locale,
  refInfoData,
  email
) {
  return new Promise((resolve, reject) => {
    html2canvas(input, {
      useCORS: true,
      allowTaint: true,
      useCss: true,
      scale:
        width >= 1440
          ? 2.0
          : startingCapital > 2000000 && time >= 10
          ? 1.0
          : openCashPart && (startingCapital < 2000000 || time < 10)
          ? 1.0
          : 1.1,
    }).then((canvas) => {
      const imgWidth = 297;
      const pageHeight = 210;
      const imgHeight = (canvas.height * imgWidth) / canvas.width;
      let heightLeft = imgHeight;
      let position = 0;
      const doc = new jsPDF('l', 'mm', [297, 210]);
      let imgData = canvas.toDataURL('image/jpeg');
      //1
      doc.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
      doc.link(
        refInfoData?.emailLinkPage1Left,
        refInfoData?.emailLinkPage1Top,
        refInfoData?.emailLinkPage1Width,
        4,
        {
          url: `mailto:${pdfContents?.emailLink}`,
        }
      );
      doc.link(
        refInfoData?.linkPage1Left,
        refInfoData?.linkPage1Top,
        refInfoData?.linkPage1Width,
        4,
        {
          url: `https://${pdfContents?.link}`,
        }
      );
      doc.link(
        refInfoData?.mainLinkPage1Left,
        refInfoData?.mainLinkPage1Top,
        refInfoData?.mainLinkPage1Width,
        4,
        {
          url: `https://${pdfContents?.mainLink}`,
        }
      );
      //2
      heightLeft -= pageHeight;
      position = heightLeft - imgHeight;
      doc.addPage();
      doc.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
      doc.link(
        refInfoData?.feedbackLinkPage2Left,
        refInfoData?.feedbackLinkPage2Top,
        refInfoData?.feedbackLinkPage2Width,
        4,
        {
          url: `${pdfContents?.feedbackLink}`,
        }
      );
      doc.link(
        refInfoData?.feedbackEmailPage2Left,
        refInfoData?.feedbackEmailPage2Top,
        refInfoData?.feedbackEmailPage2Width,
        4,
        {
          url: `mailto:${pdfContents?.emailFeedbackLink}`,
        }
      );
      //3
      heightLeft -= pageHeight;
      position = heightLeft - imgHeight;
      doc.addPage();
      doc.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
      //4
      heightLeft -= pageHeight;
      position = heightLeft - imgHeight;
      doc.addPage();
      doc.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
      //5
      heightLeft -= pageHeight;
      position = heightLeft - imgHeight;
      doc.addPage();
      doc.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
      autoTable(doc, {
        html: `#${idFromTable}`,
        didDrawCell: (data) => {
          for (let i = 0; i < siteElements?.investableProducts?.length; i++) {
            if (
              data.section === 'body' &&
              data.row.cells[5].text[0] === investmentProductIdArray[i] &&
              data.row.cells[4].x !== 0 &&
              data.row.cells[4].y !== 0 &&
              data.row.cells[4].width !== 0 &&
              data.row.cells[4].height !== 0
            ) {
              doc.link(
                data.row.cells[4].x,
                data.row.cells[4].y,
                data.row.cells[4].width,
                data.row.cells[4].height,
                {
                  url:
                    siteElements?.investableProducts?.[i]?.kidCH?.filter(
                      (item) => item == locale
                    ).length !== 0
                      ? `${process.env.BACKEND_ETF_DOCUMENTS_DOMAIN}/static/etf-documents/KID_CH_${locale}_${investmentProductIdArray[i]}.pdf`
                      : `${process.env.BACKEND_ETF_DOCUMENTS_DOMAIN}/static/etf-documents/KID_CH_en_${investmentProductIdArray[i]}.pdf`,
                }
              );
            }
          }
        },
        useCss: true,
        startY:
          locale === 'en'
            ? 91
            : locale === 'fr'
            ? 105
            : locale === 'it'
            ? 105
            : locale === 'de'
            ? 105
            : 91,
        theme: 'grid',
        tableLineWidth: 0,
        bodyStyles: { lineWidth: 0 },
        margin: { right: 11, left: 10.5 },
        tableWidth: 'auto',
        styles: {
          minCellHeight: 10.5,
          fontSize: 4,
          lineWidth: 0,
        },
        columnStyles: {
          0: {
            cellWidth: 8,
          },
          1: {
            cellWidth: 20,
          },
          2: {
            cellWidth: 17,
          },
          3: {
            cellWidth: 15,
          },
          4: {
            cellWidth: 85,
          },
          5: {
            cellWidth: 25,
          },
          6: {
            cellWidth: 17,
          },
          7: {
            cellWidth: 13,
          },
          8: {
            cellWidth: 15,
          },
          9: {
            cellWidth: 15,
          },
          10: {
            cellWidth: 13,
          },
          11: {
            cellWidth: 17,
          },
          11: {
            cellWidth: 15,
          },
        },
      });

      //6
      heightLeft -= pageHeight;
      position = heightLeft - imgHeight;
      doc.addPage();
      doc.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
      if (
        (startingCapital > 2000000 && time >= 10) ||
        (openCashPart && (startingCapital < 2000000 || time < 10))
      ) {
        autoTable(doc, {
          html: `#${idFromTable2}`,
          didDrawCell: (data) => {
            for (let i = 0; i < siteElements?.investableProducts?.length; i++) {
              if (
                data.section === 'body' &&
                data.row.cells[5].text[0] === investmentProductIdArray[i] &&
                data.row.cells[4].x !== 0 &&
                data.row.cells[4].y !== 0 &&
                data.row.cells[4].width !== 0 &&
                data.row.cells[4].height !== 0
              ) {
                doc.link(
                  data.row.cells[4].x,
                  data.row.cells[4].y,
                  data.row.cells[4].width,
                  data.row.cells[4].height,
                  {
                    url:
                      siteElements?.investableProducts?.[i]?.kidCH?.filter(
                        (item) => item == locale
                      ).length !== 0
                        ? `${process.env.BACKEND_ETF_DOCUMENTS_DOMAIN}/static/etf-documents/KID_CH_${locale}_${investmentProductIdArray[i]}.pdf`
                        : `${process.env.BACKEND_ETF_DOCUMENTS_DOMAIN}/static/etf-documents/KID_CH_en_${investmentProductIdArray[i]}.pdf`,
                  }
                );
              }
            }
          },
          useCss: true,
          startY: 58,
          theme: 'grid',
          margin: { right: 11, left: 10.5 },
          tableWidth: 'auto',
          tableLineWidth: 0,
          bodyStyles: { lineWidth: 0 },

          styles: {
            minCellHeight: 10.5,
            fontSize: 4,
            lineWidth: 0,
          },
          columnStyles: {
            0: {
              cellWidth: 8,
            },
            1: {
              cellWidth: 20,
            },
            2: {
              cellWidth: 17,
            },
            3: {
              cellWidth: 15,
            },
            4: {
              cellWidth: 85,
            },
            5: {
              cellWidth: 25,
            },
            6: {
              cellWidth: 17,
            },
            7: {
              cellWidth: 13,
            },
            8: {
              cellWidth: 15,
            },
            9: {
              cellWidth: 15,
            },
            10: {
              cellWidth: 13,
            },
            11: {
              cellWidth: 17,
            },
            11: {
              cellWidth: 15,
            },
          },
        });
      }
      //7
      heightLeft -= pageHeight;
      position = heightLeft - imgHeight;
      doc.addPage();
      doc.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
      if (startingCapital > 2000000 && time >= 10) {
        autoTable(doc, {
          html: `#${idFromTable3}`,
          didDrawCell: (data) => {
            for (let i = 0; i < siteElements?.investableProducts?.length; i++) {
              if (
                data.section === 'body' &&
                data.row.cells[5].text[0] === investmentProductIdArray[i] &&
                data.row.cells[4].x !== 0 &&
                data.row.cells[4].y !== 0 &&
                data.row.cells[4].width !== 0 &&
                data.row.cells[4].height !== 0
              ) {
                doc.link(
                  data.row.cells[4].x,
                  data.row.cells[4].y,
                  data.row.cells[4].width,
                  data.row.cells[4].height,
                  {
                    url:
                      siteElements?.investableProducts?.[i]?.kidCH?.filter(
                        (item) => item == locale
                      ).length !== 0
                        ? `${process.env.BACKEND_ETF_DOCUMENTS_DOMAIN}/static/etf-documents/KID_CH_${locale}_${investmentProductIdArray[i]}.pdf`
                        : `${process.env.BACKEND_ETF_DOCUMENTS_DOMAIN}/static/etf-documents/KID_CH_en_${investmentProductIdArray[i]}.pdf`,
                  }
                );
              }
            }
          },
          useCss: true,
          startY: 58,
          theme: 'grid',
          margin: { right: 11, left: 10.5 },
          tableWidth: 'auto',
          tableLineWidth: 0,
          bodyStyles: { lineWidth: 0 },

          styles: {
            minCellHeight: 10.5,
            fontSize: 4,
            lineWidth: 0,
          },
          columnStyles: {
            0: {
              cellWidth: 8,
            },
            1: {
              cellWidth: 20,
            },
            2: {
              cellWidth: 17,
            },
            3: {
              cellWidth: 15,
            },
            4: {
              cellWidth: 85,
            },
            5: {
              cellWidth: 25,
            },
            6: {
              cellWidth: 17,
            },
            7: {
              cellWidth: 13,
            },
            8: {
              cellWidth: 15,
            },
            9: {
              cellWidth: 15,
            },
            10: {
              cellWidth: 13,
            },
            11: {
              cellWidth: 17,
            },
            11: {
              cellWidth: 15,
            },
          },
        });
      }
      //8
      heightLeft -= pageHeight;
      position = heightLeft - imgHeight;
      doc.addPage();
      doc.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
      //9
      heightLeft -= pageHeight;
      position = heightLeft - imgHeight;
      doc.addPage();
      doc.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
      //10
      heightLeft -= pageHeight;
      position = heightLeft - imgHeight;
      doc.addPage();
      doc.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
      //11
      heightLeft -= pageHeight;
      position = heightLeft - imgHeight;
      doc.addPage();
      doc.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
      //12
      heightLeft -= pageHeight;
      position = heightLeft - imgHeight;
      doc.addPage();
      doc.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
      //13
      heightLeft -= pageHeight;
      position = heightLeft - imgHeight;
      doc.addPage();
      doc.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
      if (!openCashPart && (startingCapital < 2000000 || time < 10)) {
        doc.link(83, refInfoData?.userEmailTop, 40, 5, {
          url: `mailto:${email}`,
        });
      }

      //14
      heightLeft -= pageHeight;
      position = heightLeft - imgHeight;
      doc.addPage();
      doc.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
      if (openCashPart && (startingCapital < 2000000 || time < 10)) {
        doc.link(83, refInfoData?.userEmailTop, 40, 5, {
          url: `mailto:${email}`,
        });
      }

      //15
      heightLeft -= pageHeight;
      position = heightLeft - imgHeight;
      doc.addPage();
      doc.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
      heightLeft -= pageHeight;
      if (startingCapital > 2000000 && time >= 10) {
        doc.link(83, refInfoData?.userEmailTop, 40, 5, {
          url: `mailto:${email}`,
        });
      }
      if (!openCashPart && (startingCapital < 2000000 || time < 10)) {
        locale === 'en'
          ? doc.link(165, 45, 60, 5, {
              url: `${pdfContents?.legalNote?.paragraph1Link}`,
            })
          : locale === 'de'
          ? doc.link(210, 45, 45, 5, {
              url: `${pdfContents?.legalNote?.paragraph1Link}`,
            })
          : locale === 'fr'
          ? doc.link(10, 50, 50, 5, {
              url: `${pdfContents?.legalNote?.paragraph1Link}`,
            })
          : locale === 'it'
          ? doc.link(105, 50, 60, 5, {
              url: `${pdfContents?.legalNote?.paragraph1Link}`,
            })
          : '';
        locale === 'en'
          ? doc.link(175, 155, 60, 5, {
              url: `${pdfContents?.legalNote?.paragraph7Link}`,
            })
          : locale === 'de'
          ? doc.link(20, 175, 45, 5, {
              url: `${pdfContents?.legalNote?.paragraph7Link}`,
            })
          : locale === 'fr'
          ? doc.link(35, 177, 60, 5, {
              url: `${pdfContents?.legalNote?.paragraph1Link}`,
            })
          : locale === 'it'
          ? doc.link(175, 170, 60, 5, {
              url: `${pdfContents?.legalNote?.paragraph7Link}`,
            })
          : '';
      }

      //5a
      if (startingCapital > 2000000 && time >= 10) {
        position = heightLeft - imgHeight;
        doc.addPage();
        doc.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
        heightLeft -= pageHeight;
        position = heightLeft - imgHeight;
        doc.addPage();
        doc.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
        locale === 'en'
          ? doc.link(165, 45, 60, 5, {
              url: `${pdfContents?.legalNote?.paragraph1Link}`,
            })
          : locale === 'de'
          ? doc.link(210, 45, 45, 5, {
              url: `${pdfContents?.legalNote?.paragraph1Link}`,
            })
          : locale === 'fr'
          ? doc.link(10, 50, 50, 5, {
              url: `${pdfContents?.legalNote?.paragraph1Link}`,
            })
          : locale === 'it'
          ? doc.link(105, 50, 60, 5, {
              url: `${pdfContents?.legalNote?.paragraph1Link}`,
            })
          : '';
        locale === 'en'
          ? doc.link(175, 155, 60, 5, {
              url: `${pdfContents?.legalNote?.paragraph7Link}`,
            })
          : locale === 'de'
          ? doc.link(20, 175, 45, 5, {
              url: `${pdfContents?.legalNote?.paragraph7Link}`,
            })
          : locale === 'fr'
          ? doc.link(35, 177, 60, 5, {
              url: `${pdfContents?.legalNote?.paragraph1Link}`,
            })
          : locale === 'it'
          ? doc.link(175, 170, 60, 5, {
              url: `${pdfContents?.legalNote?.paragraph7Link}`,
            })
          : '';
        position = heightLeft - imgHeight;
        doc.addPage();
        doc.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
        locale === 'en'
          ? doc.link(165, 45, 60, 5, {
              url: `${pdfContents?.legalNote?.paragraph1Link}`,
            })
          : locale === 'de'
          ? doc.link(210, 45, 45, 5, {
              url: `${pdfContents?.legalNote?.paragraph1Link}`,
            })
          : locale === 'fr'
          ? doc.link(10, 50, 50, 5, {
              url: `${pdfContents?.legalNote?.paragraph1Link}`,
            })
          : locale === 'it'
          ? doc.link(105, 50, 60, 5, {
              url: `${pdfContents?.legalNote?.paragraph1Link}`,
            })
          : '';
        locale === 'en'
          ? doc.link(175, 155, 60, 5, {
              url: `${pdfContents?.legalNote?.paragraph7Link}`,
            })
          : locale === 'de'
          ? doc.link(20, 175, 45, 5, {
              url: `${pdfContents?.legalNote?.paragraph7Link}`,
            })
          : locale === 'fr'
          ? doc.link(35, 177, 60, 5, {
              url: `${pdfContents?.legalNote?.paragraph1Link}`,
            })
          : locale === 'it'
          ? doc.link(175, 170, 60, 5, {
              url: `${pdfContents?.legalNote?.paragraph7Link}`,
            })
          : '';
        heightLeft -= pageHeight;
      }

      // while (heightLeft >= 0) {
      //   position = heightLeft - imgHeight;
      //   doc.addPage();
      //   doc.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
      //   heightLeft -= pageHeight;
      // }
      // if (saveOnDevice) doc.save('QIO GBI Report');
      // resolve(doc.output('blob'));
      if (saveOnDevice) {
        let blob = new Blob([doc.output('blob')], {
          type: 'application/octet-stream',
        });
        resolve(saveAs(blob, 'QIO GBI Report.pdf'));
      }
    });
  });
}

export async function savePDFToFBStorage(
  saveOnDevice,
  investmentProcessID,
  guid,
  input,
  width,
  openCashPart,
  startingCapital,
  time,
  pdfContents,
  idFromTable,
  idFromTable2,
  idFromTable3,
  siteElements,
  investmentProductIdArray,
  locale,
  refInfoData,
  email
) {
  let result;
  await createPDFReport(
    saveOnDevice,
    input,
    width,
    openCashPart,
    startingCapital,
    time,
    pdfContents,
    idFromTable,
    idFromTable2,
    idFromTable3,
    siteElements,
    investmentProductIdArray,
    locale,
    refInfoData,
    email
  ).then((res) => (result = res));
  await savePDF(result, guid, investmentProcessID);
  return await getPDFPath(guid, investmentProcessID);
}

export async function savePDFToFBStorageForMail(
  saveOnDevice,
  investmentProcessID,
  guid,
  input,
  width,
  openCashPart,
  startingCapital,
  time,
  pdfContents,
  idFromTable,
  idFromTable2,
  idFromTable3,
  siteElements,
  investmentProductIdArray,
  locale,
  refInfoData,
  email
) {
  let result;
  await createPDFReport(
    saveOnDevice,
    input,
    width,
    openCashPart,
    startingCapital,
    time,
    pdfContents,
    idFromTable,
    idFromTable2,
    idFromTable3,
    siteElements,
    investmentProductIdArray,
    locale,
    refInfoData,
    email
  ).then((res) => (result = res));
  await savePDFForMail(result, guid, investmentProcessID);
  return await getPDFPathForMail(guid, investmentProcessID);
}