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

class RencanaPembelian_model extends CI_Model {

    public function get_all($status = null) {
        $this->db->select('rencana_pembelian.*, users.nama_lengkap as pengaju');
        $this->db->join('users', 'users.id = rencana_pembelian.diajukan_oleh', 'left');
        if($status) $this->db->where('rencana_pembelian.status', $status);
        $this->db->order_by('created_at', 'DESC');
        return $this->db->get('rencana_pembelian')->result();
    }

    public function get_by_id($id) {
        $this->db->select('rencana_pembelian.*, users.nama_lengkap as pengaju');
        $this->db->join('users', 'users.id = rencana_pembelian.diajukan_oleh', 'left');
        return $this->db->get_where('rencana_pembelian', ['rencana_pembelian.id' => $id])->row();
    }

    public function insert($data) {
        return $this->db->insert('rencana_pembelian', $data);
    }

    public function update($id, $data) {
        $this->db->where('id', $id);
        return $this->db->update('rencana_pembelian', $data);
    }

    public function delete($id) {
        $this->db->where('id', $id);
        return $this->db->delete('rencana_pembelian');
    }
}