f4f7ff8e0a5a1988befe35ae9109d68e372b0df0
[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::Debug;
22
23 use vars qw($VERSION @ISA $debug);
24
25 BEGIN {
26         $VERSION = 1.03;
27         @ISA = qw(ILS::Transaction);
28 }
29
30 # Most fields are handled by the Transaction superclass
31 my %fields = (
32               security_inhibit => 0,
33               due              => undef,
34               renew_ok         => 0,
35         );
36
37 sub new {
38     my $class = shift;;
39     my $self = $class->SUPER::new();
40     my $element;
41     foreach $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 $barcode        = $self->{item}->id;
54         my $patron_barcode = $self->{patron}->id;
55         $debug and warn "do_checkout: patron (" . $patron_barcode . ")";
56         # my $borrower = GetMember( $patron_barcode, 'cardnumber' );
57         # my $borrower = $self->{patron};
58         # my $borrower = GetMemberDetails(undef, $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         foreach ( keys %$issuingimpossible ) {
64                 # do something here so we pass these errors
65                 $self->screen_msg($_ . ': ' . $issuingimpossible->{$_});
66                 $noerror = 0;
67         }
68         foreach my $confirmation ( keys %$needsconfirmation ) {
69                 if ($confirmation eq 'RENEW_ISSUE'){
70                         $self->screen_msg("Item already checked out to you: renewing item.");
71                 } elsif ($confirmation eq 'RESERVED' or $confirmation eq 'RESERVE_WAITING') {
72             my $x = $self->{item}->available($patron_barcode);
73             if ($x) {
74                 $self->screen_msg("Item was reserved for you.");
75             } else {
76                 $self->screen_msg("Item is reserved for another patron.");
77                 $noerror = 0;
78             }
79                 } elsif ($confirmation eq 'ISSUED_TO_ANOTHER') {
80             $self->screen_msg("Item already checked out to another patron.  Please return item for check-in.");
81                         $noerror = 0;
82                 } elsif ($confirmation eq 'DEBT') {     # don't do anything, it's the minor debt, and alarms fire elsewhere
83         } else {
84                         $self->screen_msg($needsconfirmation->{$confirmation});
85                         $noerror = 0;
86                 }
87         }
88         unless ($noerror) {
89                 warn "cannot issue: " . Dumper($issuingimpossible) . "\n" . Dumper($needsconfirmation);
90                 $self->ok(0);
91                 return $self;
92         }
93         # can issue
94         $debug and warn "do_checkout: calling AddIssue(\$borrower,$barcode, undef, 0)\n"
95                 # . "w/ \$borrower: " . Dumper($borrower)
96                 . "w/ C4::Context->userenv: " . Dumper(C4::Context->userenv);
97         my $c4due  = AddIssue( $borrower, $barcode, undef, 0 );
98         my $due  = $c4due->output('iso') || undef;
99         $debug and warn "Item due: $due";
100         $self->{'due'} = $due;
101         $self->{item}->due_date($due);
102         $self->ok(1);
103         return $self;
104 }
105
106 1;
107 __END__