]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/asset.pm
Whitespace. gah.
[working/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 (oils_text_as_bytea(label) > ? or ( cn.id > ? and oils_text_as_bytea(label) = ? ))
234                         and owning_lib in ($orgs)
235                 order by oils_text_as_bytea(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 (oils_text_as_bytea(label) < ? or ( cn.id < ? and oils_text_as_bytea(label) = ? ))
289                                 and owning_lib in ($orgs)
290                         order by oils_text_as_bytea(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 oils_text_as_bytea(label) < ?
347                                 and owning_lib in ($orgs)
348                         order by oils_text_as_bytea(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 oils_text_as_bytea(label) >= ?
365                         and owning_lib in ($orgs)
366                 order by oils_text_as_bytea(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                  ORDER BY name
453         SQL
454         
455         my $sth = asset::copy_location->db_Main->prepare($sql);
456         $sth->execute(@binds);
457         
458         while ( my $rec = $sth->fetchrow_hashref ) {
459         
460                 my $cnct = new Fieldmapper::asset::copy_location;
461                 map {$cnct->$_($$rec{$_})} keys %$rec;
462                 $client->respond( $cnct );
463         }
464
465         return undef;
466 }
467 __PACKAGE__->register_method(
468         method          => 'ranged_asset_copy_location',
469         api_name        => 'open-ils.storage.ranged.asset.copy_location.retrieve',
470         argc            => 1,
471         stream          => 1,
472 );
473
474
475 sub fleshed_copy {
476         my $self = shift;
477         my $client = shift;
478         my @ids = @_;
479
480         return undef unless (@ids);
481
482         @ids = ($ids[0]) unless ($self->api_name =~ /batch/o);
483
484         for my $id ( @ids ) {
485                 next unless $id;
486                 my $cp = asset::copy->retrieve($id);
487                 next unless $cp;
488
489                 my $cp_fm = $cp->to_fieldmapper;
490                 $cp_fm->circ_lib( $cp->circ_lib->to_fieldmapper );
491                 $cp_fm->location( $cp->location->to_fieldmapper );
492                 $cp_fm->status( $cp->status->to_fieldmapper );
493                 $cp_fm->stat_cat_entries( [ map { $_->to_fieldmapper } $cp->stat_cat_entries ] );
494
495                 $client->respond( $cp_fm );
496         }
497
498         return undef;
499 }
500 __PACKAGE__->register_method(
501         api_name        => 'open-ils.storage.fleshed.asset.copy.batch.retrieve',
502         method          => 'fleshed_copy',
503         argc            => 1,
504         stream          => 1,
505 );
506 __PACKAGE__->register_method(
507         api_name        => 'open-ils.storage.fleshed.asset.copy.retrieve',
508         method          => 'fleshed_copy',
509         argc            => 1,
510 );
511
512 sub fleshed_copy_by_barcode {
513         my $self = shift;
514         my $client = shift;
515         my $bc = ''.shift;
516
517         my ($cp) = asset::copy->search( { barcode => $bc } );
518
519         return undef unless ($cp);
520
521         my $cp_fm = $cp->to_fieldmapper;
522         $cp_fm->circ_lib( $cp->circ_lib->to_fieldmapper );
523         $cp_fm->location( $cp->location->to_fieldmapper );
524         $cp_fm->status( $cp->status->to_fieldmapper );
525
526         return $cp_fm;
527 }       
528 __PACKAGE__->register_method(
529         api_name        => 'open-ils.storage.fleshed.asset.copy.search.barcode',
530         method          => 'fleshed_copy_by_barcode',
531         argc            => 1,
532         stream          => 1,
533 );
534
535
536 #XXX Fix stored proc calls
537 sub fleshed_asset_stat_cat {
538         my $self = shift;
539         my $client = shift;
540         my @list = @_;
541
542         @list = ($list[0]) unless ($self->api_name =~ /batch/o);
543         for my $sc (@list) {
544                 my $cat = asset::stat_cat->retrieve($sc);
545                 
546                 next unless ($cat);
547
548                 my $sc_fm = $cat->to_fieldmapper;
549                 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
550                 $client->respond( $sc_fm );
551         }
552
553         return undef;
554 }
555 __PACKAGE__->register_method(
556         api_name        => 'open-ils.storage.fleshed.asset.stat_cat.retrieve',
557         api_level       => 1,
558         method          => 'fleshed_asset_stat_cat',
559 );
560
561 __PACKAGE__->register_method(
562         api_name        => 'open-ils.storage.fleshed.asset.stat_cat.retrieve.batch',
563         api_level       => 1,
564         stream          => 1,
565         method          => 'fleshed_asset_stat_cat',
566 );
567
568
569 #XXX Fix stored proc calls
570 sub ranged_asset_stat_cat {
571         my $self = shift;
572         my $client = shift;
573         my $ou = ''.shift();
574
575         return undef unless ($ou);
576         my $s_table = asset::stat_cat->table;
577
578         my $select = <<"        SQL";
579                 SELECT  s.*
580                   FROM  $s_table s
581                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
582                   ORDER BY name
583         SQL
584
585         $fleshed = 0;
586         $fleshed = 1 if ($self->api_name =~ /fleshed/o);
587
588         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
589         $sth->execute($ou);
590
591         for my $sc ( map { asset::stat_cat->construct($_) } $sth->fetchall_hash ) {
592                 my $sc_fm = $sc->to_fieldmapper;
593                 $sc_fm->entries(
594                         [ $self->method_lookup( 'open-ils.storage.ranged.asset.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
595                 ) if ($fleshed);
596                 $client->respond( $sc_fm );
597         }
598
599         return undef;
600 }
601 __PACKAGE__->register_method(
602         api_name        => 'open-ils.storage.ranged.fleshed.asset.stat_cat.all',
603         api_level       => 1,
604         stream          => 1,
605         method          => 'ranged_asset_stat_cat',
606 );
607
608 __PACKAGE__->register_method(
609         api_name        => 'open-ils.storage.ranged.asset.stat_cat.all',
610         api_level       => 1,
611         stream          => 1,
612         method          => 'ranged_asset_stat_cat',
613 );
614
615
616 #XXX Fix stored proc calls
617 sub multiranged_asset_stat_cat {
618         my $self = shift;
619         my $client = shift;
620         my $ous = shift;
621
622         return undef unless (defined($ous) and @$ous);
623         my $s_table = asset::stat_cat->table;
624
625         my $select = <<"        SQL";
626                 SELECT  s.*
627                   FROM  $s_table s
628                   WHERE s.owner IN ( XXX )
629                   ORDER BY name
630         SQL
631
632         my $collector = ' INTERSECT ';
633         my $entry_method = 'open-ils.storage.multiranged.intersect.asset.stat_cat_entry.search.stat_cat';
634         if ($self->api_name =~ /union/o) {
635                 $collector = ' UNION ';
636                 $entry_method = 'open-ils.storage.multiranged.union.asset.stat_cat_entry.search.stat_cat';
637         }
638
639         my $binds = join($collector, map { 'SELECT id FROM actor.org_unit_full_path(?)' } grep {defined} @$ous);
640         $select =~ s/XXX/$binds/so;
641         
642         $fleshed = 0;
643         $fleshed = 1 if ($self->api_name =~ /fleshed/o);
644
645         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
646         $sth->execute(map { "$_" } grep {defined} @$ous);
647
648         for my $sc ( map { asset::stat_cat->construct($_) } $sth->fetchall_hash ) {
649                 my $sc_fm = $sc->to_fieldmapper;
650                 $sc_fm->entries(
651                         [ $self->method_lookup( $entry_method )->run($ous, $sc->id) ]
652                 ) if ($fleshed);
653                 $client->respond( $sc_fm );
654         }
655
656         return undef;
657 }
658 __PACKAGE__->register_method(
659         api_name        => 'open-ils.storage.multiranged.intersect.fleshed.asset.stat_cat.all',
660         api_level       => 1,
661         stream          => 1,
662         method          => 'multiranged_asset_stat_cat',
663 );
664 __PACKAGE__->register_method(
665         api_name        => 'open-ils.storage.multiranged.union.fleshed.asset.stat_cat.all',
666         api_level       => 1,
667         stream          => 1,
668         method          => 'multiranged_asset_stat_cat',
669 );
670
671 #XXX Fix stored proc calls
672 sub ranged_asset_stat_cat_entry {
673         my $self = shift;
674         my $client = shift;
675         my $ou = ''.shift();
676         my $sc = ''.shift();
677
678         return undef unless ($ou);
679         my $s_table = asset::stat_cat_entry->table;
680
681         my $select = <<"        SQL";
682                 SELECT  s.*
683                   FROM  $s_table s
684                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
685                   WHERE stat_cat = ?
686                   ORDER BY name
687         SQL
688
689         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
690         $sth->execute($ou,$sc);
691
692         for my $sce ( map { asset::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
693                 $client->respond( $sce->to_fieldmapper );
694         }
695
696         return undef;
697 }
698 __PACKAGE__->register_method(
699         api_name        => 'open-ils.storage.ranged.asset.stat_cat_entry.search.stat_cat',
700         api_level       => 1,
701         stream          => 1,
702         method          => 'ranged_asset_stat_cat_entry',
703 );
704
705 #XXX Fix stored proc calls
706 sub multiranged_asset_stat_cat_entry {
707         my $self = shift;
708         my $client = shift;
709         my $ous = shift;
710         my $sc = ''.shift();
711
712         return undef unless (defined($ous) and @$ous);
713         my $s_table = asset::stat_cat_entry->table;
714
715         my $collector = ' INTERSECT ';
716         $collector = ' UNION ' if ($self->api_name =~ /union/o);
717
718         my $select = <<"        SQL";
719                 SELECT  s.*
720                   FROM  $s_table s
721                   WHERE s.owner IN ( XXX ) and s.stat_cat = ?
722                   ORDER BY value
723         SQL
724
725         my $binds = join($collector, map { 'SELECT id FROM actor.org_unit_full_path(?)' } grep {defined} @$ous);
726         $select =~ s/XXX/$binds/so;
727         
728         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
729         $sth->execute(map {"$_"} @$ous,$sc);
730
731         for my $sce ( map { asset::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
732                 $client->respond( $sce->to_fieldmapper );
733         }
734
735         return undef;
736 }
737 __PACKAGE__->register_method(
738         api_name        => 'open-ils.storage.multiranged.intersect.asset.stat_cat_entry.search.stat_cat',
739         api_level       => 1,
740         stream          => 1,
741         method          => 'multiranged_asset_stat_cat_entry',
742 );
743 __PACKAGE__->register_method(
744         api_name        => 'open-ils.storage.multiranged.union.asset.stat_cat_entry.search.stat_cat',
745         api_level       => 1,
746         stream          => 1,
747         method          => 'multiranged_asset_stat_cat_entry',
748 );
749
750
751 sub cn_ranged_tree {
752         my $self = shift;
753         my $client = shift;
754         my $cn = shift;
755         my $ou = shift;
756         my $depth = shift || 0;
757
758         my $ou_list =
759                 actor::org_unit
760                         ->db_Main
761                         ->selectcol_arrayref(
762                                 'SELECT id FROM actor.org_unit_descendants(?,?)',
763                                 {},
764                                 $ou,
765                                 $depth
766                         );
767
768         return undef unless ($ou_list and @$ou_list);
769
770         $cn = asset::call_number->retrieve( $cn );
771         return undef unless ($cn);
772         return undef if ($cn->deleted);
773
774         my $call_number = $cn->to_fieldmapper;
775         $call_number->copies([]);
776
777         $call_number->record( $cn->record->to_fieldmapper );
778         $call_number->record->fixed_fields( $cn->record->record_descriptor->next->to_fieldmapper );
779
780         for my $cp ( $cn->copies(circ_lib => $ou_list) ) {
781                 next if ($cp->deleted);
782                 my $copy = $cp->to_fieldmapper;
783                 $copy->status( $cp->status->to_fieldmapper );
784                 $copy->location( $cp->location->to_fieldmapper );
785
786                 push @{ $call_number->copies }, $copy;
787         }
788
789         return $call_number;
790 }
791 __PACKAGE__->register_method(
792         api_name        => 'open-ils.storage.asset.call_number.ranged_tree',
793         method          => 'cn_ranged_tree',
794         argc            => 1,
795         api_level       => 1,
796 );
797
798
799 # XXX Since this is all we need in open-ils.storage for serial stuff ATM, just
800 # XXX putting it here instead of creating a whole new file.
801 sub issuance_ranged_tree {
802     my $self = shift;
803     my $client = shift;
804     my $iss = shift;
805     my $ou = shift;
806     my $depth = shift || 0;
807
808     my $ou_list =
809         actor::org_unit
810             ->db_Main
811             ->selectcol_arrayref(
812                 'SELECT id FROM actor.org_unit_descendants(?,?)',
813                 {},
814                 $ou,
815                 $depth
816             );
817
818     return undef unless ($ou_list and @$ou_list);
819
820     $iss = serial::issuance->retrieve( $iss );
821     return undef unless ($iss);
822
823     my $issuance = $iss->to_fieldmapper;
824     $issuance->items([]);
825
826     # Now, gather issuances on the same bib, with the same label and date_published ...
827     my @subs = map { $_->id } serial::subscription->search( record_entry => $iss->subscription->record_entry->id );
828
829     my @similar_iss = serial::issuance->search_where(
830         subscription => \@subs,
831         label => $iss->label,
832         date_published => $iss->date_published
833     );
834
835     # ... and add all /their/ items to the target issuance
836     for my $i ( @similar_iss ) {
837         for my $it ( $i->items() ) {
838             next unless $it->unit and not $it->unit->deleted;
839             next unless (grep { $it->unit->circ_lib eq $_ } @$ou_list);
840     
841             my $unit = $it->unit->to_fieldmapper;
842             $unit->status( $it->unit->status->to_fieldmapper );
843             $unit->location( $it->unit->location->to_fieldmapper );
844
845             my $item = $it->to_fieldmapper;
846             $item->unit( $unit );
847     
848             push @{ $issuance->items }, $item;
849         }
850     }
851
852     return $issuance;
853 }
854 __PACKAGE__->register_method(
855     api_name    => 'open-ils.storage.serial.issuance.ranged_tree',
856     method      => 'issuance_ranged_tree',
857     argc        => 1,
858     api_level   => 1,
859 );
860
861 sub merge_record_assets {
862         my $self = shift;
863         my $client = shift;
864         my $target = shift;
865         my @sources = @_;
866
867         my $count = 0;
868         for my $source ( @sources ) {
869                 $count += asset::call_number
870                                 ->db_Main
871                                 ->selectcol_arrayref(
872                                         "SELECT asset.merge_record_assets(?,?);",
873                                         {},
874                                         $target,
875                                         $source
876                                 )->[0];
877         }
878
879         return $count;
880 }
881 __PACKAGE__->register_method(
882         api_name        => 'open-ils.storage.asset.merge_record_assets',
883         method          => 'merge_record_assets',
884         argc            => 2,
885         api_level       => 1,
886 );
887
888 1;