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

$maindb = 'credmain';
$trandb = 'credtran';
$formname = "Bill Register";
$file = "Credit Note reg.php";
$zroot = $dbname;


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


    $party = $_POST["party"];

    $partycond = "";
    $stafftcond = "";
    $seriescond = "";
    $typecond = "";
    $scopecond = "";

    if (!empty($_POST["party"])) {
        $partycond = "AND `party` = '$party'";
    }
    
    if (empty($_POST["fdate"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; salereg.php");
    }
    if (empty($_POST["tdate"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; salereg.php");
    }


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

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

    $getdata = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE
                           `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $partycond  
                           $seriescond $typecond ORDER BY vouchno") or die(mysqli_error($conn));

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

    include("includes/linkshead.php");
    ?>
    <link href="MyStyle/rep_format.css" type="text/css" >
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        .mtotclr{
            color:#0098DA;
        }
        .ttotclr{
            color:#ed3237;
        }


    </style>
    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep2.php"); ?>

            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-condensed table-bordered table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:6%">Voucher</th>
                                <th style="width:7%">Date</th>
                                <th style="width:5%">Bill No.</th>
                                <th style="width:23%">Party Name</th>
                                <th style="width:15%">Vessel</th>
                                <th style="width:10%" class="text-right">Bill Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $totamt = 0;
                            $totdiscamt = 0;
                            $totvatp = 0;
                            $totvatamt = 0;
                            $totbamt = 0;

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

                                $ctr++;
                                Echo"<tr>";
                                Echo"<td> $pmain[vouchno]</td>";
                                Echo"<td>$pmain[mydt]</td>";
                                Echo"<td>$pmain[billno]</td>";
                                Echo"<td>$party[name]</td>";
                                Echo"<td>$pmain[vessel]</td>";
                                Echo"<td class='text-right'>$pmain[billamt]</td>";
                                Echo"</tr>";
                                $totamt = $totamt + $pmain['amount'];
                                $totdiscamt = $totdiscamt + $totdisc;
                                $totvatp = $totvatp + $pmain['vatperc'];
                                $totvatamt = $totvatamt + $pmain['vat'];
                                $totbamt = $totbamt + $pmain['billamt'];



                                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 style='background:grey'>";

                                    Echo "<th style='width:4%;'>Sr</th>";
                                    Echo "<th style='width:5%;'>M/S</th>";
                                    Echo "<th style='width:5%;'>Barcode	</th>";
                                    Echo "<th style='width:10%;'>Description1 </th>";
                                    Echo "<th style='width:10%;'>Description2</th>";
                                    Echo "<th style='width:7%;' class='text-right'>Qty.</th>";
                                    Echo "<th style='width:7%;' class='text-right'>Rate</th>";
                                    Echo "<th style='width:7%;' class='text-right'>Taxable value</th>";
                                    Echo "<th style='width:5%;' class='text-right'>CGST%</th>";
                                    Echo "<th style='width:7%;' class='text-right'>CGST Amt.</th>";
                                    Echo "<th style='width:5%;' class='text-right'>SGST%</th>";
                                    Echo "<th style='width:7%;' class='text-right'>SGST Amt.</th>";
                                    Echo "<th style='width:5%;' class='text-right'>IGST%</th>";
                                    Echo "<th style='width:7%;' class='text-right'>IGST Amt.</th>";
                                    Echo "<th style='width:7%;' class='text-right'>Total</th>";


                                    Echo "</tr>";
                                    Echo "<thead>";

                                    Echo "<tr>";


                                    $ctr2 = 0;
                                    $totqty = 0;
                                    $totrate = 0;
                                    $totamount = 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)) {

                                        $ctr2++;
                                        Echo "<tr>";
                                        Echo "<td>$ctr2</td>";
                                        Echo "<td>$tran[ms]</td>";
                                        Echo "<td>$tran[hsncode]</td>";
                                        Echo "<td>$tran[descrip1]</td>";
                                        Echo "<td>$tran[descrip2]</td>";
                                        Echo "<td class='text-right'>$tran[qty]</td>";
                                        Echo "<td class='text-right'>$tran[rate]</td>";
                                        Echo "<td class='text-right'>$tran[subtot]</td>";
                                        Echo "<td class='text-right'>$tran[cgstrate]</td>";
                                        Echo "<td class='text-right'>$tran[cgstamt]</td>";
                                        Echo "<td class='text-right'>$tran[sgstrate]</td>"; 
                                        Echo "<td class='text-right'>$tran[sgstamt]</td>"; 
                                        Echo "<td class='text-right'>$tran[igstrate]</td>"; 
                                        Echo "<td class='text-right'>$tran[igstamt]</td>"; 
                                        Echo "<td class='text-right'>$tran[total]</td>"; 
                                        Echo "</tr>";

                                        $totqty = $totqty + $tran['qty'];
                                        $totrate = $totrate + $tran['rate'];
                                        $totamount = $totamount + $tran['amount'];
                                        $totcgstamt = $totcgstamt + $tran['cgstamt'];
                                        $totsgstamt = $totsgstamt + $tran['sgstamt'];
                                        $totigstamt = $totigstamt + $tran['igstamt'];
                                        $tottotal = $tottotal + $tran['total'];
                                    }
                                    Echo "<tr>";
                                    Echo "<td></td>";
                                    Echo "<td></td>";
                                    Echo "<td></td>";
                                    Echo "<td></td>";
                                    Echo "<th class='ttotclr text-right'>Total</th>";
                                    Echo "<th class='ttotclr text-right'>$totqty</th>";
                                    Echo "<th class='ttotclr text-right'>$totrate</th>";
                                    Echo "<th class='ttotclr text-right'>$totamount</th>";
                                    Echo "<td></td>";
                                    Echo "<th class='ttotclr text-right'>$totcgstamt</th>";
                                    Echo "<td></td>";
                                    Echo "<th class='ttotclr text-right'>$totsgstamt</th>";
                                    Echo "<td></td>";
                                    Echo "<th class='ttotclr text-right'>$totigstamt</th>";
                                    Echo "<th class='ttotclr text-right'>$tottotal   </th>";


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

                            Echo"<tr>";
                            Echo"<td ></td>";
                            Echo"<td ></td>";
                            Echo"<td ></td>";
                            Echo"<td ></td>";
                            Echo"<td class='text-right mtotclr' ><b> Total </b></td>";
                            Echo"<td class='text-right mtotclr' ><b> $totbamt    </b></td>";
                            Echo"</tr>";
                            ?>

                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {

    $contents .= "Sr,Voucher,Date,Bill No, Party Name,Vessel,Location of Vessel,Delivery Address,Bill Amount \n";
    
    $totamt = 0;
    $totdiscamt = 0;
    $totvatp = 0;
    $totvatamt = 0;
    $totbamt = 0;

    while ($pmain = mysqli_fetch_array($getdata)) {
        $paytype = $pmain[cashcred];
//                                BROWSE($paytype);
        switch ($paytype) {
            case "C":
                $ptype = "CASH";
                break;
            case "R":
                $ptype = "CREDIT CARD";
                break;
            case "B":
                $ptype = "BANK TRANSFER";
                break;
            case "H":
                $ptype = "CHEQUE";
                break;
            case "S":
                $ptype = "CREDIT SALE";
                break;
            default:
                $ptype = "";
        }
        $gstaf = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[stafcode]' AND grpcode='040'  ") or die(mysqli_error($conn));
        $sstaf = mysqli_fetch_array($gstaf);
        $totdisc = $pmain[vatdisc] + $pamin[discount];

        $ctr++;
        $pvouchno = $pmain[series] . "" . $pmain[vouchno];
        $pdate = $pmain[mydt];
        $ppartyname = $pmain[partyname];
        $pstafname = $sstaf[name];
        $pptype = $ptype;
        $pamount = $pmain[amount];
        $ptotdisc = $totdisc;
        $pvatperc = $pmain[vatperc];
        $pvat = $pmain[vat];
        $pbillamt = $pmain[billamt];


        $contents.= "$ctr" . ",";
        $contents.= "$pmain[vouchno]" . ",";
        $contents.= "$pmain[mydt]" . ",";
        $contents.= "$pmain[billno]" . ",";
        $contents.= "$party[name]" . ",";
        $contents.= "$pmain[vessel]" . ",";
        $contents.= "$port[name]" . ",";
        $contents.= "$pmain[delvadd]" . ",";
        $contents.= "$pmain[billamt]" . "\n";
        
        $totamt = $totamt + $pmain[amount];
        $totdiscamt = $totdiscamt + $totdisc;
        $totvatp = $totvatp + $pmain[vatperc];
        $totvatamt = $totvatamt + $pmain[vat];
        $totbamt = $totbamt + $pmain[billamt];

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

     $contents .= "Sr,M/S,Barcode,Description1,Description2,Qty.,Rate,Taxable Value,CGST%,CGST Amt.SGST%,SGST Amt.IGST%,IGST% Amt.,Total\n";

            $ctr2 = 0;
            $totqty = 0;
            $totrate = 0;
            $totamount = 0;

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

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


                $ctr2++;
                $tbarcode = $tran[barcode];
                $titemname = $showitem[name];
                $tbrand = $tran[brand];
                $tsize = $tran[size];
                $tcolour = $tran[colour];
                $tunit = $tran[unit];
                $tqty = $tran[qty];
                $trate = $tran[rate];
                $tamount = $tran[amount];

                $contents .= "$ctr2" . ",";
                $contents .= "$tran[ms]" . ",";
                $contents .= "$tran[hsncode]" . ",";
                $contents .= "$tran[descrip1]" . ",";
                $contents .= "$tran[descrip2]" . ",";
                $contents .= "$tran[qty]" . ",";
                $contents .= "$tran[rate]" . ",";
                $contents .= "$tran[subtot]" . ",";
                $contents .= "$tran[cgstrate]" . ",";
                $contents .= "$tran[cgstamt]" . ",";
                $contents .= "$tran[sgstrate]" . ",";
                $contents .= "$tran[sgstamt]" . ",";
                $contents .= "$tran[igstrate]" . ",";
                $contents .= "$tran[igstamt]" . ",";
                $contents .= "$tran[total]" . "\n";

                $totqty = $totqty + $tran['qty'];
                $totrate = $totrate + $tran['rate'];
                $totamount = $totamount + $tran['amount'];
                $totcgstamt = $totcgstamt + $tran['cgstamt'];
                $totsgstamt = $totsgstamt + $tran['sgstamt'];
                $totigstamt = $totigstamt + $tran['igstamt'];
                $tottotal = $tottotal + $tran['total'];
            }
                                   
                                
                                    $contents.= " " . ",";
                                    $contents.= " " . ",";
                                    $contents.= " " . ",";
                                    $contents.= " " . ",";
                                    $contents.= "Total" . ",";
                                    $contents.= "$totqty" . ",";
                                    $contents.= "$totrate" . ",";
                                    $contents.= "$totamount" . ",";
                                    $contents.= "" . ",";
                                    $contents.= "$totcgstamt" . ",";
                                    $contents.= "" . ",";
                                    $contents.= "$totsgstamt" . ",";
                                    $contents.= "" . ",";
                                    $contents.= "$totigstamt" . ",";
                                    $contents.= "$tottotal" . ",";



        }
    }
  
 $contents .= "" . ",";
 $contents .= "" . ",";
 $contents .= "" . ",";
 $contents .= "" . ",";
 $contents .= "" . ",";
 $contents .= "" . ",";
 $contents .= "Total" . ",";
 $contents .= "$totbamt" . ",";

    $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;
    }
}
?>
   
