<?php

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

$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
$admin = $_SESSION["admin_" . $dbname];
$success = false;
$zroot = $_SESSION['zdir'];
// $file = "bank.php";
$myyear = $_SESSION[$myyear];

$frmvouch = $_REQUEST["frmvouch"];
$tovouch = $_REQUEST["tovouch"];
$exam = $_REQUEST["exam"];
$session = $_REQUEST["session"];
$section = $_REQUEST["section"];


if($frmvouch != ''  && $tovouch != ''){
    $vouchcond = "AND `demdvouch` BETWEEN '$frmvouch' and '$tovouch' ";
}
if(!empty ($exam)){
    $examcond = "AND `examcode` = '$exam' " ;
}
if(!empty ($session)){
    $sesscond = "AND `session` = '$session' " ;
}
if(!empty ($section)){
    $sectcond = "AND `section` = '$section' " ;
}
echo $vouchcond ;

$query = "SELECT `custcode`, `accode`, `vouchno`, `demdvouch`, `examcode`, `session`, `section`, `examcode` FROM `invmain`
          WHERE `vouchno` != '' $vouchcond $examcond $sesscond $sectcond";
$result = mysqli_query($conn, $query)or die(mysqli_error($conn));

echo mysqli_num_rows($result);

$ctr = 0;
while ($r = mysqli_fetch_assoc($result)) {

    $custcode = $r['custcode'];
    $schl = $r['accode'];
    $vouchno = $r['vouchno'];
    $demdvouch = $r['demdvouch'];

    $convmsp = (string) $vouchno;
    $clen = strlen($convmsp);
    
    $dcode = "";
    
    for ($i = 0; $i < $clen; $i++) {
        $c1 = substr($convmsp, $i, 1);
        $getmsp = mysqli_query($conn, "Select * from $zroot.msp where no = '$c1'") or die(mysqli_error($conn));
        $fetmsp = mysqli_fetch_array($getmsp);
        $codmsp = $fetmsp['code'];
        $dcode = $dcode . $codmsp;
    }
    
    $convmsp2 = (string) $myyear;
    $clen2 = strlen($convmsp2);
    
    $dcode2 = "";
    
    for ($i2 = 0; $i2 < $clen2; $i2++) {
        $c12 = substr($convmsp2, $i2, 1);
        $getmsp2 = mysqli_query($conn, "Select * from $zroot.msp where no = '$c12'") or die(mysqli_error($conn));
        $fetmsp2 = mysqli_fetch_array($getmsp2);
        $codmsp2 = $fetmsp2['code'];
        $dcode2 = $dcode2 . $codmsp2;
    }

    $getschl = mysqli_query($conn, "SELECT `accode`, `name`, `email`, `mobile` FROM `accmast` WHERE `accode`='$schl' ")or die(mysqli_error($conn));
    $fschool = mysqli_fetch_array($getschl);

    $email = $fschool['email'];


    

   $emailid = $email;
$from = "rajcomputers@hotmail.com";
$to = $emailid;
$subject = 'INVOICE FROM MAHARASHTRA BHARATEEYA SHIKSHAN MANDAL';

$headers = "From: MBSM\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$message = 'Reply at :mbsmandal@gmail.com';
$message .= '<html><body>';
$message .= '<h1></h1>';
$message .= '</body></html>';

$message .= '<html><body>';

$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . 'MAHARASHTRA BHARATEEYA SHIKSHAN MANDAL' . "</td></tr>";

//        $message .= "<tr><td><strong>COPY LINK TO DOWNLOAD OR PRINT INVOICE:</strong> </td><td>" . 'http://apps4real.com/epiweb/invoice_print_mail.php?v_qx_a=' . 'siez' . '&yr_i=' . 'nddl' . '&v_qx_n=' . $dcode . '&yr_f=' . $dcode2 . '&v_qx_o=' . 'sief' . '&yr_u=' . 'mkln' . "</td></tr>";
$message .= "<tr><td><strong>COPY LINK TO DOWNLOAD OR PRINT INVOICE:</strong> </td><td><a href='http://jazofaz.com/epiweb/invmult_print_mail.php?v_qx_a=siez&yr_i=nddl&v_qx_n=$dcode&yr_f=$dcode2&v_qx_o=sief&yr_u=mkln'>Click To View Invoice</a></td></tr>";
$addURLS = $_POST['addURLS'];
if (($addURLS) != '') {
    $message .= "<tr><td><strong>URL To Change (additional):</strong> </td><td>" . strip_tags($addURLS) . "</td></tr>";
}
$curText = htmlentities($_POST['curText']);
if (($curText) != '') {
    $message .= "<tr><td><strong>CURRENT Content:</strong> </td><td>" . $curText . "</td></tr>";
}

$message .= "</table>";
$message .= "</body></html>";

if (mail($to, $subject, $message, $headers)) {
    echo "Mail Send";
} else {
    echo "Invalid Email Id";
}

    $ctr++;

}
header("Content-type: application\json");
echo json_encode($rows);
