SIP returns (checkin) working now
[koha.git] / C4 / SIP / ILS / Item.pm
1 #
2 # ILS::Item.pm
3
4 # A Class for hiding the ILS's concept of the item from the OpenSIP
5 # system
6 #
7
8 package ILS::Item;
9
10 use strict;
11 use warnings;
12
13 use Sys::Syslog qw(syslog);
14
15 use ILS::Transaction;
16
17 use C4::Biblio;
18 use C4::Circulation;
19 use C4::Members;
20
21 our %item_db = (
22                 '1565921879' => {
23                                  title => "Perl 5 desktop reference",
24                                  id => '1565921879',
25                                  sip_media_type => '001',
26                                  magnetic_media => 0,
27                                  hold_queue => [],
28                                 },
29                 '0440242746' => {
30                                  title => "The deep blue alibi",
31                                  id => '0440242746',
32                                  sip_media_type => '001',
33                                  magnetic_media => 0,
34                                  hold_queue => [],
35                 },
36                 '660' => {
37                                  title => "Harry Potter y el cáliz de fuego",
38                                  id => '660',
39                                  sip_media_type => '001',
40                                  magnetic_media => 0,
41                                  hold_queue => [],
42                          },
43                 );
44
45 sub new {
46     my ($class, $item_id) = @_;
47     my $type = ref($class) || $class;
48     my $self;
49         my $item = GetBiblioFromItemNumber( GetItemnumberFromBarcode($item_id) );
50         
51     if (! $item) {
52                 syslog("LOG_DEBUG", "new ILS::Item('%s'): not found", $item_id);
53                 warn "no item $item_id";
54                 return undef;
55     }
56     $item->{'id'} = $item->{'barcode'};
57         # check if its on issue and if so get the borrower
58         my $issue = GetItemIssue($item->{'itemnumber'});
59         my $borrower = GetMember($issue->{'borrowernumber'},'borrowernumber');
60         $item->{patron} = $borrower->{'cardnumber'};
61     $self = $item;
62
63     bless $self, $type;
64
65     syslog("LOG_DEBUG", "new ILS::Item('%s'): found with title '%s'",
66            $item_id, $self->{title});
67
68     return $self;
69 }
70
71 sub magnetic {
72     my $self = shift;
73
74     return $self->{magnetic_media};
75 }
76
77 sub sip_media_type {
78     my $self = shift;
79
80     return $self->{sip_media_type};
81 }
82
83 sub sip_item_properties {
84     my $self = shift;
85
86     return $self->{sip_item_properties};
87 }
88
89 sub status_update {
90     my ($self, $props) = @_;
91     my $status = new ILS::Transaction;
92
93     $self->{sip_item_properties} = $props;
94     $status->{ok} = 1;
95
96     return $status;
97 }
98
99     
100 sub id {
101     my $self = shift;
102
103     return $self->{id};
104 }
105
106 sub title_id {
107     my $self = shift;
108
109     return $self->{title};
110 }
111
112 sub permanent_location {
113     my $self = shift;
114
115     return $self->{permanent_location} || '';
116 }
117
118 sub current_location {
119     my $self = shift;
120
121     return $self->{current_location} || '';
122 }
123
124 sub sip_circulation_status {
125     my $self = shift;
126
127     if ($self->{patron}) {
128         return '04';
129     } elsif (scalar @{$self->{hold_queue}}) {
130         return '08';
131     } else {
132         return '03';
133     }
134 }
135
136 sub sip_security_marker {
137     return '02';
138 }
139
140 sub sip_fee_type {
141     return '01';
142 }
143
144 sub fee {
145     my $self = shift;
146
147     return $self->{fee} || 0;
148 }
149
150 sub fee_currency {
151     my $self = shift;
152
153     return $self->{currency} || 'CAD';
154 }
155
156 sub owner {
157     my $self = shift;
158
159     return 'UWOLS';
160 }
161
162 sub hold_queue {
163     my $self = shift;
164
165     return $self->{hold_queue};
166 }
167
168 sub hold_queue_position {
169     my ($self, $patron_id) = @_;
170     my $i;
171
172     for ($i = 0; $i < scalar @{$self->{hold_queue}}; $i += 1) {
173         if ($self->{hold_queue}[$i]->{patron_id} eq $patron_id) {
174             return $i + 1;
175         }
176     }
177     return 0;
178 }
179
180 sub due_date {
181     my $self = shift;
182
183     return $self->{due_date} || 0;
184 }
185
186 sub recall_date {
187     my $self = shift;
188
189     return $self->{recall_date} || 0;
190 }
191
192 sub hold_pickup_date {
193     my $self = shift;
194
195     return $self->{hold_pickup_date} || 0;
196 }
197
198 sub screen_msg {
199     my $self = shift;
200
201     return $self->{screen_msg} || '';
202 }
203
204 sub print_line {
205      my $self = shift;
206
207      return $self->{print_line} || '';
208 }
209
210 # An item is available for a patron if
211 # 1) It's not checked out and (there's no hold queue OR patron
212 #    is at the front of the queue)
213 # OR
214 # 2) It's checked out to the patron and there's no hold queue
215 sub available {
216      my ($self, $for_patron) = @_;
217
218      return ((!defined($self->{patron_id}) && (!scalar @{$self->{hold_queue}}
219                                                || ($self->{hold_queue}[0] eq $for_patron)))
220              || ($self->{patron_id} && ($self->{patron_id} eq $for_patron)
221                  && !scalar @{$self->{hold_queue}}));
222 }
223
224 1;