]> git.koha-community.org Git - koha.git/blob - C4/SIP/ILS/Transaction/Checkout.pm
Bug 14673: Work around change to AddIssue return
[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 our $VERSION = 3.07.00.049;
29
30 # Most fields are handled by the Transaction superclass
31 my %fields = (
32               security_inhibit => 0,
33               due              => undef,
34               renew_ok         => 0,
35         );
36
37 sub new {
38     my $class = shift;;
39     my $self = $class->SUPER::new();
40     foreach my $element (keys %fields) {
41                 $self->{_permitted}->{$element} = $fields{$element};
42     }
43     @{$self}{keys %fields} = values %fields;
44 #    $self->{'due'} = time() + (60*60*24*14); # two weeks hence
45         $debug and warn "new ILS::Transaction::Checkout : " . Dumper $self;
46     return bless $self, $class;
47 }
48
49 sub do_checkout {
50         my $self = shift;
51         syslog('LOG_DEBUG', "ILS::Transaction::Checkout performing checkout...");
52         my $pending        = $self->{item}->pending_queue;
53         my $shelf          = $self->{item}->hold_shelf;
54         my $barcode        = $self->{item}->id;
55         my $patron_barcode = $self->{patron}->id;
56         my $overridden_duedate; # usually passed as undef to AddIssue
57         $debug and warn "do_checkout: patron (" . $patron_barcode . ")";
58         my $borrower = $self->{patron}->getmemberdetails_object();
59         $debug and warn "do_checkout borrower: . " . Dumper $borrower;
60         my ($issuingimpossible,$needsconfirmation) = CanBookBeIssued(
61         $borrower,
62         $barcode,
63         undef,
64         0,
65         C4::Context->preference("AllowItemsOnHoldCheckout")
66     );
67         my $noerror=1;
68     if (scalar keys %$issuingimpossible) {
69         foreach (keys %$issuingimpossible) {
70             # do something here so we pass these errors
71             $self->screen_msg($_ . ': ' . $issuingimpossible->{$_});
72             $noerror = 0;
73         }
74     } else {
75         foreach my $confirmation (keys %{$needsconfirmation}) {
76             if ($confirmation eq 'RENEW_ISSUE'){
77                 $self->screen_msg("Item already checked out to you: renewing item.");
78             } elsif ($confirmation eq 'RESERVED' or $confirmation eq 'RESERVE_WAITING') {
79                 my $x = $self->{item}->available($patron_barcode);
80                 if ($x) {
81                     $self->screen_msg("Item was reserved for you.");
82                 } else {
83                     $self->screen_msg("Item is reserved for another patron upon return.");
84                     # $noerror = 0;
85                 }
86             } elsif ($confirmation eq 'ISSUED_TO_ANOTHER') {
87                 $self->screen_msg("Item already checked out to another patron.  Please return item for check-in.");
88                 $noerror = 0;
89             } elsif ($confirmation eq 'DEBT') {
90                 $self->screen_msg('Outstanding Fines block issue');
91                 $noerror = 0;
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                 }
99             } else {
100                 $self->screen_msg($needsconfirmation->{$confirmation});
101                 $noerror = 0;
102                 syslog('LOG_DEBUG', "Blocking checkout Reason:$confirmation");
103             }
104         }
105     }
106     my $itemnumber = $self->{item}->{itemnumber};
107     foreach (@$shelf) {
108         $debug and warn "shelf has ($_->{itemnumber} for $_->{borrowernumber}). this is ($itemnumber, $self->{patron}->{borrowernumber})";
109         ($_->{itemnumber} eq $itemnumber) or next;    # skip it if not this item
110         ($_->{borrowernumber} == $self->{patron}->{borrowernumber}) and last;
111             # if item was waiting for this patron, we're done.  AddIssue takes care of the "W" hold.
112         $debug and warn "Item is on hold shelf for another patron.";
113         $self->screen_msg("Item is on hold shelf for another patron.");
114         $noerror = 0;
115     }
116     my ($fee, undef) = GetIssuingCharges($itemnumber, $self->{patron}->{borrowernumber});
117     if ( $fee > 0 ) {
118         $self->{sip_fee_type} = '06';
119         $self->{fee_amount} = sprintf '%.2f', $fee;
120         if ($self->{fee_ack} eq 'N' ) {
121             $noerror = 0;
122         }
123     }
124         unless ($noerror) {
125                 $debug and warn "cannot issue: " . Dumper($issuingimpossible) . "\n" . Dumper($needsconfirmation);
126                 $self->ok(0);
127                 return $self;
128         }
129     # Fill any reserves the patron had on the item.  
130     # TODO: this logic should be pulled internal to AddIssue for all Koha. 
131     $debug and warn "pending_queue: " . (@$pending) ? Dumper($pending) : '[]';
132     foreach (grep {$_->{borrowernumber} eq $self->{patron}->{borrowernumber}} @$pending) {
133         $debug and warn "Filling reserve (borrowernumber,biblionumber,reservedate): "
134             . sprintf("(%s,%s,%s)\n",$_->{borrowernumber},$_->{biblionumber},$_->{reservedate});
135         ModReserveFill($_);
136         # TODO: adjust representation in $self->item
137     }
138         # can issue
139         $debug and warn "do_checkout: calling AddIssue(\$borrower,$barcode, $overridden_duedate, 0)\n"
140                 # . "w/ \$borrower: " . Dumper($borrower)
141                 . "w/ C4::Context->userenv: " . Dumper(C4::Context->userenv);
142     my $issue = AddIssue( $borrower, $barcode, $overridden_duedate, 0 );
143     $self->{due} = $self->duedatefromissue($issue, $itemnumber);
144
145         $self->ok(1);
146         return $self;
147 }
148
149 1;
150 __END__