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

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

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($salesman)) {
    $salescond = " AND `salesman`= '$salesman'";
}
if (!empty($architecture)) {
    $archcond = " AND `architect`= '$architecture'";
}

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

$formname = 'Pending Delivery';


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; pendlv.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; pendlv.php");
    }
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
}

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

            $getpmain2 = mysqli_query($conn, "SELECT a.vouchno,a.party,a.salesman ,DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,b.* FROM `salemain` a, saletran b WHERE a.vouchno = b.vouchno AND a.`vouchdt` >= '$mfrmdate' 
                                        AND a.`vouchdt` <= '$mtodate' AND b.delvdate = '0000-00-00' $suppcond $ptypecond $archcond $gstcond $pcond $salescond order by a.vouchdt,b.srno")or die(mysqli_error($conn));


            $getno2 = mysqli_num_rows($getpmain2);


            if ($getno2 > 0) {
                ?>
                <div class="row">
                    <div class="col-lg-12 pad">
                        <table class="table table-condesed table-bordered">
                            <tr class="mySearchTr">
                                <!--9221292520-->
                                <th width="100%" class="h4" style="color:white"><b><?= $formname ?></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. No.</th>
                                    <th width="5%;" class="text-right">Qty.</th>
                                    <th width="7%;" class="text-right">Unit</th>
                                    <th width="8%;" class="text-right">Size</th>
                                    <th width="7%;" class="text-right">Rate</th>
                                    <th width="7%;" class="text-right">Amount</th>                                
                                    <th width="10%;">Item</th>
                                    <th width="10%;">Party</th>
                                    <th width="10%;">Sales Executive</th>
                                    <th width="7%;">Date</th>
                                    <th width="5%;">Voucher No.</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 = "";

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

                                    $voucher = $pmain['vouchno'];
                                    $qty = $pmain['qty'];
                                    $unit = $pmain['unit'];
                                    $size = $pmain['size'];
                                    $rate = $pmain['rate'];
                                    $amount = $pmain['amount'];
                                    $itemname = $pmain['itemname'];
                                    $mydt = $pmain['mydt'];

                                    $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[party]'") or die(mysqli_error($conn));
                                    $sparty = mysqli_fetch_array($gparty);
                                    $party = $sparty['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>";
                                    Echo"<td>$pctr</td>";
                                    Echo"<td class='text-right'>$qty</td>";
                                    Echo"<td class='text-right'>$unit</td>";
                                    Echo"<td class='text-right'>$size</td>";
                                    Echo"<td class='text-right'>$rate</td>";
                                    Echo"<td class='text-right'>$amount</td>";
                                    Echo"<td>$itemname</td>";
                                    Echo"<td>$party</td>";
                                    Echo"<td>$salesman</td>";
                                    Echo"<td>$mydt</td>";
                                    Echo"<td>$voucher</td>";
                                    Echo"</tr>";


                                    $tqty = $tqty + $qty;
                                    $tamount = $tamount + $amount;
                                }
                                $ptotqty = $ptotqty + $tqty;
                                $ptotamount = $ptotamount + $tamount;
                                $totamt = number_format($ptotamount, 2);
                                Echo "<tr style='font-weight:600'>";
                                Echo "<td>Total</td>";
                                Echo "<td class='text-right'>$ptotqty</td>";
                                Echo "<td colspan='3'></td>";
                                Echo "<td class='text-right'>$totamt</td>";
                                Echo "<td colspan='5'></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 a.vouchno,a.party,a.salesman ,DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,b.* FROM `salemain` a, saletran b WHERE a.vouchno = b.vouchno AND a.`vouchdt` >= '$mfrmdate' 
                                        AND a.`vouchdt` <= '$mtodate' AND b.delvdate = '0000-00-00' $suppcond $ptypecond $archcond $gstcond $pcond order by a.vouchdt,b.srno")or die(mysqli_error($conn));


    $getno2 = mysqli_num_rows($getpmain2);


    if ($getno2 > 0) {

        $contents .= "Sr,Qty,Unit,Size,Rate,Amount,Item,Party,Salesman,Date,Voucher\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)) {

            $voucher = $pmain['vouchno'];
            $qty = $pmain['qty'];
            $unit = $pmain['unit'];
            $size = $pmain['size'];
            $rate = $pmain['rate'];
            $amount = $pmain['amount'];
            $itemname = $pmain['itemname'];
            $mydt = $pmain['mydt'];

            $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[party]'") or die(mysqli_error($conn));
            $sparty = mysqli_fetch_array($gparty);
            $party = $sparty['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 .= "$qty" . ",";
            $contents .= "$unit" . ",";
            $contents .= "$size" . ",";
            $contents .= "$rate" . ",";
            $contents .= "$amount" . ",";
            $contents .= "$itemname" . ",";
            $contents .= "$party" . ",";
            $contents .= "$salesman" . ",";
            $contents .= "$mydt" . ",";
            $contents .= "$voucher" . "\n";


            $tqty = $tqty + $qty;
            $tamount = $tamount + $amount;
        }
        $ptotqty = $ptotqty + $tqty;
        $ptotamount = $ptotamount + $tamount;

        $contents .= "Total" . ",";
        $contents .= "$ptotqty" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "$ptotamount" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . "\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;
    }
}
?>