<?php
include ('sbook_plugins/attendance_costs_generated_from_xml_clog_sbook.inc');

if ($member == NULL)
{
echo "<span style=\"color: red;\">Your session has expired!</span><br />Please log in to view your payment history.";
}
else
{

  
$file_payment_and_attendance_costs2 = "tmp_files/payment_history_and_attendance_costs_sbook_$member.csv";

if (file_exists($file_payment_and_attendance_costs2 )) {
$file_payment_and_attendance_costs2 = fopen($file_payment_and_attendance_costs2, "r") or die ('cant open $ file_payment_and_attendance_costs2');
  


// empty array before each loop or will add up
 $csv = array();

while(!feof($file_payment_and_attendance_costs2)) {
  // [] mandatory
    $csv[] = fgetcsv($file_payment_and_attendance_costs2,1024,";");
//    var_dump ($csv[0][4]);
}

// $last_update = $csv[0][6]; // this is today's date only
// but should be giving last date from bank statement
$total_ach = array_sum(array_column($csv,'2'));
$total_cost = array_sum(array_column($csv,'4'));
$total_paid = array_sum(array_column($csv,'5'));
$final_balance = $total_cost + $total_paid;

$session_rate_for_attendance_history = (-$total_cost) / $total_ach; // approximate bc actually depends if f2f or on-line
$final_balance_ach = $final_balance / $session_rate_for_attendance_history;
$final_balance = number_format($final_balance, 0);  
$final_balance_ach = number_format($final_balance_ach, 2);


echo "<div class=\"sbook_tooltip2\">";
echo "<div class=\"sbook_tooltip2text\">"; 
//echo "last update: ".$last_update."<br />";
echo "total ach: ".$total_ach."<br />";
//echo "total cost: ".$total_cost."<br />";
//echo "total paid: ".$total_paid."<br />";
echo "lessons left (approx ach): ".$final_balance_ach."<br />";
echo "balance: <strong>".$final_balance."</strong>\n";
echo "</div>\n";

echo "<img src=\"pix/icons8-payment-history-100.png\" width=\"35\" border=\"0\" alt=\"payment history\"> <a download class=\"clog\" target=\"about_blank\" href=\"tmp_files/payment_history_and_attendance_costs_sbook_$member.csv\"><img src=\"pix/libre_office_calc01.png\" width=\"20\" height=\"20\" alt=\"logo\"> payment history</a>";
echo "</div>";

fclose($file_payment_and_attendance_costs2);

// add total sums
/***
$file_payment_and_attendance_costs2 = "tmp_files/payment_history_and_attendance_costs_sbook_$member.csv";
$fh_file_payment_and_attendance_costs2 = fopen($file_payment_and_attendance_costs2, "r+") or die ('cant open $ file_payment_and_attendance_costs2'); // use r+ and NOT just r!

// $data = fgets($file_payment_and_attendance_costs2, 4096);
$data = fgets($fh_file_payment_and_attendance_costs2, 4096);
fseek($data, 350);
echo ftell($data);
$table_footer1 = ";total ach;".$total_ach.";;;;\n";
fwrite($data, $table_footer1) or die("could not write data");
***/


/*
// v1  
$table_footer1 = ";total ach;".$total_ach.";;last update: ".`date +%Y%m%d`.";\n";
$table_footer2 = ";total due;;;".$total_cost.";\n";
$table_footer3 = ";total paid;;;;".$total_paid."\n";
$table_footer4 = ";BALANCE;;;;".$final_balance."\n";
shell_exec("sed -i '3i ".$table_footer1.$table_footer2.$table_footer3.$table_footer4."' tmp_files/payment_history_and_attendance_costs_sbook_$member.csv"); 
*/

/***/
// v2
$table_heading1 = ";ATTENDANCE AND COSTS: ".$member.";;;;last update: ;".`date +%Y%m%d`.";\n";
shell_exec("sed -i '1i ".$table_heading1."' tmp_files/payment_history_and_attendance_costs_sbook_$member.csv"); 
$table_footer5 = ";;;;;;\n";
shell_exec("sed -i '2i ".$table_footer5."' tmp_files/payment_history_and_attendance_costs_sbook_$member.csv"); 
$table_footer1 = "SUMMARY;total ach;".$total_ach.";;;\n";
shell_exec("sed -i '3i ".$table_footer1."' tmp_files/payment_history_and_attendance_costs_sbook_$member.csv"); 
$table_footer2 = ";total due;;;".$total_cost.";\n";
shell_exec("sed -i '4i ".$table_footer2."' tmp_files/payment_history_and_attendance_costs_sbook_$member.csv"); 
$table_footer3 = ";total paid;;;;".$total_paid."\n";
shell_exec("sed -i '5i ".$table_footer3."' tmp_files/payment_history_and_attendance_costs_sbook_$member.csv"); 
$table_footer4 = ";BALANCE;;;;;".$final_balance."\n";
shell_exec("sed -i '6i ".$table_footer4."' tmp_files/payment_history_and_attendance_costs_sbook_$member.csv"); 
$table_footer5 = ";;;;;;\n";
shell_exec("sed -i '7i ".$table_footer5."' tmp_files/payment_history_and_attendance_costs_sbook_$member.csv"); 
$table_heading4 = "date;description;ach;rate;due;paid;\n";
shell_exec("sed -i '8i ".$table_heading4."' tmp_files/payment_history_and_attendance_costs_sbook_$member.csv"); 
/***/

/***
// v3
// works w/o values
//$table_heading1 = ';ATTENDANCE AND COSTS: ".$member.";;;;;\n;total ach;".$total_ach.";;last update: ".`date +%Y%m%d`.";\n;total due;;;".$total_cost.";\n;total paid;;;;".$total_paid."\n;BALANCE;;;;".$final_balance."\n\ndate;description;ach;rate;due;paid;';
// should work, but fails
$table_heading1 = ";ATTENDANCE AND COSTS: $member;;;;;\n;total ach;$total_ach;;last update: `date +%Y%m%d`;\n;total due;;;$total_cost;\n;total paid;;;;$total_paid\n;BALANCE;;;;$final_balance\n\ndate;description;ach;rate;due;paid;";

//$table_heading1 = ";ATTENDANCE AND COSTS: ".$member.";;;;;\n;total ach;".$total_ach.";;last update: ".`date +%Y%m%d`.";\n;total due;;;".$total_cost.";\n;total paid;;;;".$total_paid."\n;BALANCE;;;;".$final_balance."\n\ndate;description;ach;rate;due;paid;";
//$table_heading1 = ";ATTENDANCE AND COSTS: $member;;;;;\n;total ach;$total_ach;;last update: `date +%Y%m%d`;\n;total due;;;$total_cost;\n;total paid;;;;$total_paid\n;BALANCE;;;;$final_balance\n\ndate;description;ach;rate;due;paid;";
//$table_heading1 = ";ATTENDANCE AND COSTS: ".$member.";;;;;\n;total ach;".$total_ach.";;last update: `date +%Y%m%d`;\n;total due;;;".$total_cost.";\n;total paid;;;;".$total_paid."\n;BALANCE;;;;".$final_balance."\n\ndate;description;ach;rate;due;paid;";
echo $table_heading1; 
shell_exec("sed -i '1i $table_heading1' tmp_files/payment_history_and_attendance_costs_sbook_$member.csv"); 

// fails
//$table_heading1 = ';ATTENDANCE AND COSTS: '.$member.';;;;;\n;total ach;'.$total_ach.';;last update: '.`date +%Y%m%d`.';\n;total due;;;'.$total_cost.';\n;total paid;;;;'.$total_paid.'\n;BALANCE;;;;'.$final_balance.'\n\ndate;description;ach;rate;due;paid;';
//shell_exec("sed -i '1i ".$table_heading1."' tmp_files/payment_history_and_attendance_costs_sbook_$member.csv"); 
***/


shell_exec("cp tmp_files/payment_history_and_attendance_costs_sbook_$member.csv tmp_files/payment_history_and_attendance_costs_sbook2_$member.csv"); 

}// if (file_exists($file_payment_and_attendance_costs2 ))
/*
else
{
  echo "Please, reload page to access payment history\n"; // needed when run for the 1st time
}
 */

} //if ($member == NULL)

?>
