<?php require_once('../auth/auth.php');
require_once('../auth/acid_balance.php');
require_once('../auth/function.php');
require_once('../auth/class_function.php');
?>
<?php




?>
<?php
$inacc = mysqli_query($conn, "SELECT co_name,owner_name,co_phone,co_email,co_address,co_logo FROM company WHERE coid='$coid' LIMIT 1") or die(mysqli_error($conn));
$cor = mysqli_fetch_assoc($inacc);
$co_logo = $cor['co_logo'];


$search     = $_GET['search'];
$bbid       = $_GET['bbid'];
$bbrid      = $_GET['bbrid'];
$debitor_id = $_GET['debitor_id'];
$nrow       = $_GET['nrow'];
$ttype      = $_GET['ttype'];
$areaid     = $_GET['areaid'];
$customer_type = $_GET['customer_type'];
$vou_type    = $_GET['vou_type'];
$fdate       = $_GET['fdate'];
$edate       = $_GET['edate'];

if ($bbid == '0') {
    $bbidcond = '';
} elseif ($bbid > 0) {
    $bbidcond = " AND accounts.bid=$bbid";
}

if ($bbrid == '0') {
    $bbridcond = '';
} elseif ($bbrid > 0) {
    $bbridcond = " AND accounts.brid=$bbrid";
}

if ($customer_type == '0') {
    $custype_condi = '';
} elseif ($customer_type == 'retail') {
    $custype_condi = " AND accounts.acsub_type='retail' ";
} elseif ($customer_type == 'ws') {
    $custype_condi = " AND accounts.acsub_type='ws' ";
} elseif ($customer_type == 'distri') {
    $custype_condi = " AND accounts.acsub_type='distri' ";
} elseif ($customer_type == 'emi') {
    $custype_condi = " AND accounts.acsub_type='emi' ";
} elseif ($customer_type == 'B2B') {
    $custype_condi = " AND accounts.acsub_type='B2B' ";
}

if ($areaid == '0') {
    $area_condi = '';
} elseif ($areaid > 0) {
    $area_condi = " AND accounts.ac_area=$areaid";
}

if ($ttype == '0') {
    $ttype_condi = '';
} elseif ($ttype == 'dr') {
    $ttype_condi = " AND accounts.balance > 0";
} elseif ($ttype == 'cr') {
    $ttype_condi = " AND accounts.balance < 0";
}

if ($search == '0') {
    $blacksearch = '';
} else {
    $blacksearch = " AND (accounts.ac_name LIKE '%$search%' || accounts.outlet_id LIKE '%$search%' || accounts.ac_alise LIKE '%$search%' || accounts.ac_number LIKE '%$search%' || accounts.ac_details LIKE '%$search%')";
}

if ($vou_type == '0') {
    $voucond = '';
} elseif ($vou_type == 'OP') {
    $voucond = " AND (transaction.ttype = 'OP')";
} elseif ($vou_type == 'OPINS') {
    $voucond = " AND (transaction.ttype = 'OPINS')";
} elseif ($vou_type == 'SOP') {
    $voucond = " AND (transaction.ttype = 'SOP')";
} elseif ($vou_type == 'EADJ') {
    $voucond = " AND (transaction.ttype = 'EADJ')";
} elseif ($vou_type == 'R') {
    $voucond = " AND (transaction.ttype = 'R')";
} elseif ($vou_type == 'INSR') {
    $voucond = " AND (transaction.ttype = 'INSR')";
} elseif ($vou_type == 'P') {
    $voucond = " AND (transaction.ttype = 'P')";
} elseif ($vou_type == 'JUR') {
    $voucond = " AND (transaction.ttype = 'JUR')";
} elseif ($vou_type == 'C') {
    $voucond = " AND (transaction.ttype = 'C')";
} elseif ($vou_type == 'W_R') {
    $voucond = " AND (transaction.ttype = 'W_R')";
} elseif ($vou_type == 'R_R') {
    $voucond = " AND (transaction.ttype = 'R_R')";
} elseif ($vou_type == 'R_I') {
    $voucond = " AND (transaction.ttype = 'R_I')";
}


//$itypecond = " AND (transaction.ttype='ECSL' || ttype='ERSL' || ttype='INSL')";

if ($vou_type == '0') {
$debitor_query = mysqli_query($conn, "SELECT accounts.*,route_name FROM accounts 
        LEFT JOIN route_area ON (accounts.ac_area=route_area.roid)
        WHERE accounts.coid='$coid' $bbidcond $bbridcond $blacksearch $area_condi $ttype_condi  $custype_condi
         AND gcatid='$debitor_id' AND accounts.acid NOT IN ( (select cr_acid from transaction where transaction.cr_acid =accounts.acid  AND transaction.ttype = ''  AND date(transaction.date) BETWEEN '$fdate' AND '$edate' )) ORDER BY accounts.acid LIMIT $nrow ") or die(mysqli_error($conn));
}else if($vou_type == 'R'){
    
    $debitor_query = mysqli_query($conn, "SELECT accounts.*,route_name FROM accounts 
        LEFT JOIN route_area ON (accounts.ac_area=route_area.roid)
        WHERE accounts.coid='$coid' $bbidcond $bbridcond $blacksearch $area_condi $ttype_condi  $custype_condi
         AND gcatid='$debitor_id' AND accounts.acid NOT IN ( (select cr_acid from transaction where transaction.cr_acid =accounts.acid AND transaction.ttype = 'R'  AND date(transaction.date) BETWEEN '$fdate' AND '$edate' )) ORDER BY accounts.acid LIMIT $nrow ") or die(mysqli_error($conn));
    
}else if($vou_type == 'P'){
    
    $debitor_query = mysqli_query($conn, "SELECT accounts.*,route_name FROM accounts 
        LEFT JOIN route_area ON (accounts.ac_area=route_area.roid)
        WHERE accounts.coid='$coid' $bbidcond $bbridcond $blacksearch $area_condi $ttype_condi  $custype_condi
         AND gcatid='$debitor_id' AND accounts.acid NOT IN ( (select dr_acid from transaction where transaction.dr_acid =accounts.acid AND  transaction.ttype = 'P'  AND date(transaction.date) BETWEEN '$fdate' AND '$edate' )) ORDER BY accounts.acid LIMIT $nrow ") or die(mysqli_error($conn));
    
}
$dataa = [];
while ($prow = mysqli_fetch_assoc($debitor_query)) {
    $dataa[] = $prow;
}

$debitor_query_count = mysqli_query($conn, "SELECT accounts.*,route_name,COUNT(*) AS accc_count FROM accounts 
        LEFT JOIN route_area ON (accounts.ac_area=route_area.roid)
        WHERE accounts.coid='$coid' $bbidcond $bbridcond $blacksearch $area_condi $ttype_condi  $custype_condi
         AND gcatid='$debitor_id' AND accounts.acid NOT IN ( (select cr_acid from transaction where transaction.cr_acid =accounts.acid   $voucond  AND date(transaction.date) BETWEEN '$fdate' AND '$edate' )) ORDER BY accounts.acid LIMIT $nrow ") or die(mysqli_error($conn));
$acc_2count = mysqli_fetch_assoc($debitor_query_count);
$totalValue = $acc_2count['accc_count'];

$account_list = mysqli_query($conn, " SELECT  COUNT(*) AS ac_count FROM accounts where  coid='$coid' AND bid='$bid' and brid='$brid' AND gcatid='$debitor_id' ") or die(mysqli_error($conn));
$s_ac_c = mysqli_fetch_assoc($account_list);
$target = $s_ac_c['ac_count'];

$achievement = (($totalValue / $target) * 100); // Calculate the
$total_percent =  100;
$calculation = $total_percent - $achievement;

?>

<table class="ttable border-bottom-0 d-none" id="header" cellpadding="0" cellspacing="0">
    <thead>
        <tr>
            <th rowspan="3" class="thborder border-bottom-0 text-center" style="width:10%;">

                <?php if ($co_logo != "") : ?>
                    <img class="profile-user-img img-fluid img-circle" src="data:image/gif;base64,<?php echo $co_logo; ?>" style="width:80px;height:77px;">
                <?php else : ?>
                    <img class="profile-user-img img-fluid img-circle  " src="../../dist/img/a.png" style="width:80px;height:77px;"></div>
                <?php endif; ?>



            </th>
            <th rowspan="3" class="thborder border-bottom-0" style="width:70%;">
                <dt style="font-size:18px;font-weight: 600;"><?php echo $cor['co_name']; ?></dt>
                <dt><?php echo $cor['owner_name']; ?></dt>
                <dt><?php echo $cor['co_address']; ?></dt>
                <dt><?php echo $cor['co_phone']; ?>#<?php echo $cor['co_email']; ?></dt>
            </th>
            <th class="thborder text-center" style="width:20%;"><?php echo language(35); ?></th>
        </tr>
        <tr>
            <th class="thborder text-center">

                Debitor
            </th>
        </tr>
        <tr>
            <th class="thborder text-center border-bottom-0"><?php echo date('d/m/y', strtotime($co_f_year_start)); ?> To <?php echo date('d/m/y', strtotime($date)); ?>
            </th>
        </tr>
    </thead>
</table>
<table id="none_tran_id" class="ttable" cellpadding="0" cellspacing="0">
    <input type="hidden" id="vounameset" value="Accounts Target Vs Achievement <?php echo date('d/m/y', strtotime($fdate)); ?> To <?php echo date('d/m/y', strtotime($edate)); ?> Report ">

    <div class="card-body text-center col-md-12">
        <div class=" bg-dark"> <b class="text-left"> None Transction Accounts || <b> Target Vs Achievement Percent : <span> <?php echo @number_format($calculation, 2); ?> % </span> </div>
    </div>
    <thead>
        <tr class="sticky-top">
            <th class="text-center" style="width:2%;"><?php echo language(15); ?></th>
            <th class="text-center" style="width:%;"><?php echo language(164); ?></th>
            <th class="text-center" style="width:%;"><?php echo language(110); ?></th>
            <th class="text-center" style="width:%;"><?php echo language(112); ?></th>
            <th class="text-center" style="width:%;"><?php echo language(111); ?> </th>
            <th class="text-center" style="width:%;"><?php echo language(116); ?> </th>
            <th class="text-center" style="width:%;"><?php echo language(119); ?> </th>
            <th class="text-center" style="width:%;"><?php echo language(224); ?> </th>
            <th class="text-center" style="width:%;"><?php echo language(233); ?></th>
        </tr>
    </thead>
    <tbody id="debitor_show"></tbody>
    <tfoot>
        <tr class="sticky-bottom">
            <th class="text-right" colspan="7" style="width:%;"><?php echo language(233); ?></th>
            <th class="text-right" colspan="1">
                <dt id="t_value"></dt>
            </th>
        </tr>
    </tfoot>
</table>
<script id="debitview" type="text/javascript">
    $(document).ready(function() {
        var dataa = '';
        var i = 0;
        var t_val = 0;
        $.each(<?php echo json_encode($dataa); ?>, function(key, value) {
            t_val += Number(value.balance);

            dataa += '<tr>';
            dataa += '<td class="text-center">' + ++i + '</td>';
            dataa += '<td onclick="acprofile(' + value.acid + ')">' + value.ac_name + ' / ' + value.outlet_id + '  </td>';
            dataa += '<td class="text-center">' + value.ac_alise + '</td>';
            dataa += '<td class="text-center">' + value.ac_email + '</td>';
            dataa += '<td class="text-center">' + value.ac_number + '</td>';
            dataa += '<td>' + value.ac_details + '</td>';
            dataa += '<td>' + value.acsub_type + '</td>';
            dataa += '<td class="text-center">' + value.route_name + '</td>';
            dataa += '<td class="text-right">' + value.balance + '</td>';
            dataa += '</tr>';
        });
        $("#debitor_show").html(dataa);
        $("#t_value").text(bden.format(t_val));

        $("#debitview").remove();
    });
</script>
<script type="text/javascript">
    function acprofile(acid) {
        $("#modal-xl").modal({
            backdrop: "static"
        });
        pagecall('../accounts/ac_profile.php?acid=' + acid, '#xlmdview');
    }
</script>