<?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' or a.cashamt > 0) 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'>Credit Card</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'>Expense</th>
                                <th style="width:8%;" class='text-right'>Pending Bal.</th>
                                <th style="width:8%;" class='text-right'>Received</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $tqty = 0;
                            $total = 0;
                            $expamt = 0;
                            $mdate = $mfrmdate;
                            $oth = $perf = $comp = $total = $cred = $cc = $balc = $actbal = $diff = 0;
                            $groth = $grperf = $grcomp = $grtotal = $grcred = $grcc = $grbalc = $gractbal = $grdiff = $grpend = $grexp =  0;
                            for ($i = 0; $i <= $mdiff; $i++) {
                                $getitem = mysqli_query($conn, "SELECT * from salemain WHERE `vouchdt`='$mdate'") or die(mysqli_error($conn));

                                $getcc1 = mysqli_query($conn, "SELECT sum(ccamt) as cc1 from salemain WHERE `vouchdt`='$mdate' and prodtype = 'P'") or die(mysqli_error($conn));
                                $fetcc1 = mysqli_fetch_array($getcc1);
                                $amtcc1 = $fetcc1[cc1] * 10 ;

                                $getcc2 = mysqli_query($conn, "SELECT sum(ccamt) as cc2 from salemain WHERE `vouchdt`='$mdate' and prodtype != 'P'") or die(mysqli_error($conn));
                                $fetcc2 = mysqli_fetch_array($getcc2);
                                $amtcc2 = $fetcc2[cc2];
//                                browse($amtcc2);
                                
                                $amtcc = $amtcc1 + $amtcc2 ;

                                $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];
                                }

                                ####eXPENSE
                                $expamt = 0;
                                $getexp = mysqli_query($conn, "SELECT sum(amount) as amt from `cashpay` WHERE `vouchdt`='$mdate' group by vouchdt") or die(mysqli_error($conn));
//                                print table($getexp);
                                if (mysqli_num_rows($getexp) > 0) {
                                    $pexp = mysqli_fetch_array($getexp);
                                    $expamt = $pexp[amt];
//                                    browse($expamt);
                                }
//                                browse(mysqli_num_rows($getexp));
                                ####
                                
                                $oth = 0;
                                $perf = 0;
                                $cred = 0;
                                while ($pmain = mysqli_fetch_array($getitem)) {

                                    $prodtype = $pmain[prodtype];
                                    $cashcred = $pmain[cashcred];
                                    $netamt = $pmain[netamt];
                                    $cashamt = $pmain[cashamt];
                                    $credamt = $pmain[credamt];
                                    $ccamt   = $pmain[ccamt];

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

                                    if ($cashcred == 'R' || $credamt > 0) {
                                        $cred = $cred + $credamt;
                                    }
                                }
                                
                                $cc = $amtcc;
                                
                                
                                $perfsh = $perf * 10;
                                $total = $oth + $perfsh;
                                $comp = $oth + $perf;
                                
                                $balc = $comp - $cred - $cc;
                                $actbal = $total - $cred - $cc;
                                $diff = $actbal - $balc;
                                $pendbal = $diff - $expamt;

                                $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>";
                                Echo "<td class='text-right'>$expamt</td>";
                                Echo "<td class='text-right'>$pendbal</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;
                                $grexp   = $grexp + $expamt;
                                $grpend   = $grpend + $pendbal;
                                $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'>$grexp</td>";
                            Echo "<td class='text-right'>$grpend</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 "<td class='text-right' style='color: blue'><b>$grexp</b></td>";
                            Echo "<td class='text-right' style='color: blue'><b>$grpend</b></td>";
                            Echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php
        include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
?>