//The complete free software package may be dowloaded from http://tyaga.org/kit/.
<?php

include_once("parser.php");
include_once(
"eval_functions.php");

$start=microtime(true); 
$create=0//these variables may be initiated by previous tally values 
$revert=0;
$inflow=0;
$outflow=0;
$cr_bal=0;
$db_bal=0;
$rev_budget 0;
$net_bal=0;
$pr_raw=array();
$flowratio_raw=array();
$netbal_raw=array();

//chart, graph constants
$chart_x_pad=150;
$chart_y_pad=20;
$graph_height=300;
$graph_width=300;

if (!
$url) {
  
$url "reports/tyagaorg_2008_hour.txt";
}

if (isset(
$_POST['content'])) {
  
$src_content $_POST['content'];
}
else if (
$url) {
  
$src_content=file_get_contents($url);
}

list(
$message,$version_no,$model_name,$main_icb,$year,$native_units,$status,$content) = prowl_content($src_content,$query_icb,$query_units,$query_year); //echo "status, model_name: ". $status .", ". $model_name ."<br />";

if ($message) {
  echo 
$message;
}
else {
  
//$per_1 = "2008"; //only eval within the same year is supported for now
  //$per_2 = 5; //echo "<br />per1,per2: ". $per_1 ." ". $per_2;
  
$record_array content_parse($content,$main_icb,$native_units,$year,4); //$record_array is array of arrays   
  
$record_array record_sort($record_array); //print_r($record_array); //to order the data value by time on x-scale               
  
$i=0//to track the number of processed records in while loop
  
$record current($record_array);
   
  while (
$record AND $i<2000) {   
    
$keyword $record['keyword'];
    
$amount $record['sign']*$record['amount'];
    
$date $record['unixtime']; //echo $date ." ";
          
    
if (!$record['message']) {    
      if (
$keyword=="from") {      
        if (
$record['from_icb']==$main_icb AND $record['to_icb']!=$main_icb) {  //txn was an outflow            
          
$outflow $outflow $amount;//echo " units ". $units;
          
$net_bal=$net_bal-$amount;
          
//$cr_bal=$cr_bal-$amount;
          
if ($outflow!=0) {$flowratio_raw[$date] = $inflow/$outflow;}
          if (
$rev_budget!=0) {$pr_raw[$date] = 100*$inflow/$rev_budget;}
          
$netbal_raw[$date] = $net_bal;                
        }
        else if (
$record['from_icb']!=$main_icb AND $record['to_icb']==$main_icb) { 
          
$inflow $inflow $amount;//echo " units ". $units;
          
$net_bal=$net_bal+$amount;
          
//$db_bal=$db_bal-$amount; 
          
if ($outflow!=0) {$flowratio_raw[$date] = $inflow/$outflow;}
          if (
$rev_budget!=0) {$pr_raw[$date] = 100*$inflow/$rev_budget;}
          
$netbal_raw[$date] = $net_bal;                            
        }              
      }
      else if (
$keyword=="add") { //this might have reporter also       
        
if ($record['param']=="unusedBudget.") { //echo "<br />amount: ". $amount ." ";
          
$create $create+$amount;  
          
$rev_budget $rev_budget+$amount;
          
$exp_budget $exp_budget+$amount;             
          if (
$outflow!=0) {$flowratio_raw[$date] = $inflow/$outflow;}
          if (
$rev_budget!=0) {$pr_raw[$date] = 100*$inflow/$rev_budget;}
          
$netbal_raw[$date] = $net_bal;              
          
//$cr_bal=$cr_bal+$amount;
          //$db_bal=$db_bal+$amount;
        
}
      }
      else if (
$keyword=="cut") {                
        if (
$record['param']=="unusedBudget.") {
          
$revert $revert+$amount;
          
$rev_budget $rev_budget-$amount;
          
$exp_budget $exp_budget-$amount;
          if (
$outflow!=0) {$flowratio_raw[$date] = $inflow/$outflow;}
          if (
$rev_budget!=0) {$pr_raw[$date] = 100*$inflow/$rev_budget;}
          
$netbal_raw[$date] = $net_bal;
          
//$cr_bal=$cr_bal-$amount;
          //$db_bal=$db_bal-$amount;
        
}
      }  
      else if (
$keyword=="set") { //echo $record['param'] ." ". $record['sub_param']; 
        
$param_value $record['param_value'];
        
        if (
$record['param']=="unusedBudget") {
          switch (
$record['sub_param']) {              
            case 
".revenueBal"$db_bal=$param_value$start_db=$param_value$rev_budget=$param_value; break;
                  
            case 
".expenseBal"$cr_bal=$param_value$start_cr=$param_value$exp_budget=$param_value; break;          
          }            
        }   
        else if (
$record['param']=="netBalance") {
          
$net_bal $param_value;
        }
      }       
    } 
//else echo $record['message'];     
  
    
$record next($record_array);
    
$i=$i+1;  
    
//$date= "";
    //$neg_bal = "";
    //$pos_bal = "";  
  
}


//The rest of the code are for presenting the evaluation results
if (!empty($flowratio_raw)) {    
  
$result3 file_put_contents("graphs/flowratio_graph.svg",graph_array($flowratio_raw,$graph_width,$graph_height,$chart_x_pad,$chart_y_pad,0,3,1));
    
?>
  <h4>Evaluation Results for <?php echo $url ?></h4>
  <h4>Requires SVG-capable browser*</h4>
  
  <h4><a href="" onclick="toggle_disp('src_content'); return false;">Click here to View and Modify Source Data</a></h4>
  <table align="center">
  <tr align="center">
  <td>
  <form method="POST" action="" align="center" id="src_content" style="display: none;" >
  <p>See what happens when period and amounts are changed, "ocaup" is changed to "cc", etc.</p>
  <textarea name="content" rows="20" cols="100">
  <?php echo $src_content?>
  </textarea><br />
  <input type="submit" name="submit" value="Submit">
  </form>
  </td>
  </tr>
  </table>  
  
  <table align="center">
  <tr align="center">
    <td width="350">
    <p style="text-align: center"><b>Inflow/Outflow Ratio</b><br />
    <embed name="flow_ratio" src="graphs/flowratio_graph.svg" align="center" width="<?php echo (2*$chart_x_pad+$graph_width?>" height="<?php echo (2*$chart_y_pad+$graph_height+10?>">
    </embed><br />
    period: <?php echo $year ?>
    </p>
    </td>  
    <td width="350">
  <?php 
}
  
if (
trim($model_name)=="ocaup") { //echo " This accounting model requires a valid revenue budget ";
  
if (!empty($pr_raw)) {      //print_r($pr_raw);
      
$result1 file_put_contents("graphs/pr_graph.svg",graph_array($pr_raw,$graph_width,$graph_height,$chart_x_pad,$chart_y_pad,0,100,1));
    
    
?>
    <p style="text-align: center"><b>Percent Recovery</b><br />
    <embed name="npr" src="graphs/pr_graph.svg" align="center" width="<?php echo (2*$chart_x_pad+$graph_width?>" height="<?php echo (2*$chart_y_pad+$graph_height+10?>">
    </embed><br />
    period: <?php echo $year ?> 
    </p>
  <?php
  
}
}
    
if (
trim($model_name)=="cc") {
  if (!empty(
$netbal_raw)) {      
    
$result2 file_put_contents("graphs/netbal_graph.svg",graph_array($netbal_raw,$graph_width,$graph_height,$chart_x_pad,$chart_y_pad));
    
?>  
    <p style="text-align: center"><b>Net Balance</b><br />
    <embed name="npr" src="graphs/netbal_graph.svg" align="center" width="<?php echo (2*$chart_x_pad+$graph_width?>" height="<?php echo (2*$chart_y_pad+$graph_height+10?>">
    </embed><br />
    period: <?php echo $year ?> 
    </p>
    <?php
  
}
}
  
?>  
    </td>    
  </table>
  <br />
  <br />
 
  <h5 align="center">*Raw data in comma-separated value format provided below (see source view); Graphing tool is under development, investigating <a href="http://code.google.com/p/flot/">flot</a> or <a href="http://www.ibm.com/developerworks/xml/library/x-svggrph/">similar</a></h5>
  <?php


echo "<br /><br /><b>Flow Ratio calculated data:</b> ";
foreach(
$flowratio_raw as $key=>$value) {echo $key .","$value ."\n";}

if (
trim($model_name)=="ocaup") {
  echo 
"<br /><br /><b>Percent Recovery calculated data:</b> ";
  foreach(
$pr_raw as $key=>$value) {
    echo 
$key .","$value ."\n";
  }
}
else if (
trim($model_name)=="cc") {
  echo 
"<br /><br /><b>Net Balance calculated data:</b> ";
  foreach(
$netbal_raw as $key=>$value) {
    echo 
$key .","$value ."\n";
  }
}

echo 
"<br />total time: ". (microtime(true)-$start) ."s"
?>
1