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

$maindb = 'salemain';
$trandb = 'saletran';
$formname = "Daily sale report";
$file = "dailysalereport_fr.php";

$cashsale = "Cash Sale";
$credsale = "Credit Sale";
$creditcard = "Credit Card Sale";


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

//$zroot = 'cepheisy_msp_root';
//
//
//$getcompany = mysqli_query($conn, "SELECT code, name FROM $zroot.`company`  ")or die(mysqli_error($conn));
//$showcompany = mysqli_fetch_array($getcompany);
//$company = $showcompany[name];

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


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

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

    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    
//////////////////////// CASH START/////////////////////////
    $getcash = mysqli_query($conn, "SELECT a.`party`,  b.`item`,  b.`qty`, b.`unit`, b.`rate`, b.`total` FROM  $maindb a, $trandb b  WHERE 
           a.`cashamt` > 0 AND a.`vouchdt`= '$frmdt' and a.`vouchno` = b.`vouchno`")or die(mysqli_error($conn));
    $getcashnum = mysqli_num_rows($getcash);
//////////////////////// CASH END/////////////////////////   

//////////////////////// CREDIT START/////////////////////////    
    $getcred = mysqli_query($conn, "SELECT a.`party`,  b.`item`,  b.`qty`, b.`unit`, b.`rate`, b.`total` FROM  $maindb a, $trandb b  WHERE 
           a.`credamt` > 0 AND a.`vouchdt`= '$frmdt' and a.`vouchno` = b.`vouchno`")or die(mysqli_error($conn));

    $getcrednum = mysqli_num_rows($getcred);
    //////////////////////// CREDIT END/////////////////////////  
  
    //////////////////////// CREDIT CARD SATRT/////////////////////////    

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

    $crdcdcnum = mysqli_num_rows($crdcd);
    //////////////////////// CREDIT CARD END/////////////////////////    

    include("includes/linkshead.php");
    ?>
    <style>
        @media print
        {    
            .no-print, .no-print *
            {
                display: none !important;
            }
        }
    </style>
    </head>
    <body>
        <div class="container">

            <!--//////////////////////// CASH START/////////////////////////-->
            <?php
            include("includes/head_rep.php");
            ?>
<!--            <div class="row">
                <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                    <label> <?php echo $mfdt; ?></label>
                </div>
            </div>-->
            <?php
//            if ($getcashnum > 0 || $getcrednum > 0 || $crdcdcnum > 0) {
                if ($getcashnum > 0) {
                    ?>
                    <div class="row">
                        <div class="col-lg-4 col-md-2 col-sm-2 col-xs-2 pad">
                            <label> <?php echo $cashsale; ?></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:10%;">Sr</th>
                                        <th style="width:30%;">Party </th>
                                        <th style="width:20%;" >Item</th>
                                        <th style="width:10%;" class='text-right'>Qty</th>
                                        <th style="width:10%;" >Unit</th>
                                        <th style="width:10%;" class='text-right'>Rate</th>
                                        <th style="width:10%;" class='text-right'>Total</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $ctr = 0;
                                    $tqty = 0;
                                    $ttotal = 0;

                                    while ($pmain = mysqli_fetch_array($getcash)) {
                                        $getitem = mysqli_query($conn, "SELECT `name`, `code` FROM item WHERE `code`='$pmain[item]'")or die(mysqli_error($conn));
                                        $showitem = mysqli_fetch_array($getitem);

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

                                        $getunit = mysqli_query($conn, "SELECT `name`, `code` FROM unit WHERE `code`='$pmain[unit]'")or die(mysqli_error($conn));
                                        $showunit = mysqli_fetch_array($getunit);

                                        $ctr++;
                                        Echo "<tr>";
                                        Echo "<td>$ctr</td>";
                                        Echo "<td>$showparty[name]</td>";
                                        Echo "<td>$showitem[name]</td>";
                                        Echo "<td class='text-right'>$pmain[qty]</td>";
                                        Echo "<td >$showunit[name]</td>";
                                        Echo "<td class='text-right'>$pmain[rate]</td>";
                                        Echo "<td class='text-right'>$pmain[total]</td>";
                                        Echo "</tr>";
                                        $tqty = $tqty + $pmain[qty];
                                        $ttotal = $ttotal + $pmain[total];
                                    }

                                    Echo "<tr>";
                                    Echo "<td></td>";
                                    Echo "<td></td>";
                                    Echo "<td class='text-right'><b>Total</b></td>";
                                    Echo "<td class='text-right'><b>$tqty</b></td>";
                                    Echo "<td class='text-right'><b></b></td>";
                                    Echo "<td class='text-right'></td>";
                                    Echo "<td class='text-right'><b>$ttotal</b></td>";
                                    Echo "</tr>";
                                    ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <?php
                }
                //////////////////////// CASH END/////////////////////////  
                //////////////////////// CREDIT START/////////////////////////    

                if ($getcrednum > 0) {
                    ?>
                    <div class="row">
                        <div class="col-lg-4 col-md-2 col-sm-2 col-xs-2 pad">
                            <label> <?php echo $credsale; ?></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:10%;">Sr</th>
                                        <th style="width:30%;">Party </th>
                                        <th style="width:20%;" >Item</th>
                                        <th style="width:10%;" class='text-right'>Qty</th>
                                        <th style="width:10%;" >Unit</th>
                                        <th style="width:10%;" class='text-right'>Rate</th>
                                        <th style="width:10%;" class='text-right'>Total</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $ctr = 0;
                                    $tqtycred = 0;
                                    $ttotalcred = 0;

                                    while ($showcred = mysqli_fetch_array($getcred)) {
                                        $getitem = mysqli_query($conn, "SELECT `name`, `code` FROM item WHERE `code`='$showcred[item]'")or die(mysqli_error($conn));
                                        $showitem = mysqli_fetch_array($getitem);

                                        $getparty = mysqli_query($conn, "SELECT `name`, `accode` FROM accmast WHERE `accode`='$showcred[party]'")or die(mysqli_error($conn));
                                        $showparty = mysqli_fetch_array($getparty);

                                        $getunit = mysqli_query($conn, "SELECT `name`, `code` FROM unit WHERE `code`='$showcred[unit]'")or die(mysqli_error($conn));
                                        $showunit = mysqli_fetch_array($getunit);

                                        $ctr++;
                                        Echo "<tr>";
                                        Echo "<td>$ctr</td>";
                                        Echo "<td>$showparty[name]</td>";
                                        Echo "<td>$showitem[name]</td>";
                                        Echo "<td class='text-right'>$showcred[qty]</td>";
                                        Echo "<td >$showunit[name]</td>";
                                        Echo "<td class='text-right'>$showcred[rate]</td>";
                                        Echo "<td class='text-right'>$showcred[total]</td>";
                                        Echo "</tr>";
                                        $tqtycred = $tqtycred + $showcred[qty];
                                        $ttotalcred = $ttotalcred + $showcred[total];
                                    }

                                    Echo "<tr>";
                                    Echo "<td></td>";
                                    Echo "<td></td>";
                                    Echo "<td class='text-right'><b>Total</b></td>";
                                    Echo "<td class='text-right'><b>$tqtycred</b></td>";
                                    Echo "<td class='text-right'><b></b></td>";
                                    Echo "<td class='text-right'></td>";
                                    Echo "<td class='text-right'><b>$ttotalcred</b></td>";
                                    Echo "</tr>";
                                    ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <?php
                }
                //////////////////////// CREDIT END/////////////////////////    
                //////////////////////// CREDIT CARD START/////////////////////////    


                if ($crdcdcnum > 0) {
                    ?>
                    <div class="row">
                        <div class="col-lg-4 col-md-2 col-sm-2 col-xs-2 pad">
                            <label> <?php echo $creditcard; ?></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:10%;">Sr</th>
                                        <th style="width:30%;">Party </th>
                                        <th style="width:20%;" >Item</th>
                                        <th style="width:10%;" class='text-right'>Qty</th>
                                        <th style="width:10%;" >Unit</th>
                                        <th style="width:10%;" class='text-right'>Rate</th>
                                        <th style="width:10%;" class='text-right'>Total</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $ctr = 0;
                                    $tqtycd = 0;
                                    $ttotalcd = 0;

                                    while ($pmain3 = mysqli_fetch_array($crdcd)) {
                                        $getitem3 = mysqli_query($conn, "SELECT `name`, `code` FROM item WHERE `code`='$pmain3[item]'")or die(mysqli_error($conn));
                                        $showitem3 = mysqli_fetch_array($getitem3);

                                        $getparty3 = mysqli_query($conn, "SELECT `name`, `accode` FROM accmast WHERE `accode`='$pmain3[party]'")or die(mysqli_error($conn));
                                        $showparty3 = mysqli_fetch_array($getparty3);

                                        $getunit3 = mysqli_query($conn, "SELECT `name`, `code` FROM unit WHERE `code`='$pmain3[unit]'")or die(mysqli_error($conn));
                                        $showunit3 = mysqli_fetch_array($getunit3);

                                        $ctr++;
                                        Echo "<tr>";
                                        Echo "<td>$ctr</td>";
                                        Echo "<td>$showparty3[name]</td>";
                                        Echo "<td>$showitem3[name]</td>";
                                        Echo "<td class='text-right'>$pmain3[qty]</td>";
                                        Echo "<td >$showunit3[name]</td>";
                                        Echo "<td class='text-right'>$pmain3[rate]</td>";
                                        Echo "<td class='text-right'>$pmain3[total]</td>";
                                        Echo "</tr>";
                                        $tqtycd = $tqtycd + $pmain3[qty];
                                        $ttotalcd = $ttotalcd + $pmain3[total];
                                    }

                                    Echo "<tr>";
                                    Echo "<td></td>";
                                    Echo "<td></td>";
                                    Echo "<td class='text-right'><b>Total</b></td>";
                                    Echo "<td class='text-right'><b>$tqtycd</b></td>";
                                    Echo "<td class='text-right'><b></b></td>";
                                    Echo "<td class='text-right'></td>";
                                    Echo "<td class='text-right'><b>$ttotalcd</b></td>";
                                    Echo "</tr>";
                                    ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <?php
                    $gtotqty = $tqtycd + $tqtycred + $tqty;
                    $gtotal = $ttotalcd + $ttotalcred + $ttotal;
                }
                ?>
                <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">
                            <tr style="background-color: #F9F9F9">
                                <th style="width:10%;"></th>
                                <th style="width:30%;"></th>
                                <th style="width:20%;color:red;" class='text-right'>Grand Toal</th>
                                <th style="width:10%;color:red;"  class='text-right'><?php Echo $gtotqty ?></th>
                                <th style="width:10%;color:red;" ></th>
                                <th style="width:10%;color:red;" class='text-right'></th>
                                <th style="width:10%;color:red;" class='text-right'><?php Echo $gtotal ?></th>
                            </tr>
                        </table>
                    </div>
                </div>
                <?php
//            } else {
//                Echo"<span style='color:red'>" . "This Date in  NO Sale" . "</span>";
//            }
            //////////////////////// CREDIT CARD START/////////////////////////    
            ?>
        </div>
        <?php
        include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
?>