<link rel="stylesheet" href="css/new_mxerp.css">
<?php ob_start();
$PageSecurity = 2;
include('includes/SQL_CommonFunctions.inc');
$title = _('Job Work Issue');
$PageName ='Job Work Issue';	

if($_REQUEST['Save']!='')
{
	$startYr = explode('-', $_SESSION['financialYrStart']);
	$endYr = explode('-', $_SESSION['financialYrEnd']);
	$startYr[0];
	$endYr[0];
	$financial=substr($startYr[0],-2)."-".substr($endYr[0],-2); 
	 
	 
	
	 	$branch=$_REQUEST['siteCode'];
	 	$prefSql="select prefix_code from prefix_tbl where prefix_name = 'Job Work 57 F Challan'";
		$resultPrfx = DB_query($prefSql,$db);
		$Prefxrow=DB_Fetch_array($resultPrfx);
		$prefixcode=$Prefxrow['prefix_code'];
		
		
	  $sql="select challanNumber from job_work_issue where site = '".$_REQUEST['siteCode']."' and CompanyId='".$_SESSION['ACGCompanyId']."' and INSTR(`challanNumber`, '$financial') > 0 order by challanNumber DESC LIMIT 0,1";
		$result = DB_query($sql,$db);
		$numrows = DB_num_rows($result);
		if($numrows>0)
		{
			$myrow=DB_Fetch_array($result);
			$ExplBill=explode("/",$myrow['challanNumber']);
			$maxval = $ExplBill[3];
		}
		else
		{$maxval=0;}
	 

		$strChar=$_REQUEST['siteCode']."/".$prefixcode."/".$financial."/";
		$strFinal = $maxval+1;
		$strBothFinal = $strChar.str_pad($strFinal,5,"0",STR_PAD_LEFT); 


	$itemarray=array();
	$itemarray=$_REQUEST['itemID'];
	 $itemDetailIds=implode(",",$itemarray); 
	
	$total=0;
	for($STR=0;$STR<count($_REQUEST['itemID']);$STR++)
	{
		$total=$total+$_REQUEST['quantity'][$STR];
	}
	//print_r($_REQUEST); exit;
	$insert_sql="Insert into job_work_issue set
								site='".$_REQUEST['siteCode']."',    
								vendor='".$_REQUEST['vendor']."',  
								jobNumber= '".$_REQUEST['jobNumber']."', 
								jobDate='".FormatDateForSQL($_REQUEST['jobDate'])."',  
								partyJWNo='".$_REQUEST['partyJWNo']."',  
								challanNumber='".$strBothFinal."',
								challanDate='".FormatDateForSQL($_REQUEST['challanDate'])."', 
								itemDetailIds='".$itemDetailIds."',
								form_no='".$_REQUEST['form_no']."',
								delivery_date='".FormatDateForSQL($_REQUEST['delivery_date'])."',
								expected_time='".$_REQUEST['expected_time']."',
								removable_date='".FormatDateForSQL($_REQUEST['removable_date'])."',
								removable_time='".$_REQUEST['removable_time']."',  
								remarks='".$_REQUEST['remarks']."',
								totalIssueQty='".$total."', 
								created_by='".$_SESSION['UserID']."',  
								created_date='".FormatDateForSQL(date('d/m/Y'))."',
								CompanyId='".$_SESSION['ACGCompanyId']."'";
							
	$insert_result=DB_query($insert_sql,$db);
	
	$LastId=DB_Last_Insert_ID($db,'job_work_issue','id');
	//echo $LastId; exit;
	
	  $update_sql="update job_work set status='1' where id='".$_REQUEST['jobNumber']."'"; 
$update_result=DB_query($update_sql,$db);
prnMsg( _('Record Inserted Successfully'),'success');


echo "<script>location.href='index.php?route=JobWorkIssueList&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}
?>
<script language="javascript" type="text/javascript">
function JobNumberList(str)
{
var site=document.getElementById("site").value;	
	if (str.length==0)
  {
  document.getElementById("JOBID").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("JOBID").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","JobNumber.php?vendor="+str+"&site="+site,true);
xmlhttp.send();
}


	function JobDetails(str)
	{
	if (str.length==0)
	  {
	  document.getElementById("jbdetails").value="";
	  document.getElementById("partyJWNo").value="";
	  document.getElementById("showItems").innerHTML="";
	  return;
	  }
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
		var data=xmlhttp.responseText.split("__");	
		document.getElementById("jobDate").value=data[0];
	  	document.getElementById("partyJWNo").value=data[1];
	 	document.getElementById("showItems").innerHTML=data[2];
		}
	  }
	xmlhttp.open("GET","ajax_job_details.php?jwID="+str,true);
	xmlhttp.send();
	}
	
	
	
function generateChallan(str)
{
var branchID=document.getElementById("site").value;
if(branchID=='0') { branchID=0;}

var vendorID=document.getElementById("vendor").value;
if(vendorID=='0') { vendorID=0;}

if (str.length==0)
  {
  document.getElementById("vendor").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {  //alert(xmlhttp.responseText);
	var data=xmlhttp.responseText.split("__");	
    document.getElementById("vendor").innerHTML=data[0];
	document.getElementById("jobNumber").innerHTML=data[1];
    }
  }
xmlhttp.open("GET","getVendor.php?branch="+branchID+"&vendor="+vendorID,true);
xmlhttp.send();
}

</script>
<div class="body_contain">
<div class="tab_div">

<h1>Job Work 57 Challan</h1>
<form action="" method="post">
<table width="99%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;" align="center">
  <tr>
    <td class="tabledata3">Branch</td>
    <td>
	<?php
	if($_SESSION['AssignedBranch']!='')
	  {	  	
  $ss="Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')";
	  	 $site_sql=DB_query($ss,$db);
	  }
	  else
	  {
		$ss="Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."'";
		$site_sql=DB_query($ss,$db);
	  }
	?>
	<select name="siteCode" id="site" style="width:46%" onchange="generateChallan(this.value)">
      <option value="0">Select</option>
      <?php 
					while($Row=DB_fetch_array($site_sql)){?>
             <option value="<?php echo $Row['loccode']?>"<?php echo($_SESSION['Site']==$Row['loccode'] ? 'selected':'');?> ><?php echo $Row['locationname']?></option>
      <?php  } ?>
    </select></td>
    <td class="tabledata3">Job Work No.</td>
	<td id="JOBID">
	<?php 
	$sql="select * from job_work where site='".$_REQUEST['site']."' and vendor='".$_REQUEST['vendor']."' and status='0'";
	$result=DB_query($sql,$db);
	?>
	<select name="jobNumber" id="jobNumber" style="width:46%" onChange="JobDetails(this.value)">
	<option value="0">Select</option>
	</select>
	</td>
  </tr>
  <tr>
    <td class="tabledata3">Vendor</td>
    <td><select name="vendor" id="vendor" style="width:46%" onChange="generateChallan(this.value)">
        <option name="0">Select</option>
    </select></td>
    <td colspan="2" rowspan="2" id="jbdetails">
	<table width="100%" border="1" align="left" style="border:#999999 solid 1px;">
	<tr><td class="tabledata3">Job Work Date</td>
    <td align="right"><input type="text" name="jobDate" id="jobDate" value="<?php echo $_REQUEST['jobDate'];?>" class="datepicker" readonly="true" style="width:80%" />    </td></tr>
	<tr><td class="tabledata3">Party Job Work No.</td>
	<td><input type="text" name="partyJWNo" id="partyJWNo" value="<?php echo $_REQUEST['partyJWNo'];?>" class="cap" readonly="true"  style="width:80%" /></td></tr>
	</table>
	</td>
  </tr>
  <tr>
    <td class="tabledata3">Challan No.</td>
    <td id="challanId"><input name="challanPrefix" type="text" class="cap" id="challanPrefix" value="Auto Generated"  style="width:38%" readonly/> </td>
  </tr>
  
  
  <tr>
  <td class="tabledata3">Challan Date</td>
  <?php $_REQUEST['challanDate']=date('d/m/Y');?>
  <td><input type="text" name="challanDate" id="challanDate" value="<?php echo $_REQUEST['challanDate'];?>" class="datepicker"  style="width:80%"></td>
  <td class="tabledata3">Form No. </td>
    <td  id="form_no"><input name="form_no" type="text" class="cap" id="form_no" value="<?php echo $_REQUEST['form_no'];?>"  style="width:80%" />
</td>
  </tr>
  <tr><td class="tabledata3">Delivery Date</td>
    <td><input name="delivery_date" type="text" id="delivery_date" value="<?php echo $_REQUEST['delivery_date'];?>"  class="datepicker"  style="width:80%" />
    	<td class="tabledata3">Expected Time </td>
    <td id="expected_time"><input name="expected_time" type="text" class="cap" id="expected_time" value="<?php echo $_REQUEST['expected_time'];?>"  style="width:80%" />
    	<tr><td class="tabledata3">Removal Date </td>
    	<?php $time = date('G:ia'); ?>
    <td><input name="removable_date" type="text" id="removable_date" value="<?php echo $_REQUEST['removable_date'];?>"  style="width:35%" class="datepicker"/>
Time      
  <input name="removable_time" type="text" class="cap" id="removable_time" value="<?php echo $time;?>"  style="width:36%" readonly/></tr>
</td></tr>
</table>

<br>

<div style="overflow:auto; width:810px;" id="showItems"> 
</div>
<br />
<table width="100%">
<tr>
<th>REMARKS</th>
<td><textarea name="remarks" id="remarks" style="width:100%; height:50px;" ></textarea></td>
</tr>
</table>
<p align="center"><input type="submit" name="Save" value="Save" /><input type="submit" name="Cancel" value="Cancel" /></p>
</form>

</div></div>