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

$zroot = $_SESSION["zdbroot"];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);


$getnod = mysqli_query($conn, "SELECT nosaledays,nopaydays FROM $zroot.`company` LIMIT 1") or die(mysqli_error($conn));
$fnod = mysqli_fetch_array($getnod);

$currdt = $_REQUEST['currdt'];

$nosaledays = $fnod['nosaledays'];
$frdt = date('Y-m-d', strtotime($currdt . ' - ' . $nosaledays . 'days'));



mysqli_query($conn, "drop temporary table if exists slparty ") or die(mysqli_error($conn));
mysqli_query($conn, "create temporary table slparty 
                                SELECT a.party, b.accode, b.name, b.city, b.mobile FROM `sale` a, accmast b 
                                       where a.party = b.accode and a.vouchdt < '$frdt' GROUP BY a.party ") or die(mysqli_error($conn));

$getnosaledays = mysqli_query($conn, "SELECT * FROM `slparty` WHERE accode NOT IN 
                                           (SELECT party FROM sale WHERE vouchdt >= '$frdt' AND vouchdt <= '$currdt') ") or die(mysqli_error($conn));

// $getacc = mysqli_query($conn, "SELECT party FROM `sale` GROUP BY party ") or die(mysqli_error($conn));
// $fetacc = mysqli_fetch_array($getacc);
// $acname = $fetacc['party'];


// $getnosaledays = mysqli_query($conn, "SELECT `accode` FROM `accmast` WHERE accode='$acname' AND accode NOT IN (SELECT party FROM sale WHERE 
//                                                     vouchdt >= '$frdt'AND vouchdt <= '$currdt') ") or die(mysqli_error($conn));
$Nosaledyas = mysqli_num_rows($getnosaledays);


$nopaydays = $fnod['nopaydays'];
$frdt = date('Y-m-d', strtotime($currdt . ' - ' . $nopaydays . 'days'));


$getnopaydays = mysqli_query($conn, "SELECT accode FROM `accmast` WHERE grpcode = '010' and accode NOT IN (Select accode FROM actran
                                                   WHERE date >= '$frdt' and date <= '$currdt' and source = 'CR' )  order by name") or die(mysqli_error($conn));

$Nopaydays = mysqli_num_rows($getnopaydays);

$getSales = mysqli_query($conn, "SELECT IFNULL(SUM(amount), 0) AS amt  FROM `actran` WHERE `date` = '$currdt' AND source = 'SL' AND amount > 0 ") or die(mysqli_error($conn));
$NSales = mysqli_fetch_object($getSales);

$getreceipts = mysqli_query($conn, "SELECT IFNULL(SUM(amount), 0) AS amt  FROM `actran` WHERE `date` = '$currdt' AND source = 'CR' AND amount > 0 ") or die(mysqli_error($conn));
$Nreceipts = mysqli_fetch_object($getreceipts);


$getpayments = mysqli_query($conn, "SELECT IFNULL(SUM(abs(amount)), 0) AS amt  FROM `actran` WHERE `date` = '$currdt' AND source = 'CP' AND amount < 0 AND 
                                           accode in (select accode from accmast where grpcode != '012') ") or die(mysqli_error($conn));
$Npayments = mysqli_fetch_object($getpayments);

$getexpense = mysqli_query($conn, "SELECT IFNULL(SUM(abs(amount)), 0) AS amt  FROM `actran` WHERE `date` = '$currdt' AND source = 'CP' AND amount < 0 AND
                                           accode in (select accode from accmast where grpcode = '012') ") or die(mysqli_error($conn));
$Nexpense = mysqli_fetch_object($getexpense);

$getcontra = mysqli_query($conn, "SELECT IFNULL(SUM(abs(amount)), 0) AS amt  FROM `actran` WHERE `date` = '$currdt' AND source = 'TR' AND amount > 0 ") or die(mysqli_error($conn));
$Ncontra = mysqli_fetch_object($getcontra);

$getjv = mysqli_query($conn, "SELECT IFNULL(SUM(abs(amount)), 0) AS amt  FROM `actran` WHERE `date` = '$currdt' AND source = 'JV' AND amount > 0  ") or die(mysqli_error($conn));
$Njv = mysqli_fetch_object($getjv);

$user = $_SESSION["username_" . $dbname];
?>

<main class="content">
    <section class="widgets">
        <div class="widget card" style="visibility: hidden;">
            <h5 style="font-size:16px">&nbsp;</h5>
        </div>

        <!-- <div class="widget card">
            <h5 style="font-size:16px">&nbsp; No Sale For <?= $nosaledays ?> Days</h5>
            <table class="table">
                <tr>
                    <td style="text-align: left;">
                        <a target="_blank" id="nosaleLink"
                            href="custnosale_fr.php?view=view&frmdt=<?= $frdt ?>&todt=<?= $currdt ?>"><span
                                style="font-size:30px"><?= $Nosaledyas; ?></span></a>
                    </td>
                </tr>
            </table>
        </div>

        <div class="widget card">
            <h5 style="font-size:16px">&nbsp; No Payment For <?= $nopaydays ?> Days</h5>
            <table class="table">
                <tr>
                    <td style="text-align: left;">
                        <a target="_blank" id="nopayLink"
                            href="custnopay_fr.php?view=view&frmdt=<?= $frdt ?>&todt=<?= $currdt ?>"><span
                                style="font-size:30px"><?= $Nopaydays; ?></span></a>
                    </td>
                </tr>
            </table>
        </div> -->

        <div class="widget card sales-card">
            <h5 style="font-size:16px">&nbsp; Sales (Daily)</h5>
            <table class="table">
                <tr>
                    <td style="text-align: left;">
                        <a target="_blank" href="salereg_fr.php?view=view&fdt=<?= $currdt ?>&tdt=<?= $currdt ?>"><span
                                style="font-size:30px"><?= number_format($NSales->amt, 0); ?></span></a>
                    </td>
                </tr>
            </table>
        </div>

        <div class="widget card receipt-card">
            <h5 style="font-size:16px">&nbsp; Receipts (Daily)</h5>
            <table class="table">
                <tr>
                    <td style="text-align: left;">
                        <a target="_blank" href="recpreg_fr.php?view=view&fdt=<?= $currdt ?>&tdt=<?= $currdt ?>"><span
                                style="font-size:30px"><?= number_format($Nreceipts->amt, 0); ?></span></a>
                    </td>
                </tr>
            </table>
        </div>

        <div class="widget card" style="visibility: hidden;">
            <h5 style="font-size:16px">&nbsp;</h5>
        </div>
    </section>

</main>