From 8f431597d975730a90efbebd68bae5efa8834188 Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Sat, 20 Jan 2024 15:51:57 +0200 Subject: [PATCH] fix(server): hotbug the invoice and payment receipt printing --- .env.example | 4 +- .../api/controllers/Sales/SalesInvoices.ts | 60 ++++++++----------- .../services/Sales/Invoices/SaleInvoicePdf.ts | 24 ++------ .../PaymentReceiveEntryTransformer.ts | 2 +- .../PaymentReceiveTransformer.ts | 1 - 5 files changed, 34 insertions(+), 57 deletions(-) diff --git a/.env.example b/.env.example index 7a86ab2808..78945ab91b 100644 --- a/.env.example +++ b/.env.example @@ -56,5 +56,5 @@ GOTENBERG_URL=http://gotenberg:3000 GOTENBERG_DOCS_URL=http://server:3000/public/ # Gotenberg API - (development) -# GOTENBERG_URL=http://gotenberg:3000 -# GOTENBERG_DOCS_URL=http://server:3000/public/ \ No newline at end of file +# GOTENBERG_URL=http://localhost:9000 +# GOTENBERG_DOCS_URL=http://host.docker.internal:3000/public/ \ No newline at end of file diff --git a/packages/server/src/api/controllers/Sales/SalesInvoices.ts b/packages/server/src/api/controllers/Sales/SalesInvoices.ts index 9e5ac8d258..c66d2dae11 100644 --- a/packages/server/src/api/controllers/Sales/SalesInvoices.ts +++ b/packages/server/src/api/controllers/Sales/SalesInvoices.ts @@ -418,44 +418,34 @@ export default class SaleInvoicesController extends BaseController { * @param {Request} req - Request object. * @param {Response} res - Response object. */ - private async getSaleInvoice( - req: Request, - res: Response, - next: NextFunction - ) { + private async getSaleInvoice(req: Request, res: Response) { const { id: saleInvoiceId } = req.params; const { tenantId, user } = req; - try { - const saleInvoice = await this.saleInvoiceApplication.getSaleInvoice( - tenantId, - saleInvoiceId, - user - ); - // Response formatter. - res.format({ - // JSON content type. - [ACCEPT_TYPE.APPLICATION_JSON]: () => { - return res - .status(200) - .send(this.transfromToResponse({ saleInvoice })); - }, - // PDF content type. - [ACCEPT_TYPE.APPLICATION_PDF]: async () => { - const pdfContent = await this.saleInvoiceApplication.saleInvoicePdf( - tenantId, - saleInvoice - ); - res.set({ - 'Content-Type': 'application/pdf', - 'Content-Length': pdfContent.length, - }); - res.send(pdfContent); - }, - }); - } catch (error) { - next(error); - } + // Response formatter. + return res.format({ + // JSON content type. + [ACCEPT_TYPE.APPLICATION_JSON]: async () => { + const saleInvoice = await this.saleInvoiceApplication.getSaleInvoice( + tenantId, + saleInvoiceId, + user + ); + return res.status(200).send(this.transfromToResponse({ saleInvoice })); + }, + // PDF content type. + [ACCEPT_TYPE.APPLICATION_PDF]: async () => { + const pdfContent = await this.saleInvoiceApplication.saleInvoicePdf( + tenantId, + saleInvoiceId + ); + res.set({ + 'Content-Type': 'application/pdf', + 'Content-Length': pdfContent.length, + }); + res.send(pdfContent); + }, + }); } /** * Retrieve paginated sales invoices with custom view metadata. diff --git a/packages/server/src/services/Sales/Invoices/SaleInvoicePdf.ts b/packages/server/src/services/Sales/Invoices/SaleInvoicePdf.ts index 9cccf94ef9..2bbe7e0038 100644 --- a/packages/server/src/services/Sales/Invoices/SaleInvoicePdf.ts +++ b/packages/server/src/services/Sales/Invoices/SaleInvoicePdf.ts @@ -1,8 +1,7 @@ import { Inject, Service } from 'typedi'; import { ChromiumlyTenancy } from '@/services/ChromiumlyTenancy/ChromiumlyTenancy'; import { TemplateInjectable } from '@/services/TemplateInjectable/TemplateInjectable'; -import HasTenancyService from '@/services/Tenancy/TenancyService'; -import { CommandSaleInvoiceValidators } from './CommandSaleInvoiceValidators'; +import { GetSaleInvoice } from './GetSaleInvoice'; @Service() export class SaleInvoicePdf { @@ -13,10 +12,7 @@ export class SaleInvoicePdf { private templateInjectable: TemplateInjectable; @Inject() - private validators: CommandSaleInvoiceValidators; - - @Inject() - private tenancy: HasTenancyService; + private getInvoiceService: GetSaleInvoice; /** * Retrieve sale invoice pdf content. @@ -28,18 +24,10 @@ export class SaleInvoicePdf { tenantId: number, invoiceId: number ): Promise { - const { SaleInvoice } = this.tenancy.models(tenantId); - - const saleInvoice = await SaleInvoice.query() - .findById(invoiceId) - .withGraphFetched('entries.item') - .withGraphFetched('entries.tax') - .withGraphFetched('customer') - .withGraphFetched('taxes.taxRate'); - - // Validates the given sale invoice existance. - this.validators.validateInvoiceExistance(saleInvoice); - + const saleInvoice = await this.getInvoiceService.getSaleInvoice( + tenantId, + invoiceId + ); const htmlContent = await this.templateInjectable.render( tenantId, 'modules/invoice-regular', diff --git a/packages/server/src/services/Sales/PaymentReceives/PaymentReceiveEntryTransformer.ts b/packages/server/src/services/Sales/PaymentReceives/PaymentReceiveEntryTransformer.ts index fe9fe9b679..7a7af28c11 100644 --- a/packages/server/src/services/Sales/PaymentReceives/PaymentReceiveEntryTransformer.ts +++ b/packages/server/src/services/Sales/PaymentReceives/PaymentReceiveEntryTransformer.ts @@ -8,7 +8,7 @@ export class PaymentReceiveEntryTransfromer extends Transformer { * @returns {Array} */ public includeAttributes = (): string[] => { - return ['paymentAmountFormatted', 'entry']; + return ['paymentAmountFormatted', 'invoice']; }; /** diff --git a/packages/server/src/services/Sales/PaymentReceives/PaymentReceiveTransformer.ts b/packages/server/src/services/Sales/PaymentReceives/PaymentReceiveTransformer.ts index afce4203bb..531023ce3a 100644 --- a/packages/server/src/services/Sales/PaymentReceives/PaymentReceiveTransformer.ts +++ b/packages/server/src/services/Sales/PaymentReceives/PaymentReceiveTransformer.ts @@ -1,7 +1,6 @@ import { IPaymentReceive, IPaymentReceiveEntry } from '@/interfaces'; import { Transformer } from '@/lib/Transformer/Transformer'; import { formatNumber } from 'utils'; -import { SaleInvoiceTransformer } from '../Invoices/SaleInvoiceTransformer'; import { PaymentReceiveEntryTransfromer } from './PaymentReceiveEntryTransformer'; export class PaymentReceiveTransfromer extends Transformer {