<?php
class Master_list extends CI_Model {

	public function add_material($post) {
          $result = $this->db->insert('materials',$post);
          if($result){return true;}else{return false;} 
		  
    }


     public function get_vendors()
			{
				$sql = $this->db->get('vendors');
				if($sql->num_rows() >0)
				{return $sql->result_array();
				}else
				{return false;}
			}

			public function get_vendors_assembling()
			{
				$this->db->where('type_vender',7);
				$sql = $this->db->get('vendors');
				if($sql->num_rows() >0)
				{return $sql->result_array();
				}else
				{return false;}
			}

			

    public function add_material_mix_color($post) {
          $result = $this->db->insert('material_mix_color',$post);
          if($result){return true;}else{return false;} 
		  
    }

    public function delete_material($id)
	{
		$sql = $this->db->delete('materials', array('id'=>$id));
		
		return true;
	}


	public function get_materials_all($id)
		{
		$sql = $this->db->where('id',$id);
		$sql = $this->db->get('materials');


		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
		}


	public function update_material($id) {
		$result = $this->db->where('id',$id);
		$result = $this->db->update('materials',$_POST);
		if($result){return true;}else{return false;}
	}

	public function get_materials()
	{
		$sql = $this->db->get('materials');
		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}


	
	public function add_product_mold($post) {





          $result = $this->db->insert('product_moulds',$post);
          if($result){return true;}else{return false;} 
		  
    }


	public function get_pro_molds($id)
	{
		
		$sql = $this->db->where('product_id',$id);
		$sql = $this->db->get('product_moulds');
		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}

	

	
    public function product_delete_mold($id)
	{
		$sql = $this->db->delete('product_moulds', array('id'=>$id));
		
		return true;
	}


	

	public function material_type()
	{
		$sql = $this->db->get('material_type');
		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}


	 public function delete_dom_data($id)
	{
		$sql = $this->db->delete('boms', array('main_product'=>$id));
		
		return true;
	}

	public function get_materials_data($id)
	{
		 $sql = $this->db->where('id',$id);
		$sql = $this->db->get('materials');
		
		
		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}
			
		public function get_products_data($id)
	{
		 $sql = $this->db->where('id',$id);
		$sql = $this->db->get('products');
		
		
		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}

	public function get_product_data_all($id)
		{
		$sql = $this->db->where('id',$id);
		$sql = $this->db->get('products');
		
		
		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}

		}


		/*public function get_parameter($id)
		{
		$sql = $this->db->where('product_id',$id);
		$sql = $this->db->get('qc_parameter');
		
		
		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}

		}*/


	public function get_products_data_child($id)
	{
		 $sql = $this->db->where('id',$id);
		$sql = $this->db->get('products');
		
		
		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}

	public function get_counter_number(){

		$this->db->select('products.pro_code');  
		  
		$this->db->from('products');
		$this->db->where('product_type',1);
		$this->db->order_by('pro_code','DESC');
		//$this->db->Group_by
		$this->db->limit(1);
		$sql = $this->db->get();

		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}

	public function get_counter_number_fg(){

		$this->db->select('products.pro_code');  
		  
		$this->db->from('products');
		$this->db->where('product_type',5);
		$this->db->order_by('pro_code','DESC');
		
		$this->db->limit(1);
		$sql = $this->db->get();

		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}

	

	public function get_counter_number_printed(){

		$this->db->select('products.pro_code');  
		  
		$this->db->from('products');
		$this->db->where('product_type',2);
		$this->db->order_by('pro_code','DESC');
		
		$this->db->limit(1);
		$sql = $this->db->get();

		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}


	public function get_counter_number_assembled(){

		$this->db->select('products.pro_code');  
		  
		$this->db->from('products');
		$this->db->where('product_type',3);
		$this->db->order_by('pro_code','DESC');
		
		$this->db->limit(1);
		$sql = $this->db->get();

		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}

	
	public function get_counter_number_sealed(){

		$this->db->select('products.pro_code');  
		  
		$this->db->from('products');
		$this->db->where('product_type',4);
		$this->db->order_by('pro_code','DESC');
		
		$this->db->limit(1);
		$sql = $this->db->get();

		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}

	public function get_counter_number_raw(){

		$this->db->select('materials.material_code');  
		  
		$this->db->from('materials');
		$this->db->where('type',1);
		$this->db->order_by('material_code','DESC');
		
		$this->db->limit(1);
		$sql = $this->db->get();

		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}	


	public function get_counter_packing(){

		$this->db->select('materials.material_code');  
		  
		$this->db->from('materials');
		$this->db->where('type',2);
		$this->db->order_by('material_code','DESC');
		
		$this->db->limit(1);
		$sql = $this->db->get();

		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}	


    public function add_products($pro_code,$product_type,$name,$weight,$apperence,$engrave,$color_code,$color_ratio,$role_size,$printing,$salophine_size,$salophine_qty,$carton,$begstandard,$remarks,$file,$user_id,$created,$modified) {
    	
					 $data = array(
					 'pro_code'=>$pro_code,
					 'name' => $name,
					 'apperence'=> $apperence, 
					 'color_code' => $color_code,
					 'color_ratio'=> $color_ratio, 
			        'product_type' => $product_type,
					'weight' => $weight,
					'engrave' => $engrave,
					'role_size'=> $role_size, 
					'salophine_size'=> $salophine_size, 
					'salophine_qty' => $salophine_qty,
					'carton' => $carton,
					'image' => $file,
					'printing' => $printing,
					'beg_standard' => $begstandard,
					'remarks' => $remarks,
					'user_id' => $user_id,
					'created' => $created,
					'modified' => $modified
			        
			        );




         	 $result = $this->db->insert('products',$data);
          if($result){return true;}else{return false;} 
		  
    }

    
     public function add_product_parameter($post) {
          $result = $this->db->insert('qc_parameter',$post);
          if($result){return true;}else{return false;} 
		  
    }

    

    public function update_products_status($id)
			{
			
				$result = $this->db->where('id',$id);
				$result = $this->db->update('products',array('qc_status' => 1));
				if($result){return true;}else{return false;}
			}

    public function get_products_parameter($id)
	{
		$sql = $this->db->get_where('qc_parameter', array('product_id'=>$id));
		
		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}

    public function delete_product($id)
	{
		$sql = $this->db->delete('products', array('id'=>$id));
		
		return true;
	}


	
	public function product_delete_parameter($id)
	{
		$sql = $this->db->delete('qc_parameter', array('id'=>$id));
		
		return true;
	}

	public function get_product_data($id)
	{
		$sql = $this->db->get_where('products', array('id'=>$id));
		
		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}


	public function update_products($id) {
		$result = $this->db->where('id',$id);
		$result = $this->db->update('products',$_POST);
		if($result){return true;}else{return false;}
	}

    
    public function get_units()
	{
		$sql = $this->db->get('units');
		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}



	

	public function get_pro_type()
	{
		$sql = $this->db->get('product_type');
		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}


	public function get_products()
	{
		//$sql = $this->db->get('products');
		//$sql = $this->db->get_where('products', array('product_type !=',5));
		
		  $this->db->select('*');
		  $this->db->from('products');
		  $sql = $this->db->where('product_type !=',5);
		  
		  $sql = $this->db->get();


		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}

	public function get_products1()

	{
		//$sql = $this->db->get('products');
		//$sql = $this->db->get_where('products', array('product_type !=',5));
		
		  $this->db->select('*');
		  $this->db->from('products');
		  //$sql = $this->db->where('product_type !=',5);
		  
		  $sql = $this->db->get();

		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}



	public function get_products_mold()
	{
		
		  $this->db->select('*');
		  $this->db->from('products');
		  $this->db->where('product_type' ,1);
		  
		  $sql = $this->db->get();


		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}

	public function get_products_finish_goods()
	{
		$sql = $this->db->get('products');
		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}
	
	public function boms_details_add($post) {
          $result = $this->db->insert('boms',$post);
          if($result){return true;}else{return false;} 
		  
    }

    public function boms_details_add_child($arrayName) {
          $result = $this->db->insert('boms',$arrayName);
          if($result){return true;}else{return false;} 
		  
    }

    


    public function boms_details_add_materials($post) {
          $result = $this->db->insert('boms_materials',$post);
          if($result){return true;}else{return false;} 
		  
    }

    public function get_bom_data($id)
	{
		/*$sql = $this->db->get_where('boms', array('id'=>$id));
		*/

		$this->db->select('boms.id as boms_id,boms.product_id,boms.weight,boms.unit,products.name,products.id,products.product_type,products.pro_code,products.remarks,boms.child_product');  
		  
		$this->db->from('boms');
		$this->db->join('products', 'boms.product_id = products.id', 'inner');
		//$this->db->order_by("products.pro_code","desc");

		$this->db->where("boms.main_product",$id);
		$this->db->where("boms.child_product",0);


		//
		$sql = $this->db->get();

		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}

	}


	public function get_bom_data_batch($fg)
	{
		/*$sql = $this->db->get_where('boms', array('id'=>$id));
		*/

		$this->db->select('products.id,products.product_type');  
		  
		$this->db->from('boms');
		$this->db->join('products', 'boms.product_id = products.id', 'inner');
		//$this->db->order_by("products.pro_code","desc");

		$this->db->where("boms.main_product",$fg);
		$this->db->where("boms.child_product",0);
		//$this->db->where_in("products.product_type",[2,3,4]);

		
		$sql = $this->db->get();

		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}

	}

	

	public function departments()
	{
		/*$sql = $this->db->get_where('boms', array('id'=>$id));
		*/

		$this->db->select('product_type.id,product_type.name');  
		  
		$this->db->from('product_type');
		$this->db->where("product_type.id !=",1);
		//$this->db->where("product_type.id !=",5);
		$sql = $this->db->get();

		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}

	}

	public function printing_transform_dep()
	{
		/*$sql = $this->db->get_where('boms', array('id'=>$id));
		*/

		$this->db->select('product_type.id,product_type.name');  
		  
		$this->db->from('product_type');
		$this->db->where("product_type.id !=",1);
		//$this->db->where("product_type.id !=",5);
		$this->db->where("product_type.id !=",2);
		$sql = $this->db->get();

		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}

	}



	public function assembled_transform_dep()
	{
		/*$sql = $this->db->get_where('boms', array('id'=>$id));
		*/

		$this->db->select('product_type.id,product_type.name');  
		  
		$this->db->from('product_type');
		$this->db->where("product_type.id !=",1);
		$this->db->where("product_type.id !=",3);
		$this->db->where("product_type.id !=",2);
		$sql = $this->db->get();

		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}

	}

	public function sealed_transform_dep()
	{
		/*$sql = $this->db->get_where('boms', array('id'=>$id));
		*/

		$this->db->select('product_type.id,product_type.name');  
		  
		$this->db->from('product_type');
		$this->db->where("product_type.id !=",1);
		$this->db->where("product_type.id !=",3);
		$this->db->where("product_type.id !=",4);
		$this->db->where("product_type.id !=",2);
		$sql = $this->db->get();

		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}

	}
	

	public function get_bom_data_child($pro,$main)
	{
		//$sql = $this->db->get_where('boms', array('product_id'=>$pro,'main_product'=>$main));
		
		$this->db->select('boms.product_id,boms.main_product,boms.weight,boms.unit,boms.child_product,products.id,products.pro_code,products.name,products.product_type,products.remarks,boms.id as bom_id');    
		$this->db->from('boms');
		$this->db->join('products', 'boms.child_product = products.id', 'inner');
		//$this->db->join('table3', 'table1.id = table3.id', 'inner');
	   $this->db->where("boms.main_product",$main );

		$this->db->where("boms.product_id", $pro );
		$this->db->where("boms.child_product !=",0);

		$sql = $this->db->get();


		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}

		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}

	}

	
	public function bom_delete($id)
	{
		$sql = $this->db->delete('boms', array('id'=>$id));
		
		return true;
	}


	public function bom_delete_material($id)
	{
		$sql = $this->db->delete('boms_materials', array('id'=>$id));
		
		return true;
	}

		public function get_bom_count()
	{
		//select products.name,products.pro_code,products.weight, count(boms.product_id) from products as products left join boms as boms on products.id = boms.product_id group by products.id ORDER BY products.id DESC

		 $this->db->select('products.id,products.name,products.pro_code,products.weight, count(boms.product_id) as items');    
		$this->db->from('products');
		$this->db->join('boms', 'products.id = boms.product_id', 'left');
	    $this->db->Group_by("products.id");
		$this->db->order_by("products.id", 'DESC'); 
		echo $sql = $this->db->get();
		exit;
		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}


	public function get_bom_count_products()
	{
		//select products.name,products.pro_code,products.weight, count(boms.product_id) from products as products left join boms as boms on products.id = boms.product_id group by products.id ORDER BY products.id DESC

		$this->db->select('products.id,products.name,products.remarks,products.pro_code,products.weight' );    
		$this->db->from('products');
		//$this->db->join('boms', 'products.id = boms.main_product', 'inner');
		$this->db->where("products.product_type",5); 
	    $this->db->Group_by("products.id");
		$this->db->order_by("products.id", 'DESC'); 
		$sql = $this->db->get();
		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}


	 public function get_bom_data_materials($id)
	{
		/*$sql = $this->db->get_where('boms', array('id'=>$id));
		*/
		$this->db->select('boms_materials.id as boms_materials_id,boms_materials.product_id,boms_materials.material_id,boms_materials.weight,boms_materials.unit,materials.name,materials.id,materials.material_code,materials.grade');    
		$this->db->from('boms_materials');
		$this->db->join('materials', 'boms_materials.material_id = materials.id', 'inner');
		//$this->db->join('table3', 'table1.id = table3.id', 'inner');
		$this->db->where("boms_materials.product_id", $id );
		$sql = $this->db->get();

		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}
    
	 public function get_bom_data_materials_pro($pro,$main)
	{

		$this->db->select('boms_materials.id as boms_materials_id,boms_materials.product_id,boms_materials.material_id,boms_materials.weight,boms_materials.unit as bom_unit,materials.name,materials.id,materials.material_code,materials.grade,materials.type,materials.size,materials.unit');    
		$this->db->from('boms_materials');
		$this->db->join('materials', 'boms_materials.material_id = materials.id', 'inner');
		//$this->db->join('table3', 'table1.id = table3.id', 'inner');
	   $this->db->where("boms_materials.main_product",$main );
		$this->db->where("boms_materials.product_id", $pro );
		$sql = $this->db->get();


		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}


	public function get_bom_data_packing_material($id)
	{

		$this->db->select('boms_materials.id as boms_materials_id,boms_materials.product_id,boms_materials.material_id,boms_materials.weight,boms_materials.unit as bom_units,materials.name,materials.id,materials.material_code,materials.grade,materials.type,materials.size,materials.unit');    
		$this->db->from('boms_materials');
		$this->db->join('materials', 'boms_materials.material_id = materials.id', 'inner');
		//$this->db->join('table3', 'table1.id = table3.id', 'inner');
	   $this->db->where("boms_materials.main_product",$id );
	   $this->db->where("boms_materials.product_id",0);
		$sql = $this->db->get();


		if($sql->num_rows() >0)
		{return $sql->result_array();
		}else
		{return false;}
	}

	
}