<?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 = 'transacn';
$zroot = $_SESSION["zdbroot"];

$formname = 'Daily Sale Report';
$file = 'dailysalsum_fr.php';

$party = $_POST["customer"];
$custcond = '';

if (!empty($party)) {
    $custcond = " AND `accode` = '$party' ";
    $custcond2 = " AND `party` = '$party' ";
    $custcond3 = " AND `partyname` = '$party' ";
}

$gparty = mysqli_query($conn, "SELECT name from accmast where accode = '$party' ")or die(mysqli_error($conn));
$fparty = mysqli_fetch_array($gparty);


$frmdate = $_POST["frmdt"];
$todate = $_POST["todt"];

function dateDiffInDays($date1, $date2) {
    // Calulating the difference in timestamps 
    $diff = strtotime($date2) - strtotime($date1);

    // 1 day = 24 hours 
    // 24 * 60 * 60 = 86400 seconds 
    return abs(round($diff / 86400));
}
$mfdt = date("d-m-Y", strtotime($frmdate));
$mtdt = date("d-m-Y", strtotime($todate));

$date1 = $frmdate;
$date2 = $todate;
$dateDiff = dateDiffInDays($date1, $date2);

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


    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }     
        table td{
            font-size: 14px;  
        }
    </style>
    </head>
    <body>
        <div class="container" style="width:80%">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12">
                    <? if ($party != '') { ?>
                        <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                            <tr class="mySearchTr">
                                <th><b>Party : <?= $fparty['name'] ; ?></b></th>
                            </tr>
                        </table>
                    <? } ?>
                    <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:5%;">Sr</th>
                                <th style="width:10%;">Date</th>
                                <th style="width:10%;">Local Sale</th>
                                <th style="width:10%;">Deemed Sale</th>
                                <th style="width:10%;">Export Sale</th>
                                <th style="width:10%;">Other Sale</th>
                                <th style="width:10%;">Online Sale</th>
                                <th style="width:10%;" class="text-right">Total</th>
                                <th style="width:10%;" class="text-right">Return</th>
                                <th style="width:10%;" class="text-right">Actual Sale</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $actsale = 0;
                            for ($i = 0; $i <= $dateDiff; $i++) {

                                if ($i == 0) {
                                    $vouchdt = $frmdate;
                                } else {
                                    $vouchdt = date('Y-m-d', strtotime("+1 day", strtotime($vouchdt)));
                                }

                                $date = date("d-m-Y", strtotime($vouchdt));


                                $localsale = mysqli_query($conn, "SELECT SUM(billamt) as totamt from salemain where vouchdt = '$vouchdt' $custcond2 ")or die(mysqli_error($conn));
                                $fetch_localsale = mysqli_fetch_array($localsale);

                                if ($fetch_localsale['totamt'] > 0) {
                                    $lsale = $fetch_localsale['totamt'];
                                } else {
                                    $lsale = 0;
                                }

                                $deemedsale = mysqli_query($conn, "SELECT SUM(billamt) as totamt from deexmain where vouchdt = '$vouchdt' $custcond2 ")or die(mysqli_error($conn));
                                $fetch_deemedsale = mysqli_fetch_array($deemedsale);

                                if ($fetch_deemedsale['totamt'] > 0) {
                                    $desale = $fetch_deemedsale['totamt'];
                                } else {
                                    $desale = 0;
                                }

                                $exportsale = mysqli_query($conn, "SELECT SUM(billamt) as totamt from salemainexp where invdate = '$vouchdt' $custcond3 ")or die(mysqli_error($conn));
                                $fetch_exportsale = mysqli_fetch_array($exportsale);

                                if ($fetch_exportsale['totamt'] > 0) {
                                    $exsale = $fetch_exportsale['totamt'];
                                } else {
                                    $exsale = 0;
                                }

                                $othersale = mysqli_query($conn, "SELECT SUM(billamt) as totamt from slotmain where vouchdt = '$vouchdt' $custcond2 ")or die(mysqli_error($conn));
                                $fetch_othsale = mysqli_fetch_array($othersale);

                                if ($fetch_othsale['totamt'] > 0) {
                                    $othsale = $fetch_othsale['totamt'];
                                } else {
                                    $othsale = 0;
                                }

                                $onlsale = mysqli_query($conn, "SELECT SUM(billamt) as totamt from slonlmain where vouchdt = '$vouchdt' $custcond2 ")or die(mysqli_error($conn));
                                $fetch_onlsale = mysqli_fetch_array($onlsale);

                                if ($fetch_onlsale['totamt'] > 0) {
                                    $olsale = $fetch_onlsale['totamt'];
                                } else {
                                    $olsale = 0;
                                }
                                
                                $salereturn = mysqli_query($conn, "SELECT SUM(billamt) as totamt from credmain where vouchdt = '$vouchdt' $custcond2 ")or die(mysqli_error($conn));
                                $fetch_salereturn = mysqli_fetch_array($salereturn);

                                if ($fetch_salereturn['totamt'] > 0) {
                                    $return = $fetch_salereturn['totamt'];
                                } else {
                                    $return = 0;
                                }

                                $totalsale = $lsale + $desale + $exsale + $othsale + $olsale;
                                $actsale = $lsale + $desale + $exsale + $othsale + $olsale - $return;

                                $ctr++;
                                Echo"<tr>";
                                Echo"<td>$ctr</td>";
                                Echo"<td>$date</td>";
                                Echo"<td class='text-right'>$lsale</td>";
                                Echo"<td class='text-right'>$desale</td>";
                                Echo"<td class='text-right'>$exsale</td>";
                                Echo"<td class='text-right'>$othsale</td>";
                                Echo"<td class='text-right'>$olsale</td>";
                                Echo"<td class='text-right'>$totalsale</td>";
                                Echo"<td class='text-right'>$return</td>";
                                Echo"<td class='text-right'>$actsale</td>";
                                Echo"</tr>";

                                $totlsale = $totlsale + $lsale;
                                $totdesale = $totdesale + $desale;
                                $totexsale = $totexsale + $exsale;
                                $totothsale = $totothsale + $othsale;
                                $totolsale = $totolsale + $olsale;
                                
                                $totsale = $totsale + $totalsale;
                                $totreturn = $totreturn + $return;
                                $totactsale = $totactsale + $actsale;
                            }
                            Echo "<tr>";
                            Echo "<td></td>";
                            Echo "<td class='text-right'  style=font-size:15px;><b>Total</b></td>";
                            Echo "<td class='text-right'  style=font-size:15px;><b>$totlsale</b></td>";
                            Echo "<td class='text-right'  style=font-size:15px;><b>$totdesale</b></td>";
                            Echo "<td class='text-right'  style=font-size:15px;><b>$totexsale</b></td>";
                            Echo "<td class='text-right'  style=font-size:15px;><b>$totothsale</b></td>";
                            Echo "<td class='text-right'  style=font-size:15px;><b>$totolsale</b></td>";
                            Echo "<td class='text-right'  style=font-size:15px;><b>$totsale</b></td>";
                            Echo "<td class='text-right'  style=font-size:15px;><b>$totreturn</b></td>";
                            Echo "<td class='text-right'  style=font-size:15px;><b>$totactsale</b></td>";
                            Echo "</tr>";

                            Echo "<tr class='mySearchTr'>";
                            Echo "<td colspan='9'></td>";
                            Echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div> 
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}




if (isset($_POST["excel"])) {
    if ($party != '') { 
    $partyname = $fparty['name'];
    $contents .= "Party, : $partyname \n";
    }
    $contents .= "Sr., Date, Local Sale, Deemed Sale,Export Sale,Other Sale,Online Sale, Total, Return, Actual Sale\n";

  $ctr = 0;
                            $actsale = 0;
                            for ($i = 0; $i <= $dateDiff; $i++) {

                                if ($i == 0) {
                                    $vouchdt = $frmdate;
                                } else {
                                    $vouchdt = date('Y-m-d', strtotime("+1 day", strtotime($vouchdt)));
                                }

                                $date = date("d-m-Y", strtotime($vouchdt));


                                $localsale = mysqli_query($conn, "SELECT SUM(billamt) as totamt from salemain where vouchdt = '$vouchdt' $custcond2 ")or die(mysqli_error($conn));
                                $fetch_localsale = mysqli_fetch_array($localsale);

                                if ($fetch_localsale['totamt'] > 0) {
                                    $lsale = $fetch_localsale['totamt'];
                                } else {
                                    $lsale = 0;
                                }

                                $deemedsale = mysqli_query($conn, "SELECT SUM(billamt) as totamt from deexmain where vouchdt = '$vouchdt' $custcond2 ")or die(mysqli_error($conn));
                                $fetch_deemedsale = mysqli_fetch_array($deemedsale);

                                if ($fetch_deemedsale['totamt'] > 0) {
                                    $desale = $fetch_deemedsale['totamt'];
                                } else {
                                    $desale = 0;
                                }

                                $exportsale = mysqli_query($conn, "SELECT SUM(billamt) as totamt from salemainexp where invdate = '$vouchdt' $custcond3 ")or die(mysqli_error($conn));
                                $fetch_exportsale = mysqli_fetch_array($exportsale);

                                if ($fetch_exportsale['totamt'] > 0) {
                                    $exsale = $fetch_exportsale['totamt'];
                                } else {
                                    $exsale = 0;
                                }

                                $othersale = mysqli_query($conn, "SELECT SUM(billamt) as totamt from slotmain where vouchdt = '$vouchdt' $custcond2 ")or die(mysqli_error($conn));
                                $fetch_othsale = mysqli_fetch_array($othersale);

                                if ($fetch_othsale['totamt'] > 0) {
                                    $othsale = $fetch_othsale['totamt'];
                                } else {
                                    $othsale = 0;
                                }

                                $onlsale = mysqli_query($conn, "SELECT SUM(billamt) as totamt from slonlmain where vouchdt = '$vouchdt' $custcond2 ")or die(mysqli_error($conn));
                                $fetch_onlsale = mysqli_fetch_array($onlsale);

                                if ($fetch_onlsale['totamt'] > 0) {
                                    $olsale = $fetch_onlsale['totamt'];
                                } else {
                                    $olsale = 0;
                                }
                                
                                $salereturn = mysqli_query($conn, "SELECT SUM(billamt) as totamt from credmain where vouchdt = '$vouchdt' $custcond2 ")or die(mysqli_error($conn));
                                $fetch_salereturn = mysqli_fetch_array($salereturn);

                                if ($fetch_salereturn['totamt'] > 0) {
                                    $return = $fetch_salereturn['totamt'];
                                } else {
                                    $return = 0;
                                }

                                $totalsale = $lsale + $desale + $exsale + $othsale + $olsale;
                                $actsale = $lsale + $desale + $exsale + $othsale + $olsale - $return;

                                $ctr++;
                                 $contents .= $ctr . ",";
                                 $contents .= $date . ",";
                                 $contents .= $lsale . ",";
                                 $contents .= $desale . ",";
                                 $contents .= $exsale . ",";
                                 $contents .= $othsale . ",";
                                 $contents .= $olsale . ",";
                                 $contents .= $totalsale . ",";
                                 $contents .= $return . ",";
                                 $contents .= $actsale . "\n";
                          

                                $totlsale = $totlsale + $lsale;
                                $totdesale = $totdesale + $desale;
                                $totexsale = $totexsale + $exsale;
                                $totothsale = $totothsale + $othsale;
                                $totolsale = $totolsale + $olsale;
                                
                                $totsale = $totsale + $totalsale;
                                $totreturn = $totreturn + $return;
                                $totactsale = $totactsale + $actsale;
                            }

    $contents .= "," . ",";
    $contents .= "Total" . ",";
    $contents .= "$totlsale" . ",";
    $contents .= "$totdesale" . ",";
    $contents .= "$totexsale" . ",";
    $contents .= "$totothsale" . ",";
    $contents .= "$totolsale" . ",";
    $contents .= "$totreturn" . ",";
    $contents .= "$totactsale" . "\n";

                            // Echo "<tr class='mySearchTr'>";
                            // Echo "<td colspan='9'></td>";
                            // Echo "</tr>";

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