[open-ils-commits] [GIT] Evergreen ILS branch rel_3_5 updated. 443ea5724ad3830eaf448d4ebe76c5f557cba42f

Evergreen Git git at git.evergreen-ils.org
Tue Apr 28 14:30:31 EDT 2020


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "Evergreen ILS".

The branch, rel_3_5 has been updated
       via  443ea5724ad3830eaf448d4ebe76c5f557cba42f (commit)
       via  66574072ae064cd81bd43f8438d6f906e1ae88ab (commit)
       via  c40ba60f944c3d56700e0b9ce43d00b1460ee71a (commit)
       via  67c97b0d82910ad9a6044bf3921a7aa91e849c05 (commit)
       via  5d9a2200fa649dfc84e8d0cc29cc59eb76f6f973 (commit)
       via  0472139309b3ff83bb5257eb9c5f67ed8cda0779 (commit)
       via  676410920c651bb5898d099d8d43d48be0ed1964 (commit)
      from  d940bd6995626bf3cbf180b7ed95066e35df9520 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 443ea5724ad3830eaf448d4ebe76c5f557cba42f
Author: Bill Erickson <berickxx at gmail.com>
Date:   Fri Apr 24 10:42:42 2020 -0400

    LP1858448 Disable inititial aged money migration
    
    Modify the existing SQL upgrade scripts to prevent the automatic
    migration of billings/payments to the new aged money tables.  This is
    done to prevent data loss for sites which have not yet upgraded to the
    affected versions.
    
    The schema changes are kept so both pre- and post-upgrade sites can
    follow the same schema update path.
    
    Signed-off-by: Bill Erickson <berickxx at gmail.com>
    Signed-off-by: Jason Stephenson <jason at sigio.com>

diff --git a/Open-ILS/src/sql/Pg/upgrade/1181.schema.aged-billing-payment.sql b/Open-ILS/src/sql/Pg/upgrade/1181.schema.aged-billing-payment.sql
index 5fbfd69788..e1dbf18c22 100644
--- a/Open-ILS/src/sql/Pg/upgrade/1181.schema.aged-billing-payment.sql
+++ b/Open-ILS/src/sql/Pg/upgrade/1181.schema.aged-billing-payment.sql
@@ -10,6 +10,9 @@ ALTER TABLE money.aged_payment ADD COLUMN payment_type TEXT NOT NULL;
 
 CREATE TABLE money.aged_billing (LIKE money.billing INCLUDING INDEXES);
 
+
+/* LP 1858448 : Disable initial aged money migration
+
 INSERT INTO money.aged_payment 
     SELECT  mp.* FROM money.payment_view mp
     JOIN action.aged_circulation circ ON (circ.id = mp.xact);
@@ -17,6 +20,7 @@ INSERT INTO money.aged_payment
 INSERT INTO money.aged_billing
     SELECT mb.* FROM money.billing mb
     JOIN action.aged_circulation circ ON (circ.id = mb.xact);
+*/
 
 CREATE OR REPLACE VIEW money.all_payments AS
     SELECT * FROM money.payment_view 
@@ -65,6 +69,8 @@ BEGIN
 
     -- Migrate billings and payments to aged tables
 
+
+/* LP 1858448 : Disable initial aged money migration
     INSERT INTO money.aged_billing
         SELECT * FROM money.billing WHERE xact = OLD.id;
 
@@ -73,11 +79,15 @@ BEGIN
 
     DELETE FROM money.payment WHERE xact = OLD.id;
     DELETE FROM money.billing WHERE xact = OLD.id;
+*/
 
     RETURN OLD;
 END;
 $$ LANGUAGE 'plpgsql';
 
+
+/* LP 1858448 : Disable initial aged money migration
+
 -- NOTE you could COMMIT here then start a new TRANSACTION if desired.
 
 \qecho Deleting aged payments and billings from active payment/billing
@@ -128,8 +138,12 @@ ALTER TABLE money.account_adjustment
     ADD CONSTRAINT account_adjustment_billing_fkey 
     FOREIGN KEY (billing) REFERENCES money.billing (id);
 
+*/
+
 COMMIT;
 
 -- Good to run after truncating -- OK to run after COMMIT.
+/* LP 1858448 : Disable initial aged money migration
 ANALYZE money.billing;
+*/
 
diff --git a/Open-ILS/src/sql/Pg/upgrade/1192.schema.fix_circ_aging.sql b/Open-ILS/src/sql/Pg/upgrade/1192.schema.fix_circ_aging.sql
index 8a1cf92a4a..eed4d5b267 100644
--- a/Open-ILS/src/sql/Pg/upgrade/1192.schema.fix_circ_aging.sql
+++ b/Open-ILS/src/sql/Pg/upgrade/1192.schema.fix_circ_aging.sql
@@ -41,6 +41,8 @@ BEGIN
 
     -- Migrate billings and payments to aged tables
 
+
+/* LP 1858448 : Disable initial aged money migration
     INSERT INTO money.aged_billing
         SELECT * FROM money.billing WHERE xact = OLD.id;
 
@@ -50,6 +52,8 @@ BEGIN
     DELETE FROM money.payment WHERE xact = OLD.id;
     DELETE FROM money.billing WHERE xact = OLD.id;
 
+*/
+
     RETURN OLD;
 END;
 $$ LANGUAGE 'plpgsql';
diff --git a/Open-ILS/src/sql/Pg/version-upgrade/3.3.3-3.4.0-upgrade-db.sql b/Open-ILS/src/sql/Pg/version-upgrade/3.3.3-3.4.0-upgrade-db.sql
index 8cd090e1cc..4a88a1b4ca 100644
--- a/Open-ILS/src/sql/Pg/version-upgrade/3.3.3-3.4.0-upgrade-db.sql
+++ b/Open-ILS/src/sql/Pg/version-upgrade/3.3.3-3.4.0-upgrade-db.sql
@@ -522,6 +522,9 @@ ALTER TABLE money.aged_payment ADD COLUMN payment_type TEXT NOT NULL;
 
 CREATE TABLE money.aged_billing (LIKE money.billing INCLUDING INDEXES);
 
+
+/* LP 1858448 : Disable initial aged money migration
+
 INSERT INTO money.aged_payment 
     SELECT  mp.* FROM money.payment_view mp
     JOIN action.aged_circulation circ ON (circ.id = mp.xact);
@@ -530,6 +533,8 @@ INSERT INTO money.aged_billing
     SELECT mb.* FROM money.billing mb
     JOIN action.aged_circulation circ ON (circ.id = mb.xact);
 
+*/
+
 CREATE OR REPLACE VIEW money.all_payments AS
     SELECT * FROM money.payment_view 
     UNION ALL
@@ -577,6 +582,7 @@ BEGIN
 
     -- Migrate billings and payments to aged tables
 
+/* LP 1858448 : Disable initial aged money migration
     INSERT INTO money.aged_billing
         SELECT * FROM money.billing WHERE xact = OLD.id;
 
@@ -585,11 +591,15 @@ BEGIN
 
     DELETE FROM money.payment WHERE xact = OLD.id;
     DELETE FROM money.billing WHERE xact = OLD.id;
+*/
 
     RETURN OLD;
 END;
 $$ LANGUAGE 'plpgsql';
 
+
+/* LP 1858448 : Disable initial aged money migration
+
 -- NOTE you could COMMIT here then start a new TRANSACTION if desired.
 
 \qecho Deleting aged payments and billings from active payment/billing
@@ -644,6 +654,8 @@ ALTER TABLE money.account_adjustment
 -- Good to run after truncating -- OK to run after COMMIT.
 ANALYZE money.billing;
 
+*/
+
 
 SELECT evergreen.upgrade_deps_block_check('1182', :eg_version);
 
@@ -797,6 +809,7 @@ BEGIN
 
     -- Migrate billings and payments to aged tables
 
+/* LP 1858448 : Disable initial aged money migration
     INSERT INTO money.aged_billing
         SELECT * FROM money.billing WHERE xact = OLD.id;
 
@@ -805,6 +818,7 @@ BEGIN
 
     DELETE FROM money.payment WHERE xact = OLD.id;
     DELETE FROM money.billing WHERE xact = OLD.id;
+*/
 
     RETURN OLD;
 END;

commit 66574072ae064cd81bd43f8438d6f906e1ae88ab
Author: Bill Erickson <berickxx at gmail.com>
Date:   Tue Apr 14 10:48:32 2020 -0400

    LP1858448 Upgrade instructions WIP
    
    Signed-off-by: Bill Erickson <berickxx at gmail.com>
    Signed-off-by: Jason Stephenson <jason at sigio.com>

diff --git a/docs/RELEASE_NOTES_NEXT/Administration/aged-money.adoc b/docs/RELEASE_NOTES_NEXT/Administration/aged-money.adoc
index f6d736f99d..82bc38a6c2 100644
--- a/docs/RELEASE_NOTES_NEXT/Administration/aged-money.adoc
+++ b/docs/RELEASE_NOTES_NEXT/Administration/aged-money.adoc
@@ -19,3 +19,24 @@ Aged Payment Additional Fields
 The aged payment table now has accepting_usr, cash_drawer, and billing
 columns to improve reporting of aged money.
 
+Manual Data Migration of Aged Money
++++++++++++++++++++++++++++++++++++
+
+For users that wish to age money along with circulations (global flag 
+'history.money.age_with_circs' is set to true), it's necessary to manaully
+age money for circulations which have already been aged.  This can be
+done directly in the database with SQL:
+
+NOTE: This SQL can take a very long time to run on large databases, so
+it may be necessary to process aged circulations in batches instead
+of all at once.
+
+[source,sql]
+-------------------------------------------------------------------------
+SELECT money.age_billings_and_payments_for_xact(circ.id)
+FROM action.aged_circulation circ
+-- limit to aged circs with billings
+JOIN money.billing mb ON mb.xact = circ.id;
+-------------------------------------------------------------------------
+
+

commit c40ba60f944c3d56700e0b9ce43d00b1460ee71a
Author: Bill Erickson <berickxx at gmail.com>
Date:   Tue Apr 14 10:28:09 2020 -0400

    LP1858448 Money aging srfsh script function repair
    
    Fixes a copy pasto in the money aging script, teaching it call the
    correct money aging function.
    
    Signed-off-by: Bill Erickson <berickxx at gmail.com>
    Signed-off-by: Jason Stephenson <jason at sigio.com>

diff --git a/Open-ILS/src/support-scripts/age_money.srfsh b/Open-ILS/src/support-scripts/age_money.srfsh
index 533403c61a..7aa55ac479 100755
--- a/Open-ILS/src/support-scripts/age_money.srfsh
+++ b/Open-ILS/src/support-scripts/age_money.srfsh
@@ -1,7 +1,7 @@
 #!BINDIR/srfsh
 open open-ils.cstore
 request open-ils.cstore open-ils.cstore.transaction.begin
-request open-ils.cstore open-ils.cstore.json_query {"from":["action.purge_circulations"]}
+request open-ils.cstore open-ils.cstore.json_query {"from":["money.age_billings_and_payments"]}
 request open-ils.cstore open-ils.cstore.transaction.commit
 close open-ils.cstore
 

commit 67c97b0d82910ad9a6044bf3921a7aa91e849c05
Author: Jason Stephenson <jason at sigio.com>
Date:   Wed Mar 11 12:47:55 2020 -0400

    LP1858448 More IDL fixups
    
    Make IDL fixes as recommended in the Launchpad bug discussion.
    
    Signed-off-by: Jason Stephenson <jason at sigio.com>
    Signed-off-by: John Amundson <jamundson at cwmars.org>
    Signed-off-by: Bill Erickson <berickxx at gmail.com>

diff --git a/Open-ILS/examples/fm_IDL.xml b/Open-ILS/examples/fm_IDL.xml
index 3cc362ab73..e849f66e2c 100644
--- a/Open-ILS/examples/fm_IDL.xml
+++ b/Open-ILS/examples/fm_IDL.xml
@@ -4943,8 +4943,8 @@ SELECT  usr,
 			<link field="target_copy" reltype="has_a" key="id" map="" class="acp"/>
 			<link field="checkin_staff" reltype="has_a" key="id" map="" class="au"/>
 			<link field="circ_lib" reltype="has_a" key="id" map="" class="aou"/>
-			<link field="payments" reltype="has_many" key="xact" map="" class="map"/>
-			<link field="billings" reltype="has_many" key="xact" map="" class="mab"/>
+			<link field="payments" reltype="has_many" key="xact" map="" class="mallp"/>
+			<link field="billings" reltype="has_many" key="xact" map="" class="mallb"/>
 			<link field="duration_rule" reltype="has_a" key="name" map="" class="crcd"/>
 			<link field="max_fine_rule" reltype="has_a" key="name" map="" class="crmf"/>
 			<link field="recurring_fine_rule" reltype="has_a" key="name" map="" class="crrf"/>
@@ -8065,7 +8065,7 @@ SELECT  usr,
 			<link field="perm" reltype="has_a" key="id" map="" class="ppl"/>
 		</links>
 	</class>
-	<class id="mp" controller="open-ils.cstore open-ils.pcrud" oils_obj:fieldmapper="money::payment" oils_persist:tablename="money.payment_view" reporter:core="true" reporter:label="Payments: All">
+	<class id="mp" controller="open-ils.cstore open-ils.pcrud" oils_obj:fieldmapper="money::payment" oils_persist:tablename="money.payment_view" reporter:label="Payments">
 		<fields oils_persist:primary="id" oils_persist:sequence="">
 			<field reporter:label="Amount" name="amount" reporter:datatype="money" />
 			<field reporter:label="Payment ID" name="id" reporter:datatype="id" />
@@ -8137,14 +8137,15 @@ SELECT  usr,
 	<class id="mallp" controller="open-ils.cstore" 
 		oils_obj:fieldmapper="money::all_payments" 
 		oils_persist:tablename="money.all_payments" 
-		oils_persist="readonly" reporter:label="All Payments">
+		oils_persist="readonly" reporter:core="true"
+		reporter:label="Payments: All">
 		<fields oils_persist:primary="id" oils_persist:sequence="money.payment_id_seq">
 			<field reporter:label="Amount" name="amount" reporter:datatype="money" />
 			<field reporter:label="Payment ID" name="id" reporter:datatype="id" />
 			<field reporter:label="Note" name="note" reporter:datatype="text"/>
 			<field reporter:label="Payment Date/Time" name="payment_ts" reporter:datatype="timestamp"/>
 			<field reporter:label="Payment Type" name="payment_type" reporter:datatype="text"/>
-			<field reporter:label="Aged Circulation" name="xact" reporter:datatype="link"/>
+			<field reporter:label="Billable Transaction" name="xact" reporter:datatype="link"/>
 			<field reporter:label="Voided?" name="voided" reporter:datatype="bool"/>
 			<field reporter:label="Accepting User" name="accepting_usr" reporter:datatype="link"/>
 			<field reporter:label="Cash Drawer" name="cash_drawer" reporter:datatype="link"/>
@@ -8154,7 +8155,7 @@ SELECT  usr,
 			<link field="xact" reltype="has_a" key="id" map="" class="mbt"/>
 			<link field="accepting_usr" reltype="has_a" key="id" map="" class="au"/>
 			<link field="cash_drawer" reltype="has_a" key="id" map="" class="aws"/>
-			<link field="billing" reltype="has_a" key="id" class="mab"/>
+			<link field="billing" reltype="has_a" key="id" class="mallb"/>
 		</links>
 	</class>
 
@@ -8488,9 +8489,7 @@ SELECT  usr,
 		<links>
 			<link field="voider" reltype="has_a" key="id" map="" class="au"/>
 			<link field="btype" reltype="has_a" key="id" map="" class="cbt"/>
-			<!-- 
-				'xact' may link to a money.billabl_xact or to a money.aged_circulation.
-			 -->
+			<link field="xact" reltype="has_a" key="id" map="" class="mbt"/>
 		</links>
 	</class>
 	<class id="pugm" controller="open-ils.cstore" oils_obj:fieldmapper="permission::usr_grp_map" oils_persist:tablename="permission.usr_grp_map" reporter:label="User Group Map">

commit 5d9a2200fa649dfc84e8d0cc29cc59eb76f6f973
Author: Bill Erickson <berickxx at gmail.com>
Date:   Thu Feb 13 11:01:41 2020 -0500

    LP1858448 Aged money control flags
    
    Adds 2 global flags for controling if/when billings and payments are
    aged.
    
    history.money.age_with_circs
    history.money.retention_age
    
    Adds a srfsh script for manually aging money based on the
    'history.money.retention_age' setting value.
    
    Signed-off-by: Bill Erickson <berickxx at gmail.com>
    Signed-off-by: John Amundson <jamundson at cwmars.org>
    Signed-off-by: Jason Stephenson <jason at sigio.com>

diff --git a/Open-ILS/src/Makefile.am b/Open-ILS/src/Makefile.am
index 9a0d5a7549..87a8b00488 100644
--- a/Open-ILS/src/Makefile.am
+++ b/Open-ILS/src/Makefile.am
@@ -75,6 +75,7 @@ core_scripts =   $(examples)/oils_ctl.sh \
 		 $(supportscr)/purge_circulations.srfsh \
 		 $(supportscr)/purge_pending_users.srfsh \
 		 $(supportscr)/clear_cc_number.srfsh \
+		 $(supportscr)/age_money.srfsh \
 		 $(supportscr)/sitemap_generator \
 		 $(srcdir)/extras/eg_config \
 		 $(srcdir)/extras/openurl_map.pl \
@@ -288,6 +289,7 @@ ilscore-install:
 	sed -i 's|SYSCONFDIR|@sysconfdir@|g' '$(DESTDIR)@bindir@/long-overdue-status-update.pl'
 	sed -i 's|BINDIR|@bindir@|g' '$(DESTDIR)@bindir@/thaw_expired_frozen_holds.srfsh'
 	sed -i 's|BINDIR|@bindir@|g' '$(DESTDIR)@bindir@/clear_cc_number.srfsh'
+	sed -i 's|BINDIR|@bindir@|g' '$(DESTDIR)@bindir@/age_money.srfsh'
 	sed -i 's|BINDIR|@bindir@|g' '$(DESTDIR)@bindir@/purge_at_events.srfsh'
 	sed -i 's|BINDIR|@bindir@|g' '$(DESTDIR)@bindir@/purge_circulations.srfsh'
 	sed -i 's|BINDIR|@bindir@|g' '$(DESTDIR)@bindir@/purge_holds.srfsh'
diff --git a/Open-ILS/src/sql/Pg/080.schema.money.sql b/Open-ILS/src/sql/Pg/080.schema.money.sql
index 631c7a5e0e..2589923cff 100644
--- a/Open-ILS/src/sql/Pg/080.schema.money.sql
+++ b/Open-ILS/src/sql/Pg/080.schema.money.sql
@@ -701,7 +701,7 @@ CREATE OR REPLACE VIEW money.cashdrawer_payment_view AS
 		LEFT JOIN money.payment_view t ON (p.id = t.id);
 
 -- serves as the basis for the aged payments data.
-CREATE OR REPLACE VIEW money.payment_view_extended AS
+CREATE OR REPLACE VIEW money.payment_view_for_aging AS
     SELECT p.*,
         bnm.accepting_usr,
         bnmd.cash_drawer,
@@ -726,7 +726,7 @@ CREATE INDEX aged_payment_billing_idx ON money.aged_payment(billing);
 CREATE TABLE money.aged_billing (LIKE money.billing INCLUDING INDEXES);
 
 CREATE OR REPLACE VIEW money.all_payments AS
-    SELECT * FROM money.payment_view_extended
+    SELECT * FROM money.payment_view_for_aging
     UNION ALL
     SELECT * FROM money.aged_payment;
 
@@ -735,5 +735,60 @@ CREATE OR REPLACE VIEW money.all_billings AS
     UNION ALL
     SELECT * FROM money.aged_billing;
 
+CREATE OR REPLACE FUNCTION money.age_billings_and_payments() RETURNS INTEGER AS $FUNC$
+-- Age billings and payments linked to transactions which were 
+-- completed at least 'older_than' time ago.
+DECLARE
+    xact_id BIGINT;
+    counter INTEGER DEFAULT 0;
+    keep_age INTERVAL;
+BEGIN
+
+    SELECT value::INTERVAL INTO keep_age FROM config.global_flag 
+        WHERE name = 'history.money.retention_age' AND enabled;
+
+    -- Confirm interval-based aging is enabled.
+    IF keep_age IS NULL THEN RETURN counter; END IF;
+
+    -- Start with non-circulation transactions
+    FOR xact_id IN SELECT DISTINCT(xact.id) FROM money.billable_xact xact
+        -- confirm there is something to age
+        JOIN money.billing mb ON mb.xact = xact.id
+        -- Avoid aging money linked to non-aged circulations.
+        LEFT JOIN action.circulation circ ON circ.id = xact.id
+        WHERE circ.id IS NULL AND AGE(NOW(), xact.xact_finish) > keep_age LOOP
+
+        PERFORM money.age_billings_and_payments_for_xact(xact_id);
+        counter := counter + 1;
+    END LOOP;
+
+    -- Then handle aged circulation money.
+    FOR xact_id IN SELECT DISTINCT(xact.id) FROM action.aged_circulation xact
+        -- confirm there is something to age
+        JOIN money.billing mb ON mb.xact = xact.id
+        WHERE AGE(NOW(), xact.xact_finish) > keep_age LOOP
+
+        PERFORM money.age_billings_and_payments_for_xact(xact_id);
+        counter := counter + 1;
+    END LOOP;
+
+    RETURN counter;
+END;
+$FUNC$ LANGUAGE PLPGSQL;
+
+CREATE OR REPLACE FUNCTION money.age_billings_and_payments_for_xact
+    (xact_id BIGINT) RETURNS VOID AS $FUNC$
+
+    INSERT INTO money.aged_billing
+        SELECT * FROM money.billing WHERE xact = $1;
+
+    INSERT INTO money.aged_payment 
+        SELECT * FROM money.payment_view_for_aging WHERE xact = xact_id;
+
+    DELETE FROM money.payment WHERE xact = $1;
+    DELETE FROM money.billing WHERE xact = $1;
+
+$FUNC$ LANGUAGE SQL;
+
 COMMIT;
 
diff --git a/Open-ILS/src/sql/Pg/090.schema.action.sql b/Open-ILS/src/sql/Pg/090.schema.action.sql
index ae1fa9adfd..af8a3e36b9 100644
--- a/Open-ILS/src/sql/Pg/090.schema.action.sql
+++ b/Open-ILS/src/sql/Pg/090.schema.action.sql
@@ -314,7 +314,6 @@ UNION ALL
 ;
 
 
-
 CREATE OR REPLACE FUNCTION action.age_circ_on_delete () RETURNS TRIGGER AS $$
 DECLARE
 found char := 'N';
@@ -354,14 +353,12 @@ BEGIN
 
     -- Migrate billings and payments to aged tables
 
-    INSERT INTO money.aged_billing
-        SELECT * FROM money.billing WHERE xact = OLD.id;
-
-    INSERT INTO money.aged_payment 
-        SELECT * FROM money.payment_view_extended WHERE xact = OLD.id;
+    SELECT 'Y' INTO found FROM config.global_flag 
+        WHERE name = 'history.money.age_with_circs' AND enabled;
 
-    DELETE FROM money.payment WHERE xact = OLD.id;
-    DELETE FROM money.billing WHERE xact = OLD.id;
+    IF found = 'Y' THEN
+        PERFORM money.age_billings_and_payments_for_xact(OLD.id);
+    END IF;
 
     RETURN OLD;
 END;
diff --git a/Open-ILS/src/sql/Pg/950.data.seed-values.sql b/Open-ILS/src/sql/Pg/950.data.seed-values.sql
index 0b30009bee..b74f672a5d 100644
--- a/Open-ILS/src/sql/Pg/950.data.seed-values.sql
+++ b/Open-ILS/src/sql/Pg/950.data.seed-values.sql
@@ -20375,3 +20375,27 @@ VALUES (
     )
 );
 
+INSERT INTO config.global_flag (name, value, enabled, label)
+VALUES (
+    'history.money.age_with_circs',
+    NULL, 
+    FALSE,
+    oils_i18n_gettext(
+        'history.money.age_with_circs',
+        'Age billings and payments when cirulcations are aged.',
+        'cgf', 'label'
+    )
+), (
+    'history.money.retention_age',
+    NULL, 
+    FALSE,
+    oils_i18n_gettext(
+        'history.money.retention_age',
+        'Age billings and payments whose transactions were completed ' ||
+        'this long ago.  For circulation transactions, this setting ' ||
+        'is superseded by the "history.money.age_with_circs" setting',
+        'cgf', 'label'
+    )
+);
+
+
diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.aged-money-fields.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.aged-money-fields.sql
index 456ea11b11..6cd0738c0e 100644
--- a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.aged-money-fields.sql
+++ b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.aged-money-fields.sql
@@ -2,9 +2,32 @@ BEGIN;
 
 -- SELECT evergreen.upgrade_deps_block_check('TODO', :eg_version);
 
+INSERT INTO config.global_flag (name, value, enabled, label)
+VALUES (
+    'history.money.age_with_circs',
+    NULL, 
+    FALSE,
+    oils_i18n_gettext(
+        'history.money.age_with_circs',
+        'Age billings and payments when cirulcations are aged.',
+        'cgf', 'label'
+    )
+), (
+    'history.money.retention_age',
+    NULL, 
+    FALSE,
+    oils_i18n_gettext(
+        'history.money.retention_age',
+        'Age billings and payments whose transactions were completed ' ||
+        'this long ago.  For circulation transactions, this setting ' ||
+        'is superseded by the "history.money.age_with_circs" setting',
+        'cgf', 'label'
+    )
+);
+
 DROP VIEW money.all_payments;
 
-CREATE OR REPLACE VIEW money.payment_view_extended AS
+CREATE OR REPLACE VIEW money.payment_view_for_aging AS
     SELECT p.*,
         bnm.accepting_usr,
         bnmd.cash_drawer,
@@ -24,10 +47,65 @@ CREATE INDEX aged_payment_cash_drawer_idx ON money.aged_payment(cash_drawer);
 CREATE INDEX aged_payment_billing_idx ON money.aged_payment(billing);
 
 CREATE OR REPLACE VIEW money.all_payments AS
-    SELECT * FROM money.payment_view_extended
+    SELECT * FROM money.payment_view_for_aging
     UNION ALL
     SELECT * FROM money.aged_payment;
 
+CREATE OR REPLACE FUNCTION money.age_billings_and_payments() RETURNS INTEGER AS $FUNC$
+-- Age billings and payments linked to transactions which were 
+-- completed at least 'older_than' time ago.
+DECLARE
+    xact_id BIGINT;
+    counter INTEGER DEFAULT 0;
+    keep_age INTERVAL;
+BEGIN
+
+    SELECT value::INTERVAL INTO keep_age FROM config.global_flag 
+        WHERE name = 'history.money.retention_age' AND enabled;
+
+    -- Confirm interval-based aging is enabled.
+    IF keep_age IS NULL THEN RETURN counter; END IF;
+
+    -- Start with non-circulation transactions
+    FOR xact_id IN SELECT DISTINCT(xact.id) FROM money.billable_xact xact
+        -- confirm there is something to age
+        JOIN money.billing mb ON mb.xact = xact.id
+        -- Avoid aging money linked to non-aged circulations.
+        LEFT JOIN action.circulation circ ON circ.id = xact.id
+        WHERE circ.id IS NULL AND AGE(NOW(), xact.xact_finish) > keep_age LOOP
+
+        PERFORM money.age_billings_and_payments_for_xact(xact_id);
+        counter := counter + 1;
+    END LOOP;
+
+    -- Then handle aged circulation money.
+    FOR xact_id IN SELECT DISTINCT(xact.id) FROM action.aged_circulation xact
+        -- confirm there is something to age
+        JOIN money.billing mb ON mb.xact = xact.id
+        WHERE AGE(NOW(), xact.xact_finish) > keep_age LOOP
+
+        PERFORM money.age_billings_and_payments_for_xact(xact_id);
+        counter := counter + 1;
+    END LOOP;
+
+    RETURN counter;
+END;
+$FUNC$ LANGUAGE PLPGSQL;
+
+CREATE OR REPLACE FUNCTION money.age_billings_and_payments_for_xact
+    (xact_id BIGINT) RETURNS VOID AS $FUNC$
+
+    INSERT INTO money.aged_billing
+        SELECT * FROM money.billing WHERE xact = $1;
+
+    INSERT INTO money.aged_payment 
+        SELECT * FROM money.payment_view_for_aging WHERE xact = xact_id;
+
+    DELETE FROM money.payment WHERE xact = $1;
+    DELETE FROM money.billing WHERE xact = $1;
+
+$FUNC$ LANGUAGE SQL;
+
 CREATE OR REPLACE FUNCTION action.age_circ_on_delete () RETURNS TRIGGER AS $$
 DECLARE
 found char := 'N';
@@ -67,18 +145,15 @@ BEGIN
 
     -- Migrate billings and payments to aged tables
 
-    INSERT INTO money.aged_billing
-        SELECT * FROM money.billing WHERE xact = OLD.id;
-
-    INSERT INTO money.aged_payment 
-        SELECT * FROM money.payment_view_extended WHERE xact = OLD.id;
+    SELECT 'Y' INTO found FROM config.global_flag 
+        WHERE name = 'history.money.age_with_circs' AND enabled;
 
-    DELETE FROM money.payment WHERE xact = OLD.id;
-    DELETE FROM money.billing WHERE xact = OLD.id;
+    IF found = 'Y' THEN
+        PERFORM money.age_billings_and_payments_for_xact(OLD.id);
+    END IF;
 
     RETURN OLD;
 END;
 $$ LANGUAGE 'plpgsql';
 
-
 COMMIT;
diff --git a/Open-ILS/src/support-scripts/age_money.srfsh b/Open-ILS/src/support-scripts/age_money.srfsh
new file mode 100755
index 0000000000..533403c61a
--- /dev/null
+++ b/Open-ILS/src/support-scripts/age_money.srfsh
@@ -0,0 +1,7 @@
+#!BINDIR/srfsh
+open open-ils.cstore
+request open-ils.cstore open-ils.cstore.transaction.begin
+request open-ils.cstore open-ils.cstore.json_query {"from":["action.purge_circulations"]}
+request open-ils.cstore open-ils.cstore.transaction.commit
+close open-ils.cstore
+
diff --git a/docs/RELEASE_NOTES_NEXT/Administration/aged-money.adoc b/docs/RELEASE_NOTES_NEXT/Administration/aged-money.adoc
new file mode 100644
index 0000000000..f6d736f99d
--- /dev/null
+++ b/docs/RELEASE_NOTES_NEXT/Administration/aged-money.adoc
@@ -0,0 +1,21 @@
+Aged Money Changes
+^^^^^^^^^^^^^^^^^^
+
+Two new global flag settings have been added to control if/when billings and
+payments are aged.  Both settings are disabled by default.
+
+* 'history.money.age_with_circs' 
+ ** Age billings and payments linked to circulations when the cirulcation 
+    is aged.
+* 'history.money.retention_age'
+  ** Age billings and payments based on the age of the finish date for
+     the linked transaction.
+  ** To age money based on this setting, there is a new srfsh script
+     at (by default) /openils/bin/age_money.srfsh.
+
+Aged Payment Additional Fields
+++++++++++++++++++++++++++++++
+
+The aged payment table now has accepting_usr, cash_drawer, and billing
+columns to improve reporting of aged money.
+
diff --git a/docs/RELEASE_NOTES_NEXT/Reports/aged-payment-fields.adoc b/docs/RELEASE_NOTES_NEXT/Reports/aged-payment-fields.adoc
deleted file mode 100644
index 475d06a2ff..0000000000
--- a/docs/RELEASE_NOTES_NEXT/Reports/aged-payment-fields.adoc
+++ /dev/null
@@ -1,6 +0,0 @@
-Aged Payment Additional Fields
-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-
-The aged payment table now has accepting_usr, cash_drawer, and billing
-columns to improve reporting of aged money.
-

commit 0472139309b3ff83bb5257eb9c5f67ed8cda0779
Author: Bill Erickson <berickxx at gmail.com>
Date:   Fri Feb 7 15:52:19 2020 -0500

    LP1858448 Aged payment fields release notes
    
    Signed-off-by: Bill Erickson <berickxx at gmail.com>
    Signed-off-by: John Amundson <jamundson at cwmars.org>
    Signed-off-by: Jason Stephenson <jason at sigio.com>

diff --git a/docs/RELEASE_NOTES_NEXT/Reports/aged-payment-fields.adoc b/docs/RELEASE_NOTES_NEXT/Reports/aged-payment-fields.adoc
new file mode 100644
index 0000000000..475d06a2ff
--- /dev/null
+++ b/docs/RELEASE_NOTES_NEXT/Reports/aged-payment-fields.adoc
@@ -0,0 +1,6 @@
+Aged Payment Additional Fields
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+The aged payment table now has accepting_usr, cash_drawer, and billing
+columns to improve reporting of aged money.
+

commit 676410920c651bb5898d099d8d43d48be0ed1964
Author: Bill Erickson <berickxx at gmail.com>
Date:   Fri Feb 7 15:19:15 2020 -0500

    LP1858448 Additional aged_payment fields
    
    Adds accepting_usr, cash_drawer, and billing columns to the
    money.aged_payment table.
    
    Modifies the existing "xact" column for aged payments and billings in
    the IDL to link to "mbt" instead of "acirc", since not all
    payments/billings are linked to aged circs.
    
    Signed-off-by: Bill Erickson <berickxx at gmail.com>
    Signed-off-by: John Amundson <jamundson at cwmars.org>
    Signed-off-by: Jason Stephenson <jason at sigio.com>

diff --git a/Open-ILS/examples/fm_IDL.xml b/Open-ILS/examples/fm_IDL.xml
index 7622efd0db..3cc362ab73 100644
--- a/Open-ILS/examples/fm_IDL.xml
+++ b/Open-ILS/examples/fm_IDL.xml
@@ -8114,11 +8114,17 @@ SELECT  usr,
 			<field reporter:label="Note" name="note" reporter:datatype="text"/>
 			<field reporter:label="Payment Date/Time" name="payment_ts" reporter:datatype="timestamp"/>
 			<field reporter:label="Payment Type" name="payment_type" reporter:datatype="text"/>
-			<field reporter:label="Aged Circulation" name="xact" reporter:datatype="link"/>
+			<field reporter:label="Transaction" name="xact" reporter:datatype="link"/>
 			<field reporter:label="Voided?" name="voided" reporter:datatype="bool"/>
+			<field reporter:label="Accepting User" name="accepting_usr" reporter:datatype="link"/>
+			<field reporter:label="Cash Drawer" name="cash_drawer" reporter:datatype="link"/>
+			<field name="billing" reporter:datatype="link"/>
 		</fields>
 		<links>
-			<link field="xact" reltype="has_a" key="id" map="" class="acirc"/>
+			<link field="xact" reltype="has_a" key="id" map="" class="mbt"/>
+			<link field="accepting_usr" reltype="has_a" key="id" map="" class="au"/>
+			<link field="cash_drawer" reltype="has_a" key="id" map="" class="aws"/>
+			<link field="billing" reltype="has_a" key="id" class="mab"/>
 		</links>
 		<permacrud xmlns="http://open-ils.org/spec/opensrf/IDL/permacrud/v1">
 			<actions>
@@ -8140,11 +8146,15 @@ SELECT  usr,
 			<field reporter:label="Payment Type" name="payment_type" reporter:datatype="text"/>
 			<field reporter:label="Aged Circulation" name="xact" reporter:datatype="link"/>
 			<field reporter:label="Voided?" name="voided" reporter:datatype="bool"/>
+			<field reporter:label="Accepting User" name="accepting_usr" reporter:datatype="link"/>
+			<field reporter:label="Cash Drawer" name="cash_drawer" reporter:datatype="link"/>
+			<field name="billing" reporter:datatype="link"/>
 		</fields>
 		<links>
-			<!-- 
-				'xact' may link to a money.billabl_xact or to a money.aged_circulation.
-			 -->
+			<link field="xact" reltype="has_a" key="id" map="" class="mbt"/>
+			<link field="accepting_usr" reltype="has_a" key="id" map="" class="au"/>
+			<link field="cash_drawer" reltype="has_a" key="id" map="" class="aws"/>
+			<link field="billing" reltype="has_a" key="id" class="mab"/>
 		</links>
 	</class>
 
@@ -8444,7 +8454,7 @@ SELECT  usr,
 			<field reporter:label="Type" name="btype" reporter:datatype="link"/>
 		</fields>
 		<links>
-			<link field="xact" reltype="has_a" key="id" map="" class="acirc"/>
+			<link field="xact" reltype="has_a" key="id" map="" class="mbt"/>
 			<link field="voider" reltype="has_a" key="id" map="" class="au"/>
 			<link field="btype" reltype="has_a" key="id" map="" class="cbt"/>
 		</links>
diff --git a/Open-ILS/src/sql/Pg/080.schema.money.sql b/Open-ILS/src/sql/Pg/080.schema.money.sql
index 88a724bdf0..631c7a5e0e 100644
--- a/Open-ILS/src/sql/Pg/080.schema.money.sql
+++ b/Open-ILS/src/sql/Pg/080.schema.money.sql
@@ -700,15 +700,33 @@ CREATE OR REPLACE VIEW money.cashdrawer_payment_view AS
 		LEFT JOIN money.bnm_desk_payment p ON (ws.id = p.cash_drawer)
 		LEFT JOIN money.payment_view t ON (p.id = t.id);
 
+-- serves as the basis for the aged payments data.
+CREATE OR REPLACE VIEW money.payment_view_extended AS
+    SELECT p.*,
+        bnm.accepting_usr,
+        bnmd.cash_drawer,
+        maa.billing
+    FROM money.payment_view p
+    LEFT JOIN money.bnm_payment bnm ON bnm.id = p.id
+    LEFT JOIN money.bnm_desk_payment bnmd ON bnmd.id = p.id
+    LEFT JOIN money.account_adjustment maa ON maa.id = p.id;
 
 -- Create 'aged' clones of billing and payment_view tables
 CREATE TABLE money.aged_payment (LIKE money.payment INCLUDING INDEXES);
-ALTER TABLE money.aged_payment ADD COLUMN payment_type TEXT NOT NULL;
+ALTER TABLE money.aged_payment 
+    ADD COLUMN payment_type TEXT NOT NULL,
+    ADD COLUMN accepting_usr INTEGER,
+    ADD COLUMN cash_drawer INTEGER,
+    ADD COLUMN billing BIGINT;
+
+CREATE INDEX aged_payment_accepting_usr_idx ON money.aged_payment(accepting_usr);
+CREATE INDEX aged_payment_cash_drawer_idx ON money.aged_payment(cash_drawer);
+CREATE INDEX aged_payment_billing_idx ON money.aged_payment(billing);
 
 CREATE TABLE money.aged_billing (LIKE money.billing INCLUDING INDEXES);
 
 CREATE OR REPLACE VIEW money.all_payments AS
-    SELECT * FROM money.payment_view 
+    SELECT * FROM money.payment_view_extended
     UNION ALL
     SELECT * FROM money.aged_payment;
 
diff --git a/Open-ILS/src/sql/Pg/090.schema.action.sql b/Open-ILS/src/sql/Pg/090.schema.action.sql
index 3fb3b4a782..ae1fa9adfd 100644
--- a/Open-ILS/src/sql/Pg/090.schema.action.sql
+++ b/Open-ILS/src/sql/Pg/090.schema.action.sql
@@ -358,7 +358,7 @@ BEGIN
         SELECT * FROM money.billing WHERE xact = OLD.id;
 
     INSERT INTO money.aged_payment 
-        SELECT * FROM money.payment_view WHERE xact = OLD.id;
+        SELECT * FROM money.payment_view_extended WHERE xact = OLD.id;
 
     DELETE FROM money.payment WHERE xact = OLD.id;
     DELETE FROM money.billing WHERE xact = OLD.id;
diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.aged-money-fields.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.aged-money-fields.sql
new file mode 100644
index 0000000000..456ea11b11
--- /dev/null
+++ b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.aged-money-fields.sql
@@ -0,0 +1,84 @@
+BEGIN;
+
+-- SELECT evergreen.upgrade_deps_block_check('TODO', :eg_version);
+
+DROP VIEW money.all_payments;
+
+CREATE OR REPLACE VIEW money.payment_view_extended AS
+    SELECT p.*,
+        bnm.accepting_usr,
+        bnmd.cash_drawer,
+        maa.billing
+    FROM money.payment_view p
+    LEFT JOIN money.bnm_payment bnm ON bnm.id = p.id
+    LEFT JOIN money.bnm_desk_payment bnmd ON bnmd.id = p.id
+    LEFT JOIN money.account_adjustment maa ON maa.id = p.id;
+
+ALTER TABLE money.aged_payment
+    ADD COLUMN accepting_usr INTEGER,
+    ADD COLUMN cash_drawer INTEGER,
+    ADD COLUMN billing BIGINT;
+
+CREATE INDEX aged_payment_accepting_usr_idx ON money.aged_payment(accepting_usr);
+CREATE INDEX aged_payment_cash_drawer_idx ON money.aged_payment(cash_drawer);
+CREATE INDEX aged_payment_billing_idx ON money.aged_payment(billing);
+
+CREATE OR REPLACE VIEW money.all_payments AS
+    SELECT * FROM money.payment_view_extended
+    UNION ALL
+    SELECT * FROM money.aged_payment;
+
+CREATE OR REPLACE FUNCTION action.age_circ_on_delete () RETURNS TRIGGER AS $$
+DECLARE
+found char := 'N';
+BEGIN
+
+    -- If there are any renewals for this circulation, don't archive or delete
+    -- it yet.   We'll do so later, when we archive and delete the renewals.
+
+    SELECT 'Y' INTO found
+    FROM action.circulation
+    WHERE parent_circ = OLD.id
+    LIMIT 1;
+
+    IF found = 'Y' THEN
+        RETURN NULL;  -- don't delete
+	END IF;
+
+    -- Archive a copy of the old row to action.aged_circulation
+
+    INSERT INTO action.aged_circulation
+        (id,usr_post_code, usr_home_ou, usr_profile, usr_birth_year, copy_call_number, copy_location,
+        copy_owning_lib, copy_circ_lib, copy_bib_record, xact_start, xact_finish, target_copy,
+        circ_lib, circ_staff, checkin_staff, checkin_lib, renewal_remaining, grace_period, due_date,
+        stop_fines_time, checkin_time, create_time, duration, fine_interval, recurring_fine,
+        max_fine, phone_renewal, desk_renewal, opac_renewal, duration_rule, recurring_fine_rule,
+        max_fine_rule, stop_fines, workstation, checkin_workstation, checkin_scan_time, parent_circ,
+        auto_renewal, auto_renewal_remaining)
+      SELECT
+        id,usr_post_code, usr_home_ou, usr_profile, usr_birth_year, copy_call_number, copy_location,
+        copy_owning_lib, copy_circ_lib, copy_bib_record, xact_start, xact_finish, target_copy,
+        circ_lib, circ_staff, checkin_staff, checkin_lib, renewal_remaining, grace_period, due_date,
+        stop_fines_time, checkin_time, create_time, duration, fine_interval, recurring_fine,
+        max_fine, phone_renewal, desk_renewal, opac_renewal, duration_rule, recurring_fine_rule,
+        max_fine_rule, stop_fines, workstation, checkin_workstation, checkin_scan_time, parent_circ,
+        auto_renewal, auto_renewal_remaining
+        FROM action.all_circulation WHERE id = OLD.id;
+
+    -- Migrate billings and payments to aged tables
+
+    INSERT INTO money.aged_billing
+        SELECT * FROM money.billing WHERE xact = OLD.id;
+
+    INSERT INTO money.aged_payment 
+        SELECT * FROM money.payment_view_extended WHERE xact = OLD.id;
+
+    DELETE FROM money.payment WHERE xact = OLD.id;
+    DELETE FROM money.billing WHERE xact = OLD.id;
+
+    RETURN OLD;
+END;
+$$ LANGUAGE 'plpgsql';
+
+
+COMMIT;

-----------------------------------------------------------------------

Summary of changes:
 Open-ILS/examples/fm_IDL.xml                       |  37 +++--
 Open-ILS/src/Makefile.am                           |   2 +
 Open-ILS/src/sql/Pg/080.schema.money.sql           |  77 +++++++++-
 Open-ILS/src/sql/Pg/090.schema.action.sql          |  13 +-
 Open-ILS/src/sql/Pg/950.data.seed-values.sql       |  24 ++++
 .../upgrade/1181.schema.aged-billing-payment.sql   |  14 ++
 .../sql/Pg/upgrade/1192.schema.fix_circ_aging.sql  |   4 +
 .../Pg/upgrade/XXXX.schema.aged-money-fields.sql   | 159 +++++++++++++++++++++
 .../Pg/version-upgrade/3.3.3-3.4.0-upgrade-db.sql  |  14 ++
 .../{purge_holds.srfsh => age_money.srfsh}         |   2 +-
 .../Administration/aged-money.adoc                 |  42 ++++++
 11 files changed, 363 insertions(+), 25 deletions(-)
 create mode 100644 Open-ILS/src/sql/Pg/upgrade/XXXX.schema.aged-money-fields.sql
 copy Open-ILS/src/support-scripts/{purge_holds.srfsh => age_money.srfsh} (64%)
 create mode 100644 docs/RELEASE_NOTES_NEXT/Administration/aged-money.adoc


hooks/post-receive
-- 
Evergreen ILS



More information about the open-ils-commits mailing list