Bug 8769: Allow SIP2 return backdating
[koha.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 use Koha::DateUtils;
20
21 use parent qw(ILS::Transaction);
22
23 my %fields = (
24     magnetic => 0,
25     sort_bin => undef,
26     collection_code  => undef,
27     # 3M extensions:
28     call_number      => undef,
29     destination_loc  => undef,
30     alert_type       => undef,  # 00,01,02,03,04 or 99
31     hold_patron_id   => undef,
32     hold_patron_name => "",
33     hold             => undef,
34 );
35
36 sub new {
37     my $class = shift;
38     my $self = $class->SUPER::new();                # start with an ILS::Transaction object
39
40     foreach (keys %fields) {
41         $self->{_permitted}->{$_} = $fields{$_};    # overlaying _permitted
42     }
43
44     @{$self}{keys %fields} = values %fields;        # copying defaults into object
45     return bless $self, $class;
46 }
47
48 sub do_checkin {
49     my $self = shift;
50     my $branch = shift;
51     my $return_date = shift;
52     if (!$branch) {
53         $branch = 'SIP2';
54     }
55     my $barcode = $self->{item}->id;
56
57     $return_date =   substr( $return_date, 0, 4 )
58                    . '-'
59                    . substr( $return_date, 4, 2 )
60                    . '-'
61                    . substr( $return_date, 6, 2 )
62                    . q{ }
63                    . substr( $return_date, 12, 2 )
64                    . ':'
65                    . substr( $return_date, 14, 2 )
66                    . ':'
67                    . substr( $return_date, 16, 2 );
68
69     $debug and warn "do_checkin() calling AddReturn($barcode, $branch)";
70     my ($return, $messages, $iteminformation, $borrower) = AddReturn($barcode, $branch, undef, undef, $return_date);
71     $self->alert(!$return);
72     # ignoring messages: NotIssued, IsPermanent, WasLost, WasTransfered
73
74     # biblionumber, biblioitemnumber, itemnumber
75     # borrowernumber, reservedate, branchcode
76     # cancellationdate, found, reservenotes, priority, timestamp
77
78     if ($messages->{BadBarcode}) {
79         $self->alert_type('99');
80     }
81     if ($messages->{withdrawn}) {
82         $self->alert_type('99');
83     }
84     if ($messages->{Wrongbranch}) {
85         $self->destination_loc($messages->{Wrongbranch}->{Rightbranch});
86         $self->alert_type('04');            # send to other branch
87     }
88     if ($messages->{WrongTransfer}) {
89         $self->destination_loc($messages->{WrongTransfer});
90         $self->alert_type('04');            # send to other branch
91     }
92     if ($messages->{NeedsTransfer}) {
93         $self->destination_loc($iteminformation->{homebranch});
94         $self->alert_type('04');            # send to other branch
95     }
96     if ($messages->{ResFound}) {
97         $self->hold($messages->{ResFound});
98         if ($branch eq $messages->{ResFound}->{branchcode}) {
99             $self->alert_type('01');
100             ModReserveAffect( $messages->{ResFound}->{itemnumber},
101                 $messages->{ResFound}->{borrowernumber}, 0);
102
103         } else {
104             $self->alert_type('02');
105             ModReserveAffect( $messages->{ResFound}->{itemnumber},
106                 $messages->{ResFound}->{borrowernumber}, 1);
107             ModItemTransfer( $messages->{ResFound}->{itemnumber},
108                 $branch,
109                 $messages->{ResFound}->{branchcode}
110             );
111
112         }
113         $self->{item}->hold_patron_id( $messages->{ResFound}->{borrowernumber} );
114         $self->{item}->destination_loc( $messages->{ResFound}->{branchcode} );
115     }
116     $self->alert(1) if defined $self->alert_type;  # alert_type could be "00", hypothetically
117     $self->ok($return);
118 }
119
120 sub resensitize {
121         my $self = shift;
122         unless ($self->{item}) {
123                 warn "resensitize(): no item found in object to resensitize";
124                 return;
125         }
126         return !$self->{item}->magnetic_media;
127 }
128
129 sub patron_id {
130         my $self = shift;
131         unless ($self->{patron}) {
132                 warn "patron_id(): no patron found in object";
133                 return;
134         }
135         return $self->{patron}->id;
136 }
137
138 1;