1 package OpenILS::WWW::BadDebt;
7 use Apache2::Const -compile => qw(OK REDIRECT DECLINED NOT_FOUND :log);
8 use APR::Const -compile => qw(:error SUCCESS);
11 use Apache2::RequestRec ();
12 use Apache2::RequestIO ();
13 use Apache2::RequestUtil;
16 use OpenSRF::EX qw(:try);
18 use OpenSRF::AppSession;
23 use Unicode::Normalize;
24 use OpenILS::Utils::Fieldmapper;
25 use OpenSRF::Utils::Logger qw/$logger/;
27 use UNIVERSAL::require;
29 # set the bootstrap config when this module is loaded
39 OpenSRF::System->bootstrap_client( config_file => $bootstrap );
40 return Apache2::Const::OK;
46 my $auth_ses = $cgi->cookie('ses') || $cgi->param('ses') || $cgi->cookie('eg.auth.token');
47 if ($auth_ses =~ /^"(.+)"$/) {
54 my $user = verify_login($auth_ses);
55 return 403 unless $user;
57 my $mark_bad = $cgi->param('action') eq 'unmark' ? 'f' : 't';
58 my $format = $cgi->param('format') || 'csv';
60 my $file = $cgi->param('idfile');
62 my $col = $cgi->param('idcolumn') || 0;
63 my $csv = new Text::CSV;
67 my @data = $csv->fields;
75 if (!@xacts) { # try pathinfo
76 my $path_rec = $cgi->path_info();
78 @xacts = map { $_ ? ($_) : () } split '/', $path_rec;
82 return 404 unless @xacts;
86 my ($yr,$mon,$day) = (localtime())[5,4,3]; $yr += 1900;
87 my $date = sprintf('%d-%02d-%02d',$yr,$mon,$day);
89 my @header = ( '"Transaction ID"', '"Message"', '"Amount Owed"', '"Transaction Start Date"', '"User Barcode"' );
91 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
92 my $actor = OpenSRF::AppSession->create('open-ils.actor');
95 $cstore->request('open-ils.cstore.transaction.begin')->gather(1);
96 $cstore->request('open-ils.cstore.set_audit_info', $auth_ses, $user->id, $user->wsid)->gather(1);
98 for my $xact ( @xacts ) {
101 my $x = $cstore->request('open-ils.cstore.direct.money.billable_xact.retrieve' => $xact)->gather(1);
102 my $s = $cstore->request('open-ils.cstore.direct.money.billable_xact_summary.retrieve' => $xact)->gather(1);
103 my $u = $cstore->request('open-ils.cstore.direct.actor.usr.retrieve' => $s->usr)->gather(1);
104 my $c = $cstore->request('open-ils.cstore.direct.actor.card.retrieve' => $u->card)->gather(1);
107 if ($s->xact_type eq 'circulation') {
108 $w = $cstore->request('open-ils.cstore.direct.action.circulation.retrieve' => $xact)->gather(1)->circ_lib;
109 } elsif ($s->xact_type eq 'grocery') {
110 $w = $cstore->request('open-ils.cstore.direct.money.grocery.retrieve' => $xact)->gather(1)->billing_location;
111 } elsif ($s->xact_type eq 'reservation') {
112 $w = $cstore->request('open-ils.cstore.direct.booking.reservation.retrieve' => $xact)->gather(1)->pickup_lib;
117 my $failures = $actor->request('open-ils.actor.user.perm.check', $auth_ses, $user->id, $w, ['MARK_BAD_DEBT'])->gather(1);
120 push @lines, [ $xact, '"Permission Failure"', '""', '""', '""' ];
122 $x->unrecovered($mark_bad);
123 my $result = $cstore->request('open-ils.cstore.direct.money.billable_xact.update' => $x)->gather(1);
124 if ($result != $x->id) {
125 push @lines, [ $xact, '"Update Failure"', '""', '""', '""' ];
127 my $amount = $s->balance_owed;
128 my $start = $s->xact_start;
129 my $barcode = $c->barcode;
131 if ( $mark_bad eq 't' ) {
132 push @lines, [ $xact, '"Marked Bad Debt"', $amount, "\"$start\"", "\"$barcode\"" ];
134 push @lines, [ $xact, '"Unmarked Bad Debt"', $amount, "\"$start\"", "\"$barcode\"" ];
139 push @lines, [ $xact, '"Update Failure"', '""', '""', '""' ];
143 $cstore->request('open-ils.cstore.transaction.commit')->gather(1);
144 $cstore->disconnect();
146 if ($format eq 'csv') {
147 $r->headers_out->set("Content-Disposition" => "inline; filename=bad_debt_$date.csv");
148 $r->content_type('application/octet-stream');
150 $r->print( join(',', @header) . "\n" );
151 $r->print( join(',', @$_ ) . "\n" ) for (@lines);
153 } elsif ($format eq 'json') {
155 $r->content_type('application/json');
160 for my $line ( @lines ) {
161 $r->print( ',' ) if $first;
165 for my $field ( 0 .. 4 ) {
166 $r->print( "$header[$field] : $$line[$field]" );
167 $r->print( ',' ) if ($field < 4);
175 return Apache2::Const::OK;
180 my $auth_token = shift;
181 return undef unless $auth_token;
183 my $user = OpenSRF::AppSession
184 ->create("open-ils.auth")
185 ->request( "open-ils.auth.session.retrieve", $auth_token )
188 if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
192 return $user if ref($user);
199 $r->content_type('text/html');
204 <title>Record Export</title>
207 <form method="POST" enctype="multipart/form-data">
208 Use field number <input type="text" size="2" maxlength="2" name="idcolumn" value="0"/> (starting from 0)
209 from CSV file <input type="file" name="idfile"/>
210 <input type="submit" value="Mark Transactions Unrecoverable"/>
217 return Apache2::Const::OK;