fix(server): hotbug the invoice and payment receipt printing

This commit is contained in:
Ahmed Bouhuolia
2024-01-20 15:51:57 +02:00
parent 03bc78a068
commit 8f431597d9
5 changed files with 34 additions and 57 deletions

View File

@@ -56,5 +56,5 @@ GOTENBERG_URL=http://gotenberg:3000
GOTENBERG_DOCS_URL=http://server:3000/public/ GOTENBERG_DOCS_URL=http://server:3000/public/
# Gotenberg API - (development) # Gotenberg API - (development)
# GOTENBERG_URL=http://gotenberg:3000 # GOTENBERG_URL=http://localhost:9000
# GOTENBERG_DOCS_URL=http://server:3000/public/ # GOTENBERG_DOCS_URL=http://host.docker.internal:3000/public/

View File

@@ -418,44 +418,34 @@ export default class SaleInvoicesController extends BaseController {
* @param {Request} req - Request object. * @param {Request} req - Request object.
* @param {Response} res - Response object. * @param {Response} res - Response object.
*/ */
private async getSaleInvoice( private async getSaleInvoice(req: Request, res: Response) {
req: Request,
res: Response,
next: NextFunction
) {
const { id: saleInvoiceId } = req.params; const { id: saleInvoiceId } = req.params;
const { tenantId, user } = req; const { tenantId, user } = req;
try { // Response formatter.
const saleInvoice = await this.saleInvoiceApplication.getSaleInvoice( return res.format({
tenantId, // JSON content type.
saleInvoiceId, [ACCEPT_TYPE.APPLICATION_JSON]: async () => {
user const saleInvoice = await this.saleInvoiceApplication.getSaleInvoice(
); tenantId,
// Response formatter. saleInvoiceId,
res.format({ user
// JSON content type. );
[ACCEPT_TYPE.APPLICATION_JSON]: () => { return res.status(200).send(this.transfromToResponse({ saleInvoice }));
return res },
.status(200) // PDF content type.
.send(this.transfromToResponse({ saleInvoice })); [ACCEPT_TYPE.APPLICATION_PDF]: async () => {
}, const pdfContent = await this.saleInvoiceApplication.saleInvoicePdf(
// PDF content type. tenantId,
[ACCEPT_TYPE.APPLICATION_PDF]: async () => { saleInvoiceId
const pdfContent = await this.saleInvoiceApplication.saleInvoicePdf( );
tenantId, res.set({
saleInvoice 'Content-Type': 'application/pdf',
); 'Content-Length': pdfContent.length,
res.set({ });
'Content-Type': 'application/pdf', res.send(pdfContent);
'Content-Length': pdfContent.length, },
}); });
res.send(pdfContent);
},
});
} catch (error) {
next(error);
}
} }
/** /**
* Retrieve paginated sales invoices with custom view metadata. * Retrieve paginated sales invoices with custom view metadata.

View File

@@ -1,8 +1,7 @@
import { Inject, Service } from 'typedi'; import { Inject, Service } from 'typedi';
import { ChromiumlyTenancy } from '@/services/ChromiumlyTenancy/ChromiumlyTenancy'; import { ChromiumlyTenancy } from '@/services/ChromiumlyTenancy/ChromiumlyTenancy';
import { TemplateInjectable } from '@/services/TemplateInjectable/TemplateInjectable'; import { TemplateInjectable } from '@/services/TemplateInjectable/TemplateInjectable';
import HasTenancyService from '@/services/Tenancy/TenancyService'; import { GetSaleInvoice } from './GetSaleInvoice';
import { CommandSaleInvoiceValidators } from './CommandSaleInvoiceValidators';
@Service() @Service()
export class SaleInvoicePdf { export class SaleInvoicePdf {
@@ -13,10 +12,7 @@ export class SaleInvoicePdf {
private templateInjectable: TemplateInjectable; private templateInjectable: TemplateInjectable;
@Inject() @Inject()
private validators: CommandSaleInvoiceValidators; private getInvoiceService: GetSaleInvoice;
@Inject()
private tenancy: HasTenancyService;
/** /**
* Retrieve sale invoice pdf content. * Retrieve sale invoice pdf content.
@@ -28,18 +24,10 @@ export class SaleInvoicePdf {
tenantId: number, tenantId: number,
invoiceId: number invoiceId: number
): Promise<Buffer> { ): Promise<Buffer> {
const { SaleInvoice } = this.tenancy.models(tenantId); const saleInvoice = await this.getInvoiceService.getSaleInvoice(
tenantId,
const saleInvoice = await SaleInvoice.query() invoiceId
.findById(invoiceId) );
.withGraphFetched('entries.item')
.withGraphFetched('entries.tax')
.withGraphFetched('customer')
.withGraphFetched('taxes.taxRate');
// Validates the given sale invoice existance.
this.validators.validateInvoiceExistance(saleInvoice);
const htmlContent = await this.templateInjectable.render( const htmlContent = await this.templateInjectable.render(
tenantId, tenantId,
'modules/invoice-regular', 'modules/invoice-regular',

View File

@@ -8,7 +8,7 @@ export class PaymentReceiveEntryTransfromer extends Transformer {
* @returns {Array} * @returns {Array}
*/ */
public includeAttributes = (): string[] => { public includeAttributes = (): string[] => {
return ['paymentAmountFormatted', 'entry']; return ['paymentAmountFormatted', 'invoice'];
}; };
/** /**

View File

@@ -1,7 +1,6 @@
import { IPaymentReceive, IPaymentReceiveEntry } from '@/interfaces'; import { IPaymentReceive, IPaymentReceiveEntry } from '@/interfaces';
import { Transformer } from '@/lib/Transformer/Transformer'; import { Transformer } from '@/lib/Transformer/Transformer';
import { formatNumber } from 'utils'; import { formatNumber } from 'utils';
import { SaleInvoiceTransformer } from '../Invoices/SaleInvoiceTransformer';
import { PaymentReceiveEntryTransfromer } from './PaymentReceiveEntryTransformer'; import { PaymentReceiveEntryTransfromer } from './PaymentReceiveEntryTransformer';
export class PaymentReceiveTransfromer extends Transformer { export class PaymentReceiveTransfromer extends Transformer {