feat: discount formatted attributes of sale transactions

This commit is contained in:
Ahmed Bouhuolia
2024-11-28 17:59:09 +02:00
parent df8391201f
commit e02ad1e795
12 changed files with 246 additions and 25 deletions

View File

@@ -25,6 +25,11 @@ export interface ISaleEstimate {
branchId?: number;
warehouseId?: number;
discountAmount?: number;
discountPercentage?: number | null;
adjustment?: number;
}
export interface ISaleEstimateDTO {
customerId: number;

View File

@@ -83,8 +83,8 @@ export interface ISaleInvoice {
}
export enum DiscountType {
Percentage = 'Percentage',
Amount = 'Amount',
Percentage = 'percentage',
Amount = 'amount',
}
export interface ISaleInvoiceDTO {

View File

@@ -28,6 +28,11 @@ export interface ISaleReceipt {
localAmount?: number;
entries?: IItemEntry[];
discountAmount: number;
discountPercentage?: number | null;
adjustment?: number;
}
export interface ISalesReceiptsFilter {

View File

@@ -72,16 +72,32 @@ export default class CreditNote extends mixin(TenantModel, [
return this.subtotal * this.exchangeRate;
}
/**
* Discount amount.
* @returns {number}
*/
get discountAmount() {
return this.discountType === DiscountType.Amount
? this.discount
: this.subtotal * (this.discount / 100);
}
/**
* Discount percentage.
* @returns {number | null}
*/
get discountPercentage(): number | null {
return this.discountType === DiscountType.Percentage
? this.discount
: null;
}
/**
* Credit note total.
* @returns {number}
*/
get total() {
const discountAmount = this.discountType === DiscountType.Amount
? this.discount
: this.subtotal * (this.discount / 100);
return this.subtotal - discountAmount - this.adjustment;
return this.subtotal - this.discountAmount - this.adjustment;
}
/**

View File

@@ -75,18 +75,34 @@ export default class SaleEstimate extends mixin(TenantModel, [
return this.localAmount;
}
/**
* Discount amount.
* @returns {number}
*/
get discountAmount() {
return this.discountType === DiscountType.Amount
? this.discount
: this.subtotal * (this.discount / 100);
}
/**
* Discount percentage.
* @returns {number | null}
*/
get discountPercentage(): number | null {
return this.discountType === DiscountType.Percentage
? this.discount
: null;
}
/**
* Estimate total.
* @returns {number}
*/
get total() {
const discountAmount = this.discountType === DiscountType.Amount
? this.discount
: this.subtotal * (this.discount / 100);
const adjustmentAmount = defaultTo(this.adjustment, 0);
return this.subtotal - discountAmount - adjustmentAmount;
return this.subtotal - this.discountAmount - adjustmentAmount;
}
/**

View File

@@ -28,7 +28,6 @@ export default class SaleInvoice extends mixin(TenantModel, [
public discountType: DiscountType;
public adjustments: number;
/**
* Table name
*/
@@ -72,6 +71,8 @@ export default class SaleInvoice extends mixin(TenantModel, [
'subtotalExludingTax',
'taxAmountWithheldLocal',
'discountAmount',
'total',
'totalLocal',
@@ -130,17 +131,33 @@ export default class SaleInvoice extends mixin(TenantModel, [
return this.taxAmountWithheld * this.exchangeRate;
}
/**
* Discount amount.
* @returns {number}
*/
get discountAmount() {
return this.discountType === DiscountType.Amount
? this.discount
: this.subtotal * (this.discount / 100);
}
/**
* Discount percentage.
* @returns {number | null}
*/
get discountPercentage(): number | null {
return this.discountType === DiscountType.Percentage
? this.discount
: null;
}
/**
* Invoice total. (Tax included)
* @returns {number}
*/
get total() {
const discountAmount = this.discountType === DiscountType.Amount
? this.discount
: this.subtotal * (this.discount / 100);
const adjustmentAmount = defaultTo(this.adjustments, 0);
const differencies = discountAmount + adjustmentAmount;
const differencies = this.discountAmount + adjustmentAmount;
return this.isInclusiveTax
? this.subtotal - differencies
@@ -616,7 +633,7 @@ export default class SaleInvoice extends mixin(TenantModel, [
join: {
from: 'sales_invoices.pdfTemplateId',
to: 'pdf_templates.id',
}
},
},
};
}

View File

@@ -67,18 +67,34 @@ export default class SaleReceipt extends mixin(TenantModel, [
return this.localAmount;
}
/**
* Discount amount.
* @returns {number}
*/
get discountAmount() {
return this.discountType === DiscountType.Amount
? this.discount
: this.subtotal * (this.discount / 100);
}
/**
* Discount percentage.
* @returns {number | null}
*/
get discountPercentage(): number | null {
return this.discountType === DiscountType.Percentage
? this.discount
: null;
}
/**
* Receipt total.
* @returns {number}
*/
get total() {
const discountAmount = this.discountType === DiscountType.Amount
? this.discount
: this.subtotal * (this.discount / 100);
const adjustmentAmount = defaultTo(this.adjustment, 0);
return this.subtotal - discountAmount - adjustmentAmount;
return this.subtotal - this.discountAmount - adjustmentAmount;
}
/**

View File

@@ -18,6 +18,9 @@ export class CreditNoteTransformer extends Transformer {
'formattedAmount',
'formattedCreditsUsed',
'formattedSubtotal',
'discountAmountFormatted',
'discountPercentageFormatted',
'adjustmentFormatted',
'entries',
'attachments',
];
@@ -83,6 +86,40 @@ export class CreditNoteTransformer extends Transformer {
return formatNumber(credit.amount, { money: false });
};
/**
* Retrieves formatted discount amount.
* @param credit
* @returns {string}
*/
protected discountAmountFormatted = (credit): string => {
return formatNumber(credit.discountAmount, {
currencyCode: credit.currencyCode,
});
};
/**
* Retrieves formatted discount percentage.
* @param credit
* @returns {string}
*/
protected discountPercentageFormatted = (credit): string => {
return credit.discountPercentage
? `${credit.discountPercentage}%`
: '';
};
/**
* Retrieves formatted adjustment amount.
* @param credit
* @returns {string}
*/
protected adjustmentFormatted = (credit): string => {
return this.formatMoney(credit.adjustment, {
currencyCode: credit.currencyCode,
});
};
/**
* Retrieves the entries of the credit note.
* @param {ICreditNote} credit

View File

@@ -24,7 +24,7 @@ export class CreateItem {
/**
* Authorize the creating item.
* @param {number} tenantId
* @param {number} tenantId
* @param {IItemDTO} itemDTO
*/
async authorize(tenantId: number, itemDTO: IItemDTO) {

View File

@@ -18,6 +18,9 @@ export class SaleEstimateTransfromer extends Transformer {
'formattedDeliveredAtDate',
'formattedApprovedAtDate',
'formattedRejectedAtDate',
'discountAmountFormatted',
'discountPercentageFormatted',
'adjustmentFormatted',
'formattedCreatedAt',
'entries',
'attachments',
@@ -98,6 +101,39 @@ export class SaleEstimateTransfromer extends Transformer {
return formatNumber(estimate.amount, { money: false });
};
/**
* Retrieves formatted discount amount.
* @param estimate
* @returns {string}
*/
protected discountAmountFormatted = (estimate: ISaleEstimate): string => {
return formatNumber(estimate.discountAmount, {
currencyCode: estimate.currencyCode,
});
};
/**
* Retrieves formatted discount percentage.
* @param estimate
* @returns {string}
*/
protected discountPercentageFormatted = (estimate: ISaleEstimate): string => {
return estimate.discountPercentage
? `${estimate.discountPercentage}%`
: '';
};
/**
* Retrieves formatted adjustment amount.
* @param estimate
* @returns {string}
*/
protected adjustmentFormatted = (estimate: ISaleEstimate): string => {
return this.formatMoney(estimate.adjustment, {
currencyCode: estimate.currencyCode,
});
};
/**
* Retrieves the entries of the sale estimate.
* @param {ISaleEstimate} estimate

View File

@@ -3,6 +3,7 @@ import { formatNumber } from 'utils';
import { SaleInvoiceTaxEntryTransformer } from './SaleInvoiceTaxEntryTransformer';
import { ItemEntryTransformer } from './ItemEntryTransformer';
import { AttachmentTransformer } from '@/services/Attachments/AttachmentTransformer';
import { DiscountType } from '@/interfaces';
export class SaleInvoiceTransformer extends Transformer {
/**
@@ -23,6 +24,9 @@ export class SaleInvoiceTransformer extends Transformer {
'subtotalExludingTaxFormatted',
'taxAmountWithheldFormatted',
'taxAmountWithheldLocalFormatted',
'discountAmountFormatted',
'discountPercentageFormatted',
'adjustmentFormatted',
'totalFormatted',
'totalLocalFormatted',
'taxes',
@@ -158,6 +162,39 @@ export class SaleInvoiceTransformer extends Transformer {
});
};
/**
* Retrieves formatted discount amount.
* @param invoice
* @returns {string}
*/
protected discountAmountFormatted = (invoice): string => {
return formatNumber(invoice.discountAmount, {
currencyCode: invoice.currencyCode,
});
};
/**
* Retrieves formatted discount percentage.
* @param invoice
* @returns {string}
*/
protected discountPercentageFormatted = (invoice): string => {
return invoice.discountType === DiscountType.Percentage
? `${invoice.discount}%`
: '';
};
/**
* Retrieves formatted adjustment amount.
* @param invoice
* @returns {string}
*/
protected adjustmentFormatted = (invoice): string => {
return this.formatMoney(invoice.adjustment, {
currencyCode: invoice.currencyCode,
})
}
/**
* Retrieves formatted total in foreign currency.
* @param invoice

View File

@@ -14,6 +14,9 @@ export class SaleReceiptTransformer extends Transformer {
public includeAttributes = (): string[] => {
return [
'formattedSubtotal',
'discountAmountFormatted',
'discountPercentageFormatted',
'adjustmentFormatted',
'formattedAmount',
'formattedReceiptDate',
'formattedClosedAtDate',
@@ -70,6 +73,39 @@ export class SaleReceiptTransformer extends Transformer {
});
};
/**
* Retrieves formatted discount amount.
* @param receipt
* @returns {string}
*/
protected discountAmountFormatted = (receipt: ISaleReceipt): string => {
return formatNumber(receipt.discountAmount, {
currencyCode: receipt.currencyCode,
});
};
/**
* Retrieves formatted discount percentage.
* @param receipt
* @returns {string}
*/
protected discountPercentageFormatted = (receipt: ISaleReceipt): string => {
return receipt.discountPercentage
? `${receipt.discountPercentage}%`
: '';
};
/**
* Retrieves formatted adjustment amount.
* @param receipt
* @returns {string}
*/
protected adjustmentFormatted = (receipt: ISaleReceipt): string => {
return this.formatMoney(receipt.adjustment, {
currencyCode: receipt.currencyCode,
});
};
/**
* Retrieves the entries of the credit note.
* @param {ISaleReceipt} credit