feat: add adjustment total in estimates, invoices, and receipts pdf templates

This commit is contained in:
Ahmed Bouhuolia
2024-12-03 23:37:55 +02:00
parent 3a19518440
commit fabc88c81a
12 changed files with 91 additions and 33 deletions

View File

@@ -1,14 +1,12 @@
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 { GetSaleEstimate } from './GetSaleEstimate'; import { GetSaleEstimate } from './GetSaleEstimate';
import HasTenancyService from '@/services/Tenancy/TenancyService'; import HasTenancyService from '@/services/Tenancy/TenancyService';
import { SaleEstimatePdfTemplate } from '../Invoices/SaleEstimatePdfTemplate'; import { SaleEstimatePdfTemplate } from '../Invoices/SaleEstimatePdfTemplate';
import { transformEstimateToPdfTemplate } from './utils'; import { transformEstimateToPdfTemplate } from './utils';
import { EstimatePdfBrandingAttributes } from './constants';
import events from '@/subscribers/events'; import events from '@/subscribers/events';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher'; import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import { renderEstimatePaperTemplateHtml } from '@bigcapital/pdf-templates'; import { renderEstimatePaperTemplateHtml, EstimatePaperTemplateProps } from '@bigcapital/pdf-templates';
@Service() @Service()
export class SaleEstimatesPdf { export class SaleEstimatesPdf {
@@ -97,7 +95,7 @@ export class SaleEstimatesPdf {
async getEstimateBrandingAttributes( async getEstimateBrandingAttributes(
tenantId: number, tenantId: number,
estimateId: number estimateId: number
): Promise<EstimatePdfBrandingAttributes> { ): Promise<EstimatePaperTemplateProps> {
const { PdfTemplate } = this.tenancy.models(tenantId); const { PdfTemplate } = this.tenancy.models(tenantId);
const saleEstimate = await this.getSaleEstimate.getEstimate( const saleEstimate = await this.getSaleEstimate.getEstimate(
tenantId, tenantId,

View File

@@ -1,9 +1,9 @@
import { EstimatePaperTemplateProps } from '@bigcapital/pdf-templates';
import { contactAddressTextFormat } from '@/utils/address-text-format'; import { contactAddressTextFormat } from '@/utils/address-text-format';
import { EstimatePdfBrandingAttributes } from './constants';
export const transformEstimateToPdfTemplate = ( export const transformEstimateToPdfTemplate = (
estimate estimate
): Partial<EstimatePdfBrandingAttributes> => { ): Partial<EstimatePaperTemplateProps> => {
return { return {
expirationDate: estimate.formattedExpirationDate, expirationDate: estimate.formattedExpirationDate,
estimateNumebr: estimate.estimateNumber, estimateNumebr: estimate.estimateNumber,
@@ -17,6 +17,7 @@ export const transformEstimateToPdfTemplate = (
})), })),
total: estimate.formattedSubtotal, total: estimate.formattedSubtotal,
subtotal: estimate.formattedSubtotal, subtotal: estimate.formattedSubtotal,
adjustment: estimate.adjustmentFormatted,
customerNote: estimate.note, customerNote: estimate.note,
termsConditions: estimate.termsConditions, termsConditions: estimate.termsConditions,
customerAddress: contactAddressTextFormat(estimate.customer), customerAddress: contactAddressTextFormat(estimate.customer),

View File

@@ -1,10 +1,12 @@
import { Inject, Service } from 'typedi'; import { Inject, Service } from 'typedi';
import { renderInvoicePaperTemplateHtml } from '@bigcapital/pdf-templates'; import {
renderInvoicePaperTemplateHtml,
InvoicePaperTemplateProps,
} from '@bigcapital/pdf-templates';
import { ChromiumlyTenancy } from '@/services/ChromiumlyTenancy/ChromiumlyTenancy'; import { ChromiumlyTenancy } from '@/services/ChromiumlyTenancy/ChromiumlyTenancy';
import { GetSaleInvoice } from './GetSaleInvoice'; import { GetSaleInvoice } from './GetSaleInvoice';
import HasTenancyService from '@/services/Tenancy/TenancyService'; import HasTenancyService from '@/services/Tenancy/TenancyService';
import { transformInvoiceToPdfTemplate } from './utils'; import { transformInvoiceToPdfTemplate } from './utils';
import { InvoicePdfTemplateAttributes } from '@/interfaces';
import { SaleInvoicePdfTemplate } from './SaleInvoicePdfTemplate'; import { SaleInvoicePdfTemplate } from './SaleInvoicePdfTemplate';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher'; import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import events from '@/subscribers/events'; import events from '@/subscribers/events';
@@ -100,7 +102,7 @@ export class SaleInvoicePdf {
async getInvoiceBrandingAttributes( async getInvoiceBrandingAttributes(
tenantId: number, tenantId: number,
invoiceId: number invoiceId: number
): Promise<InvoicePdfTemplateAttributes> { ): Promise<InvoicePaperTemplateProps> {
const { PdfTemplate } = this.tenancy.models(tenantId); const { PdfTemplate } = this.tenancy.models(tenantId);
const invoice = await this.getInvoiceService.getSaleInvoice( const invoice = await this.getInvoiceService.getSaleInvoice(

View File

@@ -1,6 +1,7 @@
import { pickBy } from 'lodash'; import { pickBy } from 'lodash';
import { InvoicePdfTemplateAttributes, ISaleInvoice } from '@/interfaces'; import { ISaleInvoice } from '@/interfaces';
import { contactAddressTextFormat } from '@/utils/address-text-format'; import { contactAddressTextFormat } from '@/utils/address-text-format';
import { InvoicePaperTemplateProps } from '@bigcapital/pdf-templates';
export const mergePdfTemplateWithDefaultAttributes = ( export const mergePdfTemplateWithDefaultAttributes = (
brandingTemplate?: Record<string, any>, brandingTemplate?: Record<string, any>,
@@ -18,7 +19,7 @@ export const mergePdfTemplateWithDefaultAttributes = (
export const transformInvoiceToPdfTemplate = ( export const transformInvoiceToPdfTemplate = (
invoice: ISaleInvoice invoice: ISaleInvoice
): Partial<InvoicePdfTemplateAttributes> => { ): Partial<InvoicePaperTemplateProps> => {
return { return {
dueDate: invoice.dueDateFormatted, dueDate: invoice.dueDateFormatted,
dateIssue: invoice.invoiceDateFormatted, dateIssue: invoice.invoiceDateFormatted,
@@ -29,6 +30,7 @@ export const transformInvoiceToPdfTemplate = (
paymentMade: invoice.paymentAmountFormatted, paymentMade: invoice.paymentAmountFormatted,
dueAmount: invoice.dueAmountFormatted, dueAmount: invoice.dueAmountFormatted,
discount: invoice.discountAmountFormatted, discount: invoice.discountAmountFormatted,
adjustment: invoice.adjustmentFormatted,
discountLabel: invoice.discountPercentageFormatted discountLabel: invoice.discountPercentageFormatted
? `Discount [${invoice.discountPercentageFormatted}]` ? `Discount [${invoice.discountPercentageFormatted}]`
: 'Discount', : 'Discount',

View File

@@ -1,13 +1,15 @@
import { Inject, Service } from 'typedi'; import { Inject, Service } from 'typedi';
import { ChromiumlyTenancy } from '@/services/ChromiumlyTenancy/ChromiumlyTenancy'; import { ChromiumlyTenancy } from '@/services/ChromiumlyTenancy/ChromiumlyTenancy';
import {
renderReceiptPaperTemplateHtml,
ReceiptPaperTemplateProps,
} from '@bigcapital/pdf-templates';
import { GetSaleReceipt } from './GetSaleReceipt'; import { GetSaleReceipt } from './GetSaleReceipt';
import HasTenancyService from '@/services/Tenancy/TenancyService'; import HasTenancyService from '@/services/Tenancy/TenancyService';
import { SaleReceiptBrandingTemplate } from './SaleReceiptBrandingTemplate'; import { SaleReceiptBrandingTemplate } from './SaleReceiptBrandingTemplate';
import { transformReceiptToBrandingTemplateAttributes } from './utils'; import { transformReceiptToBrandingTemplateAttributes } from './utils';
import { ISaleReceiptBrandingTemplateAttributes } from '@/interfaces';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher'; import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import events from '@/subscribers/events'; import events from '@/subscribers/events';
import { renderReceiptPaperTemplateHtml } from '@bigcapital/pdf-templates';
@Service() @Service()
export class SaleReceiptsPdf { export class SaleReceiptsPdf {
@@ -90,12 +92,12 @@ export class SaleReceiptsPdf {
* Retrieves receipt branding attributes. * Retrieves receipt branding attributes.
* @param {number} tenantId * @param {number} tenantId
* @param {number} receiptId * @param {number} receiptId
* @returns {Promise<ISaleReceiptBrandingTemplateAttributes>} * @returns {Promise<ReceiptPaperTemplateProps>}
*/ */
public async getReceiptBrandingAttributes( public async getReceiptBrandingAttributes(
tenantId: number, tenantId: number,
receiptId: number receiptId: number
): Promise<ISaleReceiptBrandingTemplateAttributes> { ): Promise<ReceiptPaperTemplateProps> {
const { PdfTemplate } = this.tenancy.models(tenantId); const { PdfTemplate } = this.tenancy.models(tenantId);
const saleReceipt = await this.getSaleReceiptService.getSaleReceipt( const saleReceipt = await this.getSaleReceiptService.getSaleReceipt(

View File

@@ -1,12 +1,10 @@
import { import { ISaleReceipt } from '@/interfaces';
ISaleReceipt,
ISaleReceiptBrandingTemplateAttributes,
} from '@/interfaces';
import { contactAddressTextFormat } from '@/utils/address-text-format'; import { contactAddressTextFormat } from '@/utils/address-text-format';
import { ReceiptPaperTemplateProps } from '@bigcapital/pdf-templates';
export const transformReceiptToBrandingTemplateAttributes = ( export const transformReceiptToBrandingTemplateAttributes = (
saleReceipt: ISaleReceipt saleReceipt: ISaleReceipt
): Partial<ISaleReceiptBrandingTemplateAttributes> => { ): Partial<ReceiptPaperTemplateProps> => {
return { return {
total: saleReceipt.totalFormatted, total: saleReceipt.totalFormatted,
subtotal: saleReceipt.subtotalFormatted, subtotal: saleReceipt.subtotalFormatted,
@@ -23,6 +21,7 @@ export const transformReceiptToBrandingTemplateAttributes = (
discountLabel: saleReceipt.discountPercentageFormatted discountLabel: saleReceipt.discountPercentageFormatted
? `Discount [${saleReceipt.discountPercentageFormatted}]` ? `Discount [${saleReceipt.discountPercentageFormatted}]`
: 'Discount', : 'Discount',
adjustment: saleReceipt.adjustmentFormatted,
customerAddress: contactAddressTextFormat(saleReceipt.customer), customerAddress: contactAddressTextFormat(saleReceipt.customer),
}; };
}; };

View File

@@ -239,6 +239,7 @@ export const useJournalTotals = () => {
const totalDebit = safeSumBy(entries, 'debit'); const totalDebit = safeSumBy(entries, 'debit');
const total = Math.max(totalCredit, totalDebit); const total = Math.max(totalCredit, totalDebit);
// Retrieves the formatted total money. // Retrieves the formatted total money.
const formattedTotal = React.useMemo( const formattedTotal = React.useMemo(
() => formattedAmount(total, currencyCode), () => formattedAmount(total, currencyCode),

View File

@@ -9,7 +9,7 @@
}, },
"main": "./dist/components.umd.js", "main": "./dist/components.umd.js",
"module": "./dist/components.es.js", "module": "./dist/components.es.js",
"types": "./dist/src/index.d.ts", "types": "./dist/index.d.ts",
"exports": { "exports": {
".": { ".": {
"types": "./dist/src/index.d.ts", "types": "./dist/src/index.d.ts",

View File

@@ -43,7 +43,7 @@ export interface EstimatePaperTemplateProps extends PaperTemplateProps {
companyAddress?: string; companyAddress?: string;
billedToLabel?: string; billedToLabel?: string;
// Totals // Total
total?: string; total?: string;
showTotal?: boolean; showTotal?: boolean;
totalLabel?: string; totalLabel?: string;
@@ -53,6 +53,11 @@ export interface EstimatePaperTemplateProps extends PaperTemplateProps {
showDiscount?: boolean; showDiscount?: boolean;
discountLabel?: string; discountLabel?: string;
// # Adjustment
adjustment?: string;
showAdjustment?: boolean;
adjustmentLabel?: string;
// # Subtotal // # Subtotal
subtotal?: string; subtotal?: string;
showSubtotal?: boolean; showSubtotal?: boolean;
@@ -117,6 +122,11 @@ export function EstimatePaperTemplate({
subtotalLabel = 'Subtotal', subtotalLabel = 'Subtotal',
showSubtotal = true, showSubtotal = true,
// # Adjustment
adjustment = '',
showAdjustment = true,
adjustmentLabel = 'Adjustment',
// # Customer Note // # Customer Note
showCustomerNote = true, showCustomerNote = true,
customerNote = DefaultPdfTemplateStatement, customerNote = DefaultPdfTemplateStatement,
@@ -240,6 +250,12 @@ export function EstimatePaperTemplate({
amount={discount} amount={discount}
/> />
)} )}
{showAdjustment && adjustment && (
<PaperTemplate.TotalLine
label={adjustmentLabel}
amount={adjustment}
/>
)}
{showTotal && ( {showTotal && (
<PaperTemplate.TotalLine label={totalLabel} amount={total} /> <PaperTemplate.TotalLine label={totalLabel} amount={total} />
)} )}

View File

@@ -1,3 +1,4 @@
import { isEmpty } from 'lodash';
import { import {
PaperTemplate, PaperTemplate,
PaperTemplateProps, PaperTemplateProps,
@@ -33,17 +34,21 @@ export interface InvoicePaperTemplateProps extends PaperTemplateProps {
primaryColor?: string; primaryColor?: string;
secondaryColor?: string; secondaryColor?: string;
// Company
showCompanyLogo?: boolean; showCompanyLogo?: boolean;
companyLogoUri?: string; companyLogoUri?: string;
// Invoice number
showInvoiceNumber?: boolean; showInvoiceNumber?: boolean;
invoiceNumber?: string; invoiceNumber?: string;
invoiceNumberLabel?: string; invoiceNumberLabel?: string;
// Date of issue
showDateIssue?: boolean; showDateIssue?: boolean;
dateIssue?: string; dateIssue?: string;
dateIssueLabel?: string; dateIssueLabel?: string;
// Due date
showDueDate?: boolean; showDueDate?: boolean;
dueDate?: string; dueDate?: string;
dueDateLabel?: string; dueDateLabel?: string;
@@ -66,7 +71,7 @@ export interface InvoicePaperTemplateProps extends PaperTemplateProps {
lineRateLabel?: string; lineRateLabel?: string;
lineTotalLabel?: string; lineTotalLabel?: string;
// Totals // Total
showTotal?: boolean; showTotal?: boolean;
totalLabel?: string; totalLabel?: string;
total?: string; total?: string;
@@ -76,11 +81,17 @@ export interface InvoicePaperTemplateProps extends PaperTemplateProps {
discountLabel?: string; discountLabel?: string;
discount?: string; discount?: string;
// Adjustment
showAdjustment?: boolean;
adjustmentLabel?: string;
adjustment?: string;
// Subtotal // Subtotal
showSubtotal?: boolean; showSubtotal?: boolean;
subtotalLabel?: string; subtotalLabel?: string;
subtotal?: string; subtotal?: string;
// Payment made
showPaymentMade?: boolean; showPaymentMade?: boolean;
paymentMadeLabel?: string; paymentMadeLabel?: string;
paymentMade?: string; paymentMade?: string;
@@ -97,6 +108,7 @@ export interface InvoicePaperTemplateProps extends PaperTemplateProps {
showTermsConditions?: boolean; showTermsConditions?: boolean;
termsConditions?: string; termsConditions?: string;
// Statement
statementLabel?: string; statementLabel?: string;
showStatement?: boolean; showStatement?: boolean;
statement?: string; statement?: string;
@@ -145,20 +157,24 @@ export function InvoicePaperTemplate({
totalLabel = 'Total', totalLabel = 'Total',
subtotalLabel = 'Subtotal', subtotalLabel = 'Subtotal',
discountLabel = 'Discount', discountLabel = 'Discount',
adjustmentLabel = 'Adjustment',
paymentMadeLabel = 'Payment Made', paymentMadeLabel = 'Payment Made',
dueAmountLabel = 'Balance Due', dueAmountLabel = 'Balance Due',
// Totals // Totals
showTotal = true, showTotal = true,
total = '$662.75',
showSubtotal = true, showSubtotal = true,
showDiscount = true, showDiscount = true,
showTaxes = true, showTaxes = true,
showPaymentMade = true, showPaymentMade = true,
showDueAmount = true, showDueAmount = true,
showAdjustment = true,
total = '$662.75',
subtotal = '630.00', subtotal = '630.00',
discount = '0.00', discount = '0.00',
adjustment = '',
paymentMade = '100.00', paymentMade = '100.00',
dueAmount = '$562.75', dueAmount = '$562.75',
@@ -243,17 +259,18 @@ export function InvoicePaperTemplate({
accessor: (data) => ( accessor: (data) => (
<Stack spacing={2}> <Stack spacing={2}>
<Text>{data.item}</Text> <Text>{data.item}</Text>
<Text <Text color={'#5f6b7c'} fontSize={12}>
color={'#5f6b7c'}
fontSize={12}
>
{data.description} {data.description}
</Text> </Text>
</Stack> </Stack>
), ),
thStyle: { width: '60%' }, thStyle: { width: '60%' },
}, },
{ label: lineQuantityLabel, accessor: 'quantity', align: 'right' }, {
label: lineQuantityLabel,
accessor: 'quantity',
align: 'right',
},
{ label: lineRateLabel, accessor: 'rate', align: 'right' }, { label: lineRateLabel, accessor: 'rate', align: 'right' },
{ label: lineTotalLabel, accessor: 'total', align: 'right' }, { label: lineTotalLabel, accessor: 'total', align: 'right' },
]} ]}
@@ -267,12 +284,18 @@ export function InvoicePaperTemplate({
border={PaperTemplateTotalBorder.Gray} border={PaperTemplateTotalBorder.Gray}
/> />
)} )}
{showDiscount && ( {showDiscount && !isEmpty(discount) && (
<PaperTemplate.TotalLine <PaperTemplate.TotalLine
label={discountLabel} label={discountLabel}
amount={discount} amount={discount}
/> />
)} )}
{showAdjustment && !isEmpty(adjustment) && (
<PaperTemplate.TotalLine
label={adjustmentLabel}
amount={adjustment}
/>
)}
{showTaxes && ( {showTaxes && (
<> <>
{taxes.map((tax, index) => ( {taxes.map((tax, index) => (

View File

@@ -39,6 +39,11 @@ export interface ReceiptPaperTemplateProps extends PaperTemplateProps {
showDiscount?: boolean; showDiscount?: boolean;
discountLabel?: string; discountLabel?: string;
// # Adjustment
adjustment?: string;
showAdjustment?: boolean;
adjustmentLabel?: string;
// Total // Total
total?: string; total?: string;
showTotal?: boolean; showTotal?: boolean;
@@ -111,6 +116,11 @@ export function ReceiptPaperTemplate({
discountLabel = 'Discount', discountLabel = 'Discount',
showDiscount = true, showDiscount = true,
// # Adjustment
adjustment = '',
adjustmentLabel = 'Adjustment',
showAdjustment = true,
// # Subtotal // # Subtotal
subtotal = '1000/00', subtotal = '1000/00',
subtotalLabel = 'Subtotal', subtotalLabel = 'Subtotal',
@@ -228,6 +238,12 @@ export function ReceiptPaperTemplate({
amount={discount} amount={discount}
/> />
)} )}
{showAdjustment && adjustment && (
<PaperTemplate.TotalLine
label={adjustmentLabel}
amount={adjustment}
/>
)}
{showTotal && ( {showTotal && (
<PaperTemplate.TotalLine label={totalLabel} amount={total} /> <PaperTemplate.TotalLine label={totalLabel} amount={total} />
)} )}

View File

@@ -8,8 +8,6 @@ export const renderInvoicePaperTemplateHtml = (
props: InvoicePaperTemplateProps props: InvoicePaperTemplateProps
) => { ) => {
return renderSSR( return renderSSR(
<InvoicePaperTemplate <InvoicePaperTemplate {...props} />
{...props}
/>
); );
}; };