36ad02a222
Format PDF tersimpan di library semua
209 lines
6.4 KiB
PHP
209 lines
6.4 KiB
PHP
<?php
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
class Generateneracasaldo 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;
|
|
}
|
|
|
|
// =========================
|
|
// AMBIL DATA NERACA SALDO (DIPAKAI BERSAMA OLEH get_data() & generatepdf())
|
|
// =========================
|
|
private function get_neracasaldo_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();
|
|
|
|
$result = [];
|
|
$total_debit = 0;
|
|
$total_kredit = 0;
|
|
|
|
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);
|
|
}
|
|
|
|
$total_debit += $r->saldo_debit;
|
|
$total_kredit += $r->saldo_kredit;
|
|
|
|
$result[] = [
|
|
'kode' => $r->kode_akun,
|
|
'nama' => $r->nama_akun,
|
|
'saldo_debit' => $r->saldo_debit,
|
|
'saldo_kredit' => $r->saldo_kredit,
|
|
];
|
|
}
|
|
|
|
return [
|
|
'rows' => $rows,
|
|
'data' => $result,
|
|
'total_debit' => $total_debit,
|
|
'total_kredit' => $total_kredit,
|
|
];
|
|
}
|
|
|
|
// =========================
|
|
// GET DATA NERACA SALDO (JSON, untuk AJAX/datatable)
|
|
// =========================
|
|
public function get_data()
|
|
{
|
|
$result = $this->get_neracasaldo_data();
|
|
|
|
echo json_encode($result['rows']);
|
|
}
|
|
|
|
// =========================
|
|
// PDF GENERATE — NERACA SALDO
|
|
// =========================
|
|
public function generatepdf()
|
|
{
|
|
if ($this->session->userdata('role') != 'Admin') {
|
|
show_error('Akses ditolak', 403);
|
|
}
|
|
|
|
if (ob_get_length()) {
|
|
ob_end_clean();
|
|
}
|
|
|
|
// =============================================================
|
|
// AMBIL DATA
|
|
// =============================================================
|
|
$neracasaldo = $this->get_neracasaldo_data();
|
|
|
|
// =============================================================
|
|
// DATA PERUSAHAAN
|
|
// =============================================================
|
|
$company = $this->db->get('pengaturan')->row();
|
|
|
|
// =============================================================
|
|
// BUILD STRING PERIODE
|
|
// =============================================================
|
|
$periodeStr = 'Per Tanggal : ' . date('d-m-Y');
|
|
|
|
// =============================================================
|
|
// LOAD LIBRARY PDF
|
|
// =============================================================
|
|
require_once(APPPATH . 'third_party/fpdf/fpdf.php');
|
|
require_once(APPPATH . 'libraries/PDF_Neracasaldo.php');
|
|
|
|
$pdf = new PDF ('P', 'mm', 'A4');
|
|
$pdf->SetMargins(10, 10, 10);
|
|
|
|
$pdf->setHeaderData(
|
|
$company->nama_perusahaan,
|
|
$company->alamat_perusahaan,
|
|
FCPATH . 'uploads/img/logo-ljn.png'
|
|
);
|
|
|
|
$pdf->setInfoLaporan($periodeStr);
|
|
|
|
$pdf->AddPage('P', 'A4');
|
|
$pdf->NeracaSaldoTable(
|
|
$neracasaldo['data'],
|
|
$neracasaldo['total_debit'],
|
|
$neracasaldo['total_kredit']
|
|
);
|
|
$pdf->NeracaSaldoRingkasan(
|
|
$neracasaldo['total_debit'],
|
|
$neracasaldo['total_kredit']
|
|
);
|
|
$pdf->Pembuat('Tito Ngudiana', 'Finance Department');
|
|
|
|
// =============================================================
|
|
// OUTPUT
|
|
// =============================================================
|
|
$filename = 'Neraca_Saldo_' . date('YmdHis') . '.pdf';
|
|
$pdf->Output('I', $filename);
|
|
exit;
|
|
}
|
|
} |