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