<?php 
date_default_timezone_set('Asia/Manila');
$thedatetoday = date('Y-m-d'); 


$permitted_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
 
function generate_string($input, $strength = 16) {
    $input_length = strlen($input);
    $random_string = '';
    for($i = 0; $i < $strength; $i++) {
        $random_character = $input[mt_rand(0, $input_length - 1)];
        $random_string .= $random_character;
    }
 
    return $random_string;
}

$unique_id = $_SESSION['username'];
// Fetch the status from the database
$sql = "SELECT status FROM tbl_hidebuttons LIMIT 1";
$result = $conn->query($sql);

$hidestatus = 0; // Default to 1 if not found
if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $hidestatus = $row['status'];
}

 


$sql_user_details = "SELECT * FROM tbl_admin WHERE id = '$unique_id'";
$result_user_details = mysqli_query($conn, $sql_user_details);
$row_user_details = mysqli_fetch_assoc($result_user_details);
$username = $row_user_details['username'];
 
$password = $row_user_details['password'];
//$accnt_type = $row_user_details['accnt_type'];
$level = $row_user_details['level'];


 
 // Fetch reading dates from the database
$sqlalldates = "SELECT DISTINCT reading_date
FROM tbl_members_report
ORDER BY reading_date DESC";
$result_alldates = $conn->query($sqlalldates);

$sql_checkplacement = "SELECT *   FROM tbl_reading_sched where status = 1 ";
$result_countplacement = mysqli_query($conn, $sql_checkplacement);
$row_date = mysqli_fetch_assoc($result_countplacement);

$c_readingdate =  $row_date ["reading_date"];
$c_due_date =  $row_date ["due_date"];
$c_disconnection_date=  $row_date ["disconnection_date"];


//THIS FOR CONNECTED ACCOUNTS
//get bill payments  records

//SELECT tbl_payments.unique_id,tbl_user_account.acct_holder,tbl_payments.payment_type,tbl_payments.amount,tbl_user_account.line_status FROM `tbl_payments` left JOIN tbl_user_account ON tbl_payments.unique_id = tbl_user_account.unique_id where tbl_user_account.line_status = 1 and tbl_payments.payment_type = 'WB';
//2541178
$sql_user_billrecord= "SELECT sum(tbl_payments.amount) as billspaidbalances FROM `tbl_payments` left JOIN tbl_user_account ON tbl_payments.unique_id = tbl_user_account.unique_id where tbl_user_account.line_status = 1 and tbl_payments.payment_type = 'WB'";
$result_user_billpay= mysqli_query($conn, $sql_user_billrecord);
$row_user_billpay_summary = mysqli_fetch_assoc($result_user_billpay);
//
$sql_user_readings1 = "SELECT sum(tbl_members_report.billing_amount) as totalbills FROM tbl_members_report left JOIN tbl_user_account ON tbl_user_account.unique_id = tbl_members_report.account_no where tbl_user_account.line_status = 1";
$result_user_details2 = mysqli_query($conn, $sql_user_readings1);
$row_user_readings_summary = mysqli_fetch_assoc($result_user_details2);

//
$sql_user_bklogs = "SELECT sum(tbl_backlogs_collection.amount) as bklogtotal FROM tbl_backlogs_collection left join tbl_user_account ON tbl_backlogs_collection.account_no = tbl_user_account.unique_id where tbl_user_account.line_status = 1";
$result_bl_details2 = mysqli_query($conn, $sql_user_bklogs);
$row_user_bklogread = mysqli_fetch_assoc($result_bl_details2);


$bklog = $row_user_bklogread['bklogtotal'];
$totalbillssummary = $row_user_readings_summary['totalbills'];
$remainingbalance_summary = $bklog + $totalbillssummary; // 27313 +  3088925
$billspayment_summary = $row_user_billpay_summary['billspaidbalances'];
//$totalunpaidbill_wb = $totalbillssummary;
$totalunpaidbill_wb = $remainingbalance_summary - $billspayment_summary;
//$totalunpaidbill_wb  = 3,165,063 -  2541178


//THIS IS FOR DISCONNECTED ACCOUNTS
//get bill payments  records
$dis_sql_user_billrecord= "SELECT SUM(amount) as billspaidbalances FROM tbl_payments where payment_type = 'WB'";
$dis_result_user_billpay= mysqli_query($conn, $dis_sql_user_billrecord);
$dis_row_user_billpay_summary = mysqli_fetch_assoc($dis_result_user_billpay);

$dis_sql_user_readings1 = "SELECT sum(tbl_members_report.billing_amount) as totalbills FROM tbl_members_report left JOIN tbl_user_account ON tbl_user_account.unique_id = tbl_members_report.account_no where tbl_user_account.line_status = 0;";
$dis_result_user_details2 = mysqli_query($conn, $dis_sql_user_readings1);
$dis_row_user_readings_summary = mysqli_fetch_assoc($dis_result_user_details2);

$dis_sql_user_bklogs = "SELECT sum(amount) as bklogtotal FROM tbl_backlogs_collection ";
$dis_result_bl_details2 = mysqli_query($conn, $dis_sql_user_bklogs);
$dis_row_user_bklogread = mysqli_fetch_assoc($dis_result_bl_details2);


$dis_bklog = $dis_row_user_bklogread['bklogtotal'];
$dis_totalbillssummary = $dis_row_user_readings_summary['totalbills'];
$dis_remainingbalance_summary = $dis_bklog + $dis_totalbillssummary;
$dis_billspayment_summary = $dis_row_user_billpay_summary['billspaidbalances'];

$dis_totalunpaidbill_wb = $dis_remainingbalance_summary - $dis_billspayment_summary;

//END DISCONNECTED ACCOUNTS




//unpaid waterbill

$unique_id=  $_GET['mid'] ;
//$sql_user_readings1 = "SELECT (SELECT SUM(`billing_amount`) FROM tbl_members_report  where account_no = '$unique_id' ) - (SELECT SUM(amount) FROM tbl_payments where unique_id = '$unique_id') AS total_billbalance";

//get bill payments  records
$sql_user_billrecord= "SELECT SUM(amount) as billspaidbalances FROM tbl_payments where unique_id = '$unique_id' and payment_type = 'WB'";
$result_user_billpay= mysqli_query($conn, $sql_user_billrecord);
$row_user_billpay = mysqli_fetch_assoc($result_user_billpay);

$sql_user_readings1 = "SELECT sum(billing_amount) as totalbills FROM tbl_members_report WHERE Branch = 'Laguna' and account_no = '$unique_id' order by reading_date desc";
$result_user_details2 = mysqli_query($conn, $sql_user_readings1);
$row_user_readings3 = mysqli_fetch_assoc($result_user_details2);
$wpblance = $row_user_readings3['totalbills'] ;

$sql_user_bklogs = "SELECT sum(amount) as bklogtotal FROM tbl_backlogs_collection where account_no = '$unique_id' ";
$result_bl_details2 = mysqli_query($conn, $sql_user_bklogs);
$row_user_bklogread = mysqli_fetch_assoc($result_bl_details2);
$bklog = $row_user_bklogread['bklogtotal'];


$remainingbalance = $wpblance  + $bklog;
$billspayment = $row_user_billpay['billspaidbalances'];

 
$billspayment = $row_user_billpay['billspaidbalances'];

$totalunpaidwaterbillbill = $remainingbalance - $billspayment; 


//unpaid meter device
$unique_id=  $_GET['mid'] ;
$sql_user_billrecord= "SELECT SUM(amount) as feepaidbalances FROM tbl_payments where payment_type = 'MD' and unique_id = '$unique_id'";
						$result_user_billpay= mysqli_query($conn, $sql_user_billrecord);
						$row_user_billpay_summary = mysqli_fetch_assoc($result_user_billpay);
						
						$sql_user_readings1 = "SELECT sum(fee) as totalfee FROM tbl_meterdevice where account_no = '$unique_id' ";
						$result_user_details2 = mysqli_query($conn, $sql_user_readings1);
						$row_user_readings_summary = mysqli_fetch_assoc($result_user_details2);
						
						$remainingbalance_summary = $row_user_readings_summary['totalfee'];
						$billspayment_summary = $row_user_billpay_summary['feepaidbalances'];
						
						$totalunpaidmeterfee = $remainingbalance_summary - $billspayment_summary;



//reading collection
$sql_newreadingbilled = "SELECT sum(billing_amount)  as totalbilled FROM `tbl_members_report` where transaction_date = '$thedatetoday'";
$result_bl_details2 = mysqli_query($conn, $sql_newreadingbilled);
$row_user_bklogread = mysqli_fetch_assoc($result_bl_details2);
$totalbilled = $row_user_bklogread['totalbilled'];
						
$sql_newreadingcount = "SELECT * FROM `tbl_members_report` where transaction_date = '$thedatetoday'";
$result = mysqli_query($conn, $sql_newreadingcount);
$rowtotalread = mysqli_num_rows($result);

//today payment received
$sql_user_billrecord= "SELECT SUM(amount) as todaypaid FROM tbl_payments where payment_date LIKE '$thedatetoday%'";
$result_user_billpay= mysqli_query($conn, $sql_user_billrecord);
$row_user_billpay_today = mysqli_fetch_assoc($result_user_billpay);
$todaypayreceived = $row_user_billpay_today['todaypaid'];

					 
?>
<?php if ($hidestatus == 0): ?>
    <style>
        .hidebutton { display: none; }
    </style>
<?php endif; ?>



