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