c8b8fe8b41eba432eb63210bb788bd26d66f4b96
[koha.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::Reserves qw( ModReserveAffect );
16 use C4::Items qw( ModItemTransfer );
17 use C4::Debug;
18
19 use parent qw(C4::SIP::ILS::Transaction);
20
21 my %fields = (
22     magnetic => 0,
23     sort_bin => undef,
24     collection_code  => undef,
25     # 3M extensions:
26     call_number      => undef,
27     destination_loc  => undef,
28     alert_type       => undef,  # 00,01,02,03,04 or 99
29     hold_patron_id   => undef,
30     hold_patron_name => "",
31     hold             => undef,
32 );
33
34 sub new {
35     my $class = shift;
36     my $self = $class->SUPER::new();                # start with an ILS::Transaction object
37
38     foreach (keys %fields) {
39         $self->{_permitted}->{$_} = $fields{$_};    # overlaying _permitted
40     }
41
42     @{$self}{keys %fields} = values %fields;        # copying defaults into object
43     return bless $self, $class;
44 }
45
46 sub do_checkin {
47     my $self = shift;
48     my $branch = shift;
49     my $return_date = shift;
50     my $cv_triggers_alert = shift;
51
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, $issue, $borrower) = AddReturn($barcode, $branch, undef, $return_date);
71     $self->alert(!$return);
72     # ignoring messages: NotIssued, WasLost, WasTransfered
73
74     # biblionumber, biblioitemnumber, itemnumber
75     # borrowernumber, reservedate, branchcode
76     # cancellationdate, found, reservenotes, priority, timestamp
77     if( $messages->{DataCorrupted} ) {
78         $self->alert_type('98');
79     }
80     if ($messages->{BadBarcode}) {
81         $self->alert_type('99');
82     }
83     if ($messages->{withdrawn}) {
84         $self->alert_type('99');
85     }
86     if ($messages->{Wrongbranch}) {
87         $self->{item}->destination_loc($messages->{Wrongbranch}->{Rightbranch});
88         $self->alert_type('04');            # send to other branch
89     }
90     if ($messages->{WrongTransfer}) {
91         $self->{item}->destination_loc($messages->{WrongTransfer});
92         $self->alert_type('04');            # send to other branch
93     }
94     if ($messages->{NeedsTransfer}) {
95         $self->{item}->destination_loc($messages->{NeedsTransfer});
96         $self->alert_type('04');            # send to other branch
97     }
98     if ($messages->{WasTransfered}) { # set into transit so tell unit
99         $self->{item}->destination_loc($issue->item->homebranch);
100         $self->alert_type('04');            # send to other branch
101     }
102     if ($messages->{ResFound}) {
103         $self->hold($messages->{ResFound});
104         if ($branch eq $messages->{ResFound}->{branchcode}) {
105             $self->alert_type('01');
106             ModReserveAffect( $messages->{ResFound}->{itemnumber},
107                 $messages->{ResFound}->{borrowernumber}, 0, $messages->{ResFound}->{reserve_id});
108
109         } else {
110             $self->alert_type('02');
111             ModReserveAffect( $messages->{ResFound}->{itemnumber},
112                 $messages->{ResFound}->{borrowernumber}, 1, $messages->{ResFound}->{reserve_id});
113             ModItemTransfer( $messages->{ResFound}->{itemnumber},
114                 $branch,
115                 $messages->{ResFound}->{branchcode}
116             );
117
118         }
119         $self->{item}->hold_patron_id( $messages->{ResFound}->{borrowernumber} );
120         $self->{item}->destination_loc( $messages->{ResFound}->{branchcode} );
121     }
122
123     my $alert = defined $self->alert_type;
124     if ( $cv_triggers_alert ) {
125         $self->alert($alert); # Overwrites existing alert value, should set to 0 if there is no alert type
126     } else {
127         $self->alert($alert) if $alert; # Doesn't affect alert value unless an alert type is set
128     }
129
130     $self->ok($return);
131 }
132
133 sub resensitize {
134         my $self = shift;
135         unless ($self->{item}) {
136                 warn "resensitize(): no item found in object to resensitize";
137                 return;
138         }
139         return !$self->{item}->magnetic_media;
140 }
141
142 sub patron_id {
143         my $self = shift;
144         unless ($self->{patron}) {
145                 warn "patron_id(): no patron found in object";
146                 return;
147         }
148         return $self->{patron}->id;
149 }
150
151 1;