<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class Product_Material_Core_Model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
         $this->db->query("SET sql_mode=(SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''));");
    }
    function saveProductMaterial($pd)
    {
        if ($this->db->insert('product_material', $pd)) {
            $lst = $this->db->insert_id();
            return $lst;
        } else {
            return false;
        }
    }
    function saveFinalProduct($pd)
    {
        if ($this->db->insert('product', $pd)) {
            $lst = $this->db->insert_id();
            return $lst;
        } else {
            return false;
        }
    }
    function updateProductMaterial($id,$data){
        if($id>0){
            $this->db->where('product_mat_id', $id);
            return $this->db->update('product_material',$data);  
        }else{
            return false;
        }
    }
    function updateFinalProduct($id,$data){
        if($id>0){
            $this->db->where('sub_product_of', $id);
            return $this->db->update('product',$data);  
        }else{
            return false;
        }
    }
    protected function getBasicProductMaterialMasterData($id){
        $this->db->select('p.* , c.cat_name , s.sub_cat_name, u.unit_name,b.brand_name');
        $this->db->from('product_material p');
        $this->db->join('product_category c', 'c.cat_id = p.mat_cat_id', 'left');
        $this->db->join('product_sub_category s', 's.sub_cat_id = p.mat_sub_cat_id', 'left');
        $this->db->join('mstr_unit u', 'u.unit_id = p.product_mat_unit', 'left');
         $this->db->join('master_brand b', 'p.mat_brand_id = b.brand_id', 'left');
        $this->db->where('p.product_mat_id',$id);
         $query = $this->db->get();
          if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return false;
        }
    }
    
    
    
    
    
    
    protected function getProductMaterialListServerSide($start, $length, $search_key_val)
    {
        $this->db->select('p.* , c.cat_name , s.sub_cat_name, u.unit_name');
        $this->db->from('product_material p');
        $this->db->join('product_category c', 'c.cat_id = p.mat_cat_id', 'left');
        $this->db->join('product_sub_category s', 's.sub_cat_id = p.mat_sub_cat_id', 'left');
        $this->db->join('mstr_unit u', 'u.unit_id = p.product_mat_unit', 'left');
        if ($search_key_val) {
          $this->db->where("p.product_mat_code LIKE '%$search_key_val%' AND p.product_mat_status = 1 ");
          $this->db->or_where("p.product_mat_name LIKE '%$search_key_val%' AND p.product_mat_status = 1 ");
        }
		$this->db->order_by("p.product_mat_id", "desc");
        if ($start != '' && $length != '') {
            $this->db->limit($length, $start);
            $query = $this->db->get();
            if ($query->num_rows() > 0) {
                return $query->result();
            } else {
                return false;
            }
        } else {
            $query = $this->db->get();
            if ($query->num_rows() > 0) {
                return $query->num_rows();
            } else {
                return false;
            }
        }
    }
    

  
}