| 1 | create view public.v_customer_payment_history
|
|---|
| 2 | (customer_id, customer_name, email, account_number, billing_cycle, invoice_number, billing_period_start,
|
|---|
| 3 | billing_period_end, invoice_total, invoice_status, payment_id, payment_date, paid_amount, payment_status,
|
|---|
| 4 | payment_method, subscription_number, item_type, billed_item_description, billed_item_amount)
|
|---|
| 5 | as
|
|---|
| 6 | SELECT c.customer_id,
|
|---|
| 7 | COALESCE((c.first_name || ' '::text) || c.last_name, c.company_name) AS customer_name,
|
|---|
| 8 | c.email,
|
|---|
| 9 | a.account_number,
|
|---|
| 10 | bc.cycle_name AS billing_cycle,
|
|---|
| 11 | i.invoice_number,
|
|---|
| 12 | i.billing_period_start,
|
|---|
| 13 | i.billing_period_end,
|
|---|
| 14 | i.total_amount AS invoice_total,
|
|---|
| 15 | i.status AS invoice_status,
|
|---|
| 16 | pay.payment_id,
|
|---|
| 17 | pay.payment_date,
|
|---|
| 18 | pay.amount AS paid_amount,
|
|---|
| 19 | pay.status AS payment_status,
|
|---|
| 20 | pm.method_name AS payment_method,
|
|---|
| 21 | s.subscription_number,
|
|---|
| 22 | ii.item_type,
|
|---|
| 23 | ii.description AS billed_item_description,
|
|---|
| 24 | ii.line_amount AS billed_item_amount
|
|---|
| 25 | FROM customers c
|
|---|
| 26 | JOIN accounts a ON a.customer_id = c.customer_id
|
|---|
| 27 | LEFT JOIN billing_cycles bc ON bc.billing_cycle_id = a.billing_cycle_id
|
|---|
| 28 | JOIN payments pay ON pay.account_id = a.account_id
|
|---|
| 29 | LEFT JOIN invoices i ON i.invoice_id = pay.invoice_id
|
|---|
| 30 | LEFT JOIN invoice_items ii ON ii.invoice_id = i.invoice_id
|
|---|
| 31 | LEFT JOIN subscriptions s ON s.subscription_id = ii.subscription_id
|
|---|
| 32 | LEFT JOIN payment_methods pm ON pm.payment_method_id = pay.payment_method_id;
|
|---|
| 33 |
|
|---|
| 34 | alter table public.v_customer_payment_history
|
|---|
| 35 | owner to postgres;
|
|---|