"day" sized fine intervals are handled correctly now
authormiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 15 Jul 2005 15:03:11 +0000 (15:03 +0000)
committermiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 15 Jul 2005 15:03:11 +0000 (15:03 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@1202 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/perlmods/OpenILS/Application/Storage/Driver/Pg/dbi.pm
Open-ILS/src/support-scripts/generate-fines.pl

index c8af7de..37653a2 100644 (file)
@@ -61,7 +61,7 @@
        #---------------------------------------------------------------------
        package money::user_summary;
        
-       money::billable_transaction_summary->table( 'money.usr_summary' );
+       money::user_summary->table( 'money.usr_summary' );
 
        #---------------------------------------------------------------------
        package action::circulation;
index 06794d3..ab183a9 100755 (executable)
@@ -5,7 +5,7 @@ use OpenSRF::System;
 use OpenSRF::Application;
 use OpenSRF::Utils::SettingsClient;
 use OpenILS::Utils::Fieldmapper;
-use OpenSRF::Utils;
+use OpenSRF::Utils qw/:datetime/;
 use DateTime;
 use DateTime::Format::ISO8601;
 use Data::Dumper;
@@ -26,30 +26,51 @@ try {
        while (!$req->failed && (my $res = $req->recv)) {
                my $c = $res->content;
 
-               print   "ARG! Overdue circulation ".$c->id.
+               my $due_dt = $parser->parse_datetime( clense_ISO8601( $c->due_date ) );
+
+               my $due = $due_dt->epoch;
+               my $now = time;
+               my $fine_interval = interval_to_seconds( $c->fine_interval );
+
+               if ( interval_to_seconds( $c->fine_interval ) >= interval_to_seconds('1d') ) {  
+                       my $tz_offset_s = 0;;
+                       if ($due_dt->strftime('%z') =~ /(-|\+)(\d{2}):?(\d{2})/) {
+                               $tz_offset_s = $1 . interval_to_seconds( "${2}h ${3}m"); 
+                       }
+
+                       $due -= ($due % $fine_interval) + $tz_offset_s;
+                       $now -= ($now % $fine_interval) + $tz_offset_s;
+               }
+
+               print   "\nARG! Overdue circulation ".$c->id.
                        " for item ".$c->target_copy.
-                       " (user ".$c->usr.")".
-                       " : it was due at ".$c->due_date."\n";
+                       " (user ".$c->usr.").\n".
+                       "\tItem was due on or before: ".localtime($due)."\n";
 
                my $fine = $session->request(
                        'open-ils.storage.direct.money.billing.search.xact',
                        $c->id, { order_by => 'billing_ts DESC', limit => '1' }
                )->gather(1);
 
-               my $now = time;
-               my $fine_interval = OpenSRF::Utils->interval_to_seconds( $c->fine_interval );
-
                my $last_fine;
                if ($fine) {
-                       $last_fine = $parser->parse_datetime( OpenSRF::Utils->clense_ISO8601( $fine->billing_ts ) )->epoch;
+                       $last_fine = $parser->parse_datetime( 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);
+                       $last_fine = $due;
+                       $last_fine += $fine_interval * $grace;
                }
 
                my $pending_fine_count = int( ($now - $last_fine) / $fine_interval ); 
-               next unless($pending_fine_count);
+               unless($pending_fine_count) {
+                       print "\tNo fines to create.  ";
+                       if ($grace && $now < $due + $fine_interval * $grace) {
+                               print "Still inside grace period of: ".
+                                       seconds_to_interval( $fine_interval * $grace)."\n";
+                       } else {
+                               print "Last fine generated for: ".localtime($last_fine)."\n";
+                       }
+                       next;
+               }
 
                print "\t$pending_fine_count pending fine(s)\n";
 
@@ -62,12 +83,8 @@ try {
 
                        if ($total && $total->balance_owed > $c->max_fine) {
                                $c->stop_fines('MAXFINES');
-                               
-                               $session->request(
-                                       'open-ils.storage.direct.action.circulation.update',
-                                       $c
-                               )->gather(1);
-
+                               $session->request( 'open-ils.storage.direct.action.circulation.update', $c )->gather(1);
+                               print "\tMaximum fine level of ".$c->max_fine." reached for this circulation.\n\tNo more fines will be generated.\n";
                                last;
                        }
 
@@ -75,20 +92,21 @@ try {
                        $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')
+                               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);
+                       print   "\t\tCreating fine of ".$billing->amount." for period starting ".
+                               localtime(
+                                       $parser->parse_datetime(
+                                               clense_ISO8601( $billing->billing_ts )
+                                       )->epoch
+                               )."\n";
 
+                       $session->request( 'open-ils.storage.direct.money.billing.create', $billing )->gather(1);
                }
        }
-
 } catch Error with {
        my $e = shift;
        die "Error processing overdue circulations:\n\n$e\n";