| 1 | create view public.v_customer_invoice_detail
|
|---|
| 2 | (customer_id, customer_name, email, account_number, billing_cycle, invoice_id, invoice_number,
|
|---|
| 3 | billing_period_start, billing_period_end, issue_date, due_date, invoice_total, tax_amount, discount_amount,
|
|---|
| 4 | invoice_status, invoice_item_id, item_type, item_description, quantity, unit_price, line_amount,
|
|---|
| 5 | subscription_number, plan_name, payment_date, payment_amount, payment_status, payment_method)
|
|---|
| 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 | bc.cycle_name AS billing_cycle,
|
|---|
| 12 | i.invoice_id,
|
|---|
| 13 | i.invoice_number,
|
|---|
| 14 | i.billing_period_start,
|
|---|
| 15 | i.billing_period_end,
|
|---|
| 16 | i.issue_date,
|
|---|
| 17 | i.due_date,
|
|---|
| 18 | i.total_amount AS invoice_total,
|
|---|
| 19 | i.tax_amount,
|
|---|
| 20 | i.discount_amount,
|
|---|
| 21 | i.status AS invoice_status,
|
|---|
| 22 | ii.invoice_item_id,
|
|---|
| 23 | ii.item_type,
|
|---|
| 24 | ii.description AS item_description,
|
|---|
| 25 | ii.quantity,
|
|---|
| 26 | ii.unit_price,
|
|---|
| 27 | ii.line_amount,
|
|---|
| 28 | s.subscription_number,
|
|---|
| 29 | p.plan_name,
|
|---|
| 30 | pay.payment_date,
|
|---|
| 31 | pay.amount AS payment_amount,
|
|---|
| 32 | pay.status AS payment_status,
|
|---|
| 33 | pm.method_name AS payment_method
|
|---|
| 34 | FROM customers c
|
|---|
| 35 | JOIN accounts a ON a.customer_id = c.customer_id
|
|---|
| 36 | LEFT JOIN billing_cycles bc ON bc.billing_cycle_id = a.billing_cycle_id
|
|---|
| 37 | JOIN invoices i ON i.account_id = a.account_id
|
|---|
| 38 | JOIN invoice_items ii ON ii.invoice_id = i.invoice_id
|
|---|
| 39 | LEFT JOIN subscriptions s ON s.subscription_id = ii.subscription_id
|
|---|
| 40 | LEFT JOIN plans p ON p.plan_id = s.plan_id
|
|---|
| 41 | LEFT JOIN payments pay ON pay.invoice_id = i.invoice_id AND pay.account_id = a.account_id
|
|---|
| 42 | LEFT JOIN payment_methods pm ON pm.payment_method_id = pay.payment_method_id;
|
|---|
| 43 |
|
|---|
| 44 | alter table public.v_customer_invoice_detail
|
|---|
| 45 | owner to postgres;
|
|---|