<?php
	ini_set('max_execution_time', 5000);	
	function data_fun($dept_nm,$CAT_ID)
	{
		switch($dept_nm)
		{
			case 1:
				$case_var 	= 'New Designs';
				$SLCT_CASE 	= "SELECT design_det.master_cat_id AS pen_id, COUNT(design_det_id) AS pen_cnt FROM design_det WHERE design_status = 'New Design' GROUP BY design_det.master_cat_id ORDER BY design_det.master_cat_id ASC";
				break;
			case 2:
				$case_var 	= 'CAD Loading';
				$SLCT_CASE 	= "SELECT design_det.master_cat_id AS pen_id, COUNT(cad_loading_id) AS pen_cnt FROM `cad_loading`,design_det WHERE design_det.design_det_id=cad_loading.design_det_id AND cad_loading.`emp_det_id`=0 AND `cad_status`='no' AND cad_loading.`design_det_id` NOT IN(SELECT `design_det_id` FROM `emp_current_design`) GROUP BY design_det.master_cat_id ORDER BY design_det.master_cat_id ASC";
				break;
			case 3:
				$case_var 	= 'CAD Finishing';
				$SLCT_CASE 	= "SELECT design_det.master_cat_id AS pen_id, COUNT(emp_current_design_id) AS pen_cnt FROM emp_current_design,design_det WHERE design_det.design_det_id=emp_current_design.design_det_id AND curr_status = 'closed' AND emp_current_design.`design_det_id` IN (SELECT `design_det_id` FROM cad_finishing_des WHERE cad_fin_status='no') GROUP BY design_det.master_cat_id ORDER BY design_det.master_cat_id ASC";
				break;
			case 4:
				$case_var 	= 'C / R / S';
				$SLCT_CASE 	= "SELECT pen_id, SUM(pen_cnt) AS pen_cnt FROM (SELECT design_det.master_cat_id AS pen_id,COUNT(cad_finishing_des_id) AS pen_cnt FROM cad_finishing_des,design_det WHERE design_det.design_det_id=cad_finishing_des.design_det_id AND `to_rtv_load` = 'no' AND `to_silver_load` = 'no' AND `cad_fin_status` = 'no' AND cad_finishing_des.design_det_id IN(SELECT design_det_id FROM cam_loading WHERE cam_status='no')
							   UNION
							   SELECT design_det.master_cat_id AS pen_id,COUNT(cad_finishing_des_id) AS pen_cnt FROM cad_finishing_des,design_det WHERE design_det.design_det_id=cad_finishing_des.design_det_id AND `to_rtv_load` = 'yes' AND `cad_fin_status` = 'no'
							   UNION
							   SELECT design_det.master_cat_id AS pen_id,COUNT(cad_finishing_des_id) AS pen_cnt FROM cad_finishing_des,design_det WHERE design_det.design_det_id=cad_finishing_des.design_det_id AND `to_silver_load` = 'yes' AND cad_finishing_des.design_det_id NOT IN (SELECT design_det_id FROM silver_loading)) AS TBL1 GROUP BY pen_id HAVING pen_id>0 ORDER BY pen_id ASC";
				break;
			case 5:
				$case_var 	= 'CAM Loading';
				$SLCT_CASE 	= "SELECT design_det.master_cat_id AS pen_id,COUNT(cam_loading_id) AS pen_cnt FROM cam_loading,design_det WHERE design_det.design_det_id=cam_loading.design_det_id AND cam_status = 'no' GROUP BY design_det.master_cat_id ORDER BY design_det.master_cat_id ASC";
				break;
			case 6:
				$case_var 	= 'CAM Receiving';
				$SLCT_CASE 	= "SELECT design_det.master_cat_id AS pen_id,COUNT(cam_finishing_id) AS pen_cnt FROM cam_finishing,design_det WHERE design_det.design_det_id=cam_finishing.design_det_id AND cam_finishing.`design_det_id` NOT IN (SELECT `design_det_id` FROM rtv_loading) GROUP BY design_det.master_cat_id ORDER BY design_det.master_cat_id ASC";
				break;
			case 7:
				$case_var 	= 'RTV Loading';
				$SLCT_CASE 	= "SELECT design_det.master_cat_id AS pen_id,COUNT(rtv_loading_id) AS pen_cnt FROM rtv_loading,design_det WHERE design_det.design_det_id=rtv_loading.design_det_id AND rtv_status = 'no' GROUP BY design_det.master_cat_id ORDER BY design_det.master_cat_id ASC";
				break;
			case 8:
				$case_var 	= 'Wax loading';
				//$SLCT_CASE 	= "SELECT design_det.master_cat_id AS pen_id,COUNT(rtv_finishing_des.rtv_finishing_des_id) AS pen_cnt FROM rtv_finishing_des,design_det WHERE rtv_finishing_des.rtv_fin_status = 'no' AND rtv_finishing_des.up_status = 'no' AND design_det.design_det_id = rtv_finishing_des.design_det_id AND design_det.statu_s='active' AND design_det.up_status='no' GROUP BY design_det.master_cat_id ORDER BY design_det.master_cat_id ASC";
				$SLCT_CASE 	= "SELECT design_det.master_cat_id AS pen_id,COUNT(wax_finishing_id) AS pen_cnt FROM wax_finishing,design_det WHERE wax_finishing.wax_fin_status = 'no' AND design_det.design_det_id=wax_finishing.design_det_id AND design_det.statu_s='active' AND design_det.up_status='no' AND wax_finishing.up_status='no' GROUP BY design_det.master_cat_id ORDER BY design_det.master_cat_id ASC";
				break;
			case 9:
				$case_var 	= 'Casting';
				//$SLCT_CASE 	= "SELECT design_det.master_cat_id AS pen_id,COUNT(wax_finishing_id) AS pen_cnt FROM wax_finishing,design_det WHERE wax_finishing.wax_fin_status = 'no' AND design_det.design_det_id=wax_finishing.design_det_id AND design_det.statu_s='active' AND design_det.up_status='no' AND wax_finishing.up_status='no' GROUP BY design_det.master_cat_id ORDER BY design_det.master_cat_id ASC";
				$SLCT_CASE 	= "SELECT design_det.master_cat_id AS pen_id,COUNT(casting_finishing_id) AS pen_cnt FROM casting_finishing,design_det WHERE design_det.design_det_id=casting_finishing.design_det_id AND cas_fin_status = 'no' AND casting_finishing.up_status='no' GROUP BY design_det.master_cat_id ORDER BY design_det.master_cat_id ASC";
				break;
			case 10:
				$case_var 	= 'Silver Loading';
				$SLCT_CASE 	= "SELECT design_det.master_cat_id AS pen_id,COUNT(silver_loading_id) AS pen_cnt FROM silver_loading,design_det WHERE design_det.design_det_id=silver_loading.design_det_id AND silver_status = 'no' GROUP BY design_det.master_cat_id ORDER BY design_det.master_cat_id ASC";
				break;
			case 11:
				$case_var 	= 'Silver Receiving';
				$SLCT_CASE 	= "SELECT design_det.master_cat_id AS pen_id,COUNT(silver_finishing_id) AS pen_cnt FROM silver_finishing,design_det WHERE design_det.design_det_id=silver_finishing.design_det_id AND to_solder_load = 'no' GROUP BY design_det.master_cat_id ORDER BY design_det.master_cat_id ASC";
				break;
			case 12:
				$case_var 	= 'Soldering Loading';
				$SLCT_CASE 	= "SELECT design_det.master_cat_id AS pen_id,COUNT(soldering_loading_id) AS pen_cnt FROM soldering_loading,design_det WHERE design_det.design_det_id=soldering_loading.design_det_id AND soldering_status = 'no' GROUP BY design_det.master_cat_id ORDER BY design_det.master_cat_id ASC";
				break;
			case 13:
				$case_var 	= 'Soldering Receiving';
				$SLCT_CASE 	= "SELECT design_det.master_cat_id AS pen_id,COUNT(sol_finishing_id) AS pen_cnt FROM sol_finishing_des,design_det WHERE design_det.design_det_id=sol_finishing_des.design_det_id AND sol_fin_status = 'no' GROUP BY design_det.master_cat_id ORDER BY design_det.master_cat_id ASC";
				break;	
			case 14:
				$case_var 	= 'Buffing Loading';
				$SLCT_CASE 	= "SELECT design_det.master_cat_id AS pen_id,COUNT(buf_loading_id) AS pen_cnt FROM buf_loading,design_det WHERE design_det.design_det_id=buf_loading.design_det_id AND buf_status = 'no' GROUP BY design_det.master_cat_id ORDER BY design_det.master_cat_id ASC";
				break;
			case 15:
				$case_var 	= 'Master';
				//$SLCT_CASE 	= "SELECT design_det.master_cat_id AS pen_id,COUNT(buf_finishing_des_id) AS pen_cnt FROM buf_finishing_des,design_det WHERE design_det.design_det_id=buf_finishing_des.design_det_id AND buf_fin_status ='no' AND buf_finishing_des.up_status='no' GROUP BY design_det.master_cat_id ORDER BY design_det.master_cat_id ASC";
				$SLCT_CASE 	= "SELECT design_det.master_cat_id AS pen_id,COUNT(mas_finishing_id) AS pen_cnt FROM mas_finishing,design_det WHERE design_det.design_det_id=mas_finishing.design_det_id AND mas_finishing.show_status ='no' AND mas_finishing.up_status='no' GROUP BY design_det.master_cat_id ORDER BY design_det.master_cat_id ASC";
				break;
			case 16:
				$case_var 	= 'Show Case';
				$SLCT_CASE 	= "SELECT design_det.master_cat_id AS pen_id,COUNT(show_case_id) AS pen_cnt FROM show_case,design_det WHERE design_det.design_det_id=show_case.design_det_id AND show_case.up_status='no' GROUP BY design_det.master_cat_id ORDER BY design_det.master_cat_id ASC";
				break;
			default:
				$case_var 	= '';
				$SLCT_CASE 	= "";
				break;
		}
		$PEN_VAL = '';
		$PEN_ID = '';
		$QURY_CASE 	= mysqli_query($connect,$SLCT_CASE);
		if(mysqli_num_rows($QURY_CASE)>0)
		{
			while($FTCH_CASE 	= mysqli_fetch_object($QURY_CASE))
			{
				$PEN_VAL 	= $PEN_VAL.$FTCH_CASE->pen_cnt.",";
				$PEN_ID 	= $PEN_ID.$FTCH_CASE->pen_id.",";
				$PEN_VAL_TEST[$FTCH_CASE->pen_id-1] = $FTCH_CASE->pen_cnt;
			}
		}
		else
		{
			$PEN_VAL 	= '';
			$PEN_ID 	= '';
			$PEN_VAL_TEST[0] = '0';
		}		
		$EXPLD 		= explode(",",rtrim($PEN_ID,","));
		$EXPLD_VL	= explode(",",rtrim($PEN_VAL,","));
		$MAS_VALUE 	= '';
		foreach($CAT_ID AS $key => $value)
		{
			if (in_array($key, $EXPLD)) 
			{
				$MAS_VALUE[] = $value;
			}
			else
			{
				$MAS_VALUE[] = '0';
			}
		}
		//$EXPLD_RSLT	=	explode(",",rtrim($MAS_VALUE,','));
		$EXPLD_RSLT	=	$MAS_VALUE;
		$RSLT = array_replace($EXPLD_RSLT,$PEN_VAL_TEST);
		return $dept_nm." \t ".$case_var." \t ".implode(" \t ",$RSLT)." \n ";
		$PEN_VAL 	= '';
		$PEN_ID 	= '';
		$MAS_VALUE  = '';
	}
?>