Bug 26591: SIP option prevcheckout_block_checkout to block checkout of previously...
[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 C4::SIP::Sip qw(siplog);
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     my $account = shift;
51         siplog('LOG_DEBUG', "ILS::Transaction::Checkout performing checkout...");
52     my $shelf          = $self->{item}->hold_attached;
53         my $barcode        = $self->{item}->id;
54     my $patron         = Koha::Patrons->find($self->{patron}->{borrowernumber});
55     my $overridden_duedate; # usually passed as undef to AddIssue
56     my $prevcheckout_block_checkout  = $account->{prevcheckout_block_checkout};
57     $debug and warn "do_checkout borrower: . " . $patron->borrowernumber;
58     my ($issuingimpossible, $needsconfirmation) = _can_we_issue($patron, $barcode,
59         C4::Context->preference("AllowItemsOnHoldCheckoutSIP")
60     );
61
62     my $noerror=1;  # If set to zero we block the issue
63     if (keys %{$issuingimpossible}) {
64         foreach (keys %{$issuingimpossible}) {
65             # do something here so we pass these errors
66             $self->screen_msg("Issue failed : $_");
67             $noerror = 0;
68             last;
69         }
70     } else {
71         foreach my $confirmation (keys %{$needsconfirmation}) {
72             if ($confirmation eq 'RENEW_ISSUE'){
73                 $self->screen_msg("Item already checked out to you: renewing item.");
74             } elsif ($confirmation eq 'RESERVED' or $confirmation eq 'RESERVE_WAITING') {
75                 my $x = $self->{item}->available($patron->borrowernumber);
76                 if ($x) {
77                     $self->screen_msg("Item was reserved for you.");
78                 } else {
79                     $self->screen_msg("Item is reserved for another patron upon return.");
80                     $noerror = 0;
81                 }
82             } elsif ($confirmation eq 'ISSUED_TO_ANOTHER') {
83                 $self->screen_msg("Item already checked out to another patron.  Please return item for check-in.");
84                 $noerror = 0;
85                 last;
86             } elsif ($confirmation eq 'DEBT') {
87                 $self->screen_msg('Outstanding Fines block issue');
88                 $noerror = 0;
89                 last;
90             } elsif ($confirmation eq 'HIGHHOLDS') {
91                 $overridden_duedate = $needsconfirmation->{$confirmation}->{returndate};
92                 $self->screen_msg('Loan period reduced for high-demand item');
93             } elsif ($confirmation eq 'RENTALCHARGE') {
94                 if ($self->{fee_ack} ne 'Y') {
95                     $noerror = 0;
96                     last;
97                 }
98             } elsif ($confirmation eq 'PREVISSUE') {
99                 $self->screen_msg("This item was previously checked out by you");
100                 $noerror = 0 if ($prevcheckout_block_checkout);
101                 last;
102             } elsif ( $confirmation eq 'ADDITIONAL_MATERIALS' ) {
103                 $self->screen_msg('Item must be checked out at a circulation desk');
104                 $noerror = 0;
105                 last;
106             } else {
107                 # We've been returned a case other than those above
108                 $self->screen_msg("Item cannot be issued: $confirmation");
109                 $noerror = 0;
110                 siplog('LOG_DEBUG', "Blocking checkout Reason:$confirmation");
111                 last;
112             }
113         }
114     }
115     my $itemnumber = $self->{item}->{itemnumber};
116     foreach (@$shelf) {
117         $debug and warn sprintf( "shelf has (%s for %s). this is (%is, %s)", $_->{itemnumber}, $_->{borrowernumber}, $itemnumber, $patron->borrowernumber );
118         ($_->{itemnumber} eq $itemnumber) or next;    # skip it if not this item
119         ($_->{borrowernumber} == $patron->borrowernumber) and last;
120             # if item was waiting for this patron, we're done.  AddIssue takes care of the "W" hold.
121         $debug and warn "Item is on hold for another patron.";
122         $self->screen_msg("Item is on hold for another patron.");
123         $noerror = 0;
124     }
125     my ($fee, undef) = GetIssuingCharges($itemnumber, $patron->borrowernumber);
126     if ( $fee > 0 ) {
127         $self->{sip_fee_type} = '06';
128         $self->{fee_amount} = sprintf '%.2f', $fee;
129         if ($self->{fee_ack} eq 'N' ) {
130             $noerror = 0;
131         }
132     }
133         unless ($noerror) {
134                 $debug and warn "cannot issue: " . Dumper($issuingimpossible) . "\n" . Dumper($needsconfirmation);
135                 $self->ok(0);
136                 return $self;
137         }
138         # can issue
139     $debug and warn sprintf("do_checkout: calling AddIssue(%s, %s, %s, 0)\n", $patron->borrowernumber, $barcode, $overridden_duedate)
140                 . "w/ C4::Context->userenv: " . Dumper(C4::Context->userenv);
141     my $issue = AddIssue( $patron->unblessed, $barcode, $overridden_duedate, 0 );
142     $self->{due} = $self->duedatefromissue($issue, $itemnumber);
143
144     $self->ok(1);
145     return $self;
146 }
147
148 sub _can_we_issue {
149     my ( $patron, $barcode, $pref ) = @_;
150
151     my ( $issuingimpossible, $needsconfirmation, $alerts ) =
152       CanBookBeIssued( $patron, $barcode, undef, 0, $pref );
153     for my $href ( $issuingimpossible, $needsconfirmation ) {
154
155         # some data is returned using lc keys we only
156         foreach my $key ( keys %{$href} ) {
157             if ( $key =~ m/[^A-Z_]/ ) {
158                 delete $href->{$key};
159             }
160         }
161     }
162     return ( $issuingimpossible, $needsconfirmation );
163 }
164
165 1;
166 __END__