67d0b1d7922cc3985011300783e70a093fbda650
[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     if ($checked_in_ok){
74         $debug and warn 'not raising alert when AddReturn() does not return a value for $return due to $checked_in_ok being set to true';
75     }
76     else {
77         $self->alert(!$return);
78     }
79     # ignoring messages: NotIssued, WasLost, WasTransfered
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->{Wrongbranch}) {
94         $self->{item}->destination_loc($messages->{Wrongbranch}->{Rightbranch});
95         $self->alert_type('04');            # send to other branch
96     }
97     if ($messages->{WrongTransfer}) {
98         $self->{item}->destination_loc($messages->{WrongTransfer});
99         $self->alert_type('04');            # send to other branch
100     }
101     if ($messages->{NeedsTransfer}) {
102         $self->{item}->destination_loc($messages->{NeedsTransfer});
103         $self->alert_type('04');            # send to other branch
104     }
105     if ($messages->{WasTransfered}) { # set into transit so tell unit
106         $self->{item}->destination_loc($issue->item->homebranch);
107         $self->alert_type('04');            # send to other branch
108     }
109     if ($messages->{ResFound}) {
110         $self->hold($messages->{ResFound});
111         if ($branch eq $messages->{ResFound}->{branchcode}) {
112             $self->alert_type('01');
113             ModReserveAffect( $messages->{ResFound}->{itemnumber},
114                 $messages->{ResFound}->{borrowernumber}, 0, $messages->{ResFound}->{reserve_id});
115
116         } else {
117             $self->alert_type('02');
118             ModReserveAffect( $messages->{ResFound}->{itemnumber},
119                 $messages->{ResFound}->{borrowernumber}, 1, $messages->{ResFound}->{reserve_id});
120             ModItemTransfer( $messages->{ResFound}->{itemnumber},
121                 $branch,
122                 $messages->{ResFound}->{branchcode}
123             );
124
125         }
126         $self->{item}->hold_patron_id( $messages->{ResFound}->{borrowernumber} );
127         $self->{item}->destination_loc( $messages->{ResFound}->{branchcode} );
128     }
129
130     my $alert = defined $self->alert_type;
131     if ( $cv_triggers_alert ) {
132         $self->alert($alert); # Overwrites existing alert value, should set to 0 if there is no alert type
133     } else {
134         $self->alert($alert) if $alert; # Doesn't affect alert value unless an alert type is set
135     }
136
137     $self->ok($return);
138
139     return { messages => $messages };
140 }
141
142 sub resensitize {
143         my $self = shift;
144         unless ($self->{item}) {
145                 warn "resensitize(): no item found in object to resensitize";
146                 return;
147         }
148         return !$self->{item}->magnetic_media;
149 }
150
151 sub patron_id {
152         my $self = shift;
153         unless ($self->{patron}) {
154                 warn "patron_id(): no patron found in object";
155                 return;
156         }
157         return $self->{patron}->id;
158 }
159
160 1;