<?php
class Component_model extends CI_Model {
function __construct()
	{
		parent::__construct();

	}
	public function get_component_list()
	{	//$searchtext, $column_name, $order_by
        $sql = "SELECT * FROM component where IsActive=0";
        $record = $this->db->query($sql);
        return $record->result_array();
	}		
	
public function get_category_list()
	{	//$searchtext, $column_name, $order_by
        $sql = "SELECT * FROM category where IsActive=0 group by category";
        $record = $this->db->query($sql);
        return $record->result_array();
	}	
	public function get_heading_list()
	{	//$searchtext, $column_name, $order_by
        $sql = "SELECT * FROM headingmaster where IsActive=0 group by heading";
        $record = $this->db->query($sql);
        return $record->result_array();
	}	

	public function save_component($post_data)
	{
				$component=$post_data['component'];
				$sqlc = "SELECT count(*) as count,component_no FROM component where IsActive=0 and component='$component'";
				$recordc = $this->db->query($sqlc);
				$cc=$recordc->row('count');
				$component_no=$recordc->row('component_no');
				if($cc > 0)
				{
				//	echo '131';
				$component_no=$recordc->row('component_no');
				}
				else
				{
				//	echo '22';
				$sql = "SELECT max(component_no) as component_no FROM component";
				$record = $this->db->query($sql);
				$component_no1=$record->row('component_no');
				$component_no=$component_no1+1;				
				}
			
			
				$component=$post_data['component'];
				$price=$post_data['price'];
				//$master_product=$post_data['master_product'];
			//	$category=$post_data['category'];
				$count2 = count($post_data['master_product']);
			//echo $count2;
				$query_values2 = array();

				for($i=0; $i<$count2; $i++)
				{
				//	echo $i;
				$db = get_instance()->db->conn_id;

				
				$category=$post_data['category'];;
				$master_product=mysqli_real_escape_string($db,$post_data['master_product'][$i]);
				
				$heading=$post_data['heading'];
				$data=array('component'=>$component,'price'=>$price,'category'=>$category,'heading'=>$heading,
				'master_product'=>$master_product,'component_no'=>$component_no);
				
				$in=$this->db->insert('component',$data);
				
				$sqlca = "SELECT count(*) as count FROM category where IsActive=0 and master_product='$master_product'";
				$recordca = $this->db->query($sqlca);
				$cca=$recordca->row('count');
				if($cca <= 0)
				{
				$sql1 = "SELECT max(category_no) as category_no FROM category";
				$record1 = $this->db->query($sql1);
				$category_no1=$record1->row('category_no');
				$category_no=$category_no1+1;	
				
				 $sql6 = "SELECT * FROM category where category_no='$category'";
					$record6 = $this->db->query($sql6);
					$category_name=$record6->row('category');
									
									
					
				$data=array('category'=>$category_name,'sorder'=>'0','master_product'=>$master_product,'category_no'=>$category_no);
				$in=$this->db->insert('category',$data);
				}
				
				$sqlca1 = "SELECT count(*) as count FROM headingmaster where IsActive=0 and master_product='$master_product'";
				$recordca1 = $this->db->query($sqlca1);
				$cca1=$recordca1->row('count');
				if($cca1 <= 0)
				{
					 $sql1h = "SELECT * FROM headingmaster where heading_no='$heading'";
					$record1h = $this->db->query($sql1h);
					$heading1h=$record1h->row('heading');	
					
				$sql11 = "SELECT max(heading_no) as heading_no FROM headingmaster";
				$record11 = $this->db->query($sql11);
				$heading_no1=$record11->row('heading_no');
				$heading_no=$heading_no1+1;	
					
				$data=array('heading'=>$heading1h,'master_product'=>$master_product,'heading_no'=>$heading_no);
				$in=$this->db->insert('headingmaster',$data);
				}
				
				
				
				}
				return TRUE;
				
	}	

	public function update_component($post_data)
	{	
							
				$component_no=$post_data['id'];

				$this->db->where('component_no',$component_no);
				 $in= $this->db->delete('component');
				 
				$count2 = count($post_data['master_product']);
			//echo $count2;
		
				$query_values2 = array();

				for($i=0; $i<$count2; $i++)
				{
				//	echo $i;
				$db = get_instance()->db->conn_id;

				$master_product=mysqli_real_escape_string($db,$post_data['master_product'][$i]);
				
				$component=$post_data['component'];
				$price=$post_data['price'];
				$category=$post_data['category'];
				$heading=$post_data['heading'];
				$component_no=$post_data['id'];
				
				$data=array('component'=>$component,'price'=>$price,'category'=>$category,'heading'=>$heading,
				'master_product'=>$master_product,'component_no'=>$component_no);
				$in=$this->db->insert('component',$data);
				
				$sqlca = "SELECT count(*) as count FROM category where IsActive=0 and master_product='$master_product'";
				$recordca = $this->db->query($sqlca);
				$cca=$recordca->row('count');
				if($cca <= 0)
				{
				$sql1 = "SELECT max(category_no) as category_no FROM category";
				$record1 = $this->db->query($sql1);
				$category_no1=$record1->row('category_no');
				$category_no=$category_no1+1;	
				
				 $sql6 = "SELECT * FROM category where category_no='$category'";
					$record6 = $this->db->query($sql6);
					$category_name=$record6->row('category');
									
									
					
				$data=array('category'=>$category_name,'sorder'=>'0','master_product'=>$master_product,'category_no'=>$category_no);
				$in=$this->db->insert('category',$data);
				}
				
				$sqlca1 = "SELECT count(*) as count FROM headingmaster where IsActive=0 and master_product='$master_product'";
				$recordca1 = $this->db->query($sqlca1);
				$cca1=$recordca1->row('count');
				if($cca1 <= 0)
				{
					 $sql1h = "SELECT * FROM headingmaster where heading_no='$heading'";
					$record1h = $this->db->query($sql1h);
					$heading1h=$record1h->row('heading');	
					
				$sql11 = "SELECT max(heading_no) as heading_no FROM headingmaster";
				$record11 = $this->db->query($sql11);
				$heading_no1=$record11->row('heading_no');
				$heading_no=$heading_no1+1;	
					
				$data=array('heading'=>$heading1h,'master_product'=>$master_product,'heading_no'=>$heading_no);
				$in=$this->db->insert('headingmaster',$data);
				}
				
				
				
				
				}
				return true;
	}
	
	public function delete_component($id)
	{	
	
				$data=array('IsActive'=>'1');
				$this->db->where('component_no',$id);
				 return $this->db->update('component',$data);
			
				
	}	



	

}?>