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

$maindb = 'othrecp';

$formname = "Other Receipt Register";
$file = "othrecpreg_fr.php";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];

$fseries = $_SESSION["series_" . $dbname];
$keycond = " and `series`='$fseries'";

mysqli_select_db($conn, $zdb);

$supp = $_POST["supp"];
$branch = $_POST["company"];
$bank = $_POST["bank"];

$suppcond = "";
$branchcond = "";
$bankcond = "";

$mfrmdate = trim($_POST["fromdt"]);
$mtodate = trim($_POST["todt"]);
if (empty($_POST["fromdt"])) {
    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>";
}

if (!empty($supp)) {
    $suppcond = " AND `party` = '$supp' ";
}

if (!empty($branch)) {
    $branchcond = " AND `compname`='$branch' ";
}

if (!empty($bank)) {
    $bankcond = " AND `bankname`='$bank' ";
}

$getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`,'%d-%m-%Y') as vouchdt FROM `$maindb` WHERE
                                  vouchno !='' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $keycond
                                   $branchcond $bankcond $suppcond ") or die(mysqli_error($conn));


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

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

            .no-print,
            .no-print * {
                display: none !important;
            }
        }

        .th1 {
            background-color: #78909C;
        }

        .th2 {
            background-color: #85AFC3;
        }
    </style>
    </head>

    <body>
        <div class="container">

            <?php
            include("includes/head_rep.php");
            ?>


            <div class="col-lg-12 col-sm-12 pad">
                <table style="width:100%;background: white;" class="table table-condensed table-hover table-striped table-bordered">
                    <thead class="mySearchTr">
                        <tr style="">
                            <th style="width:3%;">Sr</th>
                            <th style="width:6%;">Voucher</th>
                            <th style="width:7%;">Date</th>
                            <th style="width:10%;">Bank/Cash  Name</th>
<!--                            <th style="width:7%;">Receipt #</th>
                            <th style="width:7%;">Cheque #</th>
                            <th style="width:7%;">Chq Date</th>-->
                            <!--<th style="width:8%;">For</th>-->
                            <th style="width:10%;">Account Name</th>
                            <!--<th style="width:5%;">Advance</th>-->
                            <th style="width:7%;text-align: right">Amount</th>
                            <!--<th style="width:8%">Company</th>-->
                             <th style="width:10%" colspan="2" >Remarks</th> 
                            <!--<th style="width:3%">Action</th>-->
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $ctr = 0;
                        $gtot = 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));
                            $sparty = mysqli_fetch_array($gparty);

                            $gbank = mysqli_query($conn, "SELECT accode, name FROM `accmast` where accode='$pmain[bankname]'") or die(mysqli_error($conn));
                            $sbank = mysqli_fetch_array($gbank);




                            $ctr++;

                            echo "<tr>";
                            echo "<td><b>$ctr</b></td>";
                            echo "<td><b>$pmain[vouchno]</b></td>";
                            echo "<td><b>$pmain[vouchdt]</b></td>";
                            echo "<td><b>$sbank[name]</b></td>";
//                            echo "<td><b>$pmain[recpno]</b></td>";
//                            echo "<td><b>$pmain[chqno]</b></td>";
//                            echo "<td><b>$pmain[chqdate]</b></td>";
//                            echo "<td><b>$fortype</b></td>";
                            echo "<td><b>$sparty[name]</b></td>";
//                            echo "<td class='text-right'><b>$pmain[ordno]</b></td>";
                            echo "<td class='text-right'><b>$pmain[amount]</b></td>";
//                            echo "<td><b>$scomp[compname]</b></td>";
                             echo "<td class=''><b>$pmain[remark1]</b></td>";
                            // echo "<td class='text-right'><b>$pmain[remark2]</b></td>";
//                            echo "<td class='text-center'><a href=\"bankpay.php?mode=edit&vouchno=$pmain[vouchno]\" target='_blank' >
//                                    <i class='fa fa-pencil' style='font-size:1.3em;color:#00C0EF;cursor:pointer;'></i></a></td>";
                            echo "</tr>";

                            $gtot = $gtot + $pmain['amount'];
                        }
                        echo "<tr>";
                        echo "<td class='text-right text-danger' colspan='5'><b>Total</b></td>";
                        echo "<td class='text-right'><b>$gtot</b></td>";
//                        echo "<td colspan='5'><b></b></td>";
                        echo "</tr>";
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
        <?php include("includes/linksfoot.php"); ?>
    </body>

    </html>
<?php
}


if (isset($_POST["subpdf"])) {
    browse("");
    header("refresh:1; bankpayreg.php");
    $supp = $_POST["supp"];
    $branch = $_POST["company"];
    $frmdt = $_POST["frmdt"];
    $todt = $_POST["todt"];
    $detail = $_POST["detail"];



    $suppcond = "";
    $branchcond = "";

    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`,'%d-%m-%Y') as vouchdt FROM `$maindb` WHERE vouchno !=''
            and `vouchdt` >= '$frmdt' AND `vouchdt` <= '$todt' $suppcond $suppcond ") or die(mysqli_error($conn));


    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();
    $pdf->SetFont('Arial', 'B', 15);
    $pdf->SetTextColor(108, 105, 104);
    $pdf->Cell(0, 10, 'BEST MARINE PVT LTD', 0);
    $pdf->Ln();
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->SetTextColor(108, 105, 104);
    $pdf->Cell(0, 10, '112 MINT ROAD FORT MARKET', 0);
    $pdf->Ln();
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->SetTextColor(108, 105, 104);
    $pdf->Cell(200, 7, "Print - " . $formname, 0, 'R');
    $pdf->Cell(100, 7, "Report Date - " . Date("d-m-y"), 0, 'L');
    $pdf->Ln();
    $pdf->SetFont('Arial', '', 10);
    $pdf->SetTextColor(108, 105, 104);
    $pdf->Cell(50, 10, 'From Date' . $mfrmdate, 0);
    $pdf->SetFont('Arial', '', 10);
    $pdf->SetTextColor(108, 105, 104);
    $pdf->Cell(0, 10, 'To Date' . $mtodate, 0);
    $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(25, 8, 'OrderNo', 1, 0, '', 1);
    $pdf->Cell(30, 8, 'OrderDate', 1, 0, '', 1);
    $pdf->Cell(60, 8, 'Supplier', 1, 0, '', 1);
    $pdf->Cell(50, 8, 'Company', 1, 0, '', 1);
    $pdf->Cell(20, 8, 'CreditDays', 1, 0, '', 1);
    $pdf->Cell(30, 8, 'GivenBy', 1, 0, '', 1);
    $pdf->Cell(25, 8, 'Approvedby', 1, 0, '', 1);
    $pdf->Cell(25, 8, 'Total', 1, 0, '', 1);
    $pdf->Ln();


    $ctr = 0;
    $gtot = 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));
        $sparty = mysqli_fetch_array($gparty);

        $gcomp = mysqli_query($conn, "SELECT code, compname FROM `branch` where code='$pmain[company]'") or die(mysqli_error($conn));
        $scomp = mysqli_fetch_array($gcomp);

        $ggiven = mysqli_query($conn, "SELECT code, name FROM `staff` where code='$pmain[given]'") or die(mysqli_error($conn));
        $sgiven = mysqli_fetch_array($ggiven);

        $gapprov = mysqli_query($conn, "SELECT code, name FROM `staff` where code='$pmain[approved]'") or die(mysqli_error($conn));
        $sapprov = mysqli_fetch_array($gapprov);



        $ctr++;


        $pdf->SetFont('Arial', '', 7);
        $pdf->SetTextColor(0, 0, 0);
        $pdf->Cell(10, 8, $ctr, 1, 0);
        $pdf->Cell(25, 8, $pmain['vouchno'], 1, 0);
        $pdf->Cell(30, 8, $pmain['vouchdt'], 1, 0);
        $pdf->Cell(60, 8, $sparty['name'], 1, 0);
        $pdf->Cell(50, 8, $scomp['compname'], 1, 0);
        $pdf->Cell(20, 8, $pmain['payterm'], 1, 0);
        $pdf->Cell(30, 8, $sgiven['name'], 1, 0);
        $pdf->Cell(25, 8, $sapprov['name'], 1, 0);
        $pdf->Cell(25, 8, $pmain['total'], 1, 0);
        $pdf->Ln();


        $gtot = $gtot + $pmain['total'];
        if ($detail == 2) {




            $pdf->SetFont('Arial', 'BI', 8);
            $pdf->SetTextColor(0, 0, 0);
            $pdf->SetFillColor(177, 156, 217);
            $pdf->Cell(10, 8, "SrNo", 1, 0, '', 1);
            $pdf->Cell(60, 8, "Description", 1, 0, '', 1);
            $pdf->Cell(20, 8, "Colour", 1, 0, '', 1);
            $pdf->Cell(30, 8, "Qty.", 1, 0, '', 1);
            $pdf->Cell(30, 8, "Unit.", 1, 0, '', 1);
            $pdf->Cell(30, 8, "Rate.", 1, 0, '', 1);
            $pdf->Cell(30, 8, "Ammount.", 1, 0, '', 1);
            $pdf->Ln();

            $gtran = mysqli_query($conn, "SELECT * FROM $trandb where vouchno = '$pmain[vouchno]'") or die(mysql_erorr());

            $ttotqty = $ttotrate = $ttotamt = $ctr2 = 0;

            while ($stran = mysqli_fetch_array($gtran)) {

                $color = $stran['colour'];

                $gcolor = mysqli_query($conn, "SELECT code, name FROM `color` WHERE code='$color'") or die(mysqli_error($conn));
                $fcolor = mysqli_fetch_array($gcolor);
                $colour = $fcolor['name'];

                $ctr2++;
                $gitem = mysqli_query($conn, "SELECT code, name FROM `item` WHERE code='$stran[descrip]'") or die(mysqli_error($conn));
                $sitem = mysqli_fetch_array($gitem);

                $pdf->SetFont('Arial', '', 8);
                $pdf->Cell(10, 8, $ctr2, 1, 0);
                $pdf->Cell(60, 8, $sitem['name'], 1, 0);
                $pdf->Cell(20, 8, $colour, 1, 0);
                $pdf->Cell(30, 8, $stran['qty'], 1, 0);
                $pdf->Cell(30, 8, $stran['unit'], 1, 0);
                $pdf->Cell(30, 8, $stran['rate'], 1, 0);
                $pdf->Cell(30, 8, $stran['amt'], 1, 0);
                $ttotqty = $ttotqty + $stran['qty'];
                $ttotrate = $ttotrate + $stran['rate'];
                $ttotamt = $ttotamt + $stran['amt'];
                $pdf->Ln();
            }

            $pdf->SetFont('Arial', 'B', 8);
            $pdf->Cell(10, 8, ".", 1, 0);
            $pdf->Cell(60, 8, ".", 1, 0);
            $pdf->Cell(20, 8, "Total", 1, 0);
            $pdf->Cell(30, 8, $ttotqty, 1, 0);
            $pdf->Cell(30, 8, ".", 1, 0);
            $pdf->Cell(30, 8, $ttotrate, 1, 0);
            $pdf->Cell(30, 8, $ttoamt, 1, 0);
            $pdf->Ln();
        }
    }

    $pdf->Output('OrderFormRegister.pdf', 'I');
}



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


    $contents .= "Sr., Voucher, Date, Bank Name, Receipt, Cheque, Cheque Date, For, Account Name, Advance, Amount, Company \n";

    $ctr = 0;
    $gtot = 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));
        $sparty = mysqli_fetch_array($gparty);

        $gbank = mysqli_query($conn, "SELECT accode, name FROM `accmast` where accode='$pmain[bankname]'") or die(mysqli_error($conn));
        $sbank = mysqli_fetch_array($gbank);

        $gcomp = mysqli_query($conn, "SELECT code, compname FROM `branch` where code='$pmain[compname]'") or die(mysqli_error($conn));
        $scomp = mysqli_fetch_array($gcomp);

        $ggiven = mysqli_query($conn, "SELECT code, name FROM `staff` where code='$pmain[given]'") or die(mysqli_error($conn));
        $sgiven = mysqli_fetch_array($ggiven);

        $gapprov = mysqli_query($conn, "SELECT code, name FROM `staff` where code='$pmain[approved]'") or die(mysqli_error($conn));
        $sapprov = mysqli_fetch_array($gapprov);


        if ($pmain['fortype'] == 'P') {
            $fortype = 'Purchase';
        } elseif ($pmain['fortype'] == 'C') {
            $fortype = 'Courier';
        }

        $ctr++;

        $contents .= "$ctr" . ",";
        $contents .= "$pmain[vouchno]" . ",";
        $contents .= "$pmain[vouchdt]" . ",";
        $contents .= "$sbank[name]" . ",";
        $contents .= "$pmain[recpno]" . ",";
        $contents .= "$pmain[chqno]" . ",";
        $contents .= "$pmain[chqdate]" . ",";
        $contents .= "$fortype" . ",";
        $contents .= "$sparty[name]" . ",";
        $contents .= "$pmain[ordno]" . ",";
        $contents .= "$pmain[amount]" . ",";
        $contents .= "$scomp[compname]" . "\n";
        
        $gtot = $gtot + $pmain['amount'];
    }
    
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= "$gtot" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . "\n";

    $contents = strip_tags($contents);

    header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
    print $contents;
}
