From 895009428cc534dcda4d719f0f82ff08e2f2b717 Mon Sep 17 00:00:00 2001 From: miker Date: Mon, 10 Nov 2008 17:46:27 +0000 Subject: [PATCH] first cut of the "mark bad debt" functionality git-svn-id: svn://svn.open-ils.org/ILS/trunk@11116 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- Open-ILS/src/perlmods/OpenILS/WWW/BadDebt.pm | 173 +++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 Open-ILS/src/perlmods/OpenILS/WWW/BadDebt.pm diff --git a/Open-ILS/src/perlmods/OpenILS/WWW/BadDebt.pm b/Open-ILS/src/perlmods/OpenILS/WWW/BadDebt.pm new file mode 100644 index 0000000000..e5c1dcb7d5 --- /dev/null +++ b/Open-ILS/src/perlmods/OpenILS/WWW/BadDebt.pm @@ -0,0 +1,173 @@ +package OpenILS::WWW::BadDebt; +use strict; +use warnings; +use bytes; + +use Apache2::Log; +use Apache2::Const -compile => qw(OK REDIRECT DECLINED NOT_FOUND :log); +use APR::Const -compile => qw(:error SUCCESS); +use APR::Table; + +use Apache2::RequestRec (); +use Apache2::RequestIO (); +use Apache2::RequestUtil; +use CGI; + +use OpenSRF::EX qw(:try); +use OpenSRF::System; +use OpenSRF::AppSession; +use XML::LibXML; +use XML::LibXSLT; + +use Encode; +use Unicode::Normalize; +use OpenILS::Utils::Fieldmapper; +use OpenSRF::Utils::Logger qw/$logger/; + +use UNIVERSAL::require; + +# set the bootstrap config when this module is loaded +my $bootstrap; + +sub import { + my $self = shift; + $bootstrap = shift; +} + + +sub child_init { + OpenSRF::System->bootstrap_client( config_file => $bootstrap ); +} + +sub handler { + my $r = shift; + my $cgi = new CGI; + my $auth_ses = $cgi->cookie('ses') || $cgi->param('ses'); + + # find some IDs ... + my @xacts; + + my $user = verify_login($auth_ses); + return 403 unless $user; + + my $mark_bad = $cgi->param('action') eq 'unmark' ? 'f' : 't'; + + my $file = $cgi->param('idfile'); + if ($file) { + my $col = $cgi->param('idcolumn') || 0; + my $csv = new Text::CSV; + + while (<$file>) { + $csv->parse($_); + my @data = $csv->fields; + my $id = $data[$col]; + $id =~ s/\D+//o; + next unless ($id); + push @xacts, $id; + } + } + + if (!@xacts) { # try pathinfo + my $path_rec = $cgi->path_info(); + if ($path_rec) { + @xacts = map { $_ ? ($_) : () } split '/', $path_rec; + } + } + + return 404 unless @xacts; + + my ($yr,$mon,$day) = (localtime())[5,4,3]; $yr += 1900; + my $date = sprintf('%d-%02d-%02d',$yr,$mon,$day); + + $r->headers_out->set("Content-Disposition" => "inline; filename=bad_debt_$date.csv"); + $r->content_type('application/octet-stream'); + + $r->print( '"Transaction ID","Message","Amount Owed","Transaction Start Date","User Barcode"'."\n" ); + + my $cstore = OpenSRF::AppSession->create('open-ils.cstore'); + my $actor = OpenSRF::AppSession->create('open-ils.actor'); + + $cstore->connect(); + $cstore->request('open-ils.cstore.transaction.begin')->gather(1); + + for my $xact ( @xacts ) { + try { + + my $x = $cstore->request('open-ils.cstore.direct.money.billable_xact.retrieve' => $xact)->gather(1); + my $s = $cstore->request('open-ils.cstore.direct.money.billable_xact_summary.retrieve' => $xact)->gather(1); + my $u = $cstore->request('open-ils.cstore.direct.actor.usr.retrieve' => $s->usr)->gather(1); + my $c = $cstore->request('open-ils.cstore.direct.actor.card.retrieve' => $u->card)->gather(1); + my $w = $s->xact_type eq 'circulation' ? + $cstore->request('open-ils.cstore.direct.action.circulation.retrieve' => $xact)->gather(1)->circ_lib : + $cstore->request('open-ils.cstore.direct.money.grocery.retrieve' => $xact)->gather(1)->billing_location; + + my $failures = $actor->request('open-ils.actor.user.perm.check', $auth_ses, $user->id, $w, ['MARK_BAD_DEBT'])->gather(1); + + if (@$failures) { + $r->print("$xact,\"Permission Failure\",\"\",\"\",\"\"\n"); + } else { + $x->unrecovered($mark_bad); + my $result = $cstore->request('open-ils.cstore.direct.money.billable_xact.update' => $x)->gather(1); + if ($result != $x->id) { + $r->print("$xact,\"Update Failure\",\"\",\"\",\"\"\n"); + } else { + my $amount = $s->balance_owed; + my $start = $s->xact_start; + my $barcode = $c->barcode; + $r->print("$xact,\"Marked Bad Debt\",\"$amount\",\"$start\",\"$barcode\"\n"); + } + } + } otherwise { + $r->print("$xact,\"Update Failure\",\"\",\"\",\"\"\n"); + }; + } + + $cstore->request('open-ils.cstore.transaction.commit')->gather(1); + $cstore->disconnect(); + + return Apache2::Const::OK; + +} + +sub verify_login { + my $auth_token = shift; + return undef unless $auth_token; + + my $user = OpenSRF::AppSession + ->create("open-ils.auth") + ->request( "open-ils.auth.session.retrieve", $auth_token ) + ->gather(1); + + if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) { + return undef; + } + + return $user if ref($user); + return undef; +} + +sub show_template { + my $r = shift; + + $r->content_type('text/html'); + $r->print(< + + Record Export + + +
+ Use field number (starting from 0) + from CSV file + +
+ + + +HTML + + return Apache2::Const::OK; +} + +1; -- 2.11.0