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

$maindb = "purmain";
$trandb = "purtran";
$formname = "Purchase Register";
$file = "purchasereg_fr.php";

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

$fdate = $_POST["fdate"];
$tdate = $_POST["tdate"];
$supplier = $_POST["supplier"];

$suppcond = "";

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

if (!empty($supplier)) {
    $suppcond = "AND a.`party`='$supplier'";
}


if (empty($_POST["fdate"])) {
    browse("From Date Cannot Be Empty");
    echo "<script>window.close();</script>";
}
if (empty($_POST["tdate"])) {
    browse("To Date Cannot Be Empty");
    echo "<script>window.close();</script>";
}

$getpmain = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,b.name as suppname FROM `$maindb` a 
                                               LEFT JOIN accmast b ON a.party = b.accode    
                                               WHERE a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' $suppcond
                                               ORDER BY a.vouchno,a.vouchdt ASC") or die(mysqli_error($conn));
$get = mysqli_num_rows($getpmain);

if (isset($_POST["view"])) {

    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 ">
                    <table style="background: white;width: 100%" class="table table-condensed table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:8%">Voucher</th>
                                <th style="width:8%">Date</th>
                                <th style="width:20%">Party</th>
                                <th style="width:8%">Mode</th>
                                <th style="width:10%" class="text-right">Total Qty.</th>
                                <th style="width:10%" class="text-right">Packing</th>
                                <th style="width:10%" class="text-right">Net Amount</th>
                                <th style="width:20%">Remark</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totpairs = 0;
                            $totpack = 0;
                            $totamt = 0;

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

                                $typeOptions = [
                                    'R' => 'Credit',
                                    'U' => 'UPI / Bank',
                                    'C' => 'Cash'
                                ];
                                $typeCode = $pmain['type'];
                                $typeLabel = isset($typeOptions[$typeCode]) ? $typeOptions[$typeCode] : 'Unknown';



                                $ctr++;
                                echo "<tr id='boldfont' class='clickable-row2' data-href='purchase.php?vouchno=$pmain[vouchno]&mode=edit' data-target='_blank'>";
                                echo "<td>$pmain[vouchno]</td>";
                                echo "<td>$pmain[mydt]</td>";
                                echo "<td>$pmain[suppname]</td>";
                                echo "<td>$typeLabel</td>";
                                echo "<td class='text-right'>$pmain[totalqty]</td>";
                                echo "<td class='text-right'>$pmain[addpack]</td>";
                                echo "<td class='text-right'>$pmain[netamt]</td>";
                                echo "<td>$pmain[remark1] $pmain[remark2]</td>";
                                echo "</tr>";

                                $totpairs += $pmain['totalqty'];
                                $totpack += $pmain['addpack'];
                                $totamt += $pmain['netamt'];

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

                                    echo "<tr style='height:10px;'>";
                                    echo "<td colspan='25'>";
                                    echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                    echo "<thead>";
                                    echo "<tr class='' style='background-color:#BABFC2'>";
                                    echo "<th style='width:5%;'>Sr</th>";
                                    echo "<th style='width:15%;' class=''>Itemname</th>";
                                    echo "<th style='width:5%;' class=''>No. of Pairs</th>";
                                    echo "<th style='width:6%;' class='text-right'>Ctn. Qty.</th>";
                                    echo "<th style='width:8%;' class='text-right'>Total Pairs</th>";
                                    echo "<th style='width:10%;' class='text-right'>Rate</th>";
                                    echo "<th style='width:10%;' class='text-right'>Amount</th>";
                                    echo "</tr>";
                                    echo "<thead>";
                                    echo "<tr>";

                                    $ctr2 = 0;
                                    $pctr2 = 0;
                                    $pqty = $ptotqty = $ptotamount = 0;

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

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

                                        $ctr2++;
                                        echo "<tr>";
                                        echo "<td>$ctr2</td>";
                                        echo "<td>$showitem[name]</td>";
                                        echo "<td>$tran[cartqty]</td>";
                                        echo "<td class='text-right'>$tran[qty]</td>";
                                        echo "<td class='text-right'>$tran[totqty]</td>";
                                        echo "<td class='text-right'>$tran[rate]</td>";
                                        echo "<td class='text-right'>$tran[amount]</td>";
                                        echo "</tr>";


                                        $pqty = $pqty + $tran['qty'];
                                        $ptotqty = $ptotqty + $tran['totqty'];
                                        $ptotamount = $ptotamount + $tran['amount'];
                                    }

                                    echo "<tr style='background-color:#d8e0e6'>";
                                    echo "<td colspan='2'></td>";
                                    echo "<td>Total</td>";
                                    echo "<td class='text-right'>$pqty</td>";
                                    echo "<td class='text-right'>$ptotqty</td>";
                                    echo "<td class='text-right'></td>";
                                    echo "<td class='text-right'>$ptotamount</td>";
                                    echo "</tr>";
                                    echo "</table>";
                                }

                            }

                            echo "<tr> ";
                            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;'>" . number_format($totpairs, 2) . "</td>";
                            echo "<th class='text-right' style='color: red;'>" . number_format($totpack, 2) . "</td>";
                            echo "<th class='text-right' style='color: red;'>" . number_format($totamt, 2) . "</td>";
                            echo "<td></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

    </html>
    <?php
}

if (isset($_POST["excel"])) {

    $contents .= "Voucher, Date, Customer, Mode, Total Qty., Packing, Amount, Remark \n";

    $ctr = 0;
    $totpairs = 0;
    $totpack = 0;
    $totamt = 0;

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

        $typeOptions = [
            'R' => 'Credit',
            'U' => 'UPI / Bank',
            'C' => 'Cash'
        ];
        $typeCode = $pmain['type'];
        $typeLabel = isset($typeOptions[$typeCode]) ? $typeOptions[$typeCode] : 'Unknown';


        $ctr++;
        $contents .= "$pmain[vouchno]" . ",";
        $contents .= "$pmain[mydt]" . ",";
        $contents .= "$pmain[custname]" . ",";
        $contents .= "$typeLabel" . ",";
        $contents .= "$pmain[totalqty]" . ",";
        $contents .= "$pmain[addpack]" . ",";
        $contents .= "$pmain[netamt]" . ",";
        $contents .= "$pmain[remark]" . "\n";

        $totpairs += $pmain['totalqty'];
        $totpack += $pmain['addpack'];
        $totamt += $pmain['amount'];

    }

    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Grand Total" . ",";
    $contents .= "$totpairs" . ",";
    $contents .= "$totpack" . ",";
    $contents .= "$totamt" . ",";
    $contents .= "" . "\n";


    $contents = strip_tags($contents);

    header("Content-Disposition: attachment; filename=$formname " . "" . date('d-m-Y') . ".csv");
    print $contents;

}
?>