<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class Core_Production_Estimate_Model extends CI_Model
{
    
    private $master_table='production_cost_estimation_master';
    private $master_primary_id='pcem_id';
    private $item_table='production_cost_estimation_item';
    private $item_primary_id='pcei_id';
    private $sub_item_table='production_cost_estimation_sub_item';
    private $sub_item_primary_id='pcesi_id';
    
    public function __construct()
    {
        parent::__construct();
        $this->db->query("SET sql_mode=(SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''));");
    }
    /* standard functions*/
    protected function saveMaster($pd){
        if ($this->db->insert($this->master_table, $pd)) {
            $lst = $this->db->insert_id();
            return $lst;
        } else {
            return false;
        }
    }
    protected function saveItems($pd){
        if ($this->db->insert($this->item_table, $pd)) {
            $lst = $this->db->insert_id();
            return $lst;
        } else {
            return false;
        }
    }
    protected function saveSubItems($pd){
        if ($this->db->insert($this->sub_item_table, $pd)) {
            $lst = $this->db->insert_id();
            return $lst;
        } else {
            return false;
        }
    }
    protected function updateMaster($id,$data){
        if($id>0){
            $this->db->where($this->master_primary_id, $id);
            return $this->db->update($this->master_table,$data);  
        }else{
            return false;
        }
    }
    protected function updateItems($id,$data){
        if($id>0){
            $this->db->where($this->item_primary_id, $id);
            return $this->db->update($this->item_table,$data);  
        }else{
            return false;
        }
    }
    protected function updateSubItems($id,$data){
        if($id>0){
            $this->db->where($this->sub_item_primary_id, $id);
            return $this->db->update($this->sub_item_table,$data);  
        }else{
            return false;
        }
    }
    protected function deleteMaster($id){
        if($id>0){
            $this->db->where($this->master_primary_id, $id);
            return $this->db->delete($this->master_table);  
        }else{
            return false;
        }
    }
    protected function deleteItems($id){
        if($id>0){
            $this->db->where($this->item_primary_id, $id);
            return $this->db->delete($this->item_table);  
        }else{
            return false;
        }
    }
    protected function deleteSubItems($id){
        if($id>0){
            $this->db->where($this->sub_item_primary_id, $id);
            return $this->db->delete($this->sub_item_table);  
        }else{
            return false;
        }
    }
    protected function selectMaster($id){
        $this->db->select('*');
        $this->db->from($this->master_table);
        $this->db->where($this->master_primary_id,$id);
         $query = $this->db->get();
          if ($query->num_rows() > 0) {
            return $query->row_array();
        } else {
            return false;
        }
    }
    protected function selectItems($id){
        $this->db->select('*');
        $this->db->from($this->item_table);
        $this->db->where($this->item_primary_id,$id);
         $query = $this->db->get();
          if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return false;
        }
    }
    protected function selectSubItems($id){
        $this->db->select('*');
        $this->db->from($this->sub_item_table);
        $this->db->where($this->sub_item_primary_id,$id);
         $query = $this->db->get();
          if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return false;
        }
    }
  
}