Bug 23057: Update do_checkin
[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->{NotIssued};
76         delete $messages->{LocalUse};
77         $return = 1 unless keys %$messages;
78     }
79
80     # biblionumber, biblioitemnumber, itemnumber
81     # borrowernumber, reservedate, branchcode
82     # cancellationdate, found, reservenotes, priority, timestamp
83     if( $messages->{DataCorrupted} ) {
84         $self->alert_type('98');
85     }
86     if ($messages->{BadBarcode}) {
87         $self->alert_type('99');
88     }
89     if ($messages->{withdrawn}) {
90         $self->alert_type('99');
91     }
92     if ($messages->{ReturnOfLostItemBlocked}) {
93         $self->alert_type('99');
94     }
95     if ($messages->{Wrongbranch}) {
96         $self->{item}->destination_loc($messages->{Wrongbranch}->{Rightbranch});
97         $self->alert_type('04');            # send to other branch
98     }
99     if ($messages->{WrongTransfer}) {
100         $self->{item}->destination_loc($messages->{WrongTransfer});
101         $self->alert_type('04');            # send to other branch
102     }
103     if ($messages->{NeedsTransfer}) {
104         $self->{item}->destination_loc($messages->{NeedsTransfer});
105         $self->alert_type('04');            # send to other branch
106     }
107     if ($messages->{WasTransfered}) { # set into transit so tell unit
108         $self->{item}->destination_loc($issue->item->homebranch);
109         $self->alert_type('04');            # send to other branch
110     }
111     if ($messages->{ResFound}) {
112         $self->hold($messages->{ResFound});
113         if ($branch eq $messages->{ResFound}->{branchcode}) {
114             $self->alert_type('01');
115             ModReserveAffect( $messages->{ResFound}->{itemnumber},
116                 $messages->{ResFound}->{borrowernumber}, 0, $messages->{ResFound}->{reserve_id});
117
118         } else {
119             $self->alert_type('02');
120             ModReserveAffect( $messages->{ResFound}->{itemnumber},
121                 $messages->{ResFound}->{borrowernumber}, 1, $messages->{ResFound}->{reserve_id});
122             ModItemTransfer( $messages->{ResFound}->{itemnumber},
123                 $branch,
124                 $messages->{ResFound}->{branchcode}
125             );
126
127         }
128         $self->{item}->hold_patron_id( $messages->{ResFound}->{borrowernumber} );
129         $self->{item}->destination_loc( $messages->{ResFound}->{branchcode} );
130     }
131     # ignoring messages: NotIssued, WasLost, WasTransfered
132
133     if ($cv_triggers_alert) {
134         $self->alert( defined $self->alert_type ); # Overwrites existing alert value, should set to 0 if there is no alert type
135     }
136     else {
137         $self->alert( !$return || defined $self->alert_type );
138     }
139
140     $self->ok($return);
141
142     return { messages => $messages };
143 }
144
145 sub resensitize {
146         my $self = shift;
147         unless ($self->{item}) {
148                 warn "resensitize(): no item found in object to resensitize";
149                 return;
150         }
151         return !$self->{item}->magnetic_media;
152 }
153
154 sub patron_id {
155         my $self = shift;
156         unless ($self->{patron}) {
157                 warn "patron_id(): no patron found in object";
158                 return;
159         }
160         return $self->{patron}->id;
161 }
162
163 1;