<?php

$sql['template'] =
   ("
      INSERT INTO
      VALUES
      SELECT
      UPDATE
      SET
      DELETE
      FROM
      WHERE
      GROUP BY
      ORDER BY
      LIMIT
   ");
   
// Get query....

$sql['get_combo_golongan'] = 
   "SELECT 
     `golbrgId` as `id`,
     `golbrgNama` as `name`
   FROM 
      `golongan_barang_ref`
   ORDER BY `golbrgNama`
";

$sql['get_combo_jenis_barang'] = 
   "SELECT 
     `jenisbrgId` as `id`,
     `jenisbrgNama` as `name`
   FROM 
      `jenis_barang_ref`
   ORDER BY `jenisbrgNama`
";

$sql['get_combo_satuan_barang'] = 
   "SELECT 
     `satuanbrgId` as `id`,
     `satuanbrgNama` as `name`
   FROM 
      `satuan_barang_ref`
   ORDER BY `satuanbrgNama`
";

$sql['get_data_barang_count'] =
   ("
      SELECT 
         COUNT(`nama`) as `total` FROM
      ( 
				SELECT
				         `kelbrgId` as `idKelompok`, 
				         `subkelbrgId` as `idSubKelompok`, 
				         `barangId` as `id`, 
				         CONCAT(LPAD(golbrgKode, 1, 0), '.', LPAD(bidangbrgKode, 2, 0), '.', LPAD(kelbrgKode, 2, 0)) AS `kode`, 
				         CONCAT(LPAD(golbrgKode, 1, 0), '.', LPAD(bidangbrgKode, 2, 0), '.', LPAD(kelbrgKode, 2, 0), '.', LPAD(subkelbrgKode, 2, 0)) AS `subkode`, 
				         CONCAT(LPAD(golbrgKode, 1, 0), '.', LPAD(bidangbrgKode, 2, 0), '.', LPAD(kelbrgKode, 2, 0), '.', LPAD(subkelbrgKode, 2, 0), '.', LPAD(barangKode, 3, 0)) AS `kd`, 
				         `kelbrgNama` as kelNama,  
				         `subkelbrgNama` AS subKel,  
				         `barangNama` as `nama`, 
				         `barangHps` AS `hps`,
						  golbrgId,
						  bidangbrgId,
					      kelbrgId,
					      subkelbrgId,
					      if (barangNama, NULL,'') as barangNama,
				          barangJenisbrgId
				      FROM 
				         `golongan_barang_ref`
				         JOIN `bidang_barang_ref` ON (`bidangbrgGolbrgId` = `golbrgId`)
				         JOIN `kelompok_barang_ref` ON (`kelbrgBidangbrgId` = `bidangbrgId`)
				         LEFT JOIN `sub_kelompok_barang_ref` ON (`subkelbrgKelbrgId` = `kelbrgId`)
				         LEFT JOIN `barang_ref` ON (`barangSubkelbrgId` = `subkelbrgId`)
				 )a
      WHERE
         (`golbrgId` = %d OR %s = 'all') AND
         (`bidangbrgId` = %d OR %s = '') AND
         (`kelbrgId` = %d OR %s = '') AND
         (`subkelbrgId` = %d OR %s = '') AND
         (`%CARINAMA%` LIKE '%s') AND
         (`barangJenisbrgId` = %d OR %s = 'all')
   ");

$sql['get_data_barang'] =
   ("
     SELECT * FROM( 
				SELECT
				         `kelbrgId` as `idKelompok`, 
				         `subkelbrgId` as `idSubKelompok`, 
				         `barangId` as `id`, 
				         CONCAT(LPAD(golbrgKode, 1, 0), '.', LPAD(bidangbrgKode, 2, 0), '.', LPAD(kelbrgKode, 2, 0)) AS `kode`, 
				         CONCAT(LPAD(golbrgKode, 1, 0), '.', LPAD(bidangbrgKode, 2, 0), '.', LPAD(kelbrgKode, 2, 0), '.', LPAD(subkelbrgKode, 2, 0)) AS `subkode`, 
				         CONCAT(LPAD(golbrgKode, 1, 0), '.', LPAD(bidangbrgKode, 2, 0), '.', LPAD(kelbrgKode, 2, 0), '.', LPAD(subkelbrgKode, 2, 0), '.', LPAD(barangKode, 3, 0)) AS `kd`, 
				         `kelbrgNama` as kelNama,  
				         `subkelbrgNama` AS subKel,  
				         `barangNama` as `nama`, 
				         `barangHps` AS `hps`,
						  golbrgId,
						  bidangbrgId,
					      kelbrgId,
					      subkelbrgId,
					      if (barangNama, NULL,'') as barangNama,
				          barangJenisbrgId,
						  barangFoto AS foto_barang
				      FROM 
				         `golongan_barang_ref`
				         JOIN `bidang_barang_ref` ON (`bidangbrgGolbrgId` = `golbrgId`)
				         JOIN `kelompok_barang_ref` ON (`kelbrgBidangbrgId` = `bidangbrgId`)
				         LEFT JOIN `sub_kelompok_barang_ref` ON (`subkelbrgKelbrgId` = `kelbrgId`)
				         LEFT JOIN `barang_ref` ON (`barangSubkelbrgId` = `subkelbrgId`)
				 )a
      WHERE
         (`golbrgId` = %d OR %s = 'all') AND
         (`bidangbrgId` = %d OR %s = '') AND
         (`kelbrgId` = %d OR %s = '') AND
         (`subkelbrgId` = %d OR %s = '') AND
         (`%CARINAMA%` LIKE '%s') AND
         (`barangJenisbrgId` = %d OR %s = 'all')
      ORDER BY
         kode,kd
      LIMIT
         %s, %s
   ");

$sql['get_data_barang_count_old'] = "
      SELECT 
         COUNT(`kelbrgId`) AS total
      FROM 
         `golongan_barang_ref`
         JOIN `bidang_barang_ref` ON (`bidangbrgGolbrgId` = `golbrgId`)
         JOIN `kelompok_barang_ref` ON (`kelbrgBidangbrgId` = `bidangbrgId`)
         LEFT JOIN `sub_kelompok_barang_ref` ON (`subkelbrgKelbrgId` = `kelbrgId`)
         LEFT JOIN `barang_ref` ON (`barangSubkelbrgId` = `subkelbrgId`)
         WHERE
         `barangNama` LIKE '%s'
         AND `golbrgId` LIKE '%s'
         AND `bidangbrgId` LIKE '%s'
         AND `kelbrgId` LIKE '%s'
         AND `kelbrgId` LIKE '%s'
         AND `subkelbrgId` LIKE '%s'
      ORDER BY
         `kelbrgId`,
         `subkelbrgId`,
         `barangId`
";

$sql['get_data_barang_old'] =
   "
      SELECT 
         `kelbrgId` as `idKelompok`,
         `subkelbrgId` as `idSubKelompok`,
         `barangId` as `id`,
         CONCAT(CASE  WHEN golbrgKode<10 THEN CONCAT('0',golbrgKode)
         WHEN golbrgKode>9 THEN golbrgKode END,'.',
         CASE  WHEN bidangbrgKode<10 THEN CONCAT('0',bidangbrgKode)
         WHEN bidangbrgKode>9 THEN bidangbrgKode END, '.',
         CASE  WHEN kelbrgKode<10 THEN CONCAT('0',kelbrgKode)
         WHEN kelbrgKode>9 THEN kelbrgKode END) AS `kode`,
         CONCAT(CASE  WHEN golbrgKode<10 THEN CONCAT('0',golbrgKode)
         WHEN golbrgKode>9 THEN golbrgKode END,'.',
         CASE  WHEN bidangbrgKode<10 THEN CONCAT('0',bidangbrgKode)
         WHEN bidangbrgKode>9 THEN bidangbrgKode END, '.',
         CASE  WHEN kelbrgKode<10 THEN CONCAT('0',kelbrgKode)
         WHEN kelbrgKode>9 THEN kelbrgKode END ,'.',
         CASE  WHEN subkelbrgKode<10 THEN CONCAT('0',subkelbrgKode)
         WHEN subkelbrgKode>9 THEN subkelbrgKode END ) AS `subkode`,
         CONCAT(CASE  WHEN golbrgKode<10 THEN CONCAT('0',golbrgKode)
         WHEN golbrgKode>9 THEN golbrgKode END,'.',
         CASE  WHEN bidangbrgKode<10 THEN CONCAT('0',bidangbrgKode)
         WHEN bidangbrgKode>9 THEN bidangbrgKode END, '.',
         CASE  WHEN kelbrgKode<10 THEN CONCAT('0',kelbrgKode)
         WHEN kelbrgKode>9 THEN kelbrgKode END ,'.',
         CASE  WHEN subkelbrgKode<10 THEN CONCAT('0',subkelbrgKode)
         WHEN subkelbrgKode>9 THEN subkelbrgKode END,'.',
         CASE WHEN barangKode<10 THEN CONCAT('000',barangKode)
         WHEN barangKode<100 THEN CONCAT('00',barangKode)
         WHEN barangKode<1000 THEN CONCAT('0',barangKode)
         WHEN barangKode>9999 THEN barangKode END
          ) AS `kd`,
         `kelbrgNama` as kelNama, 
         `subkelbrgNama` AS subKel, 
         `barangNama` as `nama`,
         `barangHps` AS `hps`
      FROM 
         `golongan_barang_ref`
         JOIN `bidang_barang_ref` ON (`bidangbrgGolbrgId` = `golbrgId`)
         JOIN `kelompok_barang_ref` ON (`kelbrgBidangbrgId` = `bidangbrgId`)
         LEFT JOIN `sub_kelompok_barang_ref` ON (`subkelbrgKelbrgId` = `kelbrgId`)
         LEFT JOIN `barang_ref` ON (`barangSubkelbrgId` = `subkelbrgId`)
         WHERE
         `barangNama` LIKE %s 
         AND `golbrgId` LIKE '%s'
         AND `bidangbrgId` LIKE '%s'
         AND `kelbrgId` LIKE '%s'
         AND `kelbrgId` LIKE '%s'
         AND `subkelbrgId` LIKE '%s'
      ORDER BY
         `kelbrgId`,
         `subkelbrgId`,
         `barangId`
      LIMIT %s, %s
   ";

$sql['get_kelompok_list'] =
   ("
      SELECT 
         `kelbrgId` as `id`,
         CONCAT(LPAD(golbrgKode, 1, 0), '.', LPAD(bidangbrgKode, 2, 0), '.', LPAD(`kelbrgKode`, 2, 0)) as `kode`,
         `kelbrgNama` as `nama`
      FROM 
         `golongan_barang_ref`
         JOIN `bidang_barang_ref` ON (`bidangbrgGolbrgId` = `golbrgId`)
         JOIN `kelompok_barang_ref` ON (`kelbrgBidangbrgId` = `bidangbrgId`)
      ORDER BY
         `kelbrgNama`
   ");

$sql['get_sub_kelompok_list'] =
   ("
      SELECT
         `kelbrgId` as `idKelompok`,
         `subkelbrgId` as `id`,
         CONCAT(LPAD(golbrgKode, 1, 0), '.', LPAD(bidangbrgKode, 2, 0), '.', LPAD(`kelbrgKode`, 2, 0), '.', LPAD(`subkelbrgKode`, 2, 0)) as `kode`,
         `subkelbrgNama` as `nama`
      FROM 
         `golongan_barang_ref`
         JOIN `bidang_barang_ref` ON (`bidangbrgGolbrgId` = `golbrgId`)
         JOIN `kelompok_barang_ref` ON (`kelbrgBidangbrgId` = `bidangbrgId`)
         JOIN `sub_kelompok_barang_ref` ON (`subkelbrgKelbrgId` = `kelbrgId`)
      ORDER BY
         `subkelbrgNama`
   ");

$sql['get_data_kelompok_by_id'] =
   ("
      SELECT 
         `golbrgId` as `golongan`,
         `bidangbrgId` as `bidang`,
         `bidangbrgNama` as `bidang_name`,
         `kelbrgKode` as `kode`,
         `kelbrgNama` as `nama`
      FROM 
         `golongan_barang_ref`
         JOIN `bidang_barang_ref` ON (`bidangbrgGolbrgId` = `golbrgId`)
         JOIN `kelompok_barang_ref` ON (`kelbrgBidangbrgId` = `bidangbrgId`)
      WHERE
         `kelbrgId` = %d
   ");

$sql['get_data_sub_kelompok_by_id'] =
   ("
      SELECT 
         `golbrgId` as `golongan`,
         `bidangbrgId` as `bidang`,
         `bidangbrgNama` as `bidang_name`,
         `kelbrgId` as `kelompok`,
         `kelbrgNama` as `kelompok_name`,
         `subkelbrgKode` as `kode`,
         `subkelbrgNama` as `nama`
      FROM 
         `golongan_barang_ref`
         JOIN `bidang_barang_ref` ON (`bidangbrgGolbrgId` = `golbrgId`)
         JOIN `kelompok_barang_ref` ON (`kelbrgBidangbrgId` = `bidangbrgId`)
         JOIN `sub_kelompok_barang_ref` ON (`subkelbrgKelbrgId` = `kelbrgId`)
      WHERE
         `subkelbrgId` = %d
   ");

$sql['get_data_barang_by_id'] =
   ("
      SELECT 
         `golbrgId` as `golongan`,
         `bidangbrgId` as `bidang`,
         `bidangbrgNama` as `bidang_name`,
         `kelbrgId` as `kelompok`,
         `kelbrgNama` as `kelompok_name`,
         `subkelbrgId` as `subKelompok`,
         `subkelbrgNama` as `subKelompok_name`,
         `barangKode` as `kode`,
         `barangNama` as `nama`,
         `barangJenisbrgId` as `jenisBarang`,
         `barangSatuanbrgId` as `satuanBarang`,
         `barangHps` AS `hps`,
		 `barangFoto` AS `foto_barang`,
		 `barangUmurEkonomis` AS umurEkonomis,
		 `barangNilaiResidu` AS nilaiResidu
      FROM 
         `golongan_barang_ref`
         JOIN `bidang_barang_ref` ON (`bidangbrgGolbrgId` = `golbrgId`)
         JOIN `kelompok_barang_ref` ON (`kelbrgBidangbrgId` = `bidangbrgId`)
         JOIN `sub_kelompok_barang_ref` ON (`subkelbrgKelbrgId` = `kelbrgId`)
         JOIN `barang_ref` ON (`barangSubkelbrgId` = `subkelbrgId`)
      WHERE
         `barangId` = %d
   ");

$sql['get_detail_barang_by_id'] =
   ("
      SELECT 
         `barangId` as `idBarang`,
         (golbrgKode) as `kodeGolongan`,
         `golbrgNama` as `namaGolongan`,
         CONCAT(LPAD(golbrgKode, 1, 0), '.', LPAD(bidangbrgKode, 2, 0)) as `kodeBidang`,
         `bidangbrgNama` as `namaBidang`,
         CONCAT(LPAD(golbrgKode, 1, 0), '.', LPAD(bidangbrgKode, 2, 0), '.', LPAD(`kelbrgKode`, 2, 0)) as `kodeKelompok`,
         `kelbrgNama` as `namaKelompok`,
         CONCAT(LPAD(golbrgKode, 1, 0), '.', LPAD(bidangbrgKode, 2, 0), '.', LPAD(`kelbrgKode`, 2, 0), '.', LPAD(`subkelbrgKode`, 2, 0)) as `kodeSubKelompok`,
         `subkelbrgNama` as `namaSubKelompok`,
         CONCAT(LPAD(golbrgKode, 1, 0), '.', LPAD(bidangbrgKode, 2, 0), '.', LPAD(`kelbrgKode`, 2, 0), '.', LPAD(`subkelbrgKode`, 2, 0), '.', LPAD(`barangKode`, 3, 0)) as `kodeBarang`,
         `barangNama` as `namaBarang`,
         `jenisbrgNama` as `jenisBarang`,
         `satuanbrgNama` as `satuanBarang`,
		 `barangFoto` AS `foto_barang`,
		 `barangUmurEkonomis` AS umurEkonomis,
		 `barangNilaiResidu` AS nilaiResidu
      FROM 
         `golongan_barang_ref`
         JOIN `bidang_barang_ref` ON (`bidangbrgGolbrgId` = `golbrgId`)
         JOIN `kelompok_barang_ref` ON (`kelbrgBidangbrgId` = `bidangbrgId`)
         JOIN `sub_kelompok_barang_ref` ON (`subkelbrgKelbrgId` = `kelbrgId`)
         JOIN `barang_ref` ON (`barangSubkelbrgId` = `subkelbrgId`)
         LEFT JOIN `jenis_barang_ref` ON (`jenisbrgId` = `barangJenisbrgId`)
         LEFT JOIN `satuan_barang_ref` ON (`satuanbrgId` = `barangSatuanbrgId`)
         
      WHERE
         `barangId` = %d
   ");

$sql['cek_kode_kelompok'] = 
   "SELECT
      kelbrgId as id
   FROM
      kelompok_barang_ref
   WHERE
      kelbrgKode = %s AND
      kelbrgBidangbrgId = %s
";

$sql['cek_kode_sub_kelompok'] = 
   "SELECT
      subkelbrgId as id
   FROM
      sub_kelompok_barang_ref
   WHERE
      subkelbrgKode = %s AND
      subkelbrgKelbrgId = %s
";

$sql['cek_kode_barang'] = 
   "SELECT
      barangId as id
   FROM
      barang_ref
   WHERE
      barangKode = %s AND
      barangSubkelbrgId = %s
";

// Do Query....
$sql['add_kelompok'] =
   ("
      INSERT INTO
         `kelompok_barang_ref`
         (
            `kelbrgKode`,
            `kelbrgNama`,
            `kelbrgBidangbrgId`
         )
      VALUES
         (%d, '%s', %d)
   ");

$sql['update_kelompok'] =
   ("
      UPDATE
         `kelompok_barang_ref`
      SET
         `kelbrgKode` = %d,
         `kelbrgNama` = '%s',
         `kelbrgBidangbrgId` = %d
      WHERE
         `kelbrgId` = %d
   ");

$sql['delete_kelompok'] =
   ("
      DELETE FROM
         `kelompok_barang_ref`
      WHERE
         `kelbrgId` = %d
   ");
   
$sql['add_sub_kelompok'] =
   ("
      INSERT INTO
         `sub_kelompok_barang_ref`
         (
            `subkelbrgKode`,
            `subkelbrgNama`,
            `subkelbrgKelbrgId`
         )
      VALUES
         (%d, '%s', %d)
   ");

$sql['update_sub_kelompok'] =
   ("
      UPDATE
         `sub_kelompok_barang_ref`
      SET
         `subkelbrgKode` = %d,
         `subkelbrgNama` = '%s',
         `subkelbrgKelbrgId` = %d
      WHERE
         `subkelbrgId` = %d
   ");

$sql['delete_sub_kelompok'] =
   ("
      DELETE FROM
         `sub_kelompok_barang_ref`
      WHERE
         `subkelbrgId` = %d
   ");
   
$sql['add_barang'] =
   ("
      INSERT INTO
         `barang_ref`
         (
            `barangKode`,
            `barangNama`,
            `barangJenisbrgId`,
            `barangSatuanbrgId`,
            `barangSubkelbrgId`,
            `barangHps`,
			`barangFoto`,
			`barangUmurEkonomis`,
			`barangNilaiResidu`
         )
      VALUES
         (%d, '%s', %d, %d, %d, '%s', '%s', '%s', '%s')
   ");

$sql['update_barang'] =
   ("
      UPDATE
         `barang_ref`
      SET
         `barangKode` = %d,
         `barangNama` = '%s',
         `barangJenisbrgId` = %d,
         `barangSatuanbrgId` = %d,
         `barangSubkelbrgId` = %d,
         `barangHps` = '%s',
		 `barangFoto` = '%s',
		 `barangUmurEkonomis` = '%s',
		 `barangNilaiResidu` = '%s'
      WHERE
         `barangId` = %d
   ");

$sql['delete_barang'] =
   ("
      DELETE FROM
         `barang_ref`
      WHERE
         `barangId` = %d
   ");
?>
