Dans cet article
Introduction
This page explains how to update the file properties, accounting dates with Banana Accounting Javascript Extension using DocumentChange API .
Before start, to understand part of the code it's useful to read all the documentation about the DocumentChange API functionalities .
It is very important to have all the dates that match in the accounting document, otherwise the script may not work properly.
This script allows you to change the file dates with the current year. for setting the current year has been created a Date type object, we use his method getFullYear() to recover the year of the current date.
The current date value can be changed in the in the method: initParam();.
In an Accounting file we can find three main places with references to the dates:
- OpeningDate/ClosureDate, which indicate the opening and closing dates of the accounts
- HeaderLeft/HeaderRight, that are the two opposite side headers of a page, where a user could decide to put the dates for example for print a report
- Date, referring to the date field in the table
the internal format is always composed as follows 'YYYYMMDD'.
References
- DocumentChange API
- Script's source: ch.banana.apps.documentchange.dates.js
- Build you first Extension
Function Exec()
The exec() function is the main function, which is called when the extension is executed. This function does the following:
- creates the initial command structure part which is the same for all documents
- calls the methods used to define the rest of the structure and parameters of the documents we want to change
- for every function called, checks that the parameters are not empty
- returns an object containing the whole commands structure
function exec(inData, options) {
var param = initParam();
Banana.console.debug(JSON.stringify(param));
var ischange = false;
var documentChange = { "format": "documentChange", "error": "", "data": [] };
if (param.newaccountingopeningdate.length > 0) {
var jsonDoc = setfileInfo("AccountingDataBase", "OpeningDate", param.newaccountingopeningdate);
documentChange["data"].push(jsonDoc);
ischange = true;
}
if (param.newaccountingclosuredate.length > 0) {
jsonDoc = setfileInfo("AccountingDataBase", "ClosureDate", param.newaccountingclosuredate);
documentChange["data"].push(jsonDoc);
ischange = true;
}
if (param.newaccountingheaderleft.length > 0) {
jsonDoc = setfileInfo("Base", "HeaderLeft", param.newaccountingheaderleft);
documentChange["data"].push(jsonDoc);
ischange = true;
}
if (param.newaccountingheaderright.length > 0) {
jsonDoc = setfileInfo("Base", "HeaderRight", param.newaccountingheaderright);
documentChange["data"].push(jsonDoc);
ischange = true;
}
jsonDoc = setTransactionsDate(param);
if (typeof(jsonDoc) == "object") {
documentChange["data"].push(jsonDoc);
ischange = true;
}
jsonDoc = setBudgetDate(param);
if (typeof(jsonDoc) == "object") {
documentChange["data"].push(jsonDoc);
ischange = true;
}
if (ischange) {
return documentChange;
}
}
Function initParam()
- This method initializes the parameters, which are dates taken from the file informations, and the crurrent date.
- checks if there is a date in the headers taking as reference the opening date of the file, if there is it updates it with the current one
- check that the year of opening and closing have the current year, if not, will update them with the current year
- return an object with al the parameters wee need.
function initParam() {
var param = {};
param.differenceyear = 0;
param.accountingyear = "";
param.newaccountingyear = new Date().getFullYear();
param.newaccountingopeningdate = "";
param.newaccountingclosuredate = "";
param.newaccountingheaderleft = "";
param.newaccountingheaderright = "";
var OpeningDate = Banana.document.info("AccountingDataBase", "OpeningDate");
if (OpeningDate && OpeningDate.length > 4) {
param.accountingyear = OpeningDate.toString().substr(0, 4);
var Headerleft = Banana.document.info("Base", "HeaderLeft");
if (Headerleft && Headerleft.indexOf(param.accountingyear) >= 0) {
Headerleft = Headerleft.replace(param.accountingyear, param.newaccountingyear);
param.newaccountingheaderleft = Headerleft;
}
var Headerright = Banana.document.info("Base", "HeaderRight");
if (Headerright && Headerright.indexOf(param.accountingyear) >= 0) {
Headerright = Headerright.replace(param.accountingyear, param.newaccountingyear);
param.newaccountingheaderright = Headerright;
}
var currentYearint = parseInt(param.newaccountingyear);
var fileYearint = parseInt(param.accountingyear);
param.differenceyear = Banana.SDecimal.subtract(currentYearint, fileYearint);
if (parseInt(param.differenceyear) != 0) {
param.newaccountingopeningdate = param.newaccountingyear.toString() + OpeningDate.toString().substr(4);
param.newaccountingopeningdate = param.newaccountingopeningdate.replace(/-/g, "");
var closureDate = Banana.document.info("AccountingDataBase", "ClosureDate");
if (closureDate && closureDate.length > 4) {
var year = closureDate.toString().substr(0, 4);
var newyear = parseInt(year) + parseInt(param.differenceyear);
param.newaccountingclosuredate = newyear.toString() + closureDate.toString().substr(4);
param.newaccountingclosuredate = param.newaccountingclosuredate.replace(/-/g, "");
}
}
}
return param;
}
Function changeYearInDate()
Calculate the new date for the opening date and the closure date taking as parameters the current opening and closure date and the difference between the current effective date and the current opening date reported in the file information.
conditions:
the opening and the closure date should not be empty.
return the date changed.
function changeYearInDate(differenceyear, OpeningClosureDate) {
if (OpeningClosureDate && OpeningClosureDate.length > 4) {
var Year = OpeningClosureDate.toString().substr(0, 4);
var newyear = Banana.SDecimal.add(parseInt(Year), parseInt(differenceyear));
var changedDate = newyear.toString() + OpeningClosureDate.toString().substr(4);
changedDate = changedDate.replace(/-/g, "");
return changedDate;
}
return "";
}
Function getNewRowDate()
Calculate the new date for the table rows taking as parameters the current date in the table and the difference between the current effective date and the current opening date reported in the file information.
Conditions:
- the current date must be longer than four characters, and should exist
- it must be a difference between the current effective date and the current opening date reported in the file information
Returns the new date.
function getNewRowDate(currentDate, param) {
if (!currentDate || currentDate.length < 4)
return "";
if (param.differenceyear == 0)
return "";
var currentyear = currentDate.substr(0, 4);
var newyear = parseInt(currentyear) + parseInt(param.differenceyear);
var newDate = newyear.toString() + currentDate.substr(4);
return newDate;
}
function setfileInfo()
- this function set the structure of the document wich modify the dates into the files information table
- requires three parameters which represent the identification values of the fields who can containing dates in the file information table
- returns an object with the document structure
function setfileInfo(key1, key2, value) {
//row operation
var row = {};
row.operation = {};
row.operation.name = "modify";
row.fields = {};
row.fields["SectionXml"] = key1;
row.fields["IdXml"] = key2;
row.fields["ValueXml"] = value;
var rows = [];
rows.push(row);
//table
var dataUnitTransactions = {};
dataUnitTransactions.nameXml = "FileInfo";
dataUnitTransactions.data = {};
dataUnitTransactions.data.rowLists = [];
dataUnitTransactions.data.rowLists.push({ "rows": rows });
//document
var jsonDoc = initDocument();
jsonDoc.document.dataUnits.push(dataUnitTransactions);
return jsonDoc;
}
functions setTransactionsDate()/setBudgetDate()
these two functions are very similar, they have the same code and the same document structure, which modifies the rows of the record tables, the only difference is that they are pointing to two different kind of tables:
- transactions
- budget
both call the function getNewRowDate(), wich change the current date founded in the row with the effectvie current one.
Returns an object with the document structure.
setTransactionsDate()
function setTransactionsDate(param) {
var table = Banana.document.table("Transactions");
if (!table) {
return;
}
var rows = [];
for (var i = 0; i < table.rowCount; i++) {
var tRow = table.row(i)
var TransDate = tRow.value('Date');
TransDate = getNewRowDate(TransDate, param);
if (TransDate.length <= 0) {
continue;
}
//row operation
var row = {};
row.operation = {};
row.operation.name = "modify";
row.operation.sequence = i.toString();
row.fields = {};
row.fields["Date"] = TransDate;
rows.push(row);
}
if (rows.length <= 0)
return;
//table
var dataUnitTransactions = {};
dataUnitTransactions.nameXml = "Transactions";
dataUnitTransactions.data = {};
dataUnitTransactions.data.rowLists = [];
dataUnitTransactions.data.rowLists.push({ "rows": rows });
//document
var jsonDoc = initDocument();
jsonDoc.document.dataUnits.push(dataUnitTransactions);
return jsonDoc;
}
setBudgetDate()
function setBudgetDate(param) {
var table = Banana.document.table("Budget");
if (!table) {
return;
}
var rows = [];
for (var i = 0; i < table.rowCount; i++) {
var tRow1 = table.row(i)
var TransDate = tRow1.value('Date');
var TransDateEnd = tRow1.value('DateEnd');
TransDate = getNewRowDate(TransDate, param);
TransDateEnd = getNewRowDate(TransDateEnd, param);
if (TransDate.length <= 0) {
continue;
}
//row operation
var row = {};
row.operation = {};
row.operation.name = "modify";
row.operation.sequence = i.toString();
row.fields = {};
row.fields["Date"] = TransDate;
row.fields["DateEnd"] = TransDateEnd;
rows.push(row);
}
if (rows.length <= 0)
return;
//table
var dataUnitTransactions = {};
dataUnitTransactions.nameXml = "Budget";
dataUnitTransactions.data = {};
dataUnitTransactions.data.rowLists = [];
dataUnitTransactions.data.rowLists.push({ "rows": rows });
//document
var jsonDoc = initDocument();
jsonDoc.document.dataUnits.push(dataUnitTransactions);
return jsonDoc;
}