Bug 10550: Fix database typo wthdrawn
[koha-equinox.git] / C4 / SIP / ILS / Transaction / Checkin.pm
1 #
2 # An object to handle checkin status
3 #
4
5 package ILS::Transaction::Checkin;
6
7 use warnings;
8 use strict;
9
10 # use POSIX qw(strftime);
11
12 use ILS;
13 use ILS::Transaction;
14
15 use C4::Circulation;
16 use C4::Reserves qw( ModReserveAffect );
17 use C4::Items qw( ModItemTransfer );
18 use C4::Debug;
19
20 use parent qw(ILS::Transaction);
21
22 my %fields = (
23     magnetic => 0,
24     sort_bin => undef,
25     collection_code  => undef,
26     # 3M extensions:
27     call_number      => undef,
28     destination_loc  => undef,
29     alert_type       => undef,  # 00,01,02,03,04 or 99
30     hold_patron_id   => undef,
31     hold_patron_name => "",
32     hold             => undef,
33 );
34
35 sub new {
36     my $class = shift;
37     my $self = $class->SUPER::new();                # start with an ILS::Transaction object
38
39     foreach (keys %fields) {
40         $self->{_permitted}->{$_} = $fields{$_};    # overlaying _permitted
41     }
42
43     @{$self}{keys %fields} = values %fields;        # copying defaults into object
44     return bless $self, $class;
45 }
46
47 sub do_checkin {
48     my $self = shift;
49     my $branch = shift;
50     if (!$branch) {
51         $branch = 'SIP2';
52     }
53     my $barcode = $self->{item}->id;
54     $debug and warn "do_checkin() calling AddReturn($barcode, $branch)";
55     my ($return, $messages, $iteminformation, $borrower) = AddReturn($barcode, $branch);
56     $self->alert(!$return);
57     # ignoring messages: NotIssued, IsPermanent, WasLost, WasTransfered
58
59     # biblionumber, biblioitemnumber, itemnumber
60     # borrowernumber, reservedate, branchcode
61     # cancellationdate, found, reservenotes, priority, timestamp
62
63     if ($messages->{BadBarcode}) {
64         $self->alert_type('99');
65     }
66     if ($messages->{withdrawn}) {
67         $self->alert_type('99');
68     }
69     if ($messages->{Wrongbranch}) {
70         $self->destination_loc($messages->{Wrongbranch}->{Rightbranch});
71         $self->alert_type('04');            # send to other branch
72     }
73     if ($messages->{WrongTransfer}) {
74         $self->destination_loc($messages->{WrongTransfer});
75         $self->alert_type('04');            # send to other branch
76     }
77     if ($messages->{NeedsTransfer}) {
78         $self->destination_loc($iteminformation->{homebranch});
79         $self->alert_type('04');            # send to other branch
80     }
81     if ($messages->{ResFound}) {
82         $self->hold($messages->{ResFound});
83         if ($branch eq $messages->{ResFound}->{branchcode}) {
84             $self->alert_type('01');
85             ModReserveAffect( $messages->{ResFound}->{itemnumber},
86                 $messages->{ResFound}->{borrowernumber}, 0);
87
88         } else {
89             $self->alert_type('02');
90             ModReserveAffect( $messages->{ResFound}->{itemnumber},
91                 $messages->{ResFound}->{borrowernumber}, 1);
92             ModItemTransfer( $messages->{ResFound}->{itemnumber},
93                 $branch,
94                 $messages->{ResFound}->{branchcode}
95             );
96
97         }
98         $self->{item}->hold_patron_id( $messages->{ResFound}->{borrowernumber} );
99         $self->{item}->destination_loc( $messages->{ResFound}->{branchcode} );
100     }
101     $self->alert(1) if defined $self->alert_type;  # alert_type could be "00", hypothetically
102     $self->ok($return);
103 }
104
105 sub resensitize {
106         my $self = shift;
107         unless ($self->{item}) {
108                 warn "resensitize(): no item found in object to resensitize";
109                 return;
110         }
111         return !$self->{item}->magnetic_media;
112 }
113
114 sub patron_id {
115         my $self = shift;
116         unless ($self->{patron}) {
117                 warn "patron_id(): no patron found in object";
118                 return;
119         }
120         return $self->{patron}->id;
121 }
122
123 1;