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

$maindb = 'bankpay';
$actran = 'actran';
$formname = "Expense Report";
$file = "expenserep_fr.php";

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

$group = $_POST["group"];
$acname = $_POST["acname"];
$cust = $_POST["cust"];
$staff = $_POST["staff"];
$fdate = $_POST["fdate"];
$tdate = $_POST["tdate"];
$dtype = $_POST["dtype"];

$gcond = '';
$mcond = '';
$cucond = '';
$stcond = '';
$fdatecond = '';
$tdatecond = '';

if (!empty($group)) {
    $gcond = " AND `grpcode`='$group' ";
    $gcond1 = " AND a.`grpcode`='$group' ";
}

if (!empty($acname)) {
    $mcond = " AND `accode`='$acname' ";
    $mcond1 = " AND a.`accode`='$acname' ";
}

if (!empty($cust)) {
    $cucond = " AND `accode`='$cust' ";
}
if (!empty($staff)) {
    $stcond = " AND `stafcode`='$staff' ";
}

$mord = " order by accode, date ";
$dcond = " and `date` >= '$fdate' and `date` <= '$tdate' ";
$dcond1 = " and b.`date` >= '$fdate' and b.`date` <= '$tdate' ";

//    browse($dcond);

if (empty($fdate)) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; expenserep.php");
}

if (empty($tdate)) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; expenserep.php");
}



$mfrmdate = date("d-m-Y", strtotime($fdate));
$mtodate = date("d-m-Y", strtotime($tdate));

$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));
if (isset($_POST["view"])) {

    include("includes/linkshead.php");
?>
    <style>
        @media print {

            .no-print,
            .no-print * {
                display: none !important;
            }
        }
    </style>
    </head>

    <body>
        <div class="container">
            <?php
            include("includes/head_rep.php");
            ?>
            <div class="row">
                <div class="col-lg-4 col-md-2 col-sm-2 col-xs-2 pad">
                    <label> <?php echo $cashsale; ?></label>
                </div>
            </div>
            <?php
            if ($dtype == 1) {
                $getcash = mysqli_query($conn, "select * from `accmast` where accode != '' $gcond $mcond and  
                            grpcode in (select grpcode from grpmast where UPPER(type) = 'E' and UPPER(subtype) = 'E' ) ORDER BY `name`") or die(mysqli_error($conn));
            ?>
                <table style="width:100%;background: white;" class="table table-condensed table-striped table-bordered table-hover">
                    <thead>
                        <tr style="background-color:#8F4131">
                            <th style="width:10%;">Sr. No.</th>
                            <th style="width:30%;">Name </th>
                            <th style="width:10%;" class='text-right'>Amount</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $ctr = 0;
                        $totamt = 0;

                        while ($pmain = mysqli_fetch_array($getcash)) {
                            $getpart = mysqli_query($conn, "Select accode, sum(amount) as tamt from `actran` where `accode` = '$pmain[accode]' $dcond GROUP BY accode") or die(mysqli_error($conn));

                            while ($cmain = mysqli_fetch_array($getpart)) {

                                $getparty = mysqli_query($conn, "SELECT `name`, `accode` FROM accmast WHERE `accode`='$cmain[accode]'") or die(mysqli_error($conn));
                                $showparty = mysqli_fetch_array($getparty);

                                $mamt = abs($cmain['tamt']);
                                $ctr++;
                                echo "<tr>";
                                echo "<td>$ctr</td>";
                                echo "<td>$showparty[name]</td>";
                                echo "<td class='text-right'>$mamt</td>";
                                echo "</tr>";
                                $totamt = $totamt + $mamt;
                            }
                        }

                        echo "<tr>";
                        echo "<td></td>";
                        echo "<td class='text-right'><b>Total</b></td>";
                        echo "<td class='text-right'><b>$totamt</b></td>";
                        echo "</tr>";
                        ?>
                    </tbody>
                </table>
            <?php
            } else {
                $getcash = mysqli_query($conn, "select a.accode, a.name,b.source,date_format(b.`date`,'%d/%m/%Y') as mydt,
                                                     b.vouchno,b.amount  from `accmast` a, actran b where a.accode = b.accode $gcond1 $mcond1 $dcond1 and
                                                     a.grpcode in (select grpcode from grpmast where UPPER(type) = 'E' and UPPER(subtype) = 'E' ) order by b.date") or die(mysqli_error($conn));
            ?>
                <table style="width:100%;background: white;" class="table table-condensed table-striped table-bordered table-hover">
                    <thead>
                        <tr style="background-color: #8F4131">
                            <th style="width:10%;">Sr. No.</th>
                            <th style="width:15%;">Date</th>
                            <th style="width:15%;">Voucher No.</th>
                            <th style="width:35%;">Name </th>
                            <th style="width:15%;" class='text-right'>Amount</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $ctr = 0;
                        $totamt = 0;

                        while ($pmain = mysqli_fetch_array($getcash)) {
                            //                                    $getpart = mysqli_query($conn, "Select *, date_format(`date`,'%d/%m/%Y') as mydt from `actran` where `accode` = '$pmain[accode]'  $dcond ORDER BY `date` ASC")or die(mysqli_error($conn));
                            //                                    $getpart = mysqli_query($conn, "select * from `actran` where amount!='' $dcond $cucond $stcond and accode in (select accode from $getcash) $mord")or die(mysqli_error($conn));
                            //        browse(mysqli_num_rows($getpart));
                            //                                    while ($cmain = mysqli_fetch_array($getpart)) {
                            //
                            //                                        $getparty = mysqli_query($conn, "SELECT `name`, `accode` FROM accmast WHERE `accode`='$cmain[accode]'")or die(mysqli_error($conn));
                            //                                        $showparty = mysqli_fetch_array($getparty);


                            $mamt = abs($pmain['amount']);
                            $ctr++;
                            echo "<tr>";
                            echo "<td>$ctr</td>";
                            echo "<td>$pmain[mydt]</td>";
                            echo "<td>$pmain[source] $pmain[vouchno]</td>";
                            echo "<td>$pmain[name]</td>";
                            echo "<td class='text-right'>$mamt</td>";
                            echo "</tr>";
                            $totamt = $totamt + $mamt;
                            //                                    }
                        }

                        echo "<tr>";
                        echo "<td></td>";
                        echo "<td></td>";
                        echo "<td></td>";
                        echo "<td class='text-right'><b>Total</b></td>";
                        echo "<td class='text-right'><b>$totamt</b></td>";
                        echo "</tr>";
                        ?>
                    </tbody>
                </table>
            <?php } ?>
            <?php
            include("includes/linksfoot.php");
            ?>
    </body>

    </html>
<?php
}

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



    if ($dtype == 1) {

        $getcash = mysqli_query($conn, "select * from `accmast` where accode != '' $gcond $mcond and  
                            grpcode in (select grpcode from grpmast where UPPER(type) = 'E' and UPPER(subtype) = 'E' )") or die(mysqli_error($conn));
        $contents .= "Sr.No,Name,Amount,\n";

        $ctr = 1;
        $totamt = 0;

        while ($pmain = mysqli_fetch_array($getcash)) {
            $getpart = mysqli_query($conn, "Select accode, sum(amount) as tamt from `actran` where `accode` = '$pmain[accode]' $dcond GROUP BY accode") or die(mysqli_error($conn));

            while ($cmain = mysqli_fetch_array($getpart)) {

                $getparty = mysqli_query($conn, "SELECT `name`, `accode` FROM accmast WHERE `accode`='$cmain[accode]'") or die(mysqli_error($conn));
                $showparty = mysqli_fetch_array($getparty);

                $mamt = abs($cmain['tamt']);

                $spname = $showparty['name'];

                $contents .= $ctr . ",";
                $contents .= $spname . ",";
                $contents .= $mamt . "\n";
                $ctr++;

                $totamt = $totamt + $mamt;
            }
        }
        $contents .= " " . ",";
        $contents .= "Total" . ",";
        $contents .= $totamt . "\n";
    } else {



        $contents .= "Sr.No,Date,Voucher,Name,Amount,\n";

        $ctr = 1;
        $totamt = 0;

        $getcash = mysqli_query($conn, "select a.accode, a.name,b.source,date_format(b.`date`,'%d/%m/%Y') as mydt,
                                                     b.vouchno,b.amount  from `accmast` a, actran b where a.accode = b.accode $gcond1 $mcond1 $dcond1 and
                                                     a.grpcode in (select grpcode from grpmast where UPPER(type) = 'E' and UPPER(subtype) = 'E' ) order by b.date") or die(mysqli_error($conn));

        while ($pmain = mysqli_fetch_array($getcash)) {
            //            $getpart = mysqli_query($conn, "Select *, date_format(`date`,'%d/%m/%Y') as mydt from `actran` where `accode` = '$pmain[accode]' $dcond")or die(mysqli_error($conn));
            //
            //
            //            while ($cmain = mysqli_fetch_array($getpart)) {
            //                $getparty = mysqli_query($conn, "SELECT `name`, `accode` FROM accmast WHERE `accode`='$cmain[accode]'")or die(mysqli_error($conn));
            //                $showparty = mysqli_fetch_array($getparty);


            $mamt = abs($pmain['amount']);

            $cmydt = $pmain['mydt'];
            $csourcev = $pmain['source'] . $pmain['vouchno'];
            $spartyname = $pmain['name'];

            $contents .= $ctr . ",";
            $contents .= $cmydt . ",";
            $contents .= $csourcev . ",";
            $contents .= $spartyname . ",";
            $contents .= $mamt . "\n";
            $ctr++;

            $totamt = $totamt + $mamt;
        }
        $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"])) {

    $group = $_POST["group"];
    $acname = $_POST["acname"];
    $cust = $_POST["cust"];
    $staff = $_POST["staff"];
    $fdate = $_POST["fdate"];
    $tdate = $_POST["tdate"];
    $dtype = $_POST["dtype"];

    $gcond = '';
    $mcond = '';
    $cucond = '';
    $stcond = '';
    $fdatecond = '';
    $tdatecond = '';

    if (!empty($group)) {
        $gcond = " AND `grpcode`='$group' ";
    }

    if (!empty($acname)) {
        $mcond = " AND `accode`='$acname' ";
    }

    if (!empty($cust)) {
        $cucond = " AND `accode`='$cust' ";
    }
    if (!empty($staff)) {
        $stcond = " AND `stafcode`='$staff' ";
    }

    $mord = " order by accode, date ";
    $dcond = " and `date` >= '$fdate' and `date` <= '$tdate' ";

    if (empty($fdate)) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; expenserep.php");
    }

    if (empty($tdate)) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; expenserep.php");
    }



    $mfrmdate = date("d-m-Y", strtotime($fdate));
    $mtodate = date("d-m-Y", strtotime($tdate));

    $getcash = mysqli_query($conn, "select * from `accmast` where accode != '' $gcond $mcond and  
                            grpcode in (select grpcode from grpmast where UPPER(type) = 'E' and UPPER(subtype) = 'E' )") or die(mysqli_error($conn));

    $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('P', 'mm', 'A4');

    $pdf->AliasNbPages();

    $pdf->AddPage();

    $pdf->SetFont('Arial', 'I', 10);
    $pdf->SetTextColor(96, 130, 191);
    $pdf->SetFillColor(255, 255, 255);
    $pdf->Cell(193, 7, $cashsale, 1, 0, '', 1);
    $pdf->SetFont('Arial', '', 1, 0);

    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();
    if ($dtype == 1) {

        $pdf->SetFont('Arial', 'I', 10);
        $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFillColor(96, 130, 191);
        $pdf->Cell(8, 7, "Sr.", 1, 0, '', 1);
        $pdf->Cell(120, 7, "Name", 1, 0, '', 1);
        $pdf->Cell(65, 7, "Amount", 1, 0, '', 1);
        $pdf->SetFont('Arial', '', 1, 0);

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

        $ctr = 0;
        $totamt = 0;

        while ($pmain = mysqli_fetch_array($getcash)) {
            $getpart = mysqli_query($conn, "Select accode, sum(amount) as tamt from `actran` where `accode` = '$pmain[accode]' $dcond GROUP BY accode") or die(mysqli_error($conn));

            while ($cmain = mysqli_fetch_array($getpart)) {

                $getparty = mysqli_query($conn, "SELECT `name`, `accode` FROM accmast WHERE `accode`='$cmain[accode]'") or die(mysqli_error($conn));
                $showparty = mysqli_fetch_array($getparty);

                $mamt = abs($cmain[tamt]);
                $ctr++;
                $pdf->SetFont('Arial', '', 8);
                $pdf->Cell(8, 7, "$ctr", 1);
                $pdf->Cell(120, 7, "$showparty[name]", 1);
                $pdf->Cell(65, 7, "$mamt", 1);
                $pdf->Ln();
                $totamt = $totamt + $mamt;
            }
        }
        $pdf->SetFont('Arial', 'I', 10);
        $pdf->SetTextColor(255, 0, 0);
        $pdf->SetFillColor(255, 255, 255);
        $pdf->Cell(8, 7, " ", 1, 0, '', 1);
        $pdf->Cell(120, 7, "Total", 1, 0, '', 1);
        $pdf->Cell(65, 7, "$totamt", 1, 0, '', 1);
        $pdf->SetFont('Arial', '', 1, 0);

        $pdf->SetTextColor(0, 0, 0);
        $pdf->Ln();
    } else {

        $pdf->SetFont('Arial', 'I', 10);
        $pdf->SetTextColor(255, 255, 255);
        $pdf->SetFillColor(96, 130, 191);
        $pdf->Cell(8, 7, "Sr.", 1, 0, '', 1);
        $pdf->Cell(30, 7, "Date", 1, 0, '', 1);
        $pdf->Cell(30, 7, "Voucher", 1, 0, '', 1);
        $pdf->Cell(85, 7, "Name", 1, 0, '', 1);
        $pdf->Cell(40, 7, "Amount", 1, 0, '', 1);
        $pdf->SetFont('Arial', '', 1, 0);

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

        $ctr = 0;
        $totamt = 0;

        while ($pmain = mysqli_fetch_array($getcash)) {
            $getpart = mysqli_query($conn, "Select *, date_format(`date`,'%d/%m/%Y') as mydt from `actran` where `accode` = '$pmain[accode]' $dcond ") or die(mysqli_error($conn));

            while ($cmain = mysqli_fetch_array($getpart)) {

                $getparty = mysqli_query($conn, "SELECT `name`, `accode` FROM accmast WHERE `accode`='$cmain[accode]'") or die(mysqli_error($conn));
                $showparty = mysqli_fetch_array($getparty);

                $mamt = abs($cmain[amount]);
                $ctr++;
                $pdf->SetFont('Arial', '', 8);
                $pdf->Cell(8, 7, "$ctr", 1);
                $pdf->Cell(30, 7, "$cmain[mydt]", 1);
                $pdf->Cell(30, 7, "$cmain[source]" . $cmain['vouchno'], 1);
                $pdf->Cell(85, 7, "$showparty[name]", 1);
                $pdf->Cell(40, 7, "$mamt", 1);
                $pdf->Ln();
                $totamt = $totamt + $mamt;
            }
        }

        $pdf->SetFont('Arial', 'I', 10);
        $pdf->SetTextColor(255, 0, 0);
        $pdf->SetFillColor(255, 255, 255);
        $pdf->Cell(8, 7, " ", 1, 0, '', 1);
        $pdf->Cell(30, 7, " ", 1, 0, '', 1);
        $pdf->Cell(30, 7, " ", 1, 0, '', 1);
        $pdf->Cell(85, 7, "Total", 1, 0, '', 1);
        $pdf->Cell(40, 7, "$totamt", 1, 0, '', 1);
        $pdf->SetFont('Arial', '', 1, 0);

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