Dyrcona

forgive_bills.pl

Nov 7th, 2018
449
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl
  2. # ---------------------------------------------------------------
  3. # Copyright © 2014 Merrimack Valley Library Consortium
  4. # Jason Stephenson <jason@sigio.com>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. # ---------------------------------------------------------------
  16.  
  17. use strict;
  18. use warnings;
  19.  
  20. use OpenILS::Utils::Cronscript;
  21. use Data::Dumper;
  22.  
  23. my $apputils = 'OpenILS::Application::AppUtils';
  24.  
  25. my $script = OpenILS::Utils::Cronscript->new({nolockfile=>1});
  26.  
  27. my $login = {
  28.     username => $ARGV[0],
  29.     password => $ARGV[1],
  30.     workstation => $ARGV[2],
  31.     type => 'staff'
  32. };
  33.  
  34. my $authtoken = $script->authenticate($login);
  35.  
  36. die "failed to authenticate" unless($authtoken);
  37.  
  38. END {
  39.     $script->logout();
  40. }
  41.  
  42. my $barcode;
  43. do {
  44.     print ("Enter barcode: ");
  45.     $barcode = <STDIN>;
  46.     chomp($barcode);
  47.     if ($barcode) {
  48.         my $user = retrieve_user($barcode);
  49.         if (!defined($user) || ref($user) eq 'HASH') {
  50.             print Dumper $user;
  51.         } else {
  52.             my $bills = $apputils->simplereq(
  53.                 'open-ils.actor',
  54.                 'open-ils.actor.user.transactions.history.have_balance',
  55.                 $authtoken,
  56.                 $user->id
  57.             );
  58.  
  59.             if (ref($bills) eq 'ARRAY') {
  60.                 my @payments = ();
  61.                 foreach my $bill (@$bills) {
  62.                     printf("\t%d : %.2f\n", $bill->id, $bill->balance_owed);
  63.                     push(@payments, [$bill->id, $bill->balance_owed])
  64.                         if ($bill->balance_owed > 0.0);
  65.                 }
  66.                 if (@payments) {
  67.                     my $r = forgive_bills($user, \@payments);
  68.                     print Dumper $r;
  69.                 }
  70.             } else {
  71.                 print Dumper $bills;
  72.             }
  73.         }
  74.     }
  75. } while ($barcode);
  76.  
  77. sub retrieve_user {
  78.     my $barcode = shift;
  79.     my $e = $script->editor(authtoken=>$authtoken);
  80.     my $card = $e->search_actor_card({barcode => $barcode, active => 't'});
  81.     return $e->event unless($card);
  82.     my $user = $e->retrieve_actor_user($card->[0]->usr) if (@$card);
  83.     return $e->event unless($user);
  84.     $e->finish;
  85.     return $user;
  86. }
  87.  
  88. sub forgive_bills {
  89.     my $user = shift;
  90.     my $paymentref = shift;
  91.  
  92.     my $result = $apputils->simplereq(
  93.         'open-ils.circ',
  94.         'open-ils.circ.money.payment',
  95.         $authtoken,
  96.         {
  97.             payment_type => "forgive_payment",
  98.             userid => $user->id,
  99.             note => "Forgiven en masse: forgive_bills.pl",
  100.             payments => $paymentref
  101.         },
  102.         $user->last_xact_id
  103.     );
  104.     return $result;
  105. }
RAW Paste Data