TWC - Help with script needed-Tobias, friends

63 views
Skip to first unread message

sklpns

unread,
May 5, 2017, 10:48:28 AM5/5/17
to TiddlyWiki
Hallo TW friends

a couple of years ago Tobias Beer kindly provided a TWC script
that creates a yearly income report table by months
(see the thread: https://groups.google.com/forum/#!searchin/tiddlywiki/sklpns$20tobias%7Csort:relevance/tiddlywiki/FB00JfzzY5w/xSoTnGdL5eIJ and the script at http://web.archive.org/web/20150129060432/http://scripts.tiddlyspace.com:80/#%5B%5BS%3A%20yearly%20income%20report%5D%5D)

What it does is collect data from tagged "expense" tiddlers and outcomes a budget table with budget information
per year/month.

I'm looking for a way to modify the script so that it outputs the same information but for all the years (not one at a time-
something like












Year Month Expenses Revenues Balance Expense Total Revenue Total Balance Total
2014 Jan 0 0 0 0 0 0
2014 Feb 0 0 0 0 0 0
2014 Mar 0 0 0 0 0 0
2014 Apr 0 0 0 0 0 0
2014 May 0 0 0 0 0 0
2014 Jun 0 0 0 0 0 0
2014 Jul 0 0 0 0 0 0
2014 Aug 0 0 0 0 0 0
2014 Sep 0 0 0 0 0 0
2014 Okt 0 0 0 0 0 0
2014 Nov 200 500 300 200 500 300
2014 Dec 0 0 0 200 500 300
2015 Jan





2015 Feb





2015 Mar





2015 Apr





2015 May





2015 Jun





2015 Jul





2015 Aug





2015 Sep





2015 Okt





2015 Nov





2015 Dec













and so on

Thanks in advance

sklpns














































































































































































































































































 






whatever

unread,
May 5, 2017, 12:32:24 PM5/5/17
to TiddlyWiki
Hey!
Would you mind reposting the script here? Can't find it at the links. Otherwise, it would be useful to know the structure of your data.
w

sklpns

unread,
May 5, 2017, 2:28:34 PM5/5/17
to TiddlyWiki
Hey whatever, thanks for your reply. Here's the script as created by Tobias


<script>
(function($){

var
/*the tag for receipt tiddlers*/
tag = "receipt",
/*the names of the data fields to use*/
data = {
"revenues":"revenues",
"expenses":"expenses",
"year":"year",
"month":"month"
},
/*the month values being used*/
months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Okt","Nov","Dec"],
/*the table header*/
header = "|Month|Expenses|Revenues|Balance|Expense Total|Revenue Total|Balance Total|h\n",

/*when transcluded, use <<tiddler YearlyIncome with: 2015>>*/
get = "$1" == "$"+"1" ? null : parseInt("$1"),
y,years=[];

window.YearlyIncomeReport = function(){
var
m, out,
$sel = $(this),
$table = $sel.next(),
year = get ? get : $sel.val(),
totals = {
expenses:[0,0,0,0,0,0,0,0,0,0,0,0],
revenues:[0,0,0,0,0,0,0,0,0,0,0,0]
},
sum = function(arr,num){
var m, result = 0;
for(m=0; m<=num; m++){
result += arr[m];
}
return result;
};

if(get){
$table = $("<div>");
$(place).append($table);
}

store.forEachTiddler( function(title, tiddler) {
if(tiddler.tags.contains(tag)){
var
y = tiddler.data(data.year),
m = tiddler.data(data.month);

m = m ? months.indexOf(m) : 0;

if(0 <= m && y && y == year){
totals.expenses[m] += tiddler.data(data.expenses) || 0;
totals.revenues[m] += tiddler.data(data.revenues) || 0;
}
}
});

out = header;
for(m = 0; m< months.length; m++){
var
e = totals.expenses[m],
r = totals.revenues[m],
te = sum(totals.expenses,m),
tr = sum(totals.revenues,m);
out +=
"|" + months[m] +
"|" + e +
"|" + r +
"|" + (r - e) +
"|" + te +
"|" + tr +
"|" + (tr - te) +
"|\n";
};

$table.empty();
wikify(out,$table[0]);
}

if(get){
window.YearlyIncomeReport();
} else {
$sel = $("<select/>").change(window.YearlyIncomeReport);
$(place).append($sel);

store.forEachTiddler( function(title, tiddler) {
if(tiddler.tags.contains(tag)){
y = tiddler.data(data.year);
if(y){
years.pushUnique(y);
}
}
});
years.sort();

for(y=0;y<years.length;y++){
$sel.prepend($('<option></option>').val(years[y]).html(years[y]));
}

$(place).append("<div/>");
$sel.trigger("change");
}
})(jQuery);
</script>

and here it is slightly tweaked by me, as I actually use it

<script>
(function($){

var
/*the tag for receipt tiddlers*/

tag1 = "expense",

tag2 = "income",

/*the names of the data fields to use*/
data = {
"revenues":"incomeAmount",
"expenses":"expenseAmount",
"year":"eY",
"month":"eM"
},
/*the month values being used*/
months = ["01","02","03","04","05","06","07","08","09","10","11","12"],
/*the table header*/
header = "|borderless|k\n|!Month|!Expenses|!Revenues|!Balance|!Expense Total|!Revenue Total|!Balance Total|h\n",

/*when transcluded, use <<tiddler YearlyIncome with: 2015>>*/
get = "$1" == "$"+"1" ? null : parseInt("$1"),
y,years=[];

window.YearlyIncomeReport = function(){
var
m, out,
$sel = $(this),
$table = $sel.next(),
year = get ? get : $sel.val(),
totals = {
expenses:[0,0,0,0,0,0,0,0,0,0,0,0],
revenues:[0,0,0,0,0,0,0,0,0,0,0,0]
},
sum = function(arr,num){
var m, result = 0;
for(m=0; m<=num; m++){
result += arr[m];
}
return result;
};

if(get){
$table = $("<div>");
$(place).append($table);
}

store.forEachTiddler( function(title, tiddler) {
if (tiddler.tags.containsAny([tag1,tag2])) {

var
y = tiddler.data(data.year),
m = tiddler.data(data.month);

m = m ? months.indexOf(m) : 0;

if(0 <= m && y && y == year){
totals.expenses[m] += tiddler.data(data.expenses)*1 || 0;
totals.revenues[m] += tiddler.data(data.revenues)*1 || 0;
}
}
});

out = header;
for(m = 0; m< months.length; m++){
var
e = totals.expenses[m],
r = totals.revenues[m],
te = sum(totals.expenses,m),
tr = sum(totals.revenues,m);
out +=
"|" + months[m] +
"|" + e +
"|" + r +
"|" + (r - e) +
"|" + te +
"|" + tr +
"|" + (tr - te) +
"|\n";
};

$table.empty();
wikify(out,$table[0]);
}

if(get){
window.YearlyIncomeReport();
} else {
$sel = $("<select/>").change(window.YearlyIncomeReport);
$(place).append($sel);

store.forEachTiddler( function(title, tiddler) {

if (tiddler.tags.containsAny([tag1,tag2])){
y = tiddler.data(data.year);
if(y){
years.pushUnique(y);
}
}
});
years.sort();

for(y=0;y<years.length;y++){
$sel.prepend($('<option></option>').val(years[y]).html(years[y]));
}

$(place).append("<div/>");
$sel.trigger("change");
}
})(jQuery);
</script>

I'm on my phone right now but please let me know if more info or even an MTC is required.

Thanks again,
sklpns

Reply all
Reply to author
Forward
0 new messages