<?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 = 'kataptti_fr.php';
$maindb = 'purmain';
$trandb = 'purtran';

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

if (!empty($supplier)) {
    $suppcond = " AND `party` = '$supplier' ";
    $suppcond2 = " AND `accode` = '$supplier' ";
}
if (!empty($p)) {
    $pcond = " AND `party` = '$p' ";
}
if (!empty($godown)) {
    $gcond = " AND `godown` = '$godown' ";
}
$mfrmdate = trim($_POST["frmdt"]);

$formname = 'Kata Patti';

$getpmain2 = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` = '$mfrmdate'
$suppcond $keycond 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");

            // 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</th>
                                    <th width="40%;">Party Name</th>
                                    <th width="10%;">Bags</th>
                                    <th width="10%;">Kgs</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 = "";

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

                                    $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[party]'") or die(mysqli_error($conn));
                                    $sparty = mysqli_fetch_array($gparty);

                                    $godw = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` where `code` = '$pmain[godown]'") or die(mysqli_error($conn));
                                    $sgodw = mysqli_fetch_array($godw);

                                    $godown = $sgodw['gcode'];
                                    $voucher = $pmain['vouchno'];
                                    $invno = $pmain['invno'];
                                    $mydt = $pmain['mydt'];
                                    $party = $sparty['name'];
                                    $billamt = $pmain['billamt'];
                                    $subtot = $pmain['grossamt'];
                                    $totvat = $pmain['totvat'];
                                    $totdisc = $pmain['totdisc'];
                                    $totqty = $pmain['totqty'];
                                    $totbag = $pmain['totbag'];
                                    $marketfee = $pmain['marketfee'];
                                    $commamt = $pmain['commamt'];

                                    $pctr++;
                                    echo "<tr data-href='purchase.php?mode=edit&vouchno=$voucher' class='clickable-row2' style='cursor: pointer;' [target]='_blank'>";
                                    echo "<td>$pctr</td>";
                                    echo "<td>$party</td>";
                                    echo "<td class='text-right'>$totbag</td>";
                                    echo "<td class='text-right'>$totqty</td>";
                                    echo "<td class='text-right'>$billamt</td>";
                                    echo "</tr>";

                                    $ttotqty += $totqty;
                                    $ttotbag += $totbag;
                                    $mtotmarkamt += $marketfee;
                                    $mcommamt += $commamt;
                                    $pgrsubtot += $subtot;
                                    $pgrtotbillamt += $billamt;

                                }
                                echo "<tr style='background-color: #98F9F0;color: red;'>";
                                echo "<td></td>";
                                echo "<td class='text-right'><b>Total</b></td>";
                                echo "<td class='numeric text-right'><b>$ttotbag</b></td>";
                                echo "<td class='numeric text-right'><b>$ttotqty</b></td>";
                                echo "<td class='numeric text-right'><b>$pgrtotbillamt</b></td>";
                                echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>

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

    </html>
    <?php
}
if (isset($_POST["excel"]) || ($_GET["excel"])) {


    $contents .="Sr, Voucher, Invoice, Date, Party Name, Party Type, Total Qty, Market Fee, Comm'n Amt, Sub Total, 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";
                                    }

                                    $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[party]'") or die(mysqli_error($conn));
                                    $sparty = mysqli_fetch_array($gparty);

                                    $godw = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` where `code` = '$pmain[godown]'") or die(mysqli_error($conn));
                                    $sgodw = mysqli_fetch_array($godw);

                                    $godown = $sgodw['gcode'];
                                    $voucher = $pmain['vouchno'];
                                    $invno = $pmain['invno'];
                                    $mydt = $pmain['mydt'];
                                    $party = $sparty['name'];
                                    $billamt = $pmain['billamt'];
                                    $subtot = $pmain['grossamt'];
                                    $totvat = $pmain['totvat'];
                                    $totdisc = $pmain['totdisc'];
                                    $totqty = $pmain['totqty'];
                                    $marketfee = $pmain['marketfee'];
                                    $commamt = $pmain['commamt'];

                                    $pctr++;
                    $contents .="$pctr" . ",";
                    $contents .="$voucher" . ",";
                    $contents .="$invno" . ",";
                    $contents .="$mydt" . ",";
                    $contents .="$party" . ",";
                    $contents .="$ptyped" . ",";
                    $contents .="$totqty" . ",";
                    $contents .="$marketfee" . ",";
                    $contents .="$commamt" . ",";
                    $contents .="$subtot" . ",";
                    $contents .="$billamt" . "\n";

                                    $ttotqty += $totqty;
                                    $mtotmarkamt += $marketfee;
                                    $mcommamt += $commamt;
                                    $pgrsubtot += $subtot;
                                    $pgrtotbillamt += $billamt;

                                    if ($_POST["dtype"] == 2) {

                                        $contents .="Sr, Code, Item, Bags, Qty., Cost/ 10kg, Amount\n";

                                        $ctr2 = 0;
                                        $pctr2 = 0;
                                        $pcartqty = $pqty = $ptotqty = $ptotamount = $ptotdiscount = $ptotvat = $ptotal = 0;

                                        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' order by srno $keycond") or die(mysqli_error($conn));
                                        while ($tran = mysqli_fetch_array($gettran)) {

                                            $getitem = mysqli_query($conn, "SELECT `code`,`name`,`unit` FROM `product` WHERE `code`='$tran[itemcode]'") or die(mysqli_error($conn));
                                            $showitem = mysqli_fetch_array($getitem);

                                            $ctr2++;
                                            $contents .="$ctr2" . ",";
                                            $contents .="$tran[barcode]" . ",";
                                            $contents .="$showitem[name]" . ",";
                                            $contents .="$tran[cartqty]" . ",";
                                            $contents .="$tran[qty]" . ",";
                                            $contents .="$tran[cost]" . ",";
                                            $contents .="$tran[amount]" . "\n";

                                            $tpcscart += $tran['pcscart'];
                                            $pcartqty += $tran['cartqty'];
                                            $pqty += $tran['qty'];
                                            $tcost += $tran['cost'];
                                            $ptotamount += $tran['amount'];
                                        }

                                        $contents .="" . ",";
                                        $contents .="" . ",";
                                        $contents .="Total" . ",";
                                        $contents .="$pcartqty" . ",";
                                        $contents .="$pqty" . ",";
                                        $contents .="$tcost" . ",";
                                        $contents .="$ptotamount" . "\n";
                                    }
                                }
                                $contents .="" . ",";
                                $contents .="" . ",";
                                $contents .="" . ",";
                                $contents .="" . ",";
                                
                                if ($ptype != "") {
                                    $contents .="" . ",";
                                }
                                $contents .="" . ",";
                                $contents .="Total" . ",";
                                $contents .="$ttotqty" . ",";
                                $contents .="$mtotmarkamt" . ",";
                                $contents .="$mcommamt" . ",";
                                $contents .="$pgrsubtot" . ",";
                                $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;
    }
}
?>