<?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'";
		//echo $SLCT_log;
		$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;
		}
	}
	
	$check_acc  = check_access('44');
?>
<!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=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<title><?php echo $PROJECT_TITLE ?></title>

<script type="text/javascript" src="js/design_analize_JS.js"></script>
<link href="css/styles.css" rel="stylesheet" type="text/css" />
<!--[if IE]> <link href="css/ie.css" rel="stylesheet" type="text/css"> <![endif]-->

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>

<script type="text/javascript" src="js/plugins/forms/ui.spinner.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.mousewheel.js"></script>
 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>

<script type="text/javascript" src="js/plugins/charts/excanvas.min.js"></script>
<script type="text/javascript" src="js/plugins/charts/jquery.flot.js"></script>
<script type="text/javascript" src="js/plugins/charts/jquery.flot.orderBars.js"></script>
<script type="text/javascript" src="js/plugins/charts/jquery.flot.pie.js"></script>
<script type="text/javascript" src="js/plugins/charts/jquery.flot.resize.js"></script>
<script type="text/javascript" src="js/plugins/charts/jquery.sparkline.min.js"></script>

<script type="text/javascript" src="js/plugins/tables/jquery.dataTables.js"></script>
<script type="text/javascript" src="js/plugins/tables/jquery.sortable.js"></script>
<script type="text/javascript" src="js/plugins/tables/jquery.resizable.js"></script>

<script type="text/javascript" src="js/plugins/forms/autogrowtextarea.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.uniform.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.inputlimiter.min.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.tagsinput.min.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.maskedinput.min.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.autotab.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.chosen.min.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.dualListBox.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.cleditor.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.ibutton.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.validationEngine-en.js"></script>
<script type="text/javascript" src="js/plugins/forms/jquery.validationEngine.js"></script>

<script type="text/javascript" src="js/plugins/uploader/plupload.js"></script>
<script type="text/javascript" src="js/plugins/uploader/plupload.html4.js"></script>
<script type="text/javascript" src="js/plugins/uploader/plupload.html5.js"></script>
<script type="text/javascript" src="js/plugins/uploader/jquery.plupload.queue.js"></script>

<script type="text/javascript" src="js/plugins/wizards/jquery.form.wizard.js"></script>
<script type="text/javascript" src="js/plugins/wizards/jquery.validate.js"></script>
<script type="text/javascript" src="js/plugins/wizards/jquery.form.js"></script>

<script type="text/javascript" src="js/plugins/ui/jquery.collapsible.min.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.breadcrumbs.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.tipsy.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.progress.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.timeentry.min.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.colorpicker.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.jgrowl.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.fancybox.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.fileTree.js"></script>
<script type="text/javascript" src="js/plugins/ui/jquery.sourcerer.js"></script>

<script type="text/javascript" src="js/plugins/others/jquery.fullcalendar.js"></script>
<script type="text/javascript" src="js/plugins/others/jquery.elfinder.js"></script>

<script type="text/javascript" src="js/plugins/ui/jquery.easytabs.min.js"></script>
<script type="text/javascript" src="js/files/bootstrap.js"></script>
<script type="text/javascript" src="js/files/functions.js"></script>

<script type="text/javascript" src="js/charts/chart.js"></script>
<script type="text/javascript" src="js/charts/hBar_side.js"></script>

<!-- Shared on MafiaShare.net  --><!-- Shared on MafiaShare.net  --></head>

<body onload="tot_count();">

<!-- Top line begins -->
<?php include "header_footer/header.php"; ?>
<!-- Top line ends -->


<!-- Sidebar begins -->
<?php include "menus/left_menu.php"; ?>
<!-- Sidebar ends -->
    
    
<!-- Content begins -->
<div id="content">
    
<?php include "menus/top_menu.php"; ?>    
    <!-- Breadcrumbs line -->
    <div class="breadLine">
        <div class="bc">
            <ul id="breadcrumbs" class="breadcrumbs">
                <li><a href="#">Dashboard</a></li>
                <li><a href="javascript:">New Design Entry</a></li>
                    
            </ul>
        </div>
	<?php include "menus/top_left_menu.php"; ?>            
        
    </div>
    
    <!-- Main content -->
    <div class="wrapper">
    <?php if($check_acc==1){ ?>
    	   <?php
		   $flag = 0;
		   $cts = 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 = '';
					$mas_stage_id1 = '';
				}
				$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'";
										
				}
				else
				{
					$emp_id_url = '';
					$emp_qur_str = '';
				}
				
				
				if(isset($_GET['fdate']) && $_GET['fdate']!='' ){ $fdate = $_GET['fdate'];	}else{ $fdate = ''; }
				//echo $fdate;
				if(isset($_GET['todate']) && $_GET['todate']!='' ){ $todate = $_GET['todate'];	}else{ $todate = ''; }
				
				/*if(isset($_GET['yea_r']) && $_GET['yea_r']!='' ){ $yea_r = 'checked="checked"';	}else{ $yea_r = ''; }
				if(isset($_GET['mont_h']) && $_GET['mont_h']!='' ){ $mont_h = 'checked="checked"';	}else{ $mont_h = ''; }
				if(isset($_GET['da_y']) && $_GET['da_y']!='' ){ $da_y = 'checked="checked"';	}else{ $da_y = ''; }*/
				if(isset($_GET['yea_r']) && $_GET['yea_r']!='' ){ $yea_r = 'checked="checked"'; $y_r = $_GET['yea_r'];}else{ $yea_r = ''; $y_r = '';}
				if(isset($_GET['mont_h']) && $_GET['mont_h']!='' ){ $mont_h = 'checked="checked"'; $m_h = $_GET['mont_h'];}else{ $mont_h = ''; $m_h = ''; }
				if(isset($_GET['da_y']) && $_GET['da_y']!='' ){ $da_y = 'checked="checked"'; $d_y = $_GET['da_y'];}else{ $da_y = ''; $d_y = ''; }
				
		?>
    
        <ul class="middleNavR">
           <!-- <li><a href="#" title="Add an article" class="tipN"><img src="images/icons/middlenav/create.png" alt="" /></a></li>-->
           <!--<li><a href="design_add.php" title="Add Design" id="addpage" class="tipN"><img src="images/icons/middlenav/add.png" alt="" /></a></li>-->
           <?php /*?><li><a href="design_cat_add.php?id=<?php echo base64_encode(base64_encode(base64_encode(2)));?>" title="Add Cad Finishing Design" class="tipN"><img src="images/icons/middlenav/upload.png" alt="" /></a></li>   <?php */?>         
          <?php /*?> <li><a href="design_cat_add.php?id=<?php echo base64_encode(base64_encode(base64_encode(1)));?>" title="Add Master Finishing Design" class="tipN"><img src="images/icons/middlenav/upload_color.png" alt="" /></a></li><?php */?>
           <!--<li><a href="#" title="Check statistics" class="tipN"><img src="images/icons/middlenav/stats.png" alt="" /></a></li>-->
    	</ul>            
    		<form>
			
            <fieldset>
    		<div class="widget">
                			<div class="formRow" >
							<table width="100%">
								<tr>
									<td width="25%" style="display:table-cell; vertical-align:top;">Select Stage:</td>
									<td width="75%">
                            		<div class="searchDrop">
                               		 <select  data-placeholder="" class="select" name="sta_name" id="sta_name" style="width:350px;" onchange="reload_page()" onfocus="foc(this.id)" onblur="stnon(this.id)">
                                   		 <option selected="selected" value="">Choose a Stage...</option>
                                   			 <?php
												$SLCT = "SELECT  * FROM mas_stage WHERE statu_s='active' ORDER BY mas_stage_id ASC";
												//echo $SLCT;
												$QURY = mysqli_query($connect,$SLCT);
												while($FTCH = mysqli_fetch_object($QURY))
												{
												$ma_st_id = base64_encode(base64_encode(base64_encode($FTCH->mas_stage_id)));	
											?>
                                            <option <?php if($ma_st_id == $mas_stage_id){ ?> selected="selected" <?php }?> value="<?php echo $ma_st_id ?>"><?php echo $FTCH->stage_name; ?></option>
                                            <?php
                                            	}
                                            ?>
                                        
                                </select>
                                
                            </div> 
							</td>
							</tr>
							</table>
							</div>
							<?php if(isset($_GET['sta_name'])){ ?> 
							<input name="flag_js" id="flag_js" type="hidden" value="<?php echo $sta_iden; ?>" /> <!--THIS IS FOR JS FUNCTIONALITY-->
							
							<div class="formRow" >
							<table width="100%">
								<tr>
									<td width="9%" style="display:table-cell;vertical-align:middle">From Date:</td>
									<td width="22%"><div class="grid6"><input type="text" name="fdate" id="fdate" value="<?php echo $fdate ?>" class="datepicker" /></div></td>
							        <td width="9%" style="display:table-cell; vertical-align:middle;">To Date:</td>
							        <td width="23%"><div class="grid6"><input type="text" name="todate" id="todate" value="<?php echo $todate ?>" class="datepicker" /></div></td>
							        <td width="10%" style="display:table-cell; vertical-align:middle;">Filter:</td>
							        <td width="8%"><input id="yea_r"  name="filter" type="checkbox" value="y" <?php echo $yea_r ?> /> <label for="yea_r">Year</label></td>
							        <td width="8%"><input id="mont_h" name="filter2" type="checkbox" value="m" <?php echo $mont_h ?> /> <label for="mont_h">Month</label></td>
							        <td width="11%"><input id="da_y" name="filter3" type="checkbox" value="d" <?php echo $da_y ?> /> <label for="da_y">Day</label></td>
							  </tr>
							</table>
							</div>
							
							 <div class="formRow">
           						<input type="button" value="Submit" class="buttonM bBlack formSubmit" onclick="fom_validate();" style="float: right; margin-left:20px;"/>
                 				<input type="button" value="Reset" class="buttonM bRed" style="float: right; margin-left:20px;" onclick="form_rest();"/>
                			<div class="clear"></div></div>
                            
							<?php } ?>
							
                        <div class="clear"></div>
                    </div> 
					
                                                                         
                            
                       
    	<!-- Chart --><!-- 6 + 6 -->
		<?php if(isset($_GET['fdate']) && $_GET['fdate']!='' || isset($_GET['todate']) && $_GET['todate']!=''){ ?>
		
		 <?php
			$design_date = ''; // THIS IS FOR ASIGNING THE COLUMN NAME DYNAMICALLY
			
			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_str = ''; //THIS IS FOR QUERY STRING
						
			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($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";
				$weigh_t = 'Silver Weight';
				$flgg = 1;
			}
			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;
			}
			//echo $mas_stage_id1.$SLCT."<br>";
			?>
		
    	<div class="widget" style="display: block"> <!--style="width:600px; margin-left:150px;"-->
        <div class="whead">
		
		<h6>Design List</h6>
        <h6 style="float:right; margin-right:35px;text-decoration:underline;"><a href="design_analize_print.php?sta_name=<?php echo $_GET['sta_name'] ?>&em_name=<?php echo $_GET['em_name'] ?>&fdate=<?php echo $_GET['fdate'] ?>&todate=<?php echo $_GET['todate'] ?>&yea_r=<?php echo $_GET['yea_r'] ?>&mont_h=<?php echo $_GET['mont_h'] ?>&da_y=<?php echo $_GET['da_y'] ?>&flg=womg&flgg=frnd" target="_blank">Print</a></h6>
		<h6 style="float:right; margin-right:20px">Total Quantity : <input type="text" id="tot_qty" name="tot_qty" value="" readonly="readonly" disabled="disabled" style="width:50px; font-size:12px; text-align:center;" /></h6>
		<?php if($mas_stage_id1 == 10) { ?><h6 style="float:right; margin-right:10px">Total Weights : <input type="text" id="tot_wgts" name="tot_wgts" value="" readonly="readonly" disabled="disabled" style="width:50px; font-size:12px; text-align:center;" /></h6><?php } ?>
        <?php if($mas_stage_id1 == 3) { ?><h6 style="float:right; margin-right:10px">Total Points : <input type="text" id="tot_pnts" name="tot_pnts" value="" readonly="readonly" disabled="disabled" style="width:50px; font-size:12px; text-align:center;" /></h6>
			<h6 style="float:right; margin-right:10px">Total Time : <input type="text" id="tot_tims" name="tot_tims" value="" readonly="readonly" disabled="disabled" style="width:100px; font-size:12px; text-align:center;" /></h6>
		<?php } ?><div class="clear"></div></div>
        <div id="dyn" class="hiddenpars">
        	<a href="javascript:0" id="searchlink" onfocus="foc(this.id)" onblur="stnon(this.id)" class="tOptions" title="Options"><img src="images/icons/options.png" alt="" /></a>
            <table cellpadding="0" cellspacing="0" border="0" class="dTable" id="dynamic">
            <thead>
            <tr>  
            <?php
				if($flgg==0)
				{
			?>         
                    <th width="26" align="center" >S.No</th>                   
                    <th width="92" align="left" style="text-transform:capitalize"><?php echo  str_replace("_", " ",$design_date); ?> </th>
           	<?php
                    if($mas_stage_id1==2)
					{
          	?>
            			<th width="92" style="text-transform:capitalize">Designer Name</th>
            <?php
					}
			?>
				    <th width="92" align="right" style="text-transform:capitalize">Total Design</th>
                    <?php if($mas_stage_id1==5 || $mas_stage_id1==12 || $mas_stage_id1==14 || $mas_stage_id1==15 || $mas_stage_id1==16 ){ ?><th width="134"><?php echo "Total ".$weigh_t; ?></th><?php } ?> 
                    <th width="151" align="center"> View</th>
            
            <?php
				}
				else if($flgg==1)
				{
			?>
            		<th width="26" align="center">S.No</th>
                    <th width="134" align="left"><?php echo $empl_name; ?></th>
                    <th width="92" align="right" 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 
                        }
                    ?>
                    <?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="134"><?php echo "Total ".$weigh_t; ?></th><?php } ?> 
                    <th width="151" align="center">View</th>
            <?php
				}
			?>
          	<!--  <th width="138">View/Update</th>-->
           	<!-- <th width="146">Status</th>-->
            </tr>
            </thead>
            <tbody>

			<?php 
			//echo $SLCT;
			$QURY = mysqli_query($connect,$SLCT);
			$loop=1;
			$pnts = 0;
			$pns = 0;
			$wgts = 0;
			$we_ight='';
			$tot_time = '';
			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;	
						}
					}
					//echo $cat_hrs."<br>";
				}
				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 || $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);
						$cad_nm = $FTCH_e->name;
					}
					else
					{
						$cad_nm = 'CAD';
					}
				}
				else
				{
					$emp_id	='';
				}
				//echo $FTCH->emp_det_id."<br/>";
										
				
				if($loop%2 != "0")
					$bg = "gradeX";
				else
					$bg = "class='gradeC'";
				
				if($flgg==0)
				{
					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; }
			?>
            <tr <?php echo $bg ?>>
            <td align="center" style="display:table-cell;vertical-align:middle"><?php echo $loop ?></td>
            <input name="mas_stg_id" id="mas_stg_id" type="hidden" value="<?php echo $mas_stage_id1; ?>" />            
            <td align="left" 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 $cad_nm; ?></td>
            <?php
				}
			?>
            <td align="left" style="display:table-cell;vertical-align:middle"><?php echo $FTCH->des_cnts;?></td>
			<?php if($mas_stage_id1==5 || $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); ?></td><?php } ?>
            <td class="center">
             <a id="searchlink2<?php echo $loop; ?>" onfocus="foc(this.id)" onblur="stnon(this.id)" href="design_analize_view.php?sta_name=<?php echo $mas_stage_id; ?>&em_name=<?php echo $emp_id; ?>&fdate=<?php echo $fdate; ?>&todate=<?php echo $todate; ?>&yea_r=<?php echo $y_r; ?>&mont_h=<?php echo $m_h; ?>&da_y=<?php echo $d_y; ?>&des_dt=<?php echo $dat_e; ?>">view</a>
            </td>
			</tr>
            	
            <?php
					$cts += $FTCH->des_cnts;
				}
				else if($flgg==1)
				{
			?>
			<tr <?php echo $bg ?>>
            <td align="center" style="display:table-cell;vertical-align:middle"><?php echo $loop ?>
            </td>
            <input name="mas_stg_id" id="mas_stg_id" type="hidden" value="<?php echo $mas_stage_id1; ?>" />
            
            <td align="left" style="display:table-cell;vertical-align:middle"><?php echo $FTCH_e->name; ?></td>
            <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";
					//echo $SLCT_tim2."<br>";
					$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'";
							//echo $SLCT_ds1."<br>";
							$QURY_ds1 = mysqli_query($connect,$SLCT_ds1);
							$FTCH_ds1 = mysqli_fetch_object($QURY_ds1);
							$pns = $pns+$FTCH_ds1->cad_points;	
						}
					}
					
					/*$SLCT_re = "SELECT SUM(rework_pnts) AS rework_pnts FROM emp_current_design_rework WHERE emp_det_id = '$emp_ad_id' AND design_det_id IN($FTCH_CURR->desn_id)";
					$QURY_re = mysqli_query($connect,$SLCT_re);
					$num1 = mysqli_num_rows($QURY_re);
					
					$SLCT_re2 = "SELECT SUM(rework_pnts) AS rework_pnts, SUM(total_pnts) AS total_pnts FROM emp_current_design_rework WHERE re_emp_det_id = '$emp_ad_id' AND design_det_id IN($FTCH_CURR->desn_id)";
					$QURY_re2 = mysqli_query($connect,$SLCT_re2);
					$num2 =  mysqli_num_rows($QURY_re2);
					if($num1>0)
					{
						$FTCH_t = mysqli_fetch_object($QURY_re);
						if($FTCH_t->rework_pnts!='')
						{
							$re_pnts = $FTCH_t->rework_pnts;
						}
						else
						{
							$re_pnts = 0;
						}
					}											
					else
					{
						$re_pnts = 0;
					}
					if($num2>0)
					{
						$FTCH_t2 = mysqli_fetch_object($QURY_re2);
						if($FTCH_t2->rework_pnts!='')
						{
							$re_pnts2 = $FTCH_t2->rework_pnts;
							$tot_pnts2 = $FTCH_t2->total_pnts;
						}
						else
						{
							$re_pnts2 = 0;
							$tot_pnts2 = 0;
						}
					}
					else
					{
						$re_pnts2 = 0;
						$tot_pnts2 = 0;
					}*/
					
					$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
				}
			?> 
            <?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 } ?>
            <td class="center">
             <a id="searchlink2<?php echo $loop; ?>" onfocus="foc(this.id)" onblur="stnon(this.id)" href="design_analize_view.php?sta_name=<?php echo $mas_stage_id; ?>&em_name=<?php echo $emp_id; ?>&fdate=<?php echo $fdate; ?>&todate=<?php echo $todate; ?>&yea_r=<?php echo $y_r; ?>&mont_h=<?php echo $m_h; ?>&da_y=<?php echo $d_y; ?>">view</a>
            </td>
            
            </tr>	
            <?php
						$cts += $FTCH->des_cnts;
				}
			$loop ++;
			 }
			 }
			 ?>
            </tbody>
            </table> 
        </div>
        </div>   
	<?php 
		/*if($flgg==1)
		{
			$cnts = $cts;
		}
		else
		{
			$cnts = $loop-1;
		}*/
		//
		//print_r($tot_time);
		
	}
	
	?>
    
	<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 } ?>
        </fieldset>
     </form>
        <div class="fluid"><!-- Media table --></div>
        
        <?php } else {  echo ACCESS_MSG;} ?>
        
    </div>
    <!-- Main content ends -->
    
</div>
<!-- Content ends -->
<?php /*?><?php include "header_footer/footer.php"; ?><?php */?>
</body>
</html>
