<?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 = 'salereg_fr.php';

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

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";
}

$formname = 'Sale Register';

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


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

if (($_GET["fdt"]) != '') {
    $mfdt = $_GET["fdt"];
    $mtdt = $_GET["tdt"];
    $mfrmdate = date("y-m-d", strtotime($mfdt));
    $mtodate = date("y-m-d", strtotime($mtdt));
    $dtype = $_GET["rpt"];
} else {

    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; salereg.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; salereg.php");
    }
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
}

if (!empty($gsttype)) {
    if ($gsttype == 1) {
        $gstcond = "AND `oms` = '1'";
    } else {
        $gstcond = "AND `oms` = ''";
    }
} else {
    $gstcond = "";
}
            $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 $slmncond order by vouchdt,vouchno")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_rep2.php");

//browse (1); 

            if ($getno2 > 0) {
                ?>
                <div class="row">
                    <div class="col-lg-12 pad">
                        <?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 class="table table-condesed table-bordered">
                            <tr class="mySearchTr">
                                <!--9221292520-->
                                <th width="100%" class="h4" style="color:white"><b>Sale Register</b></th>
                            </tr>
                            <tr>
                                <td style="font-size: 15px;padding:5px" class="HDFont" colspan="2">
                                    Total No. of Record :&nbsp;&nbsp;<b><?php echo $getno2; ?></b>
                                </td>
                            </tr>
                        </table>
                        <table style="background: white;width: 100%" class="table table-condensed table-striped table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th width="3%;">Sr</th>
                                    <th width="5%;">Voucher</th>
                                    <th width="7%;">Date</th>
                                    <th width="7%;">Mobile</th>
                                    <th width="5%;">Invoice</th>
                                    <th width="22%;">Party</th>                                
                                    <th width="12%;">Name To Print</th>
                                    <th width="7%;">Architect Name</th>
                                    <th width="10%;">Salesman</th>
                                    <th width="7%;">Paid By</th>
                                    <th width="7%;" class="text-right">Total Qty</th>
                                    <th width="10%;" class="text-right">Net Amount</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $pctr = 0;
                                $ptotamt = 0;
                                $ptotdiscamt = 0;
                                $ptottaxable = 0;
                                $ptotgst3 = 0;
                                $ptotgst5 = 0;
                                $ptotgst12 = 0;
                                $ptotgst18 = 0;
                                $ptotgst28 = 0;
                                $ptotpackamt = $ptotstamt = $ptotothamt = $ptotbillamt = 0;
                                $pchtypecond = "";

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


                                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'];
                                    }

                                    $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++;
                                    Echo"<tr data-href='sale.php?mode=edit&vouchno=$voucher' class='clickable-row2' style='cursor: pointer;' [target]='_blank'>";
                                    Echo"<td>$pctr</td>";
                                    Echo"<td>$voucher</td>";
                                    Echo"<td>$mydt</td>";
                                    Echo"<td>$mobile</td>";
                                    Echo"<td>$invno</td>";
                                    Echo"<td>$partynm</td>";
                                    Echo"<td>$ntprint</td>";
                                    Echo"<td>$architect</td>";
                                    Echo"<td>$salesman</td>";
                                    Echo"<td>$ptyped</td>";
                                    Echo"<td class='text-right'>$totalqty</td>";
                                    Echo"<td class='text-right'>$billamt</td>";
                                    Echo"</tr>";


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

                                    if ($_POST["dtype"] == 2) {
                                        Echo "<tr style='height:10px;'>";
                                        Echo "<td colspan='25'>";
                                        Echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                        Echo "<thead>";
                                        Echo "<tr class='' style='background-color:#BABFC2'>";
                                        Echo "<th style='width:5%;'>Sr</th>";
                                        Echo "<th style='width:9%;'  class='text-right'>Qty.</th>";
                                        Echo "<th style='width:9%;'  class='text-right'>Unit</th>";
                                        Echo "<th style='width:15%;' class='text-right'>Size</th>";
                                        Echo "<th style='width:10%;' class='text-right'>Rate</th>";
                                        Echo "<th style='width:20%;'>Amount</th>";
                                        Echo "<th style='width:17%;'>Item</th>";
                                        Echo "<th style='width:15%;'>Delivery Date</th>";
                                        Echo "</tr>";
                                        Echo "<thead>";
                                        Echo "<tr>";

                                        $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++;
                                            Echo "<tr>";
                                            Echo "<td>$ctr2</td>";
                                            Echo "<td class='text-right'>$tran[qty]</td>";
                                            Echo "<td class='text-right'>$tran[unit]</td>";
                                            Echo "<td class='text-right'>$tran[size]</td>";
                                            Echo "<td class='text-right'>$tran[rate]</td>";
                                            Echo "<td class='text-right'>$tran[amount]</td>";
                                            Echo "<td>$tran[itemname]</td>";
                                            Echo "<td>$tran[delivery_date]</td>";
                                            Echo "</tr>";

                                            $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'];
                                        }
                                        Echo "<tr style='background-color:#d8e0e6'>";
                                        Echo "<td>Total</td>";
                                        Echo "<td class='text-right'>$pqty</td>";
                                        Echo "<td colspan='3'></td>";
                                        Echo "<td class='text-right'>$ptotamount</td>";
                                        Echo "<td colspan='2'></td>";
                                        Echo "</tr>";
                                        Echo "</table>";
                                    }
                                }
                                if ($ptype != "") {
                                    
                                }
                                Echo "<tr class='' style='background-color: #98F9F0'>";
                                Echo "<td><b></b></td>";
                                Echo "<td><b></b></td>";
                                Echo "<td><b></b></td>";
                                Echo "<td><b></b></td>";
                                Echo "<td><b></b></td>";
                                Echo "<td><b></b></td>";
                                Echo "<td><b></b></td>";
                                Echo "<td><b></b></td>";
                                Echo "<td><b></b></td>";
                                Echo "<td class='text-right' style='font-size:15px'><b>Total</b></td>";
                                Echo "<td class='numeric text-right' style='font-size:15px'><b>$pgrtotqty</b></td>";
                                Echo "<td class='numeric text-right' style='font-size:15px'><b>$pgrtotbillamt</b></td>";
                                Echo "</tr>";
                                ?>

                            </tbody>
                        </table>
                    </div> 
                </div>

                <?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;
    }
}
?>