<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']!='')
{
	$prefix=$s->getField('companies','CompanyId',$_SESSION['ACGCompanyId'],'company_code');
  $prefix_sql=DB_query("Select * from prefix_tbl where prefix_name='".$_REQUEST['frmid']."'",$db);
  $prefix_row=DB_fetch_array($prefix_sql);
  $prefix1=$prefix_row['prefix_code'];
  
  $financial=substr($_SESSION['YearStart'],-2)."-".substr($_SESSION['YearEnd1'],-2);
    $sql="Select challanNumber from job_work_issue where site='".$_REQUEST['siteCode']."' 
  and INSTR(`challanNumber`, '$financial') > 0 order by challanNumber desc limit 0,1";

  $result=DB_query($sql,$db);
  $myrow=DB_fetch_array($result);
  //echo DB_num_rows($result);
  if(DB_num_rows($result)>0)
  {
  //$Reqno=$myrow['Reqno'];
  //$req_part1=substr($Reqno,-4);
         if($prefix!='')
      {
      $ExplBill=explode("/",$myrow['challanNumber']);
      $maxval = $ExplBill[4];
        }
        else
        {
        $ExplBill=explode("/",$myrow['challanNumber']);
      $maxval = $ExplBill[3];
        }     
  //$ExplBill=explode("/",$myrow['Reqno']);
  //$maxval = $ExplBill[3];
  //echo $req_part1;
  $req_final=$maxval+1;
  }else
  {
  $req_final="1";
  }
          if($prefix!='')
       $string=$prefix."/".$_REQUEST['siteCode']."/".$financial."/".$prefix1."/";
         else
         $string=$_REQUEST['siteCode']."/".$financial."/".$prefix1."/";

  //$string=$prefix."/".$_REQUEST['siteCode']."/".$financial."/";
   $final_string=$string.str_pad($req_final,4,"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']."', 
                        store='".$_REQUEST['store']."', 
								vendor='".$_REQUEST['vendor']."',  
                        job_type='".$_REQUEST['jobtype']."',
								jobNumber= '".$_REQUEST['jobNumber']."', 
								jobDate='".FormatDateForSQL($_REQUEST['jobDate'])."',  
								partyJWNo='".$_REQUEST['partyJWNo']."',  
								challanNumber='".$final_string."',
								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']."',
                transaction_time='".date("Y-m-d H:i:s")."',  
								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');
          // print_r($_REQUEST['chk']);
   // count($_REQUEST['chk']);
           
    for($j=0;$j<count($_REQUEST['chk']);$j++)
		{
     
 				//if($_REQUEST['chk'][$j])
				//{	
            
                   $i=$_REQUEST['chk'][$j];
                  $data_arr1['masterId']= $LastId;
						$data_arr1['mst_itemid']= $_POST['itemID'][$i];
						$data_arr1['quantity']= $_REQUEST['quantity'][$i];
						$data_arr1['process']= $_REQUEST['process'][$i];
                 // print_r($data_arr1);
						$insertResult=$s->insertRecord('job_work_issue_details',$data_arr1);
						$row2=DB_query($insertResult,$db);
                 
                  
            //}
      }      
   
   
	//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');


  for($i=0;$i<count($_REQUEST['itemdataid']);$i++)
		{
 				if($_REQUEST['itemdataid'][$i]!='')
				{			
				      
						$data_arr['parent_id']= $LastId;
						$data_arr['mst_itemid']= $_POST['itemdataid'][$i];
						$data_arr['batch_no']= $_POST['batch_no'][$i];
						$data_arr['stock_qty']= $_REQUEST['stock_qty'][$i];
						$data_arr['qty']= $_REQUEST['qty'][$i];
						$data_arr['job_rate']= $_REQUEST['job_rate'][$i];
						$data_arr['jobvalue']= $_REQUEST['job_value'][$i];
						$insertResult=$s->insertRecord('job_work_issue_details1',$data_arr);
						$row2=DB_query($insertResult,$db);
                  
               
                  
				 }
								  
						
						
		}		

echo "<script>location.href='index.php?route=Inventory/transation/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="";
     document.getElementById("jobtype").value="";
     document.getElementById("jobtype1").value="";
	  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];
      document.getElementById("jobtype").value=data[3];
      document.getElementById("showItems1").innerHTML=data[4];
       document.getElementById("jobtype1").value=data[5];
		}
	  }
	xmlhttp.open("GET","ajax_job_details1.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];
   document.getElementById("store1").innerHTML=data[2];
    }
  }
xmlhttp.open("GET","getVendor1.php?branch="+branchID+"&vendor="+vendorID,true);
xmlhttp.send();
}

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

<h1><?php echo $s->GetFieldValue("modules_form","id='".$_REQUEST['frmid']."' and moduleid='".$_REQUEST['modid']."'","frm_name"); ?></h1>
<form action="" method="post">
<table width="100%" 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  colspan="2" rowspan="4" id="jbdetails">
	<table width="100%" border="1" align="left" style="border:#999999 solid 1px;">
    <tr><td class="tabledata3">Vendor</td>
    <td><select name="vendor" id="vendor" style="width:80%" onChange="generateChallan(this.value)">
        <option name="0">Select</option>
    </select></td>
     
    </tr>
	<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>
   <tr>
   <td class="tabledata3">Job Type</td>
  <td><input type="text" name="" id="jobtype" readonly style="width:80%">
  <input type="hidden" name="jobtype" id="jobtype1" readonly style="width:80%">
  </td>
   </tr>
	</table>
	</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:64%" onChange="JobDetails(this.value)">
	<option value="0">Select</option>
	</select>
	</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:46%" readonly/> </td>    
   <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>
  </tr>
  <tr>
   <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:46%" />
</td>
  <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%" />
    
 <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:20%" class="datepicker"/>
 &nbsp;&nbsp;<strong>Time</strong>
    <input name="removable_time" type="text" class="cap" id="removable_time" value="<?php echo $time;?>"  style="width:16%" readonly/></td>
   <td class="tabledata3">Store</td>
     <td id="store1"></td>
  
</tr>

</table>

<br>

<div style="overflow:auto; width:1280px;" id="showItems"> 
</div>
<br />

<div style="overflow:auto; width:1280px;" id="showItems1"> 
</div>

<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>

<script language="javascript" type="text/javascript">

 function addRow(tableID) 
  { 
	if(document)
		var table = document.getElementById(tableID);		
		var rowCount = table.rows.length;
		var row = table.insertRow(rowCount);
		document.getElementById("hiditemcount").value=rowCount;
		var colCount = table.rows[0].cells.length;
		for(var i=0; i<colCount; i++) {	            
			var newcell = row.insertCell(i);			
             newcell.innerHTML = table.rows[0].cells[i].innerHTML;
				switch(newcell.childNodes[0].type) 
				{
				case "text":
					newcell.childNodes[0].value = "";
					var nod_id = newcell.childNodes[0].id;

					newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
										
				break;

				case "hidden":

					newcell.childNodes[0].value = "";

					var nod_id_hidden = newcell.childNodes[0].id;

					newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;

				break;

				case "button":

					var nod_id = newcell.childNodes[0].id;

					newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;

				break;

				case "checkbox":

				newcell.childNodes[0].checked = false;
				var nod_id_chk = newcell.childNodes[0].id;
				newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
				

				break;

				case "select-one":
				
				newcell.childNodes[0].selectedIndex = 0;
				var nod_id = newcell.childNodes[0].id;
				newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
				break;	
			}
		}
	}
function deleteRowOrder(tableID) 
{
	try 
	{
		var table = document.getElementById(tableID);
		//alert(table);
		var rowCount = table.rows.length;
		//alert(rowCount);
		for(var i=0; i<rowCount; i++) 
			{
				var row = table.rows[i];
				var chkbox = row.cells[0].childNodes[0];
				if(null != chkbox && true == chkbox.checked) 
				{
					if(rowCount <= 1) 
					{
						alert("Cannot delete all the rows.");
						break;
					}
				table.deleteRow(i);
				rowCount--;
				i--;
				}
				
			}
	document.getElementById("hiditemcount").value=parseInt(rowCount)-1;		
	}
	catch(e) 
	{
		alert(e);
	}

}
function hidedata()
{
document.getElementById("lightbox").style.display='none';
}

function opendata(str,id)
{
var store=document.getElementById("store").value
  var sitecode=document.getElementById("site").value;
  var date=document.getElementById("challanDate").value;

	if(document.getElementById("site").value=='0')
	{
		alert("You Must Select The Site First");
		document.getElementById("site").focus();
		return false;
	}
 
 if (str.length == 0)
    {    	
     document.getElementById("lightbox").innerHTML="";
     document.getElementById("lightbox").style.border="0px";
      return;
    }	
	
	

var xmlhttp;
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("lightbox").style.display='block';	
	document.getElementById("lightbox").innerHTML=xmlhttp.responseText;
	
   // document.getElementById("textitem").innerHTML=data[1];
	
    }
  }

   xmlhttp.open("GET","showpopup1.php?site="+str+"&data="+id+"&store="+store+"&site1="+sitecode+"&date="+date,true);

  //xmlhttp.open("GET","showpopup1.php?site="+str+"&data="+id,true);
	xmlhttp.send();


	
	
}
function getItemsList1(id,code,shade,rate,ctr,stock)
{ 
     
	 var ctr1=ctr.split("_");
    var i=ctr1[1]
	var itemcount=parseInt(document.getElementById('hiditemcount').value)+1;
	
	var edit_cnt=document.getElementById('edit_cnt').value;
	if(edit_cnt==''){var counter=1;} else if(edit_cnt>itemcount){var counter=edit_cnt;
	var itemcount=parseFloat(counter)+parseFloat(itemcount);
	} else  {var counter=edit_cnt;}
	  
	
	if(ctr1[1]==undefined)
	{
	//if(document.getElementById('itemdataid').value=='')
	//{
	
	document.getElementById('itemdataid').value = id;
	document.getElementById('itemcode').value = code;
	document.getElementById('descriptions').value = shade;
	document.getElementById('uom').value = rate;
	document.getElementById('stock_qty').value = stock;
	//}
	}
	else
	{
	//if(document.getElementById('itemdataid_'+i).value=='')
	//{		
	document.getElementById('itemdataid_'+i).value = id;
	document.getElementById('itemcode_'+i).value = code;
	document.getElementById('descriptions_'+i).value = shade;
	document.getElementById('uom_'+i).value = rate;
	document.getElementById('stock_qty_'+i).value = stock;
	//}	
	}	
document.getElementById("lightbox").style.display='none';

	//var data= addRow('dataTable')
	
}
function hidedata()
{
document.getElementById("lightbox").style.display='none';
}
 
 function chkstockqty(str,value)
{

	var id=str.split("_");
	var i=id[1];
	if(id[1]==undefined)
	{
		var StockQty=parseFloat(document.getElementById("stock_qty").value);
		var Qty=parseFloat(document.getElementById("qty").value);
		if(Qty>StockQty)
		{
			alert("You Can't Take Grater Ammount Than Available Stock");
			document.getElementById("qty").value='';
			document.getElementById("qty").focus();
			return false;
		}
	}
	else
	{
		var StockQty1=parseFloat(document.getElementById("stock_qty_"+i).value);
		var Qty1=parseFloat(document.getElementById("qty_"+i).value);
		if(Qty1>StockQty1)
		{
			alert("You Can't Take Greater Qty Than Available Stock");
			document.getElementById("qty_"+i).value='';
			document.getElementById("qty_"+i).focus();
			return false;
		}
	}
}
 function caltotal(str)
{

var id=str.split("_");
var i=id[1];

if(id[1]==undefined)
{
var jobvalue1= parseFloat(document.getElementById("qty").value)*parseFloat(document.getElementById("jobrate").value);
document.getElementById("jobvalue").value=jobvalue1.toFixed(2);

}
else
{
var jobvalue1= parseFloat(document.getElementById("qty_"+i).value)*parseFloat(document.getElementById("jobrate_"+i).value);
document.getElementById("jobvalue_"+i).value=jobvalue1.toFixed(2);
}


}
</script>
<style type="text/css">
.body_lightbox {
width:100%;
background-color:#e7e7e7;
position:absolute;
left:280px;
top:95px;
padding-left:5px;
padding-right:5px;
border:#666666 solid 3px;
}
</style>


<div class="body_lightbox" id="lightbox" style="overflow:auto; width:600px; height:250px; display:none;"></div>