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

$maindb = "expense";
//$trandb = "samptran";
$formname = "Expense";
$file = "expensevoch_print_fr.php";

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

$name = $_POST["pname"];
$dtype = $_POST["dtype"];
$narr = $_POST["narr"];

//browse($dtype);
//browse($narr);
$namecond = "";
$dtypecond = "";
$narrcond = "";

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

if (!empty($name)) {
    $namecond = " AND `paidto`='$name' ";
}

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

$getpmain = mysqli_query($conn,"SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $maindb a  WHERE vouchno != '' 
                         AND a.`vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'  $namecond ")or die(mysqli_error($conn));
//print "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $maindb a  WHERE vouchno != '' 
//                         AND a.`vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'  $namecond ";

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

    include("includes/linkshead.php");
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
    </style>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </head>
    <body>
        <div class="container"style="width:800px">
            <?php include("includes/head_rep.php"); ?>
            <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 style="background-color: #59CAF7;">
                                <th style="width:10%">Sr</th>
                                <th style="width:65%" class="text-center">Expense A/C</th>
                                <th style="width:25%" class="text-right">Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totqty = 0;
                            $total = 0;

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

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

                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td class='text-center'>$party";
                                if ($_POST["narr"] == 'Y') {
                                    echo '<br>' . $pmain[remark1] . $pmain[remark2];
                                }
                                echo '</td>';
                                Echo "<td class='text-right'>$pmain[amount]</td>";

                                Echo "</tr>";
                                $total = $total + $pmain[amount];

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

                                    Echo "<tr style='height:8px;'>";
                                    Echo "<td  colspan='25'>";
                                    Echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                    Echo "<th style='background-color: #6e9393;width:10%;'>Sr</th>";
                                    Echo "<th style='background-color: #6e9393;width:15%;text-align:center' >Date</th>";
                                    Echo "<th style='background-color: #6e9393;width:10%;text-align:center' >Voucher</th>";
                                    Echo "<th style='background-color: #6e9393;width:45%;text-align:center'>Expense A/c</th>";
                                    Echo "<th style='background-color: #6e9393;width:20%;' class='numeric'>Amount</th>";
                                    Echo "<tr>";


                                    $ctr2 = 0;
                                    $tqty = 0;
                                    $tamt = 0;

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

                                    $ctr2++;
                                    Echo "<tr>";
                                    Echo "<td>$ctr2</td>";
                                    Echo "<td style='text-align:center'>$pmain[vouchdt]</td>";
                                    Echo "<td style='text-align:center'>$pmain[vouchno]</td>";
                                    Echo "<td style='text-align:center'>$party</td>";
//                                        if ($_POST["narr"] == 'Y') {
//                                            echo '<br>' . $pmain[remark1] . $pmain[remark2];
//                                        }
//                                        echo '</td>';
                                    Echo "<td class='numeric'>$pmain[amount]</td>";
                                    Echo "</tr>";
//                                        $tqty = $tqty + $tran[trqty];
                                    $tamt = $tamt + $pmain[amount];
//                                    }
                                    if ($_POST["dtype"] == 2) {
                                        $colr1 = "#59CAF7";

                                        Echo "<tr>";
                                        Echo "<td></td>";
                                        Echo "<td class='numeric'></td>";
                                        Echo "<td class='numeric'></td>";
                                        Echo "<td style='color:$colr1;' align='right' id='font1' ><h5><b>Total :</b></h5></td>";
                                        Echo "<td style='color:$colr1;' align='right' id='font1'  ><h5><b>$tamt</b></h5></td>";
                                        Echo "</tr>";
                                    }
                                    Echo "</table>";
                                    Echo "</td>";
                                }
                            }
//                               
                            $colr = "red";
                            echo "<tr> ";
//                            echo "<td align='right' style='color:$colr;' id='font' colspan='3'><h4><b></b></h4></td>";
//                            echo "<td align='right' style='color:$colr;' id='font' colspan='3'><h4><b></b></h4></td>";
                            echo "<td align='right' style='border-right-style:none;' id='font'><h4><b></b></h4></td>";
                            echo "<td align='right' style='color:$colr;border-left-style:none;' id='font'><h4><b>Grand Total</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$total</b></h4></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

//    include ("includes/head_rep_excel.php");
    include 'includes/head_rep_excel.php';

    $contents .= "Sr.,Expense AC,Amount\n";

    while ($pmain = mysqli_fetch_array($getpmain)) {
        $gparty = mysqli_query($conn,"SELECT `name`, `accode` FROM `accmast` WHERE `accode`='$pmain[paidto]' ")or die(mysqli_error($conn));
        $sparty = mysqli_fetch_array($gparty);

        $party = $sparty["name"];
        $pvouchno = $pmain[vouchno];
        $pdate = $pmain[vouchdt];
        $ppaidto = $pmain[paidto];
        $pamt = $pmain[amount];
        $premark = $pmain[remark1] . $pmain[remark2];
        $ctr = 0;
        $gtot = 0;
        
        $ctr++;
        $contents .= $ctr . ",";
//        $contents .= $pvouchno . ",";
//        $contents .= $pdate . ",";
        $contents .= $party . ",";
//        $contents .= $ptotal . ",";
        $contents .= $pamt . "\n";

        $gtot = $pamt + $gtot;

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

            $contents .= "Sr.,Date,Voucher,Expense AC,Amount\n";

            $ctr2 = 0;
//            $tqty = 0;
            $tamt = 0;

//            $gettran = mysqli_query($conn,"SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' $itemcond $itemnamecond  ")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));
//                $item = mysqli_fetch_array($getitem);

            $ctr2++;
//                $pitemcode = $tran[itemcode];
            $pvouchdt = $pmain[vouchdt];
            $pvouchno = $pmain[vouchno];
            $ppaidto = $pmain[paidto];
            $pamount = $pmain[amount];

            $contents .= $ctr2 . ",";
            $contents .= $pvouchdt . ",";
            $contents .= $pvouchno . ",";
            $contents .= $party . ",";
            $contents .= $pamount . "\n";
//                $contents .= $pmsp . "\n";
//                $tqty = $tqty + $ptrqty;
            $tamt = $tamt + $pamount;
//            }

            $contents .= " " . ",";
            $contents .= " " . ",";
            $contents .= " " . ",";
            $contents .= "Total" . ",";
//            $contents .= "$tqty" . ",";
//            $contents .= " " . ",";
//            $contents .= " " . ",";
//            $contents .= " " . ",";
            $contents .= "$tamt" . "\n";
//            $contents .= " " . "\n";
        }
    }
//    $contents .= " " . ",";
//    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= "Grand Total" . ",";
    $contents .= "$gtot" . "\n";
//    $contents .= " " . "\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"])) {

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

    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');
        }

    }

    $pdf = new pdf('L', 'mm', 'A4');

    $pdf->AliasNbPages();

    $pdf->AddPage();
    include("includes/head_rep_pdf.php");
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->SetFillColor(64, 37, 91);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->Cell(10, 7, "Sr.", 1, 0, '', 1);
    $pdf->Cell(15, 7, "Voucher", 1, 0, '', 1);
    $pdf->Cell(30, 7, "Date", 1, 0, '', 1);
    $pdf->Cell(40, 7, "Qty.", 1, 0, 'R', 1);
    $pdf->Cell(40, 7, "Total", 1, 0, 'R', 1);
    $pdf->Cell(143, 7, "Remark", 1, 0, '', 1);
    $pdf->SetFont('Arial', '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();

    $ctr = 0;
    $totqty = 0;
    $total = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {
        $ctr++;
        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(10, 7, $ctr, 1);
        $pdf->Cell(15, 7, $pmain[vouchno], 1);
        $pdf->Cell(30, 7, $pmain[mydt], 1);
        $pdf->Cell(40, 7, $pmain[qty], 1, 0, 'R');
        $pdf->Cell(40, 7, $pmain[total], 1, 0, 'R');
        $pdf->Cell(143, 7, $pmain[remark1] . $pmain[remark2], 1);
        $pdf->Ln();

        $totqty = $totqty + $pmain[qty];
        $total = $total + $pmain[total];

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

            $pdf->SetFont('Arial', 'B', 10);
            $pdf->SetFillColor(204, 204, 204);
            $pdf->SetTextColor(255, 255, 255);
            $pdf->Cell(8, 7, "Sr.", 1, 0, '', 1);
            $pdf->Cell(20, 7, "Barcode", 1, 0, '', 1);
            $pdf->Cell(100, 7, "Itemname", 1, 0, '', 1);
            $pdf->Cell(30, 7, "Qty.", 1, 0, 'R', 1);
            $pdf->Cell(30, 7, "Unit", 1, 0, 'R', 1);
            $pdf->Cell(30, 7, "Mrp", 1, 0, 'R', 1);
            $pdf->Cell(30, 7, "Rate", 1, 0, 'R', 1);
            $pdf->Cell(30, 7, "Amount", 1, 0, 'R', 1);
            $pdf->SetFont('Arial', '', 1, 0);

            $pdf->SetTextColor(0, 0, 0);
            $pdf->Ln();

            $ctr2 = 0;
            $tqty = 0;
            $tamt = 0;

            $gettran = mysqli_query($conn,"SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' ORDER BY `itemcode` ")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));
                $item = mysqli_fetch_array($getitem);

                $ctr2++;
                $pdf->SetFont('Arial', '', 8);
                $pdf->Cell(8, 7, $ctr2, 1);
                $pdf->Cell(20, 7, $tran[barcode], 1);
                $pdf->Cell(100, 7, $item[name], 1);
                $pdf->Cell(30, 7, $tran[trqty], 1, 0, 'R');
                $pdf->Cell(30, 7, $tran[unit], 1, 0, 'R');
                $pdf->Cell(30, 7, $tran[mrp], 1, 0, 'R');
                $pdf->Cell(30, 7, $tran[rate], 1, 0, 'R');
                $pdf->Cell(30, 7, $tran[amount], 1, 0, 'R');
                $pdf->Ln();

                $tqty = $tqty + $tran[trqty];
                $tamt = $tamt + $tran[amount];
            }

            if ($_POST["dtype"] == 2) {
                $pdf->SetFont('Arial', 'B', 10);
                $pdf->SetFillColor(255, 255, 255);
                $pdf->SetTextColor(0, 128, 0);
                $pdf->Cell(128, 7, "Total", 1, 0, 'R');
                $pdf->Cell(30, 7, $tqty, 1, 0, 'R');
                $pdf->Cell(30, 7, " ", 1, 0, 'R');
                $pdf->Cell(30, 7, " ", 1, 0, 'R');
                $pdf->Cell(30, 7, " ", 1, 0, 'R');
                $pdf->Cell(30, 7, $tamt, 1, 0, 'R');
                $pdf->SetFont('Arial', '', 1, 0);

                $pdf->SetTextColor(0, 0, 0);
                $pdf->Ln();
            }
        }
    }
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->SetFillColor(255, 255, 255);
    $pdf->SetTextColor(0, 0, 255);
    $pdf->Cell(55, 7, "Grand Total", 1, 0, 'R', 1);
    $pdf->Cell(40, 7, $totqty, 1, 0, 'R', 1);
    $pdf->Cell(40, 7, $total, 1, 0, 'R', 1);
    $pdf->Cell(143, 7, " ", 1, 0, '', 1);
    $pdf->Ln();
    $pdf->SetFont('Arial', '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->Output("freesamples.pdf", "D");
}
?>