Bug 20251: (bug 19280 follow-up) FIX SIP checkout
[koha.git] / C4 / SIP / ILS / Transaction / Checkout.pm
1 #
2 # An object to handle checkout status
3 #
4
5 package C4::SIP::ILS::Transaction::Checkout;
6
7 use warnings;
8 use strict;
9
10 use POSIX qw(strftime);
11 use Sys::Syslog qw(syslog);
12 use Data::Dumper;
13 use CGI qw ( -utf8 );
14
15 use C4::SIP::ILS::Transaction;
16
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;
23
24 use parent qw(C4::SIP::ILS::Transaction);
25
26 our $debug;
27
28
29 # Most fields are handled by the Transaction superclass
30 my %fields = (
31               security_inhibit => 0,
32               due              => undef,
33               renew_ok         => 0,
34         );
35
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};
41     }
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;
46 }
47
48 sub do_checkout {
49         my $self = shift;
50         syslog('LOG_DEBUG', "ILS::Transaction::Checkout performing checkout...");
51         my $pending        = $self->{item}->pending_queue;
52         my $shelf          = $self->{item}->hold_shelf;
53         my $barcode        = $self->{item}->id;
54         my $patron_barcode = $self->{patron}->id;
55         my $overridden_duedate; # usually passed as undef to AddIssue
56         $debug and warn "do_checkout: patron (" . $patron_barcode . ")";
57     my $patron = Koha::Patrons->find( { cardnumber => $patron_barcode } );
58     my $borrower = $patron->unblessed;
59         $debug and warn "do_checkout borrower: . " . Dumper $borrower;
60     my ($issuingimpossible, $needsconfirmation) = _can_we_issue($patron, $barcode,
61         C4::Context->preference("AllowItemsOnHoldCheckout")
62     );
63
64     my $noerror=1;  # If set to zero we block the issue
65     if (keys %{$issuingimpossible}) {
66         foreach (keys %{$issuingimpossible}) {
67             # do something here so we pass these errors
68             $self->screen_msg("Issue failed : $_");
69             $noerror = 0;
70             last;
71         }
72     } else {
73         foreach my $confirmation (keys %{$needsconfirmation}) {
74             if ($confirmation eq 'RENEW_ISSUE'){
75                 $self->screen_msg("Item already checked out to you: renewing item.");
76             } elsif ($confirmation eq 'RESERVED' or $confirmation eq 'RESERVE_WAITING') {
77                 my $x = $self->{item}->available($patron_barcode);
78                 if ($x) {
79                     $self->screen_msg("Item was reserved for you.");
80                 } else {
81                     $self->screen_msg("Item is reserved for another patron upon return.");
82                     $noerror = 0;
83                 }
84             } elsif ($confirmation eq 'ISSUED_TO_ANOTHER') {
85                 $self->screen_msg("Item already checked out to another patron.  Please return item for check-in.");
86                 $noerror = 0;
87                 last;
88             } elsif ($confirmation eq 'DEBT') {
89                 $self->screen_msg('Outstanding Fines block issue');
90                 $noerror = 0;
91                 last;
92             } elsif ($confirmation eq 'HIGHHOLDS') {
93                 $overridden_duedate = $needsconfirmation->{$confirmation}->{returndate};
94                 $self->screen_msg('Loan period reduced for high-demand item');
95             } elsif ($confirmation eq 'RENTALCHARGE') {
96                 if ($self->{fee_ack} ne 'Y') {
97                     $noerror = 0;
98                     last;
99                 }
100             } else {
101                 # We've been returned a case other than those above
102                 $self->screen_msg("Item cannot be issued: $confirmation");
103                 $noerror = 0;
104                 syslog('LOG_DEBUG', "Blocking checkout Reason:$confirmation");
105                 last;
106             }
107         }
108     }
109     my $itemnumber = $self->{item}->{itemnumber};
110     foreach (@$shelf) {
111         $debug and warn "shelf has ($_->{itemnumber} for $_->{borrowernumber}). this is ($itemnumber, $self->{patron}->{borrowernumber})";
112         ($_->{itemnumber} eq $itemnumber) or next;    # skip it if not this item
113         ($_->{borrowernumber} == $self->{patron}->{borrowernumber}) and last;
114             # if item was waiting for this patron, we're done.  AddIssue takes care of the "W" hold.
115         $debug and warn "Item is on hold shelf for another patron.";
116         $self->screen_msg("Item is on hold shelf for another patron.");
117         $noerror = 0;
118     }
119     my ($fee, undef) = GetIssuingCharges($itemnumber, $self->{patron}->{borrowernumber});
120     if ( $fee > 0 ) {
121         $self->{sip_fee_type} = '06';
122         $self->{fee_amount} = sprintf '%.2f', $fee;
123         if ($self->{fee_ack} eq 'N' ) {
124             $noerror = 0;
125         }
126     }
127         unless ($noerror) {
128                 $debug and warn "cannot issue: " . Dumper($issuingimpossible) . "\n" . Dumper($needsconfirmation);
129                 $self->ok(0);
130                 return $self;
131         }
132     # Fill any reserves the patron had on the item.  
133     # TODO: this logic should be pulled internal to AddIssue for all Koha. 
134     $debug and warn "pending_queue: " . (@$pending) ? Dumper($pending) : '[]';
135     foreach (grep {$_->{borrowernumber} eq $self->{patron}->{borrowernumber}} @$pending) {
136         $debug and warn "Filling reserve (borrowernumber,biblionumber,reservedate): "
137             . sprintf("(%s,%s,%s)\n",$_->{borrowernumber},$_->{biblionumber},$_->{reservedate});
138         ModReserveFill($_);
139         # TODO: adjust representation in $self->item
140     }
141         # can issue
142         $debug and warn "do_checkout: calling AddIssue(\$borrower,$barcode, $overridden_duedate, 0)\n"
143                 # . "w/ \$borrower: " . Dumper($borrower)
144                 . "w/ C4::Context->userenv: " . Dumper(C4::Context->userenv);
145     my $issue = AddIssue( $borrower, $barcode, $overridden_duedate, 0 );
146     $self->{due} = $self->duedatefromissue($issue, $itemnumber);
147
148         $self->ok(1);
149         return $self;
150 }
151
152 sub _can_we_issue {
153     my ( $patron, $barcode, $pref ) = @_;
154
155     my ( $issuingimpossible, $needsconfirmation, $alerts ) =
156       CanBookBeIssued( $patron, $barcode, undef, 0, $pref );
157     for my $href ( $issuingimpossible, $needsconfirmation ) {
158
159         # some data is returned using lc keys we only
160         foreach my $key ( keys %{$href} ) {
161             if ( $key =~ m/[^A-Z_]/ ) {
162                 delete $href->{$key};
163             }
164         }
165     }
166     return ( $issuingimpossible, $needsconfirmation );
167 }
168
169 1;
170 __END__