]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/BadDebt.pm
Lp 1694058: Fix Issue With Place Holds Reported in Testing
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / BadDebt.pm
1 package OpenILS::WWW::BadDebt;
2 use strict;
3 use warnings;
4 use bytes;
5
6 use Apache2::Log;
7 use Apache2::Const -compile => qw(OK REDIRECT DECLINED NOT_FOUND :log);
8 use APR::Const    -compile => qw(:error SUCCESS);
9 use APR::Table;
10
11 use Apache2::RequestRec ();
12 use Apache2::RequestIO ();
13 use Apache2::RequestUtil;
14 use CGI;
15
16 use OpenSRF::EX qw(:try);
17 use OpenSRF::System;
18 use OpenSRF::AppSession;
19 use XML::LibXML;
20 use XML::LibXSLT;
21
22 use Encode;
23 use Unicode::Normalize;
24 use OpenILS::Utils::Fieldmapper;
25 use OpenSRF::Utils::Logger qw/$logger/;
26
27 use UNIVERSAL::require;
28
29 # set the bootstrap config when this module is loaded
30 my $bootstrap;
31
32 sub import {
33         my $self = shift;
34         $bootstrap = shift;
35 }
36
37
38 sub child_init {
39         OpenSRF::System->bootstrap_client( config_file => $bootstrap );
40         return Apache2::Const::OK;
41 }
42
43 sub handler {
44     my $r = shift;
45     my $cgi = new CGI;
46     my $auth_ses = $cgi->cookie('ses') || $cgi->param('ses');
47
48     # find some IDs ...
49     my @xacts;
50
51     my $user = verify_login($auth_ses);
52     return 403 unless $user;
53
54     my $mark_bad = $cgi->param('action') eq 'unmark' ? 'f' : 't';
55     my $format = $cgi->param('format') || 'csv';
56
57     my $file = $cgi->param('idfile');
58     if ($file) {
59         my $col = $cgi->param('idcolumn') || 0;
60         my $csv = new Text::CSV;
61
62         while (<$file>) {
63             $csv->parse($_);
64             my @data = $csv->fields;
65             my $id = $data[$col];
66             $id =~ s/\D+//o;
67             next unless ($id);
68             push @xacts, $id;
69         }
70     }
71
72     if (!@xacts) { # try pathinfo
73         my $path_rec = $cgi->path_info();
74         if ($path_rec) {
75             @xacts = map { $_ ? ($_) : () } split '/', $path_rec;
76         }
77     }
78
79     return 404 unless @xacts;
80
81     my @lines;
82
83     my ($yr,$mon,$day) = (localtime())[5,4,3]; $yr += 1900;
84     my $date = sprintf('%d-%02d-%02d',$yr,$mon,$day);
85
86     my @header = ( '"Transaction ID"', '"Message"', '"Amount Owed"', '"Transaction Start Date"', '"User Barcode"' );
87
88     my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
89     my $actor = OpenSRF::AppSession->create('open-ils.actor');
90
91     $cstore->connect();
92     $cstore->request('open-ils.cstore.transaction.begin')->gather(1);
93     $cstore->request('open-ils.cstore.set_audit_info', $auth_ses, $user->id, $user->wsid)->gather(1);
94
95     for my $xact ( @xacts ) {
96         try {
97     
98             my $x = $cstore->request('open-ils.cstore.direct.money.billable_xact.retrieve' => $xact)->gather(1);
99             my $s = $cstore->request('open-ils.cstore.direct.money.billable_xact_summary.retrieve' => $xact)->gather(1);
100             my $u = $cstore->request('open-ils.cstore.direct.actor.usr.retrieve' => $s->usr)->gather(1);
101             my $c = $cstore->request('open-ils.cstore.direct.actor.card.retrieve' => $u->card)->gather(1);
102             my $w;
103
104             if ($s->xact_type eq 'circulation') {
105                 $w = $cstore->request('open-ils.cstore.direct.action.circulation.retrieve' => $xact)->gather(1)->circ_lib;
106             } elsif ($s->xact_type eq 'grocery') {
107                 $w = $cstore->request('open-ils.cstore.direct.money.grocery.retrieve' => $xact)->gather(1)->billing_location;
108             } elsif ($s->xact_type eq 'reservation') {
109                 $w = $cstore->request('open-ils.cstore.direct.booking.reservation.retrieve' => $xact)->gather(1)->pickup_lib;
110             } else {
111                 die;
112             }
113     
114             my $failures = $actor->request('open-ils.actor.user.perm.check', $auth_ses, $user->id, $w, ['MARK_BAD_DEBT'])->gather(1);
115     
116             if (@$failures) {
117                 push @lines, [ $xact, '"Permission Failure"', '""', '""', '""' ];
118             } else {
119                 $x->unrecovered($mark_bad);
120                 my $result = $cstore->request('open-ils.cstore.direct.money.billable_xact.update' => $x)->gather(1);
121                 if ($result != $x->id) {
122                     push @lines, [ $xact, '"Update Failure"', '""', '""', '""' ];
123                 } else {
124                     my $amount = $s->balance_owed;
125                     my $start = $s->xact_start;
126                     my $barcode = $c->barcode;
127
128                     if ( $mark_bad eq 't' ) {
129                         push @lines, [ $xact, '"Marked Bad Debt"', $amount, "\"$start\"", "\"$barcode\"" ];
130                     } else {
131                         push @lines, [ $xact, '"Unmarked Bad Debt"', $amount, "\"$start\"", "\"$barcode\"" ];
132                     }
133                 }
134             }
135         } otherwise {
136             push @lines, [ $xact, '"Update Failure"', '""', '""', '""' ];
137         };
138     }
139
140     $cstore->request('open-ils.cstore.transaction.commit')->gather(1);
141     $cstore->disconnect();
142
143     if ($format eq 'csv') {
144         $r->headers_out->set("Content-Disposition" => "inline; filename=bad_debt_$date.csv");
145         $r->content_type('application/octet-stream');
146
147         $r->print( join(',', @header) . "\n" );
148         $r->print( join(',', @$_    ) . "\n" ) for (@lines);
149
150     } elsif ($format eq 'json') {
151
152         $r->content_type('application/json');
153
154         $r->print( '[' );
155
156         my $first = 1;
157         for my $line ( @lines ) {
158             $r->print( ',' ) if $first;
159             $first = 0;
160
161             $r->print( '{' );
162             for my $field ( 0 .. 4 ) {
163                 $r->print( "$header[$field] : $$line[$field]" );
164                 $r->print( ',' ) if ($field < 4);
165             }
166             $r->print( '}' );
167         }
168
169         $r->print( ']' );
170     }
171
172     return Apache2::Const::OK;
173
174 }
175
176 sub verify_login {
177         my $auth_token = shift;
178         return undef unless $auth_token;
179
180         my $user = OpenSRF::AppSession
181                 ->create("open-ils.auth")
182                 ->request( "open-ils.auth.session.retrieve", $auth_token )
183                 ->gather(1);
184
185         if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
186                 return undef;
187         }
188
189         return $user if ref($user);
190         return undef;
191 }
192
193 sub show_template {
194     my $r = shift;
195
196     $r->content_type('text/html');
197     $r->print(<<HTML);
198
199 <html>
200     <head>
201         <title>Record Export</title>
202     </head>
203     <body>
204         <form method="POST" enctype="multipart/form-data">
205             Use field number <input type="text" size="2" maxlength="2" name="idcolumn" value="0"/> (starting from 0)
206             from CSV file <input type="file" name="idfile"/>
207             <input type="submit" value="Mark Transactions Unrecoverable"/>
208         </form>
209     </body>
210 </html>
211
212 HTML
213
214     return Apache2::Const::OK;
215 }
216
217 1;