From b155b2af10084db3aeba0791a1bcbfe27e230f0e Mon Sep 17 00:00:00 2001 From: miker Date: Tue, 19 Jul 2005 18:37:51 +0000 Subject: [PATCH] moving fine generation into the storage server so that the biz logic can force it git-svn-id: svn://svn.open-ils.org/ILS/trunk@1285 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- .../Application/Storage/Publisher/action.pm | 2 +- .../Application/Storage/Publisher/money.pm | 126 +++++++++++++++++- .../src/support-scripts/generate-fines.pl | 116 ---------------- 3 files changed, 125 insertions(+), 119 deletions(-) delete mode 100755 Open-ILS/src/support-scripts/generate-fines.pl diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/action.pm b/Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/action.pm index 6ffeef7d4a..e13e08184d 100644 --- a/Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/action.pm +++ b/Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/action.pm @@ -10,7 +10,7 @@ sub grab_overdue { my $c_t = action::circulation->table; - $grace = ' - (1 * (fine_interval))' if ($grace); + $grace = " - ($grace * (fine_interval))" if ($grace); my $sql = <<" SQL"; SELECT * diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/money.pm b/Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/money.pm index 9c3962d0aa..e0ea9c56ab 100644 --- a/Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/money.pm +++ b/Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/money.pm @@ -1,6 +1,12 @@ package OpenILS::Application::Storage::Publisher::money; use base qw/OpenILS::Application::Storage/; +use OpenSRF::Utils qw/:datetime/; use OpenSRF::Utils::Logger qw/:level/; +use OpenSRF::EX qw/:try/; +use OpenILS::Utils::Fieldmapper; +use DateTime; +use DateTime::Format::ISO8601; + my $log = 'OpenSRF::Utils::Logger'; sub xact_summary { @@ -16,10 +22,126 @@ sub xact_summary { return money::billing->db_Main->selectrow_hashref($sql, {}, "$xact"); } +#__PACKAGE__->register_method( +# api_name => 'open-ils.storage.money.billing.billable_transaction_summary', +# api_level => 1, +# method => 'xact_summary', +#); + +my $parser = DateTime::Format::ISO8601->new; +sub generate_fines { + my $self = shift; + my $client = shift; + my $grace = shift; + my $circ = shift; + + + my @circs; + if ($circ) { + push @circs, + $self->method_lookup( + 'open-ils.storage.direct.action.circulation.search_where' + )->run( { id => $circ, stop_fines => undef } ); + } else { + push @circs, $self->method_lookup('open-ils.storage.action.circulation.overdue')->run( $grace ); + } + + for my $c (@circs) { + + try { + 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; + } + + $client->respond( + "ARG! Overdue circulation ".$c->id. + " for item ".$c->target_copy. + " (user ".$c->usr.").\n". + "\tItem was due on or before: ".localtime($due)."\n"); + + my ($fine) = $self->method_lookup('open-ils.storage.direct.money.billing.search')->run( + { xact => $c->id, voided => 'f' }, + { order_by => 'billing_ts DESC', limit => '1' } + ); + + my $last_fine; + if ($fine) { + $last_fine = $parser->parse_datetime( clense_ISO8601( $fine->billing_ts ) )->epoch; + } else { + $last_fine = $due; + $last_fine += $fine_interval * $grace; + } + + my $pending_fine_count = int( ($now - $last_fine) / $fine_interval ); + unless($pending_fine_count) { + $client->respond( "\tNo fines to create. " ); + if ($grace && $now < $due + $fine_interval * $grace) { + $client->respond( "Still inside grace period of: ". seconds_to_interval( $fine_interval * $grace)."\n" ); + } else { + $client->respond( "Last fine generated for: ".localtime($last_fine)."\n" ); + } + next; + } + + $client->respond( "\t$pending_fine_count pending fine(s)\n" ); + + for my $bill (1 .. $pending_fine_count) { + + my ($total) = $self->method_lookup('open-ils.storage.direct.money.billable_transaction_summary.retrieve')->run( $c->id ); + + if ($total && $total->balance_owed > $c->max_fine) { + $c->stop_fines('MAXFINES'); + $self->method_lookup('open-ils.storage.direct.action.circulation.update')->run( $c ); + $client->respond( + "\tMaximum fine level of ".$c->max_fine. + " reached for this circulation.\n". + "\tNo more fines will be generated.\n" ); + 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') + ); + + $client->respond( + "\t\tCreating fine of ".$billing->amount." for period starting ". + localtime( + $parser->parse_datetime( + clense_ISO8601( $billing->billing_ts ) + )->epoch + )."\n" ); + + $self->method_lookup('open-ils.storage.direct.money.billing.create')->run( $billing ); + } + } catch Error with { + my $e = shift; + $client->respond( "Error processing overdue circulation [".$c->id."]:\n\n$e\n" ); + }; + } +} __PACKAGE__->register_method( - api_name => 'open-ils.storage.money.billing.billable_transaction_summary', + api_name => 'open-ils.storage.action.circulation.overdue.generate_fines', api_level => 1, - method => 'xact_summary', + stream => 1, + method => 'generate_fines', ); + 1; diff --git a/Open-ILS/src/support-scripts/generate-fines.pl b/Open-ILS/src/support-scripts/generate-fines.pl deleted file mode 100755 index 1c8952912a..0000000000 --- a/Open-ILS/src/support-scripts/generate-fines.pl +++ /dev/null @@ -1,116 +0,0 @@ -#!/usr/bin/perl -w -use strict; -use OpenSRF::EX qw/:try/; -use OpenSRF::System; -use OpenSRF::Application; -use OpenSRF::Utils::SettingsClient; -use OpenILS::Utils::Fieldmapper; -use OpenSRF::Utils qw/:datetime/; -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 - -OpenSRF::System->bootstrap_client( config_file => $ARGV[0] ); -my $session = OpenSRF::AppSession->create('open-ils.storage'); - -my $grace = $ARGV[1]; - -try { - my $req = $session->request( 'open-ils.storage.action.circulation.overdue',$grace ); - while (!$req->failed && (my $res = $req->recv)) { - my $c = $res->content; - - 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.").\n". - "\tItem was due on or before: ".localtime($due)."\n"; - - my $fine = $session->request( - 'open-ils.storage.direct.money.billing.search', - { xact => $c->id, voided => 'f' }, - { order_by => 'billing_ts DESC', limit => '1' } - )->gather(1); - - my $last_fine; - if ($fine) { - $last_fine = $parser->parse_datetime( clense_ISO8601( $fine->billing_ts ) )->epoch; - } else { - $last_fine = $due; - $last_fine += $fine_interval * $grace; - } - - my $pending_fine_count = int( ($now - $last_fine) / $fine_interval ); - 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"; - - for my $bill (1 .. $pending_fine_count) { - - my $total = $session->request( - 'open-ils.storage.direct.money.billable_transaction_summary.retrieve', - $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); - print "\tMaximum fine level of ".$c->max_fine." reached for this circulation.\n\tNo more fines will be generated.\n"; - 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') - ); - - 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"; -}; - - -- 2.43.2