mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 20:30:33 +00:00
feat: rendering pdf templates on the server-side
This commit is contained in:
@@ -59,7 +59,7 @@ export default class CreateCreditNote extends BaseCreditNotes {
|
||||
creditNoteDTO.entries
|
||||
);
|
||||
// Transformes the given DTO to storage layer data.
|
||||
const creditNoteModel = this.transformCreateEditDTOToModel(
|
||||
const creditNoteModel = await this.transformCreateEditDTOToModel(
|
||||
tenantId,
|
||||
creditNoteDTO,
|
||||
customer.currencyCode
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import { Inject } from "typedi";
|
||||
import { GetPdfTemplate } from "../PdfTemplate/GetPdfTemplate";
|
||||
import { defaultCreditNoteBrandingAttributes } from "./constants";
|
||||
import { mergePdfTemplateWithDefaultAttributes } from "../Sales/Invoices/utils";
|
||||
|
||||
export class CreditNoteBrandingTemplate {
|
||||
@Inject()
|
||||
private getPdfTemplateService: GetPdfTemplate;
|
||||
|
||||
/**
|
||||
* Retrieves the credit note branding template.
|
||||
* @param {number} tenantId
|
||||
* @param {number} templateId
|
||||
* @returns {}
|
||||
*/
|
||||
public async getCreditNoteBrandingTemplate(tenantId: number, templateId: number) {
|
||||
const template = await this.getPdfTemplateService.getPdfTemplate(
|
||||
tenantId,
|
||||
templateId
|
||||
);
|
||||
const attributes = mergePdfTemplateWithDefaultAttributes(
|
||||
template.attributes,
|
||||
defaultCreditNoteBrandingAttributes
|
||||
);
|
||||
return {
|
||||
...template,
|
||||
attributes,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import { Service, Inject } from 'typedi';
|
||||
import moment from 'moment';
|
||||
import { omit } from 'lodash';
|
||||
import * as R from 'ramda';
|
||||
import composeAsync from 'async/compose';
|
||||
import { ServiceError } from '@/exceptions';
|
||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
import { ERRORS } from './constants';
|
||||
@@ -16,6 +17,7 @@ import AutoIncrementOrdersService from '@/services/Sales/AutoIncrementOrdersServ
|
||||
import { WarehouseTransactionDTOTransform } from '@/services/Warehouses/Integrations/WarehouseTransactionDTOTransform';
|
||||
import { BranchTransactionDTOTransform } from '@/services/Branches/Integrations/BranchTransactionDTOTransform';
|
||||
import { assocItemEntriesDefaultIndex } from '../Items/utils';
|
||||
import { BrandingTemplateDTOTransformer } from '../PdfTemplate/BrandingTemplateDTOTransformer';
|
||||
|
||||
@Service()
|
||||
export default class BaseCreditNotes {
|
||||
@@ -34,17 +36,20 @@ export default class BaseCreditNotes {
|
||||
@Inject()
|
||||
private warehouseDTOTransform: WarehouseTransactionDTOTransform;
|
||||
|
||||
@Inject()
|
||||
private brandingTemplatesTransformer: BrandingTemplateDTOTransformer;
|
||||
|
||||
/**
|
||||
* Transformes the credit/edit DTO to model.
|
||||
* @param {ICreditNoteNewDTO | ICreditNoteEditDTO} creditNoteDTO
|
||||
* @param {string} customerCurrencyCode -
|
||||
*/
|
||||
protected transformCreateEditDTOToModel = (
|
||||
protected transformCreateEditDTOToModel = async (
|
||||
tenantId: number,
|
||||
creditNoteDTO: ICreditNoteNewDTO | ICreditNoteEditDTO,
|
||||
customerCurrencyCode: string,
|
||||
oldCreditNote?: ICreditNote
|
||||
): ICreditNote => {
|
||||
): Promise<ICreditNote> => {
|
||||
// Retrieve the total amount of the given items entries.
|
||||
const amount = this.itemsEntriesService.getTotalItemsEntries(
|
||||
creditNoteDTO.entries
|
||||
@@ -83,10 +88,18 @@ export default class BaseCreditNotes {
|
||||
refundedAmount: 0,
|
||||
invoicesAmount: 0,
|
||||
};
|
||||
const initialAsyncDTO = await composeAsync(
|
||||
// Assigns the default branding template id to the invoice DTO.
|
||||
this.brandingTemplatesTransformer.assocDefaultBrandingTemplate(
|
||||
tenantId,
|
||||
'CreditNote'
|
||||
)
|
||||
)(initialDTO);
|
||||
|
||||
return R.compose(
|
||||
this.branchDTOTransform.transformDTO<ICreditNote>(tenantId),
|
||||
this.warehouseDTOTransform.transformDTO<ICreditNote>(tenantId)
|
||||
)(initialDTO);
|
||||
)(initialAsyncDTO);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -63,7 +63,7 @@ export default class EditCreditNote extends BaseCreditNotes {
|
||||
creditNoteEditDTO.entries
|
||||
);
|
||||
// Transformes the given DTO to storage layer data.
|
||||
const creditNoteModel = this.transformCreateEditDTOToModel(
|
||||
const creditNoteModel = await this.transformCreateEditDTOToModel(
|
||||
tenantId,
|
||||
creditNoteEditDTO,
|
||||
customer.currencyCode,
|
||||
|
||||
@@ -2,9 +2,16 @@ import { Inject, Service } from 'typedi';
|
||||
import { ChromiumlyTenancy } from '../ChromiumlyTenancy/ChromiumlyTenancy';
|
||||
import { TemplateInjectable } from '../TemplateInjectable/TemplateInjectable';
|
||||
import GetCreditNote from './GetCreditNote';
|
||||
import { CreditNoteBrandingTemplate } from './CreditNoteBrandingTemplate';
|
||||
import { CreditNotePdfTemplateAttributes } from '@/interfaces';
|
||||
import HasTenancyService from '../Tenancy/TenancyService';
|
||||
import { transformCreditNoteToPdfTemplate } from './utils';
|
||||
|
||||
@Service()
|
||||
export default class GetCreditNotePdf {
|
||||
@Inject()
|
||||
private tenancy: HasTenancyService;
|
||||
|
||||
@Inject()
|
||||
private chromiumlyTenancy: ChromiumlyTenancy;
|
||||
|
||||
@@ -14,25 +21,62 @@ export default class GetCreditNotePdf {
|
||||
@Inject()
|
||||
private getCreditNoteService: GetCreditNote;
|
||||
|
||||
@Inject()
|
||||
private creditNoteBrandingTemplate: CreditNoteBrandingTemplate;
|
||||
|
||||
/**
|
||||
* Retrieve sale invoice pdf content.
|
||||
* Retrieves sale invoice pdf content.
|
||||
* @param {number} tenantId - Tenant id.
|
||||
* @param {number} creditNoteId - Credit note id.
|
||||
*/
|
||||
public async getCreditNotePdf(tenantId: number, creditNoteId: number) {
|
||||
const brandingAttributes = await this.getCreditNoteBrandingAttributes(
|
||||
tenantId,
|
||||
creditNoteId
|
||||
);
|
||||
console.log(brandingAttributes, 'brandingAttributes');
|
||||
|
||||
const htmlContent = await this.templateInjectable.render(
|
||||
tenantId,
|
||||
'modules/credit-note-standard',
|
||||
brandingAttributes
|
||||
);
|
||||
return this.chromiumlyTenancy.convertHtmlContent(tenantId, htmlContent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves credit note branding attributes.
|
||||
* @param {number} tenantId - The ID of the tenant.
|
||||
* @param {number} creditNoteId - The ID of the credit note.
|
||||
* @returns {Promise<CreditNotePdfTemplateAttributes>} The credit note branding attributes.
|
||||
*/
|
||||
public async getCreditNoteBrandingAttributes(
|
||||
tenantId: number,
|
||||
creditNoteId: number
|
||||
): Promise<CreditNotePdfTemplateAttributes> {
|
||||
const { PdfTemplate } = this.tenancy.models(tenantId);
|
||||
const creditNote = await this.getCreditNoteService.getCreditNote(
|
||||
tenantId,
|
||||
creditNoteId
|
||||
);
|
||||
const htmlContent = await this.templateInjectable.render(
|
||||
tenantId,
|
||||
'modules/credit-note-standard',
|
||||
{
|
||||
creditNote,
|
||||
}
|
||||
);
|
||||
return this.chromiumlyTenancy.convertHtmlContent(tenantId, htmlContent, {
|
||||
margins: { top: 0, bottom: 0, left: 0, right: 0 },
|
||||
});
|
||||
// Retrieve the invoice template id of not found get the default template id.
|
||||
const templateId =
|
||||
creditNote.pdfTemplateId ??
|
||||
(
|
||||
await PdfTemplate.query().findOne({
|
||||
resource: 'CreditNote',
|
||||
default: true,
|
||||
})
|
||||
)?.id;
|
||||
// Retrieves the credit note branding template.
|
||||
const brandingTemplate =
|
||||
await this.creditNoteBrandingTemplate.getCreditNoteBrandingTemplate(
|
||||
tenantId,
|
||||
templateId
|
||||
);
|
||||
return {
|
||||
...brandingTemplate.attributes,
|
||||
...transformCreditNoteToPdfTemplate(creditNote),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ export const ERRORS = {
|
||||
'CREDIT_NOTE_APPLY_TO_INVOICES_NOT_FOUND',
|
||||
CREDIT_NOTE_HAS_REFUNDS_TRANSACTIONS: 'CREDIT_NOTE_HAS_REFUNDS_TRANSACTIONS',
|
||||
CREDIT_NOTE_HAS_APPLIED_INVOICES: 'CREDIT_NOTE_HAS_APPLIED_INVOICES',
|
||||
CUSTOMER_HAS_LINKED_CREDIT_NOTES: 'CUSTOMER_HAS_LINKED_CREDIT_NOTES'
|
||||
CUSTOMER_HAS_LINKED_CREDIT_NOTES: 'CUSTOMER_HAS_LINKED_CREDIT_NOTES',
|
||||
};
|
||||
|
||||
export const DEFAULT_VIEW_COLUMNS = [];
|
||||
@@ -66,3 +66,72 @@ export const DEFAULT_VIEWS = [
|
||||
columns: DEFAULT_VIEW_COLUMNS,
|
||||
},
|
||||
];
|
||||
|
||||
export const defaultCreditNoteBrandingAttributes = {
|
||||
primaryColor: '',
|
||||
secondaryColor: '',
|
||||
showCompanyLogo: true,
|
||||
companyLogo: '',
|
||||
companyName: 'Bigcapital Technology, Inc.',
|
||||
|
||||
// Address
|
||||
billedToAddress: [
|
||||
'Bigcapital Technology, Inc.',
|
||||
'131 Continental Dr Suite 305 Newark,',
|
||||
'Delaware 19713',
|
||||
'United States',
|
||||
'+1 762-339-5634',
|
||||
'ahmed@bigcapital.app',
|
||||
],
|
||||
billedFromAddress: [
|
||||
'131 Continental Dr Suite 305 Newark,',
|
||||
'Delaware 19713',
|
||||
'United States',
|
||||
'+1 762-339-5634',
|
||||
'ahmed@bigcapital.app',
|
||||
],
|
||||
showBilledToAddress: true,
|
||||
showBilledFromAddress: true,
|
||||
billedToLabel: 'Billed To',
|
||||
|
||||
// Total
|
||||
total: '$1000.00',
|
||||
totalLabel: 'Total',
|
||||
showTotal: true,
|
||||
|
||||
// Subtotal
|
||||
subtotal: '1000/00',
|
||||
subtotalLabel: 'Subtotal',
|
||||
showSubtotal: true,
|
||||
|
||||
// Customer note
|
||||
showCustomerNote: true,
|
||||
customerNote:
|
||||
'It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.',
|
||||
customerNoteLabel: 'Customer Note',
|
||||
|
||||
// Terms & conditions
|
||||
showTermsConditions: true,
|
||||
termsConditions:
|
||||
'It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.',
|
||||
termsConditionsLabel: 'Terms & Conditions',
|
||||
|
||||
lines: [
|
||||
{
|
||||
item: 'Simply dummy text',
|
||||
description: 'Simply dummy text of the printing and typesetting',
|
||||
rate: '1',
|
||||
quantity: '1000',
|
||||
total: '$1000.00',
|
||||
},
|
||||
],
|
||||
// Credit note number.
|
||||
showCreditNoteNumber: true,
|
||||
creditNoteNumberLabel: 'Credit Note Number',
|
||||
creditNoteNumebr: '346D3D40-0001',
|
||||
|
||||
// Credit note date.
|
||||
creditNoteDate: 'September 3, 2024',
|
||||
showCreditNoteDate: true,
|
||||
creditNoteDateLabel: 'Credit Note Date',
|
||||
};
|
||||
|
||||
9
packages/server/src/services/CreditNotes/utils.ts
Normal file
9
packages/server/src/services/CreditNotes/utils.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { CreditNotePdfTemplateAttributes } from "@/interfaces";
|
||||
import CreditNote from "@/models/CreditNote";
|
||||
|
||||
|
||||
export const transformCreditNoteToPdfTemplate = (creditNote: CreditNote): Partial<CreditNotePdfTemplateAttributes> {
|
||||
return {
|
||||
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user