feat: add discount functionality to sales and purchase transactions

- Introduced discount_type and discount fields in Bills and SalesReceipts controllers.
- Updated database migrations to include discount and discount_type in estimates and credit notes tables.
- Enhanced SaleReceipt and SaleEstimate models to support discount attributes.
- Implemented formatting for discount amounts in transformers and PDF templates.
- Updated email templates to display discount information.

This commit enhances the handling of discounts across various transaction types, improving the overall functionality and user experience.
This commit is contained in:
Ahmed Bouhuolia
2024-11-30 14:46:43 +02:00
parent 17b3bbe1d8
commit dd1392cdc8
21 changed files with 196 additions and 35 deletions

View File

@@ -21,6 +21,8 @@ export class CreditNoteTransformer extends Transformer {
'discountAmountFormatted',
'discountPercentageFormatted',
'adjustmentFormatted',
'totalFormatted',
'totalLocalFormatted',
'entries',
'attachments',
];
@@ -86,7 +88,6 @@ export class CreditNoteTransformer extends Transformer {
return formatNumber(credit.amount, { money: false });
};
/**
* Retrieves formatted discount amount.
* @param credit
@@ -120,6 +121,28 @@ export class CreditNoteTransformer extends Transformer {
});
};
/**
* Retrieves the formatted total.
* @param credit
* @returns {string}
*/
protected totalFormatted = (credit): string => {
return formatNumber(credit.total, {
currencyCode: credit.currencyCode,
});
};
/**
* Retrieves the formatted total in local currency.
* @param credit
* @returns {string}
*/
protected totalLocalFormatted = (credit): string => {
return formatNumber(credit.totalLocal, {
currencyCode: credit.currencyCode,
});
};
/**
* Retrieves the entries of the credit note.
* @param {ICreditNote} credit

View File

@@ -254,18 +254,27 @@ export interface EstimatePdfBrandingAttributes {
companyAddress: string;
billedToLabel: string;
// # Total
total: string;
totalLabel: string;
showTotal: boolean;
// # Discount
discount: string;
showDiscount: boolean;
discountLabel: string;
// # Subtotal
subtotal: string;
subtotalLabel: string;
showSubtotal: boolean;
// # Customer Note
showCustomerNote: boolean;
customerNote: string;
customerNoteLabel: string;
// # Terms & Conditions
showTermsConditions: boolean;
termsConditions: string;
termsConditionsLabel: string;

View File

@@ -20,6 +20,10 @@ export const transformEstimateToPdfTemplate = (
customerNote: estimate.note,
termsConditions: estimate.termsConditions,
customerAddress: contactAddressTextFormat(estimate.customer),
discount: estimate.discountAmountFormatted,
discountLabel: estimate.discountPercentageFormatted
? `Discount [${estimate.discountPercentageFormatted}]`
: 'Discount',
};
};

View File

@@ -1,7 +1,6 @@
import { Inject, Service } from 'typedi';
import { renderInvoicePaperTemplateHtml } from '@bigcapital/pdf-templates';
import { ChromiumlyTenancy } from '@/services/ChromiumlyTenancy/ChromiumlyTenancy';
import { TemplateInjectable } from '@/services/TemplateInjectable/TemplateInjectable';
import { GetSaleInvoice } from './GetSaleInvoice';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { transformInvoiceToPdfTemplate } from './utils';
@@ -9,7 +8,6 @@ import { InvoicePdfTemplateAttributes } from '@/interfaces';
import { SaleInvoicePdfTemplate } from './SaleInvoicePdfTemplate';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import events from '@/subscribers/events';
import { renderInvoicePaymentEmail } from '@bigcapital/email-components';
@Service()
export class SaleInvoicePdf {

View File

@@ -28,6 +28,10 @@ export const transformInvoiceToPdfTemplate = (
subtotal: invoice.subtotalFormatted,
paymentMade: invoice.paymentAmountFormatted,
dueAmount: invoice.dueAmountFormatted,
discount: invoice.discountAmountFormatted,
discountLabel: invoice.discountPercentageFormatted
? `Discount [${invoice.discountPercentageFormatted}]`
: 'Discount',
termsConditions: invoice.termsConditions,
statement: invoice.invoiceMessage,

View File

@@ -13,9 +13,12 @@ export class SaleReceiptTransformer extends Transformer {
*/
public includeAttributes = (): string[] => {
return [
'formattedSubtotal',
'discountAmountFormatted',
'discountPercentageFormatted',
'subtotalFormatted',
'subtotalLocalFormatted',
'totalFormatted',
'totalLocalFormatted',
'adjustmentFormatted',
'formattedAmount',
'formattedReceiptDate',
@@ -58,8 +61,37 @@ export class SaleReceiptTransformer extends Transformer {
* @param {ISaleReceipt} receipt
* @returns {string}
*/
protected formattedSubtotal = (receipt: ISaleReceipt): string => {
return formatNumber(receipt.amount, { money: false });
protected subtotalFormatted = (receipt: ISaleReceipt): string => {
return formatNumber(receipt.subtotal, { money: false });
};
/**
* Retrieves the estimate formatted subtotal in local currency.
* @param {ISaleReceipt} receipt
* @returns {string}
*/
protected subtotalLocalFormatted = (receipt: ISaleReceipt): string => {
return formatNumber(receipt.subtotalLocal, {
currencyCode: receipt.currencyCode,
});
};
/**
* Retrieves the receipt formatted total.
* @param receipt
* @returns {string}
*/
protected totalFormatted = (receipt: ISaleReceipt): string => {
return formatNumber(receipt.total, { money: false });
};
/**
* Retrieves the receipt formatted total in local currency.
* @param receipt
* @returns {string}
*/
protected totalLocalFormatted = (receipt: ISaleReceipt): string => {
return formatNumber(receipt.totalLocal, { money: false });
};
/**
@@ -67,7 +99,7 @@ export class SaleReceiptTransformer extends Transformer {
* @param {ISaleReceipt} estimate
* @returns {string}
*/
protected formattedAmount = (receipt: ISaleReceipt): string => {
protected amountFormatted = (receipt: ISaleReceipt): string => {
return formatNumber(receipt.amount, {
currencyCode: receipt.currencyCode,
});

View File

@@ -1,5 +1,4 @@
import { Inject, Service } from 'typedi';
import { TemplateInjectable } from '@/services/TemplateInjectable/TemplateInjectable';
import { ChromiumlyTenancy } from '@/services/ChromiumlyTenancy/ChromiumlyTenancy';
import { GetSaleReceipt } from './GetSaleReceipt';
import HasTenancyService from '@/services/Tenancy/TenancyService';
@@ -18,9 +17,6 @@ export class SaleReceiptsPdf {
@Inject()
private chromiumlyTenancy: ChromiumlyTenancy;
@Inject()
private templateInjectable: TemplateInjectable;
@Inject()
private getSaleReceiptService: GetSaleReceipt;

View File

@@ -8,8 +8,8 @@ export const transformReceiptToBrandingTemplateAttributes = (
saleReceipt: ISaleReceipt
): Partial<ISaleReceiptBrandingTemplateAttributes> => {
return {
total: saleReceipt.formattedAmount,
subtotal: saleReceipt.formattedSubtotal,
total: saleReceipt.totalFormatted,
subtotal: saleReceipt.subtotalFormatted,
lines: saleReceipt.entries?.map((entry) => ({
item: entry.item.name,
description: entry.description,
@@ -19,6 +19,10 @@ export const transformReceiptToBrandingTemplateAttributes = (
})),
receiptNumber: saleReceipt.receiptNumber,
receiptDate: saleReceipt.formattedReceiptDate,
discount: saleReceipt.discountAmountFormatted,
discountLabel: saleReceipt.discountPercentageFormatted
? `Discount [${saleReceipt.discountPercentageFormatted}]`
: 'Discount',
customerAddress: contactAddressTextFormat(saleReceipt.customer),
};
};