<?php

// ==== GET ====
$sql['get_combo_jenis_kib'] = "
SELECT
   golbrgId AS id,
	golbrgKibKode AS kode,
   golbrgNama AS name
FROM
   golongan_barang_ref
ORDER BY golbrgNama
";

$sql['get_sumber_dana'] = "
SELECT
   sumberdanaId AS id,
   sumberdanaNama AS name
FROM
   sumber_dana_ref
ORDER BY sumberdanaNama
";

$sql['get_kepemilikan'] = "
SELECT
   kepemilikanbrgId AS id,
   kepemilikanbrgNama AS name
FROM
   kepemilikan_barang_ref
ORDER BY kepemilikanbrgNama
";

$sql['get_lokasi_kampus'] = "
SELECT
   kampusId AS id,
   kampusNama AS name
FROM
   kampus_ref
ORDER BY kampusNama
";

$sql['get_kondisi'] = "
SELECT
   kondisibrgId AS id,
   kondisibrgNama AS name
FROM
   kondisi_barang_ref
ORDER BY kondisibrgNama
";

$sql['get_cbx_satuan'] = "
SELECT
   satuanbrgId AS id,
   satuanbrgNama AS name
FROM
   satuan_barang_ref
ORDER BY satuanbrgNama
";

$sql['get_cbx_pagar'] = "
SHOW COLUMNS FROM inventarisasi_detail LIKE 'invDetPagar'
";

$sql['get_cbx_penguasaan_barang'] = "
SELECT
   statuspengbrgId AS id,
   statuspengbrgNama AS name
FROM
   status_penguasaan_barang_ref
ORDER BY statuspengbrgNama
";

$sql['get_unit_pj'] = "
SELECT
   unitkerjaId AS id,
   unitkerjaNama AS name
FROM
   unit_kerja_ref
ORDER BY unitkerjaKode
";

$sql['get_data_user_by_id'] = "
SELECT 
   UserId AS user_id,
   UserName AS user_name,
   RealName AS real_name,
   a.Description AS description,
   Active AS is_active,
   a.GroupId AS group_id,
   GroupName AS group_name,
   unitkerjaId AS unit_kerja_id,
   unitkerjaNama  AS unit_kerja_nama,
   userunitkerjaRoleId AS role_id
FROM 
   gtfw_user a
   JOIN gtfw_group b ON b.GroupId = a.GroupId
   LEFT JOIN user_unit_kerja ON UserId = userunitkerjaUserId
   LEFT JOIN unit_kerja_ref ON unitkerjaId = userunitkerjaUnitkerjaId
WHERE
   UserId = %s
";

$sql['get_data_user_by_user_name'] = "
SELECT 
   UserId AS user_id,
   UserName AS user_name,
   RealName AS real_name,
   a.Description AS description,
   Active AS is_active,
   a.GroupId AS group_id,
   GroupName AS group_name,
   unitkerjaId AS unit_kerja_id,
   unitkerjaNama  AS unit_kerja_nama,
   userunitkerjaRoleId AS role_id,
   unitkerjaKodeSistem
FROM 
   gtfw_user a
   JOIN gtfw_group b ON b.GroupId = a.GroupId
   LEFT JOIN user_unit_kerja ON UserId = userunitkerjaUserId
   LEFT JOIN unit_kerja_ref ON unitkerjaId = userunitkerjaUnitkerjaId
WHERE
   UserName = %s
";

$sql['get_data_unit_by_unit_name'] = "
SELECT
   *
FROM
   unit_kerja_ref
WHERE
   unitkerjaNama = %s
";

$sql['get_list_biaya_lain'] = "
SELECT
   biayaId AS id,
   biayaName AS name
FROM
   biaya_ref
";

$sql['get_count_inventarisasi'] = "
SELECT
   COUNT(*) AS total
FROM
   inventarisasi_mst a
   LEFT JOIN inventarisasi_detail b ON a.invMstId = b.invDetMstId
   LEFT JOIN barang_ref c ON a.invBarangId = barangId
   LEFT JOIN kampus_ref d ON b.invDetKampus = d.kampusId
   LEFT JOIN unit_kerja_ref e ON b.invDetUnitKerja = e.unitkerjaId 
   LEFT JOIN kepemilikan_barang_ref f ON f.kepemilikanbrgId = b.invDetKepemilikan
   JOIN kondisi_barang_ref h ON h.kondisibrgId = b.invDetKondisiId
   JOIN sub_kelompok_barang_ref i ON barangSubkelbrgId = subkelbrgId
   JOIN kelompok_barang_ref j ON subkelbrgKelbrgId = kelbrgId
   JOIN bidang_barang_ref k ON kelbrgBidangbrgId = bidangbrgId
   JOIN golongan_barang_ref l ON bidangbrgGolbrgId = golbrgId
WHERE
   (l.golbrgId = '%s' OR '%s') AND
   ((a.invTglPembelian > '%s' AND a.invTglPembelian < '%s') OR '%s') AND
   (a.invSumberDana = '%s' OR '%s') AND
   (b.invDetKepemilikan = '%s' OR '%s') AND
   (b.invDetKampus = '%s' OR '%s') AND
   (b.invDetKondisiId = '%s' OR '%s') AND
   (e.unitkerjaId = '%s' OR e.unitkerjaParentId = (SELECT unitkerjaId FROM unit_kerja_ref WHERE unitkerjaNama = '%s') OR '' = '%s') AND
   (
      b.invDetMstLabel LIKE '%s' OR
      invDetKodeBarang LIKE '%s'
   ) AND
   (invDetUnitKerja = '%s' OR e.unitkerjaParentId = '%s' OR '1' = '%s')
";

$sql['get_inventarisasi'] = "
SELECT 
   b.invDetId AS id,
   invDetKodeBarang AS KIB,
   a.invBarangId AS id_barang,
   c.barangKode AS kode_barang,
   b.invDetMstLabel AS nama_aset,
   CONCAT(
      kampusNama,
      IF(
         gedungNama IS NULL,
         '',
         CONCAT(
            '@@',
            gedungNama,
            IF(
               ruangNama IS NULL,
               '',
               CONCAT('@@', ruangNama)
            )
         )
      )
   ) AS lokasi_aset,
   e.unitkerjaNama AS unit_pj,
   LEFT(b.invDetTglPembelian, 4) AS thn_pengadaan,
   IF(
      mstPenystnNilaiPerolehan IS NOT NULL,
      mstPenystnNilaiPerolehan,
      IF(
         b.invDetNilaiPerolehanSatuan IS NULL,
         a.invTotalPerolehan,
         b.invDetNilaiPerolehanSatuan
      )
   ) AS nilai_perolehan,
   IF(
      mstPenystnDisusutkan IS NOT NULL,
      mstPenystnDisusutkan,
      IF(
         b.invDetNilaiPerolehanSatuan IS NULL,
         a.invTotalPerolehan,
         b.invDetNilaiPerolehanSatuan
      )
   ) AS nilai_buku,
   f.kepemilikanbrgNama AS status_kepemilikan,
   h.kondisibrgNama AS kondisi 
FROM
   inventarisasi_mst a 
   LEFT JOIN inventarisasi_detail b 
      ON a.invMstId = b.invDetMstId 
   LEFT JOIN barang_ref c 
      ON a.invBarangId = barangId 
   LEFT JOIN kampus_ref d 
      ON b.invDetKampus = d.kampusId 
   LEFT JOIN unit_kerja_ref e 
      ON b.invDetUnitKerja = e.unitkerjaId 
   LEFT JOIN kepemilikan_barang_ref f 
      ON f.kepemilikanbrgId = b.invDetKepemilikan 
   LEFT JOIN kondisi_barang_ref h 
      ON h.kondisibrgId = b.invDetKondisiId 
   JOIN sub_kelompok_barang_ref i 
      ON barangSubkelbrgId = subkelbrgId 
   JOIN kelompok_barang_ref j 
      ON subkelbrgKelbrgId = kelbrgId 
   JOIN bidang_barang_ref k 
      ON kelbrgBidangbrgId = bidangbrgId 
   JOIN golongan_barang_ref l 
      ON bidangbrgGolbrgId = golbrgId 
   LEFT JOIN gedung 
      ON invDetGedungId = gedungId 
   LEFT JOIN ruang 
      ON invDetRuanganId = ruangId 
   LEFT JOIN penyusutan_brg_mst 
      ON invDetId = mstPenystnBarangId 
WHERE (l.golbrgId = '%s' 
      OR '%s') 
   AND (
      (
         a.invTglPembelian > '%s' 
         AND a.invTglPembelian < '%s'
      ) 
      OR '%s'
   ) 
   AND (a.invSumberDana = '%s' 
      OR '%s') 
   AND (b.invDetKepemilikan = '%s' 
      OR '%s') 
   AND (b.invDetKampus = '%s' 
      OR '%s') 
   AND (b.invDetKondisiId = '%s' 
      OR '%s') 
   AND (
      e.unitkerjaId = '%s' 
      OR e.unitkerjaParentId = 
      (SELECT 
         unitkerjaId 
      FROM
         unit_kerja_ref 
      WHERE unitkerjaNama = '%s') 
      OR '' = '%s'
   ) 
   AND (
      b.invDetMstLabel LIKE '%s' 
      OR invDetKodeBarang LIKE '%s')
   AND (
      invDetUnitKerja = '%s' 
      OR e.unitkerjaParentId = '%s' 
      OR '1' = '%s'
   ) 
ORDER BY invDetKodeBarang
LIMIT %s, %s
";

$sql['get_data_inventarisasi_by_id'] = "
SELECT
   inventarisasi_mst.*,
   inventarisasi_detail.*,
   golbrgId,
   invDetKodeBarang AS invKodeAset,
   barangNama AS namaAset,
   invKondisiBrgKondId,
   CONCAT(gedungNama,IF(ruangNama IS NULL,'',CONCAT(' &raquo; ',ruangNama))) AS lokasiAset,
   unitkerjaNama,
   IF(mstPenystnNilaiPerolehan IS NULL OR mstPenystnNilaiPerolehan = mstPenystnDisusutkan, 0, 1) AS is_penyusutan
FROM 
   inventarisasi_mst
   JOIN inventarisasi_detail ON invDetMstId = invMstId
   JOIN barang_ref ON invBarangId = barangId
   JOIN sub_kelompok_barang_ref ON barangSubkelbrgId = subkelbrgId
   JOIN kelompok_barang_ref ON subkelbrgKelbrgId = kelbrgId
   JOIN bidang_barang_ref ON kelbrgBidangbrgId = bidangbrgId
   JOIN golongan_barang_ref ON bidangbrgGolbrgId = golbrgId
   JOIN kampus_ref ON invDetKampus = kampusId
   LEFT JOIN gedung ON invDetGedungId = gedungId
   LEFT JOIN ruang ON invDetRuanganId = ruangId
   LEFT JOIN inv_kondisi_history ON invDetId = invKondisiBrgBrgDetId
   JOIN unit_kerja_ref ON invDetUnitKerja = unitkerjaId
   LEFT JOIN penyusutan_brg_mst ON invDetId = mstPenystnBarangId
WHERE
   invDetId = '%s'
ORDER BY
   invKondisiBrgTglKondisi DESC
LIMIT 1
";

$sql['get_inventarisasi_detail_by_id'] = "
SELECT
   inventarisasi_mst.*,
   inventarisasi_detail.*,
   invDetKodeBarang AS invKodeAset,
   golbrgNama,
   barangNama AS namaAset,
   invKondisiBrgKondId,
   CONCAT(gedungNama,IF(ruangNama IS NULL,'',CONCAT(' &raquo; ',ruangNama))) AS lokasiAset,
   unitkerjaNama
FROM 
   inventarisasi_mst
   JOIN inventarisasi_detail ON invDetMstId = invMstId
   JOIN barang_ref ON invBarangId = barangId
   JOIN sub_kelompok_barang_ref ON barangSubkelbrgId = subkelbrgId
   JOIN kelompok_barang_ref ON subkelbrgKelbrgId = kelbrgId
   JOIN bidang_barang_ref ON kelbrgBidangbrgId = bidangbrgId
   JOIN golongan_barang_ref ON bidangbrgGolbrgId = golbrgId
   LEFT JOIN gedung ON invDetGedungId = gedungId
   LEFT JOIN ruang ON invDetRuanganId = ruangId
   LEFT JOIN inv_kondisi_history ON invDetId = invKondisiBrgBrgDetId
   JOIN unit_kerja_ref ON invDetUnitKerja = unitkerjaId
WHERE
   invDetId = '%s'
ORDER BY
   invKondisiBrgTglKondisi DESC
LIMIT 1
";

$sql['get_inventarisasi_id_by_filter'] = "
SELECT 
   invDetId AS id 
FROM
   inventarisasi_mst a 
   LEFT JOIN inventarisasi_detail b 
      ON a.invMstId = b.invDetMstId 
   LEFT JOIN barang_ref c 
      ON a.invBarangId = barangId 
   LEFT JOIN kampus_ref d 
      ON b.invDetKampus = d.kampusId 
   LEFT JOIN unit_kerja_ref e 
      ON b.invDetUnitKerja = e.unitkerjaId 
   LEFT JOIN kepemilikan_barang_ref f 
      ON f.kepemilikanbrgId = b.invDetKepemilikan 
   JOIN 
      (SELECT 
         invKondisiBrgBrgDetId,
         invKondisiBrgKondId 
      FROM
         inv_kondisi_history 
      ORDER BY invKondisiBrgTglKondisi DESC) g 
      ON g.invKondisiBrgBrgDetId = b.invDetId 
   JOIN kondisi_barang_ref h 
      ON h.kondisibrgId = g.invKondisiBrgKondId 
   JOIN sub_kelompok_barang_ref i 
      ON barangSubkelbrgId = subkelbrgId 
   JOIN kelompok_barang_ref j 
      ON subkelbrgKelbrgId = kelbrgId 
   JOIN bidang_barang_ref k 
      ON kelbrgBidangbrgId = bidangbrgId 
   JOIN golongan_barang_ref l 
      ON bidangbrgGolbrgId = golbrgId 
WHERE (l.golbrgId = '%s' 
      OR '%s') 
   AND (
      (
         a.invTglPembelian > '%s' 
         AND a.invTglPembelian < '%s'
      ) 
      OR '%s'
   ) 
   AND (a.invSumberDana = '%s' 
      OR '%s') 
   AND (b.invDetKepemilikan = '%s' 
      OR '%s') 
   AND (b.invDetKampus = '%s' 
      OR '%s') 
   AND (g.invKondisiBrgKondId = '%s' 
      OR '%s') 
   AND (e.unitkerjaNama LIKE '%s') 
   AND (
      b.invDetMstLabel LIKE '%s' 
      OR invDetKodeBarang LIKE '%s'
   ) 
GROUP BY invDetId 
ORDER BY invDetKodeBarang
";

$sql['get_biaya_lain_by_id'] = "
SELECT
   invDetBiayaBiayaId as biayaId,
   invDetBiayaNilai as biayaNilai
FROM
   inv_det_biaya
WHERE
   invDetBiayaInvId = %d
";

$sql['get_log_lokasi_header'] = "
SELECT
   invDetKodeBarang AS kodeBarang,
   barangNama,
   a.invDetMstLabel,
   a.invDetMerek,
   a.invDetSpesifikasi
FROM
   inventarisasi_detail a
   JOIN inventarisasi_mst b ON b.invMstId = a.invDetMstId
   JOIN barang_ref c ON invBarangId = barangId
WHERE
   a.invDetId = '%s'
";

$sql['get_log_lokasi_body'] = "
SELECT
   InvHisTanggal,
   kampusNama,
   CONCAT(gedungNama,if(ruangNama IS NULL,'',CONCAT(' &raquo; ',ruangNama))) as lokasiAset
FROM
   inventarisasi_lokasi_history a
   JOIN kampus_ref b ON kampusId = invHisKampusId
   LEFT JOIN gedung ON gedungId = invHisGedungId
   LEFT JOIN ruang ON ruangId =InvHisRuangId
WHERE
   a.invHisInvDetId = '%s'
";

$sql['get_log_kondisi_body'] = "
SELECT
   invKondisiBrgTglKondisi,
   kondisibrgNama
FROM
   inv_kondisi_history a
   JOIN kondisi_barang_ref b ON kondisibrgId = invKondisiBrgKondId
WHERE
   a.invKondisiBrgBrgDetId = '%s'
";

$sql['get_cetak_kib'] = "
SELECT
   invDetKodeBarang AS KIB
FROM
   inventarisasi_detail 
WHERE
   invDetId IN (%ID%)
ORDER BY
   invDetKodeBarang
";

$sql['get_penyesuaian_data'] = "
SELECT
   invDetKodeBarang AS invKodeBarang,
   barangNama,
   a.invDetMstLabel,
   a.invDetMerek,
   a.invDetSpesifikasi,
   IFNULL(mstPenystnUmrEko, a.invDetUmurEkonomis) AS mstPenystnUmrEko,
   IFNULL(mstPenystnNilaiPerolehan, a.invDetNilaiPerolehanSatuan) AS mstPenystnNilaiPerolehan,
   IFNULL(mstPenystnNilaiResidu, a.invDetNilaiResidu) AS mstPenystnNilaiResidu,
   mstPenystnBrgId
FROM
   inventarisasi_detail a
   JOIN inventarisasi_mst ON invMstId = invDetMstId
   JOIN barang_ref ON barangId = invBarangId
   LEFT JOIN penyusutan_brg_mst ON mstPenystnBarangId = InvDetId
WHERE
   invDetId = '%s'
LIMIT 1
";

/*********
Do Query
*********/
$sql['add_inv_mst'] = "
INSERT INTO
   inventarisasi_mst
   (
		invBarangId,
		invKodeAset,
		invBarcode,
		invMstLabel,
		invMerek,
		invSpesifikasi,
		invTglPembelian,
		invSumberDana,
		invJumlahAset,
		invSatuanBarang,
		invLuasTanah,
		invNilaiPerolehanSatuan,
		invNilaiFaktur,
		invTotalPerolehan,
		invTglUbah,
		invUserId
   )
VALUES
(
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   NOW(),
   (SELECT UserId FROM `gtfw_user` WHERE UserName = '%s')
)
";

$sql['add_inv_detail'] = "
INSERT INTO
   inventarisasi_detail
   (
      invDetMstId,
      invDetKodeBarang,
      invDetBarcode,
      invDetMstLabel,
      invDetMerek,
      invDetSpesifikasi,
      invDetTglPembelian,
      invDetNilaiResidu,
      invDetTipe,
      invDetSumberDana,
      invDetSatuanBarang,
      invDetNilaiPerolehanSatuan,
      invDetNJOP,
      invDetUmurEkonomis,
      invDetLokasi,
      invDetKegunaan,
      invDetSertifikat,
      invDetAkta,
      invDetBPKB,
      invDetSTNK,
      invDetNoMesin,
      invDetNoRangka,
      invDetNoPolisi,
      invDetNoLain,
      invDetWarna,
      invDetPagar,
      invDetBatasUtara,
      invDetBatasTimur,
      invDetBatasSelatan,
      invDetBatasBarat,
      invDetKepemilikan,
      invDetPenguasaanBarang,
      invDetKeteranganLain,
      invDetKampus,
      invDetGedungId,
      invDetRuanganId,
      invDetUnitKerja,
	  invDetKondisiId,
	  invDetNamaFile,
      invDetTglUbah,
      invDetUserId
   )
VALUES
(
   (SELECT MAX(invMstId) as a FROM inventarisasi_mst),
   /*(SELECT IFNULL(a, 1) FROM (SELECT MAX(SUBSTRING_INDEX(invDetKodeBarang,'.',-1))+1 AS a  FROM inventarisasi_detail JOIN inventarisasi_mst ON invMstId = invDetMstId WHERE invBarangId = '%%s') a),*/
   (SELECT * FROM
   	(SELECT 
		   CONCAT(
		      LPAD(golbrgKode, 1, '0'),
		      '.',
		      LPAD(bidangbrgKode, 2, '0'),
		      '.',
		      LPAD(kelbrgKode, 2, '0'),
		      '.',
		      LPAD(subkelbrgKode, 2, '0'),
		      '.',
		      LPAD(barangKode, 3, '0'),
		      '.',
		      LPAD((
					IFNULL(
		      (SELECT 
		         MAX(
		            SUBSTRING_INDEX(invDetKodeBarang, '.', - 1)
		         ) + 1 AS a 
		      FROM
		         inventarisasi_detail 
		         JOIN inventarisasi_mst 
		            ON invMstId = invDetMstId 
		      WHERE invBarangId = '%s'),
		      1
		   )
		      ), 4, '0')
		   ) AS kode_barang
		FROM
		   inventarisasi_mst 
		   LEFT JOIN barang_ref 
		      ON invBarangId = barangId 
		   LEFT JOIN sub_kelompok_barang_ref 
		      ON barangSubkelbrgId = subkelbrgId 
		   LEFT JOIN kelompok_barang_ref 
		      ON subkelbrgKelbrgId = kelbrgId 
		   LEFT JOIN bidang_barang_ref 
		      ON kelbrgBidangbrgId = bidangbrgId 
		   LEFT JOIN golongan_barang_ref 
		      ON bidangbrgGolbrgId = golbrgId 
		WHERE invMstId = (SELECT MAX(invMstId) FROM inventarisasi_mst) 
   ) kode),
   %s,
   %s,
   IFNULL(%s,''),
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
   %s,
	%s,
   %s,
   %s,
   NULLIF(%s, 0),
   NULLIF(%s, 0),
   %s,
	%s,
   NOW(),
   (SELECT UserId FROM `gtfw_user` WHERE UserName = '%s')
)
";

$sql['update_detil']="
UPDATE
inventarisasi_detail,
inventarisasi_mst,
barang_ref,
sub_kelompok_barang_ref,
kelompok_barang_ref,
bidang_barang_ref,
golongan_barang_ref
SET
`invDetGolId`=golbrgId,
`invDetBidangbrngId`=`bidangbrgId`,
`invDetKelbrgId`=`kelbrgId`,
`invDetSubkelbrgId`=`subkelbrgId`,
`invDetBarangId`=`barangId`
WHERE
invDetMstId =invMstId
AND
invBarangId = barangId
AND
barangSubkelbrgId = subkelbrgId
AND
subkelbrgKelbrgId=kelbrgId
AND
kelbrgBidangbrgId = bidangbrgId
AND
bidangbrgGolbrgId=golbrgId
AND
invDetId=(SELECT * FROM (SELECT MAX(invDetId) FROM inventarisasi_detail) a)
";

$sql['add_detail_biaya_by_id'] = "
INSERT INTO
   inv_det_biaya (invDetBiayaInvId, invDetBiayaBiayaId, invDetBiayaNilai)
VALUES
   ((SELECT MAX(invMstId) as a FROM inventarisasi_mst), %s, %s)
";

$sql['add_kondisi_history'] = "
INSERT INTO
   inv_kondisi_history
   (
      invKondisiBrgBrgDetId,
      invKondisiBrgKondId,
      invKondisiBrgTglKondisi,
      invKondisiTglHistory,
		invNamaFile,
      invKondisiUserId
   )
VALUES
(
   %s,
   %s,
   NOW(),
   NOW(),
	%s,
   (SELECT UserId FROM `gtfw_user` WHERE UserName = '%s')
)
";

$sql['add_lokasi_history'] = "
INSERT INTO
   inventarisasi_lokasi_history
   (
      invHisInvDetId,
      InvHisKampusId,
      invHisGedungId,
      InvHisRuangId,
      InvHisTanggal,
      InvHisUserId
   )
VALUES
(
   (SELECT MAX(invDetId) FROM inventarisasi_detail),
   %s,
   %s,
   %s,
   NOW(),
   (SELECT UserId FROM `gtfw_user` WHERE UserName = '%s')
)
";

$sql['add_penyusutan'] = "
INSERT INTO
   penyusutan_brg_mst
   (
      mstPenystnBarangId,
      mstPenystnNilaiPerolehan,
      mstPenystnNilaiResidu,
      mstPenystnUmrEko,
      mstPenystnDisusutkan,
      mstPenystnNilaiPenyusutan,
      mstPenystnTglPerubahan
   )
SELECT
   invDetId,
   IF(invDetNilaiPerolehanSatuan > 0, invDetNilaiPerolehanSatuan, invTotalPerolehan),
   IF(invDetNilaiResidu > 0, invDetNilaiResidu, NULL),
   invDetUmurEkonomis,
   IF(invDetNilaiPerolehanSatuan > 0, invDetNilaiPerolehanSatuan, invTotalPerolehan),
   IF(((IF(invDetNilaiPerolehanSatuan > 0, invDetNilaiPerolehanSatuan, invTotalPerolehan) - invDetNilaiResidu) / invDetUmurEkonomis) > 0, ((IF(invDetNilaiPerolehanSatuan > 0, invDetNilaiPerolehanSatuan, invTotalPerolehan) - invDetNilaiResidu) / invDetUmurEkonomis), NULL),
   NOW()
FROM
   inventarisasi_detail
   JOIN (SELECT invMstId, invTotalPerolehan FROM inventarisasi_mst) a ON invMstId = invDetMstId
WHERE
   invMstId = (SELECT MAX(invMstId) FROM inventarisasi_mst)
";

$sql['update_inv_detail'] = "
UPDATE
   inventarisasi_detail
SET
   invDetBarcode = %s,
   invDetMstLabel = %s,
   invDetMerek = %s,
   invDetSpesifikasi = %s,
   invDetTglPembelian = %s,
   invDetNilaiResidu = %s,
   invDetTipe = %s,
   invDetSumberDana = %s,
   invDetSatuanBarang = %s,
   invDetNilaiPerolehanSatuan = %s,
   invDetNJOP = %s,
   invDetUmurEkonomis = %s,
   invDetLokasi = %s,
   invDetKegunaan = %s,
   invDetSertifikat = %s,
   invDetAkta = %s,
   invDetBPKB = %s,
   invDetSTNK = %s,
   invDetNoMesin = %s,
   invDetNoRangka = %s,
   invDetNoPolisi = %s,
   invDetNoLain = %s,
   invDetWarna = %s,
   invDetPagar = %s,
   invDetBatasUtara = %s,
   invDetBatasTimur = %s,
   invDetBatasSelatan = %s,
   invDetBatasBarat = %s,
   invDetKepemilikan = %s,
   invDetPenguasaanBarang = %s,
   invDetKeteranganLain = %s,
   invDetKampus = %s,
   invDetGedungId = NULLIF(%s, 0),
   invDetRuanganId = NULLIF(%s, 0),
   invDetUnitKerja = %s,
   invDetTglUbah = NOW(),
   invDetUserId = (SELECT UserId FROM `gtfw_user` WHERE UserName = '%s')
WHERE
   invDetId = %s
";

$sql['update_kondisi_history'] = "
INSERT INTO
   inv_kondisi_history
   (
      invKondisiBrgBrgDetId,
      invKondisiBrgKondId,
      invKondisiBrgTglKondisi,
      invKondisiTglHistory,
      invKondisiUserId
   )
VALUES
(
   %s,
   %s,
   NOW(),
   NOW(),
   (SELECT UserId FROM `gtfw_user` WHERE UserName = '%s')
)
";

$sql['update_lokasi_history'] = "
INSERT INTO
   inventarisasi_lokasi_history
   (
      invHisInvDetId,
      InvHisKampusId,
      invHisGedungId,
      InvHisRuangId,
      InvHisTanggal,
      InvHisUserId
   )
VALUES
(
   %s,
   %s,
   NULLIF(%s, 0),
   NULLIF(%s, 0),
   NOW(),
   (SELECT UserId FROM `gtfw_user` WHERE UserName = '%s')
)
";

$sql["update_penyesuaian_aset"] = "
UPDATE
   penyusutan_brg_mst
SET
   mstPenystnUmrEko = %s,
   mstPenystnNilaiPerolehan = %s,
   mstPenystnNilaiResidu = %s
WHERE
   mstPenystnBrgId = %s
";

$sql["get_data_penempatan_aset_old"] = "
		SELECT
		SQL_CALC_FOUND_ROWS
		b.invDetId AS id,
		invDetKodeBarang AS KIB,
		a.invBarangId AS id_barang,
		c.barangKode AS kode_barang,
		b.invDetMstLabel AS nama_aset,
		CONCAT(kampusNama,IF(gedungNama IS NULL,'',CONCAT('@@',gedungNama,IF(ruangNama IS NULL,'',CONCAT('@@',ruangNama))))) AS lokasi,
		e.unitkerjaNama AS unit_pj,
		LEFT(b.invDetTglPembelian, 4) AS thn_pengadaan,
		IF(mstPenystnNilaiPerolehan IS NOT NULL, mstPenystnNilaiPerolehan,IF(b.invDetNilaiPerolehanSatuan IS NULL, a.invTotalPerolehan, b.invDetNilaiPerolehanSatuan)) AS nilai_perolehan,
		IF(mstPenystnDisusutkan IS NOT NULL, mstPenystnDisusutkan,IF(b.invDetNilaiPerolehanSatuan IS NULL, a.invTotalPerolehan, b.invDetNilaiPerolehanSatuan)) AS nilai_buku,
		f.kepemilikanbrgNama AS status_kepemilikan,
		h.kondisibrgNama AS kondisi,
		golbrgId AS golBarang
	FROM
		inventarisasi_mst a
		LEFT JOIN inventarisasi_detail b ON a.invMstId = b.invDetMstId
		LEFT JOIN barang_ref c ON a.invBarangId = barangId
		LEFT JOIN kampus_ref d ON b.invDetKampus = d.kampusId
		LEFT JOIN unit_kerja_ref e ON b.invDetUnitKerja = e.unitkerjaId
		LEFT JOIN kepemilikan_barang_ref f ON f.kepemilikanbrgId = b.invDetKepemilikan
		LEFT JOIN kondisi_barang_ref h ON h.kondisibrgId = b.invDetKondisiId
		JOIN sub_kelompok_barang_ref i ON barangSubkelbrgId = subkelbrgId
		JOIN kelompok_barang_ref j ON subkelbrgKelbrgId = kelbrgId
		JOIN bidang_barang_ref k ON kelbrgBidangbrgId = bidangbrgId
		JOIN golongan_barang_ref l ON bidangbrgGolbrgId = golbrgId
		LEFT JOIN gedung ON invDetGedungId = gedungId
		LEFT JOIN ruang ON invDetRuanganId = ruangId
		LEFT JOIN penyusutan_brg_mst ON invDetId = mstPenystnBarangId
		JOIN aset_ref_aset_status ON statusAsetId = b.invDetAsetStatusId
	WHERE
		(l.golbrgKibKode IN('b','c')) AND
		(
			b.invDetMstLabel LIKE '%s' OR
			invDetKodeBarang LIKE '%s'
		) AND
		(invUnitKerja = %s OR e.unitkerjaParentId = %s OR '1' = %s)
		%s
	ORDER BY
		b.invDetTglPembelian DESC
		LIMIT %s, %s
";

$sql["get_data_penempatan_aset"] = "
SELECT
SQL_CALC_FOUND_ROWS
b.invDetId AS id,
invDetKodeBarang AS KIB,
a.invBarangId AS id_barang,
c.barangKode AS kode_barang,
b.invDetMstLabel AS nama_aset,
CONCAT(kampusNama,IF(gedungNama IS NULL,'',CONCAT('@@',gedungNama,IF(ruangNama IS NULL,'',CONCAT('@@',ruangNama))))) AS lokasi,
e.unitkerjaNama AS unit_pj,
LEFT(b.invDetTglPembelian, 4) AS thn_pengadaan,
IF(mstPenystnNilaiPerolehan IS NOT NULL, mstPenystnNilaiPerolehan,IF(b.invDetNilaiPerolehanSatuan IS NULL, a.invTotalPerolehan, b.invDetNilaiPerolehanSatuan)) AS nilai_perolehan,
IF(mstPenystnDisusutkan IS NOT NULL, mstPenystnDisusutkan,IF(b.invDetNilaiPerolehanSatuan IS NULL, a.invTotalPerolehan, b.invDetNilaiPerolehanSatuan)) AS nilai_buku,
f.kepemilikanbrgNama AS status_kepemilikan,
h.kondisibrgNama AS kondisi,
golbrgId AS golBarang
FROM
inventarisasi_mst a
LEFT JOIN inventarisasi_detail b ON a.invMstId = b.invDetMstId
LEFT JOIN barang_ref c ON a.invBarangId = barangId
LEFT JOIN kampus_ref d ON b.invDetKampus = d.kampusId
LEFT JOIN unit_kerja_ref e ON b.invDetUnitKerja = e.unitkerjaId
LEFT JOIN kepemilikan_barang_ref f ON f.kepemilikanbrgId = b.invDetKepemilikan
LEFT JOIN kondisi_barang_ref h ON h.kondisibrgId = b.invDetKondisiId
JOIN sub_kelompok_barang_ref i ON barangSubkelbrgId = subkelbrgId
JOIN kelompok_barang_ref j ON subkelbrgKelbrgId = kelbrgId
JOIN bidang_barang_ref k ON kelbrgBidangbrgId = bidangbrgId
JOIN golongan_barang_ref l ON bidangbrgGolbrgId = golbrgId
LEFT JOIN gedung ON invDetGedungId = gedungId
LEFT JOIN ruang ON invDetRuanganId = ruangId
LEFT JOIN penyusutan_brg_mst ON invDetId = mstPenystnBarangId
JOIN aset_ref_aset_status ON statusAsetId = b.invDetAsetStatusId
WHERE
(l.golbrgKibKode IN('b','c')) AND
(
b.invDetMstLabel LIKE '%s' OR
invDetKodeBarang LIKE '%s'
) AND
(e.unitkerjaKodeSistem = '%s' OR e.unitkerjaKodeSistem LIKE '%s')
%s
ORDER BY
b.invDetTglPembelian DESC
LIMIT %s, %s
";

$sql["get_data_penempatan_aset_count"] = "
	SELECT FOUND_ROWS() AS total
";

$sql["update_penyesuaian_aset"] = "
	UPDATE
		inventarisasi_detail
	SET
		invDetKampus = '%s',
		invDetGedungId = '%s',
		invDetRuanganId = '%s',
		invDetTglUbah = NOW(),
		invDetUserId = '%s'
	WHERE
		invDetId = '%s'
";

$sql['add_lokasi_history_penempatan_aset'] = "
INSERT INTO
   inventarisasi_lokasi_history
SET
   invHisInvDetId = '%s',
   InvHisKampusId = '%s',
   invHisGedungId = NULLIF(%s, 0),
   InvHisRuangId = NULLIF(%s, 0),
   InvHisTanggal = NOW(),
   InvHisUserId = '%s'
";
?>
