diff --git a/packages/server/src/api/controllers/Sales/SalesInvoices.ts b/packages/server/src/api/controllers/Sales/SalesInvoices.ts index 999806a17..69b00a576 100644 --- a/packages/server/src/api/controllers/Sales/SalesInvoices.ts +++ b/packages/server/src/api/controllers/Sales/SalesInvoices.ts @@ -449,6 +449,7 @@ export default class SaleInvoicesController extends BaseController { const acceptType = accept.types([ ACCEPT_TYPE.APPLICATION_JSON, ACCEPT_TYPE.APPLICATION_PDF, + ACCEPT_TYPE.APPLICATION_TEXT_HTML, ]); // Retrieves invoice in PDF format. if (ACCEPT_TYPE.APPLICATION_PDF === acceptType) { @@ -463,7 +464,13 @@ export default class SaleInvoicesController extends BaseController { 'Content-Disposition': `attachment; filename="${filename}"`, }); res.send(pdfContent); - // Retrieves invoice in json format. + // Retrieves invoice in html json format. + } else if (ACCEPT_TYPE.APPLICATION_TEXT_HTML === acceptType) { + const htmlContent = await this.saleInvoiceApplication.saleInvoiceHtml( + tenantId, + saleInvoiceId + ); + return res.status(200).send({ htmlContent }); } else { const saleInvoice = await this.saleInvoiceApplication.getSaleInvoice( tenantId, diff --git a/packages/server/src/services/Sales/Invoices/SaleInvoicePdf.ts b/packages/server/src/services/Sales/Invoices/SaleInvoicePdf.ts index 394c8c222..1390ff9da 100644 --- a/packages/server/src/services/Sales/Invoices/SaleInvoicePdf.ts +++ b/packages/server/src/services/Sales/Invoices/SaleInvoicePdf.ts @@ -31,6 +31,25 @@ export class SaleInvoicePdf { @Inject() private eventPublisher: EventPublisher; + /** + * Retrieve sale invoice html content. + * @param {number} tenantId - Tenant Id. + * @param {ISaleInvoice} saleInvoice - + * @returns {Promise} + */ + public async saleInvoiceHtml( + tenantId: number, + invoiceId: number + ): Promise { + const brandingAttributes = await this.getInvoiceBrandingAttributes( + tenantId, + invoiceId + ); + return renderInvoicePaperTemplateHtml({ + ...brandingAttributes, + }); + } + /** * Retrieve sale invoice pdf content. * @param {number} tenantId - Tenant Id. @@ -43,13 +62,8 @@ export class SaleInvoicePdf { ): Promise<[Buffer, string]> { const filename = await this.getInvoicePdfFilename(tenantId, invoiceId); - const brandingAttributes = await this.getInvoiceBrandingAttributes( - tenantId, - invoiceId - ); - const htmlContent = renderInvoicePaperTemplateHtml({ - ...brandingAttributes, - }); + const htmlContent = await this.saleInvoiceHtml(tenantId, invoiceId); + // Converts the given html content to pdf document. const buffer = await this.chromiumlyTenancy.convertHtmlContent( tenantId, diff --git a/packages/server/src/services/Sales/Invoices/SaleInvoicesApplication.ts b/packages/server/src/services/Sales/Invoices/SaleInvoicesApplication.ts index 5490bec44..c57e9777e 100644 --- a/packages/server/src/services/Sales/Invoices/SaleInvoicesApplication.ts +++ b/packages/server/src/services/Sales/Invoices/SaleInvoicesApplication.ts @@ -273,6 +273,19 @@ export class SaleInvoiceApplication { return this.pdfSaleInvoiceService.saleInvoicePdf(tenantId, saleInvoiceId); } + /** + * Retrieves the html content of the given sale invoice. + * @param {number} tenantId + * @param {number} saleInvoiceId + * @returns {Promise} + */ + public saleInvoiceHtml( + tenantId: number, + saleInvoiceId: number + ): Promise { + return this.pdfSaleInvoiceService.saleInvoiceHtml(tenantId, saleInvoiceId); + } + /** * * @param {number} tenantId diff --git a/packages/webapp/src/containers/Sales/Invoices/InvoiceSendMailDrawer/InvoiceSendPdfPreviewConnected.tsx b/packages/webapp/src/containers/Sales/Invoices/InvoiceSendMailDrawer/InvoiceSendPdfPreviewConnected.tsx index abf4678c0..49843f187 100644 --- a/packages/webapp/src/containers/Sales/Invoices/InvoiceSendMailDrawer/InvoiceSendPdfPreviewConnected.tsx +++ b/packages/webapp/src/containers/Sales/Invoices/InvoiceSendMailDrawer/InvoiceSendPdfPreviewConnected.tsx @@ -1,18 +1,41 @@ +import { Spinner } from '@blueprintjs/core'; import { css } from '@emotion/css'; -import { Box } from '@/components'; -import { InvoicePaperTemplate } from '../InvoiceCustomize/InvoicePaperTemplate'; +import { Stack } from '@/components'; import { InvoiceSendMailPreviewWithHeader } from './InvoiceSendMailHeaderPreview'; +import { useInvoiceHtml } from '@/hooks/query'; +import { useDrawerContext } from '@/components/Drawer/DrawerProvider'; export function InvoiceSendPdfPreviewConnected() { return ( - - - + + + ); } + +function InvoiceSendPdfPreviewIframe() { + const { payload } = useDrawerContext(); + const { data, isLoading } = useInvoiceHtml(payload?.invoiceId); + + if (isLoading && data) { + return ; + } + const iframeSrcDoc = data?.htmlContent; + + return ( +