<?php
require_once("../includes/config.php");

$arrstatus = getFullStatus("",$link1);
$get_opr_rgts = getOprRights($_SESSION['userid'],$_REQUEST['pid'],$link1);
$docid=base64_decode($_REQUEST['id']);
///// planning details

$sql_locdet="SELECT * FROM request_production_p where system_ref_no='".$docid."'";
$res_locdet=mysqli_query($link1,$sql_locdet);
$row_locdet=mysqli_fetch_assoc($res_locdet);
?>
<!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 src="../js/jquery.validate.js"></script>
  <link rel="stylesheet" href="../css/datepicker.css">
  <script src="../js/bootstrap-datepicker.js"></script>
  <script>
$(document).ready(function(){
	$("#frm1").validate();
	var spinner = $('#loader');
    $("#frm2").validate({
		submitHandler: function (form) {
			if(!this.wasSent){
				this.wasSent = true;
				$(':submit', form).val('Please wait...')
							  .attr('disabled', 'disabled')
							  .addClass('disabled');
				spinner.show();		  
				form.submit();
			} else {
				return false;
			}
		}
	});
	$('#job_startdate').datepicker({
		format: "yyyy-mm-dd",
		startDate: "<?=$today?>",
        //endDate: "<?//=$today?>",
        todayHighlight: true,
		autoclose: true
	});
	$('#job_enddate').datepicker({
		format: "yyyy-mm-dd",
		startDate: "<?=$today?>",
        //endDate: "<?//=$today?>",
        todayHighlight: true,
		autoclose: true
	});
});
////// function for open model to view planning details
function openModel(docid){
	$.get('planning_request_viewonly.php?doc_id=' + docid, function(html){
		 $('#courierModel .modal-body').html(html);
		 $('#courierModel').modal({
			show: true,
			backdrop:"static"
		});
	 });
	 $("#docno").html(docid);
}



function checkqty(val1,val2){
//alert(val1);
//alert(val2);
if(parseInt(val1)>parseInt(val2)){
var msg="job qty can not be greater than request qty";
alert(msg);

document.getElementById("ent").disabled=true;

}else{

document.getElementById("ent").disabled=false;


}
}
  </script>
  <link href="../css/loader.css" rel="stylesheet"/>
  </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> Make Job Against Planning</h2>
        <div class="form-group"  id="page-wrap" style="margin-left:10px;" >
   <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;Planning Details</div>
         <div class="panel-body">
         <form id="frm1" name="frm1" class="form-horizontal" action="" method="post">
             <div class="form-group">
                 <div class="col-md-6"><label class="col-md-5 control-label">Planning No.</label>	  
                    <div class="col-md-6" align="left">
                       <button title="Click to view planning details" type="button" class="btn<?=$btncolor?>" onClick="openModel('<?php echo $row_locdet['system_ref_no'];?>');"><?php echo $row_locdet['system_ref_no']; ?></button>
                    </div>
                  </div>
                  <div class="col-md-6"><label class="col-md-5 control-label">Planning Date</label>
                    <div class="col-md-6" align="left">
                        <?=dt_format($row_locdet['entry_date'])." | ".time_format($row_locdet['entry_time'])?>
                    </div>
                  </div>
            </div><!--close form group-->
            <div class="form-group">
                 <div class="col-md-6"><label class="col-md-5 control-label">Job Product <span class="red_small">*</span></label>	  
                    <div class="col-md-6" align="left">
                       <select name="job_product" id="job_product" class="required form-control custom-select">
                            <option value=""/>--Please Select--</option>
                            <?php 
                            $res_jp = mysqli_query($link1,"Select partcode, req_qty, job_qty, production_date from request_production_c where system_ref_no='".$docid."' and (req_qty - job_qty) >0")or  die("ER1".mysqli_error($link1));
                            while($row_jp = mysqli_fetch_array($res_jp)){
                            ?>
                            <option value="<?=$row_jp['partcode'].'~'.$row_jp['req_qty'].'~'.$row_jp['job_qty'].'~'.$row_jp['production_date']?>" <?php if(isset($_REQUEST['job_product'])){ if($_REQUEST['job_product']==$row_jp['partcode'].'~'.$row_jp['req_qty'].'~'.$row_jp['job_qty'].'~'.$row_jp['production_date']){echo "selected";}}?>>
    <?=getAnyDetails($row_jp['partcode'],"part_name","partcode","partcode_master",$link1)." (QTY- ".($row_jp['req_qty']-$row_jp['job_qty']).")"?>

                            </option>

                            <?php }?>
                        </select>
                    </div>
                  </div>
                  <div class="col-md-6"><label class="col-md-5 control-label">Job Qty <span class="red_small">*</span></label>
                  <?php 
                            $res_jp = mysqli_query($link1,"Select partcode, req_qty, job_qty, production_date from request_production_c where system_ref_no='".$docid."' and (req_qty - job_qty) >0")or  die("ER1".mysqli_error($link1));
                            while($row_jp = mysqli_fetch_array($res_jp)){
                            ?>
                           

                            
                    <div class="col-md-6" align="left">
                        <input type="text" name="job_qty" id="job_qty"  class="form-control number required" onblur="checkqty(this.value,'<?=$row_jp['req_qty']-$row_jp['job_qty']?>'); ">
                    </div>
                    </option>
                      
                            <?php }?>
                  </div>
            </div><!--close form group-->
            <div class="form-group">
                 <div class="col-md-6"><label class="col-md-5 control-label">Job Start Date <span class="red_small">*</span></label>	  
                    <div class="col-md-6" align="left">
                       <div style="display:inline-block;float:left;"><input type="text" class="form-control span2 required" name="job_startdate"  id="job_startdate" style="width:150px;" required value="<?php if(isset($_POST['job_startdate'])){ echo $_POST['job_startdate'];}?>"></div><div style="display:inline-block;float:left;">&nbsp;<i class="fa fa-calendar fa-lg"></i>
                      </div>
                    </div>
                  </div>
                  <div class="col-md-6"><label class="col-md-5 control-label">Job End Date <span class="red_small">*</span></label>
                    <div class="col-md-6" align="left">
                        <div style="display:inline-block;float:left;"><input type="text" class="form-control span2 required" name="job_enddate"  id="job_enddate" style="width:150px;" required value="<?php if(isset($_POST['job_enddate'])){ echo $_POST['job_enddate'];}?>"></div><div style="display:inline-block;float:left;">&nbsp;<i class="fa fa-calendar fa-lg"></i>
                      </div>
                    </div>
                  </div>
            </div><!--close form group-->
            <div class="form-group">
                 <div class="col-md-12" align="center"><button class='btn<?=$btncolor?>' id="ent" type="submit" name="ent" value="Submit" title="Submit job details"><i class="fa fa-arrow-circle-right fa-lg"></i>&nbsp;&nbsp;Submit</button>&nbsp;&nbsp;&nbsp;<button class='btn<?=$btncolor?>' id="rest" type="reset" name="rest" value="Reset" title="Reset job details"><i class="fa fa-undo fa-lg"></i>&nbsp;&nbsp;Reset</button>
                  </div>
            </div><!--close form group-->
          </form>
        </div><!--close panel body-->
    </div><!--close panel-->
    <?php 
	if(isset($_POST['job_product'])){ 
	$expl_model = explode("~",$_POST['job_product']);
	/////check BOM is created or not for the selected product
	$is_bom = mysqli_num_rows(mysqli_query($link1,"SELECT bom_partcode,bom_qty,bom_unit,conversion_factor FROM bom_master where bom_modelcode='".$expl_model[0]."' and status='1'"));
	if($is_bom > 0){
		$prodet = explode("~",getAnyDetails($expl_model[0],"part_name,purchase_unit","partcode","partcode_master",$link1));
	?>
    <div class="panel panel-success table-responsive">
        <div class="panel-heading"><i class="fa fa-sitemap fa-lg"></i>&nbsp;&nbsp;<em>You are working on <?=$_POST['job_qty']?> qty of</em>   <strong>MODEL :</strong>  <span class="red_small"><?=$prodet[0]." , ".$prodet[1]?></span></div>
         <div class="panel-body">
         <form id="frm2" name="frm2" class="form-horizontal" action="make_job_save.php" method="post">
          <table class="table table-bordered" width="100%">
            <thead>
                <tr class="<?=$tableheadcolor?>">
                    <th width="30%">BOM Part</th>
                    <th width="15%">BOM/Unit Qty</th>
                    <th width="15%">Conversion Factor</th>
                    <th width="10%">BOM Qty</th>
                    <th width="10%">BOM Unit</th>
                    <th width="10%">Purchase Qty</th>
                    <th width="10%">Purcahse Unit</th>
                </tr>
            </thead>
            <tbody>
			<?php
			$bomid="";
			$res_bom = mysqli_query($link1,"SELECT bomid,bom_partcode,bom_qty,bom_unit,conversion_factor FROM bom_master where bom_modelcode='".$expl_model[0]."' and status='1'");
			while($row_bom = mysqli_fetch_assoc($res_bom)){
				$bomid = $row_bom['bomid'];
				$bomqty = $row_bom['bom_qty'] * $_POST['job_qty'];
				$purqty = $row_bom['bom_qty'] * $row_bom['conversion_factor'] * $_POST['job_qty'];
				$partdet = explode("~",getAnyDetails($row_bom['bom_partcode'],"part_name,purchase_unit","partcode","partcode_master",$link1));
			?>
              <tr>
                <td><?php echo $partdet[0]." (".$row_bom['bom_partcode'].")";?></td>
                <td align="right"><?=$row_bom['bom_qty']?></td>
                <td align="right"><?=$row_bom['conversion_factor']?></td>
                <td align="right"><?php echo getQtyFormate($bomqty,"2");?></td>
                <td><?=$row_bom['bom_unit']?></td>
                <td align="right"><?php echo getQtyFormate($purqty,"2");?></td>
                <td><?=$partdet[1]?></td>
              </tr>
            <?php }?>
             <tr>
                <td align="right"><label class="control-label">Remark <span class="red_small">*</span></label></td>
                <td align="left" colspan="6"><textarea name="remark" id="remark" class="form-control addressfield required" style="resize:none" required></textarea>
                <input name="post_plno" id="post_plno" type="hidden" value="<?=$_REQUEST['id']?>"/>
                <input name="bomid" id="bomid" type="hidden" value="<?=base64_encode($bomid)?>"/>
                <input name="bom_prod" id="bom_prod" type="hidden" value="<?=base64_encode($_POST['job_product'])?>"/>
                <input name="job_det" id="job_det" type="hidden" value="<?=base64_encode($_POST['job_qty']."~".$_POST['job_startdate']."~".$_POST['job_enddate']."~".$prodet[1])?>"/>
                <input name="pid" id="pid" type="hidden" value="<?=$_REQUEST['pid']?>"/>
               <input name="hid" id="hid" type="hidden" value="<?=$_REQUEST['hid']?>"/>
               <input name="icn" id="icn" type="hidden" value="<?=$_REQUEST['icn']?>"/>
                </td>
             </tr>  
            <tr>
                <td align="center" colspan="7">
                  <?php if($get_opr_rgts['add']=="Y"){ ?>
                  <button class='btn<?=$btncolor?>' id="saveButton" type="submit" name="saveButton" value="Save"><i class="fa fa-save fa-lg"></i>&nbsp;&nbsp;Save</button>
                  <?php }?>
                  <button title="Back" type="button" class="btn<?=$btncolor?>" onClick="window.location.href='store_planning_job.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>  
            </tbody>
          </table>
          </form>
        </div><!--close panel body-->
    </div><!--close panel-->    
    <?php 
	}else{
	?>
    <div class="form-group">
        <div class="col-md-12 alert alert-danger alert-dismissible" role="alert" align="center">
        	<strong>Alert !</strong>&nbsp;&nbsp;No active BOM is found in database for this product.
        </div>
    </div>
    <div class="form-group">
    	<div class="col-md-12" align="center">
        	<button title="Back" type="button" class="btn<?=$btncolor?>" onClick="window.location.href='store_planning_job.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>
        </div>
    </div>
	<?php 
	}
	}
	?>
    </div><!--close panel group-->
	
        </div>
        <!--End form group--> 
      </div>
      <!--End col-sm-9--> 
    </div>
    <!--End row content--> 
  </div>
  <!--End container fluid-->
  <div id="loader"></div>
  <?php
include("../includes/footer.php");
include("../includes/connection_close.php");
?>
<!-- Start Model Mapped Modal -->
<div class="modal modalTH fade" id="courierModel" role="dialog">
<form class="form-horizontal" role="form" id="frm2" name="frm2">
<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-bar-chart faicon'></i> Planning Request</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 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-->
</body>
</html>
