<?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];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);
$zroot = $_SESSION["zdbroot"];

$file = 'classrep_fr.php';

$supplier = $_POST["supplier"];
$architecture = $_POST["architecture"];
$salesmn = $_POST["salesman"];
$p = $_GET["p"];
$para = $_GET["para"];
$ptype = $_POST["ptype"];
$pchtype = $_POST["pchtype"];
$dtype = $_POST["dtype"];
$gsttype = $_POST["gsttype"];

if ($para == "MC") {
    $formname = 'Monthly Class Report';
} elseif ($para == "AW") {
    $formname = 'Agentwise Class Report';
} else {
    $formname = 'Class Report';
}
$saleexec = $_SESSION["saleexec"];
$usertype = $_SESSION["usertype"];
$username = strtoupper($_SESSION["username_" . $dbname]);
if ($usertype != 1) {
    $keycond1 = " and `salesman`='$saleexec'";
} else {
    $keycond1 = "";
}

if (!empty($supplier)) {
    $suppcond = " AND `party` = '$supplier' ";
    $suppcond2 = " AND `accode` = '$supplier' ";
}
if (!empty($p)) {
    $pcond = " AND `party` = '$p' ";
}
if (!empty($ptype)) {
    $ptypecond = " AND `paidby`= '$ptype'";
}
if (!empty($architecture)) {
    $archcond = " AND `architect`= '$architecture'";
}
if (!empty($salesmn)) {
    $slmncond = " AND `salesman`= '$salesmn'";
}

if ($ptype == "C") {
    $paytype = "CASH";
} else {
    $paytype = "CREDIT";
}

if (!empty($dtype)) {
    $dtypecond = " AND `dtype`= '$dtype' ";
}


if (!empty($gsttype)) {
    if ($gsttype == 1) {
        $gstcond = "AND `oms` = '1'";
    } else {
        $gstcond = "AND `oms` = ''";
    }
}



if (!empty($gsttype)) {
    if ($gsttype == 1) {
        $gstcond = "AND `oms` = '1'";
    } else {
        $gstcond = "AND `oms` = ''";
    }
} else {
    $gstcond = "";
}


$getpmain2 = mysqli_query($conn, "SELECT a.*,DATE_FORMAT(a.`vouchdt`,'%d-%m-%Y') as voucherdate FROM `bookform` a, visaform b where a.vouchno = b.vregnno
 $keycond1 $suppcond $ptypecond $archcond $gstcond $pcond $slmncond") or die(mysqli_error($conn));


$getno2 = mysqli_num_rows($getpmain2);

if (isset($_POST["view"]) || ($_GET["view"])) {
    include("includes/linkshead.php");
?>
    <style type="text/css">
        #tabital {
            font-style: italic;
        }

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }
    </style>
    </head>

    <body>
        <div class="container-fluid">
            <?php
            include("includes/head_rep.php");
            if ($getno2 > 0) {
            ?>
                <?php if ($ptype != "") { ?>
                    <table width="100%" class="table" style="background-color: #98F9F0">
                        <tr>
                            <td width="7%" style="font-size:14px">Pay Type : </td>
                            <td width="93%" style="font-size:14px"><b>//<?php echo $paytype; ?></b></td>
                        </tr>
                    </table>
                <?php } ?>
                <table style="background: white;width: 100%" class="table table-condensed table-striped table-hover table-bordered">
                    <thead class="mySearchTr">
                        <?php if ($para == "AW") { ?>
                            <tr>
                                <th width="3%;">Sr. No.</th>
                                <th width="5%;">Agent</th>
                                <th width="20%;">Month</th>
                                <th width="5%;">Class</th>
                                <th width="5%;">No. of Pax</th>
                                <th width="5%;">Net Amount</th>
                                <th width="10%;">Commission</th>
                                <th width="7%;">Bonus</th>
                            </tr>
                        <?php } else if ($para == "MC") { ?>
                            <tr>
                                <th width="20%;">Sr. No.</th>
                                <th width="20%;">Month </th>
                                <th width="20%;">Class</th>
                                <th width="20%;">No. Of Pax</th>
                                <th width="20%;">Net Amount</th>
                            </tr>
                        <?php } else { ?>
                            <tr>
                                <th width="3%;">Sr. No.</th>
                                <th width="5%;">Reg.</th>
                                <th width="20%;">Name</th>
                                <th width="5%;">Sex</th>
                                <th width="5%;">Passport No.</th>
                                <th width="5%;">Craft</th>
                                <th width="10%;">Agent</th>
                                <th width="7%;">MAKKA</th>
                                <th width="7%;">MADINA</th>
                                <th width="10%;">GROUP</th>
                            </tr>
                        <?php } ?>
                    </thead>
                    <tbody>
                        <?php
                        $pctr = 0;
                        $pchtypecond = "";

                        if ($para == "AW") {
                            $agentwise = mysqli_query($conn, "SELECT a.*,DATE_FORMAT(a.`vouchdt`,'%d-%m-%Y') as mydt, b.* FROM `bookform` a, visaform b where a.vouchno = b.vregnno
 $keycond1 $suppcond $ptypecond $archcond $gstcond $pcond $slmncond ") or die(mysqli_error($conn));

                            while ($fetagent = mysqli_fetch_array($agentwise)) {

                                $getmehram = mysqli_query($conn,"SELECT * FROM `mehram` where `vouchno`='$agetnwise[vouchno]' ") or die(mysqli_error($conn));
                                $mehno = mysqli_num_rows($getmehram);

                                $pctr++;
                                echo "<tr data-href='sale.php?mode=edit&vouchno=$voucher' class='clickable-row2' style='cursor: pointer;' [target]='_blank'>";
                                echo "<td>$pctr</td>";
                                echo "<td>$fetagent[agent]</td>";
                                echo "<td>$fetagent[mydt]</td>";
                                echo "<td>$fetagent[batchno]</td>";
                                echo "<td>$mehno</td>";
                                echo "<td>$fetagent[netamt]</td>";
                                echo "<td>$fetagent[agent]</td>";
                                echo "<td></td>";
                                echo "</tr>";
                            }
                        } else if ($para == "MC") {
                            while ($pmain = mysqli_fetch_array($getpmain2)) {

                                $getmehram2 = mysqli_query($conn,"SELECT * FROM `mehram` where `vouchno`='$pmain[vouchno]' ") or die(mysqli_error($conn));
                                $mehramno = mysqli_num_rows($getmehram2);
                                $pctr++;
                                echo "<tr data-href='sale.php?mode=edit&vouchno=$voucher' class='clickable-row2' style='cursor: pointer;' [target]='_blank'>";
                                echo "<td>$pctr</td>";
                                echo "<td>$pmain[voucherdate]</td>";
                                echo "<td>$pmain[batchno]</td>";
                                echo "<td>$mehramno</td>";
                                echo "<td>$pmain[netamt]</td>";
                                echo "</tr>";

                            }
                        } else {
                            while ($pmain = mysqli_fetch_array($getpmain2)) {

                                if ($pmain['paidby'] == "C") {
                                    $ptyped = "CASH";
                                } else {
                                    $ptyped = "CREDIT";
                                }

                                $voucher = $pmain['vouchno'];
                                $invno = $pmain['invno'];
                                $totalqty = $pmain['totalqty'];
                                $mydt = $pmain['mydt'];
                                $mobile = $pmain['mobile'];
                                $ntprint = $pmain['ntprint'];

                                if ($pmain['canc_flag'] == "C") {
                                    $partynm = "<b style='color:red'>CANCELLED</b>";
                                    $color = "red";
                                    $subtot = $totdisc = $totvat = $billamt = 0;
                                    $gstno = "";
                                    $billamt = $subtot = $totvat = $totdisc = $totgst = 0;
                                } else if ($pmain['canc_flag'] != "C") {
                                    $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[party]'") or die(mysqli_error($conn));
                                    $sparty = mysqli_fetch_array($gparty);



                                    $partynm = $sparty['name'];
                                    $billamt = $pmain['billamt'];
                                    $subtot = $pmain['grossamt'];
                                    $totvat = $pmain['packing'];
                                    $totgst = $pmain['addgst'];
                                    $totdisc = $pmain['totdisc'];
                                }
                                if ($pmain['cmbgender'] == "M") {
                                    $gender = "Male";
                                } else {
                                    $gender = "Female";
                                }

                                $garchitect = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[architect]'") or die(mysqli_error($conn));
                                $fetarch = mysqli_fetch_array($garchitect);
                                $architect = $fetarch['name'];

                                $gsales = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$pmain[salesman]'") or die(mysqli_error($conn));
                                $fetsale = mysqli_fetch_array($gsales);
                                $salesman = $fetsale['name'];


                                $k = 0;
                                $getq = mysqli_query($conn, "select * from bookform where vouchno = '$pmain[vregnno]'");

                                if (mysqli_num_rows($getq) > 0) {
                                    for ($i = 1; $i <= 7; $i++) {
                                        $mrm = 0;
                                        $fetq = mysqli_fetch_object($getq);

                                        // browse($i);
                                        switch ($i) {
                                            case "1";
                                                if ($fetq->mksingle != 0) {
                                                    $mrm = $fetq->mksingle;
                                                    $mk_rtype = 'SGL';
                                                }
                                                break;
                                            case "2";
                                                if ($fetq->mkdouble != 0) {
                                                    $mrm = $fetq->mkdouble;
                                                    $mk_rtype = 'DBL';
                                                }
                                                break;
                                            case "3";
                                                if ($fetq->mktriple != 0) {
                                                    $mrm = $fetq->mktriple;
                                                    $mk_rtype = 'TRP';
                                                }
                                                break;
                                            case "4";
                                                if ($fetq->mkquod != 0) {
                                                    $mrm = $fetq->mkquot;
                                                    $mk_rtype = 'QUAD';
                                                }
                                                break;
                                            case "5";
                                                if ($fetq->mkquint != 0) {
                                                    $mrm = $fetq->mkquint;
                                                    $mk_rtype = 'QUINT';
                                                }
                                                break;
                                            case "6";
                                                if ($fetq->mksuite != 0) {
                                                    $mrm = $fetq->mksuite;
                                                    $mk_rtype = 'SUITE';
                                                }
                                                break;
                                            case "7";
                                                if ($fetq->mknobed != 0) {
                                                    $mrm = $fetq->mknobed;
                                                    $mk_rtype = 'NOBED';
                                                }
                                                break;
                                            default:
                                                $mrm = 0;
                                                $mk_type = 0;
                                        }
                                        // for ($j = 1; $j <= $mrm; $j++) {
                                        //     $k = $k + 1;
                                        //     $pfld = 'p' + $k;
                                        //     $pfld = $mk_rtype;
                                        // }

                                        // if ($k >= $ppp) {
                                        //     return false;
                                        // }
                                    }
                                }

                                $a = 0;
                                $getmd = mysqli_query($conn, "select * from bookform where vouchno = '$pmain[vregnno]'");
                                if (mysqli_num_rows($getmd) > 0) {
                                    for ($b = 1; $b <= 7; $b++) {
                                        $mrm = 0;
                                        $fetmd = mysqli_fetch_object($getmd);
                                        switch ($b) {
                                            case "1";
                                                if ($fetmd->mdsingle != 0) {
                                                    $mrm = $fetmd->mdsingle;
                                                    $md_rtype = 'SGL';
                                                }
                                                break;
                                            case "2";
                                                if ($fetmd->mddouble != 0) {
                                                    $mrm = $fetmd->mddouble;
                                                    $md_rtype = 'DBL';
                                                }
                                                break;
                                            case "3";
                                                if ($fetmd->mdtriple != 0) {
                                                    $mrm = $fetmd->mdtriple;
                                                    $md_rtype = 'TRP';
                                                }
                                                break;
                                            case "4";
                                                if ($fetmd->mdquod != 0) {
                                                    $mrm = $fetmd->mmdquot;
                                                    $md_rtype = 'QUAD';
                                                }
                                                break;
                                            case "5";
                                                if ($fetmd->mdquint != 0) {
                                                    $mrm = $fetmd->mdquint;
                                                    $md_rtype = 'QUINT';
                                                }
                                                break;
                                            case "6";
                                                if ($fetmd->mdsuite != 0) {
                                                    $mrm = $fetmd->mdsuite;
                                                    $md_rtype = 'SUITE';
                                                }
                                                break;
                                            case "7";
                                                if ($fetmd->mdnobed != 0) {
                                                    $mrm = $fetmd->mdnobed;
                                                    $md_rtype = 'NOBED';
                                                }
                                                break;
                                            default:
                                                $mrm = 0;
                                                $md_type = 0;
                                        }

                                        // for ($j = 1; $j <= $mrm; $j++) {
                                        //     $k = $k + 1;
                                        //     $pfld = 'p' + $k;
                                        //     $pfld = $md_rtype;
                                        // }

                                        // if ($k >= $ppp) {
                                        //     return false;
                                        // }
                                    }
                                }

                                $pctr++;
                                echo "<tr data-href='sale.php?mode=edit&vouchno=$voucher' class='clickable-row2' style='cursor: pointer;' [target]='_blank'>";
                                echo "<td>$pctr</td>";
                                echo "<td>$pmain[vregnno]</td>";
                                echo "<td>$pmain[lastname]</td>";
                                echo "<td>$gender</td>";
                                echo "<td>$pmain[redypass]</td>";
                                echo "<td>$pmain[craft]</td>";
                                echo "<td>$pmain[agent]</td>";
                                echo "<td>$mk_rtype</td>";
                                echo "<td>$md_rtype</td>";
                                echo "<td>$pmain[groupno]</td>";
                                echo "</tr>";


                                $pgrtotqty = $pgrtotqty + $totalqty;
                                $pgrsubtot = $pgrsubtot + $subtot;
                                $pgrtotdis = $pgrtotdis + $totdisc;
                                $pgrtotvat = $pgrtotvat + $totvat;
                                $pgrtotgst = $pgrtotgst + $totgst;
                                $pgrtotbillamt = $pgrtotbillamt + $billamt;
                            }
                        }
                        ?>
                    </tbody>
                </table>
            <?php
            }
            ?>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>

    </html>
<?php
}
if (isset($_POST["excel"])) {

    //                $getpmain2 = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `salemain` WHERE `vouchdt` >= '$mfrmdate' 
    //                                            AND `vouchdt` <= '$mtodate' $suppcond $ptypecond $archcond $gstcond $pcond order by vouchdt,vouchno")or die(mysqli_error($conn));
    //    
    //    
    //                $getno2 = mysqli_num_rows($getpmain2);

    $contents .= "Sr,Voucher,Date,Mobile,Invoice,Party,Name To Print,Architect Name,Salesman,Paid By,Total Qty,Net Amount\n";

    $pctr = 0;
    $ptotamt = 0;
    $ptotdiscamt = 0;
    $ptottaxable = 0;
    $ptotgst3 = 0;
    $ptotgst5 = 0;
    $ptotgst12 = 0;
    $ptotgst18 = 0;
    $ptotgst28 = 0;
    $ptotpackamt = $ptotstamt = $ptotothamt = $ptotbillamt = 0;
    $pchtypecond = "";

    while ($pmain = mysqli_fetch_array($getpmain2)) {


        if ($pmain['paidby'] == "C") {
            $ptyped = "CASH";
        } else {
            $ptyped = "CREDIT";
        }

        $voucher = $pmain['vouchno'];
        $invno = $pmain['invno'];
        $totalqty = $pmain['totalqty'];
        $mydt = $pmain['mydt'];
        $mobile = $pmain['mobile'];
        $ntprint = $pmain['ntprint'];

        if ($pmain['canc_flag'] == "C") {
            $partynm = $contents .= "CANCELLED" . ",";
            $color = "red";
            $subtot = $totdisc = $totvat = $billamt = 0;
            $gstno = "";
            $billamt = $subtot = $totvat = $totdisc = $totgst = 0;
        } else if ($pmain['canc_flag'] != "C") {
            $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[party]'") or die(mysqli_error($conn));
            $sparty = mysqli_fetch_array($gparty);



            $partynm = $sparty['name'];
            $billamt = $pmain['billamt'];
            $subtot = $pmain['grossamt'];
            $totvat = $pmain['packing'];
            $totgst = $pmain['addgst'];
            $totdisc = $pmain['totdisc'];
        }

        $garchitect = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[architect]'") or die(mysqli_error($conn));
        $fetarch = mysqli_fetch_array($garchitect);
        $architect = $fetarch['name'];

        $gsales = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$pmain[salesman]'") or die(mysqli_error($conn));
        $fetsale = mysqli_fetch_array($gsales);
        $salesman = $fetsale['name'];


        $pctr++;

        $contents .= "$pctr" . ",";
        $contents .= "$voucher" . ",";
        $contents .= "$mydt" . ",";
        $contents .= "$mobile" . ",";
        $contents .= "$invno" . ",";
        $contents .= "$partynm" . ",";
        $contents .= "$ntprint" . ",";
        $contents .= "$architect" . ",";
        $contents .= "$salesman" . ",";
        $contents .= "$ptyped" . ",";
        $contents .= "$totalqty" . ",";
        $contents .= "$billamt" . "\n";

        $pgrtotqty = $pgrtotqty + $totalqty;
        $pgrsubtot = $pgrsubtot + $subtot;
        $pgrtotdis = $pgrtotdis + $totdisc;
        $pgrtotvat = $pgrtotvat + $totvat;
        $pgrtotgst = $pgrtotgst + $totgst;
        $pgrtotbillamt = $pgrtotbillamt + $billamt;

        if ($_POST["dtype"] == 2) {

            $contents .= "Sr,Qty.,Unit,Size,Rate,Amount,Item,Delivery Date\n";

            $ctr2 = 0;
            $pctr2 = 0;
            $pcartqty = $pqty = $ptotqty = $ptotamount = $ptotdiscount = $ptotvat = $ptotal = 0;

            $gettran = mysqli_query($conn, "SELECT * FROM `saletran` WHERE `vouchno`='$pmain[vouchno]' order by srno") or die(mysqli_error($conn));
            while ($tran = mysqli_fetch_array($gettran)) {

                $ctr2++;
                $contents .= "$ctr2" . ",";
                $contents .= "$tran[qty]" . ",";
                $contents .= "$tran[unit]" . ",";
                $contents .= "$tran[size]" . ",";
                $contents .= "$tran[rate]" . ",";
                $contents .= "$tran[amount]" . ",";
                $contents .= "$tran[itemname]" . ",";
                $contents .= "$tran[delivery_date]" . ",";

                $pcartqty = $pcartqty + $tran['cartqty'];
                $pqty = $pqty + $tran['qty'];
                $ptotqty = $ptotqty + $tran['totqty'];
                $ptotamount = $ptotamount + $tran['amount'];
                $ptotdiscount = $ptotdiscount + $tran['discamt'];
                $ptotvat = $ptotvat + $tran['vat'];
                $ptotal = $ptotal + $tran['total'];
            }

            $contents .= "Total" . ",";
            $contents .= "$pqty" . ",";
            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "" . ",";
            $contents .= "$ptotamount" . ",";
            $contents .= "" . ",";
            $contents .= "" . "\n";
        }
    }
    if ($ptype != "") {
    }
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= "$pgrtotqty" . ",";
    $contents .= "$pgrtotbillamt" . "\n";


    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1 || $_POST["dtype"] == 3) {
        header("Content-Disposition: attachment;filename = $formname" . "_Summary" . date('d-m-Y') . ".csv");
        print $contents;
    } else {
        header("Content-Disposition: attachment;filename = $formname" . "_Detail" . date('d-m-Y') . ".csv");
        print $contents;
    }
}
?>