<?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';
	}
	
	$check_acc  = check_access('45');
?>
<!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/po_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;
		   $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'"; }
				}
				else
				{
					$emp_id_url = '';
					$emp_qur_str = '';
				}

				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 = '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"></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_po_stage WHERE statu_s='active' ORDER BY mas_po_stage_id ASC";
                                    $QURY = mysqli_query($connect,$SLCT);
                                    while($FTCH = mysqli_fetch_object($QURY))
                                    {
                                        $ma_st_id = base64_encode(base64_encode(base64_encode($FTCH->mas_po_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 = "po_date"; }else if($mas_stage_id1==2){ $design_date = "date_format(date(`po_finish`.`post_dt`),'%d-%m-%Y')"; }else if($mas_stage_id1==3){ $design_date = "delivery_date"; }else if($mas_stage_id1==4){ $design_date = "po_date"; }else if($mas_stage_id1==5){ $design_date = "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($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_delivery_det.cancelled='no' AND po_entry.po_entry_id = po_delivery.po_entry_id 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 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, SUM(tot_pic) 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, SUM(tot_pic) 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;
			}
			//echo $mas_stage_id1;
			//echo $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;">
		<?php if($flag==1){ ?>
		<a href="po_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>
		<?php }else{ ?>
		<a href="po_analize_print.php?sta_name=<?php echo $_GET['sta_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" target="_blank">Print</a>
		<?php } ?>
		</h6>
		<h6 style="float:right; margin-right:20px">Total Dies : <input type="text" id="tot_die" name="tot_die" value="" readonly="readonly" disabled="disabled" style="width:50px; font-size:12px; text-align:center;" /></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>
		<h6 style="float:right; margin-right:20px">Total Cost : <input type="text" id="tot_cost" name="tot_cost" value="" readonly="readonly" disabled="disabled" style="width:100px; font-size:12px; text-align:center;" /></h6>
		<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 width="677" border="0" cellpadding="0" cellspacing="0" class="dTable" id="dynamic">
            <thead>
            <tr>
			<?php
				if($mas_stage_id1!=6  && $mas_stage_id1!=7)
				{ 
			?>
					<th width="47">S.No<span class="sorting" style="display: block;"></span></th>
					<?php if($mas_stage_id1!=2){ ?><th width="248" style="text-transform:capitalize"><?php if($flag==1){ echo "Image"; }else{ echo str_replace("_"," ",$design_date); } ?></th><?php }?>
					<th width="280"><?php if($flag==1){ echo "Design No"; }else{ echo "Po No"; } ?></th>
					<?php if($flag==1){  ?><th width="280">No of  Dies</th><?php } ?>
					<th width="280" style="text-transform:capitalize"><?php if($flag==1){ echo str_replace("_"," ",$dat_lable); }else{ echo "Company Name"; } ?></th>
					<th width="280"><?php if($flag==1){ echo $empl_name; }else{ echo "Company Code"; } ?></th>
					<th width="280"><?php if($flag==1){ echo "Design Status"; }else{ echo "Qty"; } ?></th>
					<?php if($flag!=1){  ?><th width="280">No of  Dies</th><?php } ?>
					<th width="280">Cost</th>
			<?php
				}
				else if($mas_stage_id1==6  || $mas_stage_id1==7)
				{
			?>
                    <th width="47" align="center">S.No<span class="sorting" style="display: block;"></span></th>
                    <th width="280" align="left"><?php echo $empl_name; ?></th>
                    <th width="280"  align="left"><?php echo "Total Design"; ?></th>
                    <th width="280"  align="center">No of  Dies</th>
                    <th width="280"  align="right">Cost</th>
                    <th width="280" align="center">View</th>
			<?php
				}
			?>
            </tr>
            </thead>
            <tbody>

			<?php
			$sum_qty = 0;
			$des_qty = 0;
			$QURY = mysqli_query($connect,$SLCT);
			$loop=1;
			$v1 = 0;
			$tot_cost = 0;
			$tot_ct_vl1 = 0;
			$tot_ct_vl2 = 0;
			$cost_val = 0;
			$part_cst_cnt = 0;
			$tot_pic = 0;
			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)));
					$cat_id = base64_encode(base64_encode(base64_encode($FTCH->master_cat_id)));
					$party_en_id = $FTCH->party_det_id;
					$cat_en_id = $FTCH->master_cat_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)
				{  
					$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($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 = '$party_en_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;
						}
					}
					$tot_cost =$tot_cost + $cost_val;
					if($part_cst_cnt!=0)
					{
						$flgg  = 1;
					}
					else
					{
						$flgg  = 0;
					}
				}
				else
				{
					$cost_val = 0;
				}
				if($loop%2 != "0")
					$bg = "gradeX";
				else
					$bg = "class='gradeC'";
					
				//echo $mas_stage_id1;
			?>
			<?php
				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>
				<?php if($mas_stage_id1!=2){ ?><td align="center" style="display:table-cell;vertical-align:middle"><?php if($flag==1){ ?>
				<div class="body" align="center">
				<a href="<?php echo $imag1; ?>" id="searchlink1<?php echo $loop; ?>" onfocus="foc(this.id)" onblur="stnon(this.id)" title="<h2><?php echo $FTCH_d->master_cat_name." ".$FTCH_dm->design_no; ?></h2>" class="lightbox"><img src="<?php echo $imag; ?>" width="60" height="60"/></a>                </div>
				<?php }else{ echo str_replace("-", "/",$dat_e); } ?></td><?php } ?>
				<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 $tot_pic = $tot_pic+$FTCH_dm->master_pieces; } ?>
				<td align="center" style="display:table-cell;vertical-align:middle"><?php if($flag==1){ echo str_replace("-", "/",$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 $tot_pic = $tot_pic+$FTCH->tot_pic; } ?>
				<td align="right" style="display:table-cell;vertical-align:middle"><?php echo number_format($cost_val,2); $part_cst_cnt=0; $cost_val=0; ?></td>
            </tr>
            <?php
				}
				else if($mas_stage_id1==6  || $mas_stage_id1==7)
				{ 
					if($mas_stage_id1==6)
					{
						$SLCT_FT = "SELECT die_fin_date,pack_emp_id,design_det_id,'stock' FROM stock_det WHERE ".$dat_str." AND pack_emp_id='$FTCH->pack_emp_id' UNION ALL SELECT die_fin_date,pack_emp_id,design_det_id,po_finish_id FROM po_finish WHERE ".$dat_str." AND pack_emp_id='$FTCH->pack_emp_id'";
					}
					else
					{
						$SLCT_FT = "SELECT die_fin_date,cut_emp_id,design_det_id,'stock' FROM stock_det WHERE ".$dat_str." AND cut_emp_id='$FTCH->cut_emp_id' UNION ALL  SELECT die_fin_date,cut_emp_id,design_det_id,po_finish_id FROM po_finish WHERE ".$dat_str." AND cut_emp_id='$FTCH->cut_emp_id'";
					}
					$QURY_FT = mysqli_query($connect,$SLCT_FT);
					while($FTCH_FT = mysqli_fetch_object($QURY_FT))
					{
						$dat_e = $FTCH_FT->die_fin_date; 
						if($FTCH_FT->stock!='stock')
						{
							$SLCT_FNSH = "SELECT po_entry.po_date,party_det.party_det_id,po_entry_det.master_cat_id FROM `po_finish`,`po_entry_det`,`po_entry`,`party_det` WHERE po_finish_id='$FTCH_FT->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);
								$SLCT_FNSH2 = "SELECT cost_val FROM party_cost_det WHERE ( party_det_id = '$FTCH_FNSH->party_det_id' AND master_cat_id = '$FTCH_FNSH->master_cat_id' ) AND ( CASE WHEN to_date!='' && fm_date!='' THEN STR_TO_DATE('$FTCH_FNSH->po_date','%d-%m-%Y') BETWEEN STR_TO_DATE(`fm_date`,'%d-%m-%Y') AND STR_TO_DATE(`to_date`,'%d-%m-%Y') WHEN to_date='' && fm_date!='' THEN STR_TO_DATE('$FTCH_FNSH->po_date','%d-%m-%Y') BETWEEN STR_TO_DATE(`fm_date`,'%d-%m-%Y') AND STR_TO_DATE('$dat_e','%d-%m-%Y') ELSE 0 END)";
								$QURY_FNSH2 = mysqli_query($connect,$SLCT_FNSH2);
								if(mysqli_num_rows($QURY_FNSH2)>0)
								{
									$FTCH_FNSH2 = mysqli_fetch_object($QURY_FNSH2);
									$ct_vl 		=  $FTCH_FNSH2->cost_val;
								}
								else
								{
									$ct_vl =  0;
								}
							}
							else
							{
								$ct_vl =  0;
							}
							$tot_ct_vl1 +=$ct_vl;
						}
						else
						{
							$SLCT_FNSH2a = "SELECT cost_vl FROM `design_det`,`master_cat` WHERE `design_det_id`='$FTCH_FT->design_det_id' AND master_cat.master_cat_id = design_det.master_cat_id ";
							$QURY_FNSH2a = mysqli_query($connect,$SLCT_FNSH2a);
							$FTCH_FNSH2a = mysqli_fetch_object($QURY_FNSH2a);
							$ct_vl2 	=  $FTCH_FNSH2a->cost_vl;
							
							$tot_ct_vl2 +=$ct_vl2;
						}
						$tot_cst = $tot_ct_vl1 + $tot_ct_vl2;
					}
			?>
					<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; $tot_ct_vl1=0; $tot_ct_vl2=0; ?></td>
                        <td align="center" style="display:table-cell;vertical-align:middle"><?php echo $FTCH->tot_pic; ?></td>
                        <td align="center" style="display:table-cell;vertical-align:middle"><?php echo number_format($tot_cst,2); ?></td>
                        <td align="center" style="display:table-cell;vertical-align:middle"><a id="searchlink2<?php echo $loop; ?>" onfocus="foc(this.id)" onblur="stnon(this.id)" href="po_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
            	$tot_pic = $tot_pic+$FTCH->tot_pic;
					$tot_cost += $tot_cst; 
				}				
			$loop++;
			
			if($flag=='0'){  $sum_qty = $sum_qty+$FTCH->des_count; }else if($flag==1){ $des_qty = $des_qty+$FTCH->tot_des; }
			 }
			 }
			 ?>
            </tbody>
	<?php } ?>
    
            </table> 
        </div>
        </div>   
        <input name="mas_die" id="mas_die" type="hidden" value="<?php echo $tot_pic;  ?>" />
	<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);?>" />	 
        </fieldset>
     </form>
        <div class="fluid"><!-- Media table --></div>
<?php } else {  echo ACCESS_MSG;} ?>		
    </div>
    <!-- Main content ends -->
    
</div>
<!-- Content ends -->

</body>
</html>
