<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Uom_model extends CI_Model {

    //UOM GRID
    function getuomgrid() {
        $page = $this->input->post('page');
        $limit = $this->input->post('rows'); // get how many rows we want to have into the grid
        $sidx = $this->input->post('sidx'); // get index row - i.e. user click to sort
        $sord = $this->input->post('sord'); // get the direction
        
    //Default values
        if (!$sidx)
            $sidx = 'ID';
        if (!$sord)
            $sord = 'ASC';
        if (!$page)
            $page = 1;
        if (!$limit)
            $limit = 25;
        $search = $this->input->post('_search');

        if($search=='true')
        {
        $searchField = $this->input->post('searchField');
        $searchOper = $this->input->post('searchOper');
        $searchString = $this->input->post('searchString');
        $oper='';
        if($searchOper=='cn')
            $searchQry = 'UNITS.'.$searchField . ' LIKE "%' . $searchString . '%"';
        else if($searchOper=='eq')
            $searchQry = 'UNITS.'.$searchField . ' = "' . $searchString .'"';

        $this->db->where($searchQry);
        }
        //Total Returned Rows Count
        $this->db->from('UNITS');
        $count = $this->db->count_all_results();

    //Total Pages
        if ($count > 0 && $limit > 0) {
            $total_pages = ceil($count / $limit);
            //Set start record number
        $start = $limit * $page - $limit;
        }
        else {
            $total_pages = 0;
            //Set start record number
        $start = 0;

        }

    //Check requested page
        if ($page > $total_pages)
            $page = $total_pages;

    //Format data for jqGrid output
        $data['page'] = "" . $page . "";
        $data['total'] = "" . $total_pages . "";
        $data['records'] = "" . $count . "";

    //Fetch data
        $this->db->select("ID, NAME, SYMBOL");
        if($search=='true') $this->db->where($searchQry);
        $this->db->order_by($sidx, $sord);
        $this->db->limit($limit, $start);
        $rows = $this->db->get("UNITS")->result_array();

    //Format data
        $i = 0;
        foreach ($rows as $row) {
            $data['rows'][$i]['id'] = $i + 1;
            $data['rows'][$i]['cell'] = array($row['ID'], $row['NAME'], $row['SYMBOL']);
            $i++;
        }
        return json_encode($data);
    }

    
    //RETURN UOM BY ID, FOR EDIT PAGE
    function getuombyid($id) {
        $this->db->where('ID', $id);
        return $this->db->get('UNITS')->result();
    }

  //FOR UOM AUTOCOMPLETE
    function getuomautocomplete($term) {
       $this->db->select('ID, NAME');
        $where = "NAME LIKE '". $term . "%'";
         $this->db->where($where);
         return $this->db->get("UNITS")->result();
    }

    function getsymbolautocomplete($term) {
       $this->db->select('ID, SYMBOL');
        $where = "SYMBOL LIKE '". $term . "%'";
         $this->db->where($where);
         return $this->db->get("UNITS")->result();
    }
    function getuomcombo() {
        $this->db->from('UNITS');
        $this->db->select('ID, NAME');
	return $this->db->get()->result();
    }
    function delete($id) {
        return ($this->db->delete('UNITS', array('ID' => $id))) ? true : false;
     }
    function add($form_data){
        $this->db->insert('UNITS', $form_data);
    $fdata = array('msgt' => 'S', 'msg' => 'Unit deleted successfully.');
                    $this->session->set_flashdata($fdata);
		if ($this->db->affected_rows() == '1')
		{
                      $fdata = array('msgt' => 'S', 'msg' => 'Unit deleted successfully.');
                    $this->session->set_flashdata($fdata);
		}
        return FALSE;
     }
    function update($id, $form_data){
        $this->db->where('ID', $id);
        $this->db->update('UNITS', $form_data);
		if ($this->db->affected_rows() == '1')
		{
			return TRUE;
		}
        return FALSE;
     }
}
