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

function prowl_content($content,$query_icb="",$query_units="",$query_year="") {
  
$content str_replace(array("<br />","<br>","<p>","\r"),"\n",$content);
  
$content explode("Prowl/version ",$content);
  
  if (
count($content)==1) { //echo "content: "; print_r($content);
    
$message "File is not formatted as a Prowl record.";
  }
  else {
    
$content explode("\n",$content[1]); 
    
$content array_filter($content);
    
$version_no current($content);
    list(
$model_indicator,$model_name) = explode(": ",next($content));
    list(
$main_icb,$native_units,$report_word,$for_word,$year,$status) = explode(" ",next($content)); //echo "<br />yr: ". $year ."<br />";
    
$year rtrim($year,":");
    
    if (
$query_icb) { 
      if (
$query_icb!=$main_icb OR $query_year!=$year OR $query_units!=$native_units) {
        
$message "The published Prowl report did not match the query parameters of: "$query_icb .", "$query_year .", "$query_units .".";
      }
    }
  }  

  return array(
$message,$version_no,$model_name,$main_icb,$year,$native_units,$status,$content);
}

//
function content_parse($content,$main_icb,$units,$per_1,$per_2) {
  
$i=0//$i in the while loop set a limit to the number of records processed
  
$line current($content); //echo "<br />". $record; print_r($content);
  
$record_list = array();
  
  while (
$line AND $i<2000) {
    
$record line_parse($line,$main_icb,$units,$per_1,$per_2);
    
    if (
$record['copy']) {     
      if (
in_array($record['copy'],$record_list)) {
        
$record['message'] = " (Duplicate record) ";
      }
      else {
        
$record_list[] = $record['copy'];
      }
            
      
$record_array[] = $record;  
    }
    
    
$line next($content);
    
$i++;    
  }
  
  return 
$record_array;
}

//
function line_parse($record,$main_icb,$native_units,$per_1,$per_2) {
  
$record array_filter(explode(" ",$record));  
  
$date=trim(current($record)); 
  
  if (
substr($date,0,1)==":") {
    
$unixtime trim($date,":\n");
  }  
                    
  if (
in_period($date,$per_1,$per_2)) { //echo "date=". $date ."<br />";
    
$keyword next($record); 
      
    if (
$keyword=="reverse") {
      
$sign = -1;
      
$rev_phrase $date ." reverse ";
      
$date next($record);
      
$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);
          }
          
          
$notary next($record);          
          
//this record copy will be trimmed later
          
$record_copy $date ." from "$from_icb ." to "$to_icb ." "number_format($sign*$amount,2) ." "$units ." "$id;
          
$record = array("copy"=>$record_copy,"date"=>$date,"keyword"=>$keyword,"from_icb"=>$from_icb,"to_icb"=>$to_icb,"amount"=>$amount,"sign"=>$sign);
        }
      }            
    }
    else if (
$keyword=="add" OR $keyword=="cut") { //this might have reporter also      
      
$amount$sign*next($record);
      
$units next($record);
      
$other_keyword next($record);
        
      if (
valid_amt_units($amount,$units,$native_units)) { //echo "<br />adjust,sign,keyword ". $adjust .", ". $sign .", ". $keyword;
        
if (next($record)==$main_icb) {
          
$param trim(next($record));
                      
          
//if param. or param.subparam., then record ends without id and the last element from explode will be null
          
if (substr($param,-1)!=".") { 
            
$id " "next($record);
          }   
                    
          
$notary next($record);            
          
$record_copy $date ." "$keyword ." "number_format($sign*$amount,2) ." "$units ." to "$main_icb ." "$param $id;  //this record copy will be trimmed later
          
$record = array("copy"=>$record_copy,"date"=>$date,"keyword"=>$keyword,"amount"=>$amount,"sign"=>$sign,"param"=>$param);     
        }
      }      
    }    
    else if (
$keyword=="set") { //echo ">". $main_icb ."<";
      
if (next($record)==$main_icb) {
        
$param explode(".",next($record)); //echo "<br />set param ". $param;        
                
        
if ($param[0]=="unusedBudget" OR $param[0]=="lastReport") {
          
$sub_param "."$param[1];
        }
        else {
          
$sub_param "";
        }
        
        
$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;                           
        
$notary next($record);       
        
$record_copy $date ." set "$main_icb ." "$param[0] . $sub_param ." to "$param_value $units;
        
$record = array("copy"=>$record_copy,"date"=>$date,"keyword"=>$keyword,"param"=>$param[0],"sub_param"=>$sub_param,"param_value"=>$param_value,"sign"=>$sign);
      }
    } 
    else if (
$keyword=="tally") {         
      
$param explode(".",next($record));
        
      if (
$param[0]=="unusedBudget") {
        
$sub_param "."$param[1];
      }
        
      if (
valid_amt_units($amount,$units,$native_units)) {
        
$amount next($record);
        
$units next($record);                 
        
$record_copy $date ." tally "$param[0] . $sub_param ." "number_format($amount,2) ." "$units;       
        
        
$record = array("copy"=>$record_copy,"date"=>$date,"keyword"=>$keyword,"param"=>$param,"subparam"=>$subparam,"amount"=>$amount,"sign"=>$sign);
      }
    }
    else {
      
$record = array();
    }    
  }
  
  if (
$record['copy']) {
    if (
substr($notary,0,1)=="[" AND substr($notary,-1)=="]") {
      
$record array_merge($record,array("notary"=>trim($notary,"[..]")));    
    }       
  }
  
  if (
$unixtime) {
    
$record array_merge($record,array("unixtime"=>$unixtime));
  }
  
  return 
$record;
}

//
function valid_amt_units($amount,$units,$native_units) { //echo $units ." : ". $native_units ."<br />";
  
if (is_numeric($amount)) { 
    if (
$units == $native_units OR $native_units == substr($units,0,-1) OR $units == substr($native_units,0,-1)) { //echo "valid amt_units";
      
return true
    }
    else { 
//echo "invalid amt units";
      
return false;
    } 
  }
  else {return 
false;}
}

//
function valid_tally($publ_amt,$audit_amt) {
  if (!
is_numeric($publ_amt) OR $publ_amt!=$audit_amt) {
    return 
"<font color=\"red\">"number_format($audit_amt,2) ."</font>";
  }
  else { 
    return 
number_format($audit_amt,2);
  }


//
function in_period($date,$per_1,$per_2) {
  if (
substr($date,0,$per_2)== $per_1) {  //echo "true";
    
return true;
  }    
  else { 
//echo "false";
    
return false;
  }     
}

?>
1