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