Bug 26922: Regression tests
[koha.git] / C4 / SIP / ILS / Transaction / Checkout.pm
blobaabe5e6830e3e3c288015b68e828603dcf9b09f3
2 # An object to handle checkout status
5 package C4::SIP::ILS::Transaction::Checkout;
7 use warnings;
8 use strict;
10 use POSIX qw(strftime);
11 use C4::SIP::Sip qw(siplog);
12 use Data::Dumper;
13 use CGI qw ( -utf8 );
15 use C4::SIP::ILS::Transaction;
17 use C4::Context;
18 use C4::Circulation;
19 use C4::Members;
20 use C4::Reserves qw(ModReserveFill);
21 use C4::Debug;
22 use Koha::DateUtils;
24 use parent qw(C4::SIP::ILS::Transaction);
26 our $debug;
29 # Most fields are handled by the Transaction superclass
30 my %fields = (
31 security_inhibit => 0,
32 due => undef,
33 renew_ok => 0,
36 sub new {
37 my $class = shift;;
38 my $self = $class->SUPER::new();
39 foreach my $element (keys %fields) {
40 $self->{_permitted}->{$element} = $fields{$element};
42 @{$self}{keys %fields} = values %fields;
43 # $self->{'due'} = time() + (60*60*24*14); # two weeks hence
44 $debug and warn "new ILS::Transaction::Checkout : " . Dumper $self;
45 return bless $self, $class;
48 sub do_checkout {
49 my $self = shift;
50 siplog('LOG_DEBUG', "ILS::Transaction::Checkout performing checkout...");
51 my $shelf = $self->{item}->hold_attached;
52 my $barcode = $self->{item}->id;
53 my $patron = Koha::Patrons->find($self->{patron}->{borrowernumber});
54 my $overridden_duedate; # usually passed as undef to AddIssue
55 $debug and warn "do_checkout borrower: . " . $patron->borrowernumber;
56 my ($issuingimpossible, $needsconfirmation) = _can_we_issue($patron, $barcode,
57 C4::Context->preference("AllowItemsOnHoldCheckoutSIP")
60 my $noerror=1; # If set to zero we block the issue
61 if (keys %{$issuingimpossible}) {
62 foreach (keys %{$issuingimpossible}) {
63 # do something here so we pass these errors
64 $self->screen_msg("Issue failed : $_");
65 $noerror = 0;
66 last;
68 } else {
69 foreach my $confirmation (keys %{$needsconfirmation}) {
70 if ($confirmation eq 'RENEW_ISSUE'){
71 $self->screen_msg("Item already checked out to you: renewing item.");
72 } elsif ($confirmation eq 'RESERVED' or $confirmation eq 'RESERVE_WAITING') {
73 my $x = $self->{item}->available($patron->borrowernumber);
74 if ($x) {
75 $self->screen_msg("Item was reserved for you.");
76 } else {
77 $self->screen_msg("Item is reserved for another patron upon return.");
78 $noerror = 0;
80 } elsif ($confirmation eq 'ISSUED_TO_ANOTHER') {
81 $self->screen_msg("Item already checked out to another patron. Please return item for check-in.");
82 $noerror = 0;
83 last;
84 } elsif ($confirmation eq 'DEBT') {
85 $self->screen_msg('Outstanding Fines block issue');
86 $noerror = 0;
87 last;
88 } elsif ($confirmation eq 'HIGHHOLDS') {
89 $overridden_duedate = $needsconfirmation->{$confirmation}->{returndate};
90 $self->screen_msg('Loan period reduced for high-demand item');
91 } elsif ($confirmation eq 'RENTALCHARGE') {
92 if ($self->{fee_ack} ne 'Y') {
93 $noerror = 0;
94 last;
96 } elsif ($confirmation eq 'PREVISSUE') {
97 $self->screen_msg("This item was previously checked out by you");
98 last;
99 } elsif ( $confirmation eq 'ADDITIONAL_MATERIALS' ) {
100 $self->screen_msg('Item must be checked out at a circulation desk');
101 $noerror = 0;
102 last;
103 } else {
104 # We've been returned a case other than those above
105 $self->screen_msg("Item cannot be issued: $confirmation");
106 $noerror = 0;
107 siplog('LOG_DEBUG', "Blocking checkout Reason:$confirmation");
108 last;
112 my $itemnumber = $self->{item}->{itemnumber};
113 foreach (@$shelf) {
114 $debug and warn sprintf( "shelf has (%s for %s). this is (%is, %s)", $_->{itemnumber}, $_->{borrowernumber}, $itemnumber, $patron->borrowernumber );
115 ($_->{itemnumber} eq $itemnumber) or next; # skip it if not this item
116 ($_->{borrowernumber} == $patron->borrowernumber) and last;
117 # if item was waiting for this patron, we're done. AddIssue takes care of the "W" hold.
118 $debug and warn "Item is on hold for another patron.";
119 $self->screen_msg("Item is on hold for another patron.");
120 $noerror = 0;
122 my ($fee, undef) = GetIssuingCharges($itemnumber, $patron->borrowernumber);
123 if ( $fee > 0 ) {
124 $self->{sip_fee_type} = '06';
125 $self->{fee_amount} = sprintf '%.2f', $fee;
126 if ($self->{fee_ack} eq 'N' ) {
127 $noerror = 0;
130 unless ($noerror) {
131 $debug and warn "cannot issue: " . Dumper($issuingimpossible) . "\n" . Dumper($needsconfirmation);
132 $self->ok(0);
133 return $self;
135 # can issue
136 $debug and warn sprintf("do_checkout: calling AddIssue(%s, %s, %s, 0)\n", $patron->borrowernumber, $barcode, $overridden_duedate)
137 . "w/ C4::Context->userenv: " . Dumper(C4::Context->userenv);
138 my $issue = AddIssue( $patron->unblessed, $barcode, $overridden_duedate, 0 );
139 $self->{due} = $self->duedatefromissue($issue, $itemnumber);
141 $self->ok(1);
142 return $self;
145 sub _can_we_issue {
146 my ( $patron, $barcode, $pref ) = @_;
148 my ( $issuingimpossible, $needsconfirmation, $alerts ) =
149 CanBookBeIssued( $patron, $barcode, undef, 0, $pref );
150 for my $href ( $issuingimpossible, $needsconfirmation ) {
152 # some data is returned using lc keys we only
153 foreach my $key ( keys %{$href} ) {
154 if ( $key =~ m/[^A-Z_]/ ) {
155 delete $href->{$key};
159 return ( $issuingimpossible, $needsconfirmation );
163 __END__