Bug 12225: Re-implement no block flag for checkouts
[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 qw( AddIssue GetIssuingCharges CanBookBeIssued );
19 use C4::Members;
20 use Koha::DateUtils qw( dt_from_string );
21
22 use parent qw(C4::SIP::ILS::Transaction);
23
24 # Most fields are handled by the Transaction superclass
25 my %fields = (
26           security_inhibit => 0,
27           due              => undef,
28           renew_ok         => 0,
29     );
30
31 sub new {
32     my $class = shift;;
33     my $self = $class->SUPER::new();
34     foreach my $element (keys %fields) {
35         $self->{_permitted}->{$element} = $fields{$element};
36     }
37     @{$self}{keys %fields} = values %fields;
38     return bless $self, $class;
39 }
40
41 sub do_checkout {
42         my $self = shift;
43     my $account = shift;
44     my $no_block_due_date = shift;
45         siplog('LOG_DEBUG', "ILS::Transaction::Checkout performing checkout...");
46     my $shelf          = $self->{item}->hold_attached;
47         my $barcode        = $self->{item}->id;
48     my $patron         = Koha::Patrons->find($self->{patron}->{borrowernumber});
49     my $overridden_duedate; # usually passed as undef to AddIssue
50     my $prevcheckout_block_checkout  = $account->{prevcheckout_block_checkout};
51     my ($issuingimpossible, $needsconfirmation) = _can_we_issue($patron, $barcode, 0);
52
53     if ( $no_block_due_date ) {
54         my $year = substr($no_block_due_date,0,4);
55         my $month = substr($no_block_due_date,4,2);
56         my $day = substr($no_block_due_date,6,2);
57         my $hour = substr($no_block_due_date,12,2);
58         my $minute = substr($no_block_due_date,14,2);
59         my $second = substr($no_block_due_date,16,2);
60
61         my $iso = "$year-$month-$day $hour:$minute:$second";
62         $no_block_due_date = dt_from_string( $iso, "iso" );
63     }
64
65     my $noerror=1;  # If set to zero we block the issue
66     if (keys %{$issuingimpossible}) {
67         foreach (keys %{$issuingimpossible}) {
68             # do something here so we pass these errors
69             $self->screen_msg("Issue failed : $_");
70             $noerror = 0;
71             last;
72         }
73     } else {
74         foreach my $confirmation (keys %{$needsconfirmation}) {
75             if ($confirmation eq 'RENEW_ISSUE'){
76                 $self->screen_msg("Item already checked out to you: renewing item.");
77             } elsif ($confirmation eq 'RESERVED' and !C4::Context->preference("AllowItemsOnHoldCheckoutSIP")) {
78                 $self->screen_msg("Item is reserved for another patron upon return.");
79                 $noerror = 0;
80             } elsif ($confirmation eq 'RESERVED' and C4::Context->preference("AllowItemsOnHoldCheckoutSIP")) {
81                 next;
82             } elsif ($confirmation eq 'RESERVE_WAITING'
83                       or $confirmation eq 'TRANSFERRED'
84                       or $confirmation eq 'PROCESSING') {
85                $self->screen_msg("Item is on hold for another patron.");
86                $noerror = 0;
87             } elsif ($confirmation eq 'ISSUED_TO_ANOTHER') {
88                 $self->screen_msg("Item already checked out to another patron.  Please return item for check-in.");
89                 $noerror = 0;
90                 last;
91             } elsif ($confirmation eq 'DEBT') {
92                 $self->screen_msg('Outstanding Fines block issue');
93                 $noerror = 0;
94                 last;
95             } elsif ($confirmation eq 'HIGHHOLDS') {
96                 $overridden_duedate = $needsconfirmation->{$confirmation}->{returndate};
97                 $self->screen_msg('Loan period reduced for high-demand item');
98             } elsif ($confirmation eq 'RENTALCHARGE') {
99                 if ($self->{fee_ack} ne 'Y') {
100                     $noerror = 0;
101                     last;
102                 }
103             } elsif ($confirmation eq 'PREVISSUE') {
104                 $self->screen_msg("This item was previously checked out by you");
105                 $noerror = 0 if ($prevcheckout_block_checkout);
106                 last;
107             } elsif ( $confirmation eq 'ADDITIONAL_MATERIALS' ) {
108                 $self->screen_msg('Item must be checked out at a circulation desk');
109                 $noerror = 0;
110                 last;
111             } else {
112                 # We've been returned a case other than those above
113                 $self->screen_msg("Item cannot be issued: $confirmation");
114                 $noerror = 0;
115                 siplog('LOG_DEBUG', "Blocking checkout Reason:$confirmation");
116                 last;
117             }
118         }
119     }
120     my $itemnumber = $self->{item}->{itemnumber};
121     my ($fee, undef) = GetIssuingCharges($itemnumber, $patron->borrowernumber);
122     if ( $fee > 0 ) {
123         $self->{sip_fee_type} = '06';
124         $self->{fee_amount} = sprintf '%.2f', $fee;
125         if ($self->{fee_ack} eq 'N' ) {
126             $noerror = 0;
127         }
128     }
129
130     if ( $no_block_due_date ) {
131         $overridden_duedate = $no_block_due_date;
132         $noerror = 1;
133     }
134
135         unless ($noerror) {
136                 $self->ok(0);
137                 return $self;
138         }
139
140         # can issue
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__