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

$formname = 'Debit Note - Others Register';
$zroot = $_SESSION["zdir"];
$file = 'debothreg_fr.php';


$account = $_POST["account"];
$dtype = $_POST["dtype"];
$type = $_POST["type"];
$purtype = $_POST["purtype"];


$accond = "";
if (!empty($account)) {
$accond = "AND `party` = '$account' " ;    
}
$$typecond = "";
if (!empty($type)) {
$typecond = "AND `paidby` = '$type' " ;    
}
$purtcond = "";
if (!empty($purtype)) {
$purtcond = "AND `purtype` = '$purtype' " ;    
}

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' $accond $typecond $purtcond AND type='O' ORDER BY invno")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: #6082BF;
        }

    </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 style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:5%">Invoice</th>
                                    <th style="width:5%">Date</th>
                                    <th style="width:20%">Supplier</th>
<!--                                    <th style="width:5%;">Paid By</th>
                                    <th style="width:5%;">Purchase Type</th>-->
                                    <th style="width:5%; text-align:right;">Discount</th>
                                    <th style="width:5%; text-align:right;">Taxable Value</th>
                                    <!--<th style="width:5%; text-align:right;">Total Qty.</th>-->
                                    <th style="width:5%; text-align:right;">Total GST</th>
                                    <th style="width:10%; text-align:right;">Bill Amount</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $ctr = 0;
                                $totqty = 0;

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

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

                                    $ctr++;
                                    Echo "<tr id='boldfont'>";
                                    Echo "<td>$pmain[invno]</td>";
                                    Echo "<td>$pmain[mydt]</td>";
                                    Echo "<td>$supplier[name]</td>";
//                                    Echo "<td>$pmain[paidby]</td>";
//                                    Echo "<td>$pmain[purtype]</td>";
//                                    Echo "<td style='text-align:right;'>$pmain[subtotal]</td>";
                                    Echo "<td style='text-align:right;'>$pmain[litemdisc]</td>";
                                    Echo "<td style='text-align:right;'>$pmain[tottaxamt]</td>";
//                                    Echo "<td style='text-align:right;'>$pmain[totqty]</td>";
                                    Echo "<td style='text-align:right;'>$pmain[totalgst]</td>";
                                    Echo "<td style='text-align:right;'>$pmain[netamt]</td>";
                                    Echo "</tr>";

                                    $totqty = $totqty + $pmain[totqty];
                                    $totamt = $totamt + $pmain[netamt];

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

                                        Echo "<tr style='height:10px; background-color:#BABFC2'>";
                                        Echo "<td colspan='25'>";
                                        Echo "<table style='width:100%;' id='tabital' class='table table-condensed table-bordered'>";
                                        Echo "<th style='width:2%;'  >Sr</th>";
                                        Echo "<th style='width:20%;' >Item</th>";
                                        Echo "<th style='width:5%;' >HSN Code</th>";
//                                        Echo "<th style='width:5%;text-align:right;' >Qty</th>";
//                                        Echo "<th style='width:5%;text-align:right;' >Cost</th>";
                                        Echo "<th style='width:5%;text-align:right;' >Amount</th>";
                                        Echo "<th style='width:5%;text-align:right;' >Disc Amt</th>";
                                        Echo "<th style='width:5%;text-align:right;' >CGST Rate</th>";
                                        Echo "<th style='width:5%;text-align:right;' >CGST Amt</th>";
                                        Echo "<th style='width:5%;text-align:right;' >SGST Rate</th>";
                                        Echo "<th style='width:5%;text-align:right;' >SGST Amt</th>";
                                        Echo "<th style='width:5%;text-align:right;' >IGST Rate</th>";
                                        Echo "<th style='width:5%;text-align:right;' >IGST Amt</th>";
                                        Echo "<th style='width:10%;text-align:right;'>Total</th>";
                                        Echo "<tr>";


                                        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]'")or die(mysqli_error($conn));
                                        $ctr2 = 0;
                                        $trtotqty = 0;
                                        $tramount = 0;
                                        $trcgstamt = 0;
                                        $trsgstamt = 0;
                                        $trigstamt = 0;
                                        $trtotal = 0;
                                        while ($tran = mysqli_fetch_array($gettran)) {

                                            $ctr2++;
                                            Echo "<tr>";
                                            Echo "<td>$ctr2</td>";
                                            Echo "<td>$tran[descrip]</td>";
                                            Echo "<td>$tran[hsncode]</td>";
//                                            Echo "<td style='text-align:right;'>$tran[qty]</td>";
//                                            Echo "<td style='text-align:right;'>$tran[cost]</td>";
                                            Echo "<td style='text-align:right;'>$tran[amount]</td>";
                                            Echo "<td style='text-align:right;'>$tran[discount]</td>";
                                            Echo "<td style='text-align:right;'>$tran[cgstrate]</td>";
                                            Echo "<td style='text-align:right;'>$tran[cgstamt]</td>";
                                            Echo "<td style='text-align:right;'>$tran[sgstrate]</td>";
                                            Echo "<td style='text-align:right;'>$tran[sgstamt]</td>";
                                            Echo "<td style='text-align:right;'>$tran[igstrate]</td>";
                                            Echo "<td style='text-align:right;'>$tran[igstamt]</td>";
                                            Echo "<td style='text-align:right;'>$tran[total]</td>";
                                            Echo "</tr>";

                                            $trtotqty = $trtotqty + $tran[qty];
                                            $tramount = $tramount + $tran[amount];
                                            $trcgstamt = $trcgstamt + $tran[cgstamt];
                                            $trsgstamt = $trsgstamt + $tran[sgstamt];
                                            $trigstamt = $trigstamt + $tran[igstamt];
                                            $trtotal = $trtotal + $tran[total];
                                        }
                                        Echo "<tr>";
                                        Echo "<td style='text-align:right' colspan='3'>Total</td>";
//                                        Echo "<td style='text-align:right;'>$trtotqty</td>";
//                                        Echo "<td colspan=''></td>";
                                        Echo "<td style='text-align:right;'>$tramount</td>";
                                        Echo "<td colspan=''></td>";
                                        Echo "<td colspan=''></td>";
                                        Echo "<td style='text-align:right;'>$trcgstamt</td>";
                                        Echo "<td colspan=''></td>";
                                        Echo "<td style='text-align:right;'>$trsgstamt</td>";
                                        Echo "<td colspan=''></td>";
                                        Echo "<td style='text-align:right;'>$trigstamt</td>";
                                        Echo "<td style='text-align:right;'>$trtotal</td>";
                                        Echo "</tr>";

                                        Echo "</table>";
                                        Echo "</td>";
                                    }
                                }

                                Echo "<tr>";
                                Echo "<td style='text-align:right' colspan= '5'><b>Total</b></td>";
//                                Echo "<td style='text-align:right'><b>$totqty</b></td>";
                                Echo "<td style='text-align:right'><b></b></td>";
                                Echo "<td style='text-align:right'><b>$totamt</b></td>";
                                Echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <?php include("includes/linksfoot.php");
            ?>
    </body>
    </html>
    <?php
}

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

                                
    $contents .= "Invoice,Date,Supplier,Discount,Taxable Value,Total Qty.,Total GST,Bill Amount\n";

                                $ctr = 0;
                                $totqty = 0;

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

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

                                    $ctr++;
                                    $contents .= "$pmain[invno]" . ",";
                                    $contents .= "$pmain[mydt]" . ",";
                                    $contents .= "$supplier[name]" . ",";
                                    $contents .= "$pmain[litemdisc]" . ",";
                                    $contents .= "$pmain[tottaxamt]" . ",";
                                    $contents .= "$pmain[totqty]" . ",";
                                    $contents .= "$pmain[totalgst]" . ",";
                                    $contents .= "$pmain[netamt]" . "\n";
                                    

                                    $totqty = $totqty + $pmain[totqty];
                                    $totamt = $totamt + $pmain[netamt];

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

                                        $contents .= "Sr,Item,HSN Code,Qty,Cost,Amount,Disc Amt,CGST Rate,CGST Amt,SGST Rate,SGST Amt,IGST Rate,IGST Amt,Total\n"; 
                                        
                                        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]'")or die(mysqli_error($conn));
                                        $ctr2 = 0;
                                        $trtotqty = 0;
                                        $tramount = 0;
                                        $trcgstamt = 0;
                                        $trsgstamt = 0;
                                        $trigstamt = 0;
                                        $trtotal = 0;
                                        
                                        while ($tran = mysqli_fetch_array($gettran)) {
                                            
                                            $getitem = mysqli_query($conn, "SELECT code, name FROM `product` WHERE code = '$tran[item]'") or die(mysqli_error($conn));
                                            $geti = mysqli_fetch_array($getitem);
                                            

                                            $ctr2++;

                                            $contents .= "$ctr2" . ",";
                                            $contents .= "$geti[name]" . ",";
                                            $contents .= "$tran[hsncode]" . ",";
                                            $contents .= "$tran[qty]" . ",";
                                            $contents .= "$tran[cost]" . ",";
                                            $contents .= "$tran[amount]" . ",";
                                            $contents .= "$tran[discount]" . ",";
                                            $contents .= "$tran[cgstrate]" . ",";
                                            $contents .= "$tran[cgsamt]" . ",";
                                            $contents .= "$tran[sgstrate]" . ",";
                                            $contents .= "$tran[sgsamt]" . ",";
                                            $contents .= "$tran[igstrate]" . ",";
                                            $contents .= "$tran[igsamt]" . ",";
                                            $contents .= "$tran[total]" . "\n";
                                            
                                            $trtotqty = $trtotqty + $tran[qty];
                                            $tramount = $tramount + $tran[amount];
                                            $trcgstamt = $trcgstamt + $tran[cgstamt];
                                            $trsgstamt = $trsgstamt + $tran[sgstamt];
                                            $trigstamt = $trigstamt + $tran[igstamt];
                                            $trtotal = $trtotal + $tran[total];
                                        }
                                        
                                            $contents .= "" . ",";
                                            $contents .= "" . ",";
                                            $contents .= "Total" . ",";
                                            $contents .= "$trtotqty" . ",";
                                            $contents .= "" . ",";
                                            $contents .= "$tramount" . ",";
                                            $contents .= "" . ",";
                                            $contents .= "" . ",";
                                            $contents .= "$trcgstamt" . ",";
                                            $contents .= "" . ",";
                                            $contents .= "$trsgstamt" . ",";
                                            $contents .= "" . ",";
                                            $contents .= "$trigstamt" . ",";
                                            $contents .= "$trtotal" . "\n";
                                        
                                    }
                                
                                }

                                $contents .= "" . ",";
                                $contents .= "" . ",";
                                $contents .= "" . ",";
                                $contents .= "" . ",";
                                $contents .= "Total" . ",";
                                $contents .= "$totqty" . ",";
                                $contents .= "" . ",";
                                $contents .= "$totamt" . "\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;
    }
}
?>