feat: discount vendor credit

This commit is contained in:
Ahmed Bouhuolia
2024-11-28 18:17:47 +02:00
parent e02ad1e795
commit 17b3bbe1d8
5 changed files with 135 additions and 7 deletions

View File

@@ -0,0 +1,23 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function(knex) {
return knex.schema.alterTable('vendor_credits', (table) => {
table.decimal('discount', 10, 2).nullable().after('amount');
table.string('discount_type').nullable().after('discount');
table.decimal('adjustment', 10, 2).nullable().after('discount_type');
});
};
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function(knex) {
return knex.schema.alterTable('vendor_credits', (table) => {
table.dropColumn('discount');
table.dropColumn('discount_type');
table.dropColumn('adjustment');
});
};

View File

@@ -104,20 +104,37 @@ export default class Bill 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.adjustment, 0);
return this.isInclusiveTax
? this.subtotal - discountAmount - adjustmentAmount
: this.subtotal + this.taxAmountWithheld - discountAmount - adjustmentAmount;
? this.subtotal - this.discountAmount - adjustmentAmount
: this.subtotal +
this.taxAmountWithheld -
this.discountAmount -
adjustmentAmount;
}
/**

View File

@@ -58,6 +58,24 @@ export default class VendorCredit 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;
}
/**
* Vendor credit total.
* @returns {number}

View File

@@ -24,6 +24,9 @@ export class PurchaseInvoiceTransformer extends Transformer {
'subtotalLocalFormatted',
'subtotalExcludingTaxFormatted',
'taxAmountWithheldLocalFormatted',
'discountAmountFormatted',
'discountPercentageFormatted',
'adjustmentFormatted',
'totalFormatted',
'totalLocalFormatted',
'taxes',
@@ -160,6 +163,39 @@ export class PurchaseInvoiceTransformer extends Transformer {
});
};
/**
* Retrieves the formatted discount amount.
* @param {IBill} bill
* @returns {string}
*/
protected discountAmountFormatted = (bill): string => {
return formatNumber(bill.discountAmount, {
currencyCode: bill.currencyCode,
});
};
/**
* Retrieves the formatted discount percentage.
* @param {IBill} bill
* @returns {string}
*/
protected discountPercentageFormatted = (bill): string => {
return bill.discountPercentage
? `${bill.discountPercentage}%`
: '';
};
/**
* Retrieves the formatted adjustment amount.
* @param {IBill} bill
* @returns {string}
*/
protected adjustmentFormatted = (bill): string => {
return formatNumber(bill.adjustment, {
currencyCode: bill.currencyCode,
});
};
/**
* Retrieves the total formatted.
* @param {IBill} bill

View File

@@ -17,6 +17,9 @@ export class VendorCreditTransformer extends Transformer {
'formattedCreatedAt',
'formattedCreditsRemaining',
'formattedInvoicedAmount',
'discountAmountFormatted',
'discountPercentageFormatted',
'adjustmentFormatted',
'entries',
'attachments',
];
@@ -33,7 +36,7 @@ export class VendorCreditTransformer extends Transformer {
/**
* Retireve formatted created at date.
* @param vendorCredit
* @param vendorCredit
* @returns {string}
*/
protected formattedCreatedAt = (vendorCredit): string => {
@@ -71,6 +74,37 @@ export class VendorCreditTransformer extends Transformer {
});
};
/**
* Retrieves the formatted discount amount.
* @param {IVendorCredit} credit
* @returns {string}
*/
protected discountAmountFormatted = (credit): string => {
return formatNumber(credit.discountAmount, {
currencyCode: credit.currencyCode,
});
};
/**
* Retrieves the formatted discount percentage.
* @param {IVendorCredit} credit
* @returns {string}
*/
protected discountPercentageFormatted = (credit): string => {
return credit.discountPercentage ? `${credit.discountPercentage}%` : '';
};
/**
* Retrieves the formatted adjustment amount.
* @param {IVendorCredit} credit
* @returns {string}
*/
protected adjustmentFormatted = (credit): string => {
return formatNumber(credit.adjustment, {
currencyCode: credit.currencyCode,
});
};
/**
* Retrieves the formatted invoiced amount.
* @param credit