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