| 1 | create view public.v_customer_addon_invoice_detail
|
|---|
| 2 | (customer_id, customer_name, email, account_number, subscription_number, plan_name, addon_name,
|
|---|
| 3 | addon_allowance, addon_allowance_unit, addon_activated_on, addon_deactivated_on, addon_price,
|
|---|
| 4 | invoice_number, billing_period_start, billing_period_end, invoice_line_description, quantity, unit_price,
|
|---|
| 5 | line_amount, payment_date, payment_amount, payment_status)
|
|---|
| 6 | as
|
|---|
| 7 | SELECT c.customer_id,
|
|---|
| 8 | COALESCE((c.first_name || ' '::text) || c.last_name, c.company_name) AS customer_name,
|
|---|
| 9 | c.email,
|
|---|
| 10 | a.account_number,
|
|---|
| 11 | s.subscription_number,
|
|---|
| 12 | p.plan_name,
|
|---|
| 13 | ad.addon_name,
|
|---|
| 14 | ad.allowance_value AS addon_allowance,
|
|---|
| 15 | ad.allowance_unit AS addon_allowance_unit,
|
|---|
| 16 | sa.activation_date AS addon_activated_on,
|
|---|
| 17 | sa.deactivation_date AS addon_deactivated_on,
|
|---|
| 18 | sa.price_at_activation AS addon_price,
|
|---|
| 19 | i.invoice_number,
|
|---|
| 20 | i.billing_period_start,
|
|---|
| 21 | i.billing_period_end,
|
|---|
| 22 | ii.description AS invoice_line_description,
|
|---|
| 23 | ii.quantity,
|
|---|
| 24 | ii.unit_price,
|
|---|
| 25 | ii.line_amount,
|
|---|
| 26 | pay.payment_date,
|
|---|
| 27 | pay.amount AS payment_amount,
|
|---|
| 28 | pay.status AS payment_status
|
|---|
| 29 | FROM customers c
|
|---|
| 30 | JOIN accounts a ON a.customer_id = c.customer_id
|
|---|
| 31 | JOIN subscriptions s ON s.account_id = a.account_id
|
|---|
| 32 | JOIN plans p ON p.plan_id = s.plan_id
|
|---|
| 33 | LEFT JOIN subscription_addons sa ON sa.subscription_id = s.subscription_id
|
|---|
| 34 | LEFT JOIN addons ad ON ad.addon_id = sa.addon_id
|
|---|
| 35 | JOIN invoices i ON i.account_id = a.account_id
|
|---|
| 36 | JOIN invoice_items ii ON ii.invoice_id = i.invoice_id AND ii.subscription_id = s.subscription_id
|
|---|
| 37 | JOIN payments pay ON pay.invoice_id = i.invoice_id AND pay.account_id = a.account_id;
|
|---|
| 38 |
|
|---|
| 39 | alter table public.v_customer_addon_invoice_detail
|
|---|
| 40 | owner to postgres;
|
|---|