<?php

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

class Group_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('uniform_group');
            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 select function  
      public function select_group()  
      {  
         //data is retrive from this query  
         $query = $this->db->get('uniform_group');  
         return $query;  
      }
      public function select()  
      {  
         //data is retrive from this query  
      
		$this->db->select()->from('uniformitem');
		$this->db->order_by('itemcode', 'ASC');
        $query = $this->db->get(); 		  
         return $query;  
      }   //we will use the updating INFO FO
      public function update($id)  
      {  
            $this->db->select()->from('uniform_group');
            if ($id != null) {
                $this->db->where('id', $id);
            } else {
                $this->db->order_by('id');
            }
            $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('uniform_group'); //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('uniform_group', $data);
        } else {
            $this->db->insert('uniform_group', $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;
        }
    }



    
    public function addIntoUniformposrec($data) {
        
        $this->db->insert('uniformposrec', $data);
    
}
    
    public function addIntoUniformpostemp($data) {
        
        $this->db->insert('uniformpostemp', $data);
    
}

public function addIntoUniformpostemprec($data) {
        
    $this->db->insert('uniformpostemprec', $data);

}

public function addIntoUnifrectemp($data) {
        
    $this->db->insert('unifreceipttemp', $data);

}

    public function addInvoiceNo() {
        $data = [
            'Status'      => "0",];
        $this->db->insert('invoicegenaration', $data);
    
}
public function addReceiptNo() {
    $data = [
        'Status'      => "0",];
    $this->db->insert('receiptgenaration', $data);

}

public function validateselectInvoiceNo()  
{  
    $query = $this->db->query("SELECT count(*) as inv FROM invoicegenaration where Status='1'");

    if ($query->num_rows() > 0) {
        return $query->row()->inv; // Replace 'invoice_no' with your actual column name
    } else {
        return "null"; // Return null if no result found
    }
   // return $query->result();
}  
public function validateselectReceiptNo()  
{  
    $query = $this->db->query("SELECT count(*) as rec FROM receiptgenaration where Status='1'");

    if ($query->num_rows() > 0) {
        return $query->row()->rec; // Replace 'invoice_no' with your actual column name
    } else {
        return "null"; // Return null if no result found
    }
   // return $query->result();
}
public function selectSID()  
{  
    $query = $this->db->query("SELECT student_id FROM uniformpostemp limit 1");

    if ($query->num_rows() > 0) {
        return $query->row()->student_id; // Replace 'invoice_no' with your actual column name
    } else {
        return "UnKnown"; // Return null if no result found
    }
   // return $query->result();
}  

public function selectSIDrec()  
{  
    $query = $this->db->query("SELECT student_id FROM uniformpostemprec limit 1");

    if ($query->num_rows() > 0) {
        return $query->row()->student_id; // Replace 'invoice_no' with your actual column name
    } else {
        return "UnKnown"; // Return null if no result found
    }
   // return $query->result();
} 



public function proccessData() {
  
    $this->db->query("
    INSERT INTO `uniformpos` (`student_id`,`name`, `itemcode`, `description`,`narration`,`warehouse`,`originalqnty`,`quantity`, `amount`, `payment`, `created_date`, `cost`, `discount`, `batch_num`)
    SELECT `student_id`, `name`,`itemcode`, `description`,`narration`,`warehouse`,`originalqnty`, `quantity`, `amount`, `payment`, `created_date`, `cost`, `discount`, `batch_num`
    FROM `uniformpostemp`
");

$this->db->query("DELETE FROM `uniformpostemp` ");
$data = [
    'Status' => "1",];
$this->db->insert('invoicegenaration', $data);

}

public function proccessDatarec() {
  
    $this->db->query("
 INSERT INTO `uniformposrec` (`student_id`, `name`,`gl_account`,`description`,`narration`, `reference`, `amount`, `payment`,`total_amount`, `created_date`, `discount`, `batch_num`)
SELECT `student_id`,`name`, `gl_account`,`description`,`narration`, `reference`, `amount`, `payment`,`total_amount`, `created_date`, `discount`, `batch_num`
FROM `uniformpostemprec`
");

$this->db->query("DELETE FROM `uniformpostemprec` ");
$data = [
    'Status'=> "1",];
$this->db->insert('receiptgenaration', $data);

}

}
