<?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';
	}
?>
<!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>Untitled Document</title>
</head>

<body>
<?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'";
		//echo $SLCTs;
		$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'";
		
		$SLCTse = "SELECT * FROM emp_det WHERE master_dept_id ='$emp_id_dec'";
		$QURYse = mysqli_query($connect,$SLCTse);
		$FTCHse = mysqli_fetch_object($QURYse);
		$emp_nam = $FTCHse->name;
							
	}
	else
	{
		$emp_id_url = '';
		$emp_qur_str = '';
		$emp_nam = '';
	}
	
	
	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 = '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'];
?>
<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="198">&nbsp;</td>
    <td width="60">&nbsp;</td>
    <td width="248">&nbsp;</td>
    <td width="70">&nbsp;</td>
    <td width="106">&nbsp;</td>
    <td width="138">&nbsp;</td>
    <td width="94">&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;<?php echo $FTCHs->emp_lable; ?></td>
    <td>&nbsp;</td>
    <td>&nbsp;<?php if($emp_nam!=''){ echo $emp_nam; }else{ echo "Over All"; } ?></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </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>&nbsp;<?php if($yea_r!=''){ echo "Year"; } ?></td>
    <td>&nbsp;<?php if($yea_r!=''){ echo "Month"; } ?></td>
    <td>&nbsp;<?php if($yea_r!=''){ echo "Day"; } ?></td>
  </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($mas_stage_id1==1){ $design_date = "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"; }
			
			$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 = "DATE_FORMAT(STR_TO_DATE(".$design_date.",'%d-%m-%Y'),'%m-%Y') BETWEEN DATE_FORMAT(STR_TO_DATE('$fdate','%d/%m/%Y'),'%m-%Y') AND 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')) 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 = "DATE_FORMAT(STR_TO_DATE(".$design_date.",'%d-%m-%Y'),'%m-%Y') BETWEEN DATE_FORMAT(STR_TO_DATE('$fdate','%d/%m/%Y'),'%m-%Y') AND 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') 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 * FROM design_det WHERE ".$dat_str.$emp_qur_str. " ORDER BY design_det_id ASC";
			}
			else if($mas_stage_id1==2)
			{
				$SLCT = "SELECT * FROM cad_loading WHERE ".$dat_str." ORDER BY design_det_id ASC";
			}
			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 design_det_id ASC";
			}
			else if($mas_stage_id1==4)
			{
				$SLCT = "SELECT * FROM cam_loading WHERE ".$dat_str." ORDER BY design_det_id ASC";
			}
			else if($mas_stage_id1==5)
			{
				$SLCT = "SELECT * FROM cam_finishing WHERE ".$dat_str." ORDER BY design_det_id ASC";
				$weigh_t = 'Cam Weight';
			}
			else if($mas_stage_id1==6)
			{
				$SLCT = "SELECT * FROM rtv_loading WHERE ".$dat_str." ORDER BY 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 design_det_id ASC";
			}
			else if($mas_stage_id1==8)
			{
				$SLCT = "SELECT * FROM wax_finishing WHERE ".$dat_str." ORDER BY design_det_id ASC";
			}
			else if($mas_stage_id1==9)
			{
				$SLCT = "SELECT * FROM casting_finishing WHERE ".$dat_str." ORDER BY 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 design_det_id ASC";
			}
			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 design_det_id ASC";
			}
			else if($mas_stage_id1==12)
			{
				$SLCT = "SELECT * FROM mas_finishing WHERE ".$dat_str." ORDER BY design_det_id ASC";
				$weigh_t = 'Master Weight';
			}
			
			?>
<table width="960" 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>
			 <th width="81"><?php echo  $design_date; ?> </th>
			<?php if($mas_stage_id1==5 || $mas_stage_id1==12){ ?><th width="136"><?php echo $weigh_t; ?></th><?php } ?> 
            <?php if($flag==1){ ?><th width="181"><?php echo $empl_name; ?></th><?php } ?>
            <th width="118">Design Status</th>
            <?php if($flg=="wimg"){ ?><th width="118">Image</th><?php } ?>
            <th width="175">Remarks</th>
            <!--  <th width="138">View/Update</th>-->
           <!-- <th width="146">Status</th>-->
            </tr>
            </thead>
            <tbody>

			<?php 
			//echo $SLCT;
			$QURY = mysqli_query($connect,$SLCT);
			$loop=1;
			if(mysqli_num_rows($QURY)>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);
				
				$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; }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;}
				
				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);
				}
				//echo $FTCH->emp_det_id."<br/>";
										
				
				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; }
			?>
            <tr <?php echo $bg ?>>
            <td align="center" style="display:table-cell;vertical-align:middle"><?php echo $loop ?></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>
			<td align="center" style="display:table-cell;vertical-align:middle"><?php echo $dat_e; ?></td>
			<?php if($mas_stage_id1==5 || $mas_stage_id1==12){ ?><td align="center" style="display:table-cell;vertical-align:middle"><?php echo $we_ight; ?></td><?php } ?>
           <?php 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_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>
</table>
</body>
</html>
