Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[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     if ( $return_date ) {
60         $return_date =   substr( $return_date, 0, 4 )
61                        . '-'
62                        . substr( $return_date, 4, 2 )
63                        . '-'
64                        . substr( $return_date, 6, 2 )
65                        . q{ }
66                        . substr( $return_date, 12, 2 )
67                        . ':'
68                        . substr( $return_date, 14, 2 )
69                        . ':'
70                        . substr( $return_date, 16, 2 );
71         $return_date = dt_from_string($return_date);
72     }
73
74     $debug and warn "do_checkin() calling AddReturn($barcode, $branch)";
75     my ($return, $messages, $issue, $borrower) = AddReturn($barcode, $branch, undef, $return_date);
76
77     if ( $checked_in_ok ) {
78         delete $messages->{ItemLocationUpdated};
79         delete $messages->{NotIssued};
80         delete $messages->{LocalUse};
81         $return = 1 unless keys %$messages;
82     }
83
84     # biblionumber, biblioitemnumber, itemnumber
85     # borrowernumber, reservedate, branchcode
86     # cancellationdate, found, reservenotes, priority, timestamp
87     if( $messages->{DataCorrupted} ) {
88         $self->alert_type('98');
89     }
90     if ($messages->{BadBarcode}) {
91         $self->alert_type('99');
92     }
93     if ($messages->{withdrawn}) {
94         $self->alert_type('99');
95     }
96     if ($messages->{WasLost}) {
97         $self->alert_type('99') if C4::Context->preference("BlockReturnOfLostItems");
98     }
99     if ($messages->{Wrongbranch}) {
100         $self->{item}->destination_loc($messages->{Wrongbranch}->{Rightbranch});
101         $self->alert_type('04');            # send to other branch
102     }
103     if ($messages->{WrongTransfer}) {
104         $self->{item}->destination_loc($messages->{WrongTransfer});
105         $self->alert_type('04');            # send to other branch
106     }
107     if ($messages->{NeedsTransfer}) {
108         $self->{item}->destination_loc($messages->{NeedsTransfer});
109         $self->alert_type('04');            # send to other branch
110     }
111     if ($messages->{WasTransfered}) { # set into transit so tell unit
112         $self->{item}->destination_loc($issue->item->homebranch);
113         $self->alert_type('04');            # send to other branch
114     }
115     if ($messages->{ResFound}) {
116         $self->hold($messages->{ResFound});
117         if ($branch eq $messages->{ResFound}->{branchcode}) {
118             $self->alert_type('01');
119             ModReserveAffect( $messages->{ResFound}->{itemnumber},
120                 $messages->{ResFound}->{borrowernumber}, 0, $messages->{ResFound}->{reserve_id});
121
122         } else {
123             $self->alert_type('02');
124             ModReserveAffect( $messages->{ResFound}->{itemnumber},
125                 $messages->{ResFound}->{borrowernumber}, 1, $messages->{ResFound}->{reserve_id});
126             ModItemTransfer( $messages->{ResFound}->{itemnumber},
127                 $branch,
128                 $messages->{ResFound}->{branchcode},
129                 $messages->{TransferTrigger},
130             );
131
132         }
133         $self->{item}->hold_patron_id( $messages->{ResFound}->{borrowernumber} );
134         $self->{item}->destination_loc( $messages->{ResFound}->{branchcode} );
135     }
136     # ignoring messages: NotIssued, WasTransfered
137
138     if ($cv_triggers_alert) {
139         $self->alert( defined $self->alert_type ); # Overwrites existing alert value, should set to 0 if there is no alert type
140     }
141     else {
142         $self->alert( !$return || defined $self->alert_type );
143     }
144
145     $self->ok($return);
146
147     return { messages => $messages };
148 }
149
150 sub resensitize {
151         my $self = shift;
152         unless ($self->{item}) {
153                 warn "resensitize(): no item found in object to resensitize";
154                 return;
155         }
156         return !$self->{item}->magnetic_media;
157 }
158
159 sub patron_id {
160         my $self = shift;
161         unless ($self->{patron}) {
162                 warn "patron_id(): no patron found in object";
163                 return;
164         }
165         return $self->{patron}->id;
166 }
167
168 1;