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

$maindb = "contra";
//$trandb = "invtran";
$formname = "Contra Register";
$file = "contreg_fr.php";

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

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

    $accode = $_POST[accode];
    $branch = $_POST[branch];
    $bcfrom = $_POST[cmbfrom];
    $bcto = $_POST[cmbto];
    $chqno = $_POST[chqno];
    $chqdate = $_POST[chqdate];

//    $dtype = $_POST[dtype];
//    $mtype = $_POST[mtype];
//    browse($mtype);
    $accodecond = "";
    $dtypecond = "";
    $mtypecond = "";

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

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

    if (!empty($cmbfrom)) {
        $cmbfromcond = " AND `cmbfrom`='$cmbfrom' ";
    }

    if (!empty($cmbto)) {
        $cmbtocond = " AND `cmbto`='$cmbto' ";
    }

    if (!empty($chqno)) {
        $chqnocond = " AND `chqno`='$chqno' ";
    }

    if (!empty($chqdate)) {
        $chqdatecond = " AND `chqdt`='$chqdate' ";
    }

    if (!empty($frmdt)) {
        $frmdtcond = " AND `frmdt`='$frmdt' ";
    }

    if (!empty($todt)) {
        $todtcond = " AND `todt`='$todt' ";
    }

//    if (!empty($mtype)) {
//        $mtypecond = " AND `stype`='$mtype' ";
//    }

    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; contreg.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; contreg.php");
    }
    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

// print "SELECT * FROM `$maindb` WHERE `vouchno`!='' $partyname $broker $transpor $item $godown $mtype";
    $getpmain = mysqli_query($conn,"SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' $accodecond $branchcond $cmbfromcond $cmbtocond $chqnocond $chqdatecond")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);

    include("includes/linkshead.php");
    ?>
    <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">
            <div class="row">
                <div class="col-lg-6">
                    <label class="label-control marg">Date - </label>
                    <label class="label-control marg"><?php Echo Date("d-m-Y"); ?></label>
                </div>
                <div class="col-lg-5">
                    <label class="label-control marg">Print - </label>
                    <label class="label-control marg"><?php Echo $formname; ?></label>
                </div>
                <div class="col-lg-1">
                    <a onclick="window.close()" class="btn btn-primary">GO Back</a>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white" class="table table-condensed table-hover">
                        <thead>
                            <tr style="height:10px;background-color: #64CCFC">
                                <th>Sr</th>
                                <th>Voucher No.</th>
                                <th>Date</th>
                                <th>From</th>
                                <th>To</th>
                                <th>Branch</th>
                                <th>Cheque No.</th>
                                <th>Cheque Date</th>
                                <th class="text-right">Amount</th>
                                <th>Narration</th>

                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totamt = 0;

                            while ($pmain = mysqli_fetch_array($getpmain))
                                    {
                                $getbranch = mysqli_query($conn,"SELECT * FROM `cepheisy_gujtran_root`.`branch` WHERE `code`='$pmain[branch]'")or die(mysqli_error($conn));
                                $mbranch = mysqli_fetch_array($getbranch);

                                $gbfrom = mysqli_query($conn,"SELECT * FROM `accmast` WHERE `accode`='$pmain[frombc]'")or die(mysqli_error($conn));
                                $bfrom = mysqli_fetch_array($gbfrom);

                                $gbto = mysqli_query($conn,"SELECT * FROM `accmast` WHERE `accode`='$pmain[tobc]'")or die(mysqli_error($conn));
                                $bto = mysqli_fetch_array($gbto);

                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td style>$pmain[vouchno]</td>";
                                Echo "<td>$pmain[vouchdt]</td>";
                                Echo "<td>$bfrom[name]</td>";
                                Echo "<td>$bto[name]</td>";
                                Echo "<td>$mbranch[name]</td>";
                                Echo "<td>$pmain[chqno]</td>";
                                Echo "<td>$pmain[chqdate]</td>";
                                Echo "<td class='text-right'>$pmain[amount]</td>";
                                Echo "<td>$pmain[narration]</td>";
                                Echo "</tr>";
                                
                                $totamt = $totamt + $pmain[amount];

                                $colr = "green";
                                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 style='color:$colr;'><h4><b>Grand Total</b></h4></td>";
//                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totamt</b></h4></td>";
                                echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totamt</b></h4></td>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "</tr>";
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

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

    $partycond = "";
    $brokercond = "";
    $transportcond = "";
    $itemcond = "";
    $godowncond = "";
    $mtypecond = "";

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

    if (!empty($item)) {
        $itemcond = " AND `name`='$item' ";
    }

    if (!empty($godown)) {
        $godowncond = " AND `godown`='$godown' ";
    }

    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; purchreg.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; purchreg.php");
    }
    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    if (!empty($mtype)) {
        $mtypecond = " AND `mtype`='$mtype' ";
    }
// print "SELECT * FROM `$maindb` WHERE `vouchno`!='' $partyname $broker $transpor $item $godown $mtype";
    $getpmain = mysqli_query($conn,"SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $partycond $itemcond $godowncond $mtypecond")or die(mysqli_error($conn));


    $contents.="Sr,Type,Invoice No,Date,Supplier,Amount,Discount,Freight,Qty,Net Amount\n";

    $ctr = 0;
    $subtot = 0;
    $totdisc = 0;
    $totfr = 0;
    $totqty1 = 0;
    $totnet = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {
        $getmtype = mysqli_query($conn,"SELECT * FROM `type` WHERE `code`='$pmain[type]'")or die(mysqli_error($conn));
        $mtype = mysqli_fetch_array($getmtype);

        $getcustomer = mysqli_query($conn,"SELECT * FROM `accmast` WHERE `accode`='$pmain[party]'")or die(mysqli_error($conn));
        $mcust = mysqli_fetch_array($getcustomer);

        $getmqty = mysqli_query($conn,"SELECT SUM(qty)as totqty FROM `$trandb` WHERE `vouchno`='$pmain[VOUCHNO]'")or die(mysqli_error($conn));
        $mqty = mysqli_fetch_array($getmqty);
        $ctr++;

        $ctr++;
        $name = $mtype[name];
        $invno = $mtype[invno];
        $mydt = $pmain[mydt];
        $custname = $mcust[name];
        $subtot = $pmain[subtotal];
        $disc = $pmain[discount];
        $freight = $pmain[freight];
        $totqty = $pmain[totqty];
        $netamt = $pmain[netamt];

        $content.=$ctr . ",";
        $content.=$name . ",";
        $content.=$invno . ",";
        $content.=$mydt . ",";
        $content.=$name . ",";
        $content.=$subtotal . ",";
        $content.=$discount . ",";
        $content.=$freight . ",";
        $content.=$totqty . ",";
        $content.=$netamt . "\n";


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

            $contents.="Sr,Brand,Item Name,Taka,Qty.,Tot. Qty.,Flag,Flag Qty.,Inv. Qty.,Rate,Amount\n";


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

                $getbrand = mysqli_query($conn,"SELECT * FROM `brand` WHERE `code`='$tran[brand]'")or die(mysqli_error($conn));
                $mbrand = mysqli_fetch_array($getbrand);

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

                $ctr2++;
                $brname = $mbrand[name];
                $itname = $mitem[name];
                $taka = $tran[taka];
                $qty = $tran[qty];
                $tqty = $tran[totqty];
                $flag = $tran[flag];
                $flagqty = $tran[flagqty];
                $invqty = $tran[invqty];
                $rate = $tran[rate];
                $amount = $tran[amount];

                $contents.=$ctr2 . ",";
                $contents.=$brname . ",";
                $contents.=$itname . ",";
                $contents.=$taka . ",";
                $contents.=$qty . ",";
                $contents.=$tqty . ",";
                $contents.=$flag . ",";
                $contents.=$flagqty . ",";
                $contents.=$invqty . ",";
                $contents.=$rate . ",";
                $contents.=$amount . "\n";

                $totdtqty = $totdtqty + $tran[totqty];
                $totdtinv = $totdtinv + $tran[invqty];
                $totdtamt = $totdtamt + $tran[amount];
                $totqty1 = $totqty1 + $tran[totqty];
            }
        }
    }

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