Fix for Bug 4302, shouldn't have to scroll left to right on z search results
[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;
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
24 use vars qw($VERSION @ISA $debug);
25
26 BEGIN {
27         $VERSION = 1.03;
28         @ISA = qw(ILS::Transaction);
29 }
30
31 # Most fields are handled by the Transaction superclass
32 my %fields = (
33               security_inhibit => 0,
34               due              => undef,
35               renew_ok         => 0,
36         );
37
38 sub new {
39     my $class = shift;;
40     my $self = $class->SUPER::new();
41     my $element;
42     foreach $element (keys %fields) {
43                 $self->{_permitted}->{$element} = $fields{$element};
44     }
45     @{$self}{keys %fields} = values %fields;
46 #    $self->{'due'} = time() + (60*60*24*14); # two weeks hence
47         $debug and warn "new ILS::Transaction::Checkout : " . Dumper $self;
48     return bless $self, $class;
49 }
50
51 sub do_checkout {
52         my $self = shift;
53         syslog('LOG_DEBUG', "ILS::Transaction::Checkout performing checkout...");
54         my $pending        = $self->{item}->pending_queue;
55         my $shelf          = $self->{item}->hold_shelf;
56         my $barcode        = $self->{item}->id;
57         my $patron_barcode = $self->{patron}->id;
58         $debug and warn "do_checkout: patron (" . $patron_barcode . ")";
59         my $borrower = $self->{patron}->getmemberdetails_object();
60         $debug and warn "do_checkout borrower: . " . Dumper $borrower;
61         my ($issuingimpossible,$needsconfirmation) = CanBookBeIssued( $borrower, $barcode );
62         my $noerror=1;
63     if (scalar keys %$issuingimpossible) {
64         foreach (keys %$issuingimpossible) {
65             # do something here so we pass these errors
66             $self->screen_msg($_ . ': ' . $issuingimpossible->{$_});
67             $noerror = 0;
68         }
69     } else {
70         foreach my $confirmation (keys %$needsconfirmation) {
71             if ($confirmation eq 'RENEW_ISSUE'){
72                 $self->screen_msg("Item already checked out to you: renewing item.");
73             } elsif ($confirmation eq 'RESERVED' or $confirmation eq 'RESERVE_WAITING') {
74                 my $x = $self->{item}->available($patron_barcode);
75                 if ($x) {
76                     $self->screen_msg("Item was reserved for you.");
77                 } else {
78                     $self->screen_msg("Item is reserved for another patron upon return.");
79                     # $noerror = 0;
80                 }
81             } elsif ($confirmation eq 'ISSUED_TO_ANOTHER') {
82                 $self->screen_msg("Item already checked out to another patron.  Please return item for check-in.");
83                 $noerror = 0;
84             } elsif ($confirmation eq 'DEBT') {     # don't do anything, it's the minor debt, and alarms fire elsewhere
85             } else {
86                 $self->screen_msg($needsconfirmation->{$confirmation});
87                 $noerror = 0;
88             }
89         }
90     }
91     my $itemnumber = $self->{item}->{itemnumber};
92     foreach (@$shelf) {
93         $debug and warn "shelf has ($_->{itemnumber} for $_->{borrowernumber}). this is ($itemnumber, $self->{patron}->{borrowernumber})";
94         ($_->{itemnumber} eq $itemnumber) or next;    # skip it if not this item
95         ($_->{borrowernumber} == $self->{patron}->{borrowernumber}) and last;
96             # if item was waiting for this patron, we're done.  AddIssue takes care of the "W" hold.
97         $debug and warn "Item is on hold shelf for another patron.";
98         $self->screen_msg("Item is on hold shelf for another patron.");
99         $noerror = 0;
100     }
101         unless ($noerror) {
102                 $debug and warn "cannot issue: " . Dumper($issuingimpossible) . "\n" . Dumper($needsconfirmation);
103                 $self->ok(0);
104                 return $self;
105         }
106     # Fill any reserves the patron had on the item.  
107     # TODO: this logic should be pulled internal to AddIssue for all Koha. 
108     $debug and warn "pending_queue: " . (@$pending) ? Dumper($pending) : '[]';
109     foreach (grep {$_->{borrowernumber} eq $self->{patron}->{borrowernumber}} @$pending) {
110         $debug and warn "Filling reserve (borrowernumber,biblionumber,reservedate): "
111             . sprintf("(%s,%s,%s)\n",$_->{borrowernumber},$_->{biblionumber},$_->{reservedate});
112         ModReserveFill($_);
113         # TODO: adjust representation in $self->item
114     }
115         # can issue
116         $debug and warn "do_checkout: calling AddIssue(\$borrower,$barcode, undef, 0)\n"
117                 # . "w/ \$borrower: " . Dumper($borrower)
118                 . "w/ C4::Context->userenv: " . Dumper(C4::Context->userenv);
119         my $c4due  = AddIssue($borrower, $barcode, undef, 0);
120         my $due  = $c4due->output('iso') || undef;
121         $debug and warn "Item due: $due";
122         $self->{'due'} = $due;
123         $self->{item}->due_date($due);
124         $self->ok(1);
125         return $self;
126 }
127
128 1;
129 __END__