<?php

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

class Uniformpos_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
     */
    public function get($id = null) {

        $userdata = $this->customlib->getUserData();
        $role_id = $userdata["role_id"];
        $carray = array();

            $this->db->select()->from('values');
            if ($id != null) {
                $this->db->where('id', $id);
            } else {
                $this->db->order_by('id');
            }
            $query = $this->db->get();
            if ($id != null) {
                $grouplist = $query->row_array();
            } else {
                $grouplist = $query->result_array();
            }
            // echo $this->db->last_query();
            // exit();
        return $grouplist;
    }

      //we will use the updating INFO FO
      public function update($id)  
      {  
            $this->db->select()->from('uniformpos');
            if ($id != null) {
                $this->db->where('id', $id);
            } else {
                $this->db->order_by('id');
            }
            $query = $this->db->get();
			
         return $query;  
      }
      

      //we will use the select function  
      public function select()  
      {  
         //data is retrive from this query  
         $query = $this->db->get('uniformpostemp');  
         return $query;  
      }
      public function selectrec2()  
      {  
         //data is retrive from this query  
         $query = $this->db->get('uniformpostemprec');  
         return $query;  
      }
      public function selectonunifstockmgnt()  
      {  
         //data is retrive from this query  
         $query = $this->db->get('unif_stock_mgnt');  
         return $query;  
      }

      public function selectuniformpos()  
      {  
         //data is retrive from this query  
         $query = $this->db->get('uniformpos');  
         return $query;  
      }

      public function selectuniformposreport()  
      {  
         //data is retrive from this query  
         $query = $this->db->get('unif_stock_mgnt');  
         return $query;  
      }
	  
  
	  public function selectuniformposmvmnt()  
      {  
         //data is retrive from this query  
         $query = $this->db->get('unif_stock_mvmnt');  
         return $query;  
      }

      public function selectuniformposlevels()  
      {  
         //data is retrive from this query  
         $query = $this->db->get('unif_stock_level');  
         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('uniformpos'); //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('uniformpos', $data);
        } else {
            $this->db->insert('uniformpos', $data);
        }
    }
   

    public function addrec($data) {
        if (isset($data['id'])) {
            $this->db->where('id', $data['id']);
            $this->db->update('unifreceipting', $data);
        } else {
            $this->db->insert('unifreceipting', $data);
        }
    }

    function check_data_exists($data) {
        $this->db->where('itemcode', $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;
        }
    }
    public function getItemCodesstockmvmnt() {
        $this->db->select('DISTINCT(itemcode)', false);
        $this->db->from('uniformitem');
        $query = $this->db->get();
        return $query->result_array(); // Return an array of item codes
    }
    public function getItemCodesstocklevel() {
        $this->db->select('DISTINCT(itemcode)', false);
        $this->db->from('uniformitem');
        $query = $this->db->get();
        return $query->result_array(); // Return an array of item codes
    }
    public function getItemCodes() {
        $this->db->select('DISTINCT(itemcode)', false);
        $this->db->from('uniformitem');
        $query = $this->db->get();
        return $query->result_array(); // Return an array of item codes
    }
     // Fetch filtered data based on item code range
    
     

     // Fetch filtered stock level data based on item range and date
public function getFilteredStocklevelData($filter_start_item, $filter_end_item, $filter_from_date, $filter_to_date) {
    $this->db->select('*');
    $this->db->from('unif_stock_level');

    // Filter by item codes
    if (!empty($filter_start_item)) {
        $this->db->where('itemcode >=', $filter_start_item);
    }

    if (!empty($filter_end_item)) {
        $this->db->where('itemcode <=', $filter_end_item);
    }

    // Filter by date
    if (!empty($filter_from_date)) {
        $this->db->where('date >=', $filter_from_date);
    }

    if (!empty($filter_to_date)) {
        $this->db->where('date <=', $filter_to_date);
    }

    $this->db->order_by('itemcode', 'ASC'); // optional: order by item code
    $query = $this->db->get();
    return $query; // returns CI query object
}

    
    public function getFilteredStockmvmntData($filter_from_date,$filter_to_date,$filter_start_item, $filter_end_item) {
        $this->db->select('*');
        $this->db->from('unif_stock_mvmnt');
         // Apply filters conditionally
    if (!empty($filter_from_date)) {
        $this->db->where('date >=', $filter_from_date);
    }
    if (!empty($filter_to_date)) {
        $this->db->where('date <=', $filter_to_date);

    }
    if (!empty($filter_start_item)) {
        $this->db->where('itemcode >=', $filter_start_item);
    }

    if (!empty($filter_end_item)) {
        $this->db->where('itemcode <=', $filter_end_item);
    }
    
        $query = $this->db->get();
        return $query;
    }

    
    public function getFilteredStockmgntData($filter_from_date,$filter_to_date,$filter_start_item, $filter_end_item) {
        $this->db->select('*');
        $this->db->from('unif_stock_mgnt');
         // Apply filters conditionally
    if (!empty($filter_from_date)) {
        $this->db->where('created_date >=', $filter_from_date);
    }
    if (!empty($filter_to_date)) {
        $this->db->where('created_date <=', $filter_to_date);

    }
    if (!empty($filter_start_item)) {
        $this->db->where('itemcode >=', $filter_start_item);
    }

    if (!empty($filter_end_item)) {
        $this->db->where('itemcode <=', $filter_end_item);
    }
    
        $query = $this->db->get();
        return $query;
    }

    //for unifomr receipting reference field to select from the uniform pos table 
    public function get_references() {
        $this->db->select('batch_num, itemcode, description');
        $this->db->from('uniformpos');
        $this->db->order_by('batch_num', 'ASC'); // Order by batch number for better readability
        $query = $this->db->get();
    
        return $query->result();
    }

    public function addcb_to_statement($data) {
        if (isset($data['id'])) {
            $this->db->where('id', $data['id']);
            $this->db->update('uniformstatement', $data);
        } else {
            $this->db->insert('uniformstatement', $data);
        }
    }	

    //for the uniformstatement module
    public function customer_search($name, $nameto, $from, $to) {
        $this->db->select('*');
        $this->db->from('uniformstatement');
        $this->db->where('student_id >= ', $name);
        $this->db->where('student_id <= ', $nameto);
	    $this->db->where('created_date >= ', $from);
		$this->db->where('created_date <= ', $to);
        $this->db->order_by('created_date', 'ASC');
		$query = $this->db->get();
			
        return $query;
    }
    
    public function selectstds() {
        $this->db->order_by('admission_no', 'ASC');
        $query = $this->db->get('students');  
        return $query; 
    }

}



