<?php
class M_salesquotation extends CI_Model {
    
	public $table = "at_salesquotation";
	public $detail = "at_squodetail";
	
	function get_all(){
        $query = $this->db->get($this->table);
        return $query->result();
    }
	
	// function get_detail($id){
        // $query = $this->db->get_where($this->table, array('SQuoNumber' => $id));
        // return $query->result();
    // }

	// function get_detail_item($id){
        // $query = $this->db->get_where($this->detail, array('SQuoNumber' => $id));
        // return $query->result();
    // }
	
	function get_detail($id){
        $query = $this->db->get_where($this->table, array('SQuoNumber' => $id));
		if($query->num_rows()>0){
			return $query->row();
		}
		else{
			return false;
		}      
    }	
	function get_detail_item($SQuoNumber){
	    $this->db->_protect_identifiers = FALSE;
	    $this->db->select("*");
        $query = $this->db->get_where($this->detail, array('SQuoNumber' => $SQuoNumber));
		$this->db->_protect_identifiers = TRUE;
        return $query->result();
    }
	
	function insert($data) {
		$this->db->trans_begin();
		$this->db->insert($this->table, $data);
		
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return 0;
		}
		else {
			$this->db->trans_commit();
			return 1;
		}
	}
	
	function insert_detail($data) {
		$this->db->trans_begin();
		$this->db->insert($this->detail, $data);
		
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return 0;
		}
		else {
			$this->db->trans_commit();
			return 1;
		}
	}
	
	function update($data, $id) {
		$this->db->trans_begin();
        $this->db->update($this->table, $data, array('SQuoNumber' => $id));
		
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return 0;
		}
		else {
			$this->db->trans_commit();
			return 1;
		}
	}
	
	function delete($id) {
		$this->db->trans_begin();
        $this->db->delete($this->table, array('SQuoNumber' => $id));
		
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return 0;
		}
		else {
			$this->db->trans_commit();
			return 1;
		}
	}
}