Bug 16011: $VERSION - Remove the $VERSION init
[koha.git] / C4 / SIP / ILS / Transaction / Checkout.pm
blob14a2b66c680f115d13305143d6fc2056f0b0c92f
2 # An object to handle checkout status
5 package C4::SIP::ILS::Transaction::Checkout;
7 use warnings;
8 use strict;
10 use POSIX qw(strftime);
11 use Sys::Syslog qw(syslog);
12 use Data::Dumper;
13 use CGI qw ( -utf8 );
15 use C4::SIP::ILS::Transaction;
17 use C4::Context;
18 use C4::Circulation;
19 use C4::Members;
20 use C4::Reserves qw(ModReserveFill);
21 use C4::Debug;
22 use Koha::DateUtils;
24 use parent qw(C4::SIP::ILS::Transaction);
26 our $debug;
29 # Most fields are handled by the Transaction superclass
30 my %fields = (
31 security_inhibit => 0,
32 due => undef,
33 renew_ok => 0,
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};
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;
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,
64 C4::Context->preference("AllowItemsOnHoldCheckout")
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;
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;
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 } elsif ($confirmation eq 'RENTALCHARGE') {
95 if ($self->{fee_ack} ne 'Y') {
96 $noerror = 0;
98 } else {
99 $self->screen_msg($needsconfirmation->{$confirmation});
100 $noerror = 0;
101 syslog('LOG_DEBUG', "Blocking checkout Reason:$confirmation");
105 my $itemnumber = $self->{item}->{itemnumber};
106 foreach (@$shelf) {
107 $debug and warn "shelf has ($_->{itemnumber} for $_->{borrowernumber}). this is ($itemnumber, $self->{patron}->{borrowernumber})";
108 ($_->{itemnumber} eq $itemnumber) or next; # skip it if not this item
109 ($_->{borrowernumber} == $self->{patron}->{borrowernumber}) and last;
110 # if item was waiting for this patron, we're done. AddIssue takes care of the "W" hold.
111 $debug and warn "Item is on hold shelf for another patron.";
112 $self->screen_msg("Item is on hold shelf for another patron.");
113 $noerror = 0;
115 my ($fee, undef) = GetIssuingCharges($itemnumber, $self->{patron}->{borrowernumber});
116 if ( $fee > 0 ) {
117 $self->{sip_fee_type} = '06';
118 $self->{fee_amount} = sprintf '%.2f', $fee;
119 if ($self->{fee_ack} eq 'N' ) {
120 $noerror = 0;
123 unless ($noerror) {
124 $debug and warn "cannot issue: " . Dumper($issuingimpossible) . "\n" . Dumper($needsconfirmation);
125 $self->ok(0);
126 return $self;
128 # Fill any reserves the patron had on the item.
129 # TODO: this logic should be pulled internal to AddIssue for all Koha.
130 $debug and warn "pending_queue: " . (@$pending) ? Dumper($pending) : '[]';
131 foreach (grep {$_->{borrowernumber} eq $self->{patron}->{borrowernumber}} @$pending) {
132 $debug and warn "Filling reserve (borrowernumber,biblionumber,reservedate): "
133 . sprintf("(%s,%s,%s)\n",$_->{borrowernumber},$_->{biblionumber},$_->{reservedate});
134 ModReserveFill($_);
135 # TODO: adjust representation in $self->item
137 # can issue
138 $debug and warn "do_checkout: calling AddIssue(\$borrower,$barcode, $overridden_duedate, 0)\n"
139 # . "w/ \$borrower: " . Dumper($borrower)
140 . "w/ C4::Context->userenv: " . Dumper(C4::Context->userenv);
141 my $issue = AddIssue( $borrower, $barcode, $overridden_duedate, 0 );
142 $self->{due} = $self->duedatefromissue($issue, $itemnumber);
144 $self->ok(1);
145 return $self;
149 __END__