<?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 = 'salemain';
$trandb = 'saletran';
$zroot = $_SESSION["zdbroot"];

$formname = 'Inward Register Export';
$zroot = $_SESSION["zdir"];
$file = 'exinwdreg_fr.php';



    $dtye = $_POST["dtype"];


    if (!empty($dtype)) {
        $dtypecond = " AND `dtype`='$dtype' ";
    }

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

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

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


    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' AND saletype !='C' ")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);

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: #4eb3c7;
        }
        .td1{
           text-align:right;
           /*color:black;*/
           font-size:14px;
           font-weight: bold;
        }
        .tr1{
           background: #98f9f0;
        }

    </style>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php");   ?>
            <div class="container">
                <div class="row">
                    <div class="col-lg-12 pad">
                        <table  class="table table-condensed table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:5%">Sr</th>
                                    <th style="width:7%">Voucher</th>
                                    <th style="width:10%">Bill Date</th>
                                    <th style="width:20%">Customer Code & Name</th>
                                    <th style="width:10%">PAN No.</th>
                                    <th style="width:10%; text-align:right;">PRE TCS</th>
                                    <th style="width:10%; text-align:right;">TCS %</th>
                                    <th style="width:10%; text-align:right;">TCS Value</th>
                                    <th style="width:12%; text-align:right;">Bill Amount</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $ctr = 0;
                                $totqty = 0;

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

                                    $ggodwn = mysqli_query($conn, "SELECT `accode`,`name`,`pan` FROM `accmast` WHERE `accode` = '$pmain[party]'")or die(mysqli_error($conn));
                                    $godwn = mysqli_fetch_array($ggodwn);
                                    
                                    $pretcs = $pmain[subtotal] - $pmain[discondies];

                                    $ctr++;
                                    Echo "<tr id='boldfont'>";
                                    Echo "<td>$ctr</td>";
                                    Echo "<td>$pmain[vouchno]</td>";
                                    Echo "<td>$pmain[mydt]</td>";
                                    Echo "<td>$godwn[name]</td>";
                                    Echo "<td>$godwn[pan]</td>";
                                    Echo "<td style='text-align:right;'>$pretcs</td>";
                                    Echo "<td style='text-align:right;'>$pmain[tcsperc]</td>";
                                    Echo "<td style='text-align:right;'>$pmain[tcsamt]</td>";
                                    Echo "<td style='text-align:right;'>$pmain[billamt]</td>";
                                    Echo "</tr>";

                                    $totbill = $totbill + $pmain[billamt];
                                    $tottcs = $tottcs + $pmain[tcsamt];
                                    $totpre = $totpre + $pretcs;
                                    
//                                    $totbill = number_format($totbill,2);
                                }

                                Echo "<tr class='tr1'>";
                                Echo "<td></td>";
                                Echo "<td></td>";
                                Echo "<td></td>";
                                Echo "<td></td>";
                                Echo "<td class='td1'><b>Total</b></td>";
                                Echo "<td class='td1'><b>$totpre</b></td>";
                                Echo "<td></td>";
                                Echo "<td class='td1'><b>$tottcs</b></td>";
                                Echo "<td class='td1'><b>$totbill</b></td>";
                                Echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <?php include("includes/linksfoot.php");
            ?>
    </body>
    </html>
    <?php
}

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

                                $contents .= "Sr,Voucher,Bill Date,Customer Code & Name,PAN No.,PRE TCS,TCS %,TCS Value,Bill Amount\n";
                                    
                                $ctr = 0;
                                $totqty = 0;

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

                                    $ggodwn = mysqli_query($conn, "SELECT `accode`,`name`,`pan` FROM `accmast` WHERE `accode` = '$pmain[party]'")or die(mysqli_error($conn));
                                    $godwn = mysqli_fetch_array($ggodwn);
                                    
                                    $pretcs = $pmain[subtotal] - $pmain[discondies];

                                    $ctr++;
                                    
                                    $contents .= "$ctr" . ","; 
                                    $contents .= "$pmain[vouchno]" . ","; 
                                    $contents .= "$pmain[mydt]" . ","; 
                                    $contents .= "$godwn[name]" . ","; 
                                    $contents .= "$godwn[pan]" . ","; 
                                    $contents .= "$pretcs" . ","; 
                                    $contents .= "$pmain[tcsperc]" . ","; 
                                    $contents .= "$pmain[tcsamt]" . ","; 
                                    $contents .= "$pmain[billamt]" . "\n"; 
                                    
                                    
                                    $totbill = $totbill + $pmain[billamt];
                                    $tottcs = $tottcs + $pmain[tcsamt];
                                    $totpre = $totpre + $pretcs;
                                    
                                }

                                $contents .= "" . ",";
                                $contents .= "" . ",";
                                $contents .= "" . ",";
                                $contents .= "" . ",";
                                $contents .= "Total" . ",";
                                $contents .= "$totpre" . ",";
                                $contents .= "" . ",";
                                $contents .= "$tottcs" . ",";
                                $contents .= "$totbill" . "\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;
    }
}
?>