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

class expense_model extends CI_Model {

    public function create($options)
    {
        
        $this->db->insert('kns1_expense', $options);
        return $this->db->insert_id();
    }

    public function get_all($limit = NULL, $offset = NULL)
    {
        $this->db->order_by('id', 'DESC');
        $query = $this->db->get('kns1_expense', $limit, $offset);
        return $query->result();
    }

    public function getNameBy($id)
    {
        $this->db->select('name');
        $this->db->where('id', $id);
        $this->db->where('YEAR(`created_at`)', $_SESSION['qurbani_year']);
        $query = $this->db->get('kns1_expense');
        if($query->row())
        return $query->row()->name;
    }

    public function get_by($id)
    {
        $this->db->where('id', $id);
        $this->db->where('YEAR(`created_at`)', $_SESSION['qurbani_year']);
        $query = $this->db->get('kns1_expense');
        return $query->row();
    }

    public function update($id, $options)
    {
        $this->db->where('id', $id);
        $this->db->where('YEAR(`created_at`)', $_SESSION['qurbani_year']);
        $this->db->update('kns1_expense', $options);
        return $id;
    }

    public function destroy($id)
    {
        $this->db->where('id', $id);
        $this->db->where('YEAR(`created_at`)', $_SESSION['qurbani_year']);
        $this->db->delete('kns1_expense');
        return $this->db->affected_rows();
    }
}