]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/SIP/Item.pm
added deleted check to copy search
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / SIP / Item.pm
1 #
2 #
3 # A Class for hiding the ILS's concept of the item from the OpenSIP
4 # system
5 #
6
7 package OpenILS::SIP::Item;
8 use strict; use warnings;
9
10 use Sys::Syslog qw(syslog);
11
12 use OpenILS::SIP;
13 use OpenILS::SIP::Transaction;
14 use OpenILS::Application::AppUtils;
15 use OpenILS::Application::Circ::ScriptBuilder;
16 use Data::Dumper;
17 use OpenILS::Const qw/:const/;
18 use OpenSRF::Utils qw/:datetime/;
19 use DateTime::Format::ISO8601;
20 my $U = 'OpenILS::Application::AppUtils';
21
22 my %item_db;
23
24 sub new {
25     my ($class, $item_id) = @_;
26     my $type = ref($class) || $class;
27     my $self = bless( {}, $type );
28
29         syslog('LOG_DEBUG', "OILS: Loading item $item_id...");
30         return undef unless $item_id;
31
32         my $e = OpenILS::SIP->editor();
33
34         my $copy = $e->search_asset_copy(
35                 [
36                         { barcode => $item_id, deleted => 'f' },
37                         {
38                                 flesh => 3,
39                                 flesh_fields => {
40                                         acp => [ 'circ_lib', 'call_number', 'status' ],
41                                         acn => [ 'owning_lib', 'record' ],
42                                 }
43                         }
44                 ]
45         );
46
47
48         $copy = $$copy[0];
49
50         if(!$copy) {
51                 syslog("LOG_DEBUG", "OILS: Item '%s' : not found", $item_id);
52                 return undef;
53         }
54
55         my ($circ) = $U->fetch_open_circulation($copy->id);
56         if($circ) {
57                 # if i am checked out, set $self->{patron} to the user's barcode
58                 my $user = $e->retrieve_actor_user(
59                         [
60                                 $circ->usr,
61                                 { flesh => 1, flesh_fields => { "au" => [ 'card' ] } }
62                         ]
63                 );
64
65                 my $bc = ($user) ? $user->card->barcode : "";
66                 $self->{patron} = $bc;
67                 $self->{patron_object} = $user;
68
69                 syslog('LOG_DEBUG', "OILS: Open circulation exists on $item_id : user = $bc");
70         }
71
72         $self->{id}                     = $item_id;
73         $self->{copy}           = $copy;
74         $self->{volume} = $copy->call_number;
75         $self->{record} = $copy->call_number->record;
76         $self->{mods}           = $U->record_to_mvr($self->{record}) if $self->{record}->marc;
77
78         syslog("LOG_DEBUG", "OILS: Item('$item_id'): found with title '%s'", $self->title_id);
79
80         return $self;
81 }
82
83 sub run_attr_script {
84         my $self = shift;
85         return 1 if $self->{ran_script};
86         $self->{ran_script} = 1;
87
88         my $config = OpenILS::SIP->config();
89         my $path = $config->{implementation_config}->{scripts}->{path};
90         my $item_config_script = $config->{implementation_config}->{scripts}->{item_config};
91
92         syslog('LOG_DEBUG', "OILS: Script path = $path, Item config script = $item_config_script");
93
94         my $runner = 
95                 OpenILS::Application::Circ::ScriptBuilder->build(
96                         {
97                                 copy => $self->{copy},
98                                 editor => OpenILS::SIP->editor(),
99                         }
100                 );
101
102         $runner->add_path($path);
103         $runner->load($item_config_script);
104
105         unless( $self->{item_config_result} = $runner->run ) {
106                 warn "Item config script [$path : $item_config_script] failed to run: $@\n";
107                 syslog('LOG_ERR', "OILS: Item config script [$path : $item_config_script] failed to run: $@");
108                 return undef;
109         }
110
111         return 1;
112 }
113
114 sub magnetic {
115     my $self = shift;
116          return 0 unless $self->run_attr_script;
117          my $mag = $self->{item_config_result}->{magneticMedia};
118          syslog('LOG_DEBUG', "OILS: magnetic = $mag");
119          return ($mag and $mag eq 't') ? 1 : 0;
120 }
121
122 sub sip_media_type {
123     my $self = shift;
124          return 0 unless $self->run_attr_script;
125          my $media = $self->{item_config_result}->{SIPMediaType};
126          syslog('LOG_DEBUG', "OILS: media type = $media");
127          return ($media) ? $media : '001';
128 }
129
130 sub sip_item_properties {
131     my $self = shift;
132          return "";
133 }
134
135 sub status_update {
136     my ($self, $props) = @_;
137     my $status = OpenILS::SIP::Transaction->new;
138     $self->{sip_item_properties} = $props;
139     $status->{ok} = 1;
140     return $status;
141 }
142
143
144 sub id {
145     my $self = shift;
146     return $self->{id};
147 }
148
149 sub title_id {
150     my $self = shift;
151     return ($self->{mods}) ? $self->{mods}->title : $self->{copy}->dummy_title;
152 }
153
154 sub permanent_location {
155     my $self = shift;
156          return $self->{volume}->owning_lib->name;
157 }
158
159 sub current_location {
160     my $self = shift;
161          return $self->{copy}->circ_lib->name;
162 }
163
164
165 # 2 chars 0-99 
166 # 01 Other
167 # 02 On order
168 # 03 Available
169 # 04 Charged
170 # 05 Charged; not to be recalled until earliest recall date
171 # 06 In process
172 # 07 Recalled
173 # 08 Waiting on hold shelf
174 # 09 Waiting to be re-shelved
175 # 10 In transit between library locations
176 # 11 Claimed returned
177 # 12 Lost
178 # 13 Missing 
179 sub sip_circulation_status {
180         my $self = shift;
181         my $stat = $self->{copy}->status->id;
182
183         return '02' if $stat == OILS_COPY_STATUS_ON_ORDER;
184         return '03' if $stat == OILS_COPY_STATUS_AVAILABLE;
185         return '04' if $stat == OILS_COPY_STATUS_CHECKED_OUT;
186         return '06' if $stat == OILS_COPY_STATUS_IN_PROCESS;
187         return '08' if $stat == OILS_COPY_STATUS_ON_HOLDS_SHELF;
188         return '09' if $stat == OILS_COPY_STATUS_RESHELVING;
189         return '10' if $stat == OILS_COPY_STATUS_IN_TRANSIT;
190         return '12' if $stat == OILS_COPY_STATUS_LOST;
191         return '13' if $stat == OILS_COPY_STATUS_MISSING;
192                 
193         return 01;
194 }
195
196 sub sip_security_marker {
197     return '02';
198 }
199
200 sub sip_fee_type {
201     return '01';
202 }
203
204 sub fee {
205     my $self = shift;
206          return 0;
207 }
208
209
210 sub fee_currency {
211         my $self = shift;
212         return OpenILS::SIP->config()->{implementation_config}->{currency};
213 }
214
215 sub owner {
216     my $self = shift;
217          return $self->{volume}->owning_lib->name;
218 }
219
220 sub hold_queue {
221     my $self = shift;
222          return [];
223 }
224
225 sub hold_queue_position {
226     my ($self, $patron_id) = @_;
227          return 1;
228 }
229
230 sub due_date {
231         my $self = shift;
232         my $e = OpenILS::SIP->editor();
233
234         my $circ = $e->search_action_circulation(
235                 { target_copy => $self->{copy}->id, stop_fines => undef } )->[0];
236
237         if(!$circ) {
238                 # if not, lets look for other circs we can check in
239                 $circ = $e->search_action_circulation(
240                         { 
241                                 target_copy => $self->{copy}->id, 
242                                 xact_finish => undef,
243                                 stop_fines      => [ 'CLAIMSRETURNED', 'LOST', 'LONGOVERDUE' ]
244                         } )->[0];
245         }
246
247         return 0 unless $circ;
248         my $due = OpenILS::SIP->format_date($circ->due_date);
249         syslog('LOG_DEBUG', "Item due date = $due");
250         return $due;
251 }
252
253 sub recall_date {
254     my $self = shift;
255     return 0;
256 }
257
258 sub hold_pickup_date {
259     my $self = shift;
260          return 0;
261 }
262
263 # message to display on console
264 sub screen_msg {
265     my $self = shift;
266     return $self->{screen_msg} || '';
267 }
268
269
270 # reciept printer
271 sub print_line {
272      my $self = shift;
273      return $self->{print_line} || '';
274 }
275
276
277 # An item is available for a patron if
278 # 1) It's not checked out and (there's no hold queue OR patron
279 #    is at the front of the queue)
280 # OR
281 # 2) It's checked out to the patron and there's no hold queue
282 sub available {
283         my ($self, $for_patron) = @_;
284
285         my $stat = $self->{copy}->status->id;
286         return 1 if 
287                 $stat == OILS_COPY_STATUS_AVAILABLE or
288                 $stat == OILS_COPY_STATUS_RESHELVING;
289         
290         return 0;
291 }
292
293
294 1;