Subscription — Dữ liệu & bảng đọc/ghi
Trang này cho góc nhìn database và giải thích các khái niệm bên ngoài mà Subscription tham chiếu tới (do domain khác sở hữu). Quy ước bảng: 📖 READ (đọc), ✏️ WRITE (insert/update).
Khái niệm liên quan bên ngoài
Subscription không tự sở hữu các thứ dưới đây — nó liên kết tới chúng. Hiểu chúng để biết ranh giới.
| Khái niệm | Là gì | Vai trò trong Subscription | Domain sở hữu |
|---|---|---|---|
| Term | "Kỳ học" (học kỳ) — một khoảng thời gian có lịch học/sản phẩm/suất. | Subscription link tới Term (qua SubscriptionTerm); booking & billing schedule gắn theo Term. Một subscription chạy qua nhiều Term. | Master Data |
| TermProduct | Sản phẩm/suất học cụ thể trong một Term. | Dùng để check tương thích khi link term, và resolve calendar preview (ProductIds × TermIds). | Master Data |
| Program / Product / TermProgramSet | Cấu trúc chương trình & nhóm sản phẩm. | SubscriptionProduct link tới Program; loại BookingAndBilling bắt buộc có product. | Master Data |
| TermBooking / TermBookingOrder | Booking "normal" (không qua subscription). | Khi submit subscription, hệ thống so trùng lịch với các booking normal đang Submitted/Approved. | Booking |
| Invoice / InvoiceLine / CreditNote | Hóa đơn & ghi có. | Subscription billing sinh Invoice theo schedule; Skip → regenerate invoice hoặc issue credit note. | Finance |
| Billing | "Sổ" tài chính của một booking/enrollment. | Submit booking tạo Billing; là nơi gắn invoice. | Finance |
| InvoiceScheduler | Bản ghi lên lịch xử lý invoice. | Hành động Schedule/Approve tạo InvoiceScheduler. | Finance |
| Account / Attendee / Contact | Phụ huynh / trẻ / liên hệ. | Enrollment gắn AccountId (phụ huynh) + AttendeeId (trẻ). | Master Data |
| Discount | Quy tắc giảm giá. | EnrollmentDiscount áp discount theo từng EnrollmentBillingSchedule. | Discount |
| BusinessUnit / Org (Site) | Đơn vị kinh doanh / cơ sở. | Scope dữ liệu & filter (Manager, booking). | Master Data |
📌 Các domain liên quan đã có trang riêng: Booking, Finance, Payment và Master Data.
Bảng dữ liệu — nhóm theo aggregate
| Aggregate | Bảng chính | Tác dụng |
|---|---|---|
| Subscription (template) | Subscription | Định nghĩa gói: tên, BU/Org, RecurringTypeId, BillingTypeId, publish status, CutOffMinute, TypeId. |
SubscriptionPrice, SubscriptionPriceLine | Giá & dòng giá (Xero item/account code). | |
SubscriptionProduct | Link tới Program (chỉ BookingAndBilling). | |
SubscriptionExtra, SubscriptionDiscount | Add-on/discount cấu hình ở mức template. | |
SubscriptionTerm | Link subscription ↔ Term (field Linked, IsActive, StatusId: 500 Approved / 600 Archived). | |
SubscriptionTermBillingSchedule | Lịch thanh toán khả dụng của term (tất cả kỳ có thể book). | |
| Enrollment (runtime) | Enrollment | Một ghi danh: AccountId, AttendeeId, SubscriptionId, start/end, status. |
EnrollmentPattern | Pattern lịch (dùng cho overlap guard). | |
EnrollmentPeriod | Kỳ của enrollment. | |
EnrollmentBillingSchedule | Lịch thanh toán đã book cho enrollment (nguồn cho add-on/discount). | |
EnrollmentExtra | Add-on áp theo kỳ. | |
EnrollmentDiscount | Discount áp theo EnrollmentBillingSchedule. | |
EnrollmentInvoiceLog | Log billing nội bộ (ẩn khỏi UI Billing Details). |
Bảng dữ liệu trong từng luồng
Luồng 1 — Admin setup subscription
| Hành động | Bảng | Tác dụng |
|---|---|---|
| Tạo/sửa gói | Subscription ✏️ WRITE | Lưu template. |
| Cấu hình giá | SubscriptionPrice, SubscriptionPriceLine ✏️ WRITE | Giá + dòng giá. |
| Cấu hình program | SubscriptionProduct ✏️ WRITE; Program/Product 📖 READ | Link sản phẩm. |
| Link term | SubscriptionTerm, SubscriptionTermBillingSchedule ✏️ WRITE; Term/TermProduct 📖 READ | Tạo lịch khả dụng theo term. |
| Unlink booked term | SubscriptionTerm ✏️ UPDATE (StatusId → 600, giữ IsActive=true) | Archive, giữ schedule cho Manager/Billing. |
| Re-link archived term | SubscriptionTerm ✏️ UPDATE (StatusId → 500) | Restore, giữ ProductIds/schedule, không tạo trùng. |
Luồng 2 — Khách submit booking/enrollment
| Hành động | Bảng | Tác dụng |
|---|---|---|
| Load lịch booking | SubscriptionTermBillingSchedule 📖 READ | Period/start choices (canonical). |
| Overlap guard | Enrollment, EnrollmentPattern 📖 READ; TermBookingOrder, TermBooking 📖 READ | Chặn trùng lịch (atomic fail nếu trùng). |
| Tạo runtime | Enrollment, EnrollmentPeriod, EnrollmentPattern, EnrollmentBillingSchedule ✏️ WRITE | Ghi danh thực tế. |
| Sinh tài chính | Billing ✏️ WRITE; Invoice, InvoiceLine ✏️ WRITE | Hóa đơn theo schedule (qua service Finance). |
Luồng 3 — Billing Details (Schedule / Approve / Skip)
| Hành động | Bảng | Tác dụng |
|---|---|---|
| Schedule | Invoice 📖 READ; InvoiceScheduler ✏️ WRITE | Tạo scheduler ScheduledOn = Invoice.Date (UTC). |
| Approve | Invoice ✏️ UPDATE (→ Approved); InvoiceScheduler ✏️ WRITE | Scheduler ngay (UtcNow). |
| Skip | InvoiceScheduler ✏️ UPDATE (deactivate); Invoice ✏️ UPDATE (→ Initialised 0) | Reset kỳ; có thể regenerate/credit note. |
Luồng 4 — Manage Add-ons / Discounts
| Hành động | Bảng | Tác dụng |
|---|---|---|
| Load kỳ | EnrollmentBillingSchedule 📖 READ | Chỉ kỳ đã book. |
| Lưu add-on | EnrollmentExtra ✏️ WRITE | Áp/gỡ add-on theo (add-on × kỳ). |
| Lưu discount | EnrollmentDiscount ✏️ WRITE | Áp/gỡ discount theo kỳ. |
Billing recalculation (nếu cần) đi qua service backend Finance, không phải popup.
Liên quan
- Kiến trúc & dữ liệu — quan hệ các "vật thể".
- Booking — luồng normal booking (khác subscription booking).
- Liên quan: Kiến trúc & dữ liệu, Khái niệm.