]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/asset.pm
ooops. wrong variable name
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / Publisher / asset.pm
1 package OpenILS::Application::Storage::Publisher::asset;
2 use base qw/OpenILS::Application::Storage/;
3 #use OpenILS::Application::Storage::CDBI::asset;
4 #use OpenILS::Utils::Fieldmapper;
5 use OpenSRF::Utils::Logger qw/:level/;
6 use OpenSRF::EX qw/:try/;
7 use JSON;
8
9 #
10
11 my $log = 'OpenSRF::Utils::Logger';
12
13 use MARC::Record;
14 use MARC::File::XML;
15
16 sub circ_count {
17         my $self = shift;
18         my $client = shift;
19         my $copy = shift;
20         my $granularity = shift;
21
22         my $c_table = action::circulation->table;
23
24         if (lc($granularity) eq 'year') {
25                 $granularity = ", to_char(xact_start, 'YYYY') as when";
26         } elsif (lc($granularity) eq 'month') {
27                 $granularity = ", to_char(xact_start, 'YYYY-MM') as when";
28         } elsif (lc($granularity) eq 'day') {
29                 $granularity = ", to_char(xact_start, 'YYYY-MM-DD') as when";
30         } else {
31                 $granularity = ", 'total' as when";
32         }
33
34         my $SQL = <<"   SQL";
35                 SELECT  COUNT(*) as count $granularity
36                   FROM  $c_table
37                   WHERE target_copy = ?
38         SQL
39
40
41         if ($granularity !~ /total/o) {
42                 $SQL .= ' GROUP BY 2 ORDER BY 2';
43         }
44
45         $log->debug("Circ count SQL [$SQL]", DEBUG);
46
47         return action::circulation->db_Main->selectall_hashref($SQL, 'when', {}, $copy);
48 }
49 __PACKAGE__->register_method(
50         method          => 'circ_count',
51         api_name        => 'open-ils.storage.asset.copy.circ_count',
52         argc            => 1,
53 );
54
55
56 #our $_default_subfield_map = {
57 #        call_number     => $cn,
58 #        barcode         => $bc,
59 #        owning_lib      => $ol,
60 #        circulating_lib => $cl,
61 #        copy_location   => $sl,
62 #        copy_number     => $num,
63 #        price           => $pr,
64 #        status          => $loc,
65 #        create_date     => $date,
66 #
67 #        legacy_item_type        => $it,
68 #        legacy_item_cat_1       => $ic1,
69 #        legacy_item_cat_2       => $ic2,
70 #};
71
72 my %org_cache;
73
74 sub import_xml_holdings {
75         my $self = shift;
76         my $client = shift;
77         my $editor = shift;
78         my $record = shift;
79         my $xml = shift;
80         my $tag = shift;
81         my $map = shift;
82         my $date_format = shift || 'mm/dd/yyyy';
83
84         ($record) = biblio::record_entry->search_where($record);
85
86         return 0 unless ($record);
87
88         my $r = MARC::Record->new_from_xml($xml);
89
90         my $count = 0;
91         for my $f ( $r->fields( $tag ) ) {
92                 next unless ($f->subfield( $map->{owning_lib} ));
93
94                 my ($ol,$cl);
95
96                 try {
97                         $ol = 
98                                 $org_cache{ $f->subfield( $map->{owning_lib} ) }
99                                 || actor::org_unit->search( shortname => $f->subfield( $map->{owning_lib} ) )->next->id;
100
101                         $org_cache{ $f->subfield( $map->{owning_lib} ) } = $ol;
102                 } otherwise {
103                         $log->debug('Could not find library with shortname ['.$f->subfield( $map->{owning_lib} ).'] : '. shift(), ERROR);
104                         $log->info('Failed holdings tag: ['.JSON->perl2JSON( $f ).']');
105                 };
106                 
107                 try {
108                         $cl =
109                                 $org_cache{ $f->subfield( $map->{circulating_lib} ) }
110                                 || actor::org_unit->search( shortname => $f->subfield( $map->{circulating_lib} ) )->next->id;
111
112                         $org_cache{ $f->subfield( $map->{circulating_lib} ) } = $cl;
113                 } otherwise {
114                         $log->debug('Could not find library with shortname ['.$f->subfield( $map->{circulating_lib} ).'] : '. shift(), ERROR);
115                         $log->info('Failed holdings tag: ['.JSON->perl2JSON( $f ).']');
116                 };
117
118                 next unless ($ol && $cl);
119
120                 my $cn;
121                 try {
122                         $cn = asset::call_number->find_or_create(
123                                 { label         => $f->subfield( $map->{call_number} ),
124                                   owning_lib    => $ol,
125                                   record        => $record->id,
126                                   creator       => $editor,
127                                   editor        => $editor,
128                                 }
129                         );
130                 } otherwise {
131                         $log->debug('Could not find or create callnumber ['.$f->subfield( $map->{call_number} )."] on record $record : ". shift(), ERROR);
132                         $log->info('Failed holdings tag: ['.JSON->perl2JSON( $f ).']');
133                 };
134
135                 next unless ($cn);
136
137                 my $create_date =  $f->subfield( $map->{create_date} );
138
139                 my ($m,$d,$y);
140                 if ($date_format eq 'mm/dd/yyyy') {
141                         ($m,$d,$y) = split '/', $create_date;
142
143                 } elsif ($date_format eq 'dd/mm/yyyy') {
144                         ($d,$m,$y) = split '/', $create_date;
145
146                 } elsif ($date_format eq 'mm-dd-yyyy') {
147                         ($m,$d,$y) = split '-', $create_date;
148
149                 } elsif ($date_format eq 'dd-mm-yyyy') {
150                         ($d,$m,$y) = split '-', $create_date;
151
152                 } elsif ($date_format eq 'yyyy-mm-dd') {
153                         ($y,$m,$d) = split '-', $create_date;
154
155                 } elsif ($date_format eq 'yyyy/mm/dd') {
156                         ($y,$m,$d) = split '/', $create_date;
157                 }
158
159                 if ($y == 0) {
160                         (undef,undef,undef,$d,$m,$y) = localtime;
161                         $m++;
162                         $y+=1900;
163                 }
164
165                 my $price = $f->subfield( $map->{price} );
166                 $price =~ s/[^0-9\.]+//gso;
167                 $price ||= '0.00';
168
169                 try {
170                         $cn->add_to_copies(
171                                 { circ_lib      => $cl,
172                                   copy_number   => $f->subfield( $map->{copy_number} ),
173                                   price         => $price,
174                                   barcode       => $f->subfield( $map->{barcode} ),
175                                   loan_duration => 2,
176                                   fine_level    => 2,
177                                   creator       => $editor,
178                                   editor        => $editor,
179                                   create_date   => sprintf('%04d-%02d-%02d',$y,$m,$d),
180                                 }
181                         );
182                         $count++;
183                 } otherwise {
184                         $log->debug('Could not create copy ['.$f->subfield( $map->{barcode} ).'] : '. shift(), ERROR);
185                 };
186         }
187
188         return $count;
189 }
190 __PACKAGE__->register_method(
191         method          => 'import_xml_holdings',
192         api_name        => 'open-ils.storage.asset.holdings.import.xml',
193         argc            => 5,
194         stream          => 0,
195 );
196
197 # XXX
198 # see /home/miker/cn_browse-test.sql for page up and down sql ...
199 # XXX
200
201 sub cn_browse_pagedown {
202         my $self = shift;
203         my $client = shift;
204
205         my %args = @_;
206
207         my $cn = uc($args{label});
208         my $org = $args{org_unit};
209         my $depth = $args{depth};
210         my $boundry_id = $args{boundry_id};
211         my $size = $args{page_size} || 20;
212         $size = int($size);
213
214         my $table = asset::call_number->table;
215
216         my $descendants = "actor.org_unit_descendants($org)";
217         if (defined $depth) {
218                 $descendants = "actor.org_unit_descendants($org,$depth)";
219         }
220
221         my $orgs = join(',', @{ asset::call_number->db_Main->selectcol_arrayref("SELECT DISTINCT id FROM $descendants;") });
222         
223         my $sql = <<"   SQL";
224                 select
225                         cn.label,
226                         cn.owning_lib,
227                         cn.record,
228                         cn.id
229                 from
230                         $table cn
231                 where
232                         (upper(label) > ?
233                         or ( cn.id > ? and upper(label) = ? ))
234                         and owning_lib in ($orgs)
235                 order by upper(label), 4, 2
236                 limit $size;
237         SQL
238
239         my $sth = asset::call_number->db_Main->prepare($sql);
240         $sth->execute($cn, $boundry_id, $cn);
241         while ( my @row = $sth->fetchrow_array ) {
242                 $client->respond([@row]);
243         }
244         $sth->finish;
245
246         return undef;
247 }
248 __PACKAGE__->register_method(
249         method          => 'cn_browse_pagedown',
250         api_name        => 'open-ils.storage.asset.call_number.browse.page_down',
251         argc            => 4,
252         stream          => 1,
253 );
254
255 sub cn_browse_pageup {
256         my $self = shift;
257         my $client = shift;
258
259         my %args = @_;
260
261         my $cn = uc($args{label});
262         my $org = $args{org_unit};
263         my $depth = $args{depth};
264         my $boundry_id = $args{boundry_id};
265         my $size = $args{page_size} || 20;
266         $size = int($size);
267
268         my $table = asset::call_number->table;
269
270         my $descendants = "actor.org_unit_descendants($org)";
271         if (defined $depth) {
272                 $descendants = "actor.org_unit_descendants($org,$depth)";
273         }
274
275         my $orgs = join(',', @{ asset::call_number->db_Main->selectcol_arrayref("SELECT DISTINCT id FROM $descendants;") });
276
277         my $sql = <<"   SQL";
278                 select * from (
279                         select
280                                 cn.label,
281                                 cn.owning_lib,
282                                 cn.record,
283                                 cn.id
284                         from
285                                 $table cn
286                         where
287                                 (upper(label) < ?
288                                 or ( cn.id < ? and upper(label) = ? ))
289                                 and owning_lib in ($orgs)
290                         order by upper(label) desc, 4 desc, 2 desc
291                         limit $size
292                 ) as bar
293                 order by 1,4,2;
294         SQL
295
296         my $sth = asset::call_number->db_Main->prepare($sql);
297         $sth->execute($cn, $boundry_id, $cn);
298         while ( my @row = $sth->fetchrow_array ) {
299                 $client->respond([@row]);
300         }
301         $sth->finish;
302
303         return undef;
304 }
305 __PACKAGE__->register_method(
306         method          => 'cn_browse_pageup',
307         api_name        => 'open-ils.storage.asset.call_number.browse.page_up',
308         argc            => 4,
309         stream          => 1,
310 );
311
312 sub cn_browse_target {
313         my $self = shift;
314         my $client = shift;
315
316         my %args = @_;
317
318         my $cn = uc($args{label});
319         my $org = $args{org_unit};
320         my $depth = $args{depth};
321         my $size = $args{page_size} || 20;
322         my $topsize = $size / 2;
323         $topsize = int($topsize);
324         $bottomsize = $size - $topsize;
325
326         my $table = asset::call_number->table;
327
328         my $descendants = "actor.org_unit_descendants($org)";
329         if (defined $depth) {
330                 $descendants = "actor.org_unit_descendants($org,$depth)";
331         }
332
333         my $orgs = join(',', @{ asset::call_number->db_Main->selectcol_arrayref("SELECT DISTINCT id FROM $descendants;") });
334
335         my $top_sql = <<"       SQL";
336                 select * from (
337                         select
338                                 cn.label,
339                                 cn.owning_lib,
340                                 cn.record,
341                                 cn.id
342                         from
343                                 $table cn
344                         where
345                                 upper(label) < ?
346                                 and owning_lib in ($orgs)
347                         order by upper(label) desc, 4 desc, 2 desc
348                         limit $topsize
349                 ) as bar
350                 order by 1,4,2;
351         SQL
352
353         my $bottom_sql = <<"    SQL";
354                 select
355                         cn.label,
356                         cn.owning_lib,
357                         cn.record,
358                         cn.id
359                 from
360                         $table cn
361                 where
362                         upper(label) >= ?
363                         and owning_lib in ($orgs)
364                 order by upper(label),4,2
365                 limit $bottomsize;
366         SQL
367
368         my $sth = asset::call_number->db_Main->prepare($top_sql);
369         $sth->execute($cn);
370         while ( my @row = $sth->fetchrow_array ) {
371                 $client->respond([@row]);
372         }
373         $sth->finish;
374
375         $sth = asset::call_number->db_Main->prepare($bottom_sql);
376         $sth->execute($cn);
377         while ( my @row = $sth->fetchrow_array ) {
378                 $client->respond([@row]);
379         }
380         $sth->finish;
381
382         return undef;
383 }
384 __PACKAGE__->register_method(
385         method          => 'cn_browse_target',
386         api_name        => 'open-ils.storage.asset.call_number.browse.target',
387         argc            => 4,
388         stream          => 1,
389 );
390
391
392 my %_prox_cache;
393 sub copy_proximity {
394         my $self = shift;
395         my $client = shift;
396
397         my $cp = shift;
398         my $org = shift;
399
400         return unless ($cp && $org);
401
402         my $key;
403         if (ref($cp)) {
404
405                 my $ol = $cp->circ_lib;
406                 
407                 $key = "$ol|$org";
408                 $key = "$org|$ol" if ($ol->id > $org);
409                 return $_prox_cache{$key} if (exists($_prox_cache{$key}));
410         }
411
412         $cp = asset::copy->retrieve($cp);
413
414         return unless $cp;
415         my $ol = $cp->circ_lib;
416
417         $key = "$ol|$org";
418         $key = "$org|$ol" if ($ol->id > $org);
419
420         $_prox_cache{$key} = asset::copy->db_Main->selectcol_arrayref('SELECT actor.org_unit_proximity(?,?)',{},"$ol","$org")->[0]
421                 unless (exists($_prox_cache{$key}));
422
423         return $_prox_cache{$key};
424 }
425 __PACKAGE__->register_method(
426         method          => 'copy_proximity',
427         api_name        => 'open-ils.storage.asset.copy.proximity',
428         argc            => 2,
429         stream          => 1,
430 );
431
432 sub asset_copy_location_all {
433         my $self = shift;
434         my $client = shift;
435
436         for my $rec ( asset::copy_location->retrieve_all ) {
437                 $client->respond( $rec->to_fieldmapper );
438         }
439
440         return undef;
441 }
442 __PACKAGE__->register_method(
443         method          => 'asset_copy_location_all',
444         api_name        => 'open-ils.storage.direct.asset.copy_location.retrieve.all',
445         argc            => 0,
446         stream          => 1,
447 );
448
449 # XXX arg, with the descendancy SPs...
450 sub ranged_asset_copy_location {
451         my $self = shift;
452         my $client = shift;
453         my @binds = @_;
454         
455         my $ctable = asset::copy_location->table;
456         
457         my $descendants = defined($binds[1]) ?
458                 "actor.org_unit_full_path(?, ?)" :
459                 "actor.org_unit_full_path(?)" ;
460
461         
462         my $sql = <<"   SQL";
463                 SELECT  DISTINCT c.*
464                   FROM  $ctable c
465                         JOIN $descendants d
466                                 ON (d.id = c.owning_lib)
467         SQL
468         
469         my $sth = asset::copy_location->db_Main->prepare($sql);
470         $sth->execute(@binds);
471         
472         while ( my $rec = $sth->fetchrow_hashref ) {
473         
474                 my $cnct = new Fieldmapper::asset::copy_location;
475                 map {$cnct->$_($$rec{$_})} keys %$rec;
476                 $client->respond( $cnct );
477         }
478
479         return undef;
480 }
481 __PACKAGE__->register_method(
482         method          => 'ranged_asset_copy_location',
483         api_name        => 'open-ils.storage.ranged.asset.copy_location.retrieve',
484         argc            => 1,
485         stream          => 1,
486 );
487
488
489 sub fleshed_copy {
490         my $self = shift;
491         my $client = shift;
492         my @ids = @_;
493
494         return undef unless (@ids);
495
496         @ids = ($ids[0]) unless ($self->api_name =~ /batch/o);
497
498         for my $id ( @ids ) {
499                 next unless $id;
500                 my $cp = asset::copy->retrieve($id);
501                 next unless $cp;
502
503                 my $cp_fm = $cp->to_fieldmapper;
504                 $cp_fm->circ_lib( $cp->circ_lib->to_fieldmapper );
505                 $cp_fm->location( $cp->location->to_fieldmapper );
506                 $cp_fm->status( $cp->status->to_fieldmapper );
507                 $cp_fm->stat_cat_entries( [ map { $_->to_fieldmapper } $cp->stat_cat_entries ] );
508
509                 $client->respond( $cp_fm );
510         }
511
512         return undef;
513 }
514 __PACKAGE__->register_method(
515         api_name        => 'open-ils.storage.fleshed.asset.copy.batch.retrieve',
516         method          => 'fleshed_copy',
517         argc            => 1,
518         stream          => 1,
519 );
520 __PACKAGE__->register_method(
521         api_name        => 'open-ils.storage.fleshed.asset.copy.retrieve',
522         method          => 'fleshed_copy',
523         argc            => 1,
524 );
525
526 sub fleshed_copy_by_barcode {
527         my $self = shift;
528         my $client = shift;
529         my $bc = ''.shift;
530
531         my ($cp) = asset::copy->search( { barcode => $bc } );
532
533         return undef unless ($cp);
534
535         my $cp_fm = $cp->to_fieldmapper;
536         $cp_fm->circ_lib( $cp->circ_lib->to_fieldmapper );
537         $cp_fm->location( $cp->location->to_fieldmapper );
538         $cp_fm->status( $cp->status->to_fieldmapper );
539
540         return $cp_fm;
541 }       
542 __PACKAGE__->register_method(
543         api_name        => 'open-ils.storage.fleshed.asset.copy.search.barcode',
544         method          => 'fleshed_copy_by_barcode',
545         argc            => 1,
546         stream          => 1,
547 );
548
549
550 #XXX Fix stored proc calls
551 sub fleshed_asset_stat_cat {
552         my $self = shift;
553         my $client = shift;
554         my @list = @_;
555
556         @list = ($list[0]) unless ($self->api_name =~ /batch/o);
557         for my $sc (@list) {
558                 my $cat = asset::stat_cat->retrieve($sc);
559                 
560                 next unless ($cat);
561
562                 my $sc_fm = $cat->to_fieldmapper;
563                 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
564                 $client->respond( $sc_fm );
565         }
566
567         return undef;
568 }
569 __PACKAGE__->register_method(
570         api_name        => 'open-ils.storage.fleshed.asset.stat_cat.retrieve',
571         api_level       => 1,
572         method          => 'fleshed_asset_stat_cat',
573 );
574
575 __PACKAGE__->register_method(
576         api_name        => 'open-ils.storage.fleshed.asset.stat_cat.retrieve.batch',
577         api_level       => 1,
578         stream          => 1,
579         method          => 'fleshed_asset_stat_cat',
580 );
581
582
583 #XXX Fix stored proc calls
584 sub ranged_asset_stat_cat {
585         my $self = shift;
586         my $client = shift;
587         my $ou = ''.shift();
588
589         return undef unless ($ou);
590         my $s_table = asset::stat_cat->table;
591
592         my $select = <<"        SQL";
593                 SELECT  s.*
594                   FROM  $s_table s
595                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
596                   ORDER BY name
597         SQL
598
599         $fleshed = 0;
600         $fleshed = 1 if ($self->api_name =~ /fleshed/o);
601
602         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
603         $sth->execute($ou);
604
605         for my $sc ( map { asset::stat_cat->construct($_) } $sth->fetchall_hash ) {
606                 my $sc_fm = $sc->to_fieldmapper;
607                 $sc_fm->entries(
608                         [ $self->method_lookup( 'open-ils.storage.ranged.asset.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
609                 ) if ($fleshed);
610                 $client->respond( $sc_fm );
611         }
612
613         return undef;
614 }
615 __PACKAGE__->register_method(
616         api_name        => 'open-ils.storage.ranged.fleshed.asset.stat_cat.all',
617         api_level       => 1,
618         stream          => 1,
619         method          => 'ranged_asset_stat_cat',
620 );
621
622 __PACKAGE__->register_method(
623         api_name        => 'open-ils.storage.ranged.asset.stat_cat.all',
624         api_level       => 1,
625         stream          => 1,
626         method          => 'ranged_asset_stat_cat',
627 );
628
629
630 #XXX Fix stored proc calls
631 sub multiranged_asset_stat_cat {
632         my $self = shift;
633         my $client = shift;
634         my $ous = shift;
635
636         return undef unless (defined($ous) and @$ous);
637         my $s_table = asset::stat_cat->table;
638
639         my $select = <<"        SQL";
640                 SELECT  s.*
641                   FROM  $s_table s
642                   WHERE s.owner IN ( XXX )
643                   ORDER BY name
644         SQL
645
646         my $collector = ' INTERSECT ';
647         my $entry_method = 'open-ils.storage.multiranged.intersect.asset.stat_cat_entry.search.stat_cat';
648         if ($self->api_name =~ /union/o) {
649                 $collector = ' UNION ';
650                 $entry_method = 'open-ils.storage.multiranged.union.asset.stat_cat_entry.search.stat_cat';
651         }
652
653         my $binds = join($collector, map { 'SELECT id FROM actor.org_unit_full_path(?)' } grep {defined} @$ous);
654         $select =~ s/XXX/$binds/so;
655         
656         $fleshed = 0;
657         $fleshed = 1 if ($self->api_name =~ /fleshed/o);
658
659         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
660         $sth->execute(map { "$_" } grep {defined} @$ous);
661
662         for my $sc ( map { asset::stat_cat->construct($_) } $sth->fetchall_hash ) {
663                 my $sc_fm = $sc->to_fieldmapper;
664                 $sc_fm->entries(
665                         [ $self->method_lookup( $entry_method )->run($ous, $sc->id) ]
666                 ) if ($fleshed);
667                 $client->respond( $sc_fm );
668         }
669
670         return undef;
671 }
672 __PACKAGE__->register_method(
673         api_name        => 'open-ils.storage.multiranged.intersect.fleshed.asset.stat_cat.all',
674         api_level       => 1,
675         stream          => 1,
676         method          => 'multiranged_asset_stat_cat',
677 );
678 __PACKAGE__->register_method(
679         api_name        => 'open-ils.storage.multiranged.union.fleshed.asset.stat_cat.all',
680         api_level       => 1,
681         stream          => 1,
682         method          => 'multiranged_asset_stat_cat',
683 );
684
685 #XXX Fix stored proc calls
686 sub ranged_asset_stat_cat_entry {
687         my $self = shift;
688         my $client = shift;
689         my $ou = ''.shift();
690         my $sc = ''.shift();
691
692         return undef unless ($ou);
693         my $s_table = asset::stat_cat_entry->table;
694
695         my $select = <<"        SQL";
696                 SELECT  s.*
697                   FROM  $s_table s
698                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
699                   WHERE stat_cat = ?
700                   ORDER BY name
701         SQL
702
703         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
704         $sth->execute($ou,$sc);
705
706         for my $sce ( map { asset::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
707                 $client->respond( $sce->to_fieldmapper );
708         }
709
710         return undef;
711 }
712 __PACKAGE__->register_method(
713         api_name        => 'open-ils.storage.ranged.asset.stat_cat_entry.search.stat_cat',
714         api_level       => 1,
715         stream          => 1,
716         method          => 'ranged_asset_stat_cat_entry',
717 );
718
719 #XXX Fix stored proc calls
720 sub multiranged_asset_stat_cat_entry {
721         my $self = shift;
722         my $client = shift;
723         my $ous = shift;
724         my $sc = ''.shift();
725
726         return undef unless (defined($ous) and @$ous);
727         my $s_table = asset::stat_cat_entry->table;
728
729         my $collector = ' INTERSECT ';
730         $collector = ' UNION ' if ($self->api_name =~ /union/o);
731
732         my $select = <<"        SQL";
733                 SELECT  s.*
734                   FROM  $s_table s
735                   WHERE s.owner IN ( XXX ) and s.stat_cat = ?
736                   ORDER BY value
737         SQL
738
739         my $binds = join($collector, map { 'SELECT id FROM actor.org_unit_full_path(?)' } grep {defined} @$ous);
740         $select =~ s/XXX/$binds/so;
741         
742         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
743         $sth->execute(map {"$_"} @$ous,$sc);
744
745         for my $sce ( map { asset::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
746                 $client->respond( $sce->to_fieldmapper );
747         }
748
749         return undef;
750 }
751 __PACKAGE__->register_method(
752         api_name        => 'open-ils.storage.multiranged.intersect.asset.stat_cat_entry.search.stat_cat',
753         api_level       => 1,
754         stream          => 1,
755         method          => 'multiranged_asset_stat_cat_entry',
756 );
757 __PACKAGE__->register_method(
758         api_name        => 'open-ils.storage.multiranged.union.asset.stat_cat_entry.search.stat_cat',
759         api_level       => 1,
760         stream          => 1,
761         method          => 'multiranged_asset_stat_cat_entry',
762 );
763
764
765 sub cn_ranged_tree {
766         my $self = shift;
767         my $client = shift;
768         my $cn = shift;
769         my $ou = shift;
770         my $depth = shift || 0;
771
772         my $ou_list =
773                 actor::org_unit
774                         ->db_Main
775                         ->selectcol_arrayref(
776                                 'SELECT id FROM actor.org_unit_descendants(?,?)',
777                                 {},
778                                 $ou,
779                                 $depth
780                         );
781
782         return undef unless ($ou_list and @$ou_list);
783
784         $cn = asset::call_number->retrieve( $cn );
785         return undef unless ($cn);
786
787         my $call_number = $cn->to_fieldmapper;
788         $call_number->copies([]);
789
790         $call_number->record( $cn->record->to_fieldmapper );
791         $call_number->record->fixed_fields( $cn->record->record_descriptor->next->to_fieldmapper );
792
793         for my $cp ( $cn->copies(circ_lib => $ou_list) ) {
794                 my $copy = $cp->to_fieldmapper;
795                 $copy->status( $cp->status->to_fieldmapper );
796                 $copy->location( $cp->status->to_fieldmapper );
797
798                 push @{ $call_number->copies }, $copy;
799         }
800
801         return $call_number;
802 }
803 __PACKAGE__->register_method(
804         api_name        => 'open-ils.storage.asset.call_number.ranged_tree',
805         method          => 'cn_ranged_tree',
806         argc            => 1,
807         api_level       => 1,
808 );
809
810
811 1;