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

$formname = "Profit Sharing";
$file = "profshar.php";

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

$profres = mysqli_query($conn, "SELECT * FROM `profit` ") or die(mysqli_error($conn));
$getprof = mysqli_fetch_object($profres);
$actprof = $getprof->amount;
if (empty($actprof)) {
    $actprof = 0.00;
}

$result = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `grpcode`='050' ORDER BY name") or die(mysqli_error($conn));


if (isset($_POST["submit"])) {
    $a = 1;
    $count = $_POST["items"];
    $totperc = $_POST["totperc"];

    if ($totperc != 100) {
        browse("Percentage Total Should Be 100");
        $a = 0;
    }

    if ($a == 1) {
        $ctr = 0;
        for ($i = 0; $i < $count; $i++) {
            $maccode = $_POST["accode_$i"];
            $percent = $_POST["percent_$i"];
            $prolos = $_POST["prolos_$i"];
            
            if ($prolos > 0) {
                $pdc = 'C' ;
            } else {
                $pdc = 'D' ;
            }

            mysqli_query($conn, "UPDATE `accmast` SET `percent`='$percent', `currbal` = '$prolos', "
                    . "`currdc` = '$pdc'  WHERE `accode`= '$maccode' ") or die(mysqli_error($conn));
        }
        updmess();
        header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
    }
}


include("includes/linkshead.php");
?>
<link href="MyStyle/rep_format.css" rel="stylesheet">
<style>
    @media print {
        .noPrint {
            display:none;
        }
    }

</style>
<script>
    $(document).ready(function () {
        $("body").on("focus keyup blur change", '.perc,.amt,.pro,.totperc,.totamt', calperc);
        function calperc() {
            var tr = $(this).closest("tr"),
                    iperc = tr.find(".perc").val(),
                    iamt = $('.amt').val();

            var actamt = (iperc * iamt) / 100;

            tr.find(".pro").val(actamt);

            var valperc = 0;

            $(".perc").each(function () {
                if (!isNaN(this.value) && this.value.length !== 0) {
                    valperc += parseFloat(this.value);
                }
            });
            $(".totperc").val(valperc.toFixed(2));

            var valtot = 0;

            $(".pro").each(function () {
                if (!isNaN(this.value) && this.value.length !== 0) {
                    valtot += parseFloat(this.value);

                }
            });
            $(".totamt").val(valtot.toFixed(2));
        }

    });

</script>
</head>
<body>
    <form name="" class="validation" id="form" action="" method="post">
        <div class="row">
            <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
                <label class="label-control marg"><b><?php Echo $formname; ?></b></label>
            </div>
            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2">
                <label class="label-control marg">Profit / Loss : </label>
            </div>
            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1">
                <label class="label-control marg"><?php Echo number_format($actprof, 2); ?></label>
                <?php echo "<input type='hidden' class='amt' value='$actprof' >"; ?>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-12 pad">
                <table style="width:100%;background: white" class="table table-bordered table-condensed table-hover">
                    <thead>
                        <tr style="height:10px;background-color: #26A69A">
                            <th width="50%">Account Name</th>
                            <th width="10%">Init</th>
                            <th width="10%" class="text-right">Share %</th>
                            <th width="25%" class="text-right">Profit/Loss</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $ctr = 0;
                        while ($row = mysqli_fetch_array($result)) {
                            echo "<tr>";

                            $mperc = number_format($row['percent'],2);
                            $prolos = $actprof * ($mperc / 100);

                            
                            
                            
                            echo '<input type="hidden" name="accode_' . $ctr . '" value="' . $row["accode"] . '">';
                            echo '<td><input type="text"  name="acname_' . $ctr . '"  value="' . $row["name"] . '" class="form-control " readonly tabindex="-1"></td>';
                            echo '<td><input type="text"  name="init_' . $ctr . '" value="' . $row["init"] . '" class="form-control right"  readonly/ tabindex="-1"></td>';

                            echo "<td><input type='text'  name='percent_$ctr' value='$mperc' class='form-control right text-right perc' autofocus/></td>";
                            echo '<td><input type="number"  name="prolos_' . $ctr . '" value="' . number_format((float)$prolos,2,'.','') . '" class="form-control right text-right pro" readonly tabindex="-1"></td>';
                            echo '</tr>';
                            $ctr++;
                        }
                        ?>
                    </tbody>
                </table>

            </div>
        </div>
        <div class="row">
            <table width="100%">
                <tr>
                    <td width="30%" class="text-right pad">
                        <label class="label-control pad" style="color:#26A69A;">(Total Of Share% Should be 100)</label>
                    </td>
                    <td width="4%">
                        <input type="text" name="totperc" style="color:#26A69A;" class="form-control text-right totperc" readonly>
                    </td>
                    <td width="1%" class="text-right pad">
                        <label class="label-control pad"></label>
                    </td>
                    <td width="12%" class="text-right pad">
                        <input type="text" style="color:#26A69A;" name="totamt" class="form-control text-right totamt" readonly>
                    </td>
                </tr>
            </table>
        </div>
        <div style="overflow: auto; ">
            <table class="table-condensed" style="margin: 0 auto;">
                <tr>
                    <td class=" buttonform tooltip-bottom" id="ssubmit" style="display: <? Echo $savedisabled ?>" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Save Record">
                        <button id='submit' type='submit' name='submit' value='<?php echo ($addflag == 0) ? "Update" : "Save"; ?>' class="btn btn-pad btn-square " style="background-color:#008D4C; ">
                            <i class="fa fa-check" style="color:#ffffff"></i>
                        </button>
                    </td>
                    <td class=" buttonform tooltip-bottom" id="hexit" style="display: <? Echo $exitdisabled ?>" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Exit">
                        <button id='exit' type="button" class="btn btn-pad btn-square " style="background-color:#cccccc; "  onClick="window.location.href ='default.php'">
                            <i class="fa fa-power-off" style="color:red"></i>
                        </button>
                    </td>
                <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : $ctr; ?>" />  
                </tr>
            </table>
        </div>
    </form>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>



