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

class Kas_model extends CI_Model {

    // ─────────────────────────────────────────────
    // READ
    // ─────────────────────────────────────────────

    public function get_all() {
        $this->db->select('kas_rt.*, users.nama_lengkap as nama_petugas, mk.nama as nama_kas, mk.kode as kode_kas, mk.warna as warna_kas, mk.icon as icon_kas');
        $this->db->from('kas_rt');
        $this->db->join('users', 'users.id = kas_rt.input_oleh', 'left');
        $this->db->join('master_kas mk', 'mk.id = kas_rt.master_kas_id', 'left');
        $this->db->order_by('kas_rt.tanggal', 'DESC');
        $this->db->order_by('kas_rt.id', 'DESC');
        return $this->db->get()->result();
    }

    public function get_saldo() {
        $this->db->select('SUM(CASE WHEN jenis = "pemasukan" THEN nominal ELSE -nominal END) as saldo');
        $query = $this->db->get('kas_rt');
        return $query->row()->saldo ?: 0;
    }

    public function get_pemasukan_bulan_ini() {
        $this->db->select_sum('nominal');
        $this->db->where('jenis', 'pemasukan');
        $this->db->where('MONTH(tanggal)', date('m'));
        $this->db->where('YEAR(tanggal)', date('Y'));
        $query = $this->db->get('kas_rt');
        return $query->row()->nominal ?: 0;
    }

    public function get_pengeluaran_bulan_ini() {
        $this->db->select_sum('nominal');
        $this->db->where('jenis', 'pengeluaran');
        $this->db->where('MONTH(tanggal)', date('m'));
        $this->db->where('YEAR(tanggal)', date('Y'));
        $query = $this->db->get('kas_rt');
        return $query->row()->nominal ?: 0;
    }

    // ─────────────────────────────────────────────
    // INSERT — otomatis update saldo master_kas
    // ─────────────────────────────────────────────

    public function insert($data) {
        $result = $this->db->insert('kas_rt', $data);

        if ($result && !empty($data['master_kas_id'])) {
            // Gunakan method dari Master_kas_model
            $this->load->model('Master_kas_model');
            $this->Master_kas_model->update_saldo($data['master_kas_id']);
        }

        return $result;
    }

    // ─────────────────────────────────────────────
    // LAPORAN
    // ─────────────────────────────────────────────

    public function get_pemasukan_periode($bulan, $tahun, $master_kas_id = null) {
        $this->db->select('kas_rt.*, users.nama_lengkap as nama_petugas, mk.nama as nama_kas, mk.kode as kode_kas, mk.warna as warna_kas');
        $this->db->from('kas_rt');
        $this->db->join('users', 'users.id = kas_rt.input_oleh', 'left');
        $this->db->join('master_kas mk', 'mk.id = kas_rt.master_kas_id', 'left');
        $this->db->where('kas_rt.jenis', 'pemasukan');
        $this->db->where('MONTH(kas_rt.tanggal)', $bulan);
        $this->db->where('YEAR(kas_rt.tanggal)', $tahun);
        if ($master_kas_id) {
            $this->db->where('kas_rt.master_kas_id', $master_kas_id);
        }
        return $this->db->get()->result();
    }

    public function get_pengeluaran_periode($bulan, $tahun, $master_kas_id = null) {
        $this->db->select('kas_rt.*, users.nama_lengkap as nama_petugas, mk.nama as nama_kas, mk.kode as kode_kas, mk.warna as warna_kas');
        $this->db->from('kas_rt');
        $this->db->join('users', 'users.id = kas_rt.input_oleh', 'left');
        $this->db->join('master_kas mk', 'mk.id = kas_rt.master_kas_id', 'left');
        $this->db->where('kas_rt.jenis', 'pengeluaran');
        $this->db->where('MONTH(kas_rt.tanggal)', $bulan);
        $this->db->where('YEAR(kas_rt.tanggal)', $tahun);
        if ($master_kas_id) {
            $this->db->where('kas_rt.master_kas_id', $master_kas_id);
        }
        return $this->db->get()->result();
    }

    public function get_total_pemasukan_periode($bulan, $tahun, $master_kas_id = null) {
        $this->db->select_sum('nominal');
        $this->db->where('jenis', 'pemasukan');
        $this->db->where('MONTH(tanggal)', $bulan);
        $this->db->where('YEAR(tanggal)', $tahun);
        if ($master_kas_id) {
            $this->db->where('master_kas_id', $master_kas_id);
        }
        $query = $this->db->get('kas_rt');
        return $query->row()->nominal ?: 0;
    }

    public function get_total_pengeluaran_periode($bulan, $tahun, $master_kas_id = null) {
        $this->db->select_sum('nominal');
        $this->db->where('jenis', 'pengeluaran');
        $this->db->where('MONTH(tanggal)', $bulan);
        $this->db->where('YEAR(tanggal)', $tahun);
        if ($master_kas_id) {
            $this->db->where('master_kas_id', $master_kas_id);
        }
        $query = $this->db->get('kas_rt');
        return $query->row()->nominal ?: 0;
    }
}