adding generate-fines and supporting functions
authormiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 13 Jul 2005 21:25:13 +0000 (21:25 +0000)
committermiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 13 Jul 2005 21:25:13 +0000 (21:25 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@1173 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/action.pm
Open-ILS/src/sql/Postgres/080.schema.money.sql
Open-ILS/src/support-scripts/generate-fines.pl [new file with mode: 0755]

index 6b37605..6ffeef7 100644 (file)
@@ -1,10 +1,38 @@
 package OpenILS::Application::Storage::Publisher::action;
 use base qw/OpenILS::Application::Storage/;
-#use OpenILS::Application::Storage::CDBI::action;
-#use OpenSRF::Utils::Logger qw/:level/;
-#use OpenILS::Utils::Fieldmapper;
-#
-#my $log = 'OpenSRF::Utils::Logger';
+use OpenSRF::Utils::Logger qw/:level/;
+my $log = 'OpenSRF::Utils::Logger';
+
+sub grab_overdue {
+       my $self = shift;
+       my $client = shift;
+       my $grace = shift || '';
+
+       my $c_t = action::circulation->table;
+
+       $grace = ' - (1 * (fine_interval))' if ($grace);
+
+       my $sql = <<"   SQL";
+               SELECT  *
+                 FROM  $c_t
+                 WHERE stop_fines IS NULL
+                       AND due_date < ( CURRENT_TIMESTAMP $grace)
+       SQL
+
+       my $sth = action::circulation->db_Main->prepare_cached($sql);
+       $sth->execute;
+
+       $client->respond( $_->to_fieldmapper ) for ( map { action::circulation->construct($_) } $sth->fetchall_hash );
+
+       return undef;
+
+}
+__PACKAGE__->register_method(
+       api_name        => 'open-ils.storage.action.circulation.overdue',
+       api_level       => 1,
+       stream          => 1,
+       method          => 'grab_overdue',
+);
 
 sub next_resp_group_id {
        my $self = shift;
index 3f9d9dc..37a38b4 100644 (file)
@@ -30,26 +30,27 @@ CREATE TABLE money.payment (
 );
 CREATE INDEX m_p_xact_idx ON money.payment (xact);
 
-CREATE VIEW money.usr_billable_summary_total AS
-       SELECT  xact.usr AS usr,
-               SUM(COALESCE(credit.amount,0)) AS payed, SUM(COALESCE(debit.amount,0)) AS owed
-         FROM  money.billable_xact xact
-               JOIN money.billing debit ON (xact.id = debit.xact)
-               JOIN money.payment credit ON (xact.id = credit.xact)
-         WHERE xact.xact_finish IS NULL
-       GROUP BY 1;
-
-CREATE VIEW money.usr_billable_summary_xact AS
+CREATE OR REPLACE VIEW money.usr_billable_summary_xact AS
        SELECT  xact.id AS transaction,
                xact.usr AS usr,
-               SUM(COALESCE(credit.amount,0)) AS payed, SUM(COALESCE(debit.amount,0)) AS owed
+               SUM(COALESCE(credit.amount,0)) AS total_paid,
+               MAX(credit.payment_ts) AS last_payment_ts,
+               SUM(COALESCE(debit.amount,0)) AS total_owed,
+               MAX(debit.billing_ts) AS last_billing_ts,
+               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)
          WHERE xact.xact_finish IS NULL
        GROUP BY 1,2;
 
-
+CREATE OR REPLACE VIEW money.usr_billable_summary_total AS
+       SELECT  usr,
+               SUM(total_paid) AS total_paid,
+               SUM(total_owed) AS total_owed, 
+               SUM(balance_owed) AS balance_owed
+         FROM money.usr_billable_summary_xact
+         GROUP BY 1;
 
 CREATE TABLE money.bnm_payment (
        amount_collected        NUMERIC(6,2)    NOT NULL,
diff --git a/Open-ILS/src/support-scripts/generate-fines.pl b/Open-ILS/src/support-scripts/generate-fines.pl
new file mode 100755 (executable)
index 0000000..e2cae47
--- /dev/null
@@ -0,0 +1,48 @@
+#!/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;
+use Time::Local ('timegm_nocheck');
+
+die "USAGE:\n\t$0 config_file [grace?]\n" unless @ARGV;
+
+
+# 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;
+
+               print   "ARG! overdue circ ".$c->id.
+                       " for item ".$c->target_copy.
+                       " : was due at ".$c->due_date."\n";
+
+               my $fine = $session->request(
+                       'open-ils.storage.direct.money.billing.xact',
+                       $c->id, { order_by => 'billing_ts DESC' }
+               )->gather(1);
+
+               my $last_fine;
+               if ($fine) {
+                       $last_fine = $fine->billing_ts;
+               } else {
+                       # Use Date::Manip here
+               }
+       }
+
+} catch Error with {
+       my $e = shift;
+       die "Error processing overdue circulations:\n\n$e\n";
+};
+
+