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