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

class Unit_model extends CI_Model
{
    public function insert($model)
    {
        $this->db->insert('unit_master', $model);
        return $this->db->insert_id();
    }

    public function selectall()
    {
        $this->db->select('id, unit_name, status');
        $this->db->where('is_deleted', 0);
        $this->db->order_by('id', 'DESC');
        return $this->db->get('unit_master')->result();
    }

    public function getbyid($id)
    {
        $this->db->where('id', $id);
        return $this->db->get('unit_master')->result();
    }

    public function update($id, $data)
    {
        return $this->db->update('unit_master', $data, ['id' => $id]);
    }

    public function delete($id)
    {
        // Hard delete. unit FK वर ON DELETE CASCADE नाही,
        // म्हणून हा record कुठे वापरला असेल (product_master) तर DB exception टाकेल.
        $this->db->where('id', $id);
        $this->db->delete('unit_master');
        return $this->db->affected_rows() > 0;
    }

    public function getActive()
    {
        $this->db->where(['status' => 1, 'is_deleted' => 0]);
        return $this->db->get('unit_master')->result();
    }

    public function count($where = []) {
        if (!empty($where)) $this->db->where($where);
        return $this->db->count_all_results('unit_master');
    }
}
