Skip to content

Commit

Permalink
Merge pull request #315 from bigcapitalhq/hotfix-pdf-printing
Browse files Browse the repository at this point in the history
fix(server): the invoice and payment receipt printing
  • Loading branch information
abouolia authored Jan 20, 2024
2 parents 03bc78a + 8f43159 commit f03d011
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 57 deletions.
4 changes: 2 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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/
# GOTENBERG_URL=http://localhost:9000
# GOTENBERG_DOCS_URL=http://host.docker.internal:3000/public/
60 changes: 25 additions & 35 deletions packages/server/src/api/controllers/Sales/SalesInvoices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 6 additions & 18 deletions packages/server/src/services/Sales/Invoices/SaleInvoicePdf.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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.
Expand All @@ -28,18 +24,10 @@ export class SaleInvoicePdf {
tenantId: number,
invoiceId: number
): Promise<Buffer> {
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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class PaymentReceiveEntryTransfromer extends Transformer {
* @returns {Array}
*/
public includeAttributes = (): string[] => {
return ['paymentAmountFormatted', 'entry'];
return ['paymentAmountFormatted', 'invoice'];
};

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit f03d011

Please sign in to comment.