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

$maindb = "salemain";
$trandb = "saletran";

$file = "partywisesale.php";
$formname = "Partywise Sale";

$zroot = $_SESSION["zdir"];

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

if (isset($_POST["view"])) {
    $party = $_POST["party"];
    $partycond = "";

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

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

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

    $gettran = mysqli_query($conn, "SELECT a.*, b.party from $trandb a, $maindb b where
                            a.vouchno = b.vouchno $partycond ") or die(mysqli_error($conn));

    $ptran = mysqli_fetch_array($gettran);

    $getparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$ptran[party]'") or die(mysqli_error($conn));
    $fetparty = mysqli_fetch_array($getparty);

    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));


    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital {
            font-style: italic;
        }

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }

        .right {
            text-align: right;
        }

        table td {
            font-size: 15px;
        }
    </style>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </head>

    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>
            <br>
            <!-- <div class="row">
                <div style="color:green" class="col-lg-12 col-md-12">Party&nbsp;: &nbsp;<b><?= $fetparty['name']; ?></b>
                </div>
            </div> -->
            <div class="row">
                <div class="col-lg-12">
                    <table class="table table-bordered">
                        <tr class="mySearchTr">
                            <td style="color: white;"><b>Party : <?php echo $fetparty['name']; ?></b></td>
                        </tr>
                    </table>
                    <table style="background: white;width: 100%" class="table table-condensed table-bordered table-hover">
                        <thead class="mySearchTr">

                            <tr>
                                <th style="width:5%">Sr</th>
                                <th style="width:15%">Item Code</th>
                                <th style="width:30%">Item Name</th>
                                <th style="width:10%" class="text-right">Qty</th>
                                <th style="width:10%" class="text-right">Rate</th>
                                <th style="width:10%" class="text-right">Amount</th>
                                <!-- <th style="width:10%" class="text-right">Disc</th>
                                <th style="width:10%" class="text-right">Net Amount</th> -->
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $tqty = 0;
                            $trate = 0;
                            $tdisc = 0;
                            $tamount = 0;
                            $tnet = 0;



                            $gettran2 = mysqli_query($conn, "SELECT a.*, b.party from $trandb AS a
                                                                    INNER JOIN $maindb AS b ON a.vouchno = b.vouchno
                                                                    where b.vouchno != '' $partycond ") or die(mysqli_error($conn));

                            $getno = mysqli_num_rows($gettran2);

                            if ($getno == 0) {
                                browse("No Record Found");
                                echo "<script>window.close()</script>";
                            }

                            while ($gtran = mysqli_fetch_array($gettran2)) {

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



                                $ctr++;
                                echo "<tr>";
                                echo "<td>$ctr</td>";
                                echo "<td>$gtran[itemcode]</td>";
                                echo "<td>$gtran[itemname]</td>";
                                echo "<td class='text-right'>$gtran[qty]</td>";
                                echo "<td class='text-right'>$gtran[rate]</td>";
                                echo "<td class='text-right'>$gtran[amount]</td>";
                                // Echo "<td class='text-right'>$gtran[discamt]</td>";
                                // Echo "<td class='text-right'>$gtran[billamt]</td>";
                                echo "</tr>";


                                $tqty = $tqty + $gtran['qty'];
                                $tdisc = $tdisc + $gtran['discamt'];
                                $trate = $trate + $gtran['rate'];
                                $tamount = $tamount + $gtran['amount'];
                                $tnet = $tnet + $gtran['billamt'];
                            }
                            echo "<tr>";
                            echo "<td ></td>";
                            echo "<td ></td>";
                            echo "<td style='color:red;' class='right'><b>Total</b></td>";
                            echo "<td class='right'><b>$tqty</b></td>";
                            echo "<td class='right'><b></b></td>";
                            echo "<td class='right'><b>$tamount</b></td>";
                            // Echo "<td class='right'><b>$tdisc</b></td>";
                            // Echo "<td class='right'><b>$tnet</b></td>";
                            echo "</tr>";
                            echo "</table>";
                            echo "</td>";

                            echo "<tr class='mySearchTr'>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            // Echo "<td></td>";
                            // Echo "<td></td>";
                            echo "</tr>";
                            echo "</table>";
                            echo "</td>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

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

    $partycond = "";

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

    if (!empty($party)) {
        $partycond = " AND `party`='$party' ";
    }
    if (empty($_POST["fdate"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; purreg.php");
    }
    if (empty($_POST["tdate"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; purreg.php");
    }


    $gettran = mysqli_query($conn, "SELECT a.*, b.party from $trandb a, $maindb b
                                             where a.vouchno = b.vouchno $partycond ") or die(mysqli_error($conn));

    $ptran = mysqli_fetch_array($gettran);

    $getparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$ptran[party]'") or die(mysqli_error($conn));
    $fetparty = mysqli_fetch_array($getparty);

    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));


    $contents .= "Party : $fetparty[name] \n";
    $contents .= "Sr, Item Code, Item Name, Qty, Rate, Amount, Disc, Net Amount \n";

    $ctr = 0;
    $tqty = 0;
    $trate = 0;
    $tamount = 0;
    $tnet = 0;

    $gettran = mysqli_query($conn, "SELECT a.*, b.party from $trandb a, $maindb b
                                             where a.vouchno = b.vouchno $partycond ") or die(mysqli_error($conn));
    while ($gtran = mysqli_fetch_array($gettran)) {

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

        $ctr++;
        $contents .= $ctr . ",";
        $contents .= $gtran["itemcode"] . ",";
        $contents .= $gtran["itemname"] . ",";
        $contents .= $gtran["qty"] . ",";
        $contents .= $gtran["rate"] . ",";
        $contents .= $gtran["amount"] . ",";
        $contents .= $gtran["discamt"] . ",";
        $contents .= $gtran["subtot"] . "\n";



        $tqty = $tqty + $gtran["qty"];
        $trate = $trate + $gtran["rate"];
        $tamount = $tamount + $gtran["amount"];
        $tnet = $tnet + $gtran["subtot"];
    }
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= "Total" . ",";
    $contents .= $tqty . ",";
    $contents .= $trate . ",";
    $contents .= $tamount . ",";
    $contents .= " " . ",";
    $contents .= $tnet . "\n";

    $contents = strip_tags($contents);

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