<?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];
$maindb = "challan";
$zroot = $_SESSION['zdbroot'];
$zroot1 = substr($zroot, 0, -4);


$myyear = $_SESSION[$myyear];


$getcomp = mysqli_query($conn, "Select * from $zroot.compyear where finyear != '$myyear' order by lastdate DESC") or die(mysqli_error($conn));
$fetcomp = mysqli_fetch_array($getcomp);
$oldyear = $fetcomp['finyear'];
$olddb = $zroot1 . '1_' . $oldyear;



if ($_REQUEST["vouchdt"]) {

    $vouchdt = $_REQUEST["vouchdt"];
    $party = $_REQUEST["party"];
    $sizemm = $_REQUEST["sizemm"];
    $guagemm = $_REQUEST["guagemm"];
    $makemm = $_REQUEST["makemm"];

    $partycond = "";
    if ($party != '') {
        $partycond = "and b.`name`='$party'";
    }

    $vouchdtcond = "";
    if ($vouchdt != '') {
        $vouchdtcond = "and a.`vouchdt`!='$vouchdt'";
    }



    $query = "SELECT n.* from ( SELECT a.*, DATE_FORMAT(a.`vouchdt`,'%d-%m-%Y') as mydt, DATE_FORMAT(a.`delvdate`,'%d-%m-%Y') as delvdt, b.name  FROM `$maindb` a , accmast b
                         where a.party = b.accode and b.name = '$party' and a.sizemm='$sizemm' and a.guagemm='$guagemm' and a.makemm='$makemm' 
            union all
            SELECT a.*, DATE_FORMAT(a.`vouchdt`,'%d-%m-%Y') as mydt, DATE_FORMAT(a.`delvdate`,'%d-%m-%Y') as delvdt, b.name  FROM $olddb.`$maindb` a , $olddb.accmast b
                         where a.party = b.accode and b.name = '$party' and a.sizemm='$sizemm' and a.guagemm='$guagemm' and a.makemm='$makemm' ) n where vouchdt != '$vouchdt'
             order by  vouchdt DESC";
    $result = mysqli_query($conn, $query) or die(mysqli_error($conn));
    $query2 = "SELECT n.* from ( SELECT a.*, DATE_FORMAT(a.`vouchdt`,'%d-%m-%Y') as mydt, DATE_FORMAT(a.`delvdate`,'%d-%m-%Y') as delvdt, b.name  FROM `$maindb` a , accmast b
                         where a.party = b.accode and b.name != '$party' and a.sizemm='$sizemm' and a.guagemm='$guagemm' and a.makemm='$makemm' 
            union all
            SELECT a.*, DATE_FORMAT(a.`vouchdt`,'%d-%m-%Y') as mydt, DATE_FORMAT(a.`delvdate`,'%d-%m-%Y') as delvdt, b.name  FROM $olddb.`$maindb` a , $olddb.accmast b
                         where a.party = b.accode and b.name != '$party' and a.sizemm='$sizemm' and a.guagemm='$guagemm' and a.makemm='$makemm' ) n where vouchdt != '$vouchdt'
             order by  vouchdt DESC";
    $result2 = mysqli_query($conn, $query2) or die(mysqli_error($conn));
//    print table($result2);
    while ($r = mysqli_fetch_assoc($result)) {
        if ($r['cashcred'] == 'C') {
            $cashcred = 'Cash';
        } else {
            $cashcred = 'Credit';
        }

        echo "<tr style='font-size: 11px;
                                    padding:0px;
                                    '>" .
        "<td align='left'></td>" .
        "<td align='left'>" . $r[mark] . "</td>" .
        "<td align='left'>" . $r[mydt] . "</td>" .
        "<td align='left'>" . $r[delv] . "</td>" .
        "<td align='left'>" . $r[delvdt] . "</td>" .
        "<td align='left' class='partynametd'>" . $r[name] . "</td>" .
        "<td align='left' class='text-right'>" . $r[nag] . "</td>" .
        "<td align='left' class='text-right'>" . $r[sizemm] . "</td>" .
        "<td align='left' class='text-right'>" . $r[guagemm] . "</td>" .
        "<td align='left' class='text-right'>" . $r[makemm] . "</td>" .
        "<td align='left'>" . $r[dia] . "</td>" .
        "<td align='left'>" . $r[ap] . "</td>" .
        "<td align='left'>" . $r[weld] . "</td>" .
        "<td align='left'>" . $r[straight] . "</td>" .
        "<td align='left' class='text-right'>" . $r[foot] . "</td>" .
        "<td align='left' class='text-right'>" . $r[totfeet] . "</td>" .
        "<td align='left' class='text-right'>" . $r[weight] . "</td>" .
        "<td align='left' class='text-right' style='background-color:lightyellow'>" . $r[rate] . "</td>" .
        "<td align='left' class='text-right'>" . $r[amount] . "</td>" .
        "<td align='left'>" . $r[descrip] . "</td>" .
        "<td align='left'>" . $r[cashcred] . "</td>" .
//                    "<td align='left'>" . $r[cashvouch] . "</td>" .
        "<td align='left' class='gstperctd'>" . $r[gstperc] . "</td>" .
        "</tr>";
    }
    
    
    echo "<tr style='height:26px'><td colspan='18' class='text-center'><b style='color:red;font-size:19px'>Previous Year</b></td></tr>";
    
    while ($r2 = mysqli_fetch_assoc($result2)) {
        if ($r2['cashcred'] == 'C') {
            $cashcred = 'Cash';
        } else {
            $cashcred = 'Credit';
        }

        echo "<tr style='font-size: 11px;
                                    padding:0px;
                                    '>" .
        "<td align='left'></td>" .
        "<td align='left'>" . $r2[mark] . "</td>" .
        "<td align='left'>" . $r2[mydt] . "</td>" .
        "<td align='left'>" . $r2[delv] . "</td>" .
        "<td align='left'>" . $r2[delvdt] . "</td>" .
        "<td align='left' class='partynametd'>" . $r2[name] . "</td>" .
        "<td align='left' class='text-right'>" . $r2[nag] . "</td>" .
        "<td align='left' class='text-right'>" . $r2[sizemm] . "</td>" .
        "<td align='left' class='text-right'>" . $r2[guagemm] . "</td>" .
        "<td align='left' class='text-right'>" . $r2[makemm] . "</td>" .
        "<td align='left'>" . $r2[dia] . "</td>" .
        "<td align='left'>" . $r2[ap] . "</td>" .
        "<td align='left'>" . $r2[weld] . "</td>" .
        "<td align='left'>" . $r2[straight] . "</td>" .
        "<td align='left' class='text-right'>" . $r2[foot] . "</td>" .
        "<td align='left' class='text-right'>" . $r2[totfeet] . "</td>" .
        "<td align='left' class='text-right'>" . $r2[weight] . "</td>" .
        "<td align='left' class='text-right' style='background-color:lightgrey'>" . $r2[rate] . "</td>" .
        "<td align='left' class='text-right'>" . $r2[amount] . "</td>" .
        "<td align='left'>" . $r2[descrip] . "</td>" .
        "<td align='left'>" . $r2[cashcred] . "</td>" .
//                    "<td align='left'>" . $r2[cashvouch] . "</td>" .
        "<td align='left' class='gstperctd'>" . $r2[gstperc] . "</td>" .
        "</tr>";
    }
    
} else {
    echo "ERROR";
}
?>