b321a2487608dbd5b3829a1a7499aa1689eaf407
[srvgit] / 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 Koha::DateUtils;
22
23 use parent qw(C4::SIP::ILS::Transaction);
24
25 # Most fields are handled by the Transaction superclass
26 my %fields = (
27           security_inhibit => 0,
28           due              => undef,
29           renew_ok         => 0,
30     );
31
32 sub new {
33     my $class = shift;;
34     my $self = $class->SUPER::new();
35     foreach my $element (keys %fields) {
36         $self->{_permitted}->{$element} = $fields{$element};
37     }
38     @{$self}{keys %fields} = values %fields;
39     return bless $self, $class;
40 }
41
42 sub do_checkout {
43         my $self = shift;
44     my $account = 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     my $noerror=1;  # If set to zero we block the issue
54     if (keys %{$issuingimpossible}) {
55         foreach (keys %{$issuingimpossible}) {
56             # do something here so we pass these errors
57             $self->screen_msg("Issue failed : $_");
58             $noerror = 0;
59             last;
60         }
61     } else {
62         foreach my $confirmation (keys %{$needsconfirmation}) {
63             if ($confirmation eq 'RENEW_ISSUE'){
64                 $self->screen_msg("Item already checked out to you: renewing item.");
65             } elsif ($confirmation eq 'RESERVED' and !C4::Context->preference("AllowItemsOnHoldCheckoutSIP")) {
66                 $self->screen_msg("Item is reserved for another patron upon return.");
67                 $noerror = 0;
68             } elsif ($confirmation eq 'RESERVED' and C4::Context->preference("AllowItemsOnHoldCheckoutSIP")) {
69                 next;
70             } elsif ($confirmation eq 'RESERVE_WAITING'
71                       or $confirmation eq 'TRANSFERRED'
72                       or $confirmation eq 'PROCESSING') {
73                $self->screen_msg("Item is on hold for another patron.");
74                $noerror = 0;
75             } elsif ($confirmation eq 'ISSUED_TO_ANOTHER') {
76                 $self->screen_msg("Item already checked out to another patron.  Please return item for check-in.");
77                 $noerror = 0;
78                 last;
79             } elsif ($confirmation eq 'DEBT') {
80                 $self->screen_msg('Outstanding Fines block issue');
81                 $noerror = 0;
82                 last;
83             } elsif ($confirmation eq 'HIGHHOLDS') {
84                 $overridden_duedate = $needsconfirmation->{$confirmation}->{returndate};
85                 $self->screen_msg('Loan period reduced for high-demand item');
86             } elsif ($confirmation eq 'RENTALCHARGE') {
87                 if ($self->{fee_ack} ne 'Y') {
88                     $noerror = 0;
89                     last;
90                 }
91             } elsif ($confirmation eq 'PREVISSUE') {
92                 $self->screen_msg("This item was previously checked out by you");
93                 $noerror = 0 if ($prevcheckout_block_checkout);
94                 last;
95             } elsif ( $confirmation eq 'ADDITIONAL_MATERIALS' ) {
96                 $self->screen_msg('Item must be checked out at a circulation desk');
97                 $noerror = 0;
98                 last;
99             } else {
100                 # We've been returned a case other than those above
101                 $self->screen_msg("Item cannot be issued: $confirmation");
102                 $noerror = 0;
103                 siplog('LOG_DEBUG', "Blocking checkout Reason:$confirmation");
104                 last;
105             }
106         }
107     }
108     my $itemnumber = $self->{item}->{itemnumber};
109     my ($fee, undef) = GetIssuingCharges($itemnumber, $patron->borrowernumber);
110     if ( $fee > 0 ) {
111         $self->{sip_fee_type} = '06';
112         $self->{fee_amount} = sprintf '%.2f', $fee;
113         if ($self->{fee_ack} eq 'N' ) {
114             $noerror = 0;
115         }
116     }
117         unless ($noerror) {
118                 $self->ok(0);
119                 return $self;
120         }
121         # can issue
122     my $issue = AddIssue( $patron->unblessed, $barcode, $overridden_duedate, 0 );
123     $self->{due} = $self->duedatefromissue($issue, $itemnumber);
124
125     $self->ok(1);
126     return $self;
127 }
128
129 sub _can_we_issue {
130     my ( $patron, $barcode, $pref ) = @_;
131
132     my ( $issuingimpossible, $needsconfirmation, $alerts ) =
133       CanBookBeIssued( $patron, $barcode, undef, 0, $pref );
134     for my $href ( $issuingimpossible, $needsconfirmation ) {
135
136         # some data is returned using lc keys we only
137         foreach my $key ( keys %{$href} ) {
138             if ( $key =~ m/[^A-Z_]/ ) {
139                 delete $href->{$key};
140             }
141         }
142     }
143     return ( $issuingimpossible, $needsconfirmation );
144 }
145
146 1;
147 __END__