Files
BE_IBL/sql/manual_changes/2026-05-31-pdp-birt-sp-cache-join.sql
sas.fajri d7930d5dbc FHM31052601IBL - BIRT proxy + 5 SP header decrypt via patient_print_cache
- Birt_proxy.php: decrypt PII sebelum call BIRT, cache 5 menit
- 5 SP (hasil_header, _2, _eng, fo_001, card_patient): tambah LEFT JOIN
  ke patient_print_cache dengan COALESCE fallback ke masked data
- SP signature tidak berubah, .rptdesign tidak perlu diupdate

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-31 17:33:02 +07:00

224 lines
12 KiB
SQL

-- PDP: Update 5 SP header BIRT untuk decrypt via patient_print_cache
-- SP signature TIDAK berubah — .rptdesign tidak perlu diupdate
-- Cache diisi oleh Birt_proxy.php sebelum BIRT dipanggil,
-- dan dihapus setelah PDF berhasil digenerate.
-- ============================================================
-- Tabel cache (sudah dibuat, ini untuk referensi prod)
-- ============================================================
CREATE TABLE IF NOT EXISTS one_lab.patient_print_cache (
ppc_id INT AUTO_INCREMENT PRIMARY KEY,
ppc_order_id INT NULL,
ppc_patient_id INT NULL,
ppc_name VARCHAR(200) NULL,
ppc_dob VARCHAR(20) NULL,
ppc_hp VARCHAR(25) NULL,
ppc_email VARCHAR(100) NULL,
ppc_address VARCHAR(500) NULL,
ppc_created DATETIME DEFAULT NOW(),
INDEX idx_order (ppc_order_id),
INDEX idx_patient (ppc_patient_id)
);
-- ============================================================
-- 1. sp_rpt_hasil_header
-- ============================================================
DROP PROCEDURE IF EXISTS one_lab.sp_rpt_hasil_header;
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_rpt_hasil_header`(IN `PID` int, IN `username` varchar(100))
BEGIN
-- Hapus cache expired (> 5 menit) sebagai cleanup otomatis
DELETE FROM patient_print_cache WHERE ppc_created < NOW() - INTERVAL 5 MINUTE;
SELECT
DATE_FORMAT(T_OrderHeaderDate, "%d-%m-%Y") AS T_OrderHeaderDate,
T_OrderHeaderLabNumber,
CONCAT(M_TitleName, ". ", COALESCE(NULLIF(ppc.ppc_name,''), M_PatientName)) AS M_PatientName,
m_sexname AS Gender,
M_PatientNoReg,
COALESCE(NULLIF(ppc.ppc_dob,''), DATE_FORMAT(M_PatientDOB, "%d-%m-%Y")) AS M_PatientDOB,
T_OrderHeaderM_PatientAge,
M_CompanyName AS CorporateName,
COALESCE(NULLIF(ppc.ppc_address,''),
(SELECT CONCAT(M_PatientAddressDescription,' ',IFNULL(M_DistrictName,''),' ',IFNULL(M_CityName,''))
FROM m_patientaddress AS p
LEFT JOIN (SELECT regional_cd, regional_nm AS M_KelurahanName, pro_cd, kab_cd, kec_cd FROM regional) reg_kel
ON NULLIF(TRIM(p.M_PatientAddressRegionalCd),'') = reg_kel.regional_cd
LEFT JOIN (SELECT regional_cd, regional_nm AS M_DistrictName FROM regional) reg_kec
ON CONCAT(reg_kel.pro_cd, reg_kel.kab_cd, reg_kel.kec_cd, '000') = reg_kec.regional_cd
LEFT JOIN (SELECT regional_cd, regional_nm AS M_CityName FROM regional) reg_kab
ON CONCAT(reg_kel.pro_cd, reg_kel.kab_cd, '000000') = reg_kab.regional_cd
WHERE M_PatientAddressM_PatientID = M_PatientID
ORDER BY M_PatientAddressM_PatientID LIMIT 1)
) AS M_PatientAddress,
COALESCE(NULLIF(ppc.ppc_hp,''), M_PatientHp) AS M_PatientHp,
COALESCE(NULLIF(ppc.ppc_email,''), M_PatientEmail) AS M_PatientEmail,
'' AS M_PatientAddressCity,
'' AS M_PatientAddressState,
M_CompanyName AS CorporateAddress,
M_CompanyEmail AS CorporateEmail,
M_CompanyPhone AS CorporatePhone,
M_CompanyAddressCity AS CorporateAddressCity,
'' AS CorporateAddressState,
CONCAT(IFNULL(pj.M_DoctorPrefix,''),' ',IFNULL(pj.M_DoctorPrefix2,''),' ',pj.M_DoctorName,' ',IFNULL(pj.M_DoctorSufix,''),' ',IFNULL(pj.M_DoctorSufix2,'')) AS M_DoctorName,
CONCAT(IFNULL(pjj.M_DoctorPrefix,''),' ',IFNULL(pjj.M_DoctorPrefix2,''),' ',pjj.M_DoctorName,' ',IFNULL(pjj.M_DoctorSufix,''),' ',IFNULL(pjj.M_DoctorSufix2,'')) AS M_DoctorName2,
CONCAT(COALESCE(NULLIF(ppc.ppc_dob,''), DATE_FORMAT(M_PatientDOB,'%d-%m-%Y')),' / ',T_OrderHeaderM_PatientAge) AS Umur,
M_PatientNIP, M_PatientJob, M_PatientPosisi, M_PatientDivisi, M_PatientLocation,
CONCAT(M_PatientDepartement,' - ',M_PatientNIP) AS M_PatientDepartement
FROM t_orderheader
LEFT JOIN m_patient ON T_OrderHeaderM_PatientID = M_PatientID AND M_PatientIsActive = 'Y'
LEFT JOIN m_sex ON M_PatientM_SexID = M_SexID
LEFT JOIN m_title ON M_PatientM_TitleID = M_TitleID AND M_TitleIsActive = 'Y'
JOIN m_company ON T_OrderHeaderM_CompanyID = M_CompanyID AND M_CompanyIsActive = 'Y'
LEFT JOIN m_doctor pjj ON T_OrderHeaderPj2M_DoctorID = pjj.M_DoctorID AND pjj.M_DoctorIsActive = 'Y'
LEFT JOIN m_doctor pj ON T_OrderHeaderPjM_DoctorID = pj.M_DoctorID AND pj.M_DoctorIsActive = 'Y'
LEFT JOIN patient_print_cache ppc ON ppc.ppc_order_id = T_OrderHeaderID
WHERE T_OrderHeaderID = PID AND T_OrderHeaderIsActive = 'Y';
END//
DELIMITER ;
-- ============================================================
-- 2. sp_rpt_hasil_header_2
-- ============================================================
DROP PROCEDURE IF EXISTS one_lab.sp_rpt_hasil_header_2;
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_rpt_hasil_header_2`(IN `PID` int, IN `username` varchar(100))
BEGIN
DELETE FROM patient_print_cache WHERE ppc_created < NOW() - INTERVAL 5 MINUTE;
SELECT
DATE_FORMAT(T_OrderHeaderDate, "%d-%m-%Y") AS T_OrderHeaderDate,
T_OrderHeaderLabNumber,
CONCAT(M_TitleName, ". ", COALESCE(NULLIF(ppc.ppc_name,''), M_PatientName)) AS M_PatientName,
m_sexname AS Gender,
M_PatientNoReg,
COALESCE(NULLIF(ppc.ppc_dob,''), DATE_FORMAT(M_PatientDOB,'%d-%m-%Y')) AS M_PatientDOB,
T_OrderHeaderM_PatientAge,
M_CompanyName AS CorporateName,
COALESCE(NULLIF(ppc.ppc_address,''), M_PatientAddressDescription) AS M_PatientAddress,
COALESCE(NULLIF(ppc.ppc_hp,''), M_PatientHp) AS M_PatientHp,
COALESCE(NULLIF(ppc.ppc_email,''), M_PatientEmail) AS M_PatientEmail,
M_PatientAddressCity,
M_PatientAddressState,
M_CompanyName AS CorporateAddress,
M_CompanyEmail AS CorporateEmail,
M_CompanyPhone AS CorporatePhone,
M_CompanyAddressCity AS CorporateAddressCity,
'' AS CorporateAddressState,
CONCAT(COALESCE(NULLIF(ppc.ppc_dob,''), DATE_FORMAT(M_PatientDOB,'%d-%m-%Y')),' / ',T_OrderHeaderM_PatientAge) AS Umur,
M_PatientNIP, M_PatientJob, M_PatientPosisi, M_PatientDivisi, M_PatientLocation,
CONCAT(M_PatientDepartement,' - ',M_PatientNIP) AS M_PatientDepartement
FROM t_orderheader
LEFT JOIN m_patient ON T_OrderHeaderM_PatientID = M_PatientID AND M_PatientIsActive = 'Y'
LEFT JOIN m_sex ON M_PatientM_SexID = M_SexID
LEFT JOIN m_title ON M_PatientM_TitleID = M_TitleID AND M_TitleIsActive = 'Y'
JOIN m_company ON T_OrderHeaderM_CompanyID = M_CompanyID AND M_CompanyIsActive = 'Y'
LEFT JOIN m_patientaddress pa ON pa.M_PatientAddressM_PatientID = M_PatientID AND pa.M_PatientAddressNote = 'Utama' AND pa.M_PatientAddressIsActive = 'Y'
LEFT JOIN m_doctor pjj ON T_OrderHeaderPj2M_DoctorID = pjj.M_DoctorID AND pjj.M_DoctorIsActive = 'Y'
LEFT JOIN m_doctor pj ON T_OrderHeaderPjM_DoctorID = pj.M_DoctorID AND pj.M_DoctorIsActive = 'Y'
LEFT JOIN patient_print_cache ppc ON ppc.ppc_order_id = T_OrderHeaderID
WHERE T_OrderHeaderID = PID AND T_OrderHeaderIsActive = 'Y';
END//
DELIMITER ;
-- ============================================================
-- 3. sp_rpt_hasil_header_eng
-- ============================================================
DROP PROCEDURE IF EXISTS one_lab.sp_rpt_hasil_header_eng;
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_rpt_hasil_header_eng`(IN `PID` int, IN `username` varchar(100))
BEGIN
DELETE FROM patient_print_cache WHERE ppc_created < NOW() - INTERVAL 5 MINUTE;
SELECT
DATE_FORMAT(T_OrderHeaderDate, "%d-%m-%Y") AS T_OrderHeaderDate,
T_OrderHeaderLabNumber,
CONCAT(IFNULL(titlang.M_TitleName, M_TitleName), ". ", COALESCE(NULLIF(ppc.ppc_name,''), M_PatientName)) AS M_PatientName,
m_sexname AS Gender,
M_PatientNoReg,
COALESCE(NULLIF(ppc.ppc_dob,''), DATE_FORMAT(M_PatientDOB,'%d-%m-%Y')) AS M_PatientDOB,
T_OrderHeaderM_PatientAge,
M_CompanyName AS CorporateName,
COALESCE(NULLIF(ppc.ppc_address,''), M_PatientAddressDescription) AS M_PatientAddress,
COALESCE(NULLIF(ppc.ppc_hp,''), M_PatientHp) AS M_PatientHp,
COALESCE(NULLIF(ppc.ppc_email,''), M_PatientEmail) AS M_PatientEmail,
M_PatientAddressCity,
M_PatientAddressState,
M_CompanyName AS CorporateAddress,
M_CompanyEmail AS CorporateEmail,
M_CompanyPhone AS CorporatePhone,
M_CompanyAddressCity AS CorporateAddressCity,
'' AS CorporateAddressState,
CONCAT(COALESCE(NULLIF(ppc.ppc_dob,''), DATE_FORMAT(M_PatientDOB,'%d-%m-%Y')),' / ',
REPLACE(REPLACE(REPLACE(T_OrderHeaderM_PatientAge,'tahun','year'),'bulan','month'),'hari','days')) AS Umur,
M_PatientNIP, M_PatientJob, M_PatientPosisi, M_PatientDivisi, M_PatientLocation,
CONCAT(M_PatientDepartement,' - ',M_PatientNIP) AS M_PatientDepartement
FROM t_orderheader
LEFT JOIN m_patient ON T_OrderHeaderM_PatientID = M_PatientID AND M_PatientIsActive = 'Y'
LEFT JOIN m_sex ON M_PatientM_SexID = M_SexID
LEFT JOIN m_title ON M_PatientM_TitleID = M_TitleID AND M_TitleIsActive = 'Y'
LEFT JOIN m_titlelang titlang ON titlang.M_TitleLangM_TitleID = M_TitleID AND titlang.M_TitleLangM_LangID = 2
JOIN m_company ON T_OrderHeaderM_CompanyID = M_CompanyID AND M_CompanyIsActive = 'Y'
LEFT JOIN m_patientaddress pa ON pa.M_PatientAddressM_PatientID = M_PatientID AND pa.M_PatientAddressNote = 'Utama' AND pa.M_PatientAddressIsActive = 'Y'
LEFT JOIN m_doctor pjj ON T_OrderHeaderPj2M_DoctorID = pjj.M_DoctorID AND pjj.M_DoctorIsActive = 'Y'
LEFT JOIN m_doctor pj ON T_OrderHeaderPjM_DoctorID = pj.M_DoctorID AND pj.M_DoctorIsActive = 'Y'
LEFT JOIN patient_print_cache ppc ON ppc.ppc_order_id = T_OrderHeaderID
WHERE T_OrderHeaderID = PID AND T_OrderHeaderIsActive = 'Y';
END//
DELIMITER ;
-- ============================================================
-- 4. sp_rpt_fo_001 (invoice, kuitansi, inform consent)
-- ============================================================
DROP PROCEDURE IF EXISTS one_lab.sp_rpt_fo_001;
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_rpt_fo_001`(IN `PID` int, IN `username` varchar(100))
BEGIN
DELETE FROM patient_print_cache WHERE ppc_created < NOW() - INTERVAL 5 MINUTE;
SELECT
CONCAT(M_TitleName,' ',IFNULL(M_PatientPrefix,''),' ',
COALESCE(NULLIF(ppc.ppc_name,''), M_PatientName),' ',IFNULL(M_PatientSuffix,'')) AS M_PatientName,
COALESCE(NULLIF(ppc.ppc_dob,''), DATE_FORMAT(M_PatientDOB,'%d-%m-%Y')) AS M_PatientDOB,
CONCAT(COALESCE(NULLIF(ppc.ppc_dob,''), DATE_FORMAT(M_PatientDOB,'%d-%m-%Y')),' / ',T_OrderHeaderM_PatientAge,' / ',M_SexCode) AS Umur,
COALESCE(NULLIF(ppc.ppc_hp,''), M_PatientHP) AS M_PatientHP,
COALESCE(NULLIF(ppc.ppc_hp,''), M_PatientPhone) AS M_PatientPhone,
COALESCE(NULLIF(ppc.ppc_email,''), M_PatientEmail) AS M_PatientEmail,
COALESCE(NULLIF(ppc.ppc_address,''),
(SELECT M_PatientAddressDescription FROM m_patientaddress AS p
WHERE M_PatientAddressM_PatientID = M_PatientID
ORDER BY M_PatientAddressM_PatientID LIMIT 1)
) AS M_PatientAddressDescription,
M_PatientNoReg, M_PatientIDNumber, M_PatientNIK,
T_OrderHeaderLabNumber, DATE_FORMAT(T_OrderHeaderDate,'%d-%m-%Y') AS T_OrderHeaderDate,
T_OrderHeaderID, M_CompanyName, M_PatientNote
FROM t_orderheader
LEFT JOIN m_patient ON T_OrderHeaderM_PatientID = M_PatientID
LEFT JOIN m_title ON M_PatientM_TitleID = M_TitleID
LEFT JOIN m_sex ON M_PatientM_SexID = M_SexID
JOIN m_company ON T_OrderHeaderM_CompanyID = M_CompanyID
LEFT JOIN patient_print_cache ppc ON ppc.ppc_order_id = T_OrderHeaderID
WHERE T_OrderHeaderID = PID AND T_OrderHeaderIsActive = 'Y';
END//
DELIMITER ;
-- ============================================================
-- 5. sp_rpt_card_patient (PID = M_PatientID, bukan order ID)
-- ============================================================
DROP PROCEDURE IF EXISTS one_lab.sp_rpt_card_patient;
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_rpt_card_patient`(IN `PID` int, IN `username` varchar(100))
BEGIN
DELETE FROM patient_print_cache WHERE ppc_created < NOW() - INTERVAL 5 MINUTE;
SELECT
M_PatientID,
M_PatientNoReg,
COALESCE(NULLIF(ppc.ppc_name,''), M_PatientName) AS M_PatientName,
CONCAT('http://localhost/one-api/v1/su/code128/?no=', M_PatientNoReg) AS Barcode
FROM m_patient
LEFT JOIN patient_print_cache ppc ON ppc.ppc_patient_id = M_PatientID
WHERE M_PatientID = PID;
END//
DELIMITER ;