<?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);

$maindb = 'actran';
$zroot = $_SESSION["zdbroot"];

$formname = 'TDS Report';
$file = 'tdsrep_fr.php';

$party = $_POST["party"];

if (!empty($party)) {
    $partycond = " AND `accode` = '$party' ";
    $getparty = mysqli_query($conn, "SELECT name FROM `accmast` WHERE `accode` = '$party' ")or die(mysqli_error($conn));
    $fparty = mysqli_fetch_array($getparty);
}

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


$mfrmdate = trim($_POST["frmdt"]);
$mtodate = trim($_POST["todt"]);

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

$comp = $_SESSION["comp_" . $dbname];

$getcomp = mysqli_query($conn, "SELECT tdscode FROM $zroot.`company` WHERE `code` = '$comp' ")or die(mysqli_error($conn));
$fcomp = mysqli_fetch_array($getcomp);
$tdscode = $fcomp['tdscode'];

$mainqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`date`,'%d-%m-%Y') as vouchdt from actran where vouchno != '' 
                        and `date` >= '$mfrmdate' and `date` <= '$mtodate' and source = 'JV'  and amount > 0 
                        and accode != '$tdscode' $partycond and vouchno in ( select vouchno from jvtran
                        where accode = '$tdscode') order by date  ")or die(mysqli_error($conn));
$getno = mysqli_num_rows($productgrp);


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

    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        th{
            background: #26A69A;
        }
        .dth{
            background:#BABFC2;
        }
        .colr{
            background: #26A69A;
            font-size: 17px;
        }
        table tr td {            
            font-size: 14px;

        }

    </style>
    </head>
    <body>


        <div class="container" style="width:75%">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <?php if (!empty($party)) { ?>
                        <table class="table table-bordered table-condensed">
                            <tr class="mySearchTr">
                                <td class="h4" style="color:white" ><b>Party : <?php echo $fparty['name']; ?></b></td>
                            <tr>
                        </table>
                    <?php } ?>
                    <table style="background: white;width: 100%" class="table table-condensed table-striped table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th width='3%'>Sr</th>
                                <th width='8%' >Date</th>
                                <?php if (empty($party)) { ?>
                                    <th width='27%'>Party Name</th>
                                <?php } ?>
                                <th width='8%'>Bill No.</th>
                                <th width='7%' class="text-right">Bill Amt.</th>
                                <th width='7%' class="text-right">TDS Amt.</th>
                                <th width='7%' class="text-right">Paid Amt.</th>
                                <th width='7%' class="text-right">Balance Amt.</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;

                            $totbillamt = $totpaidamt = $tottdsamt = $totbalamt = 0;

                            while ($fmain = mysqli_fetch_array($mainqry)) {

                                $vouchdt = $fmain['vouchdt'];
                                $vouchno = $fmain['vouchno'];
                                $p = $fmain['accode'];

                                $billamt = $fmain['amount'];

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

                                $gbill = mysqli_query($conn, "SELECT billno FROM `jvmain` WHERE `vouchno` = '$vouchno' ")or die(mysqli_error($conn));
                                $fetbill = mysqli_fetch_array($gbill);
                                $billno = $fetbill['billno'];


                                $tdsqry = mysqli_query($conn, "SELECT amount from actran where vouchno = '$vouchno' and source = 'JV'
                                                        and amount > 0 and accode = '$tdscode' ")or die(mysqli_error($conn));
                                $fettds = mysqli_fetch_array($tdsqry);
                                $tdsamt = $fettds['amount'];

                                $paidqry = mysqli_query($conn, "SELECT sum(setoff) as stamt from billsett where vouchno = '$vouchno'  
                                                        and type =  'J' and party = '$tdscode' ")or die(mysqli_error($conn));
                                $fetpaid = mysqli_fetch_array($paidqry);
                                $paidamt = $fetpaid['stamt'];

                                $balamt = $tdsamt - $paidamt;

                                $ctr++;
                                Echo"<tr>";
                                Echo"<td>$ctr</td>";
                                Echo"<td>$vouchdt</td>";
                                if (empty($party)) {
                                    Echo"<td>$pname</td>";
                                }
                                Echo"<td class=''>$billno</td>";
                                Echo"<td class='text-right'>$billamt</td>";
                                Echo"<td class='text-right'>$tdsamt</td>";
                                Echo"<td class='text-right'>$paidamt</td>";
                                Echo"<td class='text-right'>$balamt</td>";
                                Echo"</tr>";

                                $totbillamt = $totbillamt + $billamt;
                                $tottdsamt = $tottdsamt + $tdsamt;
                                $totpaidamt = $totpaidamt + $paidamt;
                                $totbalamt = $totbalamt + $balamt;
                            }

                            Echo "<tr>";
                            Echo "<td class='colr'></td>";
                            if (empty($party)) {
                                Echo "<td class='colr'></td>";
                            }
                            Echo "<td class='colr'></td>";
                            Echo "<td class='text-right colr'><b>Total</b></td>";
                            Echo "<td class='numeric text-right colr '><b>$totbillamt</b></td>";
                            Echo "<td class='numeric text-right colr '><b>$tottdsamt</b></td>";
                            Echo "<td class='numeric text-right colr '><b>$totpaidamt</b></td>";
                            Echo "<td class='numeric text-right colr '><b>$totbalamt</b></td>";
                            Echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div> 
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}

if (isset($_POST["excel"])) {
    if (!empty($party)) {

        $contents .= ", Party : $fparty[name],\n";
    }
    if (empty($party)) {
        $contents .= "Sr., Date, Party Name, Bill No., Bill Amt., TDS Amt., Paid Amt., Balance Amt.,\n";
    } else {
        $contents .= "Sr., Date ,Bill No., Bill Amt., TDS Amt., Paid Amt., Balance Amt.,\n";
    }

    $ctr = 0;

    $totbillamt = $totpaidamt = $tottdsamt = $totbalamt = 0;

    while ($fmain = mysqli_fetch_array($mainqry)) {

        $vouchdt = $fmain['vouchdt'];
        $vouchno = $fmain['vouchno'];
        $p = $fmain['accode'];

        $billamt = $fmain['amount'];

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

        $gbill = mysqli_query($conn, "SELECT billno FROM `jvmain` WHERE `vouchno` = '$vouchno' ")or die(mysqli_error($conn));
        $fetbill = mysqli_fetch_array($gbill);
        $billno = $fetbill['billno'];

        $tdsqry = mysqli_query($conn, "SELECT amount from actran where vouchno = '$vouchno' and source = 'JV'
                                                        and amount > 0 and accode = '$tdscode' ")or die(mysqli_error($conn));
        $fettds = mysqli_fetch_array($tdsqry);
        $tdsamt = $fettds['amount'];

        $paidqry = mysqli_query($conn, "SELECT sum(setoff) as stamt from billsett where vouchno = '$vouchno'  
                                                        and type =  'J' and party = '$tdscode' ")or die(mysqli_error($conn));
        $fetpaid = mysqli_fetch_array($paidqry);
        $paidamt = $fetpaid['stamt'];

        $balamt = $tdsamt - $paidamt;

        $ctr++;


        $contents .= $ctr . ",";
        $contents .= $vouchdt . ",";
        if (empty($party)) {
            $contents .= $pname . ",";
        }
        $contents .= $billno . ",";
        $contents .= $billamt . ",";
        $contents .= $tdsamt . ",";
        $contents .= $paidamt . ",";
        $contents .= $balamt . "\n";



        $totbillamt = $totbillamt + $billamt;
        $tottdsamt = $tottdsamt + $tdsamt;
        $totpaidamt = $totpaidamt + $paidamt;
        $totbalamt = $totbalamt + $balamt;
    }
    $contents .= "" . ",";
    if (empty($party)) {
        $contents .= "" . ",";
    }
    $contents .= "" . ",";
    $contents.= "Total" . ",";
    $contents.= $totbillamt . ",";
    $contents.= $tottdsamt . ",";
    $contents.= $totpaidamt . ",";
    $contents.= $totbalamt . "\n";



    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1) {
        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;
    }
}
?>