<?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 jobcard_master 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
	});
	$('#production_date').datepicker({
		format: "yyyy-mm-dd",
		startDate: "<?=$today?>",
        //endDate: "<?//=$today?>",
        todayHighlight: true,
		autoclose: true
	});
});
////// function to extract qty of SFG which has to be ready to complete main JOB
function getJobQty(val){
	var spltval = val.split("~");
	if(spltval[2]){
		$("#job_qty").val(spltval[2]);
	}else{
		$("#job_qty").val("");
	}
}
////// 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);
}
  </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 Sub Job Against Job</h2>
                <h4 align="center"><?=$docid?></h4>
        		<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;Sub Job 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['planning_no'];?>');"><?php echo $row_locdet['planning_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['planning_date'])?>
                            </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" onChange="getJobQty(this.value);">
                                    <option value=""/>--Please Select--</option>
                                    <?php 
                                    $res_jp = mysqli_query($link1,"Select partcode, qty, balance_qty from  jobcard_data where system_ref_no='".$docid."' and balance_qty >0")or  die("ER1".mysqli_error($link1));
                                    while($row_jp = mysqli_fetch_array($res_jp)){
										$only_sfg = getAnyDetails($row_jp['partcode'],"itemtypeid","partcode","partcode_master",$link1);
										if($only_sfg == 4){
                                    ?>
                                    <option value="<?=$row_jp['partcode'].'~'.$row_jp['qty'].'~'.$row_jp['balance_qty']?>" <?php if(isset($_REQUEST['job_product'])){ if($_REQUEST['job_product']==$row_jp['partcode'].'~'.$row_jp['qty'].'~'.$row_jp['balance_qty']){echo "selected";}}?>>
            <?=getAnyDetails($row_jp['partcode'],"part_name","partcode","partcode_master",$link1)." (QTY- ".($row_jp['balance_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>
                            <div class="col-md-6" align="left">
                                <input type="text" name="job_qty" class="form-control number required" id="job_qty" style="width:150px;" value="<?php if(isset($_POST['job_qty'])){ echo $_POST['job_qty'];}?>" readonly>
                            </div>
                          </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>
                        &nbsp;&nbsp;&nbsp;<button title="Back" type="button" class="btn<?=$btncolor?>" onClick="window.location.href='<?=$_REQUEST['pageref']?>.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'];}?>&id=<?php if(isset($_REQUEST['id'])){ echo $_REQUEST['id'];}?><?=$pagenav?>'"><i class="fa fa-reply fa-lg"></i>&nbsp;&nbsp;Back</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_subjob_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,6);?></td>
									<td><?=$row_bom['bom_unit']?></td>
									<td align="right"><?php echo getQtyFormate($purqty,6);?></td>
									<td><?=$partdet[1]?></td>
								  </tr>
								<?php }?>
								 <tr>
									<td align="right"><label class="control-label">Production Date <span class="red_small">*</span></label></td>
									<td align="left">
                                    <div style="display:inline-block;float:left;"><input type="text" class="form-control span2 required" name="production_date"  id="production_date" style="width:100px;" required value="<?php if(isset($_POST['production_date'])){ echo $_POST['production_date'];}?>"></div><div style="display:inline-block;float:left;">&nbsp;<i class="fa fa-calendar fa-lg"></i>
                              </div>
                                    <input name="main_jobno" id="main_jobno" 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>
									<td align="right"><label class="control-label">Remark <span class="red_small">*</span></label></td>
									<td colspan="4" align="left"><textarea name="remark" id="remark" class="form-control addressfield required" style="resize:none" required></textarea></td>
								  </tr>  
								<tr>
									<td align="center" colspan="7">
									  <button class='btn<?=$btncolor?>' id="saveButton" type="submit" name="saveButton" value="Save"><i class="fa fa-save fa-lg"></i>&nbsp;&nbsp;Save</button>
									 <button title="Back" type="button" class="btn<?=$btncolor?>" onClick="window.location.href='<?=$_REQUEST['pageref']?>.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'];}?>&id=<?php if(isset($_REQUEST['id'])){ echo $_REQUEST['id'];}?><?=$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='<?=$_REQUEST['pageref']?>.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'];}?>&id=<?php if(isset($_REQUEST['id'])){ echo $_REQUEST['id'];}?><?=$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="frm3" name="frm3">
<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>