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

class Uom extends MY_Controller {

	function __construct() {
		parent::__construct();
	}

    function index()
    {
              $data['template'] = 'settings/uom_view';
             $this->load->view('template',$data);
    }

    //GET UOM GRID
    public function getuomgrid() {
            $this->load->model('settings/uom_model');
            echo $this->uom_model->getuomgrid();
    }

    //FOR UNITS AUTOCOMPLETE
    function getuomautocomplete(){
            $term = $this->input->post('term',TRUE);
            $this->load->model('settings/uom_model');
            $rows = $this->uom_model->getuomautocomplete($term);
            $keywords = array();
            foreach ($rows as $row)
                array_push($keywords, $row->NAME);
            echo json_encode($keywords);
     }
    //FOR SYMBOLS AUTOCOMPLETE
    function getsymbolautocomplete(){
            $term = $this->input->post('term',TRUE);
            $this->load->model('settings/uom_model');
            $rows = $this->uom_model->getsymbolautocomplete($term);
            $keywords = array();
            foreach ($rows as $row)
                array_push($keywords, $row->SYMBOL);
            echo json_encode($keywords);
     }

        public function getuom() {
        $data['data'] = $this->common->getAll('UNITS');
        echo json_encode($data);
    }
    

       public function adduom() {
        $id = $this->input->post('id');
        $data = array( 
            'ID' => uniqid(),       
            'NAME' => $this->input->post('name'),
            'SYMBOL' => $this->input->post('symbol')
        );
       if($id == '')
       { 
           $existdata = $this->common->existdata('UNITS',$data);
          // print_r( $existdata);die();
           if(!empty($existdata))
           {
           
             $this->session->set_flashdata('success_msg', 'The record is already exist');
           }
           else
           {

               $this->common->insert('UNITS',$data);
               $this->session->set_flashdata('success_msg', 'The record is added successfuly.');
           }
       
       }
        else
       {
          $where = array('ID' => $id);
          $this->session->set_flashdata('success_msg', 'The record has been updated successfuly.');
          $this->common->update('UNITS',$where,$data);
       }
        redirect('settings/uom');
 
    }
    
      public function edituom($id) {
        $where = array('ID' => $id,);
        $data = $this->common->getWhere('UNITS',$where);

        echo json_encode($data);
 
    }
       public function deleteuom($id) {

        $where = array('ID' => $id);  
        $this->common->delete('UNITS',$where);
        $this->session->set_flashdata('success_msg', 'The record has been deleted successfuly.');
        redirect('settings/uom');
 
    }

}

