<?php
require_once("../includes/config.php");
$arrstatus = getFullStatus("", $link1);
$docid = base64_decode($_REQUEST['id']);
//// grn details
$sql_grn = "SELECT * FROM grn_inv_master where system_ref_no='" . $docid . "'";
$res_grn = mysqli_query($link1, $sql_grn);
$row_grn = mysqli_fetch_assoc($res_grn);
/////// bin allocation save
if ($_POST) {
	if ($_POST['addButton'] == 'Save') {
		////// initialize parameter   
        $flag = true;
        mysqli_autocommit($link1, false);
        $err_msg = "";
		/////// fetch all entered bin no. and bin qty
        for($i=0; $i <= $_POST['rowno']; $i++){
			if($_POST['bin_no'][$i]!="" && $_POST['bin_qty'][$i]!="" && $_POST['bin_qty'][$i]!="0"){
			/////check bin no. along with partcode, location code and  stock type is already in system or not
        	if (mysqli_num_rows(mysqli_query($link1, "select id from bin_allocation where bin_id='" . $_POST['bin_no'][$i] . "' and location_code='" . $_POST['location_code'] . "' and partcode='" . $_POST['partcode'] . "' and stock_type='" . $_POST['stock_type'][$i] . "'")) > 0) {
            	////////// update bin qty///////////
            	$sql_bin = "UPDATE bin_allocation set qty = qty + '" . $_POST['bin_qty'][$i] . "' ,update_by='" . $_SESSION['userid'] . "',update_date='" . $datetime . "' where bin_id='" . $_POST['bin_no'][$i] . "' and location_code='" . $_POST['location_code'] . "' and partcode='" . $_POST['partcode'] . "' and stock_type='" . $_POST['stock_type'][$i] . "'";
            	$res_bin = mysqli_query($link1, $sql_bin);
			} else {
            	////////// bin_allocation insert ///////////
            	$sql_bin = "INSERT INTO bin_allocation set bin_id='" . $_POST['bin_no'][$i] . "', location_code='" . $_POST['location_code'] . "',partcode='" . $_POST['partcode'] . "',stock_type='" . $_POST['stock_type'][$i] . "',qty='" . $_POST['bin_qty'][$i] . "' ,update_by='" . $_SESSION['userid'] . "',update_date='" . $datetime . "' ";
            	$res_bin = mysqli_query($link1, $sql_bin);
			}
			//// check if query is not executed
			if (!$res_bin) {
					$flag = false;
					$err_msg = "ER 1. " . mysqli_error($link1) . ".";
			}
			////////// bin_allocation history insert ///////////
			$sql_binh = "INSERT INTO bin_history set document_no='".$docid."', document_date='".$row_grn['receive_date']."', bin_id='" . $_POST['bin_no'][$i] . "',bin_qty='" . $_POST['bin_qty'][$i] . "' ,partcode='" . $_POST['partcode'] . "',stock_type='" . $_POST['stock_type'][$i] . "' ,entry_by='" . $_SESSION['userid'] . "',entry_date='" . $datetime . "'";
			$res_binh = mysqli_query($link1, $sql_binh);
			//// check if query is not executed
			if (!$res_binh) {
					$flag = false;
					$err_msg = "ER 2. " . mysqli_error($link1) . ".";
			}
			}
		}///// close for loop
        ////////// grn_inv_data upadate ///////////
        $sql_gid = "UPDATE grn_inv_data set bin_allocation='Y' where system_ref_no='" . $docid ."' and partcode='".$_POST['partcode']."'";
        $res_gid = mysqli_query($link1, $sql_gid);
        //// check if query is not executed
        if (!$res_gid) {
        	$flag = false;
            $err_msg = "ER 3. " . mysqli_error($link1) . ".";
        }
        ////// transaction history
		$flag =	dailyActivity($_SESSION['userid'],$docid,"BIN ALLOCATION","BIN ALLOCATE",$ip,$link1,$flag);
		///// check both master and data query are successfully executed
		if ($flag) {
			mysqli_commit($link1);
			$msg = "Bin is successfully allocated";
			$cflag="success";
			$cmsg = "Success";
		} else {
			mysqli_rollback($link1);
			$msg = "Request could not be processed ".$err_msg.". Please try again.";
			$cflag="danger";
			$cmsg = "Failed";
		} 
		mysqli_close($link1);	
	}
	///// move to parent page
	header("location:bin_allocation_iqc.php?id=".base64_encode($docid)."&status=".$_REQUEST['status']."&request_from=".$_REQUEST['request_from']."&request_to=".$_REQUEST['request_to']."&daterange=".$_REQUEST['daterange']."&msg=".$msg."&chkflag=".$cflag."&chkmsg=".$cmsg."".$pagenav);
	exit;
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title><?= siteTitle ?></title>
        <link rel="shortcut icon" href="../images/titleimg.png" type="image/png">
        <script src="../js/jquery.js"></script>
        <?php
        include("../includes/fontawesome.html");
        include("../includes/main_css.html");
        include("../includes/bootstrap.html");
        include("../includes/datatables.html");
        ?>
        <script type="text/javascript" language="javascript" >
		$(document).ready(function(){
        	$("#frm2").validate();
		});
        ////// function for open model to view bin allocation details
            function openModel(docid,pk,loc_code) {

                $.get('bin_allocation_request_viewonly.php?doc_id=' + docid + '&pky=' + pk + '&loccode=' + loc_code, function (html) {
                    $('#courierModel .modal-body').html(html);
                    $('#courierModel').modal({
                        show: true,
                        backdrop: "static"
                    });
                });
                $("#docno").html(docid);
            }



        </script>
        <script src="../js/jquery.validate.js"></script>
    </head>
    <body>
        <div class="container-fluid">
            <div class="row content">
                <?php
                include("../includes/leftnav_admin.php");
                ?>
                <div class="<?= $screenwidth ?>">
                    <h2 align="center"><i class="fa <?= $fa_icon ?>"></i> Bin Allocation IQC</h2>
                    <h4 align="center"><?php echo "( " . $row_grn['system_ref_no'] . " )"; ?></h4>
                     <?php if (isset($_REQUEST['msg'])) { ?>
                        <div class="alert alert-<?php echo $_REQUEST['chkflag']; ?> alert-dismissible" role="alert">
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                            <strong><?php echo $_REQUEST['chkmsg']; ?>!</strong>&nbsp;&nbsp;<?= $_REQUEST['msg'] ?>.
                        </div>
                    <?php } ?>
                    <div class="form-group"  id="page-wrap" style="margin-left:10px;" >
                        <form id="frm1" name="frm1" class="form-horizontal" action="" method="post">
                            <div class="panel-group">
                                <div class="panel panel-success table-responsive">
                                    <div class="panel-heading"><i class="fa fa-newspaper-o fa-lg"></i>&nbsp;&nbsp;GRN Details</div>
                                    <div class="panel-body">
                                        <table class="table table-bordered" width="100%">
                                            <tbody>
                                                <tr>
                                                    <td width="20%"><label class="control-label">Vendor Name</label></td>
                                                    <td width="30%"><?php echo $row_grn['vendor_name'] . " (" . $row_grn['vendor_code'] . ")"; ?></td>
                                                    <td width="20%"><label class="control-label">Location Name</label></td>
                                                    <td width="30%"><?php echo getAnyDetails($row_grn['location_code'], "locationname", "location_code", "location_master", $link1) . " (" . $row_grn['location_code'] . ")"; ?></td>
                                                </tr>
                                                <tr>
                                                    <td width="20%"><label class="control-label">Vendor Address</label></td>
                                                    <td width="30%"><?= $row_grn['vendor_address'] ?></td>
                                                    <td width="20%"><label class="control-label">Status</label></td>
                                                    <td width="30%"><?= $arrstatus[$row_grn['status']]; ?></td>
                                                </tr>
                                                <tr>
                                                    <td width="20%"><label class="control-label">Gate Entry No.</label></td>
                                                    <td width="30%"><?= $row_grn['gate_entry_no']; ?></td>
                                                    <td width="20%"><label class="control-label">PO No./Ref. No.</label></td>
                                                    <td width="30%"><?= $row_grn['po_no'] ?></td>
                                                </tr>
                                                <tr>
                                                    <td width="20%"><label class="control-label">Create By</label></td>
                                                    <td width="30%"><?= getAnyDetails($row_grn['entry_by'], "name", "username", "admin_users", $link1) . " (" . $row_grn['entry_by'] . ")"; ?></td>
                                                    <td width="20%"><label class="control-label">Create Date</label></td>
                                                    <td width="30%"><?= dt_format($row_grn['entry_date']) . " | " . time_format($row_grn['entry_time']); ?></td>
                                                </tr>
                                                <tr>
                                                    <td width="20%"><label class="control-label">Receive By</label></td>
                                                    <td width="30%"><?= getAnyDetails($row_grn['receive_by'], "name", "username", "admin_users", $link1) . " (" . $row_grn['receive_by'] . ")"; ?></td>
                                                    <td width="20%"><label class="control-label">Receive Date</label></td>
                                                    <td width="30%"><?= dt_format($row_grn['receive_date']) . " | " . time_format($row_grn['receive_time']); ?></td>
                                                </tr>
                                                <tr>
                                                    <td><label class="control-label">GRN Remark</label></td>
                                                    <td colspan="3"><?= $row_grn['remark'] ?></td>
                                                </tr> 
                                                <tr>
                                                    <td width="20%"><label class="control-label">IQC By</label></td>
                                                    <td width="30%"><?= getAnyDetails($row_grn['iqc_by'], "name", "username", "admin_users", $link1) . " (" . $row_grn['iqc_by'] . ")"; ?></td>
                                                    <td width="20%"><label class="control-label">IQC Date</label></td>
                                                    <td width="30%"><?= dt_format($row_grn['iqc_date']) . " | " . time_format($row_grn['iqc_time']); ?></td>
                                                </tr>
                                                <tr>
                                                    <td><label class="control-label">IQC Remark</label></td>
                                                    <td colspan="3"><?= $row_grn['iqc_remark'] ?></td>
                                                </tr>                                
                                            </tbody>
                                        </table>
                                    </div><!--close panel body-->
                                </div><!--close panel-->
                                <div class="panel panel-success table-responsive">
                                    <div class="panel-heading"><i class="fa fa-search fa-lg"></i>&nbsp;&nbsp;IQC of GRN</div>
                                    <div class="panel-body">
                                        <table class="table table-bordered" width="100%">
                                            <thead>
                                                <tr class="<?= $tableheadcolor ?>">
                                                    <th width="20%" rowspan="2" style="text-align:center;">Part Name</th>
                                                    <th width="14%" rowspan="2" style="text-align:center;">PO No.</th>
                                                    <th width="10%" rowspan="2" style="text-align:center;">GRN Qty</th>
                                                    <th colspan="4" style="text-align:center;">IQC As</th>
                                                    <th width="10%" rowspan="2" style="text-align:center;">Remark</th>
                                                    <th width="10%" rowspan="2" style="text-align:center;">Bin Allocation</th>
                                                </tr>
                                                <tr class="<?= $tableheadcolor ?>">
                                                    <th width="10%" style="text-align:center;">OK</th>
                                                    <th width="10%" style="text-align:center;">IQC</th>
													<th width="10%" style="text-align:center;">VR</th>
													<th width="10%" style="text-align:center;">PR</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                /// fetch selected PO items
                                                $i = 1;
                                                $res_grndata = mysqli_query($link1, "SELECT * FROM grn_inv_data where system_ref_no='" . $docid . "'");
                                                while ($row_grndata = mysqli_fetch_assoc($res_grndata)) {
                                                    $partdet = explode("~", getAnyDetails($row_grndata['partcode'], "part_name,hsn_code", "partcode", "partcode_master", $link1));
                                                    ?>
                                                    <tr>
                                                        <td><?php echo $partdet[0] . " (" . $row_grndata['partcode'] . ")"; ?></td>
                                                        <td><?= $row_grndata['system_ref_no'] ?></td>
                                                        <td align="right"><?= $row_grndata['receive_qty'] ?></td>
                                                        <td align="right"><?= $row_grndata['okqty'] ?></td>
                                                        <td align="right"><?= $row_grndata['damage_qty'] ?></td>
														<td align="right"><?= $row_grndata['faulty_qty'] ?></td>
														<td align="right"><?= $row_grndata['p_rej_qty'] ?></td>
                                                        <td><?= $row_grndata['remark'] ?></td>
                                                        <td><?php if($row_grndata["bin_allocation"]==""){ ?><button title='Click to allocate stock in BIN' type='button' class='btn<?= $btncolor ?>' onClick = "openModel('<?= $row_grndata['system_ref_no'] ?>','<?= $row_grndata['id'] ?>','<?=$row_grn['location_code']?>');">Bin Allocation</button><?php }else{ echo "Bin Allocated";}?></td>
                                                    </tr>
                                                    <?php
                                                    $i++;
                                                }
                                                ?>
                                        </table>
                                        <table class="table table-bordered" width="100%">
                                            <tr>
                                                <td width="50%" colspan="2" align="center">
                                                    <button title="Back" type="button" class="btn<?= $btncolor ?>" onClick="window.location.href = 'grn_iqc.php?status=<?php if (isset($_REQUEST['status'])) {
                                                    echo $_REQUEST['status'];
                                                } ?>&request_from=<?php if (isset($_REQUEST['request_from'])) {
                                                    echo $_REQUEST['request_from'];
                                                } ?>&request_to=<?php if (isset($_REQUEST['request_to'])) {
                                                    echo $_REQUEST['request_to'];
                                                } ?>&daterange=<?php if (isset($_REQUEST['daterange'])) {
                                                    echo $_REQUEST['daterange'];
                                                } ?><?= $pagenav ?>'"><i class="fa fa-reply fa-lg"></i>&nbsp;&nbsp;Back</button>
                                                </td>
                                            </tr>  
                                            <td width="50%"></tbody>
                                        </table>        
                                    </div><!--close panel body-->
                                </div><!--close panel-->
                            </div><!--close panel group-->
                        </form>

                        <!-- Start Model Mapped Modal -->
                        <div class="modal modalTH fade" id="courierModel" role="dialog">
                            <form class="form-horizontal" role="form" id="frm2" name="frm2" method="post">
                                <div class="modal-dialog modal-dialogTH modal-lg">

                                    <!-- Modal content-->
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                                            <h2 class="modal-title" align="center"><i class='fa <?= $fa_icon ?>'></i> Bin Allocation</h2>
                                            <h4 id="docno" align="center"></h4>
                                        </div>
                                        <div class="modal-body modal-bodyTH">
                                            <!-- here dynamic task details will show -->
                                        </div>
                                        <div class="modal-footer">
                                        	<button class='btn<?=$btncolor?>' id="add" type="submit" name="addButton" value="Save"><i class="fa fa-save fa-lg"></i>&nbsp;&nbsp;Save</button>
                                            <button type="button" id="btnCancel" class="btn<?= $btncolor ?>" data-dismiss="modal"><i class="fa fa-window-close fa-lg"></i> Close</button>
                                        </div>

                                    </div>
                                </div>
                            </form>
                        </div><!--close Model Mapped modal-->
                    </div><!--End form group--> 
                </div><!--End col-sm-9--> 
            </div><!--End row content--> 
        </div>
        <!--End container fluid-->
<?php
include("../includes/footer.php");
include("../includes/connection_close.php");
?>
    </body>
</html>
