<?php
//===GET===
$sql['tahun_aktif'] = "
SELECT
   thanggarId AS id
FROM
   tahun_anggaran
WHERE
   thanggarIsAktif = 'Y'
";

$sql['combo_tahun_anggaran'] = "
SELECT
   thanggarId AS id,
   thanggarNama AS name
FROM
   tahun_anggaran
";

$sql['combo_program'] = "
SELECT
   programId AS id,
   programNama AS name
FROM
   program_ref
";

$sql['combo_jenis_kegiatan'] = "
SELECT
   jeniskegId AS id,
   jeniskegNama AS name
FROM
   jenis_kegiatan_ref
";

$sql['combo_kegiatan'] = "
SELECT
   CONCAT(LPAD(programNomor,2,0),'.',LPAD(subprogJeniskegId,2,0),'.',LPAD(subprogNomor,2,0),'.',LPAD(kegrefNomor,2,0)) AS id,
   kegrefNama AS name
FROM
   program_ref
   JOIN sub_program ON (subprogProgramId = programId)
   JOIN kegiatan_ref ON (kegrefSubprogId = subprogId)
   JOIN kegiatan_detail ON (kegdetKegrefId = kegrefId)
WHERE
   kegdetIsAprove = 'Ya'
GROUP BY
   id
ORDER BY
   programNomor, subprogNomor, kegrefNomor
";

$sql['pop_up_list_count'] = "
SELECT
   COUNT(*) AS total
FROM
   (
      SELECT
         CONCAT(LPAD(programNomor,2,0),'.',LPAD(subprogJeniskegId,2,0),'.',LPAD(subprogNomor,2,0),'.',LPAD(kegrefNomor,2,0)) AS kodeKegiatan
      FROM
         program_ref
         JOIN sub_program ON (subprogProgramId = programId)
         JOIN kegiatan_ref ON (kegrefSubprogId = subprogId)
         JOIN kegiatan_detail ON (kegdetKegrefId = kegrefId)
         JOIN kegiatan ON (kegId = kegdetKegId)
         JOIN jenis_kegiatan_ref ON (jeniskegId = subprogJeniskegId)
         LEFT JOIN rencana_pengeluaran ON (rncnpengeluaranKegdetId = kegdetId)
         LEFT JOIN komponen_kegiatan ON (kompkegKegrefId = kegrefId)
      WHERE
         kegdetIsAprove = 'Ya' AND
         (kegThanggarId = %s OR 'all' = %s) AND
         (programId = %s OR 'all' = %s) AND
         (subprogJeniskegId = %s OR 'all' = %s)
      GROUP BY
         kodeKegiatan
      ORDER BY
         programNomor, subprogNomor, kegrefNomor
   ) a
";

$sql['pop_up_list'] = "
SELECT
   CONCAT(LPAD(programNomor,2,0),'.',LPAD(subprogJeniskegId,2,0),'.',LPAD(subprogNomor,2,0),'.',LPAD(kegrefNomor,2,0)) AS kodeKegiatan,
   programNama AS namaProgram,
   subprogNama AS namaSubProgram,
   jeniskegNama AS jenisKegiatan,
   kegrefNama AS namaKegiatan,
   SUM(rncnpengeluaranKomponenNominal * rncnpengeluaranSatuan) AS nominal,
   SUM(rncnpengeluaranKomponenNominalAprove * rncnpengeluaranSatuanAprove) - IFNULL(transdtpengadaanNominal,0) AS nominal_approve,
   kegThanggarId
FROM
   program_ref
   JOIN sub_program ON (subprogProgramId = programId)
   JOIN kegiatan_ref ON (kegrefSubprogId = subprogId)
   JOIN kegiatan_detail ON (kegdetKegrefId = kegrefId)
   JOIN kegiatan ON (kegId = kegdetKegId)
   JOIN jenis_kegiatan_ref ON (jeniskegId = subprogJeniskegId)
   LEFT JOIN rencana_pengeluaran ON (rncnpengeluaranKegdetId = kegdetId)
   LEFT JOIN komponen_kegiatan ON (kompkegKegrefId = kegrefId)
   LEFT JOIN 
   (
      SELECT
         transdtpengadaanMAK,
         SUM(transdtpengadaanNominal) AS transdtpengadaanNominal,
         transThanggarId
      FROM
         transaksi_detail_pengadaan
         JOIN transaksi ON (transId = transdtpengadaanTransId)
      GROUP BY
         transdtpengadaanMAK, transThanggarId
   ) terpakai ON (transdtpengadaanMAK = CONCAT(LPAD(programNomor,2,0),'.',LPAD(subprogJeniskegId,2,0),'.',LPAD(subprogNomor,2,0),'.',LPAD(kegrefNomor,2,0)) AND transThanggarId = kegThanggarId)
WHERE
   kegdetIsAprove = 'Ya' AND
   (kegThanggarId = %s OR 'all' = %s) AND
   (programId = %s OR 'all' = %s) AND
   (subprogJeniskegId = %s OR 'all' = %s)
GROUP BY
   kodeKegiatan
ORDER BY
   kodeKegiatan
";
?>
