Application scripts

In this article

Application scripts are used to extend the program's functionalities, like verify the accounting or build custom reports. This functionality is available only in Banana Premium or Banana Experimental.

With the dialog "Manage Scripts" from the "Account1" menu, you can install, modify or remove scripts. The script can be located everywhere, usually they are in the same folder as the accounting file.

 

Example to check for duplicates transactions (download script with test accounting).

Note: Instead of showing a message, it had could be possible to list the rows (like the command extract) in a separated table with the command Banana.Document.Table.extractRows().

// This script verify if there are duplicate transactions in the book accounting.
// Duplicate transactions have the same Doc, DateDocument, and Customer's Account
//
// @id = ch.banana.scripts.check.duplicate.js
// @version = 1.1
// @pubdate = 2014-06-10
// @publisher = Banana.ch SA
// @description = Find duplicate transactions
// @task = app.command
// @outputformat = none
// @inputdatasource = none
// @timeout = -1
//

function exec(string) {
    if (!Banana.document)
        return;
    Banana.document.clearMessages();

    var transactionsMap = new Array();
    var tableTransactions = Banana.document.table('Transactions');

    var progressBar = Banana.application.progressBar;
    progressBar.start(tableTransactions.rowCount);

    // Pass every transaction and save in the map
    for (rowNr = 0; rowNr < tableTransactions.rowCount; rowNr++) {

        if (!progressBar.step(1))
            break;

        // Verify if the row is to be checked
        var trDocNr = tableTransactions.value(rowNr, 'Doc');
        if (!trDocNr)
            continue;
        var trDocDate = tableTransactions.value(rowNr, 'DateDocument');
        if (!trDocDate)
            continue;
        var trCustomerAccount = null;
        var trCreditAccount = tableTransactions.value(rowNr, 'AccountCredit');
        var trDebitAccount = tableTransactions.value(rowNr, 'AccountDebit');
        if (isCustormerAccount(trCreditAccount))
            trCustomerAccount = trCreditAccount;
        else if (isCustormerAccount(trDebitAccount))
            trCustomerAccount = trDebitAccount;
        if (!trCustomerAccount)
            continue;

        // Insert the combination in map[Doc][DateDocument][CustomerAccount][rowNr]
        if (!transactionsMap[trDocNr])
            transactionsMap[trDocNr] = new Array();
        if (!transactionsMap[trDocNr][trDocDate])
            transactionsMap[trDocNr][trDocDate] = new Array();
        if (!transactionsMap[trDocNr][trDocDate][trCustomerAccount])
            transactionsMap[trDocNr][trDocDate][trCustomerAccount] = new Array();
        transactionsMap[trDocNr][trDocDate][trCustomerAccount].push(rowNr);
    }

    // For each combination of [Doc][DateDocument][CustomerAccount], if there are more than two rows, show a message
    for (var doc in transactionsMap) {
        for (var date in transactionsMap[doc]) {
            for (var customer in transactionsMap[doc][date]) {
                if (transactionsMap[doc][date][customer].length > 1) {
                    for (var i = 0; i < transactionsMap[doc][date][customer].length; i++) {
                        rowNr = transactionsMap[doc][date][customer][i];
                        duplKeys = doc + "/" + date + "/" + customer;
                        tableTransactions.addMessage("Registrazione duplicata trovata (" + duplKeys + ")", rowNr, "Doc");
                    }
                }
            }
        }
    }

    progressBar.finish();
}

/**
* Return true if the account string is a custormer's account
* Actually an'account is a customers's account if it start with "100." or "200."
*/
function isCustormerAccount(string) {
    if (!string) {
        return false;
    }
    if (string.substring(0,4) == "100." || string.substring(0,4) == "200.") {
        return true;
    }
    // We could also just check if the account is added to the groups 110A or 220A
    return false;
}

 

Example to extract rows with missing vat code (download script with test accounting).

// @id = ch.banana.filter.extract.missingvat
// @version = 1.1
// @pubdate = 2014-06-10
// @publisher = Banana.ch SA
// @description = Missing VAT
// @task = app.command
// @outputformat = none
// @inputdatasource = none

/**
 * This example extract transactions whitout VAT and an income or expense account
 */
function exec( inData) {
    var tableTransactions = Banana.document.table('Transactions');
    if ( tableTransactions) {
        tableTransactions.extractRows( extractRowsWithoutVat, 'Transactions without VAT');
    }
}

/** Return true if the transaction is missing the vat code. */
function extractRowsWithoutVat( row, i, table) {
    var debitAccount = row.value('AccountDebit');
    var creditAccount = row.value('AccountCredit');

    if (accountShouldHaveVatCode(debitAccount) || accountShouldHaveVatCode(creditAccount)) {
        var vatCode = row.value('VatCode');    
        if (typeof vatCode === 'undefined' || vatCode === '')
            return true;
    }
    
    return false;
}

var tableAccounts = Banana.document.table('Accounts');

/** Return true if the account should have a vat code. */
function accountShouldHaveVatCode(account) {
    if (typeof account === 'undefined' || account === '')
        return;
    if (tableAccounts) {
        var accountRow = tableAccounts.findRowByValue('Account', account);
        if (accountRow) {
            var bclass = accountRow.value('BClass');
            if (bclass == '3' || bclass == '4')
                return true;
        }                
    }    
    return false;
}

 

Questa documentazione è superata

Ricerca nella documentazione più recente

Manca la caratteristica che stavi cercando? Prova la nuova versione! Banana Accounting 9