allowing all-voided bills to show there last billing... even though it was voided
authormiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Thu, 15 Jun 2006 14:48:20 +0000 (14:48 +0000)
committermiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Thu, 15 Jun 2006 14:48:20 +0000 (14:48 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@4627 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/perlmods/OpenILS/Application/Storage/Driver/Pg/dbi.pm
Open-ILS/src/sql/Pg/080.schema.money.sql

index f9cf0f1..cbd0621 100644 (file)
        #---------------------------------------------------------------------
        package money::billable_transaction_summary;
        
-       money::billable_transaction_summary->table( 'money.billable_xact_summary' );
+       money::billable_transaction_summary->table( 'money.billable_xact_with_void_summary' );
 
        #---------------------------------------------------------------------
        package money::open_user_summary;
index d3458ff..60f3a12 100644 (file)
@@ -78,6 +78,26 @@ CREATE OR REPLACE VIEW money.transaction_payment_summary AS
          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,
@@ -111,6 +131,26 @@ CREATE OR REPLACE VIEW money.open_transaction_payment_summary AS
          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,