<script type="text/javascript" language="javascript">
function valdata()
{
	if(document.getElementById("site").value=='0')
	{
		alert("Please Select a Unit/Plant");
		document.getElementById("site").focus();
		return false;
	}
	if(document.getElementById("client_id").value=='')
	{
		alert("Please Select a Client");
		document.getElementById("client_id").focus();
		return false;
	}
	if(document.getElementById("salesorder_id").value=='')
	{
		alert("Please Select a Sale Order");
		document.getElementById("salesorder_id").focus();
		return false;
	}
}
</script>
<?php
if($_REQUEST['Save']){
	$insert="insert into ppl_planning set 
	site='".$_REQUEST['site']."',
	ref_no='".$_REQUEST['ref_no']."',
	plan_date='".FormatDateForSQL($_REQUEST['date_plan'])."',
	client='".$_REQUEST['client']."',
	order_id='".$_REQUEST['salesorder']."',
	tot_no_days='".$_REQUEST['tot_no_days']."',
	exp_pro_date='".FormatDateForSQL($_REQUEST['exp_pro_date'])."',
	created='".date("Y-m-d")."',
	created_by='".$_SESSION['UserID']."',
	CompanyId='".$_SESSION['ACGCompanyId']."'";
	$query=DB_query($insert,$db);
	
	$lastid=DB_Last_Insert_ID($db,'ppl_planning', 'id');
	
	foreach($_REQUEST['days_chk'] as $k=>$v)
	{
		$rowval=$_REQUEST['days_chk'][$k][0];	
		$insert1="insert into ppl_planning_detail set 
		parent_id='".$lastid."',
		product='".$_REQUEST['product']."',
		uom='".$_REQUEST['uom']."',
		quantity='".$_REQUEST['quantity']."',
		process='".$_REQUEST['process'][$k]."',
		machine='".$_REQUEST['mac'][$rowval]."',
		efficiency='".$_REQUEST['mac_eff'][$rowval]."',
		capacity='".$_REQUEST['mac_cap'][$rowval]."',
		no_days='".$_REQUEST['mac_days'][$rowval]."'";
		$query=DB_query($insert1,$db);
	}
}
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<form action="" method="post" enctype="multipart/form-data">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
	<tr class="row1">
		<td colspan="6"><h3><strong>General Details</strong></h3></td>
	</tr>
	<tr class="row1">
        <td class="tabledata1">Unit/Plant</td>
        <td><select name="site" id="site" style="width:150px;" onchange="getClients(this.value)">
    	<option value="0">Select Unit</option>
     <?php 
		$site_sql = "Select * from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')";
		$site_result = DB_query($site_sql,$db);
		while($site_row = DB_fetch_array($site_result))
		{
		?>
         <option value="<?php echo $site_row['loccode']; ?>" <?php echo ($_REQUEST['site']==$site_row['loccode']?'selected':'')?> ><?php echo $site_row['locationname']; ?></option>
            <?php } ?>
    </select></td>
		<td class="tabledata1" scope="row">Ref. No. </td>
        <td><input type="text" name="ref_no" id="ref_no" style="width:150px;" value="<?php echo $_REQUEST['ref_no']?>"/></td>
		<td class="tabledata1" scope="row">Date</td>
        <td><input type="text" name="date_plan" id="date_plan" style="width:150px;" class="datepicker" value="<?php echo date('d/m/Y');?>" /></td>
	</tr>
	<tr class="row1">
        <td class="tabledata1" scope="row">Client</td>
        <td><select name="client" id="client_id" onchange="getSalesOrders(this.value)">
		<option value="">Select</option>
		<?php if($_REQUEST['site']!=''){
		$query=DB_query("Select distinct(client) from salesorder where branch='".$_REQUEST['site']."'",$db);
		while($row=DB_fetch_array($query))
		{	
			if($_REQUEST['client']==$row['client']){$selected="selected";}else{$selected='';}
			echo '<option value="'.$row['client'].'" '.$selected.'>'.$s->getField('clientmaster','id',$row['client'],'c_name').'</option>';
		}
		 }?>
          </select>
       </td>
		<td class="tabledata1" scope="row" id="abc">Sale Order </td>
        <td><select name="salesorder" id="salesorder_id">
		<option value="">Select</option>	
		<?php if($_REQUEST['client']!=''){
		$query1=DB_query("Select id,orderNo from salesorder where client='".$_REQUEST['client']."'",$db);
		while($row1=DB_fetch_array($query1))
		{
			if($_REQUEST['salesorder']==$row1['id']){$selected="selected";}else{$selected='';}
			echo '<option value="'.$row1['id'].'" '.$selected.'>'.$row1['orderNo'].'</option>';
		}
		}
		?>
        </select></td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
</table>
<p align="center"><input type="submit" name="Plan" value="Show Planning" class="search" onclick="return valdata();" /></p>

<table width="100%" border="0" cellpadding="3" cellspacing="0">
	<tr class="row1">
		<td colspan="6"><h3><strong>Production Plan</strong></h3></td>
	</tr>
</table>
<?php if($_REQUEST['Plan']!=''){?>
<table width="100%" border="1" cellpadding="3" cellspacing="0">
	<tr class="row1">
		<td width="14%"><strong>Product</strong></td>
		<td width="5%"><strong>Unit</strong></td>
		<td width="7%"><strong>Quantity</strong></td>
		<td width="74%" style="text-align:center"><strong>Plan</strong></td>
	</tr>
	<tr class="row1">
		<td width="14%">&nbsp;</td>
		<td width="5%">&nbsp;</td>
		<td width="7%">&nbsp;</td>
		<td width="74%">
			<table width="100%" border="1" cellpadding="3" cellspacing="0">
				<tr>
					<td width="20%"><strong>Process</strong></td>
					<td>
						<table width="100%" border="1" cellpadding="3" cellspacing="0">
							<tr>
								<td width="20%"><strong>Process Machines</strong></td>
								<td width="20%"><strong>Capacity</strong></td>
								<td width="20%"><strong>Efficiency</strong></td>
								<td width="15%"><strong>Shifts</strong></td>
								<td width="20%"><strong>Expected Production Time</strong></td>
								<td width="5%"><strong>Select</strong></td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
		</td>
	</tr>
<?php
$sumday='';
$chrow=1;
$chpro=1;
$query2="select * from salesorder_details where masterId=".$_REQUEST['salesorder'];
$result2=DB_query($query2,$db);
while($row2=DB_fetch_array($result2))
{	extract($row2);
?>
	<tr class="row1">
		<td><?php echo $s->getField('item_master','id',$mst_itemid,'item_name');?>
		<input type="hidden" name="product" value="<?php echo $mst_itemid;?>" style="width:70px" readonly />
		</td>
		<td><?php echo $s->getField('unitsofmeasure','unitid',$s->getField('ppl_product_wise_capacity','mst_itemid',$mst_itemid,'uom'),'unitname');?>
		<input type="hidden" name="uom" value="<?php echo $s->getField('ppl_product_wise_capacity','mst_itemid',$mst_itemid,'uom');?>" style="width:70px" readonly />
		</td>
		<td><?php echo $Quantity;?>
		<input type="hidden" name="quantity" value="<?php echo $Quantity;?>" style="width:70px" readonly />
		</td>
		<td>
			<table width="100%" border="1" cellpadding="3" cellspacing="0">
				<?php $query3=DB_query("Select B.process, B.id from ppl_product_wise_capacity A, ppl_product_wise_capacity_process B where A.id=B.parent_id and A.mst_itemid=".$mst_itemid,$db);
				while($row3=DB_fetch_array($query3))
				{
				?>
				<tr>
					<td width="20%"><?php echo $s->getField('production_process_master','id',$row3['process'],'manuf_process_name');?>&nbsp;<img src="images/red-arrow.jpg" height="20" width="30" />
					<input type="hidden" name="process[<?php echo $chpro;?>]" value="<?php echo $row3['process'];?>" style="width:70px" readonly />
					</td>
					<td>
						<table width="100%" border="1" cellpadding="3" cellspacing="0">
						<?php
						$mcap=0; 
						$query4=DB_query("Select * from ppl_product_wise_capacity_machines where parent_id = ".$row3['id'],$db);
						while($row4=DB_fetch_array($query4))
						{
						if($mcap<=$row4['capacity'])
						{
							$cap=$chrow;
							$mcap=$row4['capacity'];
						}
						?>
							<tr style="height:50">
								<td width="23%">
									<?php echo $s->getField('machine_center','id',$row4['machine'],'name');?>
									<input type="hidden" name="mac[]" id="mac_<?php echo $chrow;?>" value="<?php echo $row4['machine'];?>" style="width:70px" readonly />
								</td>
								<td width="22%"><input type="text" name="mac_cap[]" id="mac_cap_<?php echo $chrow;?>" value="<?php echo $row4['capacity'];?>" style="width:70px" readonly /></td>
								<td width="23%"><input type="text" name="mac_eff[]" id="mac_eff_<?php echo $chrow;?>" value="<?php echo $row4['efficiency'];?>"  style="width:70px" readonly/></td>
								<td width="22%"></td>
								<?php 
								if($row4['capacity']==0){$row4['capacity']=1;}
								$days=floor($Quantity/$row4['capacity']);
								$hrs1=$Quantity%$row4['capacity'];
								$hrs=floor(($hrs1*24)/$row4['capacity']);
								?>
								<td width="25%">
								<input type="hidden" name="mac_days[]" id="mac_days_<?php echo $chrow;?>" value="<?php echo $days.":".$hrs;?>" style="width:70px" readonly />
								<?php 
								if($hrs>0 && $days>0)
								{ 
									echo $days." days and ".$hrs." hrs"; 
								}else if($hrs==0 && $days>0){
									echo $days." days";
								}else if($hrs>0 && $days==0){
									echo $hrs." hrs";
								} ?>
								</td>
								<td>
								
								<input type="radio" name="days_chk[<?php echo $chpro;?>][]" id="days_chk_<?php echo $chrow;?>" value="<?php echo $chrow-1;?>" onclick="sumdays(<?php echo $chrow;?>)" style="width:70px" readonly <?php if($cap==$chrow){ ?> checked="checked"<?php } ?> />
								<?php if($cap==$chrow){ 
								$sumday[$chpro]=($days*24)+$hrs;
								 } ?>
								</td>
							</tr>
							
				  <?php $chrow++;} ?>
				 
						</table>
					</td>	
				</tr>
		  <?php $chpro++;} ?>
			</table>
		</td>
	</tr>
<?php } ?>
	<tr>
		<td>Total No. of days</td>
		<td>
		<?php 
		if(!empty($sumday)){
			$ss=array_sum($sumday);
		}
		$ss1=floor($ss/24);
		$ss2=$ss%24;
		?>
			<input type="hidden" name="tot_no_days" id="tot_no_days" value="<?php echo $ss1.":".$ss2;?>" class="search" readonly="true" />
			<input type="text" name="tot_no_days1" id="tot_no_days1" value="<?php if($ss1>0 && $ss2>0){echo $ss1." days and ".$ss2." hrs";}else if($ss1==0 && $ss2>0){echo $ss2." hrs";}else if($ss1>0 && $ss2==0){echo $ss1." days";}?>" class="search" readonly="true" />
		</td>
		<td>Expected Production Date</td>
		<td>
		<?php $pmdate=date("Y-m-d");?>
		<input type="text" name="exp_pro_date" id="exp_pro_date" value="<?php  echo date("d/m/Y",(strtotime($pmdate)+(3600*$ss)));?>" class="search" readonly="true" />
		<input type="hidden" id="hideitem" value="<?php echo $chrow-1;?>" />
		</td>
	</tr>
</table>
<?php } ?>
<p align="center"><input type="submit" name="Save" value="Save" class="search" /></p>
</form>
</div>
</div>
<script language="javascript" type="text/javascript">
function sumdays(i)
{
	var hitem=document.getElementById("hideitem").value;
	var totd=0;
	var toth=0;
	for(i=1;i<=hitem;i++){
		var tot_days=document.getElementById("tot_no_days").value;
		var ttd='';
		var ttd1='';
		var ttd2='';
		var ttd3='';
		var ttd4='';
		if(document.getElementById("days_chk_"+i).checked==true)
		{
			ttd1=document.getElementById("mac_days_"+i).value;
			ttd=ttd1.split(":");
						
			totd=parseInt(ttd[0])+parseInt(totd);
			toth=parseInt(ttd[1])+parseInt(toth);
		}
	}
	if(parseInt(toth)>24){
		ttd4=Math.floor(toth/24);
		totd=parseInt(totd)+parseInt(ttd4);
		toth=toth%24;
	}
	document.getElementById("tot_no_days").value=totd+":"+toth;
	if(totd>0 && toth>0)
	{
		document.getElementById("tot_no_days1").value=totd+" days and "+toth+" hrs";
	}else if(totd>0 && toth==0)
	{
		document.getElementById("tot_no_days1").value=totd+" days";
	}else if(totd==0 && toth>0)
	{
		document.getElementById("tot_no_days1").value=toth+" hrs";
	}  	
	//else if(document.getElementById("days_chk_"+i).checked==false)
	//{
	//	ttd1=document.getElementById("mac_days_"+i).value;
	//	ttd=ttd1.split(":");
	//	ttd3=tot_days.split(":");
	//	if(parseInt(ttd[1])>parseInt(ttd3[1])){
	//		ttd[1]=(parseInt(ttd3[1])+24)-parseInt(ttd[1]);
	//		ttd3[0]=parseInt(ttd3[0])-1;
	//		ttd[0]=parseInt(ttd3[0])-parseInt(ttd[0]);
	//	}else{
	//		ttd[1]=parseInt(ttd3[1])-parseInt(ttd[1]);
	//		ttd[0]=parseInt(ttd3[0])-parseInt(ttd[0]);
	//	}
	//	document.getElementById("tot_no_days").value=ttd[0]+":"+ttd[1];
	//}
	if(document.getElementById("tot_no_days").value!='')
	{
		var tot_days1=document.getElementById("tot_no_days").value;
		var date_plan=document.getElementById("date_plan").value;
		var date_plan1=date_plan.split("/");
		var dd=date_plan1[0];
		var mm=date_plan1[1];
		var yy=date_plan1[2];
		var datediv=tot_days1.split(":");
		mm=parseInt(mm)-1;
		var theDate = new Date(yy, mm, dd);
		var myNewDate = new Date(theDate);
		
		var duration = datediv[0]; //In Days
		var ndate=myNewDate.setTime(myNewDate.getTime() +  (duration * 24 * 60 * 60 * 1000));
		var ndate1= new Date(ndate);
		var dd = ndate1.getDate();
		var mm = ndate1.getMonth() + 1;
		var yy = ndate1.getFullYear();
		
		
		//var dd=date_plan1[0];
		//var mm=date_plan1[1];
		//var yy=date_plan1[2];
		//var datediv=tot_days1.split(":");
		//dd=parseInt(dd)+parseInt(datediv[0]);
		//if(datediv[0]>0)
		//{
		//	dd=parseInt(dd)+1;
		//}
		/*if(mm=='01' || mm=='03' || mm=='05' || mm=='07' || mm=='08' || mm=='10' || mm=='12'){
			if(parseInt(dd)>31)
			{
				dd=parseInt(dd)%31;
				mm=parseInt(mm)+Math.floor(parseInt(dd)/31);
				if(parseInt(mm)>12)
				{
					mm=parseInt(mm)%12;
					yy=parseInt(yy)+Math.floor(parseInt(mm)/12);
				}
			}
		}else if(mm=='04' || mm=='06' || mm=='09' || mm=='11'){
			if(parseInt(dd)>30)
			{
				dd=parseInt(dd)%30;
				mm=parseInt(mm)+Math.floor(parseInt(dd)/30);
				if(parseInt(mm)>12)
				{
					mm=parseInt(mm)%12;
					yy=parseInt(yy)+Math.floor(parseInt(mm)/12);
				}
			}
		}else if(mm=='02'){
			if((parseInt(yy)%4)==0){
				if(parseInt(dd)>29)
				{
					dd=parseInt(dd)%29;
					mm=parseInt(mm)+Math.floor(parseInt(dd)/29);
					if(parseInt(mm)>12)
					{
						mm=parseInt(mm)%12;
						yy=parseInt(yy)+Math.floor(parseInt(mm)/12);
					}
				}
			}else{
				if(parseInt(dd)>28)
				{
					dd=parseInt(dd)%28;
					mm=parseInt(mm)+Math.floor(parseInt(dd)/28);
					if(parseInt(mm)>12)
					{
						mm=parseInt(mm)%12;
						yy=parseInt(yy)+Math.floor(parseInt(mm)/12);
					}
				}
			}
		}*/
		document.getElementById("exp_pro_date").value=dd+"/"+mm+"/"+yy;
	}
}
function getClients(str)
{
if(str.length==0)
  {
  document.getElementById("client_id").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("client_id").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajx_clients.php?str="+str,true);
xmlhttp.send();
}

function getSalesOrders(str)
{
	if(str.length==0)
  {
  document.getElementById("salesorder_id").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("salesorder_id").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajx_salesorders.php?str="+str,true);
xmlhttp.send();
}
</script>
