<?php
	include "db_connection/db_connection.php";
	include "config/config.php";
	
	if($_SESSION['admin_login_id'] == '')
	{
		echo "<script>window.location='index.php';</script>";
	}
	else // THIS IS FOR GETTING PARTY DET ID ROM LOAGIN DET AND PARTY DET TABLE
	{
		$admin_id = $_SESSION['admin_login_id'];
		
		$SLCT_log = "SELECT * FROM login_details,ac_party_det WHERE ac_party_det.party_det_id = login_details.party_det_id AND login_details.admin_id = '$admin_id'";
		$QURY_log = mysqli_query($connect,$SLCT_log);
		$FTCH_log = mysqli_fetch_object($QURY_log);
		$party_id = '$FTCH_log->party_det_id';
	}
	
	function sum_the_time($tot_time=array()) 
	{
		if(count($tot_time)>0)
		{
			$seconds = 0;
			foreach ($tot_time as $time)
			{
				list($hour,$minute,$second) = explode(':', $time);
				$seconds += $hour*3600;
				$seconds += $minute*60;
				$seconds += $second;
			}
			$hours = floor($seconds/3600);
			$seconds -= $hours*3600;
			$minutes  = floor($seconds/60);
			$seconds -= $minutes*60;
			return "{$hours}:{$minutes}:{$seconds}";
		}
		else
		{
			return;
		}
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<title><?php echo $PROJECT_TITLE ?></title>
		<script type="text/javascript" src="js/design_analize_JS.js"></script>
	</head>
	<body onload="tot_count();">
		<?php
			$flag = 0;
			$sta_iden = '0';
			// THIS IS FOR CHECKING THE STAGE NAME
			if(isset($_GET['sta_name']) && $_GET['sta_name']!='' ) 
			{
				$mas_stage_id = $_GET['sta_name'];
				$mas_stage_id1 =	base64_decode(base64_decode(base64_decode($_GET['sta_name']))); // this is for getting the value from url		
				$SLCTs = "SELECT * FROM mas_stage WHERE mas_stage_id ='$mas_stage_id1'";
				$QURYs = mysqli_query($connect,$SLCTs);
				$FTCHs = mysqli_fetch_object($QURYs);
				if($FTCHs->stage_identity =='1') // this is for checking the finishing stage only
				{
					$sta_iden = $FTCHs->stage_identity;
					$empl_name = $FTCHs->emp_lable;	
					$flag = 1;
				}			
			}
			else
			{
				$mas_stage_id = '';
			}	
			$emp_qur_str = '';
			//THIS IS FOR CHECKING THE EMPLOYEE NAME
			if(isset($_GET['em_name']) && $_GET['em_name']!='' )
			{
				$emp_id_url = $_GET['em_name'];
				$emp_id_dec = base64_decode(base64_decode(base64_decode($_GET['em_name'])));  
				$emp_qur_str = "AND emp_det_id='$emp_id_dec'";
				
				if($emp_id_dec!=0)
				{
					$SLCTse = "SELECT * FROM emp_det WHERE emp_det_id ='$emp_id_dec'";
					$QURYse = mysqli_query($connect,$SLCTse);
					$FTCHse = mysqli_fetch_object($QURYse);
					$emp_nam = $FTCHse->name;
				}
				else
				{
					$emp_nam = 'CAD';
				}									
			}
			else
			{
				$emp_id_url = '';
				$emp_qur_str = '';
				$emp_nam = '';
			}
			if(isset($_GET['fdate']) && $_GET['fdate']!='' ){ $fdate = $_GET['fdate'];	}else{ $fdate = ''; }
			if(isset($_GET['todate']) && $_GET['todate']!='' ){ $todate = $_GET['todate'];	}else{ $todate = ''; }	
			if(isset($_GET['yea_r']) && $_GET['yea_r']!='' ){ $yea_r = 'Year';	}else{ $yea_r = ''; }
			if(isset($_GET['mont_h']) && $_GET['mont_h']!='' ){ $mont_h = 'Month';	}else{ $mont_h = ''; }
			if(isset($_GET['da_y']) && $_GET['da_y']!='' ){ $da_y = 'Day';	}else{ $da_y = ''; }
	
			$flg = $_GET['flg'];
			if(isset($_GET['flgg']) && $_GET['flgg']!='')
			{
				$flgg = $_GET['flgg'];
			}
			else
			{
				$flgg = '';
			}	
		?>
		<table width="960" border="0" align="center" style="outline:#000000 solid 1px;">
			<tr>
				<th colspan="7" scope="col"><?php echo $FTCHs->stage_name; ?> Report</th>
			</tr>
			<tr>
				<td width="187">&nbsp;</td>
				<td width="55">&nbsp;</td>
				<td width="233">&nbsp;</td>
				<td width="88">&nbsp;</td>
				<td width="93">&nbsp;</td>
				<td width="66">&nbsp;</td>
				<td width="203"></td>
			</tr>
			<tr>
				<td>&nbsp;<?php echo $FTCHs->emp_lable; ?></td>
				<td>&nbsp;</td>
				<td>&nbsp;<?php if($FTCHs->emp_lable!=''){if($emp_nam!=''){ echo $emp_nam; }else{ echo "Over All"; }}else{ echo "";} ?></td>
				<?php 
				if($mas_stage_id1 == 3) 
				{ 
				?> 
					<td>Tot. Design :&nbsp;</td>
					<td colspan="2"><input type="text" id="tot_qty" name="tot_qty" value="" readonly="readonly" disabled="disabled" style="width:50px; background:#FFFFFF; font-size:12px; text-align:center; border:none;" /><?php if($flgg!='frnd') { ?> (<input title="No of Pieces" type="text" id="tot_die" name="tot_die" value="" readonly="readonly" disabled="disabled" style="width:50px; background:#FFFFFF; font-size:12px; text-align:center; border:none;" />)<?php } ?></td>
					<td>&nbsp;Tot. Points &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;<input type="text" id="tot_pnts" name="tot_pnts" value="" readonly="readonly" disabled="disabled" style="width:50px; background:#FFFFFF; font-size:12px; text-align:center; border:none;" /></td>
				<?php 
				} 
				if($mas_stage_id1 == 10) 
				{ 
				?> 
					<td colspan="3">&nbsp;</td>
					<td>Tot. Weights :&nbsp;<input type="text" id="tot_wgts" name="tot_wgts" value="" readonly="readonly" disabled="disabled" style="width:50px; background:#FFFFFF; font-size:12px; text-align:center; border:none;" /></td>
				<?php 
				} 
				else
				{
				?>
					<td width="1">&nbsp;</td>
					<td width="1">&nbsp;</td>
					<td width="1">&nbsp;</td>
					<td width="1">&nbsp;</td>
				<?php
				}
				?>   
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
			</tr>
			<tr>
				<td>&nbsp;From Date: <?php echo $fdate; ?></td>
				<td>&nbsp;</td>
				<td>&nbsp;To Date: <?php echo $todate; ?></td>
				<td>&nbsp;Filter:</td>
				<td colspan="2">&nbsp;
					<?php if($yea_r!=''){ echo "Year"; } ?>&nbsp;-
					<?php if($mont_h!=''){ echo "Month"; } ?>&nbsp;-
					<?php if($da_y!=''){ echo "Day"; } ?>&nbsp;
				</td>
				<?php 
				if($mas_stage_id1 == 3) 
				{ 
				?> 
					<td>&nbsp;Tot. Des Time :&nbsp; <input type="text" id="tot_tims" name="tot_tims" value="" readonly="readonly" disabled="disabled" style="width:80px; background:#FFFFFF; font-size:12px; text-align:center; border:none;" /></td>
				<?php 
				} 
				else
				{
				?>
					<td>Tot. Design :&nbsp;<input type="text" id="tot_qty" name="tot_qty" value="" readonly="readonly" disabled="disabled" style="width:50px; background:#FFFFFF; font-size:12px; text-align:center; border:none;" /><?php if($mas_stage_id1>3){ if($flgg!='frnd') { ?> (<input title="No of Pieces" type="text" id="tot_die" name="tot_die" value="" readonly="readonly" disabled="disabled" style="width:50px; background:#FFFFFF; font-size:12px; text-align:center; border:none;" />) <?php } } ?></td>
				<?php
				}
				?>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
			</tr>
		</table>
		<?php
			$design_date = ''; // THIS IS FOR ASIGNING THE COLUMN NAME DYNAMICALLY			
			if(isset($_GET['des_dt']) && $_GET['des_dt']!='')
			{
		 		$des_st = $_GET['des_dt'];
				if($mas_stage_id1==1)
				{ 
					$design_date = "hand_design_date";  
					$dat_qr = ""; 
				}
				else if($mas_stage_id1==2)
				{ 
					$design_date = "loading_date"; 
					$dat_qr = " AND STR_TO_DATE(loading_date,'%d-%m-%Y')=STR_TO_DATE('".$des_st."','%d-%m-%Y')"; 
				}
				else if($mas_stage_id1==3)
				{ 
					$design_date = "finishing_date"; 
					$dat_qr = ""; 
				}
				else if($mas_stage_id1==4)
				{ 
					$design_date = "loading_date"; 
					$dat_qr = " AND STR_TO_DATE(loading_date,'%d-%m-%Y')=STR_TO_DATE('".$des_st."','%d-%m-%Y')"; 
				}
				else if($mas_stage_id1==5)
				{ 
					$design_date = "finishing_date"; 
					$dat_qr = " AND STR_TO_DATE(finishing_date,'%d-%m-%Y')=STR_TO_DATE('".$des_st."','%d-%m-%Y')"; 
				}
				else if($mas_stage_id1==6)
				{ 
					$design_date = "loading_date"; 
					$dat_qr = " AND STR_TO_DATE(loading_date,'%d-%m-%Y')=STR_TO_DATE('".$des_st."','%d-%m-%Y')"; 
				}
				else if($mas_stage_id1==7)
				{ 
					$design_date = "finishing_date"; 
					$dat_qr = ""; 
				}
				else if($mas_stage_id1==8)
				{ 
					$design_date = "finishing_date"; 
					$dat_qr = " AND STR_TO_DATE(finishing_date,'%d-%m-%Y')=STR_TO_DATE('".$des_st."','%d-%m-%Y')"; 
				}
				else if($mas_stage_id1==9)
				{ 
					$design_date = "finishing_date";
					$dat_qr = " AND STR_TO_DATE(finishing_date,'%d-%m-%Y')=STR_TO_DATE('".$des_st."','%d-%m-%Y')";  
				}
				else if($mas_stage_id1==10)
				{ 
					$design_date = "finishing_date"; 
					$dat_qr = ""; 
				}
				else if($mas_stage_id1==11)
				{ 
					$design_date = "finishing_date"; 
					$dat_qr = ""; 
				}
				else if($mas_stage_id1==12)
				{ 
					$design_date = "finishing_date"; 
					$dat_qr = " AND STR_TO_DATE(finishing_date,'%d-%m-%Y')=STR_TO_DATE('".$des_st."','%d-%m-%Y')"; 
				}
				else if($mas_stage_id1==13)
				{
					$design_date = "loading_date"; 
					$dat_qr = ""; 
				}
				else if($mas_stage_id1==14)
				{
					$design_date = "finishing_date"; 
					$dat_qr = ""; 
				}
				else if($mas_stage_id1==15)
				{
					$design_date = "loading_date"; 
					$dat_qr = ""; 
				}
				else if($mas_stage_id1==16)
				{
					$design_date = "loading_date"; 
					$dat_qr = ""; 
				}
			}
			else
			{
				if($mas_stage_id1==1){ $design_date = "hand_design_date";  }else if($mas_stage_id1==2){ $design_date = "loading_date"; }else if($mas_stage_id1==3){ $design_date = "finishing_date"; }else if($mas_stage_id1==4){ $design_date = "loading_date"; }else if($mas_stage_id1==5){ $design_date = "finishing_date"; }else if($mas_stage_id1==6){ $design_date = "loading_date"; }else if($mas_stage_id1==7){ $design_date = "finishing_date"; }else if($mas_stage_id1==8){ $design_date = "finishing_date"; }else if($mas_stage_id1==9){ $design_date = "finishing_date"; }else if($mas_stage_id1==10){ $design_date = "finishing_date"; }else if($mas_stage_id1==11){ $design_date = "finishing_date"; }else if($mas_stage_id1==12){ $design_date = "finishing_date"; }
				else if($mas_stage_id1==13){ $design_date = "loading_date"; }else if($mas_stage_id1==14){ $design_date = "finishing_date"; }else if($mas_stage_id1==15){ $design_date = "loading_date"; }else if($mas_stage_id1==16){ $design_date = "loading_date"; }
				$dat_qr = '';
			}
			$dat_str = ''; //THIS IS FOR QUERY STRING
			$mass_flg = 1;			
			if($fdate!='' && $todate!='')
			{
				if($yea_r!='' && $mont_h!='' && $da_y!=''){ $dat_str = "STR_TO_DATE(".$design_date.",'%d-%m-%Y') BETWEEN STR_TO_DATE('$fdate','%d/%m/%Y') AND STR_TO_DATE('$todate','%d/%m/%Y')"; }
				if($yea_r!='' && $mont_h!='' && $da_y==''){ $dat_str = "MONTH(STR_TO_DATE(".$design_date.",'%d-%m-%Y')) BETWEEN MONTH(STR_TO_DATE('$fdate','%d/%m/%Y')) AND MONTH(STR_TO_DATE('$todate','%d/%m/%Y')) AND YEAR(STR_TO_DATE(".$design_date.",'%d-%m-%Y')) BETWEEN YEAR(STR_TO_DATE('$fdate','%d/%m/%Y')) AND YEAR(STR_TO_DATE('$todate','%d/%m/%Y'))"; }
				if($yea_r!='' && $mont_h=='' && $da_y==''){ $dat_str = "YEAR(STR_TO_DATE(".$design_date.",'%d-%m-%Y')) BETWEEN YEAR(STR_TO_DATE('$fdate','%d/%m/%Y')) AND YEAR(STR_TO_DATE('$todate','%d/%m/%Y'))"; }
				if($yea_r=='' && $mont_h=='' && $da_y==''){ $dat_str = "STR_TO_DATE(".$design_date.",'%d-%m-%Y') BETWEEN STR_TO_DATE('$fdate','%d/%m/%Y') AND STR_TO_DATE('$todate','%d/%m/%Y')"; }
				if($yea_r=='' && $mont_h!='' && $da_y==''){ $dat_str = "MONTH(STR_TO_DATE(".$design_date.",'%d-%m-%Y')) BETWEEN MONTH(STR_TO_DATE('$fdate','%d/%m/%Y')) AND MONTH(STR_TO_DATE('$todate','%d/%m/%Y')) AND YEAR(STR_TO_DATE(".$design_date.",'%d-%m-%Y')) BETWEEN YEAR(STR_TO_DATE('$fdate','%d/%m/%Y')) AND YEAR(STR_TO_DATE('$todate','%d/%m/%Y'))"; }
				if($yea_r=='' && $mont_h=='' && $da_y!=''){ $dat_str = "STR_TO_DATE(".$design_date.",'%d-%m-%Y') BETWEEN STR_TO_DATE('$fdate','%d/%m/%Y') AND STR_TO_DATE('$todate','%d/%m/%Y')"; }
				if($yea_r!='' && $mont_h=='' && $da_y!=''){ $dat_str = "STR_TO_DATE(".$design_date.",'%d-%m-%Y') BETWEEN STR_TO_DATE('$fdate','%d/%m/%Y') AND STR_TO_DATE('$todate','%d/%m/%Y')"; }
				if($yea_r=='' && $mont_h!='' && $da_y!=''){ $dat_str = "STR_TO_DATE(".$design_date.",'%d-%m-%Y') BETWEEN STR_TO_DATE('$fdate','%d/%m/%Y') AND STR_TO_DATE('$todate','%d/%m/%Y')"; }				
			}
			else if($fdate=='' && $todate!='')
			{
				if($yea_r!='' && $mont_h!='' && $da_y!=''){ $dat_str = "STR_TO_DATE(".$design_date.",'%d-%m-%Y') = STR_TO_DATE('$todate','%d/%m/%Y')"; }
				if($yea_r!='' && $mont_h!='' && $da_y==''){ $dat_str = "DATE_FORMAT(STR_TO_DATE(".$design_date.",'%d-%m-%Y'),'%m-%Y') = DATE_FORMAT(STR_TO_DATE('$todate','%d/%m/%Y'),'%m-%Y')"; }
				if($yea_r!='' && $mont_h=='' && $da_y==''){ $dat_str = "YEAR(STR_TO_DATE(".$design_date.",'%d-%m-%Y')) = YEAR(STR_TO_DATE('$todate','%d/%m/%Y'))"; }
				if($yea_r=='' && $mont_h=='' && $da_y==''){ $dat_str = "STR_TO_DATE(".$design_date.",'%d-%m-%Y') <= STR_TO_DATE('$todate','%d/%m/%Y')"; }
				if($yea_r=='' && $mont_h!='' && $da_y==''){ $dat_str = "DATE_FORMAT(STR_TO_DATE(".$design_date.",'%d-%m-%Y'),'%m-%Y') = DATE_FORMAT(STR_TO_DATE('$todate','%d/%m/%Y'),'%m-%Y')"; }
				if($yea_r=='' && $mont_h=='' && $da_y!=''){ $dat_str = "STR_TO_DATE(".$design_date.",'%d-%m-%Y') = STR_TO_DATE('$todate','%d/%m/%Y')"; }
				if($yea_r!='' && $mont_h=='' && $da_y!=''){ $dat_str = "STR_TO_DATE(".$design_date.",'%d-%m-%Y') = STR_TO_DATE('$todate','%d/%m/%Y')"; }
				if($yea_r=='' && $mont_h!='' && $da_y!=''){ $dat_str = "STR_TO_DATE(".$design_date.",'%d-%m-%Y') = STR_TO_DATE('$todate','%d/%m/%Y')"; }
			}
			if($flgg=='')
			{
				if($mas_stage_id1==1)
				{
					$SLCT = "SELECT * FROM design_det WHERE ".$dat_str.$emp_qur_str. " ORDER BY STR_TO_DATE(design_date,'%d-%m-%Y'),`design_det_id` ASC";
					$mass_flg = 0;
				}
				else if($mas_stage_id1==2)
				{
					$SLCT = "SELECT * FROM cad_loading WHERE ".$dat_str.$dat_qr.$emp_qur_str." ORDER BY STR_TO_DATE(loading_date,'%d-%m-%Y'),design_det_id ASC";
					$mass_flg = 0;
				}
				else if($mas_stage_id1==3)
				{
					$SLCT = "SELECT * FROM cad_finishing_des,cad_finishing WHERE cad_finishing_des.cad_finishing_id IN(SELECT cad_finishing_id FROM cad_finishing WHERE ".$dat_str.$emp_qur_str.") AND cad_finishing.cad_finishing_id = cad_finishing_des.cad_finishing_id ORDER BY STR_TO_DATE(finishing_date,'%d-%m-%Y'),design_det_id ASC";
					$SLCT_tim = "SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(`cad_time`))) AS tm_to FROM cad_finishing_des,cad_finishing WHERE cad_finishing_des.cad_finishing_id IN(SELECT cad_finishing_id FROM cad_finishing WHERE ".$dat_str.$emp_qur_str.") AND cad_finishing.cad_finishing_id = cad_finishing_des.cad_finishing_id ORDER BY STR_TO_DATE(finishing_date,'%d-%m-%Y'),design_det_id ASC";
					$QURY_tim = mysqli_query($connect,$SLCT_tim);
					$FTCH_tim = mysqli_fetch_object($QURY_tim);
					$tot_tm = $FTCH_tim->tm_to;
				}
				else if($mas_stage_id1==4)
				{
					$SLCT = "SELECT * FROM cam_loading WHERE ".$dat_str.$dat_qr." ORDER BY STR_TO_DATE(loading_date,'%d-%m-%Y'),design_det_id ASC";
				}
				else if($mas_stage_id1==5)
				{
					$SLCT = "SELECT * FROM cam_finishing WHERE ".$dat_str.$dat_qr." ORDER BY STR_TO_DATE(finishing_date,'%d-%m-%Y'),design_det_id ASC";
					$weigh_t = 'Cam Weight';
				}
				else if($mas_stage_id1==6)
				{
					$SLCT = "SELECT * FROM rtv_loading WHERE ".$dat_str.$dat_qr." ORDER BY STR_TO_DATE(loading_date,'%d-%m-%Y'),design_det_id ASC";
				}
				else if($mas_stage_id1==7)
				{
					$SLCT = "SELECT * FROM rtv_finishing_des,rtv_finishing WHERE rtv_finishing_des.rtv_finishing_id IN(SELECT rtv_finishing_id FROM rtv_finishing WHERE ".$dat_str.$emp_qur_str.") AND rtv_finishing.rtv_finishing_id = rtv_finishing_des.rtv_finishing_id ORDER BY STR_TO_DATE(finishing_date,'%d-%m-%Y'),design_det_id ASC";
				}
				else if($mas_stage_id1==8)
				{
					$SLCT = "SELECT * FROM wax_finishing WHERE ".$dat_str.$dat_qr." ORDER BY STR_TO_DATE(finishing_date,'%d-%m-%Y'),design_det_id ASC";
				}
				else if($mas_stage_id1==9)
				{
					$SLCT = "SELECT * FROM casting_finishing WHERE ".$dat_str.$dat_qr." ORDER BY STR_TO_DATE(finishing_date,'%d-%m-%Y'),design_det_id ASC";
				}
				else if($mas_stage_id1==10)
				{
					$SLCT = "SELECT * FROM sol_finishing_des,sol_finishing WHERE sol_finishing_des.sol_finishing_id IN(SELECT sol_finishing_id FROM sol_finishing WHERE ".$dat_str.$emp_qur_str.") AND sol_finishing.sol_finishing_id = sol_finishing_des.sol_finishing_id ORDER BY STR_TO_DATE(finishing_date,'%d-%m-%Y'),design_det_id ASC";
					$mass_flg = 2;
					$weigh_t = 'Silver Weight';
					$flgg = 0;
				}
				else if($mas_stage_id1==11)
				{
					$SLCT = "SELECT * FROM buf_finishing_des,buf_finishing WHERE buf_finishing_des.buf_finishing_id IN(SELECT buf_finishing_id FROM buf_finishing WHERE ".$dat_str.$emp_qur_str.") AND buf_finishing.buf_finishing_id = buf_finishing_des.buf_finishing_id ORDER BY STR_TO_DATE(finishing_date,'%d-%m-%Y'),design_det_id ASC";
					$mass_flg = 2;
				}
				else if($mas_stage_id1==12)
				{
					$SLCT = "SELECT * FROM mas_finishing WHERE ".$dat_str.$dat_qr." ORDER BY STR_TO_DATE(finishing_date,'%d-%m-%Y'),design_det_id ASC";
					$weigh_t = 'Master Weight';
					$mass_flg = 2;
				}
				else if($mas_stage_id1==13)
				{
					$SLCT = "SELECT * FROM silver_loading WHERE ".$dat_str.$dat_qr." ORDER BY STR_TO_DATE(loading_date,'%d-%m-%Y') ASC";
					$flgg = 0;
				}
				else if($mas_stage_id1==14)
				{
					$SLCT = "SELECT * FROM silver_finishing WHERE ".$dat_str.$dat_qr." ORDER BY STR_TO_DATE(finishing_date,'%d-%m-%Y') ASC";
					$weigh_t = 'Silver Weight';
					$flgg = 0;
				}
				else if($mas_stage_id1==15)
				{
					$SLCT = "SELECT * FROM soldering_loading WHERE ".$dat_str.$dat_qr.$emp_qur_str." ORDER BY STR_TO_DATE(loading_date,'%d-%m-%Y') ASC";
					$weigh_t = 'Solder Weight';
					$flgg = 1;
				}
				else if($mas_stage_id1==16)
				{
					$SLCT = "SELECT * FROM buf_loading WHERE ".$dat_str.$dat_qr." ORDER BY STR_TO_DATE(loading_date,'%d-%m-%Y') ASC";
					$weigh_t = 'Buffing Weight';
					$flgg = 0;
					$mass_flg = 2;
				}
		?>
				<table width="962" border="1" align="center" cellpadding="0" cellspacing="0" >
					<thead>
						<tr>
							<th width="39">S.No<span class="sorting" style="display: block;"></span></th>
							<th width="94">Design No</th>
							<?php 
							if($mass_flg == 1) 
							{
							?>
								<th width="100">No of Pieces</th>
							<?php
							}
							else if($mass_flg == 2) 
							{
							?>
								<th width="100">No of Masters</th>
							<?php
							}
							if($mas_stage_id1 == 3) 
							{ 
								if($flag==1)
								{ 
							?>
									<th width="160"><?php echo $empl_name; ?></th>
							<?php 
								}
							?>
								<th width="80" style="text-transform:capitalize">Points</th>
								<th width="100" style="text-transform:capitalize"><?php echo  str_replace("_", " ",$design_date); ?> </th>
								<th width="100" style="text-transform:capitalize">Design Time</th>
							<?php 
							}
							else
							{
							?>
								<th width="81" style="text-transform:capitalize"><?php echo  str_replace("_", " ",$design_date); ?> </th>
								<?php if($mas_stage_id1==2){ ?><th width="136">Designer Name</th><?php } ?> 
								<?php if($mas_stage_id1==5 || $mas_stage_id1==10 || $mas_stage_id1==12 || $mas_stage_id1==14 || $mas_stage_id1==15 || $mas_stage_id1==16){ ?><th width="136"><?php echo $weigh_t; ?></th><?php } ?> 
								<?php if($flgg==1){ ?><th width="181"><?php echo $empl_name; ?></th><?php } ?>
								<th width="138">Design Status</th>
							<?php 
							}
							?>
							<?php if($flg=="wimg"){ ?><th width="118">Image</th><?php } ?>
							<th width="120">Remarks</th>
						</tr>
					</thead>
					<tbody>
					<?php 
						$QURY = mysqli_query($connect,$SLCT);
						$loop=1;
						if(mysqli_num_rows($QURY)>0)
						{	
							$pnts = 0;	
							$tot_pic=0;	
							$wgts=0;
							while($FTCH = mysqli_fetch_object($QURY))
							{
								$design_id	=	base64_encode(base64_encode(base64_encode($FTCH->design_det_id)));
								$SLCT_dm = "SELECT  * FROM design_det WHERE design_det_id = '$FTCH->design_det_id'";
								$QURY_dm = mysqli_query($connect,$SLCT_dm);
								$FTCH_dm = mysqli_fetch_object($QURY_dm);
								if($mass_flg == 1) 
								{
									$mas_pic = $FTCH_dm->no_of_pieces;
								}
								else  if($mass_flg == 2) 
								{
									$mas_pic = $FTCH_dm->master_pieces;
								}
								$SLCT_d = "SELECT  * FROM master_cat WHERE master_cat_id = '$FTCH_dm->master_cat_id'";
								$QURY_d = mysqli_query($connect,$SLCT_d);
								$FTCH_d = mysqli_fetch_object($QURY_d);
								if($mas_stage_id1 == 3) 
								{
									$SLCT_ds = "SELECT  * FROM master_cad_cat WHERE master_cad_cat_id = '$FTCH_dm->master_cad_cat_id'";
									$QURY_ds = mysqli_query($connect,$SLCT_ds);
									$FTCH_ds = mysqli_fetch_object($QURY_ds);
									$pnts = $pnts+$FTCH_ds->cad_points;
									
									$SLCT_CUR = "SELECT start_dt FROM emp_current_design WHERE emp_det_id = '$FTCH->emp_det_id' AND design_det_id = '$FTCH->design_det_id'";
									$QURY_CUR = mysqli_query($connect,$SLCT_CUR);
									$FTCH_CUR = mysqli_fetch_object($QURY_CUR);
									
									$cat_hrs="";
									$SLCT_cd1 = "SELECT fm_date,to_date,cat_hrs FROM emp_cad_det WHERE master_cad_cat_id='$FTCH_dm->master_cad_cat_id' AND emp_det_id = '$FTCH->emp_det_id'"; 
									$QURY_cd1 = mysqli_query($connect,$SLCT_cd1);
									$num_rows=mysqli_num_rows($QURY_cd1); 
									if($num_rows>0)
									{
										while($FTCH_cd1= mysqli_fetch_object($QURY_cd1))
										{																							
											if($FTCH_cd1->to_date!="")
											{
												$fm_date = strtotime($FTCH_cd1->fm_date);
												$to_date = strtotime($FTCH_cd1->to_date);
												$po_date = strtotime($FTCH_CUR->start_dt);
												
												if(($fm_date <= $po_date) && ($po_date <= $to_date))
												{
													$cat_hrs = $FTCH_cd1->cat_hrs;
												}
												else
												{
													$cat_hrs = $FTCH_ds->cat_hrs;
												}
											}
											else
											{
												if($FTCH_cd1->fm_date!="")
												{
													$cur_date1 = date("d-m-Y", strtotime($cur_date));
													$fm_date = strtotime($FTCH_cd1->fm_date);
													$po_date = strtotime($FTCH_CUR->start_dt);
													$toddate = strtotime($cur_date1);
													if(($fm_date <= $po_date) && ($po_date <= $toddate))
													{											
														$cat_hrs = $FTCH_cd1->cat_hrs;
													}
													else
													{
														$cat_hrs = $FTCH_ds->cat_hrs;
													}
												}
												else
												{									
													$cat_hrs = $FTCH_ds->cat_hrs;
												}
											}
										}
									}
									else
									{
										$cat_hrs = $FTCH_ds->cat_hrs;
									}
								}
								else
								{
									$pnts = '';
								}
								if($mas_stage_id1==1){ $dat_e = $FTCH->design_date; }else if($mas_stage_id1==2){ $dat_e = $FTCH->loading_date;  }else if($mas_stage_id1==3){ $dat_e = $FTCH->finishing_date;  }else if($mas_stage_id1==4){ $dat_e = $FTCH->loading_date; }else if($mas_stage_id1==5){ $dat_e = $FTCH->finishing_date; $we_ight = $FTCH->cam_weight;}else if($mas_stage_id1==6){ $dat_e = $FTCH->loading_date; }else if($mas_stage_id1==7){ $dat_e = $FTCH->finishing_date; }else if($mas_stage_id1==8){ $dat_e = $FTCH->finishing_date; }else if($mas_stage_id1==9){ $dat_e = $FTCH->finishing_date; }else if($mas_stage_id1==10){ $dat_e = $FTCH->finishing_date; $we_ight = $FTCH->sil_weight; }else if($mas_stage_id1==11){ $dat_e = $FTCH->finishing_date; }else if($mas_stage_id1==12){ $dat_e = $FTCH->finishing_date; $we_ight = $FTCH->mas_weight;}
								else if($mas_stage_id1==13){ $dat_e = $FTCH->loading_date;}else if($mas_stage_id1==14){ $dat_e = $FTCH->finishing_date; $we_ight = $FTCH->silver_weight;}else if($mas_stage_id1==15){ $dat_e = $FTCH->loading_date; $we_ight = $FTCH->solder_weight;}else if($mas_stage_id1==16){ $dat_e = $FTCH->loading_date; $we_ight = $FTCH->buf_weight;}	
								if($flag==1)
								{
									$SLCT_e = "SELECT * FROM emp_det WHERE emp_det_id = '$FTCH->emp_det_id'";
									$QURY_e = mysqli_query($connect,$SLCT_e);
									$FTCH_e = mysqli_fetch_object($QURY_e);
								}
								if($mas_stage_id1==2)
								{
									if($FTCH->emp_det_id!=0)
									{
										$SLCT_e = "SELECT * FROM emp_det WHERE emp_det_id = '$FTCH->emp_det_id'";
										$QURY_e = mysqli_query($connect,$SLCT_e);
										$FTCH_e = mysqli_fetch_object($QURY_e);
										$emp_nm = $FTCH_e->name;
									}
									else
									{
										$emp_nm = 'CAD';
									}
								}
								if($loop%2 != "0")
									$bg = "gradeX";
								else
									$bg = "class='gradeC'";
									
								if($FTCH_dm->mas_fin_ext!=''){ $imag = "../mas_fin_images/S.".$FTCH_dm->mas_fin_ext; $imag1 = "../mas_fin_images/L.".$FTCH_dm->mas_fin_ext; }
								else if($FTCH_dm->cad_fin_ext!=''){ $imag = "../cad_fin_images/S.".$FTCH_dm->cad_fin_ext; $imag1 = "../cad_fin_images/L.".$FTCH_dm->cad_fin_ext; }
								else if($FTCH_dm->imag_ext!=''){ $imag = "../design_images/S.".$FTCH_dm->imag_ext; $imag1 = "../design_images/L.".$FTCH_dm->imag_ext; }

								if($mas_stage_id1 == 3) 
								{
									if(strtotime($FTCH->cad_time) > strtotime($cat_hrs))
									{
										$fnt_wt = "bold";
									}
									else
									{
										$fnt_wt = "normal";
									}
								}
								else
								{
									$fnt_wt = "normal";
								}

					?>
								<tr <?php echo $bg ?> style="font-weight:<?php echo $fnt_wt; ?>">
									<td align="center" style="display:table-cell;vertical-align:middle">
										<?php echo $loop ?>
										<input name="mas_stg_id" id="mas_stg_id" type="hidden" value="<?php echo $mas_stage_id1.",".$mass_flg; ?>" />
									</td>
									<td align="center" style="display:table-cell;vertical-align:middle"><?php echo $FTCH_d->master_cat_name." ".$FTCH_dm->design_no; ?><?php if($FTCH_dm->up_status=="yes"){ echo " ( Old ".$FTCH_dm->no_of_tm_alt." )"; } ?></td>
									<?php 
										if($mass_flg!=0)
										{
									?>
											<td align="center" style="display:table-cell;vertical-align:middle"><?php echo $mas_pic; ?></td>
									<?php
											$tot_pic = $tot_pic+$mas_pic;						
										}
										if($mas_stage_id1 == 3) 
										{
											if($FTCH->cad_time != '' || $FTCH->cad_time != 0)
											{
												$cat_tm = $FTCH->cad_time." / ".$cat_hrs;
											}
											else
											{
												$cat_tm = "00:00 / ".$cat_hrs;
											}
											if($flag==1)
											{ 
									?> 	
												<td align="center" style="display:table-cell;vertical-align:middle"><?php echo $FTCH_e->name; ?></td>
									<?php 
											} 
									?>            
											<td align="center" style="display:table-cell;vertical-align:middle"><?php echo $FTCH_ds->cad_points;;?></td>
											<td align="center" style="display:table-cell;vertical-align:middle"><?php echo str_replace("-", "/",$dat_e);?></td>
											<td align="center" style="display:table-cell;vertical-align:middle"><?php echo $cat_tm; ?></td>
									<?php
										}
										else
										{
									?>
											<td align="center" style="display:table-cell;vertical-align:middle"><?php echo str_replace("-", "/",$dat_e); ?></td>
											<?php if($mas_stage_id1==2){ ?> <td align="center" style="display:table-cell;vertical-align:middle"><?php echo $emp_nm; ?></td><?php } ?>
											<?php if($mas_stage_id1==5 || $mas_stage_id1==10 || $mas_stage_id1==12 || $mas_stage_id1==14 || $mas_stage_id1==15 || $mas_stage_id1==16){ ?><td align="center" style="display:table-cell;vertical-align:middle"><?php echo number_format($we_ight,2)." g"; $wgts=$wgts+$we_ight; ?></td><?php } ?>
											<?php if($flgg==1){ ?> <td align="center" style="display:table-cell;vertical-align:middle"><?php echo $FTCH_e->name; ?></td><?php } ?>
											<td align="center" style="display:table-cell;vertical-align:middle"><?php echo $FTCH_dm->design_status; ?></td>
									<?php
										}
										if($flg=="wimg")
										{ 
									?>
											<td class="center">
												<div class="body" align="center">
													<a href="<?php echo $imag1; ?>" title="<h2><?php echo $FTCH_d->master_cat_name." ".$FTCH_dm->design_no; ?></h2>" class="lightbox" rel="group"><img src="<?php echo $imag; ?>" width="60" height="60"/></a>                
												</div>             
											</td>
									<?php 
										} 
									?>
									<td class="center">&nbsp;</td>
								</tr>
					<?php
								$loop ++;
							}
						}
					?>
					</tbody>
            		<?php 
						if($mass_flg!=0)
						{
					?>
							<input name="mas_die" id="mas_die" type="hidden" value="<?php echo $tot_pic; ?>" />
					<?php
						}
					?>
					<input name="tot_cnt" id="tot_cnt" type="hidden" value="<?php echo $loop-1; ?>" />	
		            <input name="tot_pn_cnt" id="tot_pn_cnt" type="hidden" value="<?php echo $pnts; ?>" />
            		<?php if(isset($mas_stage_id1) && $mas_stage_id1 == 10) { ?>
    				<input name="tot_wgt_cnt" id="tot_wgt_cnt" type="hidden" value="<?php echo number_format($wgts,2)." g"; ?>" />
				    <?php } if($mas_stage_id1 == 3) { ?><input name="tot_tm_cnt" id="tot_tm_cnt" type="hidden" value="<?php echo $tot_tm; ?>" /><?php } ?>
				</table>
		<?php
			}
			else if($flgg=='frnd')
			{	
				if($mas_stage_id1==1)
				{
					$SLCT = "SELECT *,COUNT(*) AS des_cnts FROM design_det WHERE ".$dat_str." GROUP BY emp_det_id ORDER BY STR_TO_DATE(design_date,'%d-%m-%Y'),`design_det_id` ASC";
					$flgg = 1;
				}
				else if($mas_stage_id1==2)
				{
					$SLCT = "SELECT *, COUNT(*) AS des_cnts FROM cad_loading WHERE ".$dat_str." GROUP BY STR_TO_DATE(loading_date,'%d-%m-%Y'),emp_det_id ORDER BY STR_TO_DATE(loading_date,'%d-%m-%Y') ASC";
					$flgg = 0;
				}
				else if($mas_stage_id1==3)
				{
					$SLCT = "SELECT *,GROUP_CONCAT(design_det_id) AS des_id, COUNT(*) AS des_cnts FROM cad_finishing_des,cad_finishing WHERE cad_finishing_des.cad_finishing_id IN(SELECT cad_finishing_id FROM cad_finishing WHERE ".$dat_str.") AND cad_finishing.cad_finishing_id = cad_finishing_des.cad_finishing_id GROUP BY emp_det_id ORDER BY STR_TO_DATE(finishing_date,'%d-%m-%Y'),design_det_id ASC";
					$flgg = 1;
				}
				else if($mas_stage_id1==4)
				{
					$SLCT = "SELECT *, COUNT(*) AS des_cnts FROM cam_loading WHERE ".$dat_str." GROUP BY STR_TO_DATE(loading_date,'%d-%m-%Y') ORDER BY STR_TO_DATE(loading_date,'%d-%m-%Y') ASC";
					$flgg = 0;
				}
				else if($mas_stage_id1==5)
				{
					$SLCT = "SELECT *, COUNT(*) AS des_cnts,SUM(cam_weight) AS cam_weight FROM cam_finishing WHERE ".$dat_str." GROUP BY STR_TO_DATE(finishing_date,'%d-%m-%Y') ORDER BY STR_TO_DATE(finishing_date,'%d-%m-%Y') ASC";
					$weigh_t = 'Cam Weight';
					$flgg = 0;
				}
				else if($mas_stage_id1==6)
				{
					$SLCT = "SELECT *, COUNT(*) AS des_cnts FROM rtv_loading WHERE ".$dat_str." GROUP BY STR_TO_DATE(loading_date,'%d-%m-%Y') ORDER BY STR_TO_DATE(loading_date,'%d-%m-%Y') ASC";
					$flgg = 0;
				}
				else if($mas_stage_id1==7)
				{
					$SLCT = "SELECT *,COUNT(*) AS des_cnts FROM rtv_finishing_des,rtv_finishing WHERE rtv_finishing_des.rtv_finishing_id IN(SELECT rtv_finishing_id FROM rtv_finishing WHERE ".$dat_str.") AND rtv_finishing.rtv_finishing_id = rtv_finishing_des.rtv_finishing_id GROUP BY emp_det_id ORDER BY STR_TO_DATE(finishing_date,'%d-%m-%Y'),design_det_id ASC";
					$flgg = 1;
				}
				else if($mas_stage_id1==8)
				{
					$SLCT = "SELECT *, COUNT(*) AS des_cnts FROM wax_finishing WHERE ".$dat_str." GROUP BY STR_TO_DATE(finishing_date,'%d-%m-%Y') ORDER BY STR_TO_DATE(finishing_date,'%d-%m-%Y') ASC";
					$flgg = 0;
				}
				else if($mas_stage_id1==9)
				{
					$SLCT = "SELECT *, COUNT(*) AS des_cnts FROM casting_finishing WHERE ".$dat_str." GROUP BY STR_TO_DATE(finishing_date,'%d-%m-%Y') ORDER BY STR_TO_DATE(finishing_date,'%d-%m-%Y') ASC";
					$flgg = 0;
				}				
				else if($mas_stage_id1==10)
				{
					$SLCT = "SELECT *,COUNT(*) AS des_cnts, SUM(sil_weight) AS sil_weight FROM sol_finishing_des,sol_finishing WHERE sol_finishing_des.sol_finishing_id IN(SELECT sol_finishing_id FROM sol_finishing WHERE ".$dat_str.") AND sol_finishing.sol_finishing_id = sol_finishing_des.sol_finishing_id GROUP BY emp_det_id ORDER BY STR_TO_DATE(finishing_date,'%d-%m-%Y'),design_det_id ASC";
					$flgg = 1;
					$weigh_t = 'Silver Weight';
				}
				else if($mas_stage_id1==11)
				{
					$SLCT = "SELECT *,COUNT(*) AS des_cnts FROM buf_finishing_des,buf_finishing WHERE buf_finishing_des.buf_finishing_id IN(SELECT buf_finishing_id FROM buf_finishing WHERE ".$dat_str.") AND buf_finishing.buf_finishing_id = buf_finishing_des.buf_finishing_id GROUP BY emp_det_id ORDER BY STR_TO_DATE(finishing_date,'%d-%m-%Y'),design_det_id ASC";
					$flgg = 1;
				}
				else if($mas_stage_id1==12)
				{
					$SLCT = "SELECT *, COUNT(*) AS des_cnts, SUM(mas_weight) AS mas_weight FROM mas_finishing WHERE ".$dat_str." GROUP BY STR_TO_DATE(finishing_date,'%d-%m-%Y') ORDER BY STR_TO_DATE(finishing_date,'%d-%m-%Y') ASC";
					$weigh_t = 'Master Weight';
					$flgg = 0;
				}
				else if($mas_stage_id1==13)
				{
					$SLCT = "SELECT *, COUNT(*) AS des_cnts FROM silver_loading WHERE ".$dat_str." GROUP BY STR_TO_DATE(loading_date,'%d-%m-%Y') ORDER BY STR_TO_DATE(loading_date,'%d-%m-%Y') ASC";
					$flgg = 0;
				}
				else if($mas_stage_id1==14)
				{
					$SLCT = "SELECT *, COUNT(*) AS des_cnts , SUM(silver_weight) AS silver_weight FROM silver_finishing WHERE ".$dat_str." GROUP BY STR_TO_DATE(finishing_date,'%d-%m-%Y') ORDER BY STR_TO_DATE(finishing_date,'%d-%m-%Y') ASC";
					$weigh_t = 'Silver Weight';
					$flgg = 0;
				}
				else if($mas_stage_id1==15)
				{
					$SLCT = "SELECT *, COUNT(*) AS des_cnts , SUM(solder_weight) AS solder_weight FROM soldering_loading WHERE ".$dat_str." GROUP BY STR_TO_DATE(loading_date,'%d-%m-%Y') ORDER BY STR_TO_DATE(loading_date,'%d-%m-%Y') ASC";
					$weigh_t = 'Solder Weight';
					$flgg = 1;
				}
				else if($mas_stage_id1==16)
				{
					$SLCT = "SELECT *, COUNT(*) AS des_cnts, SUM(buf_weight) AS buf_weight FROM buf_loading WHERE ".$dat_str." GROUP BY STR_TO_DATE(loading_date,'%d-%m-%Y') ORDER BY STR_TO_DATE(loading_date,'%d-%m-%Y') ASC";
					$weigh_t = 'Buffing Weight';
					$flgg = 0;
				}							
			?>
				<table width="962" border="1" align="center" cellpadding="0" cellspacing="0" >
					<thead>
						<tr>  
							<th width="26">S.No</th>
							<?php
								if($flgg==0)
								{
							?>                                                
									<th width="92" style="text-transform:capitalize"><?php echo  str_replace("_", " ",$design_date); ?> </th>
									<?php if($mas_stage_id1==2){ ?><th width="134">Designer Name</th><?php } ?>
									<th width="92" style="text-transform:capitalize">Total Design</th>
									<?php if($mas_stage_id1==5 || $mas_stage_id1==12){ ?><th width="134"><?php echo "Total ".$weigh_t; ?></th><?php } ?>
							<?php
								}
								else
								{
							
									if($mas_stage_id1!=10 || ($mas_stage_id1==10 && $emp_nam==''))
									{ 
							?>
										<th width="134"><?php echo $empl_name; ?></th>
							<?php
									}
							?>
									<th width="92" style="text-transform:capitalize">Total Design</th> 
							<?php 
									if($mas_stage_id1 == 3) 
									{                            
							?>    
										<th width="92" style="text-transform:capitalize">Total Points</th>
										<th width="151">Total Design Time</th>                   
							<?php 
									}
									if($mas_stage_id1==10){ ?><th width="134"><?php echo "Total ".$weigh_t; ?></th><?php } 								}
							?>
							<th width="120">Remarks</th>
						</tr>
					</thead>
					<tbody>
					<?php 
						$QURY = mysqli_query($connect,$SLCT);
						$loop=1;
						$pnts = 0;
						$pns = 0;
						$cts=0;
						$wgts=0;
						if(mysqli_num_rows($QURY)>0)
						{			
							while($FTCH = mysqli_fetch_object($QURY))
							{
								$design_id	=	base64_encode(base64_encode(base64_encode($FTCH->design_det_id)));				
								if($mas_stage_id1 == 3) 
								{
									$SLCT_ds = "SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(`cat_hrs`))) AS cat_hrs FROM design_det,master_cad_cat WHERE design_det.design_det_id IN($FTCH->des_id) AND master_cad_cat.master_cad_cat_id = design_det.master_cad_cat_id";
									$QURY_ds = mysqli_query($connect,$SLCT_ds);
									$FTCH_ds = mysqli_fetch_object($QURY_ds);
									
									$cat_hrs = "00:00:00";
									$SLCT_dm = "SELECT design_det_id,master_cad_cat_id FROM design_det WHERE design_det_id IN($FTCH->des_id)";
									$QURY_dm = mysqli_query($connect,$SLCT_dm);
									while($FTCH_dm = mysqli_fetch_object($QURY_dm))
									{									
										$SLCT_CUR = "SELECT start_dt FROM emp_current_design WHERE emp_det_id = '$FTCH->emp_det_id' AND design_det_id = '$FTCH_dm->design_det_id'";
										$QURY_CUR = mysqli_query($connect,$SLCT_CUR);
										$FTCH_CUR = mysqli_fetch_object($QURY_CUR);
										
										$SLCT_cd1 = "SELECT fm_date,to_date,cat_hrs FROM emp_cad_det WHERE master_cad_cat_id='$FTCH_dm->master_cad_cat_id' AND emp_det_id = '$FTCH->emp_det_id'"; 
										$QURY_cd1 = mysqli_query($connect,$SLCT_cd1);
										$num_rows=mysqli_num_rows($QURY_cd1); 
										if($num_rows>0)
										{
											while($FTCH_cd1= mysqli_fetch_object($QURY_cd1))
											{																							
												if($FTCH_cd1->to_date!="")
												{
													$fm_date = strtotime($FTCH_cd1->fm_date);
													$to_date = strtotime($FTCH_cd1->to_date);
													$po_date = strtotime($FTCH_CUR->start_dt);
													
													if(($fm_date <= $po_date) && ($po_date <= $to_date))
													{
														$cat_hrs = time_addition($cat_hrs,$FTCH_cd1->cat_hrs);
													}
													else
													{
														$cat_hrs = $FTCH_ds->cat_hrs;
													}
												}
												else
												{
													if($FTCH_cd1->fm_date!="")
													{
														$cur_date1 = date("d-m-Y", strtotime($cur_date));
														$fm_date = strtotime($FTCH_cd1->fm_date);
														$po_date = strtotime($FTCH_CUR->start_dt);
														$toddate = strtotime($cur_date1);
														if(($fm_date <= $po_date) && ($po_date <= $toddate))
														{	
															$cat_hrs = time_addition($cat_hrs,$FTCH_cd1->cat_hrs);	
														}
														else
														{
															$cat_hrs = $FTCH_ds->cat_hrs;
														}
													}
													else
													{				
														$cat_hrs = $FTCH_ds->cat_hrs;	
													}
												}
											}
										}
										else
										{
											$cat_hrs = $FTCH_ds->cat_hrs;	
										}
									}
								}
								else
								{
									$SLCT_dm = "SELECT  * FROM design_det WHERE design_det_id = '$FTCH->design_det_id'";
									$QURY_dm = mysqli_query($connect,$SLCT_dm);
									$FTCH_dm = mysqli_fetch_object($QURY_dm);								
									$SLCT_d = "SELECT  * FROM master_cat WHERE master_cat_id = '$FTCH_dm->master_cat_id'";
									$QURY_d = mysqli_query($connect,$SLCT_d);
									$FTCH_d = mysqli_fetch_object($QURY_d);
								}
								if($mas_stage_id1==1){ $dat_e = $FTCH->design_date; }else if($mas_stage_id1==2){ $dat_e = $FTCH->loading_date;  }else if($mas_stage_id1==3){ $dat_e = $FTCH->finishing_date;  }else if($mas_stage_id1==4){ $dat_e = $FTCH->loading_date; }else if($mas_stage_id1==5){ $dat_e = $FTCH->finishing_date; $we_ight = $FTCH->cam_weight;}else if($mas_stage_id1==6){ $dat_e = $FTCH->loading_date; }else if($mas_stage_id1==7){ $dat_e = $FTCH->finishing_date; }else if($mas_stage_id1==8){ $dat_e = $FTCH->finishing_date; }else if($mas_stage_id1==9){ $dat_e = $FTCH->finishing_date; }else if($mas_stage_id1==10){ $dat_e = $FTCH->finishing_date; $we_ight = $FTCH->sil_weight; }else if($mas_stage_id1==11){ $dat_e = $FTCH->finishing_date; }else if($mas_stage_id1==12){ $dat_e = $FTCH->finishing_date; $we_ight = $FTCH->mas_weight;}
								else if($mas_stage_id1==13){ $dat_e = $FTCH->loading_date;}else if($mas_stage_id1==14){ $dat_e = $FTCH->finishing_date; $we_ight = $FTCH->silver_weight;}else if($mas_stage_id1==15){ $dat_e = $FTCH->loading_date; $we_ight = $FTCH->solder_weight;}else if($mas_stage_id1==16){ $dat_e = $FTCH->loading_date; $we_ight = $FTCH->buf_weight;}
								if($flag==1)
								{
									$emp_id	=	base64_encode(base64_encode(base64_encode($FTCH->emp_det_id)));					
									$SLCT_e = "SELECT * FROM emp_det WHERE emp_det_id = '$FTCH->emp_det_id'";
									$QURY_e = mysqli_query($connect,$SLCT_e);
									$FTCH_e = mysqli_fetch_object($QURY_e);
								}				
								if($mas_stage_id1==2)
								{
									$emp_id	=	base64_encode(base64_encode(base64_encode($FTCH->emp_det_id)));								
									if($FTCH->emp_det_id!=0)
									{
										$SLCT_e = "SELECT * FROM emp_det WHERE emp_det_id = '$FTCH->emp_det_id'";
										$QURY_e = mysqli_query($connect,$SLCT_e);
										$FTCH_e = mysqli_fetch_object($QURY_e);
										$des_nm = $FTCH_e->name;
									}
									else
									{
										$des_nm = 'CAD';
									}
								}										
					
								if($loop%2 != "0")
									$bg = "gradeX";
								else
									$bg = "class='gradeC'";
					?>
								<tr <?php echo $bg ?>>            
									<td align="center" style="display:table-cell;vertical-align:middle"><?php echo $loop ?></td>
									<?php
										if($flgg==0)
										{					
									?>
											<input name="mas_stg_id" id="mas_stg_id" type="hidden" value="<?php echo $mas_stage_id1.",0"; ?>" />            
											<td align="center" style="display:table-cell;vertical-align:middle"><?php echo str_replace("-", "/",$dat_e);?></td>
											<?php if($mas_stage_id1==2){ ?><td align="center" style="display:table-cell;vertical-align:middle"><?php echo $des_nm; ?></td><?php } ?>
											<td align="center" style="display:table-cell;vertical-align:middle"><?php echo $FTCH->des_cnts;?></td>
											<?php if($mas_stage_id1==5 || $mas_stage_id1==12){ ?><td align="center" style="display:table-cell;vertical-align:middle"><?php echo number_format($we_ight,2)." g"; ?></td><?php } ?>
									<?php
										}
										else if($flgg==1)
										{
									?>
											<input name="mas_stg_id" id="mas_stg_id" type="hidden" value="<?php echo $mas_stage_id1.",0"; ?>" />            
									<?php
											if($mas_stage_id1!=10 || ($mas_stage_id1==10 && $emp_nam==''))
											{ 
									?>
												<td align="center" style="display:table-cell;vertical-align:middle"><?php echo $FTCH_e->name; ?></td>
									<?php
											}
									?>
											
											<td align="center" style="display:table-cell;vertical-align:middle"><?php echo $FTCH->des_cnts; ?></td>
									<?php
											if($mas_stage_id1 == 3) 
											{	
												$SLCT_tim = "SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(`cad_time`))) AS tm_to FROM cad_finishing_des,cad_finishing WHERE cad_finishing_des.cad_finishing_id IN(SELECT cad_finishing_id FROM cad_finishing WHERE ".$dat_str.") AND cad_finishing.cad_finishing_id = cad_finishing_des.cad_finishing_id AND emp_det_id = '$FTCH->emp_det_id' ORDER BY STR_TO_DATE(finishing_date,'%d-%m-%Y'),design_det_id ASC";
												$QURY_tim = mysqli_query($connect,$SLCT_tim);
												$FTCH_tim = mysqli_fetch_object($QURY_tim);
												$tot_tm = $FTCH_tim->tm_to;
												if($FTCH->cad_time != '' || $FTCH->cad_time != 0)
												{
													$cat_tm = $tot_tm." / ".$cat_hrs;
												}
												else
												{
													$cat_tm = "00:00 / ".$cat_hrs;
												}
												$tot_time[] = $tot_tm;
												$SLCT_tim2 = "SELECT * FROM cad_finishing_des,cad_finishing WHERE cad_finishing_des.cad_finishing_id IN(SELECT cad_finishing_id FROM cad_finishing WHERE ".$dat_str.") AND emp_det_id='$FTCH->emp_det_id' AND cad_finishing.cad_finishing_id = cad_finishing_des.cad_finishing_id ORDER BY STR_TO_DATE(finishing_date,'%d-%m-%Y'),design_det_id ASC";
												$QURY_tim2 = mysqli_query($connect,$SLCT_tim2);
												while($FTCH_tim2 = mysqli_fetch_object($QURY_tim2))
												{
													$SLCT_dm1 = "SELECT  * FROM design_det WHERE design_det_id = '$FTCH_tim2->design_det_id'";
													$QURY_dm1 = mysqli_query($connect,$SLCT_dm1);
													$FTCH_dm1 = mysqli_fetch_object($QURY_dm1);

													$SLCT_d1 = "SELECT  * FROM master_cat WHERE master_cat_id = '$FTCH_dm1->master_cat_id'";
													$QURY_d1 = mysqli_query($connect,$SLCT_d1);
													$FTCH_d1 = mysqli_fetch_object($QURY_d1);
													if($mas_stage_id1 == 3) 
													{
														$SLCT_ds1 = "SELECT  * FROM master_cad_cat WHERE master_cad_cat_id = '$FTCH_dm1->master_cad_cat_id'";
														$QURY_ds1 = mysqli_query($connect,$SLCT_ds1);
														$FTCH_ds1 = mysqli_fetch_object($QURY_ds1);
														$pns = $pns+$FTCH_ds1->cad_points;	
													}
												}
												$pnts = $pnts+$pns;
									?>        
												<td align="center" style="display:table-cell;vertical-align:middle"><?php echo $pns;$pns=0;?></td>
												<td align="center" style="display:table-cell;vertical-align:middle"><?php echo $cat_tm; ?></td>
									<?php
											}
											if($mas_stage_id1 == 10) 
											{
									?>        
												<td align="center" style="display:table-cell;vertical-align:middle"><?php echo number_format($we_ight,2)." g";$wgts = $wgts+$we_ight;?></td>
									<?php	
											}
										}
									?>   
									<td class="center">&nbsp;</td>         
								</tr>	
						<?php
								$cts += $FTCH->des_cnts;				
								$loop ++;
							}
						}
						?>
					</tbody>
					<input name="tot_cnt" id="tot_cnt" type="hidden" value="<?php echo $cts; ?>" />	
				    <input name="tot_pn_cnt" id="tot_pn_cnt" type="hidden" value="<?php echo $pnts; ?>" />
				    <?php if(isset($mas_stage_id1) && $mas_stage_id1 == 10) { ?>
    				<input name="tot_wgt_cnt" id="tot_wgt_cnt" type="hidden" value="<?php echo number_format($wgts,2)." g"; ?>" />
				    <?php } if(isset($mas_stage_id1) && $mas_stage_id1 == 3) { ?>
				    <input name="tot_tm_cnt" id="tot_tm_cnt" type="hidden" value="<?php echo sum_the_time($tot_time); ?>" />
				    <?php } ?>	
				</table> 
		<?php 
			}
		?>
	</body>
</html>
