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

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = 'techfar_epi_root';

$year = $_GET['yr_f'];


$dcodey = $year;

$clen3 = strlen($dcodey);
$dcode3 = "";

for ($i3 = 0; $i3 < $clen3; $i3++) {

    $c13 = substr($dcodey, $i3, 1);
    $getmsp3 = mysqli_query($conn, "Select * from msp where code = '$c13'") or die(mysqli_error($conn));
    $fetmsp3 = mysqli_fetch_array($getmsp3);
    $codmsp3 = $fetmsp3['no'];

    $dcode3 = $dcode3 . $codmsp3;
}


$comp = mysqli_query($conn, "SELECT * FROM `company`") or die(mysql_errno());
$zcomp = mysqli_fetch_assoc($comp);

//browse('apps4rea_epi_1_1819');
mysqli_select_db($conn, 'techfar_epi_1_'.$dcode3);

$maindb = 'invmain';
$trandb = 'invtran';
//$zroot = $_SESSION["zdbroot"];
$formname = 'Invoice Form Print';
$vouchno = $_GET['v_qx_n'];
//$y = $_GET['y'];
$dcode = $vouchno;
$clen2 = strlen($dcode);
$dcode2 = "";
for ($i2 = 0; $i2 < $clen2; $i2++) {

    $c12 = substr($dcode, $i2, 1);
    $getmsp2 = mysqli_query($conn, "Select * from $zroot.msp where code = '$c12'") or die(mysqli_error($conn));
    $fetmsp2 = mysqli_fetch_array($getmsp2);
    $codmsp2 = $fetmsp2['no'];

    $dcode2 = $dcode2 . $codmsp2;
}



$para = $_GET['para'];

##############################Print Start Here##################################

if ($para == 'M') {
    $frmvouch = $_POST["frmvouch"];
    $tovouch = $_POST["tovouch"];
    $section = $_POST["section"];
    $session = $_POST["session"];
    $exam = $_POST["exam"];

    if (!empty($exam)) {
        $examcond = " AND `examcode` = '$exam' ";
    }

    if (!empty($session)) {
        $sessioncond = " AND `session` = '$session' ";
    }

    if (!empty($section)) {
        $sectioncond = " AND `section` = '$section' ";
    }

    if (!empty($voucher)) {
        $vouchnocond = " AND `vouchno` = '$voucher' ";
    }

    if (!empty($frmvouch)) {
        $rangecond = "and demdvouch between $frmvouch and $tovouch ";
    }
    $gmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as mydt  FROM `$maindb` WHERE `vouchno`!='' $rangecond $sectioncond 
                          $sessioncond $examcond") or die(mysqli_error($conn));
    $getno = mysqli_num_rows($gmain);

    if ($getno == 0) {
        browse("No Record Found");
        echo "<script>window.close()</script>";
    }
} else {
    $gmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as mydt FROM $maindb  WHERE vouchno='$dcode2' order by vouchno")
            or die(mysqli_error($conn));
}

include("includes/linkshead.php");
?>
<link href="MyStyle/rep_format.css" rel="stylesheet">
<style>

    body{
        font-size: 12px;

    }
    .mtable{
        border:1px solid black;
        boder-collapse:collapse;
    }
    .mtd{
        border:1px solid black;
        boder-collapse:collapse;
        text-align:right;
    }

    td,th,.t4{
        padding:2px;
        font-size:12px;
        font-size:12px;
    }

    table{
        width:100%;
    }
    
    .page{
        width:210mm;
        height:297mm;    
    }
    
    .subpage {
        height:276mm;
    }
    hr { 
        border-style: inset;
        border-width: 2px;
    } 
    .np { 
        padding: 0px;
    } 

    #tablenote td,th{
        padding:2px;
        font-size: 10px;
    }

</style>
</head>
<body>    
    <?php
    $ctr = 0;
    while ($fmain = mysqli_fetch_array($gmain)) {
        $section = $fmain['section'];
        $vouchno = $fmain['vouchno'];

        $gexam = mysqli_query($conn, "SELECT * FROM `exam` WHERE `code` = $fmain[examcode]");
        $shexam = mysqli_fetch_array($gexam);

        $gschool = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `grpcode` = '010' AND `accode` = $fmain[accode]") or die(mysqli_error($conn));
        $sschool = mysqli_fetch_array($gschool);

        if ($section == 'P') {
            $ssection = "Primary Section";
        } elseif ($section == 'S') {
            $ssection = "Secondary Section";
        } elseif ($section == 'M') {
            $ssection = "Prelim Section";
        }
        include('invoice_header.php');
        ?>


        <?php
        if ($section == 'S') {
            $totamt = $ctr1 = 0;
            $gtran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno` = '$vouchno' ") or die(mysqli_error($conn));
            while ($tran = mysqli_fetch_array($gtran)) {

                $srno2 = $tran['srno'];
                $pq5 = $tran['pq5'];
                $pq6 = $tran['pq6'];
                $pq7 = $tran['pq7'];
                $pq8 = $tran['pq8'];
                $pq9 = $tran['pq9'];
                $pq10 = $tran['pq10'];

                $rt5 = $tran['rt5'];
                $rt6 = $tran['rt6'];
                $rt7 = $tran['rt7'];
                $rt8 = $tran['rt8'];
                $rt9 = $tran['rt9'];
                $rt10 = $tran['rt10'];

                $totpq2 = $pq5 + $pq6 + $pq7 + $pq8 + $pq9 + $pq10;

                if ($totpq2 > 1) {
                    $ctr1++;
                    echo "<tr>";
                    echo "<td class='mtable'>$tran[subjname]</td>";
                    if ($pq5 > 0) {
                        echo "<td class='mtd'>$pq5 " . ' x ' . $rt5 . "</td>";
                    } else {
                        echo "<td class='mtd'></td>";
                    }
                    if ($pq6 > 0) {
                        echo "<td class='mtd'>$pq6 " . ' x ' . $rt6 . "</td>";
                    } else {
                        echo "<td class='mtd'></td>";
                    }
                    if ($pq7 > 0) {
                        echo "<td class='mtd'>$pq7 " . ' x ' . $rt7 . "</td>";
                    } else {
                        echo "<td class='mtd'></td>";
                    }
                    if ($pq8 > 0) {
                        echo "<td class='mtd'>$pq8 " . ' x ' . $rt8 . "</td>";
                    } else {
                        echo "<td class='mtd'></td>";
                    }
                    if ($pq9 > 0) {
                        echo "<td class='mtd'>$pq9 " . ' x ' . $rt9 . "</td>";
                    } else {
                        echo "<td class='mtd'></td>";
                    }
                    if ($pq10 > 0) {
                        echo "<td class='mtd'>$pq10 " . ' x ' . $rt10 . "</td>";
                    } else {
                        echo "<td class='mtd'></td>";
                    }
                    echo "<td class='mtd'>$tran[amount]</td>";
                    echo "</tr>";
                }
                $totamt = $totamt + $tran['amount'];
                if ($ctr1 > 22) {
                    include('invoice_footer.php');
                    ?>
                    <!--<br><br>-->
                  <?php
                    include('invoice_header.php');
                    $ctr1 = 0;
                }
                $brk = 1;
            }
        } elseif ($section == 'P') {
            $totamt = $ctr1 = 0;
            $gtran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno` = '$vouchno' ") or die(mysqli_error($conn));
            while ($tran = mysqli_fetch_array($gtran)) {

                $srno = $tran['srno'];
                $pq1 = $tran['pq1'];
                $pq2 = $tran['pq2'];
                $pq3 = $tran['pq3'];
                $pq4 = $tran['pq4'];
                $rt1 = $tran['rt1'];
                $rt2 = $tran['rt2'];
                $rt3 = $tran['rt3'];
                $rt4 = $tran['rt4'];
                $totpq = $pq1 + $pq2 + $pq3 + $pq4;

                if ($totpq > 1) {
                    $ctr1++;

                    echo "<tr>";
                    echo "<td class='mtable'>$tran[subjname]</td>";
                    if ($pq1 > 0) {
                        echo "<td class='mtd'>$pq1 " . ' x ' . $rt1 . "</td>";
                    } else {
                        echo "<td class='mtd'></td>";
                    }
                    if ($pq2 > 0) {
                        echo "<td class='mtd'>$pq2 " . ' x ' . $rt2 . "</td>";
                    } else {
                        echo "<td class='mtd'></td>";
                    }
                    if ($pq3 > 0) {
                        echo "<td class='mtd'>$pq3 " . ' x ' . $rt3 . "</td>";
                    } else {
                        echo "<td class='mtd'></td>";
                    }
                    if ($pq4 > 0) {
                        echo "<td class='mtd'>$pq4 " . ' x ' . $rt4 . "</td>";
                    } else {
                        echo "<td class='mtd'></td>";
                    }
                    echo "<td class='mtd'>$tran[amount]</td>";
                    echo "</tr>";
                }
                $totamt = $totamt + $tran['amount'];
                $brk = 1;
            }
        } elseif ($section == 'M') {
            $totamt = $ctr1 = 0;
            $gtran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno` = '$vouchno'  ") or die(mysqli_error($conn));
            while ($tran = mysqli_fetch_array($gtran)) {

                $srno3 = $tran3['srno'];
                $pqrelm = $tran['pqrelm'];
                if ($pqrelm > 0) {
                    echo "<tr>";
                    echo "<td class='mtable'>$tran[subjname]</td>";
                    echo "<td class='mtd'>$pqrelm</td>";
                    echo "<td class='mtd'>$tran[rtprelm]</td>";
                    echo "<td class='mtd'>$tran[amount]</td>";
                    echo "</tr>";
                    $ctr1++;
                }
                $totamt = $totamt + $tran[amount];
                $brk = 1;
            }
        }
//        include('invoice_footer.php');
        ?>
    </table>
    <?php
    include('invoice_footer.php');
//    echo "<br>";
//    echo "<br>";
}
include('includes/linksfoot.php');
?>
</body>
</html>
