                                 hepb_dose2_date::TIMESTAMPTZ,
                                 hepb_comvax1_date::TIMESTAMPTZ,
                                 hepb_comvax2_date::TIMESTAMPTZ,
+                                dose1_recvd, dose2_recvd, dose3_recvd,
+                                dose4_recvd, dose5_recvd, dose6_recvd,
+                                dose7_recvd, dose8_recvd,
                                 CASE
                                     WHEN dce.birth_date IS NOT NULL THEN
                                         CASE

                                WHEN fdd_act_code = 1 THEN birth_date
                                WHEN fdd_act_code = 2 THEN birth_date
                                WHEN fdd_act_code = 3 THEN birth_date
                                WHEN fdd_act_code = 4 THEN COALESCE(hepb_dose1_date, hepb_comvax1_date) + INTERVAL '6 months'
                                WHEN fdd_act_code = 5 THEN COALESCE(hepb_dose2_date, hepb_comvax2_date) + INTERVAL '6 months'
                                WHEN fdd_act_code = 6 THEN birth_date + INTERVAL '9 months'
                                WHEN fdd_act_code = 7 THEN NULL
                                WHEN fdd_act_code = 8 THEN NULL
                                WHEN fdd_act_code = 9 THEN NULL
                            END AS first_due_date,
                            CASE
                                WHEN fdd_act_code = 1 THEN 'Needs HBIG and Hepatitis B Dose 1'

                                -- comments inline. The current maximum value
                                -- is 9
                                CASE
                                    WHEN dce.disposition LIKE 'Closed: %' THEN -1
                                    WHEN (trisano.get_contact_hbsag_before(dce.id, CURRENT_DATE)).test_result = 'Positive / Reactive' THEN 9
                                    WHEN dce.contact_type = 'Infant' THEN
                                        CASE
                                            WHEN dce.birth_date IS NULL THEN 8 -- Enter date of birth
                                            WHEN hbig IS NULL AND CURRENT_DATE - dce.birth_date < 7 THEN 2 -- "Needs HBIG"
                                            WHEN dose1_recvd IS NULL THEN 3 -- Needs dose1
                                            WHEN dose2_recvd IS NULL THEN 4 -- Needs dose2
                                            WHEN dose3_recvd IS NULL THEN 5 -- Needs dose3
                                            WHEN 
                                                now() - dce.birth_date BETWEEN (interval '30 days' * 9) AND (interval '30 days' * 18) AND (
                                                    (trisano.get_contact_antihb_before(dce.id, CURRENT_DATE)).lab_test_date IS NULL OR
                                                    (trisano.get_contact_antihb_before(dce.id, CURRENT_DATE)).test_result IS NULL
                                                ) THEN 6 -- Needs serology
                                            WHEN (trisano.get_contact_antihb_before(dce.id, CURRENT_DATE)).test_result = 'Negative / Non-reactive' THEN 7 -- Check serology and vaccinate
                                            WHEN dose4_recvd IS NULL AND (trisano.get_contact_antihb_before(dce.id, CURRENT_DATE)).test_result = 'Negative / Non-reactive' THEN 10 -- Needs dose 5
                                            WHEN dose5_recvd IS NULL AND (trisano.get_contact_antihb_before(dce.id, CURRENT_DATE)).test_result = 'Negative / Non-reactive' THEN 11 -- Needs dose 6
                                            WHEN
                                                COALESCE(dose8_recvd, COALESCE(dose7_recvd, dose6_recvd)) IS NOT NULL AND (
                                                    (trisano.get_contact_antihb_before(dce.id, CURRENT_DATE)).lab_test_date IS NULL OR
                                                    (trisano.get_contact_antihb_before(dce.id, CURRENT_DATE)).collection_date - COALESCE(dose8_recvd, COALESCE(dose7_recvd, dose6_recvd)) >= 30 --days
                                                ) THEN 12 -- Needs Serology after Final Dose of 2nd Hepatitis B Series
                                            WHEN
                                                dose6_recvd IS NOT NULL AND 
                                                (trisano.get_contact_antihb_before(dce.id, CURRENT_DATE)).collection_date - dose6_recvd >= 30 -- days
                                                THEN 9
                                            ELSE -1
                                        END
                                    WHEN
                                        COALESCE(dose1_recvd, COALESCE(dose2_recvd, dose3_recvd)) IS NULL OR
                                        (
                                            dose3_recvd IS NOT NULL AND
                                            (
                                                (trisano.get_contact_antihb_before(dce.id, CURRENT_DATE)).collection_date IS NULL OR
                                                (trisano.get_contact_antihb_before(dce.id, CURRENT_DATE)).collection_date < dose3_recvd
                                            )
                                        ) THEN 7 -- Check history. test or vaccinate
                                    WHEN
                                        dose3_recvd IS NOT NULL AND dose6_recvd IS NULL AND
                                        (trisano.get_contact_antihb_before(dce.id, CURRENT_DATE)).test_result = 'Negative / Non-reactive' AND
                                        (trisano.get_contact_antihb_before(dce.id, CURRENT_DATE)).collection_date > dose3_recvd
                                        THEN 13 -- Complete 2nd Hep B series
                                    WHEN dose6_recvd IS NOT NULL THEN 14 -- Close after completing 2nd
                                    ELSE -1
                                END AS fdd_act_code
                            FROM
                                trisano.dw_contact_events_view dce
