//The complete free software package may be dowloaded from http://tyaga.org/kit/.
<?php
$start
=microtime(); 

$create=0//these variables may be initiated by previous tally values 
$revert=0;
$inflow=0;
$outflow=0;
$cr_bal=0;
$db_bal=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 (isset(
$_POST['content'])) {
  
$src_content $_POST['content'];
}
else if (
$url) {
  
$src_content=file_get_contents($url);
}

$content explode("Prowl/version ",$src_content);

if (
count($content)==1) { //print_r($content); //echo " url: ". $url;
  
echo "File is not formatted as a Prowl record.";
}
else {
  
$content explode(" report for 2008:",$content[1]); //print_r($content);
  
$head explode("\n",$content[0]); //print_r($head);
  
$head array_filter($head);
  
$version_no current($head);
  list(
$model_indicator,$model_name) = explode(": ",next($head)); 
  list(
$main_icb,$native_units) = explode(" ",next($head)); 
  
$year="2008";
  
//echo "<br />icb,year,units: ". $main_icb ." ". $year ." ". $native_units;
  
  
$per_1 "2008-"//only eval within the same year is supported for now
  
$per_2 5//echo "<br />per1,per2: ". $per_1 ." ". $per_2;
  
  
include ("eval_functions_2.php");
  
  if (
$query_icb AND ($query_icb!=$main_icb OR $query_units!=$native_units)) {
    echo 
"The published Prowl report did not match the query parameters of: "$query_icb .", "$query_year .", "$query_units .".";
  }  
  else {
    
$content explode($year,end($content)); 
    
$status trim(array_shift($content)); //echo "<br />status: ". $status;
    
$temp_content = array();
      
    foreach (
$content as $key=>$value) {
      
$mm substr($value,1,2);
      
$dd substr($value,4,2); 
      
$new_key $mm $dd $key//echo "<br />mm,dd: ". $new_key .", ". $value .", ". $mm .", ". $dd;
      
$temp_content $temp_content+array($new_key=>$value);
    }
    
    
ksort($temp_content);
    
$content $temp_content;//print_r($content);
            
    
$i=0//to prevent infinite loop
    
$record current($content);
   
    while (
$record!=false AND $i<2000) {  
      
//list($record,$bal_line)=explode($record,"</a>");
      
$record explode(" ",$record); //echo "<br />". $i .": "; print_r($record);
      
$word current($record);
      
$date $year $word//echo "date=". $date ."<br />";
          
      
if (in_period($date,$per_1,$per_2)==true) { //echo "<br />date: ". $date;
        
$keyword next($record); 
            
        if (
$keyword=="reverse") {
          
$sign = -1;
          
$truncated_date next($record); //the original record date is not used in the evaluation
          
$keyword next($record);
        } else {
          
$sign 1;
        }
            
        if (
$keyword=="from") {      
          
$from_icb=next($record);
          
$to_wordnext($record);
                     
          if (
$to_word=="to") {
            
$to_icb=next($record);                
            
$amount=$sign*next($record);        
            
$units next($record);
              
            if (
valid_amt_units($amount,$units,$native_units)) {
              if (
substr($units,-1)!=".") {
                
$id next($record);
              }                        
              
              if (
$from_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 array_merge($flowratio_raw, array($date=>($inflow/$outflow)));};
                if (
$rev_budget!=0) {$pr_raw array_merge($pr_raw,array($date => 100*$inflow/($rev_budget)));};
                
$netbal_raw array_merge($netbal_raw, array($date=>$net_bal));                
              }
              else if (
$to_icb==$main_icb) { //txn was an inflow
                
$inflow $inflow $amount;//echo " units ". $units;
                
$net_bal=$net_bal+$amount;
                
//$db_bal=$db_bal-$amount; 
                
if ($outflow!=0) {$flowratio_raw array_merge($flowratio_raw, array($date=>($inflow/$outflow)));};
                if (
$rev_budget!=0) {$pr_raw array_merge($pr_raw,array($date => 100*$inflow/($rev_budget)));};
                
$netbal_raw array_merge($netbal_raw, array($date=>$net_bal));                            
              }              
            }
          }      
        }
        else if (
$keyword=="add" OR $keyword=="cut") { //this might have reporter also                
          
$amount=$sign*next($record);        
          
$units next($record);
              
          if (
valid_amt_units($amount,$units,$native_units)) {          
            if (
next($record)=="to" AND next($record)==$main_icb) { //echo "<br /> valid amt units ". $amout ." ". $units;
              
$param=explode(".",next($record)); 
                  
              if (
$param[0]=="unusedBudget") {            
                if (
$keyword=="add") { //echo "<br />add param ". $param;
                  
$create $create+$amount
                  
$rev_budget $rev_budget+$amount;
                  
$exp_budget $exp_budget+$amount;             
                  if (
$outflow!=0) {$flowratio_raw array_merge($flowratio_raw, array($date=>($inflow/$outflow)));};
                  if (
$rev_budget!=0) {$pr_raw array_merge($pr_raw,array($date => 100*$inflow/($rev_budget)));};
                  
$netbal_raw array_merge($netbal_raw, array($date=>$net_bal));              
                  
//$cr_bal=$cr_bal+$amount;
                  //$db_bal=$db_bal+$amount;
                
}
                else { 
//echo "<br />cut param ". $param;
                  
$revert $revert+$amount;
                  
$rev_budget $rev_budget-$amount;
                  
$exp_budget $exp_budget-$amount;
                  if (
$outflow!=0) {$flowratio_raw array_merge($flowratio_raw, array($date=>($inflow/$outflow)));};
                  if (
$rev_budget!=0) {$pr_raw array_merge($pr_raw,array($date => 100*$inflow/($rev_budget)));};
                  
$netbal_raw array_merge($netbal_raw, array($date=>$net_bal));
                      
//$cr_bal=$cr_bal-$amount;
                      //$db_bal=$db_bal-$amount;
                
}
              }
            }              
          }      
        }      
        else if (
$keyword=="set") {
          if (
next($record)==$main_icb) {
            
$param explode(".",next($record)); //echo "<br />set param ". $param;
                
            
if ($param[0]=="unusedBudget") {
              
$sub_param $param[1];
              
$to_word next($record);
              
$param_value next($record); //echo $param_value ." ". $tally['revenue-budget'];
              
$units trim(substr(next($record),0,-1)); //echo "<br />units= ". $units ."". $native_units;         
                
              
if (valid_amt_units($param_value,$units,$native_units)) {
                switch (
$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 (
$param[0]=="netBalance") {
              
$to_word next($record);
              
$param_value next($record); //echo $param_value ." ". $tally['revenue-budget'];
              
$units trim(substr(next($record),0,-1)); //echo "<br />units= ". $units ." ". $native_units;            
              
$net_bal $param_value;
            }
          }
        }   
      }                 
        
      
$record next($content);
      
$i=$i+1;    
      
$keyword"";
      
$opp_icb "";
      
$reporter "";  
      
$date "";
      
$from_icb "";
      
$to_icb "";
      
$amount "";
      
$units "";  
      
$neg_bal "";
      
$pos_bal "";  
    }    
  }


if (
function_exists(graph_array)) {
  if (!empty(
$flowratio_raw)) {
    
$result3 file_put_contents("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'); toggle_disp('form_note'); return false;">Click Here to View and Modify Evaluated Data</a></h4>
<table align="center">
<tr align="center">
<td>
<p id="form_note" style="display: none; text-align: center;">See what happens when period and amounts are changed, "ocaup" is changed to "cc", etc. </p><br />
<form method="POST" action="" align="center" id="src_content" style="display: none;" >
<textarea name="content" rows="20" cols="80">
<?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="flowratio_graph.php" align="center" width="<?php echo (2*$chart_x_pad+$graph_width?>" height="<?php echo (2*$chart_y_pad+$graph_height+10?>" type="image/svg+xml">
    </embed><br />
    period: 2008
    </p>
    </td>  
    <td width="350">
  <?php 
  
}
  
  if (
trim($model_name)=="ocaup") {
    if (!empty(
$pr_raw)) {
      
$result1 file_put_contents("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="pr_graph.php" align="center" width="<?php echo (2*$chart_x_pad+$graph_width?>" height="<?php echo (2*$chart_y_pad+$graph_height+10?>" type="image/svg+xml">
    </embed><br />
    period: 2008 
    </p>
  <?php
    
}
  }
    
  if (
trim($model_name)=="cc") {
    if (
$netbal_raw) {
      
$result2 file_put_contents("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="netbal_graph.php" align="center" width="<?php echo (2*$chart_x_pad+$graph_width?>" height="<?php echo (2*$chart_y_pad+$graph_height+10?>" type="image/svg+xml">
    </embed><br />
    period: 2008 
    </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()-$start) ."s"
?>
1