USE superbill;

DELIMITER $$


DROP PROCEDURE IF EXISTS superbill.pr_sb_superbill$$
 
CREATE PROCEDURE superbill.pr_sb_superbill
  ( pi_sb_id           INT(32)
  , pi_tp_id           VARCHAR(1024)
  , pi_token           VARCHAR(64)
  , pi_patient_id      INT(32)
  , pi_clinic_id       INT(32)
  , pi_provider_id     INT(32)  
  , pi_subject_id      INT(32)
  , pi_service_date    DATE
  , pi_abn             VARCHAR(1024)
  , pi_estimate        CHAR(1)
  , pi_uid             VARCHAR(64)
  , pi_type            CHAR(1)
  , pi_oper            CHAR(1)
  )
  
BEGIN
		
  DECLARE v_user_id       INT(32)     DEFAULT 0;
  DECLARE v_sb_id         INT(32)     DEFAULT 0;
  DECLARE v_clinic_id     INT(32)     DEFAULT 0;
  DECLARE v_cnt           INT(32)     DEFAULT 0;
  DECLARE v_role_code     CHAR(3);
  DECLARE v_change_col    VARCHAR(1024);
  DECLARE v_query      		VARCHAR(2048);
  
  SET v_user_id = superbill.fc_sb_user(pi_uid);
  SET v_role_code = superbill.fc_sb_role(pi_uid);
  SET v_clinic_id = superbill.fc_sb_clinic(pi_uid);  
  
  sb_lable:BEGIN  
  
    IF pi_oper = 'S' THEN 
      
      IF pi_type = 'T' THEN 

				SET v_query = CONCAT('
					SELECT t.tp_id 
					 , t.clinic_name
					 , t.auth_id
					 , t.joint_desc
					 , t.proc_desc
					 , t.diag_desc
					 , t.avoid_desc
					FROM superbill.vw_sb_treatment t
					WHERE t.tp_id IN(', 'PRM-1', ')
					AND t.clinic_id = IF(', v_clinic_id, '=0, ', 'clinic_id',', ', v_clinic_id,');');
	
				CALL superbill.pr_gs_dyna_sql(v_query, pi_tp_id);			

      ELSEIF pi_type = 'P' THEN 

        SELECT s.full_name
             , s.dob
             , s.employer_name
             , s.clinic_name
             , s.service_date 
             , s.superbill_items
             , s.status_txt
             , p.total_amt amount
        FROM superbill.vw_sb_superbill s
           , superbill.sb_payment_map p
        WHERE s.payment_id = p.payment_id 
        AND s.clinic_id = IF(v_clinic_id=0, clinic_id, v_clinic_id)
        AND s.status = 'Y';
        
      ELSE 
      
        SELECT s.*
        FROM superbill.vw_sb_superbill s
        WHERE ( CASE 
                WHEN pi_type = 'N' AND v_role_code = 'CLI' AND s.status IN ('N', 'J') THEN TRUE
                WHEN pi_type = 'N' AND v_role_code <> 'CLI' AND s.status IN ('P', 'G') THEN TRUE
                WHEN pi_type = 'V' AND s.status = 'V' THEN TRUE
                WHEN pi_type = 'B' AND s.status = 'B' THEN TRUE          
                WHEN pi_type = 'N' AND s.wf_flag = 'N' AND s.estimate = 'N' THEN TRUE  
                WHEN pi_type = 'A' AND s.status = 'T' THEN TRUE
                WHEN pi_type = 'Y' AND s.status = 'Y' THEN TRUE
                WHEN pi_type = 'R' AND s.wf_flag IN ('R', 'E') THEN TRUE
                ELSE pi_type = 'X' 
                END
              )               
        AND s.sb_id = IF(pi_sb_id=0, s.sb_id, pi_sb_id)
        AND s.clinic_id = IF(v_clinic_id=0, clinic_id, v_clinic_id);
      
      END IF;      
    
    ELSEIF pi_oper = 'I' THEN 
    
      SELECT COUNT(1)
      INTO v_cnt
      FROM superbill.sb_superbill 
      WHERE patient_id = pi_patient_id
      AND service_date = pi_service_date;
      
      IF v_cnt > 0 THEN 
        SET v_sb_id = -1;
        LEAVE sb_lable;
      END IF;

      INSERT INTO superbill.sb_superbill 
      SET patient_id = pi_patient_id
        , tp_id = pi_tp_id
        , token = pi_token
        , clinic_id = pi_clinic_id
        , provider_id = pi_provider_id 
        , subject_id = pi_subject_id 
        , service_date = pi_service_date    
        , abn = pi_abn 
        , estimate = pi_estimate          
        , created_by = v_user_id
        , created_on = NOW();

      SET v_sb_id = LAST_INSERT_ID();     
      
      UPDATE superbill.sb_item
      SET sb_id = v_sb_id
      WHERE token = pi_token;

      UPDATE superbill.sb_notes
      SET context_id = v_sb_id 
      WHERE token = pi_token;
      
      UPDATE superbill.sb_upload
      SET context_id = v_sb_id 
      WHERE token = pi_token;
    
    ELSEIF pi_oper = 'U' THEN 

      UPDATE superbill.sb_superbill 
      SET patient_id = pi_patient_id
        , tp_id = pi_tp_id
        , clinic_id = pi_clinic_id
        , provider_id = pi_provider_id 
        , subject_id = pi_subject_id 
        , service_date = pi_service_date    
        , abn = pi_abn 
        , estimate = pi_estimate      
        , status = IF(status='J', 'K', status)        
        , updated_by = v_user_id
        , updated_on = NOW()
      WHERE sb_id = pi_sb_id;

    ELSEIF pi_oper = 'D' THEN 
      
      UPDATE superbill.sb_superbill 
      SET active = 'N'
        , updated_by = v_user_id
        , updated_on = NOW()
      WHERE sb_id = pi_sb_id
      AND active = 'Y';

    ELSEIF pi_oper IN ('A', 'R') THEN 

      UPDATE superbill.sb_superbill 
      SET status = 
            ( CASE 
              WHEN (pi_oper = 'R') THEN 'R' 
              WHEN (pi_oper = 'A' AND v_role_code = 'SBC') THEN 'V'
              WHEN (pi_oper = 'A' AND v_role_code = 'SBS') THEN 'B'
              WHEN (pi_oper = 'A' AND v_role_code = 'SRS') THEN 'T'
              ELSE status 
              END 
            )            
        , wf_flag = pi_oper
        , updated_by = v_user_id
        , updated_on = NOW()
      WHERE sb_id = pi_sb_id;

    ELSEIF pi_oper = 'Q' THEN 

      UPDATE superbill.sb_superbill 
      SET status = 'Q'
        , updated_by = v_user_id
        , updated_on = NOW()
      WHERE sb_id = pi_sb_id;

    ELSEIF pi_oper = 'J' THEN 

      UPDATE superbill.sb_superbill 
      SET status = 'J'
        , updated_by = v_user_id
        , updated_on = NOW()
      WHERE sb_id = pi_sb_id;
      
    END IF;

    IF pi_oper IN ('I', 'U') THEN 
    
      UPDATE superbill.sb_treatment t
      SET auth_used = 'N'
      WHERE EXISTS 
        ( SELECT 1 
          FROM superbill.sb_superbill_cdc s 
          WHERE s.sb_id = pi_sb_id
          AND FIND_IN_SET(t.tp_id, s.tp_id)
        );
          
      UPDATE superbill.sb_treatment
      SET auth_used = 'Y'
      WHERE FIND_IN_SET(tp_id, pi_tp_id);
      
    END IF;
  
  END;

  IF pi_oper <> 'S' THEN 
  
    INSERT INTO superbill.sb_superbill_cdc
      ( cdc_date 
      , oper
      , sb_id
      , tp_id
      , token
      , payment_id
      , patient_id 
      , provider_id 
      , subject_id
      , service_date 
      , abn
      , estimate 
      , status 
      , wf_flag 
      )
    SELECT CURRENT_DATE 
         , pi_oper
         , sb_id 
         , tp_id 
         , token
         , payment_id
         , patient_id 
         , provider_id 
         , subject_id 
         , service_date 
         , abn 
         , estimate 
         , status 
         , wf_flag
    FROM superbill.sb_superbill
    WHERE sb_id = IF(pi_oper='I', v_sb_id, pi_sb_id);

  END IF;
  
  SELECT v_sb_id sb_id;
  
END$$

DELIMITER ;