<?php

require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');

$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);

$maindb = 'latefees';
$accdb = 'actran';
$myyear = $_SESSION[$myyear];

$source = 'SR';
$purcode = '9999915';

$result = mysqli_query($conn, "SELECT * FROM `pax` order by vouchno asc") or die(mysqli_error($conn));
$zroot = $_SESSION["zdir"];

//print table($result);
//die();
$getnum = mysqli_num_rows($result);
//browse($getnum);
$mvouch = 0;
while ($get = mysqli_fetch_array($result)) {

//    $fname = $get['fname'];
    $accountname = $get['surname'] . ' ' . $get['name'] . ' ' . $get['midname'];

    $fmobile = $get['mobile'];

    $chkadd = mysqli_query($conn, "SELECT accode FROM $zroot.address WHERE name = '$accountname' and mobile = '$fmobile'") or die(mysqli_error($conn));
    $mnoad = mysqli_num_rows($chkadd);
    if ($mnoad <= 0) {
        
        $address = $get['address'];
        $add1 = substr($address,0,40);
        $add2 = substr($address,41,80);
        $add3 = substr($address,81,120);

        $gena = mysqli_query($conn, "SELECT MAX(accode) AS maxcode FROM  $zroot.address where accode != '' LIMIT 0,1 ") or die(mysqli_error($conn));
        if (mysqli_num_rows($gena) > 0) {
            $vvoucha = mysqli_fetch_assoc($gena);
            $qaccode = $vvoucha["maxcode"] + 1;
        } else {
            $qaccode = '51';
        }
        mysqli_query($conn, "INSERT INTO $zroot.address(`yearno`, `tour`, `regnno`, `grpcode`, `accode`, `name`, `nameprin`, 
                                                            `init`, `add1`, `add2`, `add3`, `city`, `pin`,
                                                            `tel`, `mobile`, `email`) 
                                                    VALUES ('2023', '$get[tourno]', '$get[vouchno]', '010', '$qaccode', '$accountname', '$accountname', 
                                                            'SDR', '$add1', '$add2', '$add3', '$get[city]', '$get[pin]',
                                                            '$get[tel]', '$get[mobile]', '$get[email]')") or die(mysqli_error($conn));
    }
}
browse("success");
?>