Skip to content

Commit

Permalink
Merge pull request #524 from bigcapitalhq/fix-cashflow-transactions-type
Browse files Browse the repository at this point in the history
fix: Cashflow transactions types
  • Loading branch information
abouolia authored Jul 9, 2024
2 parents d096e49 + 533006b commit da435d8
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand Down
15 changes: 11 additions & 4 deletions packages/server/src/data/TransactionTypes.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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,
};
Original file line number Diff line number Diff line change
@@ -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
`),
});
};
3 changes: 2 additions & 1 deletion packages/server/src/models/AccountTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default class AccountTransaction extends TenantModel {
debit: number;
exchangeRate: number;
taxRate: number;
transactionType: string;

/**
* Table name
Expand Down Expand Up @@ -53,7 +54,7 @@ export default class AccountTransaction extends TenantModel {
* @return {string}
*/
get referenceTypeFormatted() {
return getTransactionTypeLabel(this.referenceType);
return getTransactionTypeLabel(this.referenceType, this.transactionType);
}

/**
Expand Down
7 changes: 3 additions & 4 deletions packages/server/src/models/CashflowTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,7 +64,7 @@ export default class CashflowTransaction extends TenantModel {
* @returns {string}
*/
get transactionTypeFormatted() {
return getTransactionTypeLabel(this.transactionType);
return getCashflowTransactionFormattedType(this.transactionType);
}

get typeMeta() {
Expand Down Expand Up @@ -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');
},
},

Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down
37 changes: 34 additions & 3 deletions packages/server/src/utils/transactions-types.ts
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit da435d8

Please sign in to comment.