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

$maindb = "purchsale";
$trandb = "";
$formname = "Purchase Sale Register";
$file = "pursalereg_fr.php";

$zroot = $_SESSION["zdir"];
//browse($zroot);

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

if (isset($_POST["view"])) {
    $fdate = $_POST["fdate"];
    $tdate = $_POST["tdate"];
    $supplier = $_POST["supplier"];
    $customer = $_POST["customer"];

    $dtypecond = "";
    $mscopecond = "";

    $mfrmdate = trim($_POST["fdate"]);
    $mtodate = trim($_POST["tdate"]);

    if($supplier){
        $suppcond = "AND `supplier`='$supplier'";
    }

    if($customer){
        $custcond = "AND `customer`='$customer'";
    }


    if (empty($_POST["fdate"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; trnfreg.php");
    }
    if (empty($_POST["tdate"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; trnfreg.php");
    }

    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' $custcond $suppcond order by vouchdt ASC")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);

//browse($get);
    include("includes/linkshead.php");
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
    </style>

    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-condensed table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:3%">Sr</th>
                                <th style="width:8%">Voucher</th>
                                <th style="width:8%">Date</th>
                                <th style="width:15%">Supplier</th>
                                <th style="width:8%" class="text-right">Purchase Qty.</th>
                                <th style="width:8%" class="text-right">Purchase Rate</th>
                                <th style="width:8%" class="text-right">Purchase Amount</th>
                                <th style="width:15%">Customer</th>
                                <th style="width:8%" class="text-right">Sale Rate</th>
                                <th style="width:8%" class="text-right">Sale Amount</th>
                                <th style="width:10%;" class="text-right">Difference</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $mtqty = 0;
                            $mgtot = 0;
                            $tpamt = $tsamt = $tdamt = 0;

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

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

                                $getcust = mysqli_query($conn, "SELECT `name` from `accmast` WHERE `accode`='$pmain[customer]' ")or die(mysqli_error($conn));
                                $cust = mysqli_fetch_array($getcust);


                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[vouchno]</td>";
                                Echo "<td>$pmain[mydt]</td>";
                                Echo "<td>$supp[name]</td>";
                                Echo "<td class='text-right' >$pmain[purqty]</td>";
                                Echo "<td class='text-right' >$pmain[purrate]</td>";
                                Echo "<td class='text-right' >$pmain[puramt]</td>";
                                Echo "<td>$cust[name]</td>";
                                Echo "<td class='text-right' >$pmain[salerate]</td>";
                                Echo "<td class='text-right' >$pmain[saleamt]</td>";
                                Echo "<td class='text-right' >$pmain[diffamt]</td>";
                                $tpamt = $pmain['puramt'] + $tpamt;
                                $tsamt = $pmain['saleamt'] + $tsamt;
                                $tdamt = $pmain['diffamt'] + $tdamt;

                                Echo "</tr>";
                            }

                            echo "<tr> ";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<th class='text-right' style='color: red;'>Grand Total</td>";
                            echo "<th class='text-right' style='color: red;'>$tpamt</td>";
                            echo "<td></td>";
                            echo "<td></td>";
//                            echo "<td></td>";
                            echo "<th class='text-right' style='color: red;'>$tdamt</td>";
                            echo "<th class='text-right' style='color: red;'>$tsamt</td>";
//                            echo "<td></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
?>