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

$formname = 'Invoice Register';
$file = 'invreg_fr.php';
$maindb = 'invoice';

$partycond = "";
$custcond = "";
$party = $_POST["account"];
$custcd = $_POST["custcode"];

if (!empty($party)) {
    $partycond = " AND `accode`= '$party'";
}
if (!empty($custcd)) {
    $custcond = " AND `custcode`= '$custcd'";
}


if (($_GET["fdt"]) != '') {
    $mfdt = $_GET["fdt"];
    $mtdt = $_GET["tdt"];
    $mfrmdate = date("y-m-d", strtotime($mfdt));
    $mtodate = date("y-m-d", strtotime($mtdt));
    $dtype = $_GET["rpt"];
} else {

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

    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>";
    }
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
}



$getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`invdate`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `invdate` >= '$mfrmdate' 
AND `invdate` <= '$mtodate' $custcond $partycond") 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>
                        <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:5%">Sr</th>
                                    <th style="width:8%">Invoice No.</th>
                                    <th style="width:12%">Invoice Date</th>
                                    <th style="width:12%">A/c No.</th>
                                    <th style="width:20%">A/c Name</th>
                                    <th style="width:10%;">Sub Total</th>
                                    <th style="width:11%;">Operating Chg.</th>
                                    <th style="width:8%;">Roff.</th>
                                    <th style="width:20%;">Bill Amt.</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $ctr = 0;
                                $totqty = 0;

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

                                    $getparty = mysqli_query($conn, "SELECT * FROM `accmast` where accode=$pmain[accode] ORDER BY srl") or die(mysqli_error($conn));
                                    $fetparty = mysqli_fetch_object($getparty);
                                    $party = $fetparty->name;
                                    $ctr++;
                                    echo "<tr id='boldfont'>";
                                    echo "<td>$ctr</td>";
                                    echo "<td>$pmain[invno]</td>";
                                    echo "<td>$pmain[mydt]</td>";
                                    echo "<td>$pmain[custcode]</td>";
                                    echo "<td>$party</td>";
                                    echo "<td style='text-align:right;'>$pmain[subtotal]</td>";
                                    echo "<td style='text-align:right;'>$pmain[operchgs]</td>";
                                    echo "<td style='text-align:right;'>$pmain[roff]</td>";
                                    echo "<td style='text-align:right;'>$pmain[billamt]</td>";
                                    echo "</tr>";

                                    $totamt = $totamt + $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 "<th style='width:5%;'>Sr</th>";
                                    //     Echo "<th style='width:10%;'>Barcode</th>";
                                    //     Echo "<th style='width:10%;' class='text-right'>Item Code</th>";
                                    //     Echo "<th style='width:15%;' class='text-right'>Item Name</th>";
                                    //     Echo "<th style='width:10%;' class='text-right'>HSN Code</th>";
                                    //     Echo "<th style='width:8%;' class='text-right'>Gram</th>";
                                    //     Echo "<th style='width:8%;' class='text-right'>Unit</th>";
                                    //     Echo "<th style='width:8%;' class='text-right'>Batch</th>";
                                    //     Echo "<th style='width:8%;' class='text-right'>Qty.</th>";
                                    //     Echo "<th style='width:20%;' class='text-right'>Remark</th>";
                                    //     Echo "<tr>";


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

                                    //         $ctr2++;
                                    //         Echo "<tr>";
                                    //         Echo "<td>$ctr2</td>";
                                    //         Echo "<td>$tran[barcode]</td>";
                                    //         Echo "<td>$tran[itemcode]</td>";
                                    //         Echo "<td>$tran[itemname]</td>";
                                    //         Echo "<td>$tran[hsncode]</td>";
                                    //         Echo "<td>$tran[gram]</td>";
                                    //         Echo "<td>$tran[unit]</td>";
                                    //         Echo "<td>$tran[batch]</td>";
                                    //         Echo "<td>$tran[qty]</td>";
                                    //         Echo "<td>$tran[remark]</td>";
                                    //         Echo "</tr>";

                                    //         $trtotqty = $trtotqty + $tran[qty];
                                    //     }
                                    //         Echo "<tr>";
                                    //         Echo "<td></td>";
                                    //         Echo "<td></td>";
                                    //         Echo "<td></td>";
                                    //         Echo "<td></td>";
                                    //         Echo "<td></td>";
                                    //         Echo "<td></td>";
                                    //         Echo "<td></td>";
                                    //         Echo "<td>Total</td>";
                                    //         Echo "<td>$trtotqty</td>";
                                    //         Echo "<td></td>";
                                    //         Echo "</tr>";

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

                                echo "<tr>";
                                echo "<td style='text-align:right' colspan='8'><b>Total</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 .= "Sr., Invoice No., Invoice Date, A/c No., A/c Name, Sub Total, Operating Chg., Roff., Bill Amt.\n";

    $ctr = 0;
    $totqty = 0;

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

        $getparty = mysqli_query($conn, "SELECT * FROM `accmast` where accode=$pmain[accode] ORDER BY srl") or die(mysqli_error($conn));
        $fetparty = mysqli_fetch_object($getparty);
        $party = $fetparty->name;
        $ctr++;

        $contents .= "$ctr" . ",";
        $contents .= "$pmain[invno]" . ",";
        $contents .= "$pmain[mydt]" . ",";
        $contents .= "$pmain[custcode]" . ",";
        $contents .= "$party" . ",";
        $contents .= "$pmain[subtotal]" . ",";
        $contents .= "$pmain[operchgs]" . ",";
        $contents .= "$pmain[roff]" . ",";
        $contents .= "$pmain[billamt]" . "\n";

        $totamt = $totamt + $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 "<th style='width:5%;'>Sr</th>";
        //     Echo "<th style='width:10%;'>Barcode</th>";
        //     Echo "<th style='width:10%;' class='text-right'>Item Code</th>";
        //     Echo "<th style='width:15%;' class='text-right'>Item Name</th>";
        //     Echo "<th style='width:10%;' class='text-right'>HSN Code</th>";
        //     Echo "<th style='width:8%;' class='text-right'>Gram</th>";
        //     Echo "<th style='width:8%;' class='text-right'>Unit</th>";
        //     Echo "<th style='width:8%;' class='text-right'>Batch</th>";
        //     Echo "<th style='width:8%;' class='text-right'>Qty.</th>";
        //     Echo "<th style='width:20%;' class='text-right'>Remark</th>";
        //     Echo "<tr>";


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

        //         $ctr2++;
        //         Echo "<tr>";
        //         Echo "<td>$ctr2</td>";
        //         Echo "<td>$tran[barcode]</td>";
        //         Echo "<td>$tran[itemcode]</td>";
        //         Echo "<td>$tran[itemname]</td>";
        //         Echo "<td>$tran[hsncode]</td>";
        //         Echo "<td>$tran[gram]</td>";
        //         Echo "<td>$tran[unit]</td>";
        //         Echo "<td>$tran[batch]</td>";
        //         Echo "<td>$tran[qty]</td>";
        //         Echo "<td>$tran[remark]</td>";
        //         Echo "</tr>";

        //         $trtotqty = $trtotqty + $tran[qty];
        //     }
        //         Echo "<tr>";
        //         Echo "<td></td>";
        //         Echo "<td></td>";
        //         Echo "<td></td>";
        //         Echo "<td></td>";
        //         Echo "<td></td>";
        //         Echo "<td></td>";
        //         Echo "<td></td>";
        //         Echo "<td>Total</td>";
        //         Echo "<td>$trtotqty</td>";
        //         Echo "<td></td>";
        //         Echo "</tr>";

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

    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $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;
    // }
}


if (isset($_POST["subpdf"])) {
    require("library/fpdf/fpdf.php");
    class pdf extends fpdf
    {
        function Footer()
        {
            //Position at 1.5 cm from bottom
            $this->SetY(-15);
            //Arial italic 8
            $this->SetFont('Arial', 'I', 8);
            //Page number
            $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
        }
    }
    // require('includes/head_rep_pdf.php');
    $pdf = new pdf('L', 'mm', 'A4');

    $pdf->AliasNbPages();

    $black = '0,0,0';
    $pdf->AddPage();
    $pdf->SetFont('Arial', 'B', 15);
    $pdf->SetTextColor($black);
    $pdf->SetFillColor(152, 249, 240);
    $pdf->Cell(0, 10, 'VIVEK AUTOMOBILES', 0, 0, 'L', 1);
    $pdf->Ln();

    $pdf->SetFont('Arial', 'B', 10);
    $pdf->SetTextColor(255, 0, 0);
    $pdf->Cell(0, 7, $formname, 0, 0, 'L', 1);
    $pdf->Ln();

    $pdf->SetFont('Arial', 'B', 10);
    $pdf->SetTextColor($black);
    $pdf->Cell(0, 7, "From Date - " . $mfdt . "To Date - " . $mtdt, 0, 0, 'L', 1);
    $pdf->Cell(0, 7, "Report Date - " . Date("d-m-y"), 0, 0, 'R', 1);
    $pdf->Ln();

    $pdf->SetFont('Arial', 'B', 10);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(120, 144, 156);
    $pdf->Cell(10, 8, 'Sr.', 1, 0, '', 1);
    $pdf->Cell(30, 8, 'Inv No.', 1, 0, '', 1);
    $pdf->Cell(30, 8, 'Inv Date', 1, 0, '', 1);
    $pdf->Cell(20, 8, 'A/c No.', 1, 0, '', 1);
    $pdf->Cell(70, 8, 'A/c Name', 1, 0, '', 1);
    $pdf->Cell(30, 8, 'Sub Total', 1, 0, '', 1);
    $pdf->Cell(30, 8, 'Operating Chg.', 1, 0, '', 1);
    $pdf->Cell(30, 8, 'Roff.', 1, 0, '', 1);
    $pdf->Cell(27, 8, 'Bill Amt.', 1, 0, '', 1);
    $pdf->Ln();
    $ctr = 0;
    $totqty = 0;
    $pdf->SetTextColor(0, 0, 0);
    $pdf->SetFillColor(255, 255, 255);

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

        $getparty = mysqli_query($conn, "SELECT * FROM `accmast` where accode=$pmain[accode] ORDER BY srl") or die(mysqli_error($conn));
        $fetparty = mysqli_fetch_object($getparty);
        $party = $fetparty->name;
        $ctr++;

        $pdf->SetFont('Arial', 'B', 10);
        $pdf->Cell(10, 8, $ctr, 1, 0, '', 1);
        $pdf->Cell(30, 8, $pmain['invno'], 1, 0, '', 1);
        $pdf->Cell(30, 8, $pmain['mydt'], 1, 0, '', 1);
        $pdf->Cell(20, 8, $pmain['custcode'], 1, 0, '', 1);
        $pdf->Cell(70, 8, $party, 1, 0, '', 1);
        $pdf->Cell(30, 8, $pmain['subtotal'], 1, 0, 'R', 1);
        $pdf->Cell(30, 8, $pmain['operchgs'], 1, 0, 'R', 1);
        $pdf->Cell(30, 8, $pmain['roff'], 1, 0, 'R', 1);
        $pdf->Cell(27, 8, $pmain['billamt'], 1, 0, 'R', 1);
        $pdf->Ln();

        $totamt = $totamt + $pmain['billamt'];
        $subtot = $subtot + $pmain['subtotal'];
        $opchgs = $opchgs + $pmain['operchgs'];

        // 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 "<th style='width:5%;'>Sr</th>";
        //     Echo "<th style='width:10%;'>Barcode</th>";
        //     Echo "<th style='width:10%;' class='text-right'>Item Code</th>";
        //     Echo "<th style='width:15%;' class='text-right'>Item Name</th>";
        //     Echo "<th style='width:10%;' class='text-right'>HSN Code</th>";
        //     Echo "<th style='width:8%;' class='text-right'>Gram</th>";
        //     Echo "<th style='width:8%;' class='text-right'>Unit</th>";
        //     Echo "<th style='width:8%;' class='text-right'>Batch</th>";
        //     Echo "<th style='width:8%;' class='text-right'>Qty.</th>";
        //     Echo "<th style='width:20%;' class='text-right'>Remark</th>";
        //     Echo "<tr>";


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

        //         $ctr2++;
        //         Echo "<tr>";
        //         Echo "<td>$ctr2</td>";
        //         Echo "<td>$tran[barcode]</td>";
        //         Echo "<td>$tran[itemcode]</td>";
        //         Echo "<td>$tran[itemname]</td>";
        //         Echo "<td>$tran[hsncode]</td>";
        //         Echo "<td>$tran[gram]</td>";
        //         Echo "<td>$tran[unit]</td>";
        //         Echo "<td>$tran[batch]</td>";
        //         Echo "<td>$tran[qty]</td>";
        //         Echo "<td>$tran[remark]</td>";
        //         Echo "</tr>";

        //         $trtotqty = $trtotqty + $tran[qty];
        //     }
        //         Echo "<tr>";
        //         Echo "<td></td>";
        //         Echo "<td></td>";
        //         Echo "<td></td>";
        //         Echo "<td></td>";
        //         Echo "<td></td>";
        //         Echo "<td></td>";
        //         Echo "<td></td>";
        //         Echo "<td>Total</td>";
        //         Echo "<td>$trtotqty</td>";
        //         Echo "<td></td>";
        //         Echo "</tr>";

        //     Echo "</table>";
        //     Echo "</td>";
        // }
    }
    $pdf->SetTextColor(255, 0, 0);
    $pdf->SetFillColor(255, 255, 255);
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(10, 8, "", 1, 0, '', 1);
    $pdf->Cell(30, 8, "", 1, 0, '', 1);
    $pdf->Cell(30, 8, "", 1, 0, '', 1);
    $pdf->Cell(20, 8, "", 1, 0, '', 1);
    $pdf->Cell(70, 8, "Total", 1, 0, 'R', 1);
    $pdf->Cell(30, 8, $subtot, 1, 0, 'R', 1);
    $pdf->Cell(30, 8, $opchgs, 1, 0, 'R', 1);
    $pdf->Cell(30, 8, "", 1, 0, '', 1);
    $pdf->Cell(27, 8, $totamt, 1, 0, 'R', 1);
    $pdf->Ln();

    $pdf->Output('Invoice Register', 'I');
}
?>