<?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"];
    browse($party);
    $partycond = "";

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




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


    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><?php ECHO $fetparty[name]; ?></b></div>
            </div>
            <div class="row">
                <div class="col-lg-12 pad">
                    <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;

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


                            $getno = mysqli_num_rows($gettran);

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

                            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++;
                                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[subtot]</td>";
                                Echo "</tr>";


                                $tqty = $tqty + $gtran['qty'];
                                $tdisc = $tdisc + $gtran['discamt'];
                                $trate = $trate + $gtran['rate'];
                                $tamount = $tamount + $gtran['amount'];
                                $tnet = $tnet + $gtran['subtot'];
                            }
                            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>$trate</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 .= "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;
}
?>
