]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/asset.pm
3a813ec5281068384a1e4e7e50891c027977888b
[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 sub copy_proximity {
393         my $self = shift;
394         my $client = shift;
395
396         my $cp = shift;
397         my $org = shift;
398
399         return unless ($cp && $org);
400
401         $cp = $cp->id if (ref $cp);
402         $cp = asset::copy->retrieve($cp);
403         return unless $copy;
404         my $ol = $cp->call_number->owning_lib;
405
406         return asset::copy->db_Main->selectcol_arrayref('SELECT actor.org_unit_proximity(?,?)',{},"$ol","$org")->[0];
407 }
408 __PACKAGE__->register_method(
409         method          => 'copy_proximity',
410         api_name        => 'open-ils.storage.asset.copy.proximity',
411         argc            => 2,
412         stream          => 1,
413 );
414
415 sub asset_copy_location_all {
416         my $self = shift;
417         my $client = shift;
418
419         for my $rec ( asset::copy_location->retrieve_all ) {
420                 $client->respond( $rec->to_fieldmapper );
421         }
422
423         return undef;
424 }
425 __PACKAGE__->register_method(
426         method          => 'asset_copy_location_all',
427         api_name        => 'open-ils.storage.direct.asset.copy_location.retrieve.all',
428         argc            => 0,
429         stream          => 1,
430 );
431
432 # XXX arg, with the descendancy SPs...
433 sub ranged_asset_copy_location {
434         my $self = shift;
435         my $client = shift;
436         my @binds = @_;
437         
438         my $ctable = asset::copy_location->table;
439         
440         my $descendants = defined($binds[1]) ?
441                 "actor.org_unit_full_path(?, ?)" :
442                 "actor.org_unit_full_path(?)" ;
443
444         
445         my $sql = <<"   SQL";
446                 SELECT  DISTINCT c.*
447                   FROM  $ctable c
448                         JOIN $descendants d
449                                 ON (d.id = c.owning_lib)
450         SQL
451         
452         my $sth = asset::copy_location->db_Main->prepare($sql);
453         $sth->execute(@binds);
454         
455         while ( my $rec = $sth->fetchrow_hashref ) {
456         
457                 my $cnct = new Fieldmapper::asset::copy_location;
458                 map {$cnct->$_($$rec{$_})} keys %$rec;
459                 $client->respond( $cnct );
460         }
461
462         return undef;
463 }
464 __PACKAGE__->register_method(
465         method          => 'ranged_asset_copy_location',
466         api_name        => 'open-ils.storage.ranged.asset.copy_location.retrieve',
467         argc            => 1,
468         stream          => 1,
469 );
470
471
472 sub fleshed_copy {
473         my $self = shift;
474         my $client = shift;
475         my @ids = @_;
476
477         return undef unless (@ids);
478
479         @ids = ($ids[0]) unless ($self->api_name =~ /batch/o);
480
481         for my $id ( @ids ) {
482                 next unless $id;
483                 my $cp = asset::copy->retrieve($id);
484                 next unless $cp;
485
486                 my $cp_fm = $cp->to_fieldmapper;
487                 $cp_fm->circ_lib( $cp->circ_lib->to_fieldmapper );
488                 $cp_fm->location( $cp->location->to_fieldmapper );
489                 $cp_fm->status( $cp->status->to_fieldmapper );
490                 $cp_fm->stat_cat_entries( [ map { $_->to_fieldmapper } $cp->stat_cat_entries ] );
491
492                 $client->respond( $cp_fm );
493         }
494
495         return undef;
496 }
497 __PACKAGE__->register_method(
498         api_name        => 'open-ils.storage.fleshed.asset.copy.batch.retrieve',
499         method          => 'fleshed_copy',
500         argc            => 1,
501         stream          => 1,
502 );
503 __PACKAGE__->register_method(
504         api_name        => 'open-ils.storage.fleshed.asset.copy.retrieve',
505         method          => 'fleshed_copy',
506         argc            => 1,
507 );
508
509 sub fleshed_copy_by_barcode {
510         my $self = shift;
511         my $client = shift;
512         my $bc = ''.shift;
513
514         my ($cp) = asset::copy->search( { barcode => $bc } );
515
516         return undef unless ($cp);
517
518         my $cp_fm = $cp->to_fieldmapper;
519         $cp_fm->circ_lib( $cp->circ_lib->to_fieldmapper );
520         $cp_fm->location( $cp->location->to_fieldmapper );
521         $cp_fm->status( $cp->status->to_fieldmapper );
522
523         return $cp_fm;
524 }       
525 __PACKAGE__->register_method(
526         api_name        => 'open-ils.storage.fleshed.asset.copy.search.barcode',
527         method          => 'fleshed_copy_by_barcode',
528         argc            => 1,
529         stream          => 1,
530 );
531
532
533 #XXX Fix stored proc calls
534 sub fleshed_asset_stat_cat {
535         my $self = shift;
536         my $client = shift;
537         my @list = @_;
538
539         @list = ($list[0]) unless ($self->api_name =~ /batch/o);
540         for my $sc (@list) {
541                 my $cat = asset::stat_cat->retrieve($sc);
542                 
543                 next unless ($cat);
544
545                 my $sc_fm = $cat->to_fieldmapper;
546                 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
547                 $client->respond( $sc_fm );
548         }
549
550         return undef;
551 }
552 __PACKAGE__->register_method(
553         api_name        => 'open-ils.storage.fleshed.asset.stat_cat.retrieve',
554         api_level       => 1,
555         method          => 'fleshed_asset_stat_cat',
556 );
557
558 __PACKAGE__->register_method(
559         api_name        => 'open-ils.storage.fleshed.asset.stat_cat.retrieve.batch',
560         api_level       => 1,
561         stream          => 1,
562         method          => 'fleshed_asset_stat_cat',
563 );
564
565
566 #XXX Fix stored proc calls
567 sub ranged_asset_stat_cat {
568         my $self = shift;
569         my $client = shift;
570         my $ou = ''.shift();
571
572         return undef unless ($ou);
573         my $s_table = asset::stat_cat->table;
574
575         my $select = <<"        SQL";
576                 SELECT  s.*
577                   FROM  $s_table s
578                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
579                   ORDER BY name
580         SQL
581
582         $fleshed = 0;
583         $fleshed = 1 if ($self->api_name =~ /fleshed/o);
584
585         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
586         $sth->execute($ou);
587
588         for my $sc ( map { asset::stat_cat->construct($_) } $sth->fetchall_hash ) {
589                 my $sc_fm = $sc->to_fieldmapper;
590                 $sc_fm->entries(
591                         [ $self->method_lookup( 'open-ils.storage.ranged.asset.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
592                 ) if ($fleshed);
593                 $client->respond( $sc_fm );
594         }
595
596         return undef;
597 }
598 __PACKAGE__->register_method(
599         api_name        => 'open-ils.storage.ranged.fleshed.asset.stat_cat.all',
600         api_level       => 1,
601         stream          => 1,
602         method          => 'ranged_asset_stat_cat',
603 );
604
605 __PACKAGE__->register_method(
606         api_name        => 'open-ils.storage.ranged.asset.stat_cat.all',
607         api_level       => 1,
608         stream          => 1,
609         method          => 'ranged_asset_stat_cat',
610 );
611
612
613 #XXX Fix stored proc calls
614 sub multiranged_asset_stat_cat {
615         my $self = shift;
616         my $client = shift;
617         my $ous = shift;
618
619         return undef unless (defined($ous) and @$ous);
620         my $s_table = asset::stat_cat->table;
621
622         my $select = <<"        SQL";
623                 SELECT  s.*
624                   FROM  $s_table s
625                   WHERE s.owner IN ( XXX )
626                   ORDER BY name
627         SQL
628
629         my $collector = ' INTERSECT ';
630         my $entry_method = 'open-ils.storage.multiranged.intersect.asset.stat_cat_entry.search.stat_cat';
631         if ($self->api_name =~ /union/o) {
632                 $collector = ' UNION ';
633                 $entry_method = 'open-ils.storage.multiranged.union.asset.stat_cat_entry.search.stat_cat';
634         }
635
636         my $binds = join($collector, map { 'SELECT id FROM actor.org_unit_full_path(?)' } grep {defined} @$ous);
637         $select =~ s/XXX/$binds/so;
638         
639         $fleshed = 0;
640         $fleshed = 1 if ($self->api_name =~ /fleshed/o);
641
642         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
643         $sth->execute(map { "$_" } grep {defined} @$ous);
644
645         for my $sc ( map { asset::stat_cat->construct($_) } $sth->fetchall_hash ) {
646                 my $sc_fm = $sc->to_fieldmapper;
647                 $sc_fm->entries(
648                         [ $self->method_lookup( $entry_method )->run($ous, $sc->id) ]
649                 ) if ($fleshed);
650                 $client->respond( $sc_fm );
651         }
652
653         return undef;
654 }
655 __PACKAGE__->register_method(
656         api_name        => 'open-ils.storage.multiranged.intersect.fleshed.asset.stat_cat.all',
657         api_level       => 1,
658         stream          => 1,
659         method          => 'multiranged_asset_stat_cat',
660 );
661 __PACKAGE__->register_method(
662         api_name        => 'open-ils.storage.multiranged.union.fleshed.asset.stat_cat.all',
663         api_level       => 1,
664         stream          => 1,
665         method          => 'multiranged_asset_stat_cat',
666 );
667
668 #XXX Fix stored proc calls
669 sub ranged_asset_stat_cat_entry {
670         my $self = shift;
671         my $client = shift;
672         my $ou = ''.shift();
673         my $sc = ''.shift();
674
675         return undef unless ($ou);
676         my $s_table = asset::stat_cat_entry->table;
677
678         my $select = <<"        SQL";
679                 SELECT  s.*
680                   FROM  $s_table s
681                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
682                   WHERE stat_cat = ?
683                   ORDER BY name
684         SQL
685
686         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
687         $sth->execute($ou,$sc);
688
689         for my $sce ( map { asset::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
690                 $client->respond( $sce->to_fieldmapper );
691         }
692
693         return undef;
694 }
695 __PACKAGE__->register_method(
696         api_name        => 'open-ils.storage.ranged.asset.stat_cat_entry.search.stat_cat',
697         api_level       => 1,
698         stream          => 1,
699         method          => 'ranged_asset_stat_cat_entry',
700 );
701
702 #XXX Fix stored proc calls
703 sub multiranged_asset_stat_cat_entry {
704         my $self = shift;
705         my $client = shift;
706         my $ous = shift;
707         my $sc = ''.shift();
708
709         return undef unless (defined($ous) and @$ous);
710         my $s_table = asset::stat_cat_entry->table;
711
712         my $collector = ' INTERSECT ';
713         $collector = ' UNION ' if ($self->api_name =~ /union/o);
714
715         my $select = <<"        SQL";
716                 SELECT  s.*
717                   FROM  $s_table s
718                   WHERE s.owner IN ( XXX ) and s.stat_cat = ?
719                   ORDER BY value
720         SQL
721
722         my $binds = join($collector, map { 'SELECT id FROM actor.org_unit_full_path(?)' } grep {defined} @$ous);
723         $select =~ s/XXX/$binds/so;
724         
725         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
726         $sth->execute(map {"$_"} @$ous,$sc);
727
728         for my $sce ( map { asset::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
729                 $client->respond( $sce->to_fieldmapper );
730         }
731
732         return undef;
733 }
734 __PACKAGE__->register_method(
735         api_name        => 'open-ils.storage.multiranged.intersect.asset.stat_cat_entry.search.stat_cat',
736         api_level       => 1,
737         stream          => 1,
738         method          => 'multiranged_asset_stat_cat_entry',
739 );
740 __PACKAGE__->register_method(
741         api_name        => 'open-ils.storage.multiranged.union.asset.stat_cat_entry.search.stat_cat',
742         api_level       => 1,
743         stream          => 1,
744         method          => 'multiranged_asset_stat_cat_entry',
745 );
746
747
748 sub cn_ranged_tree {
749         my $self = shift;
750         my $client = shift;
751         my $cn = shift;
752         my $ou = shift;
753         my $depth = shift || 0;
754
755         my $ou_list =
756                 actor::org_unit
757                         ->db_Main
758                         ->selectcol_arrayref(
759                                 'SELECT id FROM actor.org_unit_descendants(?,?)',
760                                 {},
761                                 $ou,
762                                 $depth
763                         );
764
765         return undef unless ($ou_list and @$ou_list);
766
767         $cn = asset::call_number->retrieve( $cn );
768         return undef unless ($cn);
769
770         my $call_number = $cn->to_fieldmapper;
771         $call_number->copies([]);
772
773         $call_number->record( $cn->record->to_fieldmapper );
774         $call_number->record->fixed_fields( $cn->record->record_descriptor->next->to_fieldmapper );
775
776         for my $cp ( $cn->copies(circ_lib => $ou_list) ) {
777                 my $copy = $cp->to_fieldmapper;
778                 $copy->status( $cp->status->to_fieldmapper );
779                 $copy->location( $cp->status->to_fieldmapper );
780
781                 push @{ $call_number->copies }, $copy;
782         }
783
784         return $call_number;
785 }
786 __PACKAGE__->register_method(
787         api_name        => 'open-ils.storage.asset.call_number.ranged_tree',
788         method          => 'cn_ranged_tree',
789         argc            => 1,
790         api_level       => 1,
791 );
792
793
794 1;