From 1010d97a92a06e2bc2a69787b32756617c9179d9 Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Tue, 3 Dec 2024 13:54:26 +0200 Subject: [PATCH] fix: discount and adjustment fields across financial forms --- .../src/api/controllers/Sales/CreditNotes.ts | 11 ++++++++--- .../src/api/controllers/Sales/SalesInvoices.ts | 8 ++++++-- .../src/api/controllers/Sales/SalesReceipts.ts | 6 +++++- ...0241128080734_add_discount_to_invoices_table.js | 4 ++-- packages/server/src/models/SaleEstimate.ts | 12 ++++++++++++ packages/server/src/models/SaleInvoice.ts | 4 ++-- .../GetSaleEstimateMailStateTransformer.ts | 14 +------------- .../Sales/Estimates/SaleEstimateTransformer.ts | 4 ++-- .../Sales/Receipts/ReceiptForm/utils.tsx | 2 +- 9 files changed, 39 insertions(+), 26 deletions(-) diff --git a/packages/server/src/api/controllers/Sales/CreditNotes.ts b/packages/server/src/api/controllers/Sales/CreditNotes.ts index 6f7a9a6ea..55ec28e98 100644 --- a/packages/server/src/api/controllers/Sales/CreditNotes.ts +++ b/packages/server/src/api/controllers/Sales/CreditNotes.ts @@ -4,6 +4,7 @@ import { Inject, Service } from 'typedi'; import { AbilitySubject, CreditNoteAction, + DiscountType, ICreditNoteEditDTO, ICreditNoteNewDTO, } from '@/interfaces'; @@ -253,7 +254,10 @@ export default class PaymentReceivesController extends BaseController { // Discount. check('discount').optional({ nullable: true }).isNumeric().toFloat(), - check('discount_type').optional({ nullable: true }).isString().trim(), + check('discount_type') + .optional({ nullable: true }) + .isString() + .isIn([DiscountType.Percentage, DiscountType.Amount]), // Adjustment. check('adjustment').optional({ nullable: true }).isNumeric().toFloat(), @@ -763,8 +767,9 @@ export default class PaymentReceivesController extends BaseController { const { tenantId } = req; try { - const data = - await this.getCreditNoteStateService.getCreditNoteState(tenantId); + const data = await this.getCreditNoteStateService.getCreditNoteState( + tenantId + ); return res.status(200).send({ data }); } catch (error) { next(error); diff --git a/packages/server/src/api/controllers/Sales/SalesInvoices.ts b/packages/server/src/api/controllers/Sales/SalesInvoices.ts index 389c8b4d2..da0a40d18 100644 --- a/packages/server/src/api/controllers/Sales/SalesInvoices.ts +++ b/packages/server/src/api/controllers/Sales/SalesInvoices.ts @@ -11,6 +11,7 @@ import { SaleInvoiceAction, AbilitySubject, SendInvoiceMailDTO, + DiscountType, } from '@/interfaces'; import CheckPolicies from '@/api/middleware/CheckPolicies'; import { SaleInvoiceApplication } from '@/services/Sales/Invoices/SaleInvoicesApplication'; @@ -284,10 +285,13 @@ export default class SaleInvoicesController extends BaseController { // Discount check('discount').optional({ nullable: true }).isNumeric().toFloat(), - check('discount_type').optional({ nullable: true }).isString().trim(), + check('discount_type') + .optional({ nullable: true }) + .isString() + .isIn([DiscountType.Percentage, DiscountType.Amount]), // Adjustments - check('adjustment').optional({ nullable: true }).isArray(), + check('adjustment').optional({ nullable: true }).isNumeric().toFloat(), ]; } diff --git a/packages/server/src/api/controllers/Sales/SalesReceipts.ts b/packages/server/src/api/controllers/Sales/SalesReceipts.ts index 368dd2c8e..a48a10e09 100644 --- a/packages/server/src/api/controllers/Sales/SalesReceipts.ts +++ b/packages/server/src/api/controllers/Sales/SalesReceipts.ts @@ -176,14 +176,18 @@ export default class SalesReceiptsController extends BaseController { check('attachments').isArray().optional(), check('attachments.*.key').exists().isString(), - // Pdf template id. + // # Pdf template check('pdf_template_id').optional({ nullable: true }).isNumeric().toInt(), // # Discount check('discount').optional({ nullable: true }).isNumeric().toFloat(), check('discount_type') .optional({ nullable: true }) + .isString() .isIn([DiscountType.Percentage, DiscountType.Amount]), + + // # Adjustment + check('adjustment').optional({ nullable: true }).isNumeric().toFloat(), ]; } diff --git a/packages/server/src/database/migrations/20241128080734_add_discount_to_invoices_table.js b/packages/server/src/database/migrations/20241128080734_add_discount_to_invoices_table.js index a3cc3f2c0..d65b00615 100644 --- a/packages/server/src/database/migrations/20241128080734_add_discount_to_invoices_table.js +++ b/packages/server/src/database/migrations/20241128080734_add_discount_to_invoices_table.js @@ -6,7 +6,7 @@ exports.up = function (knex) { return knex.schema.alterTable('sales_invoices', (table) => { table.decimal('discount', 10, 2).nullable().after('credited_amount'); table.string('discount_type').nullable().after('discount'); - table.decimal('adjustments', 10, 2).nullable().after('discount_type'); + table.decimal('adjustment', 10, 2).nullable().after('discount_type'); }); }; @@ -18,6 +18,6 @@ exports.down = function (knex) { return knex.schema.alterTable('sale_invoices', (table) => { table.dropColumn('discount'); table.dropColumn('discount_type'); - table.dropColumn('adjustments'); + table.dropColumn('adjustment'); }); }; diff --git a/packages/server/src/models/SaleEstimate.ts b/packages/server/src/models/SaleEstimate.ts index 3fa8a7905..83df38c1e 100644 --- a/packages/server/src/models/SaleEstimate.ts +++ b/packages/server/src/models/SaleEstimate.ts @@ -23,6 +23,14 @@ export default class SaleEstimate extends mixin(TenantModel, [ public adjustment: number; + public expirationDate!: string; + public deliveredAt!: string | null; + public approvedAt!: string | null; + public rejectedAt!: string | null; + + public convertedToInvoiceId!: number | null; + public convertedToInvoiceAt!: string | null; + /** * Table name */ @@ -45,6 +53,10 @@ export default class SaleEstimate extends mixin(TenantModel, [ 'localAmount', 'discountAmount', 'discountPercentage', + 'total', + 'totalLocal', + 'subtotal', + 'subtotalLocal', 'isDelivered', 'isExpired', 'isConvertedToInvoice', diff --git a/packages/server/src/models/SaleInvoice.ts b/packages/server/src/models/SaleInvoice.ts index 7d56b59e2..7e874d2de 100644 --- a/packages/server/src/models/SaleInvoice.ts +++ b/packages/server/src/models/SaleInvoice.ts @@ -26,7 +26,7 @@ export default class SaleInvoice extends mixin(TenantModel, [ public deliveredAt: Date; public discount: number; public discountType: DiscountType; - public adjustments: number; + public adjustment: number | null; /** * Table name @@ -157,7 +157,7 @@ export default class SaleInvoice extends mixin(TenantModel, [ * @returns {number} */ get total() { - const adjustmentAmount = defaultTo(this.adjustments, 0); + const adjustmentAmount = defaultTo(this.adjustment, 0); const differencies = this.discountAmount + adjustmentAmount; return this.isInclusiveTax diff --git a/packages/server/src/services/Sales/Estimates/GetSaleEstimateMailStateTransformer.ts b/packages/server/src/services/Sales/Estimates/GetSaleEstimateMailStateTransformer.ts index 9dd2b749a..06fdf5436 100644 --- a/packages/server/src/services/Sales/Estimates/GetSaleEstimateMailStateTransformer.ts +++ b/packages/server/src/services/Sales/Estimates/GetSaleEstimateMailStateTransformer.ts @@ -118,25 +118,13 @@ export class GetSaleEstimateMailStateTransformer extends SaleEstimateTransfromer : 'Discount'; } - /** - * Retrieves the formatted total of the estimate. - * @param estimate - * @returns {string} - */ - protected totalFormatted(estimate) { - return this.formatMoney(estimate.amount, { - currencyCode: estimate.currencyCode, - money: true, - }); - } - /** * Retrieves the formatted subtotal of the estimate. * @param estimate * @returns {string} */ protected subtotalFormatted = (estimate) => { - return this.formatNumber(estimate.amount, { money: false }); + return this.formattedSubtotal(estimate); }; /** diff --git a/packages/server/src/services/Sales/Estimates/SaleEstimateTransformer.ts b/packages/server/src/services/Sales/Estimates/SaleEstimateTransformer.ts index 8876d0a14..dff7915ca 100644 --- a/packages/server/src/services/Sales/Estimates/SaleEstimateTransformer.ts +++ b/packages/server/src/services/Sales/Estimates/SaleEstimateTransformer.ts @@ -141,7 +141,7 @@ export class SaleEstimateTransfromer extends Transformer { * @returns {string} */ protected totalFormatted = (estimate: ISaleEstimate): string => { - return formatNumber(estimate.total, { + return this.formatMoney(estimate.total, { currencyCode: estimate.currencyCode, }); }; @@ -152,7 +152,7 @@ export class SaleEstimateTransfromer extends Transformer { * @returns {string} */ protected totalLocalFormatted = (estimate: ISaleEstimate): string => { - return formatNumber(estimate.totalLocal, { + return this.formatMoney(estimate.totalLocal, { currencyCode: estimate.currencyCode, }); }; diff --git a/packages/webapp/src/containers/Sales/Receipts/ReceiptForm/utils.tsx b/packages/webapp/src/containers/Sales/Receipts/ReceiptForm/utils.tsx index 00fabb037..c7365c680 100644 --- a/packages/webapp/src/containers/Sales/Receipts/ReceiptForm/utils.tsx +++ b/packages/webapp/src/containers/Sales/Receipts/ReceiptForm/utils.tsx @@ -284,7 +284,7 @@ export const useReceiptTotal = () => { const adjustmentAmount = useReceiptAdjustmentAmount(); const discountAmount = useReceiptDiscountAmount(); - return subtotal - discountAmount + adjustmentAmount; + return subtotal - discountAmount - adjustmentAmount; }; /**