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

$maindb = 'cacamain';
$trandb = 'cacatran';
$zroot = $_SESSION["zdbroot"];

$formname = 'Cash And Carry Register';
$zroot = $_SESSION["zdir"];
$file = 'exinwdreg_fr.php';



$dtye = $_POST["dtype"];
$owner = $_POST["owner"];


$owncond = "";
if (!empty($owner)) {
    $owncond = "AND party = $owner";
}

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>";
}

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

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

$getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $maindb WHERE `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate' $owncond order by vouchdt,vouchno  ")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: #4eb3c7;
        }
        .td1{
           text-align:right;
           /*color:black;*/
           font-size:14px;
           font-weight: bold;
        }
        .tr1{
           background: #98f9f0;
        }
    </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 class="col-lg-12 pad">
                        <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:10%">Voucher</th>
                                    <th style="width:10%">Date</th>
                                    <th style="width:10%">Shift</th>
                                    <th style="width:40%">Party</th>
                                    <th style="width:10%">RTGS</th>
                                    <th style="width:10%; text-align:right;">Total Qty.</th>
                                    <th style="width:10%; text-align:right;">RTGS Amount</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $ctr = 0;
                                $totqty = 0;
                                $totamt = 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));
                                    $party = mysqli_fetch_array($gparty);

                                    $ctr++;
                                    Echo "<tr id='boldfont'>";
                                    Echo "<td>$pmain[vouchno]</td>";
                                    Echo "<td>$pmain[mydt]</td>";
                                    Echo "<td>$pmain[shift]</td>";
                                    Echo "<td>$party[name]</td>";
                                    Echo "<td>$pmain[rtgs]</td>";
                                    Echo "<td style='text-align:right;'>$pmain[totqty]</td>";
                                    Echo "<td style='text-align:right;'>$pmain[subtotal]</td>";
                                    Echo "</tr>";

                                    $totqty = $totqty + $pmain[totqty];
                                    $totamt = $totamt + $pmain[subtotal];

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

                                        Echo "<tr style='height:10px; background-color:#BABFC2'>";
                                        Echo "<td colspan='25'>";
                                        Echo "<table style='width:100%;' id='tabital' class='table table-condensed table-bordered'>";
                                        Echo "<th style='width:5%;'  >Sr</th>";
                                        Echo "<th style='width:10%;' >Vehicle</th>";
                                        Echo "<th style='width:10%;' >Prodcode</th>";
                                        Echo "<th style='width:25%;' >Product</th>";
                                        Echo "<th style='width:10%;' class='text-right'>Qty.</th>";
                                        Echo "<th style='width:10%;' >Unit</th>";
                                        Echo "<th style='width:10%;' class='text-right'>Rate</th>";
                                        Echo "<th style='width:20%;' class='text-right'>Amount</th>";
                                        Echo "<tr>";


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

                                            $ctr2++;
                                            Echo "<tr>";
                                            Echo "<td>$ctr2</td>";
                                            Echo "<td>$tran[vehicleno]</td>";
                                            Echo "<td>$tran[prodcode]</td>";
                                            Echo "<td>$tran[prodname]</td>";
                                            Echo "<td style='text-align:right;'>$tran[qty]</td>";
                                            Echo "<td>$tran[unit]</td>";
                                            Echo "<td style='text-align:right;'>$tran[rate]</td>";
                                            Echo "<td style='text-align:right;'>$tran[amount]</td>";
                                            Echo "</tr>";

                                            $trtotqty = $trtotqty + $tran[qty];
                                            $tramount = $tramount + $tran[amount];
                                        }
                                        Echo "<tr>";
                                        Echo "<td style='text-align:right' colspan='4'>Total</td>";
                                        Echo "<td style='text-align:right;'>$trtotqty</td>";
                                        Echo "<td colspan='2'></td>";
                                        Echo "<td style='text-align:right;'>$tramount</td>";
                                        Echo "</tr>";

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

                                Echo "<tr class='tr1'>";
                                Echo "<td class='td1' colspan= '5'><b>Total</b></td>";
                                Echo "<td class='td1'><b>$totqty</b></td>";
                                Echo "<td class='td1'><b>$totamt</b></td>";
                                Echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <?php include("includes/linksfoot.php");
            ?>
    </body>
    </html>
    <?php
}

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


    $contents.="Voucher,Date,Shift,Party,RTGS,Total Qty.,Amount \n";

    $ctr = 0;
    $totqty = 0;
    $totamt = 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));
        $party = mysqli_fetch_array($gparty);

        $ctr++;

//                                    $contents.= $ctr . ",";
        $contents.= $pmain[vouchno] . ",";
        $contents.= $pmain[mydt] . ",";
        $contents.= $pmain[shift] . ",";
        $contents.= $party[name] . ",";
        $contents.= $pmain[rtgs] . ",";
        $contents.= $pmain[totqty] . ",";
        $contents.= $pmain[subtotal] . "\n";

        $totqty = $totqty + $pmain[totqty];
        $totamt = $totamt + $pmain[subtotal];

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

            $contents.="Sr,Vehicle,Prodcode,Product,Qty.,Unit,Rate,Amount\n";

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

                $ctr2++;

                $contents.=$ctr2 . ",";
                $contents.=$tran[vehicleno] . ",";
                $contents.=$tran[prodcode] . ",";
                $contents.=$tran[prodname] . ",";
                $contents.=$tran[qty] . ",";
                $contents.=$tran[unit] . ",";
                $contents.=$tran[rate] . ",";
                $contents.=$tran[amount] . "\n";

                $trtotqty = $trtotqty + $tran[qty];
                $tramount = $tramount + $tran[amount];
                
            }

            $contents.= "" . ",";
            $contents.= "" . ",";
            $contents.= "" . ",";
            $contents.= "Total" . ",";
            $contents.= "$tramount" . ",";
            $contents.= "" . ",";
            $contents.= "" . ",";
            $contents.= "$tramount" . "\n";
        }
    }

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