Bug 22043: SIP Checkin Response alert flag set to often set to Y incorrectly
[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::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     if (!$branch) {
51         $branch = 'SIP2';
52     }
53     my $barcode = $self->{item}->id;
54
55     $return_date =   substr( $return_date, 0, 4 )
56                    . '-'
57                    . substr( $return_date, 4, 2 )
58                    . '-'
59                    . substr( $return_date, 6, 2 )
60                    . q{ }
61                    . substr( $return_date, 12, 2 )
62                    . ':'
63                    . substr( $return_date, 14, 2 )
64                    . ':'
65                    . substr( $return_date, 16, 2 );
66
67     $debug and warn "do_checkin() calling AddReturn($barcode, $branch)";
68     my ($return, $messages, $issue, $borrower) = AddReturn($barcode, $branch, undef, undef, $return_date);
69
70     # biblionumber, biblioitemnumber, itemnumber
71     # borrowernumber, reservedate, branchcode
72     # cancellationdate, found, reservenotes, priority, timestamp
73     if( $messages->{DataCorrupted} ) {
74         $self->alert_type('98');
75     }
76     if ($messages->{BadBarcode}) {
77         $self->alert_type('99');
78     }
79     if ($messages->{withdrawn}) {
80         $self->alert_type('99');
81     }
82     if ($messages->{Wrongbranch}) {
83         $self->{item}->destination_loc($messages->{Wrongbranch}->{Rightbranch});
84         $self->alert_type('04');            # send to other branch
85     }
86     if ($messages->{WrongTransfer}) {
87         $self->{item}->destination_loc($messages->{WrongTransfer});
88         $self->alert_type('04');            # send to other branch
89     }
90     if ($messages->{NeedsTransfer}) {
91         $self->{item}->destination_loc($messages->{NeedsTransfer});
92         $self->alert_type('04');            # send to other branch
93     }
94     if ($messages->{WasTransfered}) { # set into transit so tell unit
95         $self->{item}->destination_loc($issue->item->homebranch);
96         $self->alert_type('04');            # send to other branch
97     }
98     if ($messages->{ResFound}) {
99         $self->hold($messages->{ResFound});
100         if ($branch eq $messages->{ResFound}->{branchcode}) {
101             $self->alert_type('01');
102             ModReserveAffect( $messages->{ResFound}->{itemnumber},
103                 $messages->{ResFound}->{borrowernumber}, 0, $messages->{ResFound}->{reserve_id});
104
105         } else {
106             $self->alert_type('02');
107             ModReserveAffect( $messages->{ResFound}->{itemnumber},
108                 $messages->{ResFound}->{borrowernumber}, 1, $messages->{ResFound}->{reserve_id});
109             ModItemTransfer( $messages->{ResFound}->{itemnumber},
110                 $branch,
111                 $messages->{ResFound}->{branchcode}
112             );
113
114         }
115         $self->{item}->hold_patron_id( $messages->{ResFound}->{borrowernumber} );
116         $self->{item}->destination_loc( $messages->{ResFound}->{branchcode} );
117     }
118     $self->alert(defined $self->alert_type);  # alert_type could be "00", hypothetically
119     $self->ok($return);
120 }
121
122 sub resensitize {
123         my $self = shift;
124         unless ($self->{item}) {
125                 warn "resensitize(): no item found in object to resensitize";
126                 return;
127         }
128         return !$self->{item}->magnetic_media;
129 }
130
131 sub patron_id {
132         my $self = shift;
133         unless ($self->{patron}) {
134                 warn "patron_id(): no patron found in object";
135                 return;
136         }
137         return $self->{patron}->id;
138 }
139
140 1;