<?php
class seas_socio_Model {
	
	public function socio_composition($survey_detail,$factorid=0) {
		
		$srv_mrkt = $survey_detail['srv_mrkt'];
		$srv_type = $survey_detail['type'];
		$sample_size = $survey_detail['sample'];
		
		
		$socio_composition = array();
		
		try  {
			$sql_factordes = "select * from mst_factor where 
								srv_type=0
								and srv_mrkt=". $srv_mrkt ." 
								and factordes not like 'weight'";
			if($factorid>0) $sql_factordes .= " and factorid = " . $factorid;
			$sql_factordes .= " order by pos";
			
			$sql_rslt = mysql_query($sql_factordes);
			
			while($fact_rw = mysql_fetch_assoc($sql_rslt)) {
				
				$tot_option_count =0;
				
				$id = $fact_rw['srv_mrkt'] . "_". $fact_rw['srv_type'] . "_". $fact_rw['factorid']; 
				$socio_composition[$id]['factordes'] = $fact_rw['factordes'];
				$socio_composition[$id]['options'] = array();
				$options_sql  = "select * from mst_socio where srv_mrkt=". $fact_rw['srv_mrkt'] ." and factorid=" . $fact_rw['factorid'];
				$options_rslt = mysql_query($options_sql);

				while($opt_rw = mysql_fetch_assoc($options_rslt)) {
					
					$socio_composition[$id]['options'][$opt_rw['opt_id']]['opt_id']   = $opt_rw['opt_id'];
					$socio_composition[$id]['options'][$opt_rw['opt_id']]['opt_desc'] = $opt_rw['opt_desc'];
					$socio_composition[$id]['options'][$opt_rw['opt_id']]['opt_count'] = 0;
					$socio_composition[$id]['options'][$opt_rw['opt_id']]['opt_perc'] = 0;
					
					$resp_sql = 'select sum(weight) as tot_sample from survey_resp where respid in (select survey_resp.respid from survey_resp 
									left join survey_respsocio on survey_resp.respid = survey_respsocio.respid
								where survey_resp.srv_mrkt=' . $srv_mrkt .  ' and survey_resp.srv_type=' . $srv_type .  '  
									and (survey_resp.srv_dt >= "' . $survey_detail['rw_srv_stdt'] .  '" 
										and survey_resp.srv_dt <= "' . $survey_detail['rw_srv_eddt'] .  '")
									and (survey_respsocio.factorid=' . $fact_rw['factorid'] .  '  
										and survey_respsocio.opt_id=' . $opt_rw['opt_id'] .  ' ))';
					
					$resp_rslt = mysql_query($resp_sql);
					
					if(mysql_num_rows($resp_rslt)) {
						$dt_row = mysql_fetch_assoc($resp_rslt);
						$perc = round(($dt_row['tot_sample'] / $sample_size) * 100,2);
						
						//$socio_composition[$id]['options'][$opt_rw['opt_id']]['opt_desc'] = $socio_composition[$id]['options'][$opt_rw['opt_id']]['opt_desc'] ."-". $perc;
						$socio_composition[$id]['options'][$opt_rw['opt_id']]['opt_count'] = round($dt_row['tot_sample'],2);
						$socio_composition[$id]['options'][$opt_rw['opt_id']]['opt_perc']  = $perc;
					}

					$tot_option_count = $tot_option_count + $socio_composition[$id]['options'][$opt_rw['opt_id']]['opt_count'];

					mysql_free_result($resp_rslt);
					
				}
				mysql_free_result($options_rslt);	
				
				
				if($tot_option_count <=0) {
					unset($socio_composition[$id]);
				}

			}
			
			mysql_free_result($sql_rslt);
		}
		catch (Exception $e) {
			
		}
		
		return $socio_composition;
	} 



}

?>