<?php

function backendPassswordReset($email,$password){
$URL= "https://app.pgsterling.com/api/backend-password-reset-client";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_POST, 2);

// In real life you should use something like:
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query(array('email' => $email,'password' => $password)));

// Receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec($ch);

curl_close ($ch);

//var_dump($server_output);

return json_decode($server_output,true);

//string(60) "{"statusCode":200,"message":"Password reset was successful"}"
}


function format_gh_phoneNum($phoneNum){
    $data = substr($phoneNum,1,9);
    $data = "+233{$data}";
    return $data;
}
function format_phoneNum($dbc,$countryCode,$phoneNum){
    $country = get_system_country_by_countryCode($dbc,$countryCode);

    $data = substr($phoneNum,-9);
    $data = $country['callCode']."{$data}";
    return $data;
}
function check_phoneNum_and_pin($dbc,$phoneNum,$pin){
    //$query = "SELECT * FROM customer_tbl WHERE phoneNum = '$phoneNum' AND ussdPasscode = '$pin' AND registeredPhoneType ='USSD'";
    $query = "SELECT * FROM customer_tbl WHERE phoneNum = '$phoneNum' AND ussdPasscode = '$pin'";
    $q = mysqli_query($dbc,$query);
    if(mysqli_num_rows($q) > 0){
        return true;
    }
}

function check_phoneNum_and_password($dbc,$phoneNum,$password){
    //$query = "SELECT * FROM customer_tbl WHERE phoneNum = '$phoneNum' AND ussdPasscode = '$pin' AND registeredPhoneType ='USSD'";
    $query = "SELECT * FROM customer_tbl WHERE phoneNum = '$phoneNum' AND password = '$password'";
    $q = mysqli_query($dbc,$query);
    if(mysqli_num_rows($q) > 0){
        return true;
    }
}



function check_email_and_password($dbc,$email,$password){

		$SQL = "SELECT * FROM customer_tbl WHERE email = '$email' OR phoneNum = '$email' ";
	
		$query = mysqli_query($dbc,$SQL);
	
		if(mysqli_num_rows($query)==1):
		   $row = mysqli_fetch_object($query);
			if(password_verify($password, $row->password)):
			   return true; 
			 else: 
			  return false;
			 endif;
		else: 
	    	return false; 
		endif;
            
}


function encrypting_pass($password)
{	
	$hash =  bin2hex(random_bytes(20));
	$data = array('passwd_salt'  => $hash);
	$crypt_pass = password_hash($password, PASSWORD_BCRYPT, $data);

	return  $crypt_pass;
}



function restore_ussd_account_on_smart($dbc,$phoneNum,$keyCode,$password,$simsid,$simSerial,$phoneIMEI){
    $q = mysqli_query($dbc,"UPDATE customer_tbl SET keyCode = '$keyCode', password = '$password',simSID = '$simsid',simSerial ='$simSerial', phoneIMEI = '$phoneIMEI', registeredPhoneType = 'ALL' WHERE phoneNum ='$phoneNum' ");
    if($q){
        return true;
    }
}

function add_customer_by_smartPhone($dbc,$phoneNum,$password,$email,$fullName,$dob,$phoneIMEI,$keyCode,$simSerial,$simID,$countryCode){

    //Constant Variables
    $accountType = 1;
    $uGroup = 1;
    $status = "ACTIVE";
    $registeredPhoneType  = "SMART";
    $pin = gen_USSD_PIN();

    $kycData = get_KYC_by_countryCode_and_name($dbc,$countryCode,'medium');

    if($countryCode == 'lr' OR $countryCode == 'lr1'){
        $currencyType = 'MULTIPLE';
    }else{
        $currencyType = 'SINGLE';
    }


    $q = mysqli_query($dbc,"INSERT INTO customer_tbl (id,phoneNum,password,email,fullName,dob,phoneIMEI,keyCode,simSerial,simSID,countryCode,accountType,uGroup,registeredPhoneType,status,accountKYC,ussdPasscode,currencyType)
      VALUES (UUID(),'$phoneNum','$password','$email','$fullName','$dob','$phoneIMEI','$keyCode','$simSerial','$simID','$countryCode','$accountType','$uGroup','$registeredPhoneType','$status','$kycData[id]','$pin','$currencyType')
    ");
    if($q){

        return true;

    }
}

function add_customer_by_web($dbc,$post_data){

    $data = (object)$post_data;

    //Constant Variables
    $accountType = 1;
    $uGroup      = 1;
    $status      = "ACTIVE";
    $registeredPhoneType  = "WEB";
    $pin = gen_USSD_PIN();

    $kycData = get_KYC_by_countryCode_and_name($dbc,$data->countryCode,'medium');
    $currencyType = 'SINGLE';

    $q = mysqli_query($dbc,"INSERT INTO customer_tbl (id,phoneNum,password,email,fullName,dob,countryCode, physical_address ,accountType,uGroup,registeredPhoneType,status,accountKYC,ussdPasscode,currencyType)
      VALUES (UUID(),'$data->phoneNum','$data->password','$data->email','$data->fullName','$data->dob','$data->countryCode', '$data->physical_address','$accountType','$uGroup','$registeredPhoneType','$status','$kycData[id]','$pin','$currencyType')
   ");
    if($q){
        return true;
    }
}

function add_new_super_agent($dbc,$phoneNum,$fullName,$password,$companyName,$companyRegNum,$companyRegDate,$address,$country,$accountType,$superType,$is_external){
    $phoneNum = format_phoneNum($dbc,$country,$phoneNum);

    //$kycData = get_KYC_by_countryCode_and_name($dbc,$country,'super');$kycData[id]

    if($country == 'lr' OR $country == 'lr1'){
        $currencyType = 'MULTIPLE';
    }else{
        $currencyType = 'SINGLE';
    }


    $q = mysqli_query($dbc,"INSERT INTO `customer_tbl` (`id`,`phoneNum`,`fullName,password`,`address`,`companyName`,`companyRegNumber`,`companyRegDate`,`uGroup`,`accountType`,`status`,`countryCode`,`accountKYC`,`superType`,`is_external`,`currencyType`)
      VALUES (UUID(),'$phoneNum','$fullName','$password','$address','$companyName','$companyRegNum','$companyRegDate',1,'$accountType','ACTIVE','$country','4','$superType','$is_external','$currencyType')
    ");
    if($q){
        /*$qq = mysqli_query($dbc,"SELECT id FROM customer_tbl WHERE phoneNum = '$phoneNum'");
        $data = mysqli_fetch_assoc($qq);

        if(create_new_account($dbc,$data['id'])){
            return true;
        }*/
        return true;
    }
}


function update_customer_switch_currency($dbc,$phoneNum,$countryCode){

    $query = " UPDATE customer_tbl SET
                    countryCode = '$countryCode'
                    WHERE phoneNum = '$phoneNum'";
	
	
    $q = mysqli_query($dbc,$query);
   
	
	if($q){
	return true;
	}
	
	
	
}

function update_customer_phone($dbc,$customerID,$phoneNum,$fullName,$idType,$idNumber,$phoneIMEI,$status,$gender,$keyCode,$simSerial,$simID,$dob,$countryCode){
    $query = " UPDATE customer_tbl SET
                    fullName = '$fullName',
                    idType = '$idType',
                    idNumber = '$idNumber',
                    phoneIMEI = '$phoneIMEI',
                    status = '$status',
                    gender = '$gender',
                    keyCode = '$keyCode',
                    simSerial = '$simSerial',
                    simSID = '$simID',
                    dob = '$dob',
                    countryCode = '$countryCode'
                    WHERE phoneNum = '$phoneNum' AND id = '$customerID'
    ";
    $q = mysqli_query($dbc,$query);
    if($q){
        return true;
    }
}



function update_customer($dbc,$customerID,$fullName,$idType,$idNumber,$status,$gender,$dob,$accountType,$uGroup,$companyName,$companyRegNumber,$companyRegDate,$address,$kyc,$branch1,$branch2,$branch3,$branch4,$branch5,$parent,$superType,$external){
    $query = " UPDATE customer_tbl SET
                    fullName = '$fullName',
                    idType = '$idType',
                    idNumber = '$idNumber',
                    status = '$status',
                    gender = '$gender',
                    dob = '$dob',
                    accountType = '$accountType',
                    companyName = '$companyName',
                    companyRegNumber = '$companyRegNumber',
                    companyRegDate = '$companyRegDate',
                    address = '$address',
                    accountKYC = '$kyc',
                    superType = '$superType',
                    is_external = '$external',
                    uGroup = '$uGroup',
                    branch1 = '$branch1',
                    branch2 = '$branch2',
                    branch3 = '$branch3',
                    branch4 = '$branch4',
                    branch5 = '$branch5',
                    parent = '$parent'
                    WHERE id = '$customerID'
    ";
    $q = mysqli_query($dbc,$query);
    if($q){
        return true;
    }
}
function update_customer_smart($dbc, $phoneNum, $fullName, $idType, $idNumber, $address, $email,$idExpiringDate,$city,$region,$postCode,$nextOfKin,$nextOfKin_phone,$socialSec){
    $query = " UPDATE customer_tbl SET
                    fullName = '$fullName',
                    idType = '$idType',
                    idNumber = '$idNumber',
                    email = '$email',
                    city = '$city',
                    idexpiry_date = '$idExpiringDate',
                    region = '$region',
                    postcode = '$postCode',
                    next_of_kin = '$nextOfKin',
                    next_of_kin_phoneNum = '$nextOfKin_phone',
                    social_security = '$socialSec',
                    address = '$address'
                    WHERE phoneNum = '$phoneNum'
    ";
    $q = mysqli_query($dbc,$query);
    if($q){
        return true;
    }
}

function update_customer_new_admin($dbc, $phoneNum, $fullName, $idType, $idNumber, $address, $email,$postCode,$companyName,$companyRegNumber,$companyRegDate,$accountType,$accountKYC,$uGroup,$status,$dob,$gender){
    
    $query = "UPDATE `customer_tbl` SET 
    `fullName` = '$fullName',
    `email` = '$email', 
    `gender` = '$gender', 
    `dob` = '$dob',
    `postcode` = '$postCode', 
    `companyName` = '$companyName',
    `companyRegNumber` = '$companyRegNumber',
    `companyRegDate` = '$companyRegDate', 
    `idType` = '$idType',
    `idNumber` = '$idNumber',
    `status` = '$status',
    `accountType` = '$accountType',
    `accountKYC` = '$accountKYC', 
    `uGroup` = '$uGroup'
    WHERE `customer_tbl`.`phoneNum` = '$phoneNum'
    ";
    /**
    $query = " UPDATE customer_tbl SET
                    `fullName` = '$fullName',
                    `idType` = '$idType',
                    `idNumber` = '$idNumber',
                    `email` = '$email',
                    `companyName` = '$companyName',
                    `companyRegNumber` = '$companyRegNumber',
                    `companyRegDate` = '$companyRegDate',
                    `postcode` = '$postCode',
                    `accountType` = '$accountType',
                    `accountKYC` = '$accountKYC',
                    `gender` = '$gender',
                    `dob` = '$dob',
                    `uGroup` = '$uGroup',
                    `status` = '$status',
                    `address` = '$address'
                    WHERE `customer_tbl`.`phoneNum` = '$phoneNum'
    ";
    **/
    $q = mysqli_query($dbc,$query);
    if($q){
        return true;
    }
}

function update_customer_hubtel_pull($dbc,$customer_id,$status){
    $query = " UPDATE customer_tbl SET
                    hubtel_pull_status = '$status'
                    WHERE id = '$customer_id'
    ";
    $q = mysqli_query($dbc,$query);
    if($q){
        return true;
    }
}

function update_customer_hubtel_push($dbc,$customer_id,$status){
    $query = " UPDATE customer_tbl SET
                    hubtel_push_status = '$status'
                    WHERE id = '$customer_id'
    ";
    $q = mysqli_query($dbc,$query);
    if($q){
        return true;
    }
}

function update_customer_new_money_credit($dbc,$customer_id,$status,$amount){
    $query = " UPDATE customer_tbl SET
                    hubtel_push_status = '$status',
					new_money = new_money + '$amount'
                    WHERE id = '$customer_id'
    ";
    $q = mysqli_query($dbc,$query);
    if($q){
        return true;
    }
}


function update_customer_new_money_debit($dbc,$customer_id,$status,$amount){
    $query = " UPDATE customer_tbl SET
                    hubtel_push_status = '$status',
					new_money = new_money - '$amount'
                    WHERE id = '$customer_id'
    ";
    $q = mysqli_query($dbc,$query);
    if($q){
        return true;
    }
}


function change_customer_password($dbc,$customerID,$newPassword){
    $query = "UPDATE customer_tbl SET
        password = '$newPassword'
        WHERE id = '$customerID' 
    ";
    $q = mysqli_query($dbc,$query);
    if($q){
        return true;
    }
}
function change_customer_ussd_pin($dbc,$customerID,$newPin){
    $query = "UPDATE customer_tbl SET
        ussdPasscode = '$newPin'
        WHERE id = '$customerID'
    ";
    $q = mysqli_query($dbc,$query);
    if($q){
        return true;
    }
}

function get_all_customers($dbc){
    $query = "
        SELECT * FROM customer_tbl    
    ";
    $q = mysqli_query($dbc,$query);
    $data = array();
    while ($row = mysqli_fetch_assoc($q)){
        $data[] = $row;
    }
    return $data;
}
function get_all_customers_phoneNum($dbc){
    $query = "
        SELECT phoneNum FROM customer_tbl WHERE accountType = 2 OR accountType = 1    
    ";
    $q = mysqli_query($dbc,$query);
    $data = array();
    while ($row = mysqli_fetch_assoc($q)){
        $data[] = $row;
    }
    return $data;
}
function get_customer_by_id($dbc,$customerID){
     $query = "
        SELECT * FROM customer_tbl WHERE id = '$customerID'
    ";
     $q = mysqli_query($dbc,$query);
     $data = mysqli_fetch_assoc($q);
     return $data;
}
function get_customer_by_phoneNum($dbc,$phoneNum){
     $query = "SELECT * FROM customer_tbl WHERE phoneNum = '$phoneNum' OR phoneNum2 = '$phoneNum'
    ";
     $q = mysqli_query($dbc,$query);
     $data = mysqli_fetch_assoc($q);
     return $data;
}

function get_customer_by_email($dbc,$email){
     $query = "SELECT * FROM customer_tbl WHERE email = '$email' OR phoneNum = '$email' LIMIT 1  ";
     $q = mysqli_query($dbc,$query);
     $data = mysqli_fetch_assoc($q);
     return $data;
}


function upload_idCard($dbc,$img,$fileName,$phoneNum,$newFileName){

    $dir = "uploads/idcards/";
    $img = str_replace('data:image/jpg;base64,', '', $img);
    $img = str_replace(' ', '+', $img);
    $data = base64_decode($img);
    $file = $dir .$newFileName . '.jpg';
    $files = $newFileName . '.jpg';
    if(file_put_contents($file, $data)){
        $query = "UPDATE customer_tbl SET imageUrl = $files WHERE phoneNum = '$phoneNum' ";
        $q = mysqli_query($dbc,$query);
        if($q){
            return true;
        }
    }
}

function check_if_number_exist($dbc,$phoneNum){
    $query = "SELECT * FROM customer_tbl WHERE phoneNum = '$phoneNum' AND status = 'ACTIVE'";
    $q = mysqli_query($dbc,$query);
    if(mysqli_num_rows($q) > 0){
        return true;
    }
}

function check_if_email_exist($dbc,$email){
    $query = "SELECT * FROM customer_tbl WHERE email = '$email' ";
    $q = mysqli_query($dbc,$query);
    if(mysqli_num_rows($q) > 0){
        return true;
    }
}

function check_if_number_exist_as_merchant($dbc,$phoneNum){
    $query = "SELECT * FROM customer_tbl WHERE phoneNum = '$phoneNum' AND accountType = 3  AND status = 'ACTIVE'";
    $q = mysqli_query($dbc,$query);
    if(mysqli_num_rows($q) > 0){
        return true;
    }
}
function check_if_number_exist_as_individual($dbc,$phoneNum){
    $query = "SELECT * FROM customer_tbl WHERE phoneNum = '$phoneNum' AND accountType = 1  AND status = 'ACTIVE'";
    $q = mysqli_query($dbc,$query);
    if(mysqli_num_rows($q) > 0){
        return true;
    }
}


function check_if_number_exist_as_merchant_or_individual($dbc,$phoneNum){
    $query = "SELECT * FROM customer_tbl WHERE phoneNum = '$phoneNum' AND (accountType = 3 OR accountType = 1)  AND status = 'ACTIVE'";
    $q = mysqli_query($dbc,$query);
    if(mysqli_num_rows($q) > 0){
        return true;
    }
}

function check_if_number_exist_as_superAgent_or_agent_or_individual($dbc,$phoneNum){
    $query = "SELECT * FROM customer_tbl WHERE (accountType = 1 OR accountType = 2 OR accountType = 4) AND phoneNum = '$phoneNum'  AND status = 'ACTIVE' ";
    $q = mysqli_query($dbc,$query);
    if(mysqli_num_rows($q) > 0){
        return true;
    }
}

function check_if_number_exist_as_superAgent_or_agent_or_individual_or_merchant($dbc,$phoneNum){
    $query = "SELECT * FROM customer_tbl WHERE (accountType = 1 OR accountType = 2 OR accountType = 4 OR accountType = 3) AND phoneNum = '$phoneNum'  AND status = 'ACTIVE' ";
    $q = mysqli_query($dbc,$query);
    if(mysqli_num_rows($q) > 0){
        return true;
    }
}

function check_if_number_exist_as_agent($dbc,$phoneNum){
    $query = "SELECT * FROM customer_tbl WHERE (accountType = 2 OR accountType = 4) AND phoneNum = '$phoneNum'  AND status = 'ACTIVE'";
    $q = mysqli_query($dbc,$query);
    if(mysqli_num_rows($q) > 0){
        return true;
    }
}

function check_if_number_exist_as_superAgent($dbc,$phoneNum){
    $query = "SELECT * FROM customer_tbl WHERE  (accountType = 4 OR accountType = 6) AND phoneNum = '$phoneNum' AND status = 'ACTIVE'";
    $q = mysqli_query($dbc,$query);
    if(mysqli_num_rows($q) > 0){
        return true;
    }
}

function check_phoneNum_and_keyCode($dbc,$phoneNum,$keyCode){
    $query = "SELECT * FROM customer_tbl WHERE phoneNum = '$phoneNum' AND keyCode = '$keyCode'";
    $q = mysqli_query($dbc,$query);
    if(mysqli_num_rows($q) > 0){
        return true;
    }
}



function update_key_code($dbc,$keyCode,$phoneNum){
    $q = mysqli_query($dbc,"UPDATE customer_tbl SET keyCode = '$keyCode' WHERE phoneNum = '$phoneNum' OR email = '$phoneNum' ");
    if($q){
        return true;
    }
}

function get_customer_by_PhoneNum_and_keyCode($dbc,$phoneNum,$keyCode){
    $query = "SELECT * FROM customer_tbl WHERE phoneNum = '$phoneNum' AND keyCode = '$keyCode'  AND status = 'ACTIVE'";
    $q = mysqli_query($dbc,$query);
    return $data = mysqli_fetch_assoc($q);
}

function update_keyCode_variables($dbc,$keyCode,$phoneIMEI,$simSID,$simSerial,$phoneNum){
    $query = "
        UPDATE customer_tbl SET
          keyCode = '$keyCode',
          phoneIMEI = '$phoneIMEI',
          simSID = '$simSID',
          simSerial = '$simSerial'
          WHERE phoneNum = '$phoneNum'
    ";
    $q = mysqli_query($dbc,$query);
    if($q){
        return true;
    }
}

function update_ussdCode($dbc,$phoneNum,$ussdCode){
    $q = mysqli_query($dbc,"UPDATE customer_tbl SET ussdPasscode = '$ussdCode' WHERE phoneNum = '$phoneNum'");
    if($q){
        return true;
    }
}

function  get_all_from_uGroup($dbc){
    $q = mysqli_query($dbc,"SELECT * FROM ugroup_tbl");
    $data = array();
    while ($row = mysqli_fetch_assoc($q)){
        $data[] = $row;
    }
    return $data;
}

function get_uGroup_by_id($dbc,$id){
    $q = mysqli_query($dbc,"SELECT * FROM ugroup_tbl WHERE id = '$id'");
    $data = mysqli_fetch_assoc($q);
    return $data;
}
function get_all_from_accountType($dbc){
    $q = mysqli_query($dbc,"SELECT * FROM accounttype_tbl");
    $data = array();
    while ($row = mysqli_fetch_assoc($q)){
        $data[] = $row;
    }
    return $data;
}

function get_all_from_kyc($dbc){
    $q = mysqli_query($dbc,"SELECT * FROM kyc_tbl");
    $data = array();
    while ($row = mysqli_fetch_assoc($q)){
        $data[] = $row;
    }
    return $data;
}

function get_all_from_kyc_by_Id($dbc, $id){
    $q = mysqli_query($dbc,"SELECT * FROM kyc_tbl where id ='$id'");
    $data = mysqli_fetch_assoc($q);
    return $data;
}

function get_accountType_by_id($dbc,$id){
    $q = mysqli_query($dbc,"SELECT * FROM accounttype_tbl WHERE id = '$id'");
    $data = mysqli_fetch_assoc($q);
    return $data;
}

function add_new_accountType($dbc,$accountType){
    $q = mysqli_query($dbc,"INSERT INTO accounttype_tbl (accountType, added_by) VALUES ('$accountType','$_SESSION[uid]')");
    if($q){
        return true;
    }
}
function add_new_group($dbc,$groupName){
    $q = mysqli_query($dbc,"INSERT INTO ugroup_tbl (groupName, added_by) VALUES ('$groupName','$_SESSION[uid]')");
    if($q){
        return true;
    }
}

function get_all_service_providers($dbc){
    $q = mysqli_query($dbc,"SELECT * FROM service_providers_tbl");
    $data = array();
    while ($row = mysqli_fetch_assoc($q)){
        $data[] = $row;
    }
    return $data;
}

function update_country_code($dbc,$phoneNum,$countryCode,$currencyType){

    $q = mysqli_query($dbc,"UPDATE customer_tbl SET countryCode = '$countryCode', currencyType ='$currencyType' WHERE phoneNum = '$phoneNum'");
    if($q){
        return true;
    }
}

function update_countryCode_in_customeraccount($dbc,$countryCode,$id){
    $q = mysqli_query($dbc,"UPDATE customeraccount_tbl SET countryCode = '$countryCode' WHERE customerID = '$id'");
    if($q){
        return true;
    }
}
