Dyrcona

cstore direct copy lookup

Dec 9th, 2021
680
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use OpenILS::Utils::Cronscript;
  5. use Spreadsheet::Read;
  6. use List::MoreUtils qw(uniq);
  7. my $U = 'OpenILS::Application::AppUtils';
  8.  
  9. my %defaults = (
  10.     'username=s' => '',
  11.     'password=s' => '',
  12.     'workstation=s' => '',
  13.     'staff' => 1,
  14.     nolockfile => 1
  15. );
  16.  
  17. my $script = OpenILS::Utils::Cronscript->new(\%defaults);
  18. my $opts = $script->MyGetOptions();
  19.  
  20. my $authtoken = $script->authenticate({
  21.     username => $opts->{username},
  22.     password => $opts->{password},
  23.     workstation => $opts->{workstation},
  24.     staff => $opts->{staff}
  25. });
  26.  
  27. die("Failed to authenticate to Evergreen. Check auth parameters.")
  28.     unless ($authtoken);
  29.  
  30. my $book;
  31. for my $file (@ARGV) {
  32.     if ($book) {
  33.         $book->add($file);
  34.     } else {
  35.         $book = Spreadsheet::Read->new($file);
  36.     }
  37. }
  38.  
  39. # For statistics reporting:
  40. my $total = 0; # Number of spreadsheet entries.
  41. my $seen = 0; # Copies found in the database
  42. my $circs = 0; # Number of open circulations found
  43. my $checkins = 0; # Number of successful check ins
  44. my $checkfails = 0; # Number of failed check ins
  45. my $deleted = 0; # Number of successful deletions
  46. my $delfails = 0; # Failed deletes
  47.  
  48. # Read barcodes from the spreadsheet(s) into a list
  49. my @barcodes;
  50.  
  51. for (my $i = 1; $i <= scalar($book->sheets); $i++) {
  52.     my $sheet = $book->sheet($i);
  53.     my $rows = $sheet->maxrow();
  54.     my $cols = $sheet->maxcol();
  55.     # search the first row of headers for the barcode column
  56.     my ($bcolumn, $bfound);
  57.     for ($bcolumn = 1; $bcolumn <= $cols; $bcolumn++) {
  58.         my $val = $sheet->cell($bcolumn, 1);
  59.         if ($val =~ /^(?:item|copy)? *barcode/i) {
  60.             $bfound = 1;
  61.             last;
  62.         }
  63.     }
  64.     next unless ($bfound);
  65.     for (my $row = 2; $row <= $rows; $row++) {
  66.         my $barcode = $sheet->cell($bcolumn, $row);
  67.         next unless ($barcode);
  68.         push(@barcodes, $barcode);
  69.     }
  70. }
  71.  
  72. # Process said list
  73. for my $barcode (uniq(sort @barcodes)) {
  74.     $total++;
  75.     my $cstoreses = $script->session('open-ils.cstore');
  76.     my $cstorereq = $cstoreses->request(
  77.         'open-ils.cstore.direct.asset.copy.search',
  78.         {barcode=>$barcode, deleted=>'f'},
  79.         {flesh=>1, flesh_fields=>{acp=>['circulations']}}
  80.     );
  81.     while (my $cstoreres = $cstorereq->recv(timeout=>600)) {
  82.         my $copy = $cstoreres->content;
  83.         if (ref($copy) eq 'Fieldmapper::asset::copy') {
  84.             $seen++
  85.         }
  86.     }
  87.     $cstorereq->finish();
  88.     $cstoreses->disconnect();
  89. }
  90.  
  91. END {
  92.     $script->logout();
  93.     # Report.
  94.     print <<EREPORT;
  95.  
  96. Total Barcodes: $total
  97. Barcodes Found: $seen
  98.    Total Circs: $circs
  99.       Checkins: $checkins
  100.  Checkin Fails: $checkfails
  101. Copies Deleted: $deleted
  102. Failed Deletes: $delfails
  103. EREPORT
  104. }
  105.  
RAW Paste Data