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

class Neracasaldo extends CI_Controller {
    
    public function __construct()
    {
        parent::__construct();
        $this->load->model('DynamicModel', 'dm');

        if (!$this->session->userdata('logged_in')) {
            redirect('auth');
        }
    }

    public function index()
    {

        if($this->session->userdata('role') != 'Admin'){
            show_error('Akses ditolak', 403);
        }

        $data = ["active_menu" => "neraca_saldo"];
        $this->load->view('partials/header', $data);
        $this->load->view('laporan/neraca_saldo');
        $this->load->view('partials/footer');
    }

    // =========================
    // HITUNG LABA BERJALAN (REALTIME)
    // =========================
    private function get_laba_berjalan()
    {
        $this->db->select('
            accounts.tipe,
            COALESCE(SUM(journal_details.debit),0) as debit,
            COALESCE(SUM(journal_details.kredit),0) as kredit
        ');
        $this->db->from('accounts');
        $this->db->join('journal_details','journal_details.account_id = accounts.id','LEFT');
        $this->db->where_in('accounts.tipe',['revenue','expense']);

        $rows = $this->db->get()->result();

        $revenue = 0;
        $expense = 0;

        foreach ($rows as $r) {
            if ($r->tipe == 'revenue') {
                $revenue += ($r->kredit - $r->debit);
            } else {
                $expense += ($r->debit - $r->kredit);
            }
        }

        return $revenue - $expense;
    }

    public function get_data()
    {
        $this->db->select('
            accounts.kode_akun,
            accounts.nama_akun,
            accounts.tipe,
            accounts.posisi,
            COALESCE(SUM(journal_details.debit),0) as debit,
            COALESCE(SUM(journal_details.kredit),0) as kredit
        ');
        $this->db->from('accounts');
        $this->db->join('journal_details','journal_details.account_id = accounts.id','LEFT');

        // OPTIONAL (best practice)
        $this->db->where('accounts.is_active',1);
        $this->db->where('accounts.kategori','neraca');

        $this->db->group_by('accounts.id');
        $this->db->order_by('accounts.kode_akun','ASC');

        $rows = $this->db->get()->result();

        // =========================
        // HITUNG LABA BERJALAN
        // =========================
        $laba = $this->get_laba_berjalan();

        foreach ($rows as $r) {

            // ================= SALDO NORMAL
            if ($r->posisi == 'debit') {
                $saldo = $r->debit - $r->kredit;
            } else {
                $saldo = $r->kredit - $r->debit;
            }

            // ================= OVERRIDE LABA BERJALAN
            if ($r->kode_akun == '302') {
                $saldo = $laba;
            }

            // ================= NORMALISASI KE KOLOM
            if ($saldo >= 0 AND $r->posisi == 'debit') {
                $r->saldo_debit = $saldo;
                $r->saldo_kredit = 0;
            } else {
                $r->saldo_debit = 0;
                $r->saldo_kredit = abs($saldo);
            }
        }

        echo json_encode($rows);
    }
}