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');
51 my $user = verify_login($auth_ses);
52 return 403 unless $user;
54 my $mark_bad = $cgi->param('action') eq 'unmark' ? 'f' : 't';
55 my $format = $cgi->param('format') || 'csv';
57 my $file = $cgi->param('idfile');
59 my $col = $cgi->param('idcolumn') || 0;
60 my $csv = new Text::CSV;
64 my @data = $csv->fields;
72 if (!@xacts) { # try pathinfo
73 my $path_rec = $cgi->path_info();
75 @xacts = map { $_ ? ($_) : () } split '/', $path_rec;
79 return 404 unless @xacts;
83 my ($yr,$mon,$day) = (localtime())[5,4,3]; $yr += 1900;
84 my $date = sprintf('%d-%02d-%02d',$yr,$mon,$day);
86 my @header = ( '"Transaction ID"', '"Message"', '"Amount Owed"', '"Transaction Start Date"', '"User Barcode"' );
88 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
89 my $actor = OpenSRF::AppSession->create('open-ils.actor');
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);
95 for my $xact ( @xacts ) {
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);
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;
114 my $failures = $actor->request('open-ils.actor.user.perm.check', $auth_ses, $user->id, $w, ['MARK_BAD_DEBT'])->gather(1);
117 push @lines, [ $xact, '"Permission Failure"', '""', '""', '""' ];
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"', '""', '""', '""' ];
124 my $amount = $s->balance_owed;
125 my $start = $s->xact_start;
126 my $barcode = $c->barcode;
128 if ( $mark_bad eq 't' ) {
129 push @lines, [ $xact, '"Marked Bad Debt"', $amount, "\"$start\"", "\"$barcode\"" ];
131 push @lines, [ $xact, '"Unmarked Bad Debt"', $amount, "\"$start\"", "\"$barcode\"" ];
136 push @lines, [ $xact, '"Update Failure"', '""', '""', '""' ];
140 $cstore->request('open-ils.cstore.transaction.commit')->gather(1);
141 $cstore->disconnect();
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');
147 $r->print( join(',', @header) . "\n" );
148 $r->print( join(',', @$_ ) . "\n" ) for (@lines);
150 } elsif ($format eq 'json') {
152 $r->content_type('application/json');
157 for my $line ( @lines ) {
158 $r->print( ',' ) if $first;
162 for my $field ( 0 .. 4 ) {
163 $r->print( "$header[$field] : $$line[$field]" );
164 $r->print( ',' ) if ($field < 4);
172 return Apache2::Const::OK;
177 my $auth_token = shift;
178 return undef unless $auth_token;
180 my $user = OpenSRF::AppSession
181 ->create("open-ils.auth")
182 ->request( "open-ils.auth.session.retrieve", $auth_token )
185 if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
189 return $user if ref($user);
196 $r->content_type('text/html');
201 <title>Record Export</title>
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"/>
214 return Apache2::Const::OK;