/Open-ILS/src/perlmods/lib/OpenILS/SIP/Transaction/Checkout.pm

https://github.com/PTFS-Europe/SEDAR-Evergreen · Perl · 142 lines · 89 code · 35 blank · 18 comment · 19 complexity · 47602026235d7ec5f66cfc9ee36bca86 MD5 · raw file

  1. #
  2. # An object to handle checkout status
  3. #
  4. package OpenILS::SIP::Transaction::Checkout;
  5. use warnings;
  6. use strict;
  7. use POSIX qw(strftime);
  8. use OpenILS::SIP;
  9. use OpenILS::SIP::Transaction;
  10. use OpenILS::SIP::Msg qw/:const/;
  11. use Sys::Syslog qw(syslog);
  12. use OpenILS::Application::AppUtils;
  13. my $U = 'OpenILS::Application::AppUtils';
  14. our @ISA = qw(OpenILS::SIP::Transaction);
  15. # Most fields are handled by the Transaction superclass
  16. my %fields = (
  17. security_inhibit => 0,
  18. due => undef,
  19. renew_ok => 0,
  20. );
  21. sub new {
  22. my $class = shift;
  23. my $self = $class->SUPER::new(@_);
  24. my $element;
  25. foreach $element (keys %fields) {
  26. $self->{_permitted}->{$element} = $fields{$element};
  27. }
  28. @{$self}{keys %fields} = values %fields;
  29. $self->load_override_events;
  30. return bless $self, $class;
  31. }
  32. # Lifted from Checkin.pm to load the list of events that we'll try to
  33. # override if they occur during checkout or renewal.
  34. my %override_events;
  35. sub load_override_events {
  36. return if %override_events;
  37. my $override = OpenILS::SIP->config->{implementation_config}->{checkout_override};
  38. return unless $override;
  39. my $events = $override->{event};
  40. $events = [$events] unless ref $events eq 'ARRAY';
  41. $override_events{$_} = 1 for @$events;
  42. }
  43. # if this item is already checked out to the requested patron,
  44. # renew the item and set $self->renew_ok to true.
  45. # XXX if it's a renewal and the renewal is not permitted, set
  46. # $self->screen_msg("Item on Hold for Another User"); (or somesuch)
  47. # XXX Set $self->ok(0) on any errors
  48. sub do_checkout {
  49. my $self = shift;
  50. my $is_renew = shift || 0;
  51. $self->ok(0);
  52. my $args = {
  53. barcode => $self->{item}->id,
  54. patron_barcode => $self->{patron}->id
  55. };
  56. my ($resp, $method);
  57. my $override = 0;
  58. if ($is_renew) {
  59. $method = 'open-ils.circ.renew';
  60. } else {
  61. $method = 'open-ils.circ.checkout.full';
  62. }
  63. while (1) {
  64. $method .= '.override' if ($override);
  65. $resp = $U->simplereq('open-ils.circ', $method, $self->{authtoken}, $args);
  66. $resp = [$resp] unless ref $resp eq 'ARRAY';
  67. syslog('LOG_DEBUG', "OILS: $method returned event: " . OpenSRF::Utils::JSON->perl2JSON($resp));
  68. my $first_code = $U->event_code($$resp[0]);
  69. if (@$resp > 1 || !defined $first_code || $first_code ne '0') {
  70. # We got one or more non-success events
  71. $self->screen_msg('');
  72. for my $r (@$resp) {
  73. my $code = $U->event_code($r);
  74. my $txt = $r->{textcode};
  75. syslog('LOG_INFO', "OILS: $method failed with event $code : $txt");
  76. if ($override_events{$txt} && $method !~ /override$/) {
  77. # Found an event we've been configured to override.
  78. $override = 1;
  79. } elsif ($txt =~ /ITEM_(?:DEPOSIT|RENTAL)_FEE_REQUIRED/ && $self->fee_ack) {
  80. # Patron acknowledged the fee.
  81. $override = 1;
  82. } elsif ( $txt eq 'OPEN_CIRCULATION_EXISTS' ) {
  83. $self->screen_msg(OILS_SIP_MSG_CIRC_EXISTS);
  84. return 0;
  85. } else {
  86. $self->screen_msg(OILS_SIP_MSG_CIRC_PERMIT_FAILED);
  87. return 0;
  88. }
  89. }
  90. # This looks potentially dangerous, but we shouldn't
  91. # end up here if the loop iterated with $override = 1;
  92. next if ($override && $method !~ /override$/);
  93. } else {
  94. # We appear to have success.
  95. # De-arrayify the response.
  96. $resp = $$resp[0];
  97. syslog('LOG_INFO', "OILS: $method succeeded");
  98. my $circ = $resp->{payload}->{circ};
  99. $self->{'due'} = OpenILS::SIP->format_date($circ->due_date, 'due');
  100. $self->ok(1);
  101. last;
  102. }
  103. last if ($method =~ /override$/);
  104. }
  105. return $self->ok;
  106. }
  107. 1;