From 533006b90e8def0967b00976f64ba9ddf42f15dd Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Tue, 9 Jul 2024 14:47:30 +0200 Subject: [PATCH] fix: Cashflow transactions types --- .../Banking/BankingRulesController.ts | 10 +--- packages/server/src/data/TransactionTypes.ts | 15 +++-- ...saction_type_to_transaction_type_column.js | 60 +++++++++++++++++++ .../server/src/models/AccountTransaction.ts | 3 +- .../server/src/models/CashflowTransaction.ts | 7 +-- .../Banking/Rules/GetBankRulesTransformer.ts | 6 +- .../server/src/utils/transactions-types.ts | 37 +++++++++++- 7 files changed, 113 insertions(+), 25 deletions(-) create mode 100644 packages/server/src/database/migrations/20240709122347_move_cashflow_transaction_type_to_transaction_type_column.js diff --git a/packages/server/src/api/controllers/Banking/BankingRulesController.ts b/packages/server/src/api/controllers/Banking/BankingRulesController.ts index f005a0da0f..42c073cc33 100644 --- a/packages/server/src/api/controllers/Banking/BankingRulesController.ts +++ b/packages/server/src/api/controllers/Banking/BankingRulesController.ts @@ -38,15 +38,7 @@ export class BankingRulesController extends BaseController { body('conditions.*.value').exists(), // Assign - body('assign_category') - .isString() - .isIn([ - 'interest_income', - 'other_income', - 'deposit', - 'expense', - 'owner_drawings', - ]), + body('assign_category').isString(), body('assign_account_id').isInt({ min: 0 }), body('assign_payee').isString().optional({ nullable: true }), body('assign_memo').isString().optional({ nullable: true }), diff --git a/packages/server/src/data/TransactionTypes.ts b/packages/server/src/data/TransactionTypes.ts index 9815ab1833..bdf407d32a 100755 --- a/packages/server/src/data/TransactionTypes.ts +++ b/packages/server/src/data/TransactionTypes.ts @@ -1,7 +1,16 @@ +export const CashflowTransactionTypes = { + OtherIncome: 'Other income', + OtherExpense: 'Other expense', + OwnerDrawing: 'Owner drawing', + OwnerContribution: 'Owner contribution', + TransferToAccount: 'Transfer to account', + TransferFromAccount: 'Transfer from account', +}; + export const TransactionTypes = { SaleInvoice: 'Sale invoice', SaleReceipt: 'Sale receipt', - PaymentReceive: 'Payment receive', + PaymentReceive: 'Payment received', Bill: 'Bill', BillPayment: 'Payment made', VendorOpeningBalance: 'Vendor opening balance', @@ -17,12 +26,10 @@ export const TransactionTypes = { OtherExpense: 'Other expense', OwnerDrawing: 'Owner drawing', InvoiceWriteOff: 'Invoice write-off', - CreditNote: 'transaction_type.credit_note', VendorCredit: 'transaction_type.vendor_credit', - RefundCreditNote: 'transaction_type.refund_credit_note', RefundVendorCredit: 'transaction_type.refund_vendor_credit', - LandedCost: 'transaction_type.landed_cost', + CashflowTransaction: CashflowTransactionTypes, }; diff --git a/packages/server/src/database/migrations/20240709122347_move_cashflow_transaction_type_to_transaction_type_column.js b/packages/server/src/database/migrations/20240709122347_move_cashflow_transaction_type_to_transaction_type_column.js new file mode 100644 index 0000000000..cc8a52e8cd --- /dev/null +++ b/packages/server/src/database/migrations/20240709122347_move_cashflow_transaction_type_to_transaction_type_column.js @@ -0,0 +1,60 @@ +exports.up = function (knex) { + return knex('accounts_transactions') + .whereIn('referenceType', [ + 'OtherIncome', + 'OtherExpense', + 'OwnerDrawing', + 'OwnerContribution', + 'TransferToAccount', + 'TransferFromAccount', + ]) + .update({ + transactionType: knex.raw(` + CASE + WHEN REFERENCE_TYPE = 'OtherIncome' THEN 'OtherIncome' + WHEN REFERENCE_TYPE = 'OtherExpense' THEN 'OtherExpense' + WHEN REFERENCE_TYPE = 'OwnerDrawing' THEN 'OwnerDrawing' + WHEN REFERENCE_TYPE = 'OwnerContribution' THEN 'OwnerContribution' + WHEN REFERENCE_TYPE = 'TransferToAccount' THEN 'TransferToAccount' + WHEN REFERENCE_TYPE = 'TransferFromAccount' THEN 'TransferFromAccount' + END + `), + referenceType: knex.raw(` + CASE + WHEN REFERENCE_TYPE IN ('OtherIncome', 'OtherExpense', 'OwnerDrawing', 'OwnerContribution', 'TransferToAccount', 'TransferFromAccount') THEN 'CashflowTransaction' + ELSE REFERENCE_TYPE + END + `), + }); +}; + +exports.down = function (knex) { + return knex('accounts_transactions') + .whereIn('transactionType', [ + 'OtherIncome', + 'OtherExpense', + 'OwnerDrawing', + 'OwnerContribution', + 'TransferToAccount', + 'TransferFromAccount', + ]) + .update({ + referenceType: knex.raw(` + CASE + WHEN TRANSACTION_TYPE = 'OtherIncome' THEN 'OtherIncome' + WHEN TRANSACTION_TYPE = 'OtherExpense' THEN 'OtherExpense' + WHEN TRANSACTION_TYPE = 'OwnerDrawing' THEN 'OwnerDrawing' + WHEN TRANSACTION_TYPE = 'OwnerContribution' THEN 'OwnerContribution' + WHEN TRANSACTION_TYPE = 'TransferToAccount' THEN 'TransferToAccount' + WHEN TRANSACTION_TYPE = 'TransferFromAccount' THEN 'TransferFromAccount' + ELSE REFERENCE_TYPE + END + `), + transactionType: knex.raw(` + CASE + WHEN TRANSACTION_TYPE IN ('OtherIncome', 'OtherExpense', 'OwnerDrawing', 'OwnerContribution', 'TransferToAccount', 'TransferFromAccount') THEN NULL + ELSE TRANSACTION_TYPE + END + `), + }); +}; diff --git a/packages/server/src/models/AccountTransaction.ts b/packages/server/src/models/AccountTransaction.ts index 368d57afae..f5d968b539 100644 --- a/packages/server/src/models/AccountTransaction.ts +++ b/packages/server/src/models/AccountTransaction.ts @@ -10,6 +10,7 @@ export default class AccountTransaction extends TenantModel { debit: number; exchangeRate: number; taxRate: number; + transactionType: string; /** * Table name @@ -53,7 +54,7 @@ export default class AccountTransaction extends TenantModel { * @return {string} */ get referenceTypeFormatted() { - return getTransactionTypeLabel(this.referenceType); + return getTransactionTypeLabel(this.referenceType, this.transactionType); } /** diff --git a/packages/server/src/models/CashflowTransaction.ts b/packages/server/src/models/CashflowTransaction.ts index f1cb8dd7ac..3e50886f10 100644 --- a/packages/server/src/models/CashflowTransaction.ts +++ b/packages/server/src/models/CashflowTransaction.ts @@ -6,7 +6,7 @@ import { getCashflowTransactionType, } from '@/services/Cashflow/utils'; import { CASHFLOW_DIRECTION } from '@/services/Cashflow/constants'; -import { getTransactionTypeLabel } from '@/utils/transactions-types'; +import { getCashflowTransactionFormattedType } from '@/utils/transactions-types'; export default class CashflowTransaction extends TenantModel { transactionType: string; @@ -64,7 +64,7 @@ export default class CashflowTransaction extends TenantModel { * @returns {string} */ get transactionTypeFormatted() { - return getTransactionTypeLabel(this.transactionType); + return getCashflowTransactionFormattedType(this.transactionType); } get typeMeta() { @@ -159,8 +159,7 @@ export default class CashflowTransaction extends TenantModel { to: 'accounts_transactions.referenceId', }, filter(builder) { - const referenceTypes = getCashflowAccountTransactionsTypes(); - builder.whereIn('reference_type', referenceTypes); + builder.where('reference_type', 'CashflowTransaction'); }, }, diff --git a/packages/server/src/services/Banking/Rules/GetBankRulesTransformer.ts b/packages/server/src/services/Banking/Rules/GetBankRulesTransformer.ts index 431b4f42df..0a5767652f 100644 --- a/packages/server/src/services/Banking/Rules/GetBankRulesTransformer.ts +++ b/packages/server/src/services/Banking/Rules/GetBankRulesTransformer.ts @@ -1,6 +1,5 @@ -import { upperFirst, camelCase } from 'lodash'; import { Transformer } from '@/lib/Transformer/Transformer'; -import { getTransactionTypeLabel } from '@/utils/transactions-types'; +import { getCashflowTransactionFormattedType } from '@/utils/transactions-types'; export class GetBankRulesTransformer extends Transformer { /** @@ -29,8 +28,7 @@ export class GetBankRulesTransformer extends Transformer { * @returns {string} */ protected assignCategoryFormatted(bankRule: any) { - const assignCategory = upperFirst(camelCase(bankRule.assignCategory)); - return getTransactionTypeLabel(assignCategory); + return getCashflowTransactionFormattedType(bankRule.assignCategory); } /** diff --git a/packages/server/src/utils/transactions-types.ts b/packages/server/src/utils/transactions-types.ts index ab5981115a..bda6a21594 100644 --- a/packages/server/src/utils/transactions-types.ts +++ b/packages/server/src/utils/transactions-types.ts @@ -1,5 +1,36 @@ -import { TransactionTypes } from '@/data/TransactionTypes'; +import { isObject, upperFirst, camelCase } from 'lodash'; +import { + TransactionTypes, + CashflowTransactionTypes, +} from '@/data/TransactionTypes'; -export const getTransactionTypeLabel = (transactionType: string) => { - return TransactionTypes[transactionType]; +/** + * Retrieves the formatted type of account transaction. + * @param {string} referenceType + * @param {string} transactionType + * @returns {string} + */ +export const getTransactionTypeLabel = ( + referenceType: string, + transactionType?: string +) => { + const _referenceType = upperFirst(camelCase(referenceType)); + const _transactionType = upperFirst(camelCase(transactionType)); + + return isObject(TransactionTypes[_referenceType]) + ? TransactionTypes[_referenceType][_transactionType] + : TransactionTypes[_referenceType] || null; +}; + +/** + * Retrieves the formatted type of cashflow transaction. + * @param {string} transactionType + * @returns {string¿} + */ +export const getCashflowTransactionFormattedType = ( + transactionType: string +) => { + const _transactionType = upperFirst(camelCase(transactionType)); + + return CashflowTransactionTypes[_transactionType] || null; };