<?php

//==========GET DATA ======================================
$sql['get_count_data_perbaikan_barang'] = 
   "SELECT 
      count(*) AS total
	FROM 
		aset_perbaikan
	LEFT JOIN mitra_ref ON mitraId =perbaikanMitraId
	WHERE
		perbaikanBA LIKE '%s' AND
		(mitraId = '%s' %OPERATOR% mitraNama LIKE '%s')
	";
		
$sql['get_data_perbaikan_barang']="
	SELECT 
		perbaikanId								AS id,
		perbaikanBA								AS nomor_ba,
		perbaikanTgl								AS tanggal_perbaikan,
		perbaikanDoDate						AS do_date,
		perbaikanPerkiraanTotalHarga		AS total_harga,
		mitraNama								AS mitra,
                b.isDone
	FROM  aset_perbaikan
	LEFT JOIN mitra_ref ON mitraId = perbaikanMitraId
        /* cek apakah sudah melalui tahap perbaikan barang history*/
        LEFT JOIN (
            SELECT 
                perbaikanPerbaikanId,
                COUNT(perbaikanDetHisId) AS isDone
            FROM aset_perbaikan_det
            LEFT JOIN aset_perbaikan_det_history ON perbaikanPerbaikanDetId = perbaikanDetId
            GROUP BY perbaikanPerbaikanId
        ) b ON b.perbaikanPerbaikanId = perbaikanId
	WHERE
		perbaikanBA LIKE '%s' AND
		(mitraId = '%s' %OPERATOR% mitraNama LIKE '%s')
	 ORDER BY perbaikanTgl DESC
	 LIMIT %s, %s
";
 
  $sql['get_data_perbaikan_barang_by_id']=" 	
	SELECT 
		perbaikanId								AS id,
		perbaikanBA								AS nomorba,
		perbaikanTgl								AS tanggal_perbaikan,
		perbaikanDoDate						AS do_date,
		perbaikanPerkiraanTotalHarga		AS total_harga,
		mitraId										AS idMitra,
		mitraNama								AS namaMitra,
		perbaikanCatatan 						AS catatan,
		perbaikanPerkiraanHarga			AS perbaikanBiaya,
		invDetKodeBarang AS barangKode,
		barangNama AS barangNama,
		invDetId AS barangId,
		kondisibrgNama AS barangKondisi,
                perbaikanPengendalianBrgDetId AS pengendalianDetId
	FROM 
		aset_perbaikan
	LEFT JOIN mitra_ref ON mitraId =perbaikanMitraId
	LEFT JOIN aset_perbaikan_det ON perbaikanPerbaikanId=perbaikanId
	LEFT JOIN inventarisasi_detail  ON invDetId = perbaikanInvDetId
    LEFT JOIN inventarisasi_mst  ON invDetMstId = invMstId
	LEFT JOIN barang_ref c ON invBarangId = barangId
	LEFT JOIN inv_kondisi_history d ON invDetId = invKondisiBrgBrgDetId
    LEFT JOIN kondisi_barang_ref e ON invKondisiBrgKondId = kondisibrgId
	LEFT JOIN pengendalian_barang_det ON pengBrgInvDetId = invDetId
	WHERE
		perbaikanId IN('%s')
		AND invKondisiBrgId = (SELECT
                             MAX(invKondisiBrgId)
                           FROM inv_kondisi_history
                           WHERE invKondisiBrgBrgDetId = invDetId)
	 ORDER BY perbaikanId DESC
";

//do-----------------------------------------------------
$sql['do_add_perbaikan_barang'] = 
   "INSERT INTO aset_perbaikan  
      (perbaikanBA, perbaikanTgl, perbaikanDoDate, perbaikanMitraId, perbaikanPerkiraanTotalHarga, perbaikanCatatan, perbaikanTglUbah, perbaikanUserId)
   VALUES 
      ('%s','%s','%s','%s','%s','%s',NOW(),'%s')";

$sql['do_update_investaris_detil'] = "
	UPDATE inventarisasi_detail
	SET invDetAsetStatusId = '%s',
	  invDetTglUbah = NOW(),
	  invDetUserId = '%s'
	WHERE invDetId = '%s'
";

$sql['do_add_perbaikan_barang_det'] = 
   "INSERT INTO aset_perbaikan_det 
	(perbaikanPerbaikanId, perbaikanInvDetId, perbaikanPengendalianBrgDetId, perbaikanPerkiraanHarga, perbaikanTglUbah, perbaikanUserId)
	VALUES
	((SELECT MAX(perbaikanId) FROM aset_perbaikan) ,'%s','%s','%s',NOW(),'%s')";

//update
$sql['do_update_perbaikan_barang'] = 
   "UPDATE aset_perbaikan
	SET
      perbaikanBA  = '%s', 
	  perbaikanTgl  = '%s',
	  perbaikanDoDate  = '%s',
	  perbaikanMitraId  = '%s',
	  perbaikanPerkiraanTotalHarga  = '%s',
	  perbaikanCatatan  = '%s', 
	  perbaikanTglUbah = NOW(),
	  perbaikanUserId  = '%s'
	WHERE
	  perbaikanId = '%s'
   ";


$sql['do_update_perbaikan_barang_det'] = 
   "INSERT INTO aset_perbaikan_det 
	(perbaikanPerbaikanId, perbaikanInvDetId, perbaikanPengendalianBrgDetId,perbaikanPerkiraanHarga, perbaikanTglUbah, perbaikanUserId)
	VALUES
	('%s' ,'%s','%s','%s',NOW(),'%s')";

//delete
$sql['do_delete_perbaikan_barang_det'] = 
   "DELETE FROM aset_perbaikan_det 
	WHERE perbaikanPerbaikanId='%s'";

$sql['do_delete_perbaikan_barang'] = 
   "DELETE FROM aset_perbaikan
	WHERE perbaikanId='%s'";

$sql['do_delete_perbaikan_barang_by_array_id'] = 
   "DELETE from aset_perbaikan
   WHERE 
     perbaikanId  IN ('%s')";
	 
//untuk popup mitra
$sql['get_mitra_view'] =
   ("
      SELECT
         mitraId as mkId,
         mitraNama as mkNama,
         mitraAlamat as mkAlamat,
         mitraTelp as mkTelepon
      FROM
         mitra_ref
         LEFT JOIN aset_mitra_bidang_usaha ON mitraId = mitrabidusahaMitraId
      WHERE
         mitrabidusahaJnsbidusahaId ='%s' %OPERATOR%
         mitraNama LIKE '%s' AND
         mitraAlamat LIKE '%s'
      GROUP BY
         mitraId
      ORDER BY
         mitraNama
      LIMIT
         %d, %d
   ");

$sql['get_count_mitra_view'] =
   ("
      SELECT
         mitraId as id
      FROM
         mitra_ref
         LEFT JOIN aset_mitra_bidang_usaha ON mitraId = mitrabidusahaMitraId
      WHERE
         mitrabidusahaJnsbidusahaId ='%s' %OPERATOR%
         mitraNama LIKE '%s' AND
         mitraAlamat LIKE '%s'
      GROUP BY
         mitraId
   ");

$sql['get_bidang_usaha_list'] =
   ("
      SELECT
         jnsbidusahaId as id,
         jnsbidusahaNama as name
      FROM
         jenis_bidang_usaha_ref
   ");
?>
