<?php

namespace App\Controllers;

use App\Models\KeuanganModel;

class Keuangan extends BaseController
{
    protected $keuanganM;

    public function __construct()
    {
        $this->keuanganM = new KeuanganModel();
    }

    public function index()
    {
        $db = \Config\Database::connect();

        // Menggunakan LEFT JOIN agar transaksi manual tetap tampil
        $builder = $db->table('keuangan');
        $builder->select('keuangan.*, petani.nama_petani');
        $builder->join('transaksi_penebusan', 'transaksi_penebusan.id_transaksi = keuangan.id_referensi_transaksi', 'left');
        $builder->join('petani', 'petani.id_petani = transaksi_penebusan.id_petani', 'left');
        $builder->orderBy('keuangan.tanggal', 'ASC');
        $builder->orderBy('keuangan.id_kas', 'ASC');

        $kas = $builder->get()->getResultArray();

        $data = [
            'title' => 'Buku Kas Umum & Laporan Keuangan',
            'kas'   => $kas
        ];
        return view('keuangan/index', $data);
    }

    public function cetak()
    {
        $db = \Config\Database::connect();

        $builder = $db->table('keuangan');
        $builder->select('keuangan.*, petani.nama_petani');
        $builder->join('transaksi_penebusan', 'transaksi_penebusan.id_transaksi = keuangan.id_referensi_transaksi', 'left');
        $builder->join('petani', 'petani.id_petani = transaksi_penebusan.id_petani', 'left');
        $builder->orderBy('keuangan.tanggal', 'ASC');
        $builder->orderBy('keuangan.id_kas', 'ASC');

        $kas = $builder->get()->getResultArray();

        $data = [
            'title' => 'Cetak Laporan Buku Kas',
            'kas'   => $kas
        ];

        return view('keuangan/cetak', $data);
    }
}
