GROUP BY xact
ORDER BY MAX(payment_ts);
+CREATE OR REPLACE VIEW money.transaction_billing_with_void_summary AS
+ SELECT xact,
+ LAST(billing_type) AS last_billing_type,
+ LAST(note) AS last_billing_note,
+ MAX(billing_ts) AS last_billing_ts,
+ SUM(CASE WHEN voided THEN 0 ELSE COALESCE(amount,0) END) AS total_owed
+ FROM money.billing
+ GROUP BY xact
+ ORDER BY MAX(billing_ts);
+
+CREATE OR REPLACE VIEW money.transaction_payment_with_void_summary AS
+ SELECT xact,
+ LAST(payment_type) AS last_payment_type,
+ LAST(note) AS last_payment_note,
+ MAX(payment_ts) as last_payment_ts,
+ SUM(CASE WHEN voided THEN 0 ELSE COALESCE(amount,0) END) AS total_paid
+ FROM money.payment_view
+ GROUP BY xact
+ ORDER BY MAX(payment_ts);
+
CREATE OR REPLACE VIEW money.open_transaction_billing_type_summary AS
SELECT xact,
billing_type AS last_billing_type,
GROUP BY xact
ORDER BY MAX(payment_ts);
+CREATE OR REPLACE VIEW money.billable_xact_with_void_summary AS
+ SELECT xact.id AS id,
+ xact.usr AS usr,
+ xact.xact_start AS xact_start,
+ xact.xact_finish AS xact_finish,
+ credit.total_paid,
+ credit.last_payment_ts,
+ credit.last_payment_note,
+ credit.last_payment_type,
+ debit.total_owed,
+ debit.last_billing_ts,
+ debit.last_billing_note,
+ debit.last_billing_type,
+ COALESCE(debit.total_owed,0) - COALESCE(credit.total_paid,0) AS balance_owed,
+ p.relname AS xact_type
+ FROM money.billable_xact xact
+ JOIN pg_class p ON (xact.tableoid = p.oid)
+ LEFT JOIN money.transaction_billing_with_void_summary debit ON (xact.id = debit.xact)
+ LEFT JOIN money.transaction_payment_with_void_summary credit ON (xact.id = credit.xact);
+
CREATE OR REPLACE VIEW money.billable_xact_summary AS
SELECT xact.id AS id,
xact.usr AS usr,