[open-ils-commits] r11372 - trunk/Open-ILS/src/perlmods/OpenILS/WWW

svn at svn.open-ils.org svn at svn.open-ils.org
Mon Dec 1 15:36:52 EST 2008


Author: miker
Date: 2008-12-01 15:36:48 -0500 (Mon, 01 Dec 2008)
New Revision: 11372

Modified:
   trunk/Open-ILS/src/perlmods/OpenILS/WWW/BadDebt.pm
Log:
add JSON output for programatic table-ification of bad-debt report

Modified: trunk/Open-ILS/src/perlmods/OpenILS/WWW/BadDebt.pm
===================================================================
--- trunk/Open-ILS/src/perlmods/OpenILS/WWW/BadDebt.pm	2008-12-01 20:27:40 UTC (rev 11371)
+++ trunk/Open-ILS/src/perlmods/OpenILS/WWW/BadDebt.pm	2008-12-01 20:36:48 UTC (rev 11372)
@@ -51,6 +51,7 @@
     return 403 unless $user;
 
 	my $mark_bad = $cgi->param('action') eq 'unmark' ? 'f' : 't';
+	my $format = $cgi->param('format') || 'csv';
 
 	my $file = $cgi->param('idfile');
 	if ($file) {
@@ -76,14 +77,13 @@
 
     return 404 unless @xacts;
 
+    my @lines;
+
     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');
+    my @header = ( '"Transaction ID"', '"Message"', '"Amount Owed"', '"Transaction Start Date"', '"User Barcode"' );
 
-    $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');
 
@@ -104,27 +104,61 @@
             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");
+                push @lines, [ $xact, '"Permission Failure"', '""', '""', '""' ];
             } 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");
+                    push @lines, [ $xact, '"Update Failure"', '""', '""', '""' ];
                 } 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");
+
+                    if ( $mark_bad eq 't' ) {
+                        push @lines, [ $xact, '"Marked Bad Debt"', $amount, "\"$start\"", "\"$barcode\"" ];
+                    } else {
+                        push @lines, [ $xact, '"Unmarked Bad Debt"', $amount, "\"$start\"", "\"$barcode\"" ];
+                    }
                 }
             }
         } otherwise {
-            $r->print("$xact,\"Update Failure\",\"\",\"\",\"\"\n");
+            push @lines, [ $xact, '"Update Failure"', '""', '""', '""' ];
         };
     }
 
     $cstore->request('open-ils.cstore.transaction.commit')->gather(1);
     $cstore->disconnect();
 
+    if ($format eq 'csv') {
+        $r->headers_out->set("Content-Disposition" => "inline; filename=bad_debt_$date.csv");
+	    $r->content_type('application/octet-stream');
+
+        $r->print( join(',', @header) . "\n" );
+        $r->print( join(',', @$_    ) . "\n" ) for (@lines);
+
+    } elsif ($format eq 'json') {
+
+	    $r->content_type('application/json');
+
+        $r->print( '[' );
+
+        my $first = 1;
+        for my $line ( @lines ) {
+            $r->print( ',' ) if $first;
+            $first = 0;
+
+            $r->print( '{' );
+            for my $field ( 0 .. 4 ) {
+                $r->print( "$header[$field] : $$line[$field]" );
+                $r->print( ',' ) if ($field < 4);
+            }
+            $r->print( '}' );
+        }
+
+        $r->print( ']' );
+    }
+
 	return Apache2::Const::OK;
 
 }



More information about the open-ils-commits mailing list