1 package OpenILS::Application::Storage::Publisher::money;
2 use base qw/OpenILS::Application::Storage/;
3 use OpenSRF::Utils qw/:datetime/;
4 use OpenSRF::Utils::Logger qw/:level/;
5 use OpenSRF::EX qw/:try/;
6 use OpenILS::Utils::Fieldmapper;
8 use DateTime::Format::ISO8601;
10 my $log = 'OpenSRF::Utils::Logger';
15 my $xact = shift || '';
19 FROM money.usr_billable_summary_xact
23 return money::billing->db_Main->selectrow_hashref($sql, {}, "$xact");
25 #__PACKAGE__->register_method(
26 # api_name => 'open-ils.storage.money.billing.billable_transaction_summary',
28 # method => 'xact_summary',
31 my $parser = DateTime::Format::ISO8601->new;
43 'open-ils.storage.direct.action.circulation.search_where'
44 )->run( { id => $circ, stop_fines => undef } );
46 push @circs, $self->method_lookup('open-ils.storage.action.circulation.overdue')->run( $grace );
52 my $due_dt = $parser->parse_datetime( clense_ISO8601( $c->due_date ) );
54 my $due = $due_dt->epoch;
56 my $fine_interval = interval_to_seconds( $c->fine_interval );
58 if ( interval_to_seconds( $c->fine_interval ) >= interval_to_seconds('1d') ) {
60 if ($due_dt->strftime('%z') =~ /(-|\+)(\d{2}):?(\d{2})/) {
61 $tz_offset_s = $1 . interval_to_seconds( "${2}h ${3}m");
64 $due -= ($due % $fine_interval) + $tz_offset_s;
65 $now -= ($now % $fine_interval) + $tz_offset_s;
69 "ARG! Overdue circulation ".$c->id.
70 " for item ".$c->target_copy.
71 " (user ".$c->usr.").\n".
72 "\tItem was due on or before: ".localtime($due)."\n");
74 my ($fine) = $self->method_lookup('open-ils.storage.direct.money.billing.search')->run(
75 { xact => $c->id, voided => 'f' },
76 { order_by => 'billing_ts DESC', limit => '1' }
81 $last_fine = $parser->parse_datetime( clense_ISO8601( $fine->billing_ts ) )->epoch;
84 $last_fine += $fine_interval * $grace;
87 my $pending_fine_count = int( ($now - $last_fine) / $fine_interval );
88 unless($pending_fine_count) {
89 $client->respond( "\tNo fines to create. " );
90 if ($grace && $now < $due + $fine_interval * $grace) {
91 $client->respond( "Still inside grace period of: ". seconds_to_interval( $fine_interval * $grace)."\n" );
93 $client->respond( "Last fine generated for: ".localtime($last_fine)."\n" );
98 $client->respond( "\t$pending_fine_count pending fine(s)\n" );
100 for my $bill (1 .. $pending_fine_count) {
102 my ($total) = $self->method_lookup('open-ils.storage.direct.money.billable_transaction_summary.retrieve')->run( $c->id );
104 if ($total && $total->balance_owed > $c->max_fine) {
105 $c->stop_fines('MAXFINES');
106 $self->method_lookup('open-ils.storage.direct.action.circulation.update')->run( $c );
108 "\tMaximum fine level of ".$c->max_fine.
109 " reached for this circulation.\n".
110 "\tNo more fines will be generated.\n" );
114 my $billing = new Fieldmapper::money::billing;
115 $billing->xact( $c->id );
116 $billing->note( "Overdue Fine" );
117 $billing->amount( $c->recuring_fine );
119 $billing->billing_ts(
120 DateTime->from_epoch( epoch => $last_fine + $fine_interval * $bill )->strftime('%FT%T%z')
124 "\t\tCreating fine of ".$billing->amount." for period starting ".
126 $parser->parse_datetime(
127 clense_ISO8601( $billing->billing_ts )
131 $self->method_lookup('open-ils.storage.direct.money.billing.create')->run( $billing );
135 $client->respond( "Error processing overdue circulation [".$c->id."]:\n\n$e\n" );
139 __PACKAGE__->register_method(
140 api_name => 'open-ils.storage.action.circulation.overdue.generate_fines',
143 method => 'generate_fines',