basic fine generator is now operational!
authormiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Thu, 14 Jul 2005 21:29:42 +0000 (21:29 +0000)
committermiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Thu, 14 Jul 2005 21:29:42 +0000 (21:29 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@1190 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher.pm
Open-ILS/src/sql/Postgres/080.schema.money.sql
Open-ILS/src/support-scripts/generate-fines.pl

index c88351e..8ce47d4 100644 (file)
@@ -309,6 +309,7 @@ use OpenILS::Application::Storage::Publisher::asset;
 use OpenILS::Application::Storage::Publisher::biblio;
 use OpenILS::Application::Storage::Publisher::config;
 use OpenILS::Application::Storage::Publisher::metabib;
+use OpenILS::Application::Storage::Publisher::money;
 use OpenILS::Application::Storage::Publisher::permission;
 ';
 
index 37a38b4..ccca385 100644 (file)
@@ -40,7 +40,7 @@ CREATE OR REPLACE VIEW money.usr_billable_summary_xact AS
                SUM(COALESCE(debit.amount,0) - COALESCE(credit.amount,0)) AS balance_owed
          FROM  money.billable_xact xact
                JOIN money.billing debit ON (xact.id = debit.xact)
-               JOIN money.payment credit ON (xact.id = credit.xact)
+               LEFT JOIN money.payment credit ON (xact.id = credit.xact)
          WHERE xact.xact_finish IS NULL
        GROUP BY 1,2;
 
index e2cae47..78c6320 100755 (executable)
@@ -6,10 +6,13 @@ use OpenSRF::Application;
 use OpenSRF::Utils::SettingsClient;
 use OpenILS::Utils::Fieldmapper;
 use OpenSRF::Utils;
-use Time::Local ('timegm_nocheck');
+use DateTime;
+use DateTime::Format::ISO8601;
+use Data::Dumper;
 
 die "USAGE:\n\t$0 config_file [grace?]\n" unless @ARGV;
 
+my $parser = DateTime::Format::ISO8601->new;
 
 # hard coded for now, option later
 
@@ -28,15 +31,60 @@ try {
                        " : was due at ".$c->due_date."\n";
 
                my $fine = $session->request(
-                       'open-ils.storage.direct.money.billing.xact',
+                       'open-ils.storage.direct.money.billing.search.xact',
                        $c->id, { order_by => 'billing_ts DESC' }
                )->gather(1);
 
+               my $now = time;
+               my $fine_interval = OpenSRF::Utils->interval_to_seconds( $c->fine_interval );
+
                my $last_fine;
                if ($fine) {
-                       $last_fine = $fine->billing_ts;
+                       $last_fine = $parser->parse_datetime( OpenSRF::Utils->clense_ISO8601( $fine->billing_ts ))->epoch;
                } else {
                        # Use Date::Manip here
+                       $last_fine = $parser->parse_datetime( OpenSRF::Utils->clense_ISO8601( $c->due_date ))->epoch;
+                       $last_fine += $fine_interval if ($grace);
+               }
+
+               my $pending_fine_count = int( ($now - $last_fine) / $fine_interval ); 
+               next unless($pending_fine_count);
+
+               print "Circ ".$c->id." has $pending_fine_count pending fine(s).\n";
+
+               for my $bill (1 .. $pending_fine_count) {
+
+                       my $total = $session->request(
+                               'open-ils.storage.money.billing.billable_transaction_summary',
+                               $c->id
+                       )->gather(1);
+
+                       if ($total && $total->{balance_owed} > $c->max_fine) {
+                               $c->stop_fines('MAXFINES');
+                               
+                               $session->request(
+                                       'open-ils.storage.direct.action.circulation.update',
+                                       $c
+                               )->gather(1);
+
+                               last;
+                       }
+
+                       my $billing = new Fieldmapper::money::billing;
+                       $billing->xact( $c->id );
+                       $billing->note( "Overdue Fine" );
+                       $billing->amount( $c->recuring_fine );
+                       $billing->billing_ts(
+                               DateTime->from_epoch(
+                                       epoch => $last_fine + $fine_interval * $bill
+                               )->strftime('%FT%T%z')
+                       );
+
+                       $session->request(
+                               'open-ils.storage.direct.money.billing.create',
+                               $billing
+                       )->gather(1);
+
                }
        }