<?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><?php echo $PROJECT_TITLE ?></title>
<script type="text/javascript" src="js/po_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_po_stage WHERE mas_po_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'])));  
	
		if($FTCHs->mas_po_stage_id =='6'){	$emp_qur_str = "AND pack_emp_id='$emp_id_dec'";	}else if($FTCHs->mas_po_stage_id =='7'){ $emp_qur_str = "AND cut_emp_id='$emp_id_dec'"; }
		if($flag==1)
		{
			$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_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'];
	if(isset($_GET['flgg']) && $_GET['flgg']!='')
	{
		$flgg = $_GET['flgg'];
	}
	else
	{
		$flgg = '';
	}
	if($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="201">&nbsp;</td>
    <td width="61">&nbsp;</td>
    <td width="252">&nbsp;</td>
    <td width="71">&nbsp;</td>
    <td width="107">&nbsp;</td>
    <td width="80">&nbsp;</td>
    <td width="158">&nbsp;</td>
  </tr>
  <?php if($flag==1){ ?>
  <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>
    
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <?php } ?>
  <tr>
    <td>&nbsp;From Date: <?php if($fdate!=''){ echo $fdate; }else{ echo "-----"; }; ?></td>
    <td>&nbsp;</td>
    <td>&nbsp;To Date: <?php if($todate!=''){ echo $todate; }else{ echo "-----"; }?></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"; } ?></td>
    <td>&nbsp;Tot. Des &nbsp;:&nbsp;<input type="text" id="tot_qty" name="tot_qty" value="" readonly="readonly" disabled="disabled" style="width:80px; background:#FFFFFF; font-size:12px; text-align:right; border:none;" />
    &nbsp;Tot. Die &nbsp;:&nbsp;<input type="text" id="tot_die" name="tot_die" value="" readonly="readonly" disabled="disabled" style="width: 80px; background:#FFFFFF; font-size:12px; text-align:right; border:none;" /></td>
  </tr>
  <?php if($mas_stage_id1!=6 && $mas_stage_id1!=7){ ?>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;Tot. Cost :&nbsp;<input type="text" id="tot_cost" name="tot_cost" value="" readonly="readonly" disabled="disabled" style="width:80px; background:#FFFFFF; font-size:12px; text-align:right; border:none;" /></td>
  </tr>
  <?php } ?>
  <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
	}
	else if($flgg=='frnd')
	{
	?>
    <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="201">&nbsp;</td>
    <td width="61">&nbsp;</td>
    <td width="252">&nbsp;</td>
    <td width="71">&nbsp;</td>
    <td width="107">&nbsp;</td>
    <td width="80">&nbsp;</td>
    <td width="158">&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;From Date: <?php if($fdate!=''){ echo $fdate; }else{ echo "-----"; }; ?></td>
    <td>&nbsp;</td>
    <td>&nbsp;To Date: <?php if($todate!=''){ echo $todate; }else{ echo "-----"; }?></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"; } ?></td>
    <td>&nbsp;Tot. Qty &nbsp;:&nbsp;<input type="text" id="tot_qty" name="tot_qty" value="" readonly="readonly" disabled="disabled" style="width:80px; background:#FFFFFF; font-size:12px; text-align:right; border:none;" />
    &nbsp;Tot. Die &nbsp;:&nbsp;<input type="text" id="tot_die" name="tot_die" value="" readonly="readonly" disabled="disabled" style="width: 80px; background:#FFFFFF; font-size:12px; text-align:right; border:none;" /></td>
  </tr>
  <?php if($mas_stage_id1!=6 && $mas_stage_id1!=7){ ?>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;Tot. Cost :&nbsp;<input type="text" id="tot_cost" name="tot_cost" value="" readonly="readonly" disabled="disabled" style="width:80px; background:#FFFFFF; font-size:12px; text-align:right; border:none;" /></td>
  </tr>
  <?php } ?>
  <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 = "po_date"; $des_lbl = "PO Entry Dt"; }else if($mas_stage_id1==2){ $design_date = "date_format(date(`po_finish`.`post_dt`),'%d-%m-%Y')"; $des_lbl = "PO Finish Dt"; }else if($mas_stage_id1==3){ $design_date = "delivery_date"; $des_lbl = "PO Delivery Dt"; }else if($mas_stage_id1==4){ $design_date = "po_date"; $des_lbl = "PO Date"; }else if($mas_stage_id1==5){ $design_date = "po_date"; $des_lbl = "PO Date"; }else if($mas_stage_id1==6){ $design_date = "cut_die_fin_date"; $dat_lable = "Packing Date"; }else if($mas_stage_id1==7){ $design_date = "die_fin_date"; $dat_lable = "Cutting 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($flgg=='')
			{
			if($mas_stage_id1==1)	
			{
				$SLCT = "SELECT *,COUNT(po_entry_det.design_det_id) AS des_count,GROUP_CONCAT(po_entry_det.design_det_id) AS des_det,IFNULL(SUM(master_pieces),'0') AS tot_pic FROM po_entry_det,po_entry,design_det WHERE design_det.design_det_id=po_entry_det.design_det_id AND po_entry_det.po_entry_id IN(SELECT po_entry_id FROM po_entry WHERE ".$dat_str.") AND po_entry.po_entry_id = po_entry_det.po_entry_id AND po_entry_det.cancelled='no' GROUP BY po_entry_det.po_entry_id";
				$mas_id = 1;
			}
			else if($mas_stage_id1==2)
			{
				$SLCT = "SELECT *,COUNT(po_finish.design_det_id) AS des_count,GROUP_CONCAT(po_finish.design_det_id) AS des_det,IFNULL(SUM(master_pieces),'0') AS tot_pic FROM po_finish,po_entry_det,po_entry,design_det WHERE design_det.design_det_id=po_finish.design_det_id AND ".$dat_str." AND po_entry_det.po_entry_det_id = po_finish.po_entry_det_id AND po_entry.po_entry_id = po_entry_det.po_entry_id AND po_entry_det.cancelled='no' GROUP BY po_entry.po_entry_id ASC";
				$mas_id = 1;
			}
			else if($mas_stage_id1==3)
			{
				$SLCT = "SELECT *,COUNT(po_delivery_det.design_det_id) AS des_count,GROUP_CONCAT(po_delivery_det.design_det_id) AS des_det,IFNULL(SUM(master_pieces),'0') AS tot_pic FROM po_entry,po_delivery_det,po_delivery,design_det WHERE design_det.design_det_id=po_delivery_det.design_det_id AND po_delivery_det.po_delivery_id IN(SELECT po_delivery_id FROM po_delivery WHERE ".$dat_str.") AND po_delivery.po_delivery_id = po_delivery_det.po_delivery_id AND po_entry.po_entry_id = po_delivery.po_entry_id AND po_delivery_det.cancelled='no' GROUP BY po_entry.po_entry_id";
				$mas_id = 1;
			}
			else if($mas_stage_id1==4)
			{
				$SLCT = "SELECT *,COUNT(po_entry_det.design_det_id) AS des_count,GROUP_CONCAT(po_entry_det.design_det_id) AS des_det,IFNULL(SUM(master_pieces),'0') AS tot_pic FROM po_entry_det,po_entry,design_det WHERE design_det.design_det_id=po_entry_det.design_det_id AND po_entry_det.po_entry_id IN(SELECT po_entry_id FROM po_entry WHERE ".$dat_str.") AND po_entry.po_entry_id = po_entry_det.po_entry_id AND po_entry_det.fin_status='no' AND po_entry_det.cancelled='no' GROUP BY po_entry_det.po_entry_id";
				$mas_id = 1;
			}
			else if($mas_stage_id1==5)
			{
				$SLCT = "SELECT *,COUNT(po_entry_det.design_det_id) AS des_count,GROUP_CONCAT(po_entry_det.design_det_id) AS des_det,IFNULL(SUM(master_pieces),'0') AS tot_pic FROM po_entry_det,po_entry,design_det WHERE design_det.design_det_id=po_entry_det.design_det_id AND po_entry_det.po_entry_id IN(SELECT po_entry_id FROM po_entry WHERE ".$dat_str.") AND po_entry.po_entry_id = po_entry_det.po_entry_id AND po_entry_det.fin_status ='yes' AND po_entry_det.del_status= 'no' AND po_entry_det.cancelled='no' GROUP BY po_entry_det.po_entry_id";
				$mas_id = 1;
			}
			else if($mas_stage_id1==6)
			{
				$SLCT = "SELECT die_fin_date,pack_emp_id,stock_det.design_det_id,'stock',IFNULL(master_pieces,'0') AS tot_pic FROM stock_det,design_det WHERE design_det.design_det_id=stock_det.design_det_id AND ".$dat_str.$emp_qur_str." UNION ALL SELECT die_fin_date,pack_emp_id,po_finish.design_det_id,po_finish_id,IFNULL(master_pieces,'0') AS tot_pic FROM po_finish,design_det WHERE design_det.design_det_id=po_finish.design_det_id AND ".$dat_str.$emp_qur_str." ORDER BY STR_TO_DATE(die_fin_date,'%d-%m-%Y')";
				$mas_id = 0;
			}
			else if($mas_stage_id1==7)
			{
				$SLCT = "SELECT die_fin_date,cut_emp_id,stock_det.design_det_id,'stock',IFNULL(master_pieces,'0') AS tot_pic FROM stock_det,design_det WHERE design_det.design_det_id=stock_det.design_det_id AND ".$dat_str.$emp_qur_str." UNION ALL  SELECT die_fin_date,cut_emp_id,po_finish.design_det_id,po_finish_id,IFNULL(master_pieces,'0') AS tot_pic FROM po_finish,design_det WHERE design_det.design_det_id=po_finish.design_det_id AND ".$dat_str.$emp_qur_str." ORDER BY STR_TO_DATE(die_fin_date,'%d-%m-%Y') ";
				$mas_id = 0;
			}
			
			?>
			
	<table width="962" border="1" align="center" cellpadding="0" cellspacing="0" class="dTable" id="dynamic" >
            <thead>
            <tr>
			<?php
				if($mas_stage_id1!=6  && $mas_stage_id1!=7)
				{ 
			?>
					<th width="40">S.No<span class="sorting" style="display: block;"></span></th>
					<th width="117"><?php if($flag==1){ echo "Design No"; }else{ echo "Po No"; } ?></th>
					<?php if($flag==1){  ?><th width="117">No of Dies</th><?php } ?>
					<th width="210"style="text-transform:capitalize"><?php if($flag==1){ echo str_replace("_"," ",$dat_lable); }else{ echo "Company Name"; } ?></th>
					<th width="128"><?php if($flag==1){ echo $empl_name; }else{ echo "Company Code"; } ?></th>
					<th width="63"><?php if($flag==1){ echo "Design Status"; }else{ echo "Qty"; } ?></th>
					<?php if($flag!=1){  ?><th width="117">No of Dies</th><?php } ?>
					<?php if($mas_id != 0) { ?><th width="63">Cost</th><?php } ?>
					<?php if($flag==1){ ?><?php if($flg=="wimg"){ ?><th width="116">  Image </th>
					<?php } ?><?php }else{ ?><th width="91" style="text-transform:capitalize"><?php echo $des_lbl; ?></th>
					<?php } ?>
					<th width="179">Remarks</th>
			<?php
			}
			else if($mas_stage_id1==6  || $mas_stage_id1==7)
			{
			?>
					<th width="47">S.No<span class="sorting" style="display: block;"></span></th>
					<th width="117"><?php echo "Po Date"; ?></th>
					<th width="120"><?php echo "Po No"; ?></th>
					<th width="120" style="text-transform:capitalize"><?php echo "Company Code"; ?></th>
					<?php /*?><th width="128" style="text-transform:capitalize"><?php echo "Company Name"; ?></th><?php */?>
					<th width="180"><?php echo $empl_name; ?></th>					
					<th width="150" style="text-transform:capitalize"><?php echo str_replace("_"," ",$dat_lable); ?></th>
                    <th width="128" style="text-transform:capitalize"><?php echo "Design No"; ?></th>
                    <th width="128" style="text-transform:capitalize"><?php echo "No of Dies"; ?></th>
					<?php if($flg=="wimg"){ ?><th width="116" style="text-transform:capitalize">Image</th><?php } ?>
					<th width="280">Remarks</th>
			<?php
				}
			?>				
            <!--<th width="102">Design Status</th>-->
            
          <!--  <th width="138">View/Update</th>-->
           <!-- <th width="146">Status</th>-->
            </tr>
            </thead>
            <tbody>

			<?php 
			//echo $SLCT;
			$sum_qty = 0;
			$tot_cost = 0;
			$cost_val = 0;
			$part_cst_cnt = 0;
			$tot_dies = 0;
			$QURY = mysqli_query($connect,$SLCT);
			$loop=1;
			if(mysqli_num_rows($QURY)>0)
			{			
			while($FTCH = mysqli_fetch_object($QURY))
			{
				if($flag==1)
				{
					$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($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; }
				}
				else
				{				
					$po_id = base64_encode(base64_encode(base64_encode($FTCH->po_entry_id)));		
						
					$SLCTp = "SELECT  * FROM party_det WHERE party_det_id ='$FTCH->party_det_id'";
					$QURYp = mysqli_query($connect,$SLCTp);
					$FTCHp = mysqli_fetch_object($QURYp);
				}	
				
				if($mas_stage_id1==1)
				{
					$dat_e = $FTCH->po_date; 
					$prin_link = '<a href="po_entry_view.php?po_id='.$po_id.'&fl=vie" target="_blank" id="searchlink1a'.$loop.'" onfocus="foc(this.id)" onblur="stnon(this.id)" style="text-decoration:underline;">'.$FTCH->des_count.'</a>';
					$SLCT_qy = "SELECT design_det.master_cat_id FROM `design_det`,po_entry_det WHERE po_entry_det.`design_det_id`=`design_det`.design_det_id AND po_entry_det.po_entry_id='$FTCH->po_entry_id' AND po_entry_det.cancelled = 'no'";					
				}
				else if($mas_stage_id1==2)
				{
					$dat_e = $FTCH->die_fin_date;  
					$prin_link = '<a href="po_finishing_view.php?code_no='.base64_encode(base64_encode(base64_encode(2))) .'&po_id='.$po_id .'" target="_blank" id="searchlink2a'.$loop.'" onfocus="foc(this.id)" onblur="stnon(this.id)" style="text-decoration:underline;">'.$FTCH->des_count.'</a>';
					$SLCT_qy = "SELECT design_det.master_cat_id FROM `design_det`,po_entry_det,po_finish WHERE po_entry_det.`design_det_id`=`design_det`.design_det_id AND po_entry_det.po_entry_id='$FTCH->po_entry_id' AND po_entry_det.po_entry_det_id = po_finish.po_entry_det_id";
				}
				else if($mas_stage_id1==3)
				{
					$dat_e = $FTCH->delivery_date;  
					$prin_link = '<a href="po_delivery_view.php?code_no='.base64_encode(base64_encode(base64_encode(2))) .'&po_id='.$po_id .'" target="_blank" id="searchlink3a'.$loop.'" onfocus="foc(this.id)" onblur="stnon(this.id)" style="text-decoration:underline;">'.$FTCH->des_count.'</a>';
					$SLCT_qy = "SELECT design_det.master_cat_id FROM `design_det`,po_delivery_det WHERE po_delivery_det.`design_det_id`=`design_det`.design_det_id AND po_delivery_det.po_delivery_id='$FTCH->po_entry_id'";
				}
				else if($mas_stage_id1==4)
				{ 
					$dat_e = $FTCH->po_date; 
					$prin_link = '<a href="po_finishing_pen_view.php?code_no='.base64_encode(base64_encode(base64_encode(2))) .'&po_id='.$po_id .'" target="_blank" id="searchlink4a'.$loop.'" onfocus="foc(this.id)" onblur="stnon(this.id)" style="text-decoration:underline;">'.$FTCH->des_count.'</a>';
					$SLCT_qy = "SELECT design_det.master_cat_id FROM `design_det`,po_entry_det WHERE po_entry_det.`design_det_id`=`design_det`.design_det_id AND po_entry_det.po_entry_id='$FTCH->po_entry_id' AND po_entry_det.fin_status='no' AND po_entry_det.cancelled='no'";
					//echo $SLCT_qy."<br>";
				}
				else if($mas_stage_id1==5)
				{ 
					$dat_e = $FTCH->po_date; 
					$prin_link = '<a href="po_delivery_pen_view.php?code_no='.base64_encode(base64_encode(base64_encode(3))) .'&po_id='.$po_id .'" target="_blank" id="searchlink5a'.$loop.'" onfocus="foc(this.id)" onblur="stnon(this.id)" style="text-decoration:underline;">'.$FTCH->des_count.'</a>';					
					$SLCT_qy = "SELECT design_det.master_cat_id FROM `design_det`,po_entry_det WHERE po_entry_det.`design_det_id`=`design_det`.design_det_id AND po_entry_det.po_entry_id='$FTCH->po_entry_id' AND po_entry_det.fin_status ='yes' AND po_entry_det.del_status= 'no' AND po_entry_det.cancelled='no'";
					//echo $SLCT_qy."<br>";
				}
				else if($mas_stage_id1==6)
				{ 
					$dat_e = $FTCH->die_fin_date; 
					
					$SLCT_e = "SELECT * FROM emp_det WHERE emp_det_id = '$FTCH->pack_emp_id'";
					$QURY_e = mysqli_query($connect,$SLCT_e);
					$FTCH_e = mysqli_fetch_object($QURY_e);
					$mas_id = 0;					
				}
				else if($mas_stage_id1==7)
				{ 
					$dat_e = $FTCH->die_fin_date; 
					
					$SLCT_e = "SELECT * FROM emp_det WHERE emp_det_id = '$FTCH->cut_emp_id'";
					$QURY_e = mysqli_query($connect,$SLCT_e);
					$FTCH_e = mysqli_fetch_object($QURY_e);
					$mas_id = 0;					
				}
				if($mas_stage_id1!=6 && $mas_stage_id1!=7)
				{
					$QURY_qy = mysqli_query($connect,$SLCT_qy);
					while($FTCH_qy = mysqli_fetch_object($QURY_qy))
					{					
						$SLCT_E1 = "SELECT * FROM party_cost_det WHERE party_det_id = '$FTCH->party_det_id' AND master_cat_id = '$FTCH_qy->master_cat_id' AND updated = 'no'"; 
						$QURY_E1 = mysqli_query($connect,$SLCT_E1);
						if(mysqli_num_rows($QURY_E1)>0)
						{
							$FTCH_E1 = mysqli_fetch_object($QURY_E1);							
							$cost_val = $cost_val + $FTCH_E1->cost_val;
							if($FTCH_E1->cost_val==0)
							{
								$part_cst_cnt = $part_cst_cnt+1;
							}
						}
						else
						{
							$cost_val = $cost_val + 0;
						}
					}
					if($part_cst_cnt!=0)
					{
						$flgg  = 1;
					}
					else
					{
						$flgg  = 0;
					}
					$tot_cost = $tot_cost +$cost_val;
				}
				//echo $cost_val."+";										
				
				if($loop%2 != "0")
					$bg = "gradeX";
				else
					$bg = "class='gradeC'";
					
			if($mas_stage_id1!=6  && $mas_stage_id1!=7)
			{
			?>
            <tr <?php echo $bg ?> <?php if($flgg==1){?> style="font-weight:bold" <?php } ?>>			
            <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 if($flag==1){ echo $FTCH_d->master_cat_name." ".$FTCH_dm->design_no; }else{ echo $FTCH->po_no; } ?></td>
            <?php if($flag==1){  ?><td align="center" style="display:table-cell;vertical-align:middle"><?php echo $FTCH_dm->master_pieces; ?></td><?php } ?>
            <td align="center" style="display:table-cell;vertical-align:middle"><?php if($flag==1){ echo $dat_e; }else{ echo $FTCHp->cmp_name; } ?></td>
            <td align="center" style="display:table-cell;vertical-align:middle"><?php if($flag==1){ echo $FTCH_e->name; }else{ echo $FTCHp->party_code; } ?></td>
            <td align="center" style="display:table-cell;vertical-align:middle"><?php if($flag==1){ echo $FTCH_dm->design_status; }else{ echo $prin_link; } ?></td>
			<?php if($flag!=1){  ?><td align="center" style="display:table-cell;vertical-align:middle"><?php echo $FTCH->tot_pic; ?></td><?php } ?>
			<?php if($mas_id != 0) { ?><td align="center" style="display:table-cell;vertical-align:middle"><?php echo number_format($cost_val,2); $part_cst_cnt=0; $cost_val=0;?></td><?php } ?>
			<?php if($flag==1){ ?><?php if($flg=="wimg"){ ?>
			<td align="center" style="display:table-cell;vertical-align:middle"><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 } ?><?php }else{ ?>
					     
			 <td align="center" style="display:table-cell;vertical-align:middle"><?php echo str_replace("-", "/",$dat_e); ?></td>
			 <?php } ?>
			 <td align="center" style="display:table-cell;vertical-align:middle">&nbsp;</td>
            </tr>            
            <?php
            	if($flag==1){
            	$tot_dies = $tot_dies + $FTCH_dm->master_pieces;
            	} else if($flag!=1){
            	$tot_dies = $tot_dies + $FTCH->tot_pic;	
            	}
				}
				else if($mas_stage_id1==6  || $mas_stage_id1==7)
				{ 
					if($FTCH->stock!='stock')
					{
						$SLCT_FNSH = "SELECT po_entry.po_no,po_entry.po_date,party_det.party_code,party_det.cmp_name FROM `po_finish`,`po_entry_det`,`po_entry`,`party_det` WHERE po_finish_id='$FTCH->stock' AND po_finish.po_entry_det_id = po_entry_det.po_entry_det_id AND po_entry_det.po_entry_id=po_entry.po_entry_id AND po_entry.party_det_id =party_det.party_det_id";
						$QURY_FNSH = mysqli_query($connect,$SLCT_FNSH);
						if(mysqli_num_rows($QURY_FNSH)>0)
						{
							$FTCH_FNSH = mysqli_fetch_object($QURY_FNSH);
							 
							$po_no 		=  $FTCH_FNSH->po_no;
							$po_dt 		=  str_replace("-", "/",$FTCH_FNSH->po_date);
							$party_cd 	=  $FTCH_FNSH->party_code;
							$party_nm 	=  $FTCH_FNSH->cmp_name;
						}
						else
						{
							$po_no 		=  "";
							$po_dt 		=  "";
							$party_cd 	=  "";
							$party_nm 	=  "";
						}
						
					}
					else
					{
						$po_no 		=  "Stock";
						$po_dt 		=  "Stock";
						$party_cd 	=  "Stock";
						$party_nm 	=  "Stock";
					}
					
			?>
					<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 $po_dt;  ?></td>
						<td align="center" style="display:table-cell;vertical-align:middle"><?php echo $po_no; ?></td>
						<td align="center" style="display:table-cell;vertical-align:middle"><?php echo $party_cd; ?></td>
						<?php /*?><td align="center" style="display:table-cell;vertical-align:middle"><?php echo $party_nm;  ?></td><?php */?>
						<td align="center" 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 str_replace("-", "/",$dat_e);; ?></td>
                        <td align="center" style="display:table-cell;vertical-align:middle"><?php echo $FTCH_d->master_cat_name." ".$FTCH_dm->design_no; ?></td>
						 <td align="center" style="display:table-cell;vertical-align:middle"><?php echo $FTCH_dm->master_pieces; ?></td>
						<?php if($flg=="wimg"){ ?><td align="center" style="display:table-cell;vertical-align:middle"><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 align="center" style="display:table-cell;vertical-align:middle">&nbsp; </td>
            		</tr>
            <?php
            	$tot_dies = $tot_dies + $FTCH_dm->master_pieces;
				}
			$loop ++;
			
			if($flag=='0'){  $sum_qty = $sum_qty+$FTCH->des_count; }
			 }
			 }
			 ?>
            </tbody>
			<input name="tot_cnt" id="tot_cnt" type="hidden" value="<?php if($flag==1){  echo $loop-1; }else{ echo $sum_qty; } ?>" />
            <input name="tot_cst" id="tot_cst" type="hidden" value="<?php echo number_format($tot_cost,2);?>" />	
            <input name="mas_die" id="mas_die" type="hidden" value="<?php echo $tot_dies;?>" />	 
</table>
<?php
			}
			else
			{        		
				if($mas_stage_id1==6)
				{
					$SLCT = "SELECT COUNT(*) AS cunt, SUM(cnt) AS tot_des, pack_emp_id, GROUP_CONCAT(design_det_id) AS design_det_id , GROUP_CONCAT(stock) AS stock,IFNULL(SUM(tot_pic),'0') AS tot_pic FROM( SELECT COUNT(*) AS cnt,GROUP_CONCAT(die_fin_date) AS die_fin_date, pack_emp_id,GROUP_CONCAT(stock_det.design_det_id) AS design_det_id,GROUP_CONCAT('stock') AS stock,IFNULL(SUM(master_pieces),'0') AS tot_pic FROM stock_det,design_det WHERE design_det.design_det_id=stock_det.design_det_id AND ".$dat_str.$emp_qur_str." GROUP BY pack_emp_id UNION ALL SELECT COUNT(*) AS cnt,GROUP_CONCAT(die_fin_date) AS die_fin_date,pack_emp_id,GROUP_CONCAT(po_finish.design_det_id) AS design_det_id , GROUP_CONCAT(po_finish_id) AS stock,IFNULL(SUM(master_pieces),'0') AS tot_pic FROM po_finish,design_det WHERE design_det.design_det_id=po_finish.design_det_id AND ".$dat_str.$emp_qur_str." GROUP BY pack_emp_id) AS X GROUP BY pack_emp_id";
				}
				else if($mas_stage_id1==7)
				{
					$SLCT = "SELECT COUNT(*) AS cunt, SUM(cnt) AS tot_des, cut_emp_id, GROUP_CONCAT(design_det_id) AS design_det_id , GROUP_CONCAT(stock) AS stock,IFNULL(SUM(tot_pic),'0') AS tot_pic FROM( SELECT COUNT(*) AS cnt,GROUP_CONCAT(die_fin_date) AS die_fin_date, cut_emp_id,GROUP_CONCAT(stock_det.design_det_id) AS design_det_id,GROUP_CONCAT('stock') AS stock,IFNULL(SUM(master_pieces),'0') AS tot_pic FROM stock_det,design_det WHERE design_det.design_det_id=stock_det.design_det_id AND ".$dat_str.$emp_qur_str." GROUP BY cut_emp_id UNION ALL  SELECT COUNT(*) AS cnt,GROUP_CONCAT(die_fin_date) AS die_fin_date,cut_emp_id,GROUP_CONCAT(po_finish.design_det_id) AS design_det_id , GROUP_CONCAT(po_finish_id) AS stock,IFNULL(SUM(master_pieces),'0') AS tot_pic FROM po_finish,design_det WHERE design_det.design_det_id=po_finish.design_det_id AND ".$dat_str.$emp_qur_str." GROUP BY cut_emp_id) AS X GROUP BY cut_emp_id";
					$mas_id = 0;
				}
			?>
            <table width="962" border="1" align="center" cellpadding="0" cellspacing="0" class="dTable" id="dynamic" >
            <thead>
            <tr>
                <th width="47">S.No<span class="sorting" style="display: block;"></span></th>
                <th width="280"><?php echo $empl_name; ?></th>
                <th width="280"><?php echo "Total Design"; ?></th>
                <th width="280"><?php echo "No of Dies"; ?></th>
                <th width="280"><?php echo "Remarks"; ?></th>
            </tr>
            </thead>
            <tbody>

			<?php
			$sum_qty = 0;
			$des_qty = 0;
			$QURY = mysqli_query($connect,$SLCT);
			$loop=1;
			$v1 = 0;
			$tot_dies = 0;
			if(mysqli_num_rows($QURY)>0)
			{			
			while($FTCH = mysqli_fetch_object($QURY))
			{				
								
				if($mas_stage_id1==6)
				{  
					$emp_id = base64_encode(base64_encode(base64_encode($FTCH->pack_emp_id)));
					$SLCT_e = "SELECT * FROM emp_det WHERE emp_det_id = '$FTCH->pack_emp_id'";
					$QURY_e = mysqli_query($connect,$SLCT_e);
					$FTCH_e = mysqli_fetch_object($QURY_e);
				}
				else if($mas_stage_id1==7)
				{ 
					$emp_id = base64_encode(base64_encode(base64_encode($FTCH->cut_emp_id)));
					$SLCT_e = "SELECT * FROM emp_det WHERE emp_det_id = '$FTCH->cut_emp_id'";
					$QURY_e = mysqli_query($connect,$SLCT_e);
					$FTCH_e = mysqli_fetch_object($QURY_e);
				}
									
				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>
						<td align="center" 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->tot_des;  ?></td>
                        <td align="center" style="display:table-cell;vertical-align:middle"><?php echo $FTCH->tot_pic;  ?></td>
                        <td class="center">&nbsp;</td>						
            		</tr>
            <?php
            		$tot_dies = $tot_dies + $FTCH->tot_pic;
					 $des_qty = $des_qty+$FTCH->tot_des; 
					 $loop++;
				}				
				
			 }
			 ?>
            </tbody>
    		<input name="tot_cnt" id="tot_cnt" type="hidden" value="<?php if($flag==1){ echo $des_qty; }else{ echo $sum_qty; } ?>" />	 
    		<input name="tot_cst" id="tot_cst" type="hidden" value="<?php echo number_format($tot_cost,2);?>" />
    		<input name="mas_die" id="mas_die" type="hidden" value="<?php echo $tot_dies;?>" />
            </table> 
        <?php
			}
		?>
</body>
</html>
