Bug 8769: (follow-up) remove unnecessary module import
[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     my $return_date = shift;
51     if (!$branch) {
52         $branch = 'SIP2';
53     }
54     my $barcode = $self->{item}->id;
55
56     $return_date =   substr( $return_date, 0, 4 )
57                    . '-'
58                    . substr( $return_date, 4, 2 )
59                    . '-'
60                    . substr( $return_date, 6, 2 )
61                    . q{ }
62                    . substr( $return_date, 12, 2 )
63                    . ':'
64                    . substr( $return_date, 14, 2 )
65                    . ':'
66                    . substr( $return_date, 16, 2 );
67
68     $debug and warn "do_checkin() calling AddReturn($barcode, $branch)";
69     my ($return, $messages, $iteminformation, $borrower) = AddReturn($barcode, $branch, undef, undef, $return_date);
70     $self->alert(!$return);
71     # ignoring messages: NotIssued, IsPermanent, WasLost, WasTransfered
72
73     # biblionumber, biblioitemnumber, itemnumber
74     # borrowernumber, reservedate, branchcode
75     # cancellationdate, found, reservenotes, priority, timestamp
76
77     if ($messages->{BadBarcode}) {
78         $self->alert_type('99');
79     }
80     if ($messages->{withdrawn}) {
81         $self->alert_type('99');
82     }
83     if ($messages->{Wrongbranch}) {
84         $self->destination_loc($messages->{Wrongbranch}->{Rightbranch});
85         $self->alert_type('04');            # send to other branch
86     }
87     if ($messages->{WrongTransfer}) {
88         $self->destination_loc($messages->{WrongTransfer});
89         $self->alert_type('04');            # send to other branch
90     }
91     if ($messages->{NeedsTransfer}) {
92         $self->destination_loc($iteminformation->{homebranch});
93         $self->alert_type('04');            # send to other branch
94     }
95     if ($messages->{ResFound}) {
96         $self->hold($messages->{ResFound});
97         if ($branch eq $messages->{ResFound}->{branchcode}) {
98             $self->alert_type('01');
99             ModReserveAffect( $messages->{ResFound}->{itemnumber},
100                 $messages->{ResFound}->{borrowernumber}, 0);
101
102         } else {
103             $self->alert_type('02');
104             ModReserveAffect( $messages->{ResFound}->{itemnumber},
105                 $messages->{ResFound}->{borrowernumber}, 1);
106             ModItemTransfer( $messages->{ResFound}->{itemnumber},
107                 $branch,
108                 $messages->{ResFound}->{branchcode}
109             );
110
111         }
112         $self->{item}->hold_patron_id( $messages->{ResFound}->{borrowernumber} );
113         $self->{item}->destination_loc( $messages->{ResFound}->{branchcode} );
114     }
115     $self->alert(1) if defined $self->alert_type;  # alert_type could be "00", hypothetically
116     $self->ok($return);
117 }
118
119 sub resensitize {
120         my $self = shift;
121         unless ($self->{item}) {
122                 warn "resensitize(): no item found in object to resensitize";
123                 return;
124         }
125         return !$self->{item}->magnetic_media;
126 }
127
128 sub patron_id {
129         my $self = shift;
130         unless ($self->{patron}) {
131                 warn "patron_id(): no patron found in object";
132                 return;
133         }
134         return $self->{patron}->id;
135 }
136
137 1;