aonestar
.public
Tables
(current)
Columns
Constraints
Relationships
Orphan Tables
Anomalies
Routines
sp_tr_booking_ensure_group_folio
Parameters
Name
Type
Mode
Definition
DECLARE v_folio_id integer; BEGIN -- Reuse the booking's active B/G folio when it is valid. SELECT f.id INTO v_folio_id FROM folio f WHERE f.id = NEW.folio_id AND f.booking_id = NEW.id AND f.folio_type IN ('B', 'G') AND f.status = 'A'; -- Recover an already-created active group folio before creating a new one. IF v_folio_id IS NULL THEN SELECT f.id INTO v_folio_id FROM folio f WHERE f.booking_id = NEW.id AND f.folio_type = 'G' AND f.status = 'A' ORDER BY f.id LIMIT 1; END IF; IF v_folio_id IS NULL THEN INSERT INTO folio(folio_type, booking_id, folio_pattern_id, open_user, close_date) VALUES ('G', NEW.id, NEW.folio_pattern_id, COALESCE(NEW.modify_user::text, fn_current_user()), NEW.departure) RETURNING id INTO v_folio_id; ELSE UPDATE folio SET folio_type = 'G', close_date = NEW.departure WHERE id = v_folio_id AND folio_type = 'B'; END IF; NEW.folio_id := v_folio_id; RETURN NEW; END