aonestar
.public
Tables
(current)
Columns
Constraints
Relationships
Orphan Tables
Anomalies
Routines
sp_sync_split_folio_departure
Parameters
Name
Type
Mode
anchor_id
integer
IN
new_departure
date
IN
user_name
text
IN (DEFAULT NULL)
Definition
DECLARE _f record; _f_schedule jsonb; BEGIN user_name := coalesce(nullif(user_name, ''), fn_current_user()); FOR _f IN SELECT f.id, f.arrival, f.departure, f.rate_id FROM registration f WHERE f.shared_register_id = $1 AND NOT f.share_rate AND f.status = 'I' AND f.departure IS DISTINCT FROM new_departure -- ป้องกันไว้: ถ้า new_departure ต่ำกว่า arrival ของ follower ช่วงคืนจะว่าง -> rate_schedule -- เป็น NULL -> sp_save_registration_rates ตกไปสาขา booking_rates แล้ว copy เรทเต็มของ -- booking มาลง follower = คิดค่าห้องซ้ำ เข้าไม่ถึงจาก caller ปัจจุบัน (ทั้งสองตัวกัน -- new_departure < fn_system_date() ด้วย 20101 และ follower.arrival <= system date เสมอ) -- แต่กันไว้เพราะผลเสียเป็นเงิน ไม่ใช่แค่ error AND new_departure >= f.arrival ORDER BY f.id LOOP UPDATE registration rg SET departure = new_departure, modify_user = user_name, modify_time = current_timestamp WHERE rg.id = _f.id; -- rate_schedule ของ follower: คงแถวเดิมไว้ แล้วเติมคืนใหม่ด้วยค่าของแถวที่ใกล้ที่สุด -- semantics เดียวกับ keep_original_rate ของ sp_verify_rate_and_available และ gap-filler -- ใน sp_save_registration_rates เอง (primary-only follower = 0 ทั้งแถบ) -- ห้ามปล่อยเป็น NULL: follower ใช้ booking_item ร่วมกับ primary สาขา booking_rates ของ -- sp_save_registration_rates จะ copy เรทเต็มมาลง follower = คิดค่าห้องซ้ำ SELECT jsonb_agg(jsonb_build_object( 'date', d.charge_date, 'amount', COALESCE(near.room_rate, 0), 'extra_adult_rate', COALESCE(near.extrabed_rate, 0), 'extra_child_rate', COALESCE(near.child_rate, 0), 'extra_infant_rate', COALESCE(near.infant_rate, 0), 'comp', COALESCE(near.comp, false), 'rateplan_id', near.rateplan_id, 'mealtype_id', near.mealtype_id ) ORDER BY d.charge_date) INTO _f_schedule FROM fn_date_range(_f.arrival, IIF(_f.arrival = new_departure, new_departure, new_departure - 1)) AS d(charge_date) LEFT JOIN LATERAL ( SELECT rr.room_rate, rr.extrabed_rate, rr.child_rate, rr.infant_rate, rr.comp, rr.rateplan_id, rr.mealtype_id FROM registration_rates rr WHERE rr.register_id = _f.id ORDER BY abs(rr.charge_date - d.charge_date), rr.charge_date LIMIT 1 ) AS near ON true; -- p_validate = false: เรทของ follower เป็น 0 โดยเจตนา (sp_checkin_split_folio / -- sp_split_inhouse_guest_folio ตั้งไว้) sp_validate_rate_amount ไม่มีคอนเซ็ปต์ของ split -- share จะฟ้อง 10801 ถ้า rate plan มี min_rate และ plan_type ไม่ใช่ 5/9 PERFORM sp_save_registration_rates(_f.id, _f.rate_id, _f.arrival, new_departure, _f_schedule, p_validate := false ); PERFORM sp_gen_request_charge_schedule(gr.id) FROM guest_request gr WHERE gr.register_id = _f.id; PERFORM sp_update_registration_summary(_f.id); -- sp_recapitulate แยกนับ extend_stay_rooms / early_checkout_rooms จาก title ของ log IF _f.departure < new_departure THEN PERFORM sp_log_inhouse(_f.id, 'Extend stay', 'Extend stay from ' ||to_char(_f.departure, 'dd/MM/yyyy')||' to ' ||to_char(new_departure, 'dd/MM/yyyy') ||' (split folio, follow register #'||$1||')', user_name); ELSE PERFORM sp_log_inhouse(_f.id, 'Change departure', 'Change departure from ' ||to_char(_f.departure, 'dd/MM/yyyy')||' to ' ||to_char(new_departure, 'dd/MM/yyyy') ||' (split folio, follow register #'||$1||')', user_name); END IF; END LOOP; END