Bug 7904 Change SIP modules to use standard LIB path
[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 parent qw(C4::SIP::ILS::Transaction);
23
24 our $debug;
25
26 our $VERSION = 3.07.00.049;
27
28 # Most fields are handled by the Transaction superclass
29 my %fields = (
30               security_inhibit => 0,
31               due              => undef,
32               renew_ok         => 0,
33         );
34
35 sub new {
36     my $class = shift;;
37     my $self = $class->SUPER::new();
38     foreach my $element (keys %fields) {
39                 $self->{_permitted}->{$element} = $fields{$element};
40     }
41     @{$self}{keys %fields} = values %fields;
42 #    $self->{'due'} = time() + (60*60*24*14); # two weeks hence
43         $debug and warn "new ILS::Transaction::Checkout : " . Dumper $self;
44     return bless $self, $class;
45 }
46
47 sub do_checkout {
48         my $self = shift;
49         syslog('LOG_DEBUG', "ILS::Transaction::Checkout performing checkout...");
50         my $pending        = $self->{item}->pending_queue;
51         my $shelf          = $self->{item}->hold_shelf;
52         my $barcode        = $self->{item}->id;
53         my $patron_barcode = $self->{patron}->id;
54         my $overridden_duedate; # usually passed as undef to AddIssue
55         $debug and warn "do_checkout: patron (" . $patron_barcode . ")";
56         my $borrower = $self->{patron}->getmemberdetails_object();
57         $debug and warn "do_checkout borrower: . " . Dumper $borrower;
58         my ($issuingimpossible,$needsconfirmation) = CanBookBeIssued(
59         $borrower,
60         $barcode,
61         undef,
62         0,
63         C4::Context->preference("AllowItemsOnHoldCheckout")
64     );
65         my $noerror=1;
66     if (scalar keys %$issuingimpossible) {
67         foreach (keys %$issuingimpossible) {
68             # do something here so we pass these errors
69             $self->screen_msg($_ . ': ' . $issuingimpossible->{$_});
70             $noerror = 0;
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             } elsif ($confirmation eq 'DEBT') {
88                 $self->screen_msg('Outstanding Fines block issue');
89                 $noerror = 0;
90             } elsif ($confirmation eq 'HIGHHOLDS') {
91                 $overridden_duedate = $needsconfirmation->{$confirmation}->{returndate};
92                 $self->screen_msg('Loan period reduced for high-demand item');
93             } else {
94                 $self->screen_msg($needsconfirmation->{$confirmation});
95                 $noerror = 0;
96             }
97         }
98     }
99     my $itemnumber = $self->{item}->{itemnumber};
100     foreach (@$shelf) {
101         $debug and warn "shelf has ($_->{itemnumber} for $_->{borrowernumber}). this is ($itemnumber, $self->{patron}->{borrowernumber})";
102         ($_->{itemnumber} eq $itemnumber) or next;    # skip it if not this item
103         ($_->{borrowernumber} == $self->{patron}->{borrowernumber}) and last;
104             # if item was waiting for this patron, we're done.  AddIssue takes care of the "W" hold.
105         $debug and warn "Item is on hold shelf for another patron.";
106         $self->screen_msg("Item is on hold shelf for another patron.");
107         $noerror = 0;
108     }
109         unless ($noerror) {
110                 $debug and warn "cannot issue: " . Dumper($issuingimpossible) . "\n" . Dumper($needsconfirmation);
111                 $self->ok(0);
112                 return $self;
113         }
114     # Fill any reserves the patron had on the item.  
115     # TODO: this logic should be pulled internal to AddIssue for all Koha. 
116     $debug and warn "pending_queue: " . (@$pending) ? Dumper($pending) : '[]';
117     foreach (grep {$_->{borrowernumber} eq $self->{patron}->{borrowernumber}} @$pending) {
118         $debug and warn "Filling reserve (borrowernumber,biblionumber,reservedate): "
119             . sprintf("(%s,%s,%s)\n",$_->{borrowernumber},$_->{biblionumber},$_->{reservedate});
120         ModReserveFill($_);
121         # TODO: adjust representation in $self->item
122     }
123         # can issue
124         $debug and warn "do_checkout: calling AddIssue(\$borrower,$barcode, $overridden_duedate, 0)\n"
125                 # . "w/ \$borrower: " . Dumper($borrower)
126                 . "w/ C4::Context->userenv: " . Dumper(C4::Context->userenv);
127         my $due_dt  = AddIssue($borrower, $barcode, $overridden_duedate, 0);
128     if ($due_dt) {
129         $self->{due} = $due_dt->clone();
130     } else {
131         $self->{due} = undef;
132     }
133
134     #$self->{item}->due_date($due);
135         $self->ok(1);
136         return $self;
137 }
138
139 1;
140 __END__