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

include ("common_functions.php");

//returns nothing on success, message on failure
function check_journal($record) {
  
$sql "SELECT status_code,entered_on,entered_on,updated_on FROM journal WHERE record='$record'";    
  
$result mysql_query($sql);
       
  if (!
$result) {
    return 
"Query Error: "$sql ." "mysql_error();
  }
  else {
    
$num_rows mysql_num_rows($result);      
    if (
$num_rows!=0) {
      
$select=mysql_fetch_assoc($result);
      
      if (
$select['status_code']==0) {//will add fix-code later; check which ledger has not been updated, if any        
        
return "This record has been verified on "$select['entered_on'] .". Would you like tyaga.org to maintain an itemized report and provide other reporter services for your currency brand? <a href=''>Yes</a>";
      } 
      else if (
$select['status_code']==1) {//will add fix-code later; check which ledger has not been updated, if any        
        
return "This record has to be rolled forward from the entry date of "$select['entered_on'] .".";
      }
      else {      
        return 
"This record has already been entered and posted on "$select['updated_on'] .".";
      }
    }
    else if (
$num_rows>1) {//echo $sql;
      //try to fix (to be coded later), then return the following message
      
return "The record has duplicates in the journal."
    }    
  }  
}

//returns array(null message, period) on success, message on failure
function check_brand($icb,$units) {
  
$sql="SELECT status_code,period FROM brand WHERE brand_name='$icb' AND units='$units' LIMIT 1";
  
$result mysql_query($sql);
       
  if (!
$result) { 
    return array(
"Query Error: "$sql .". "mysql_error(),"");
  }
  else {
    
$num_rows mysql_num_rows($result);      
    if (
$num_rows==0) {
      return array(
"external","2008");
    }
    else if (
$num_rows>1) { 
      
//fix to be coded later, then return the following message
      
return array("Error: Brand "$icb ." has more than one "$units ." tally.","");
    }
    else {
      
$select=mysql_fetch_assoc($result); 
      return array(
$icb,$select['period']);      
    } 
  }
}

//returns array(null message, journal log no) on success, message on failure
function journal_entry($from_link,$to_link,$record,$reporter,$status_code) {
  
//status code 0 indicates a verified record that has not been posted, such as when both icbs are non-members
  //status code 1 indicates a record that has been initiated for posting
  //status code 2 indicates a record that has been posted

  
$sql="INSERT INTO journal VALUES($status_code,NOW(),DEFAULT,DEFAULT,'$from_link','$to_link','$record','$reporter')"
  
$result mysql_query($sql);
            
  if (!
$result) {
    return 
"Query error: "mysql_error();          
  }
  else if (
mysql_affected_rows()!=1) {
    return 
"Affected rows != 1";
  }
  else { 
//check to make sure that there were no concurrent entry for the same record
    
$sql "SELECT log_no FROM journal WHERE record='$record'";    
    
$result mysql_query($sql);
       
    if (!
$result) {
      
$message "Query Error: "mysql_error();
    }
    else {
      
$num_rows mysql_num_rows($result);      
      if (
$num_rows==0) {
        return array(
"Failed to make journal entry. Please re-try.",$select['log_no']);
      }
      else  if (
$num_rows!=1) {
        
//try to fix (to be coded later), then return the following message
        
return array("The record has duplicates in the journal.",$select['log_no']);
      }
      else {
        
$select=mysql_fetch_assoc($result);
        return array (
"",$select['log_no']);
      }        
    }
  }
}

//
function update_ledger($icb,$units,$period,$record) {
  if (
$icb) {
    
$ledger_file="reports/"str_replace(array(".","/"),"",$icb) ."_"$period ."_"$units .".txt";
    if (
file_put_contents($ledger_file,"\r\n"$record,FILE_APPEND)==false) {
      return 
"Brand "$icb ." ledger was not updated.";
    }
  }
  else {
    return 
"Brand "$icb ." ledger is not maintained on this site.";
  }  
}

function 
update_journal($record) { 
  
$sql "UPDATE journal SET status_code=2 WHERE record='$record' LIMIT 1";
  
$result mysql_query($sql);
  if (!
$result) {
    return 
"Did not quite finish the posting. "mysql_error();
  }
  else if (
mysql_affected_rows()==0) {
    return 
"Did not quite finish the posting.";
  }  
}
?>
1