<?php

$sql['get_kode_sistem_by_user_id'] = "
SELECT unitkerjaKodeSistem
FROM unit_kerja_ref
WHERE unitkerjaId = (SELECT userunitkerjaUnitkerjaId FROM user_unit_kerja WHERE userunitkerjaUserId = '%s')
";

$sql['template'] =
   ("
      INSERT INTO
      VALUES
      SELECT
      UPDATE
      SET
      DELETE
      FROM
      WHERE
      GROUP BY
      ORDER BY
      LIMIT
   ");
   
// Get query....
$sql['get_mitra_view'] =
   ("
      SELECT
   		 SQL_CALC_FOUND_ROWS
         `mitraId` as `mkId`,
         `mitraNama` as `mkNama`,
         `mitraAlamat` as `mkAlamat`,
         `mitraTelp` as `mkTelepon`,
   		 (SELECT unitkerjaKodeSistem FROM unit_kerja_ref WHERE unitkerjaId = (SELECT userunitkerjaUnitkerjaId FROM user_unit_kerja WHERE userunitkerjaUserId = mitraDataPrepared)) AS kodeSistem
      FROM
         `mitra_ref`
         LEFT JOIN `aset_mitra_bidang_usaha` ON mitraId = mitrabidusahaMitraId
		 LEFT JOIN aset_mitra_cp_relation ON mitraId = mitraCpRelasiMitraId
		 LEFT JOIN aset_ref_cp ON cpId = mitraCpRelasiCpId
      WHERE
         `mitrabidusahaJnsbidusahaId` = %d %OPERATOR%
		 `cpNama` LIKE %s %OPERATOR2%
         (`mitraNama` LIKE '%s' AND
         `mitraAlamat` LIKE '%s')
      GROUP BY
         `mitraId`
   	  HAVING (kodeSistem = '%s' OR kodeSistem LIKE '%s')
      ORDER BY
         `mitraNama`
      LIMIT
         %d, %d
   ");

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

$sql['get_count_mitra_view'] =
   ("
      SELECT
         `mitraId` as `id`
      FROM
         `mitra_ref`
         LEFT JOIN `aset_mitra_bidang_usaha` ON mitraId = mitrabidusahaMitraId
		 LEFT JOIN aset_mitra_cp_relation ON mitraId = mitraCpRelasiMitraId
		 LEFT JOIN aset_ref_cp ON cpId = mitraCpRelasiCpId
      WHERE
         `mitrabidusahaJnsbidusahaId` = %d %OPERATOR%
		 `cpNama` LIKE %s %OPERATOR2%
         (`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`
   ");

$sql['get_bank_list'] =
   ("
      SELECT
         `bankId` as `id`,
         `bankNama` as `name`
      FROM
         `aset_ref_bank`
   ");

$sql['get_mitra_by_id'] =
   ("
      SELECT
		`mitraId`					AS mkId,
        `mitraNama`					AS mknama, 
		`mitraAlamat`				AS mkalamat, 
		`mitraHP`					AS mkhp, 
		`mitraTelp`					AS mktelp, 
		`mitraFax`					AS mkfax, 
		`mitraCity`					AS mkcity, 
		`mitraZipCode`				AS mkzipcode, 
		`mitraCountry`				AS mkcountry, 
		`mitraEmail`				AS mkemail, 
		`mitraWeb`					AS mkweb, 
		`mitraWorkshopAddress`		AS mkwsaddress, 
		`mitraWorkshopAddressCity`	AS mkwsaddresscity, 
		`mitraWorkshopZipCode`		AS mkwszipcode, 
		`mitraWorkshopTelepon`		AS mkwstelepon, 
		`mitraWorkshopFax`			AS mkwsfax, 
		`mitraNPWP`					AS mknpwp, 
		`mitraSIUP` 				AS mksiup, 
		`mitraTDP` 					AS mktdp, 
		`mitraAOA` 					AS mkaoa,
		`mitraDocProfile` 			AS mkdocprofile, 
		`mitraDataPrepared` 		AS mkdataprapared
      FROM
         `mitra_ref`
      WHERE
         `mitraId` = %d
      LIMIT
         1
   ");

$sql['get_mitra_bidang_usaha_by_id'] =
   ("
      SELECT
         `jnsbidusahaId` as `buId`,
         `jnsbidusahaNama` as `buNama`
      FROM
         `aset_mitra_bidang_usaha`
         LEFT JOIN `jenis_bidang_usaha_ref` ON `jnsbidusahaId` = `mitrabidusahaJnsbidusahaId`
      WHERE
         `mitrabidusahaMitraId` = %d
   ");

//untuk bank----------
$sql['get_mitra_bank_by_id'] =
   ("
      SELECT
         `bankId` as `bankId`,
         `bankNama` as `bankNama`
      FROM
         `aset_mitra_bank_relation`
         LEFT JOIN `aset_ref_bank` ON `bankId` = `mitrabankBankRefId`
      WHERE
         `mitrabankMitraRefId` = %d
   ");
//untuk contactPerson----

$sql['get_cp_by_id'] =
   ("
      SELECT 	
		 `cpId` AS id, 
		 `cpNama` AS nama, 
		 `cpAlamat` AS alamat, 
		 `cpHP` AS hp, 
		 `cpTlp` AS telp, 
		 `cpEmail` AS email, 
		 `cpFoto` AS foto
	  FROM aset_mitra_cp_relation
	  LEFT JOIN `aset_ref_cp` ON `cpId`= `mitraCpRelasiCpId`
	  WHERE
		`mitraCpRelasiMitraId` = %d
   ");
//-------------------------------
$sql['get_last_mitra_id'] =
   ("
      SELECT
         MAX(`mitraId`) as `mkId`
      FROM
         `mitra_ref`
   ");
// Do Category....
$sql['update_mitra'] =
   ("
      UPDATE
         `mitra_ref`
      SET
        `mitraNama`= '%s', 
		`mitraAlamat` = '%s',
		`mitraHP` = '%s', 
		`mitraTelp` = '%s', 
		`mitraFax` = '%s', 
		`mitraCity` = '%s', 
		`mitraZipCode` = '%s', 
		`mitraCountry` = '%s', 
		`mitraEmail` = '%s', 
		`mitraWeb` = '%s', 
		`mitraWorkshopAddress` = '%s', 
		`mitraWorkshopAddressCity` = '%s', 
		`mitraWorkshopZipCode` = '%s', 
		`mitraWorkshopTelepon` = '%s', 
		`mitraWorkshopFax` = '%s', 
		`mitraNPWP` = '%s', 
		`mitraSIUP` = '%s', 
		`mitraTDP` = '%s', 
		`mitraAOA` = '%s', 
		`mitraDocProfile` = '%s', 
		`mitraDateUpdate` = now(), 
		`mitraDataPrepared` = '%s'
      WHERE
        `mitraId` = %d
   ");

$sql['add_mitra'] =
   ("
      INSERT INTO
         `mitra_ref` 
	(
	`mitraNama`, 
	`mitraAlamat`,
	`mitraHP`, 
	`mitraTelp`, 
	`mitraFax`, 
	`mitraCity`, 
	`mitraZipCode`, 
	`mitraCountry`, 
	`mitraEmail`, 
	`mitraWeb`, 
	`mitraWorkshopAddress`, 
	`mitraWorkshopAddressCity`, 
	`mitraWorkshopZipCode`, 
	`mitraWorkshopTelepon`, 
	`mitraWorkshopFax`, 
	`mitraNPWP`, 
	`mitraSIUP`, 
	`mitraTDP`, 
	`mitraAOA`, 
	`mitraDocProfile`, 
	`mitraDateUpdate`, 
	`mitraDataPrepared`
	)
      VALUES
         ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s',
		  '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), '%s')
   ");


//add bidang usaha
$sql['add_mkbu_by_mkid'] =
   ("
      INSERT INTO
         `aset_mitra_bidang_usaha` (`mitrabidusahaMitraId`, `mitrabidusahaJnsbidusahaId`)
      VALUES
         %VALUES%
   ");

//add cp ref
/*$sql['add_cp_ref'] =
   ("
      INSERT INTO
         `aset_mitra_cp_relation` 
			(`contactPersonNama`, `contactPersonAlamat`,`contactPersonHP`,`contactPersonTlp`,
			`contactPersonEmail`,`contactPersonFoto`,`contactPersonUpdateDate`,`contactPersonUserUpdate`
			)
      VALUES
         %VALUES%  
   ");*/
   
$sql['add_cp_ref'] =
   ("
      INSERT INTO
         `aset_ref_cp` 
			(`cpNama`, `cpAlamat`,`cpHP`,`cpTlp`,
			`cpEmail`,`cpFoto`,`cpUpdateDate`,`cpUserUpdate`
			)
      VALUES
         (%d, %d, %d, %d, %d,  %d, NOW(), %d) 
   ");

//add cp relation

$sql['add_cp_relation'] =
   ("
      INSERT INTO
         `aset_mitra_cp_relation` (`mitraCpRelasiMitraId`, `mitraCpRelasiCpId`)
      VALUES
         (%d, (SELECT MAX(cpId) FROM aset_ref_cp))
   "); 
 
 
//add bank relation
$sql['add_mkbank_by_mkid'] =
   ("
      INSERT INTO
         `aset_mitra_bank_relation` (`mitrabankMitraRefId`, `mitrabankBankRefId`)
      VALUES
         %VALUES%
   ");

//untuk bidang usaha
$sql['delete_mkbu_by_mkid'] =
   ("
      DELETE FROM
         `aset_mitra_bidang_usaha`
      WHERE
         `mitrabidusahaMitraId` = %d AND
         `mitrabidusahaJnsbidusahaId` IN (%IN%)
   ");

// untuk bank
$sql['delete_mkbank_by_mkid'] =
   ("
      DELETE FROM
         `aset_mitra_bank_relation`
      WHERE
         `mitrabankMitraRefId` = %d AND
         `mitrabankBankRefId` IN (%IN%)
   ");

// untuk contactperson
$sql['delete_mkcp_by_mkid'] =
   ("
      DELETE FROM
         `aset_mitra_cp_relation`
      WHERE
         `mitraCpRelasiMitraId` = %d AND
         `mitraCpRelasiCpId` IN (%IN%)
   ");
   
// untuk ref contactperson
$sql['delete_mkcpref_by_mkid'] =
   ("
      DELETE FROM
         `aset_ref_cp`
      WHERE
         `cpId` IN (%IN%)
   ");

$sql['delete_mitra'] =
   ("
      DELETE FROM
         `mitra_ref`
      WHERE
         `mitraId` = %d
   ");
?>