@memberjunction/actions-bizapps-accounting
Version:
Accounting system integration actions for MemberJunction
123 lines • 5.27 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { RegisterClass } from '@memberjunction/global';
import { QuickBooksBaseAction } from '../quickbooks-base.action.js';
import { BaseAction } from '@memberjunction/actions';
/**
* Action to retrieve the Chart of Accounts (GL Codes) from QuickBooks Online
*/
let GetQuickBooksGLCodesAction = class GetQuickBooksGLCodesAction extends QuickBooksBaseAction {
/**
* Description of the action
*/
get Description() {
return 'Retrieves the Chart of Accounts (GL Codes) from QuickBooks Online for a specific company';
}
/**
* Main execution method
*/
async InternalRunAction(params) {
try {
const contextUser = params.ContextUser;
if (!contextUser) {
throw new Error('Context user is required for QuickBooks API calls');
}
// Store params for use in other methods
this._params = params.Params;
// Get parameter values
const includeInactive = this.getParamValue(params.Params, 'IncludeInactive') || false;
const accountTypes = this.getParamValue(params.Params, 'AccountTypes');
const parentAccountID = this.getParamValue(params.Params, 'ParentAccountID');
// Build the query
let query = 'SELECT * FROM Account';
const conditions = [];
// Add active filter
if (!includeInactive) {
conditions.push('Active = true');
}
// Add account type filter
if (accountTypes) {
const types = accountTypes.split(',').map((t) => `'${t.trim()}'`);
conditions.push(`AccountType IN (${types.join(',')})`);
}
// Add parent account filter
if (parentAccountID) {
conditions.push(`ParentRef = '${parentAccountID}'`);
}
// Add conditions to query
if (conditions.length > 0) {
query += ' WHERE ' + conditions.join(' AND ');
}
// Add ordering
query += ' ORDER BY FullyQualifiedName';
// Execute the query
const response = await this.queryQBO(query, contextUser);
// Process the results
const accounts = response.QueryResponse?.Account || [];
const glCodes = accounts.map(account => this.mapQBOAccountToGLCode(account));
// Set output parameters
const outputParams = [
{
Name: 'GLCodes',
Value: glCodes,
Type: 'Output'
},
{
Name: 'TotalCount',
Value: glCodes.length,
Type: 'Output'
}
];
return {
Success: true,
ResultCode: 'SUCCESS',
Params: [...params.Params, ...outputParams],
Message: `Successfully retrieved ${glCodes.length} GL codes from QuickBooks`
};
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
return {
Success: false,
ResultCode: 'ERROR',
Message: errorMessage,
Params: params.Params
};
}
}
/**
* Maps a QuickBooks account to our standard GL Code interface
*/
mapQBOAccountToGLCode(account) {
return {
id: account.Id,
code: account.AcctNum || account.Id,
name: account.Name,
type: this.mapAccountType(account.AccountType),
subType: account.AccountSubType,
normalBalance: this.determineNormalBalance(account.Classification),
currentBalance: account.CurrentBalance || 0,
active: account.Active,
parentId: account.ParentRef?.value,
level: account.FullyQualifiedName.split(':').length - 1,
fullyQualifiedName: account.FullyQualifiedName
};
}
/**
* Determines the normal balance based on account classification
*/
determineNormalBalance(classification) {
// In QuickBooks: Asset, Expense = Debit; Liability, Equity, Revenue = Credit
const debitClassifications = ['Asset', 'Expense'];
return debitClassifications.includes(classification) ? 'Debit' : 'Credit';
}
};
GetQuickBooksGLCodesAction = __decorate([
RegisterClass(BaseAction, 'GetQuickBooksGLCodesAction')
], GetQuickBooksGLCodesAction);
export { GetQuickBooksGLCodesAction };
//# sourceMappingURL=get-gl-codes.action.js.map