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

$maindb = 'salemain';
$trandb = 'saletran';
$formname = "Daily Cash report";
$file = "dailycash_fr.php";

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


if (isset($_POST["view"])) {
    $frmdt = $_POST["frmdt"];
    $todt = $_POST["todt"];


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

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

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

    $mdiff = floor((abs(strtotime($todt) - strtotime($frmdt)) / (60 * 60 * 24)));
//    browse($mdiff);
//    $days = date('l', strtotime($mfrmdate));
//    browse($days);

    $getcash = mysqli_query($conn, "SELECT a.`party`,  b.`item`,  b.`qty`, b.`unit`, b.`rate`, b.`total` FROM  $maindb a, $trandb b  WHERE 
           a.`cashcred` = 'C' AND a.`vouchdt`= '$frmdt' and a.`vouchno` = b.`vouchno`")or die(mysqli_error($conn));



    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 $formname; ?></label>
                            </div>
                        </div>-->
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="width:100%;background: white;" class="table table-condensed table-striped table-bordered table-hover">
                        <thead>
                            <tr style="background-color: #78909C">
                                <th style="width:8%;">Date</th>
                                <th style="width:8%;">Day</th>
                                <th style="width:8%;" class='text-right'>Other </th>
                                <th style="width:8%;" class='text-right'>Perfume</th>
                                <th style="width:8%;" class='text-right'>Total</th>
                                <th style="width:8%;" class='text-right'>Comp.</th>
                                <th style="width:8%;" class='text-right'>Credit</th>
                                <th style="width:8%;" class='text-right'>C C</th>
                                <th style="width:8%;" class='text-right'>Balance</th>
                                <th style="width:8%;" class='text-right'>Act. Bal.</th>
                                <th style="width:8%;" class='text-right'>Difference</th>
                                <th style="width:8%;" class='text-right'>Received</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $tqty = 0;
                            $total = 0;
                            $mdate = $mfrmdate;
                            $oth = $perf = $comp = $total = $cred = $cc = $balc = $actbal = $diff = 0;
                            $groth = $grperf = $grcomp = $grtotal = $grcred = $grcc = $grbalc = $gractbal = $grdiff = 0;
                            for ($i = 0; $i <= $mdiff; $i++) {
                                $getitem = mysqli_query($conn, "SELECT * from salemain WHERE `vouchdt`='$mdate'") or die(mysqli_error($conn));

                                $amtrecd = 0;
                                $getrecd = mysqli_query($conn, "SELECT sum(amtrecd) as amt from `chrecd` WHERE `vouchdt`='$mdate' group by vouchdt") or die(mysqli_error($conn));
                                if (mysqli_num_rows($getrecd) > 0) {
                                    $precd = mysqli_fetch_array($getrecd);
                                    $amtrecd = $precd[amt];
                                }
                                $oth = 0;
                                $perf = 0;
                                $cred = 0;
                                $cc = 0;
                                while ($pmain = mysqli_fetch_array($getitem)) {

                                    $prodtype = $pmain[prodtype];
                                    $cashcred = $pmain[cashcred];
                                    $netamt = $pmain[netamt];

                                    if ($prodtype == 'P') {
                                        $perf = $perf + $netamt;
                                    } else {
                                        $oth = $oth + $netamt;
                                    }

                                    if ($cashcred == 'R') {
                                        $cred = $cred + $netamt;
                                    }

                                    if ($cashcred == 'D') {
                                        $cc = $cc + $netamt;
                                    }
                                }
                                $perfsh = $perf * 10;
                                $total = $oth + $perfsh;
                                $comp = $oth + $perf;
                                $balc = $comp - $cred - $cc;
                                $actbal = $total - $cred - $cc;
                                $diff = $actbal - $balc;

                                $mday = date('l', strtotime($mdate));
                                $mdate = date("d-m-Y", strtotime($mdate));

                                Echo "<tr>";
                                Echo "<td>$mdate</td>";
                                Echo "<td>$mday</td>";
                                Echo "<td class='text-right'>$oth</td>";
                                Echo "<td class='text-right'>$perfsh</td>";
                                Echo "<td class='text-right'>$total</td>";
                                Echo "<td class='text-right'>$comp</td>";
                                Echo "<td class='text-right'>$cred</td>";
                                Echo "<td class='text-right'>$cc</td>";
                                Echo "<td class='text-right'><b>$balc</b></td>";
                                Echo "<td class='text-right'>$actbal</td>";
                                Echo "<td class='text-right'>$diff</td>";
                                if ($amtrecd > 0) {
                                    $arecd = number_format($amtrecd, 0);
                                    Echo "<td style='color: red' class='text-right'><b> $arecd</b></td>";
                                } else {
                                    Echo "<td></td>";
                                }

                                Echo "</tr>";

                                $groth = $groth + $oth;
                                $grperf = $grperf + $perf;
                                $grtotal = $grtotal + $total;
                                $grcomp = $grcomp + $comp;
                                $grcred = $grcred + $cred;
                                $grcc = $grcc + $cc;
                                $grbalc = $grbalc + $balc;
                                $grdiff = $grdiff + $diff;
                                $gractbal = $gractbal + $actbal;
                                $totrecd = $totrecd + $amtrecd;

                                $mdate = date('Y-m-d', strtotime($mdate . ' + 1 days'));
                            }
                            $netbal = number_format($grdiff - $totrecd,0);
                            $totrecd1 = number_format($totrecd, 0);
                            Echo "<tr>";
                            Echo "<td></td>";
                            Echo "<td class='text-right'><b>Total</b></td>";
                            Echo "<td class='text-right'>$groth</td>";
                            Echo "<td class='text-right'>$grperf</td>";
                            Echo "<td class='text-right'>$grtotal</td>";
                            Echo "<td class='text-right'>$grcomp</td>";
                            Echo "<td class='text-right'>$grcred</td>";
                            Echo "<td class='text-right'>$grcc</td>";
                            Echo "<td class='text-right'><b>$grbalc</b></td>";
                            Echo "<td class='text-right'>$gractbal</td>";
                            Echo "<td class='text-right'>$grdiff</td>";
                            Echo "<td class='text-right' style='color: red'><b>$totrecd1</b></td>";
                            Echo "</tr>";

                            Echo "<tr>";
                            Echo "<td colspan='9'></td>";
                            Echo "<td class='text-right'><b>Net Balance</b></td>";
                            Echo "<td class='text-right' style='color: blue'><b>$netbal</b></td>";
                            Echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php
        include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
?>