Bug 13315 - Add feedback for last item checked out to circulation.pl
[koha.git] / C4 / SIP / ILS / Transaction / Checkout.pm
blobd602fa6bf67ddd507e06ebfb9b879612902394b7
2 # An object to handle checkout status
5 package 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;
15 use ILS;
16 use ILS::Transaction;
18 use C4::Context;
19 use C4::Circulation;
20 use C4::Members;
21 use C4::Reserves qw(ModReserveFill);
22 use C4::Debug;
23 use Koha::DateUtils;
25 use parent qw(C4::SIP::ILS::Transaction);
27 our $debug;
29 our $VERSION = 3.07.00.049;
31 # Most fields are handled by the Transaction superclass
32 my %fields = (
33 security_inhibit => 0,
34 due => undef,
35 renew_ok => 0,
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};
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;
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 my $overridden_duedate; # usually passed as undef to AddIssue
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(
62 $borrower,
63 $barcode,
64 undef,
66 C4::Context->preference("AllowItemsOnHoldCheckout")
68 my $noerror=1;
69 if (scalar keys %$issuingimpossible) {
70 foreach (keys %$issuingimpossible) {
71 # do something here so we pass these errors
72 $self->screen_msg($_ . ': ' . $issuingimpossible->{$_});
73 $noerror = 0;
75 } else {
76 foreach my $confirmation (keys %{$needsconfirmation}) {
77 if ($confirmation eq 'RENEW_ISSUE'){
78 $self->screen_msg("Item already checked out to you: renewing item.");
79 } elsif ($confirmation eq 'RESERVED' or $confirmation eq 'RESERVE_WAITING') {
80 my $x = $self->{item}->available($patron_barcode);
81 if ($x) {
82 $self->screen_msg("Item was reserved for you.");
83 } else {
84 $self->screen_msg("Item is reserved for another patron upon return.");
85 # $noerror = 0;
87 } elsif ($confirmation eq 'ISSUED_TO_ANOTHER') {
88 $self->screen_msg("Item already checked out to another patron. Please return item for check-in.");
89 $noerror = 0;
90 } elsif ($confirmation eq 'DEBT') {
91 $self->screen_msg('Outstanding Fines block issue');
92 $noerror = 0;
93 } elsif ($confirmation eq 'HIGHHOLDS') {
94 $overridden_duedate = $needsconfirmation->{$confirmation}->{returndate};
95 $self->screen_msg('Loan period reduced for high-demand item');
96 } elsif ($confirmation eq 'RENTALCHARGE') {
97 if ($self->{fee_ack} ne 'Y') {
98 $noerror = 0;
100 } else {
101 $self->screen_msg($needsconfirmation->{$confirmation});
102 $noerror = 0;
103 syslog('LOG_DEBUG', "Blocking checkout Reason:$confirmation");
107 my $itemnumber = $self->{item}->{itemnumber};
108 foreach (@$shelf) {
109 $debug and warn "shelf has ($_->{itemnumber} for $_->{borrowernumber}). this is ($itemnumber, $self->{patron}->{borrowernumber})";
110 ($_->{itemnumber} eq $itemnumber) or next; # skip it if not this item
111 ($_->{borrowernumber} == $self->{patron}->{borrowernumber}) and last;
112 # if item was waiting for this patron, we're done. AddIssue takes care of the "W" hold.
113 $debug and warn "Item is on hold shelf for another patron.";
114 $self->screen_msg("Item is on hold shelf for another patron.");
115 $noerror = 0;
117 my ($fee, undef) = GetIssuingCharges($itemnumber, $self->{patron}->{borrowernumber});
118 if ( $fee > 0 ) {
119 $self->{sip_fee_type} = '06';
120 $self->{fee_amount} = sprintf '%.2f', $fee;
121 if ($self->{fee_ack} eq 'N' ) {
122 $noerror = 0;
125 unless ($noerror) {
126 $debug and warn "cannot issue: " . Dumper($issuingimpossible) . "\n" . Dumper($needsconfirmation);
127 $self->ok(0);
128 return $self;
130 # Fill any reserves the patron had on the item.
131 # TODO: this logic should be pulled internal to AddIssue for all Koha.
132 $debug and warn "pending_queue: " . (@$pending) ? Dumper($pending) : '[]';
133 foreach (grep {$_->{borrowernumber} eq $self->{patron}->{borrowernumber}} @$pending) {
134 $debug and warn "Filling reserve (borrowernumber,biblionumber,reservedate): "
135 . sprintf("(%s,%s,%s)\n",$_->{borrowernumber},$_->{biblionumber},$_->{reservedate});
136 ModReserveFill($_);
137 # TODO: adjust representation in $self->item
139 # can issue
140 $debug and warn "do_checkout: calling AddIssue(\$borrower,$barcode, $overridden_duedate, 0)\n"
141 # . "w/ \$borrower: " . Dumper($borrower)
142 . "w/ C4::Context->userenv: " . Dumper(C4::Context->userenv);
143 my $issue = AddIssue( $borrower, $barcode, $overridden_duedate, 0 );
144 my $due_dt = dt_from_string( $issue->date_due() );
145 if ($due_dt) {
146 $self->{due} = $due_dt->clone();
147 } else {
148 $self->{due} = undef;
151 #$self->{item}->due_date($due);
152 $self->ok(1);
153 return $self;
157 __END__