Bug 24566: UpdateItemLocationOnCheckin triggers SIP2 alert flag, even with checked_in...
[koha-equinox.git] / C4 / SIP / ILS / Transaction / Checkin.pm
1 #
2 # An object to handle checkin status
3 #
4
5 package C4::SIP::ILS::Transaction::Checkin;
6
7 use warnings;
8 use strict;
9
10 # use POSIX qw(strftime);
11
12 use C4::SIP::ILS::Transaction;
13
14 use C4::Circulation;
15 use C4::Debug;
16 use C4::Items qw( ModItemTransfer );
17 use C4::Reserves qw( ModReserveAffect );
18 use Koha::DateUtils qw( dt_from_string );
19
20 use parent qw(C4::SIP::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     my $cv_triggers_alert = shift;
52     my $checked_in_ok = shift;
53
54     if (!$branch) {
55         $branch = 'SIP2';
56     }
57     my $barcode = $self->{item}->id;
58
59     $return_date =   substr( $return_date, 0, 4 )
60                    . '-'
61                    . substr( $return_date, 4, 2 )
62                    . '-'
63                    . substr( $return_date, 6, 2 )
64                    . q{ }
65                    . substr( $return_date, 12, 2 )
66                    . ':'
67                    . substr( $return_date, 14, 2 )
68                    . ':'
69                    . substr( $return_date, 16, 2 );
70
71     $debug and warn "do_checkin() calling AddReturn($barcode, $branch)";
72     my ($return, $messages, $issue, $borrower) = AddReturn($barcode, $branch, undef, dt_from_string($return_date));
73
74     if ( $checked_in_ok ) {
75         delete $messages->{ItemLocationUpdated};
76         delete $messages->{NotIssued};
77         delete $messages->{LocalUse};
78         $return = 1 unless keys %$messages;
79     }
80
81     # biblionumber, biblioitemnumber, itemnumber
82     # borrowernumber, reservedate, branchcode
83     # cancellationdate, found, reservenotes, priority, timestamp
84     if( $messages->{DataCorrupted} ) {
85         $self->alert_type('98');
86     }
87     if ($messages->{BadBarcode}) {
88         $self->alert_type('99');
89     }
90     if ($messages->{withdrawn}) {
91         $self->alert_type('99');
92     }
93     if ($messages->{WasLost}) {
94         $self->alert_type('99') if C4::Context->preference("BlockReturnOfLostItems");
95     }
96     if ($messages->{Wrongbranch}) {
97         $self->{item}->destination_loc($messages->{Wrongbranch}->{Rightbranch});
98         $self->alert_type('04');            # send to other branch
99     }
100     if ($messages->{WrongTransfer}) {
101         $self->{item}->destination_loc($messages->{WrongTransfer});
102         $self->alert_type('04');            # send to other branch
103     }
104     if ($messages->{NeedsTransfer}) {
105         $self->{item}->destination_loc($messages->{NeedsTransfer});
106         $self->alert_type('04');            # send to other branch
107     }
108     if ($messages->{WasTransfered}) { # set into transit so tell unit
109         $self->{item}->destination_loc($issue->item->homebranch);
110         $self->alert_type('04');            # send to other branch
111     }
112     if ($messages->{ResFound}) {
113         $self->hold($messages->{ResFound});
114         if ($branch eq $messages->{ResFound}->{branchcode}) {
115             $self->alert_type('01');
116             ModReserveAffect( $messages->{ResFound}->{itemnumber},
117                 $messages->{ResFound}->{borrowernumber}, 0, $messages->{ResFound}->{reserve_id});
118
119         } else {
120             $self->alert_type('02');
121             ModReserveAffect( $messages->{ResFound}->{itemnumber},
122                 $messages->{ResFound}->{borrowernumber}, 1, $messages->{ResFound}->{reserve_id});
123             ModItemTransfer( $messages->{ResFound}->{itemnumber},
124                 $branch,
125                 $messages->{ResFound}->{branchcode},
126                 $messages->{TransferTrigger},
127             );
128
129         }
130         $self->{item}->hold_patron_id( $messages->{ResFound}->{borrowernumber} );
131         $self->{item}->destination_loc( $messages->{ResFound}->{branchcode} );
132     }
133     # ignoring messages: NotIssued, WasTransfered
134
135     if ($cv_triggers_alert) {
136         $self->alert( defined $self->alert_type ); # Overwrites existing alert value, should set to 0 if there is no alert type
137     }
138     else {
139         $self->alert( !$return || defined $self->alert_type );
140     }
141
142     $self->ok($return);
143
144     return { messages => $messages };
145 }
146
147 sub resensitize {
148         my $self = shift;
149         unless ($self->{item}) {
150                 warn "resensitize(): no item found in object to resensitize";
151                 return;
152         }
153         return !$self->{item}->magnetic_media;
154 }
155
156 sub patron_id {
157         my $self = shift;
158         unless ($self->{patron}) {
159                 warn "patron_id(): no patron found in object";
160                 return;
161         }
162         return $self->{patron}->id;
163 }
164
165 1;