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

$search     = $_GET['search'];
$bbid       = $_GET['bbid'];
$bbrid      = $_GET['bbrid'];
$itype      = $_GET['itype'];
$nrow       = $_GET['nrow'];
$fdate      = $_GET['fdate'];
$edate      = $_GET['edate'];
// $us_em_id   = $_GET['us_em_id'];

$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_name        = $cor['co_name'];
$owner_name     = $cor['owner_name'];
$co_address     = $cor['co_address'];
$co_phone       = $cor['co_phone'];
$co_email       = $cor['co_email'];
$co_logo        = $cor['co_logo'];




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

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

$inven_type = 'P';


if ($itype == '0') {
    $itypecond = " AND (order_list.order_type='ECSL' || order_type='ERSL' || order_type='INSL')";
} elseif ($itype == 'EPUR') {
    $itypecond = " AND order_list.order_type='EPUR'";
} elseif ($itype == 'EPRET') {
    $itypecond = " AND order_list.order_type='EPRET'";
}


if ($search == '0') {
    $blacksearch = '';
} else {
    $blacksearch = " AND (item.item_code LIKE '%$search%' || item.item_name LIKE '%$search%' || item.item_details LIKE '%$search%')";
}

$inv_query = mysqli_query($conn, " SELECT *,item.pro_id FROM item
 LEFT JOIN pro_unit ON (item.pro_unit=pro_unit.puid)
        LEFT JOIN item_group ON (item.pgid=item_group.pgid)
        LEFT JOIN item_catagory ON (item.pcid=item_catagory.pcid)
        LEFT JOIN item_brand ON (item.pbid=item_brand.pbid)
 WHERE item.coid='$coid' $bbidcond $bbridcond $blacksearch AND item.pro_id NOT IN ( (select pro_id from order_list where order_list.pro_id =item.pro_id   $itypecond  AND date(order_list.datetime) BETWEEN '$fdate' AND '$edate' )) ORDER BY item.item_name ASC LIMIT $nrow  ") or die(mysqli_error($conn));


$dataa = [];
while ($prow = mysqli_fetch_assoc($inv_query)) {
    $dataa[] = $prow;
}

$custom_field = mysqli_query($conn, "SELECT item FROM custom_field WHERE coid='$coid' LIMIT 1 ") or die(mysqli_error($conn));
$rowcf = mysqli_fetch_assoc($custom_field);
$p = $rowcf['item'];
function input_check($ap)
{
    if (strpos($GLOBALS['p'], $ap) !== false) {
        return 1;
    }
}


$inv_query22 = mysqli_query($conn, " SELECT *,item.pro_id, COUNT(*) AS item_count FROM item
 LEFT JOIN pro_unit ON (item.pro_unit=pro_unit.puid)
        LEFT JOIN item_group ON (item.pgid=item_group.pgid)
        LEFT JOIN item_catagory ON (item.pcid=item_catagory.pcid)
        LEFT JOIN item_brand ON (item.pbid=item_brand.pbid)
 WHERE item.coid='$coid' $bbidcond $bbridcond $blacksearch AND item.pro_id NOT IN ( (select pro_id from order_list where order_list.pro_id =item.pro_id   $itypecond  AND date(order_list.datetime) BETWEEN '$fdate' AND '$edate' )) ORDER BY item.item_name ASC LIMIT $nrow  ") or die(mysqli_error($conn));
$prow2 = mysqli_fetch_assoc($inv_query22);
$totalValue = $prow2['item_count'];


$item_obj = new item();
$target =  $item_obj->item_count($coid, $bid, $brid, $active = 1);


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


<div class="card-body cscroll" style="overflow-x: auto;overflow-y: auto;">





    <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 'Item Not ordered List'; ?></th>
</tr>
<tr>
    <th class="thborder text-center">


        <?php
        if ($inven_type == 'S') {
            echo 'Service';
        } elseif ($inven_type == 'P') {
            echo 'Product';
        }
        ?>
    </th>
</tr>
<tr>
    <th class="thborder text-center border-bottom-0"><?php echo date('d/m/y', strtotime($fdate)); ?> To <?php echo date('d/m/y', strtotime($edate)); ?>
    </th>
</tr>
</thead>
</table>

<div class="card-body text-center col-md-12">
    <div class=" bg-dark">None Transction  Item || Target Vs Achievement Percent : <span> <?php echo @number_format($calculation, 2); ?> % </span> </div>
</div>
<table id="not_order_acc" class="ttable table-bordered table-hover">
    <input type="hidden" id="vounameset" value="Item Target Vs Achievement <?php echo date('d/m/y', strtotime($fdate)); ?> To <?php echo date('d/m/y', strtotime($edate)); ?> Report ">

    <thead class="text-nowrap">
        <tr class="sticky-top" style="background:#e7e7e7;">
            <th class="text-center" style="width:2%;"><?php echo language(15); ?></th>
            <?php if ($inven_type == 'P') {  ?>
                <th class="text-center" style="width:7%;"><?php echo language(76); ?></th>
                <th style="width:25%;"><?php echo language(77); ?> </th>
                <th class="text-center" style="width:5%;"><?php echo language(78); ?></th>

                <th class="text-center" style="width:5%;"><?php echo language(80); ?></th>

                <th class="text-center" title="Purchase" style="width:5%;"><?php echo language(81); ?></th>



                <th class="text-center" title="MRP" style="width:5%;"><?php echo language(84); ?> </th>
                <th class="text-center" style="width:7%;"><?php echo language(86); ?></th>
                <th class="text-center" style="width:5%;"><?php echo language(88); ?></th>
                <th class="text-center" style="width:5%;"><?php echo language(89); ?></th>
            <?php } ?>
            <?php if ($inven_type == 'S') {  ?>
                <th class="text-center" style="width:8%;">Service Name </th>
                <th class="text-center" style="width:20%;">Service Details </th>
                <th class="text-center" style="width:20%;">MRP </th>
            <?php } ?>
            <th class="text-center" style="width:5%;"><?php echo language(90); ?> </th>
        </tr>
    </thead>
    <tbody id="proshow"></tbody>
    <tfoot style="background:#e3e3e3;">
        <tr class="sticky-bottom">


            <td> </td>






            <td class="text-right" colspan="3">
                <dt><?php echo language(91); ?></dt>
            </td>
            <td class="text-center">
                <dt id="tqty"></dt>
            </td>


            <td class="text-right" colspan="3">
                <dt><?php echo language(210); ?> </dt>
            </td>
            <td class="text-center" colspan="3">
                <dt id="tsval"></dt>
            </td>

        </tr>
    </tfoot>
</table>

</div>
<script id="sview" type="text/javascript">
    $(document).ready(function() {
        var dataa = '';
        var i = 0;
        var status = '';
        var warrenty = [];
        var tqty = 0;
        var tpur = 0;
        var tavg = 0;
        var tslm = 0;
        var tslx = 0;
        // var tval = 0;
        var tsval = 0;
        $.each(<?php echo json_encode($dataa); ?>, function(key, value) {

            tqty += Number(value.item_qty);
            tsval += Number((value.mrp_sales * value.item_qty));


            if (value.active == 1) {
                status = 'Active';
            } else {
                status = 'Deactive';
            }

            if (value.war_type == 'N') {
                warrenty = 'None';
            } else if (value.war_type == 'D') {
                warrenty = value.wa_dmy + ' Days';
            } else if (value.war_type == 'M') {
                warrenty = value.wa_dmy + ' Month';
            } else if (value.war_type == 'Y') {
                warrenty = value.wa_dmy + ' Years';
            } else if (value.war_type == 'L') {
                warrenty = value.wa_dmy + ' Lifetime';
            }


            if (value.imei_status == '0') {
                imei_status_check = '';
            } else if (value.imei_status == '1') {
                imei_status_check = '<i class="fa fa-barcode"> </i>';
            }


            dataa += '<tr>';
            dataa += '<td class="text-center">' + ++i + '</td>';
            dataa += '<td class="btn-outline-dark text-nowrap" onclick="itemprofile(' + value.pro_id + ')">' + <?php if (input_check('t_group') == 1) { ?> value.pgroup_name + ' - ' + <?php } ?> <?php if (input_check('t_category') == 1) { ?> value.pcat_name + ' - ' + <?php } ?>value.pbrand_name + '</td>';
            dataa += '<td class="text-nowrap">  ' + imei_status_check + ' ' + value.item_name + '  ' + value.item_details + '</td>';
            dataa += '<td class="text-center">' + value.item_code + '  </td>';


            if (value.item_qty == 0) {
                dataa += '<td class="text-center text-danger">' + bden.format(value.item_qty) + ' ' + value.unit_name + '</td>';
            } else if (value.item_qty < 0) {
                dataa += '<td class="text-center text-orange ">' + bden.format(value.item_qty) + ' ' + value.unit_name + '</td>';
            } else {
                dataa += '<td class="text-center">' + bden.format(value.item_qty) + ' ' + value.unit_name + '</td>';
            }

            dataa += '<td class="text-right">' + bden.format(value.mrp_purchase) + '</td>';



            dataa += '<td class="text-right">' + bden.format(value.mrp_sales) + '</td>';
            dataa += '<td class="text-center">' + warrenty + '</td>';
            dataa += '<td class="text-center">' + value.item_disc + '</td>';
            dataa += '<td class="text-right">' + bden.format((value.mrp_sales * value.item_qty)) + '</td>';
            dataa += '<td class="text-center">' + status + '</td>';
            dataa += '</tr>';
        });
        $("#proshow").html(dataa);
        $("#tqty").text(bden.format(tqty));
        $("#tsval").text(bden.format(tsval));
        $("#sview").remove();
    });



    function itemprofile(productid) {
        $("#modal-xl").modal({
            backdrop: "static"
        });
        pagecall('../itemstock/item_profile.php?pro_id=' + productid, '#xlmdview');
    }
</script>