-- Stored procedure: hitung total dari 3 tabel dan update orderSubtotal+orderTotal
DROP PROCEDURE IF EXISTS one_klinik.sync_order_total;

DELIMITER $$

CREATE PROCEDURE one_klinik.sync_order_total(IN p_order_id INT)
BEGIN
  DECLARE v_total DECIMAL(15,2);
  SET v_total = (
    COALESCE((SELECT SUM(orderDetailOrderT_PriceTotal)
              FROM one_klinik.order_detail_order
              WHERE orderDetailOrderOrderID = p_order_id
                AND orderDetailOrderIsActive = 'Y'), 0)
    +
    COALESCE((SELECT SUM(orderVaccineTotal)
              FROM one_klinik.order_vaccine
              WHERE orderVaccineOrderID = p_order_id
                AND orderVaccineIsActive = 'Y'), 0)
    +
    COALESCE((SELECT SUM(orderTindakanTotal)
              FROM one_klinik.order_tindakan
              WHERE orderTindakanOrderID = p_order_id
                AND orderTindakanIsActive = 'Y'), 0)
  );
  UPDATE one_klinik.`order`
  SET orderSubtotal = v_total,
      orderTotal    = v_total
  WHERE orderID = p_order_id;
END$$

-- ── order_detail_order ────────────────────────────────────────────────────────
DROP TRIGGER IF EXISTS one_klinik.trg_odo_ai$$
CREATE TRIGGER one_klinik.trg_odo_ai
  AFTER INSERT ON one_klinik.order_detail_order
  FOR EACH ROW
  CALL one_klinik.sync_order_total(NEW.orderDetailOrderOrderID)$$

DROP TRIGGER IF EXISTS one_klinik.trg_odo_au$$
CREATE TRIGGER one_klinik.trg_odo_au
  AFTER UPDATE ON one_klinik.order_detail_order
  FOR EACH ROW
  CALL one_klinik.sync_order_total(NEW.orderDetailOrderOrderID)$$

DROP TRIGGER IF EXISTS one_klinik.trg_odo_ad$$
CREATE TRIGGER one_klinik.trg_odo_ad
  AFTER DELETE ON one_klinik.order_detail_order
  FOR EACH ROW
  CALL one_klinik.sync_order_total(OLD.orderDetailOrderOrderID)$$

-- ── order_vaccine ─────────────────────────────────────────────────────────────
DROP TRIGGER IF EXISTS one_klinik.trg_ov_ai$$
CREATE TRIGGER one_klinik.trg_ov_ai
  AFTER INSERT ON one_klinik.order_vaccine
  FOR EACH ROW
  CALL one_klinik.sync_order_total(NEW.orderVaccineOrderID)$$

DROP TRIGGER IF EXISTS one_klinik.trg_ov_au$$
CREATE TRIGGER one_klinik.trg_ov_au
  AFTER UPDATE ON one_klinik.order_vaccine
  FOR EACH ROW
  CALL one_klinik.sync_order_total(NEW.orderVaccineOrderID)$$

DROP TRIGGER IF EXISTS one_klinik.trg_ov_ad$$
CREATE TRIGGER one_klinik.trg_ov_ad
  AFTER DELETE ON one_klinik.order_vaccine
  FOR EACH ROW
  CALL one_klinik.sync_order_total(OLD.orderVaccineOrderID)$$

-- ── order_tindakan ────────────────────────────────────────────────────────────
DROP TRIGGER IF EXISTS one_klinik.trg_ot_ai$$
CREATE TRIGGER one_klinik.trg_ot_ai
  AFTER INSERT ON one_klinik.order_tindakan
  FOR EACH ROW
  CALL one_klinik.sync_order_total(NEW.orderTindakanOrderID)$$

DROP TRIGGER IF EXISTS one_klinik.trg_ot_au$$
CREATE TRIGGER one_klinik.trg_ot_au
  AFTER UPDATE ON one_klinik.order_tindakan
  FOR EACH ROW
  CALL one_klinik.sync_order_total(NEW.orderTindakanOrderID)$$

DROP TRIGGER IF EXISTS one_klinik.trg_ot_ad$$
CREATE TRIGGER one_klinik.trg_ot_ad
  AFTER DELETE ON one_klinik.order_tindakan
  FOR EACH ROW
  CALL one_klinik.sync_order_total(OLD.orderTindakanOrderID)$$

DELIMITER ;
