<?php

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

class Glledger_accounts_model extends CI_Model {

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

    /**
     * This funtion takes id as a parameter and will fetch the record.
     * If id is not provided, then it will fetch all the records form the table.
     * @param int $id
     * @return mixed
     */

      //we will use the updating INFO FO
      public function update($id)  
      {  
            $this->db->select()->from('glledger_account');
            if ($id != null) {
                $this->db->where('id', $id);
            } else {
                $this->db->order_by('id');
            }
            $query = $this->db->get();
			
         return $query;  
      }
   
      public function select()  
      {  
         //data is retrive from this query  
      // $query = "SELECT * FROM `glledger_account` INNER JOIN glaccount_types on glledger_account.gl_code=glaccount_types.name ORDER BY glledger_account.gl_code ASC";
       
		$this->db->select()->from('glledger_account');
		$this->db->order_by('gl_code', 'ASC');
        $query = $this->db->get(); 		  
         return $query;  
      }
      
	  public function selects()  
      {  
         //data is retrive from this query  
      // $query = "SELECT * FROM `glledger_account` INNER JOIN glaccount_types on glledger_account.gl_code=glaccount_types.name ORDER BY glledger_account.gl_code ASC";
       
		$this->db->select()->from('glledger_account');
		$this->db->order_by('gl_code', 'ASC');
        $query = $this->db->get(); 		  
         return $query;  
      }
    /**
     * This function will delete the record based on the id
     * @param $id
     */
    public function remove($id) {
        $this->db->trans_begin();
        $this->db->where('id', $id);
        $this->db->delete('glledger_account'); //class record delete.

       // $this->db->where('group_id', $id);
       // $this->db->delete('uniform_group'); //class_sections record delete.

        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
        } else {
            $this->db->trans_commit();
        }
        return TRUE;
    }

    /**
     * This function will take the post data passed from the controller
     * If id is present, then it will do an update
     * else an insert. One function doing both add and edit.
     * @param $data
     */
    public function add($data) {
        if (isset($data['id'])) {
            $this->db->where('id', $data['id']);
            $this->db->update('glledger_account', $data);
        } else {
            $this->db->insert('glledger_account', $data);
        }
    }

    function check_data_exists($data) {
        $this->db->where('groups', $data);

        $query = $this->db->get('uniform_group');
        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return FALSE;
        }
    }

    public function class_exists($str) {

        $class = $this->security->xss_clean($str);
        $res = $this->check_data_exists($class);

        if ($res) {
            $pre_class_id = $this->input->post('pre_class_id');
            if (isset($pre_class_id)) {
                if ($res->id == $pre_class_id) {
                    return TRUE;
                }
            }
            $this->form_validation->set_message('class_exists', 'Record already exists');
            return FALSE;
        } else {
            return TRUE;
        }
    }
}
