3 use OpenSRF::EX qw/:try/;
5 use OpenSRF::Application;
6 use OpenSRF::Utils::SettingsClient;
7 use OpenILS::Utils::Fieldmapper;
8 use OpenSRF::Utils qw/:datetime/;
10 use DateTime::Format::ISO8601;
13 die "USAGE:\n\t$0 config_file [grace?]\n" unless @ARGV;
15 my $parser = DateTime::Format::ISO8601->new;
17 # hard coded for now, option later
19 OpenSRF::System->bootstrap_client( config_file => $ARGV[0] );
20 my $session = OpenSRF::AppSession->create('open-ils.storage');
25 my $req = $session->request( 'open-ils.storage.action.circulation.overdue',$grace );
26 while (!$req->failed && (my $res = $req->recv)) {
27 my $c = $res->content;
29 my $due_dt = $parser->parse_datetime( clense_ISO8601( $c->due_date ) );
31 my $due = $due_dt->epoch;
33 my $fine_interval = interval_to_seconds( $c->fine_interval );
35 if ( interval_to_seconds( $c->fine_interval ) >= interval_to_seconds('1d') ) {
37 if ($due_dt->strftime('%z') =~ /(-|\+)(\d{2}):?(\d{2})/) {
38 $tz_offset_s = $1 . interval_to_seconds( "${2}h ${3}m");
41 $due -= ($due % $fine_interval) + $tz_offset_s;
42 $now -= ($now % $fine_interval) + $tz_offset_s;
45 print "\nARG! Overdue circulation ".$c->id.
46 " for item ".$c->target_copy.
47 " (user ".$c->usr.").\n".
48 "\tItem was due on or before: ".localtime($due)."\n";
50 my $fine = $session->request(
51 'open-ils.storage.direct.money.billing.search',
52 { xact => $c->id, voided => 'f' },
53 { order_by => 'billing_ts DESC', limit => '1' }
58 $last_fine = $parser->parse_datetime( clense_ISO8601( $fine->billing_ts ) )->epoch;
61 $last_fine += $fine_interval * $grace;
64 my $pending_fine_count = int( ($now - $last_fine) / $fine_interval );
65 unless($pending_fine_count) {
66 print "\tNo fines to create. ";
67 if ($grace && $now < $due + $fine_interval * $grace) {
68 print "Still inside grace period of: ".
69 seconds_to_interval( $fine_interval * $grace)."\n";
71 print "Last fine generated for: ".localtime($last_fine)."\n";
76 print "\t$pending_fine_count pending fine(s)\n";
78 for my $bill (1 .. $pending_fine_count) {
80 my $total = $session->request(
81 'open-ils.storage.direct.money.billable_transaction_summary.retrieve',
85 if ($total && $total->balance_owed > $c->max_fine) {
86 $c->stop_fines('MAXFINES');
87 $session->request( 'open-ils.storage.direct.action.circulation.update', $c )->gather(1);
88 print "\tMaximum fine level of ".$c->max_fine." reached for this circulation.\n\tNo more fines will be generated.\n";
92 my $billing = new Fieldmapper::money::billing;
93 $billing->xact( $c->id );
94 $billing->note( "Overdue Fine" );
95 $billing->amount( $c->recuring_fine );
98 DateTime->from_epoch( epoch => $last_fine + $fine_interval * $bill )->strftime('%FT%T%z')
101 print "\t\tCreating fine of ".$billing->amount." for period starting ".
103 $parser->parse_datetime(
104 clense_ISO8601( $billing->billing_ts )
108 $session->request( 'open-ils.storage.direct.money.billing.create', $billing )->gather(1);
113 die "Error processing overdue circulations:\n\n$e\n";