<?php
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_and_attendance_costs2_$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][0];
$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;

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 "final balance: ".$final_balance."<br />";
echo "lessons left (approx ach): ".$final_balance_ach;
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_and_attendance_costs2_$member.csv\"><img src=\"pix/libre_office_calc01.png\" width=\"20\" height=\"20\" alt=\"logo\"> payment history</a>";
echo "</div>";

}// 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)

?>
