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