Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions packages/invoice-dashboard/src/lib/view-requests.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -521,10 +521,9 @@
try {
await exportToPDF(
request,
// FIXME: Use a non deprecated function
currencyManager.from(
request.currencyInfo.value,
request.currencyInfo.network
getCurrencyFromManager(
request.currencyInfo,
currencyManager
),
config.logo
);
Expand Down
148 changes: 79 additions & 69 deletions packages/invoice-dashboard/src/utils/generateInvoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,72 +23,65 @@ export const exportToPDF = async (
) => {
await ensureHtml2PdfLoaded();

const formatDate = (date: string | undefined) => {
return date ? new Date(date).toLocaleDateString() : "-";
};

const renderAddress = (info: any) => {
const parts = [
info?.address?.["street-address"],
info?.address?.locality,
info?.address?.["postal-code"],
info?.address?.["country-name"],
].filter(Boolean);
return parts.length > 0 ? parts.join(", ") : "-";
};

const content = `
<html>
<head>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link href="https://fonts.googleapis.com/css2?family=Urbanist:ital,wght@0,100..900;1,100..900&display=swap" rel="stylesheet" />
<style>
body { font-family: 'Urbanist', sans-serif; }
</style>
</head>
<body>
<div id="invoice" style="font-family: Urbanist, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px;">
<div id="invoice" style="max-width: 800px; margin: 0 auto; padding: 20px;">
<div style="display: flex; justify-content: space-between; align-items: start;">
<img src="${logo}" alt="Logo" style="width: 50px; height: 50px;">
${logo ? `<img src="${logo}" alt="Logo" style="width: 50px; height: 50px;">` : ""}
<div style="text-align: right;">
<p>Issued on ${new Date(
invoice.contentData.creationDate
).toLocaleDateString()}</p>
<p>Payment due by ${new Date(
invoice.contentData.paymentTerms.dueDate
).toLocaleDateString()}</p>
<p>Issued on ${formatDate(invoice.contentData?.creationDate)}</p>
<p>Payment due by ${formatDate(invoice.contentData?.paymentTerms?.dueDate)}</p>
</div>
</div>

<h1 style="text-align: center; color: #333; font-size: 28px; font-style: bold; margin-bottom: 14px;">INVOICE #${
invoice.contentData.invoiceNumber
<h1 style="text-align: center; color: #333; font-size: 28px; font-weight: bold; margin-bottom: 14px;">INVOICE #${
invoice.contentData?.invoiceNumber || "-"
}</h1>

<div style="display: flex; justify-content: space-between; margin-bottom: 20px; background-color: #FBFBFB; padding: 10px;">
<div>
<strong>From:</strong><br>
<p style="font-size: 14px">${invoice.payee.value}</p>
${invoice.contentData.sellerInfo.firstName ?? ""} ${
invoice.contentData.sellerInfo.lastName ?? ""
}<br>
${invoice.contentData.sellerInfo.address["street-address"] ?? ""}<br>
${invoice.contentData.sellerInfo.address.locality ?? ""}${
invoice.contentData.sellerInfo.address.locality ? "," : ""
} ${invoice.contentData.sellerInfo.address["postal-code"] ?? ""}<br>
${invoice.contentData.sellerInfo.address["country-name"] ?? ""}<br>
${
invoice.contentData.sellerInfo.taxRegistration
? `VAT: ${invoice.contentData.sellerInfo.taxRegistration}`
: ""
}<br>
<p style="font-size: 14px">${invoice.payee?.value || "-"}</p>
${invoice.contentData?.sellerInfo?.firstName || ""} ${invoice.contentData?.sellerInfo?.lastName || ""}<br>
${renderAddress(invoice.contentData?.sellerInfo)}<br>
${invoice.contentData?.sellerInfo?.taxRegistration ? `VAT: ${invoice.contentData.sellerInfo.taxRegistration}` : ""}
</div>

<div>
<strong>To:</strong><br>
<p style="font-size: 14px">${invoice.payer.value}</p>
${invoice.contentData.buyerInfo.firstName ?? ""} ${
invoice.contentData.buyerInfo.lastName ?? ""
}<br>
${invoice.contentData.buyerInfo.address["street-address"] ?? ""}<br>
${invoice.contentData.buyerInfo.address.locality ?? ""}${
invoice.contentData.sellerInfo.address.locality ? "," : ""
} ${invoice.contentData.buyerInfo.address["postal-code"] ?? ""}<br>
${invoice.contentData.buyerInfo.address["country-name"] ?? ""}<br>
${
invoice.contentData.buyerInfo.taxRegistration
? `VAT: ${invoice.contentData.buyerInfo.taxRegistration}`
: ""
}<br>
<p style="font-size: 14px">${invoice.payer?.value || "-"}</p>
${invoice.contentData?.buyerInfo?.firstName || ""} ${invoice.contentData?.buyerInfo?.lastName || ""}<br>
${renderAddress(invoice.contentData?.buyerInfo)}<br>
${invoice.contentData?.buyerInfo?.taxRegistration ? `VAT: ${invoice.contentData.buyerInfo.taxRegistration}` : ""}
</div>
</div>

<div style="margin-bottom: 20px;">
<strong>Payment Chain:</strong> ${invoice.currencyInfo.network}<br>
<strong>Invoice Currency:</strong> ${invoice.currency}<br>
<strong>Payment Chain:</strong> ${invoice.currencyInfo?.network || "-"}<br>
<strong>Invoice Currency:</strong> ${invoice.currency || "-"}<br>
<strong>Invoice Type:</strong> Regular Invoice
</div>

Expand All @@ -104,31 +97,33 @@ export const exportToPDF = async (
</tr>
</thead>
<tbody>
${invoice.contentData.invoiceItems
${(invoice.contentData?.invoiceItems || [])
.map(
(item: any) => `
<tr>
<td style="border: 1px solid #ddd; padding: 8px;">${
item.name
<td style="border: 1px solid #ddd; padding: 8px;">${item.name || "-"}</td>
<td style="border: 1px solid #ddd; padding: 8px; text-align: right;">${item.quantity || "-"}</td>
<td style="border: 1px solid #ddd; padding: 8px; text-align: right;">${
item.unitPrice
? formatUnits(BigInt(item.unitPrice), currency?.decimals || 0)
: "-"
}</td>
<td style="border: 1px solid #ddd; padding: 8px; text-align: right;">${
item.quantity
item.discount
? formatUnits(BigInt(item.discount), currency?.decimals || 0)
: "-"
}</td>
<td style="border: 1px solid #ddd; padding: 8px; text-align: right;">${formatUnits(
item.unitPrice,
currency.decimals
)}</td>
<td style="border: 1px solid #ddd; padding: 8px; text-align: right;">${formatUnits(
item.discount,
currency.decimals
)}</td>
<td style="border: 1px solid #ddd; padding: 8px; text-align: right;">${
item.tax.amount
}%</td>
<td style="border: 1px solid #ddd; padding: 8px; text-align: right;">${formatUnits(
calculateItemTotal(item),
currency?.decimals
)}</td>
item.tax?.amount ? `${item.tax.amount}%` : "-"
}</td>
<td style="border: 1px solid #ddd; padding: 8px; text-align: right;">${
item
? formatUnits(
BigInt(calculateItemTotal(item)),
currency?.decimals || 0
)
: "-"
}</td>
</tr>
`
)
Expand All @@ -137,18 +132,19 @@ export const exportToPDF = async (
<tfoot>
<tr>
<td colspan="5" style="border: 1px solid #ddd; padding: 8px; text-align: right;"><strong>Due:</strong></td>
<td style="border: 1px solid #ddd; padding: 8px; text-align: right;"><strong>${formatUnits(
invoice.expectedAmount,
currency.decimals
)} ${invoice.currency}</strong></td>
<td style="border: 1px solid #ddd; padding: 8px; text-align: right;"><strong>${
invoice.expectedAmount
? `${formatUnits(BigInt(invoice.expectedAmount), currency?.decimals || 0)} ${invoice.currency || ""}`
: "-"
}</strong></td>
</tr>
</tfoot>
</table>

${
invoice.contentData.note
invoice.contentData?.note
? `<div style="margin-top: 20px;">
<h3>Memo:</h3>
<h3>Note:</h3>
<p>${invoice.contentData.note}</p>
</div>`
: ""
Expand All @@ -160,11 +156,25 @@ export const exportToPDF = async (

const opt = {
margin: 10,
filename: `invoice-${invoice.contentData.invoiceNumber}.PDF`,
image: { type: "jpeg", quality: 0.98 },
html2canvas: { scale: 2 },
jsPDF: { unit: "mm", format: "a4", orientation: "portrait" },
filename: `invoice-${invoice.contentData?.invoiceNumber || "unknown"}.pdf`,
html2canvas: {
scale: 2,
useCORS: true,
letterRendering: true,
},
jsPDF: {
unit: "mm",
format: "a4",
orientation: "portrait",
compress: true,
},
};

window.html2pdf().from(content).set(opt).save();
const element = document.createElement("div");
element.innerHTML = content;
document.body.appendChild(element);

await window.html2pdf().from(element).set(opt).save();

document.body.removeChild(element);
};