Bug 11944: use CGI( -utf8 ) everywhere
[koha.git] / C4 / SIP / ILS / Transaction / Checkout.pm
1 #
2 # An object to handle checkout status
3 #
4
5 package 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 ILS;
16 use ILS::Transaction;
17
18 use C4::Context;
19 use C4::Circulation;
20 use C4::Members;
21 use C4::Reserves qw(ModReserveFill);
22 use C4::Debug;
23 use parent qw(ILS::Transaction);
24
25 our $debug;
26
27 our $VERSION = 3.07.00.049;
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         syslog('LOG_DEBUG', "ILS::Transaction::Checkout performing checkout...");
51         my $pending        = $self->{item}->pending_queue;
52         my $shelf          = $self->{item}->hold_shelf;
53         my $barcode        = $self->{item}->id;
54         my $patron_barcode = $self->{patron}->id;
55         my $overridden_duedate; # usually passed as undef to AddIssue
56         $debug and warn "do_checkout: patron (" . $patron_barcode . ")";
57         my $borrower = $self->{patron}->getmemberdetails_object();
58         $debug and warn "do_checkout borrower: . " . Dumper $borrower;
59         my ($issuingimpossible,$needsconfirmation) = CanBookBeIssued(
60         $borrower,
61         $barcode,
62         undef,
63         0,
64         C4::Context->preference("AllowItemsOnHoldCheckout")
65     );
66         my $noerror=1;
67     if (scalar keys %$issuingimpossible) {
68         foreach (keys %$issuingimpossible) {
69             # do something here so we pass these errors
70             $self->screen_msg($_ . ': ' . $issuingimpossible->{$_});
71             $noerror = 0;
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' or $confirmation eq 'RESERVE_WAITING') {
78                 my $x = $self->{item}->available($patron_barcode);
79                 if ($x) {
80                     $self->screen_msg("Item was reserved for you.");
81                 } else {
82                     $self->screen_msg("Item is reserved for another patron upon return.");
83                     # $noerror = 0;
84                 }
85             } elsif ($confirmation eq 'ISSUED_TO_ANOTHER') {
86                 $self->screen_msg("Item already checked out to another patron.  Please return item for check-in.");
87                 $noerror = 0;
88             } elsif ($confirmation eq 'DEBT') {
89                 $self->screen_msg('Outstanding Fines block issue');
90                 $noerror = 0;
91             } elsif ($confirmation eq 'HIGHHOLDS') {
92                 $overridden_duedate = $needsconfirmation->{$confirmation}->{returndate};
93                 $self->screen_msg('Loan period reduced for high-demand item');
94             } else {
95                 $self->screen_msg($needsconfirmation->{$confirmation});
96                 $noerror = 0;
97             }
98         }
99     }
100     my $itemnumber = $self->{item}->{itemnumber};
101     foreach (@$shelf) {
102         $debug and warn "shelf has ($_->{itemnumber} for $_->{borrowernumber}). this is ($itemnumber, $self->{patron}->{borrowernumber})";
103         ($_->{itemnumber} eq $itemnumber) or next;    # skip it if not this item
104         ($_->{borrowernumber} == $self->{patron}->{borrowernumber}) and last;
105             # if item was waiting for this patron, we're done.  AddIssue takes care of the "W" hold.
106         $debug and warn "Item is on hold shelf for another patron.";
107         $self->screen_msg("Item is on hold shelf for another patron.");
108         $noerror = 0;
109     }
110         unless ($noerror) {
111                 $debug and warn "cannot issue: " . Dumper($issuingimpossible) . "\n" . Dumper($needsconfirmation);
112                 $self->ok(0);
113                 return $self;
114         }
115     # Fill any reserves the patron had on the item.  
116     # TODO: this logic should be pulled internal to AddIssue for all Koha. 
117     $debug and warn "pending_queue: " . (@$pending) ? Dumper($pending) : '[]';
118     foreach (grep {$_->{borrowernumber} eq $self->{patron}->{borrowernumber}} @$pending) {
119         $debug and warn "Filling reserve (borrowernumber,biblionumber,reservedate): "
120             . sprintf("(%s,%s,%s)\n",$_->{borrowernumber},$_->{biblionumber},$_->{reservedate});
121         ModReserveFill($_);
122         # TODO: adjust representation in $self->item
123     }
124         # can issue
125         $debug and warn "do_checkout: calling AddIssue(\$borrower,$barcode, $overridden_duedate, 0)\n"
126                 # . "w/ \$borrower: " . Dumper($borrower)
127                 . "w/ C4::Context->userenv: " . Dumper(C4::Context->userenv);
128         my $due_dt  = AddIssue($borrower, $barcode, $overridden_duedate, 0);
129     if ($due_dt) {
130         $self->{due} = $due_dt->clone();
131     } else {
132         $self->{due} = undef;
133     }
134
135     #$self->{item}->due_date($due);
136         $self->ok(1);
137         return $self;
138 }
139
140 1;
141 __END__