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

$maindb = "accmast";
$trandb = "salemain";

$formname = "Brokerwise Sale Report";
$file = "brsale.php";

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

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

    if (!empty($broker)) {
        
    } else {
        browse('Broker Cannot Be Empty');
        header("refresh:1; $file");
    }
}
$getpmain = mysqli_query($conn, "SELECT a.*, b.name, b.openbal, b.dc FROM `$trandb` a, `$maindb` b WHERE
                         a.broker = '$broker' and a.broker = b.accode GROUP BY a.`party` ORDER BY b.`name`") or die(mysqli_error($conn));

$maintr = mysqli_num_rows($getpmain);

if ($maintr > 0) {
    $getbrok = mysqli_query($conn, "SELECT name  FROM `$maindb` WHERE accode = '$broker' ") or die(mysqli_error($conn));
    $getb = mysqli_fetch_object($getbrok);
    $brokername = $getb->name;
}

include("includes/linkshead.php");
?>
</head>
<body>
    <div class="container">
        <div class="row">
            <?php include("includes/head_rep.php"); ?>
            <div class="col-lg-12 pad">
                <table style="width:100%;background: white" class="table table-condensed table-hover">
                    <thead>
                        <tr style="height:10px;background-color: #1ABC9C">
                            <th>Broker</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        Echo "<tr>";
                        Echo "<td>$brokername</td>";
                        Echo "</tr>";

                        Echo "<tr style='height:10px;'>";
                        Echo "<td colspan='25'>";
                        Echo "<table style='width:100%;' class='table table-condensed table-hover'>";
                        Echo "<th style='background-color: #cccccc'>Sr</th>";
                        Echo "<th style='background-color: #cccccc'>Customer</th>";
                        Echo "<th style='background-color: #cccccc' class='numeric'>Opening</th>";
                        Echo "<th style='background-color: #cccccc' class='numeric'>Inv. Amount</th>";
                        Echo "<th style='background-color: #cccccc' class='numeric'>Recd. Amount</th>";
                        Echo "<th style='background-color: #cccccc' class='numeric'>Balance Amount</th>";
                        Echo "<tr>";
                        Echo "</td>";

                        $totopen = 0;
                        $totinv = 0;
                        $totrecd = 0;
                        $totbal = 0;

                        $ctr = 0;
                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            $mopen = $pmain[openbal];
                            $mdc = $pmain[dc];
                            $partyname = $pmain[name];
                            $paccode = $pmain[party];

                            $getopen = mysqli_query($conn, "SELECT sum(amount) as opamt FROM `actran` WHERE `accode`= '$paccode' and 
                                                    date < '$fromdate'") or die(mysqli_error($conn));
                            $geto = mysqli_fetch_object($getopen);
                            $acopen = $geto->opamt;

                            if ($mdc == 'D') {
                                $mopen = $mopen * -1;
                            }

                            $openbal = $mopen + $acopen;

                            $getinv = mysqli_query($conn, "SELECT sum(netamt) as invamt FROM `$trandb` WHERE `party`='$paccode' and 
                                                    vouchdt >= '$fromdate' and vouchdt <= '$todate' ") or die(mysqli_error($conn));
                            $geti = mysqli_fetch_object($getinv);
                            $invamt = $geti->invamt;

                            $getrecd = mysqli_query($conn, "SELECT sum(amount) as recdamt FROM `actran` WHERE `accode`='$paccode' and 
                                                    date >= '$fromdate' and date <= '$todate' and amount > 0 ") or die(mysqli_error($conn));
                            $getr = mysqli_fetch_object($getrecd);
                            $recdamt = $getr->recdamt;

                            $balamt = $openbal + $invamt - $recdamt;

                            $totopen = $totopen + $openbal;
                            $totinv = $totinv + $invamt;
                            $totrecd = $totrecd + $recdamt;
                            $totbal = $totbal + $balamt;

                            $ctr++;
                            Echo "<tr>";
                            Echo "<td>$ctr</td>";
                            Echo "<td>$partyname</td>";
                            Echo "<td class='numeric'>" . number_format($openbal, 2) . "</td>";
                            Echo "<td class='numeric'>" . number_format($invamt, 2) . "</td>";
                            Echo "<td class='numeric'>" . number_format($recdamt, 2) . "</td>";
                            Echo "<td class='numeric'>" . number_format($balamt, 2) . "</td>";
                            Echo "</tr>";
                        }
                        Echo "<tr>";
                        Echo "<td></td>";
                        Echo "<td>Total</td>";
                        Echo "<td class='numeric'>" . number_format($totopen,2) . "</td>";
                        Echo "<td class='numeric'>" . number_format($totinv, 2) . "</td>";
                        Echo "<td class='numeric'>" . number_format($totrecd, 2) . "</td>";
                        Echo "<td class='numeric'>" . number_format($totbal, 2) . "</td>";
                        Echo "</tr>";
                        Echo "</table>";
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
