/* Kmymoney basic viewer - tfe < tfe@ptain.info> */ // Loadding google visualizacion apis /* google.load('visualization', '1', {packages: ['annotatedtimeline']}); google.load('visualization', '1', {packages: ['areachart']}); google.load('visualization', '1', {packages: ['columnchart']}); google.load('visualization', '1', {packages: ['linechart']}); */ /* Main */ var kmymoney = { // XML Vars file : 'Budget.xml', xml : false, xmlDoc : false, prefix : '/KMYMONEY-FILE', // Google chart types graph_types : [ 'AnnotatedTimeLine', 'AreaChart', 'ColumnChart', 'LineChart' ], chart_width : '90%', // 90% of screen width chart_height : '400px', // Data created_date : false, // Default filter data startdate : '', enddate : '', step_trans : 50, // HTML Links layout : false, // Init: get xml file init : function(layout) { // Init default values this.layout = $(layout); this.layout.update(''); this.startdate='20001230'; this.enddate=this.date_format(new Date()); // Loading advise after basic menu this.layout.appendChild(new Element('p').update('Loading Kmymoney XML File '+this.file+'. Please wait...')); // Request XML Kmymoney file this.xml= new Ajax.Request(this.file,{ onSuccess: function() { // Not using "this" (?) , bug?? setTimeout(function() { kmymoney.firstanalyse();},100); }, onFailure : function() { setTimeout(function() { kmymoney.failure();},100); } }); }, // On load xml failure failure : function() { $(this.layout).update(''); if(this.xml.transport.status=='404') { $(this.layout).appendChild(new Element('p', { 'class':'error'}).update('The file '+this.file+' cannot be found. Please check the file and try again later.')); } else { $(this.layout).appendChild(new Element('p', { 'class':'error'}).update('An error occured downloading the file '+this.file+' : error '+this.xml.transport.status)); } var p = new Element('p'); p.appendChild(new Element('a', { href:'#', onclick:'kmymoney.init(\'loading\'); return false;'}).update('Retry')); $(this.layout).appendChild(p); }, // XML FOrmat to user readable format date_format_user : function(dateformat) { return dateformat.replace(/(\d{4})(\d{2})(\d{2})/,'$3/$2/$1'); }, // XML user format to xml format undate_format_user : function(dateformat) { return dateformat.replace(/(\d{2})\/(\d{2})\/(\d{4})/,'$3$2$1'); }, // date object to xml format date_format : function(dateformat) { var year = dateformat.getYear()+1900; var month = dateformat.getMonth()+1; var day = dateformat.getDate(); if(month<10) { month='0'+month; } if(day<10) { day='0'+day; } return year+''+month+''+day; }, // xml format to date object undate_format : function(date_format) { var reg= /(\d{4})(\d{2})(\d{2})/i; var date_data = reg.exec(date_format); if(!date_data) { return false; } return new Date(date_data[1], date_data[2]-1, date_data[3]); }, // GetXPath // Cross browser xpath : function(path) { if(this.xmlDoc.evaluate) { return this.xmlDoc.evaluate( this.prefix+path, this.xmlDoc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE , null ); } else { return this.xmlDoc.selectNodes(this.prefix+path); } }, // XPath result item // Cross browser xpathItem : function(elt,i) { if(elt.snapshotLength) { return elt.snapshotItem(i); } return elt[i]; }, // XPath result length // Cross browser xpathLength : function(elt) { if(elt.snapshotLength) { return elt.snapshotLength } return elt.length; }, // First analysis // Used to correct date-formats and create basic elements // Generate menu firstanalyse : function() { // Deleting loading advise $(this.layout).update(''); $(this.layout).appendChild(new Element('p').update('XML Kmymoney file '+this.file+' loaded.')); this.xmlDoc= this.xml.transport.responseXML; // Correcting date format (numerics) var trans = this.xpath('/TRANSACTIONS/TRANSACTION'); for(var i=0;i0) { for(i=0; i0) { p.appendChild(new Element('a', { id:'next_trans', href:'#', onclick:'this.parentNode.remove();kmymoney.mouvements(\''+account_id+'\',\''+(endid+1)+'\'); return false'}).update('Display more')); p.appendChild(document.createTextNode(' - ')); $(this.layout).appendChild(p); } p.appendChild(new Element('a', {href:'#', onclick:'kmymoney.mouvements_list()'}).update('Go back')); $(this.layout).appendChild(p); var cats_name= new Hash(); var payee_name= new Hash(); /* Transaction loops */ for(i=rowstart; i>rowend; i--) { var trans = this.xpathItem(data,i).parentNode.parentNode; var splits = this.xpathItem(data,i).parentNode; var current = this.xpathItem(data,i).getAttribute('shares'); var current=current.replace(/\/10(0?)$/,'$1'); var categorie=''; /* Splits loop */ for(var j=0;j0 ? 'ok' : 'ko'; td= new Element('td', { 'class':className+' euro'}).update(num+'€'); tr.appendChild(td); num = (parseInt(this.xpathItem(data,i).getAttribute('total'))/100).toFixed(2); var className = num>0 ? 'ok_total' : 'ko_total'; td= new Element('td', { 'class':className+' euro'}).update(num+'€'); tr.appendChild(td); td= new Element('td').update(this.xpathItem(data,i).getAttribute('memo')); tr.appendChild(td); table.appendChild(tr); } }, // Chart part // Calculats sums for each categories calculate_sums : function() { /* Reset category totals */ var xml_trans = this.xpath('/ACCOUNTS/ACCOUNT'); for(i=0; i'+startdate+' and @postdate<'+enddate+']/SPLITS/SPLIT'); var xml_split = this.xpath('/TRANSACTIONS/TRANSACTION/SPLITS/SPLIT'); var reg = /(.*)\/(.*)/; /* Loop - calculate totals */ for(i=0; i0) { var ul = new Element('ul'); for(var i=0;idate_end) { return false; } if(categories.length==0) { html_item.appendChild(new Element('p').update('No data available')); return false; } var data = new google.visualization.DataTable(); data.addColumn('date', 'Date'); if(display_subcats) { data.addColumn('number', 'Total'); } // Sub query for categories var xpath_sub= new Array(); if(categories.length==0) { xpath_sub.push('@account=0'); } else { for(var i=0;i