]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Serial.pm
Serials: an alternative batch receiving interface, to support certain
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Serial.pm
1 #!/usr/bin/perl
2
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16
17 =head1 NAME
18
19 OpenILS::Application::Serial - Performs serials-related tasks such as receiving issues and generating predictions
20
21 =head1 SYNOPSIS
22
23 TBD
24
25 =head1 DESCRIPTION
26
27 TBD
28
29 =head1 AUTHOR
30
31 Dan Wells, dbw2@calvin.edu
32
33 =cut
34
35 package OpenILS::Application::Serial;
36
37 use strict;
38 use warnings;
39
40 use OpenILS::Application;
41 use base qw/OpenILS::Application/;
42 use OpenILS::Application::AppUtils;
43 use OpenILS::Event;
44 use OpenSRF::AppSession;
45 use OpenSRF::Utils qw/:datetime/;
46 use OpenSRF::Utils::Logger qw/:logger/;
47 use OpenILS::Utils::CStoreEditor q/:funcs/;
48 use OpenILS::Utils::Fieldmapper;
49 use OpenILS::Utils::MFHD;
50 use MARC::File::XML (BinaryEncoding => 'utf8');
51 my $U = 'OpenILS::Application::AppUtils';
52 my @MFHD_NAMES = ('basic','supplement','index');
53 my %MFHD_NAMES_BY_TAG = (  '853' => $MFHD_NAMES[0],
54                         '863' => $MFHD_NAMES[0],
55                         '854' => $MFHD_NAMES[1],
56                         '864' => $MFHD_NAMES[1],
57                         '855' => $MFHD_NAMES[2],
58                         '865' => $MFHD_NAMES[2] );
59 my %MFHD_TAGS_BY_NAME = (  $MFHD_NAMES[0] => '853',
60                         $MFHD_NAMES[1] => '854',
61                         $MFHD_NAMES[2] => '855');
62
63 # helper method for conforming dates to ISO8601
64 sub _cleanse_dates {
65     my $item = shift;
66     my $fields = shift;
67
68     foreach my $field (@$fields) {
69         $item->$field(OpenSRF::Utils::clense_ISO8601($item->$field)) if $item->$field;
70     }
71     return 0;
72 }
73
74 sub _get_mvr {
75     $U->simplereq(
76         "open-ils.search",
77         "open-ils.search.biblio.record.mods_slim.retrieve",
78         @_
79     );
80 }
81
82
83 ##########################################################################
84 # item methods
85 #
86 __PACKAGE__->register_method(
87     method    => 'fleshed_item_alter',
88     api_name  => 'open-ils.serial.item.fleshed.batch.update',
89     api_level => 1,
90     argc      => 2,
91     signature => {
92         desc     => 'Receives an array of one or more items and updates the database as needed',
93         'params' => [ {
94                  name => 'authtoken',
95                  desc => 'Authtoken for current user session',
96                  type => 'string'
97             },
98             {
99                  name => 'items',
100                  desc => 'Array of fleshed items',
101                  type => 'array'
102             }
103
104         ],
105         'return' => {
106             desc => 'Returns 1 if successful, event if failed',
107             type => 'mixed'
108         }
109     }
110 );
111
112 sub fleshed_item_alter {
113     my( $self, $conn, $auth, $items ) = @_;
114     return 1 unless ref $items;
115     my( $reqr, $evt ) = $U->checkses($auth);
116     return $evt if $evt;
117     my $editor = new_editor(requestor => $reqr, xact => 1);
118     my $override = $self->api_name =~ /override/;
119
120 # TODO: permission check
121 #        return $editor->event unless
122 #            $editor->allowed('UPDATE_COPY', $class->copy_perm_org($vol, $copy));
123
124     for my $item (@$items) {
125
126         my $itemid = $item->id;
127         $item->editor($editor->requestor->id);
128         $item->edit_date('now');
129
130         if( $item->isdeleted ) {
131             $evt = _delete_sitem( $editor, $override, $item);
132         } elsif( $item->isnew ) {
133             # TODO: reconsider this
134             # if the item has a new issuance, create the issuance first
135             if (ref $item->issuance eq 'Fieldmapper::serial::issuance' and $item->issuance->isnew) {
136                 fleshed_issuance_alter($self, $conn, $auth, [$item->issuance]);
137             }
138             _cleanse_dates($item, ['date_expected','date_received']);
139             $evt = _create_sitem( $editor, $item );
140         } else {
141             _cleanse_dates($item, ['date_expected','date_received']);
142             $evt = _update_sitem( $editor, $override, $item );
143         }
144     }
145
146     if( $evt ) {
147         $logger->info("fleshed item-alter failed with event: ".OpenSRF::Utils::JSON->perl2JSON($evt));
148         $editor->rollback;
149         return $evt;
150     }
151     $logger->debug("item-alter: done updating item batch");
152     $editor->commit;
153     $logger->info("fleshed item-alter successfully updated ".scalar(@$items)." items");
154     return 1;
155 }
156
157 sub _delete_sitem {
158     my ($editor, $override, $item) = @_;
159     $logger->info("item-alter: delete item ".OpenSRF::Utils::JSON->perl2JSON($item));
160     return $editor->event unless $editor->delete_serial_item($item);
161     return 0;
162 }
163
164 sub _create_sitem {
165     my ($editor, $item) = @_;
166
167     $item->creator($editor->requestor->id);
168     $item->create_date('now');
169
170     $logger->info("item-alter: new item ".OpenSRF::Utils::JSON->perl2JSON($item));
171     return $editor->event unless $editor->create_serial_item($item);
172     return 0;
173 }
174
175 sub _update_sitem {
176     my ($editor, $override, $item) = @_;
177
178     $logger->info("item-alter: retrieving item ".$item->id);
179     my $orig_item = $editor->retrieve_serial_item($item->id);
180
181     $logger->info("item-alter: original item ".OpenSRF::Utils::JSON->perl2JSON($orig_item));
182     $logger->info("item-alter: updated item ".OpenSRF::Utils::JSON->perl2JSON($item));
183     return $editor->event unless $editor->update_serial_item($item);
184     return 0;
185 }
186
187 __PACKAGE__->register_method(
188     method  => "fleshed_serial_item_retrieve_batch",
189     authoritative => 1,
190     api_name    => "open-ils.serial.item.fleshed.batch.retrieve"
191 );
192
193 sub fleshed_serial_item_retrieve_batch {
194     my( $self, $client, $ids ) = @_;
195 # FIXME: permissions?
196     $logger->info("Fetching fleshed serial items @$ids");
197     return $U->cstorereq(
198         "open-ils.cstore.direct.serial.item.search.atomic",
199         { id => $ids },
200         { flesh => 2,
201           flesh_fields => {sitem => [ qw/issuance creator editor stream unit notes/ ], sstr => ["distribution"], sunit => ["call_number"], siss => [qw/creator editor subscription/]}
202         });
203 }
204
205
206 ##########################################################################
207 # issuance methods
208 #
209 __PACKAGE__->register_method(
210     method    => 'fleshed_issuance_alter',
211     api_name  => 'open-ils.serial.issuance.fleshed.batch.update',
212     api_level => 1,
213     argc      => 2,
214     signature => {
215         desc     => 'Receives an array of one or more issuances and updates the database as needed',
216         'params' => [ {
217                  name => 'authtoken',
218                  desc => 'Authtoken for current user session',
219                  type => 'string'
220             },
221             {
222                  name => 'issuances',
223                  desc => 'Array of fleshed issuances',
224                  type => 'array'
225             }
226
227         ],
228         'return' => {
229             desc => 'Returns 1 if successful, event if failed',
230             type => 'mixed'
231         }
232     }
233 );
234
235 sub fleshed_issuance_alter {
236     my( $self, $conn, $auth, $issuances ) = @_;
237     return 1 unless ref $issuances;
238     my( $reqr, $evt ) = $U->checkses($auth);
239     return $evt if $evt;
240     my $editor = new_editor(requestor => $reqr, xact => 1);
241     my $override = $self->api_name =~ /override/;
242
243 # TODO: permission support
244 #        return $editor->event unless
245 #            $editor->allowed('UPDATE_COPY', $class->copy_perm_org($vol, $copy));
246
247     for my $issuance (@$issuances) {
248         my $issuanceid = $issuance->id;
249         $issuance->editor($editor->requestor->id);
250         $issuance->edit_date('now');
251
252         if( $issuance->isdeleted ) {
253             $evt = _delete_siss( $editor, $override, $issuance);
254         } elsif( $issuance->isnew ) {
255             _cleanse_dates($issuance, ['date_published']);
256             $evt = _create_siss( $editor, $issuance );
257         } else {
258             _cleanse_dates($issuance, ['date_published']);
259             $evt = _update_siss( $editor, $override, $issuance );
260         }
261     }
262
263     if( $evt ) {
264         $logger->info("fleshed issuance-alter failed with event: ".OpenSRF::Utils::JSON->perl2JSON($evt));
265         $editor->rollback;
266         return $evt;
267     }
268     $logger->debug("issuance-alter: done updating issuance batch");
269     $editor->commit;
270     $logger->info("fleshed issuance-alter successfully updated ".scalar(@$issuances)." issuances");
271     return 1;
272 }
273
274 sub _delete_siss {
275     my ($editor, $override, $issuance) = @_;
276     $logger->info("issuance-alter: delete issuance ".OpenSRF::Utils::JSON->perl2JSON($issuance));
277     return $editor->event unless $editor->delete_serial_issuance($issuance);
278     return 0;
279 }
280
281 sub _create_siss {
282     my ($editor, $issuance) = @_;
283
284     $issuance->creator($editor->requestor->id);
285     $issuance->create_date('now');
286
287     $logger->info("issuance-alter: new issuance ".OpenSRF::Utils::JSON->perl2JSON($issuance));
288     return $editor->event unless $editor->create_serial_issuance($issuance);
289     return 0;
290 }
291
292 sub _update_siss {
293     my ($editor, $override, $issuance) = @_;
294
295     $logger->info("issuance-alter: retrieving issuance ".$issuance->id);
296     my $orig_issuance = $editor->retrieve_serial_issuance($issuance->id);
297
298     $logger->info("issuance-alter: original issuance ".OpenSRF::Utils::JSON->perl2JSON($orig_issuance));
299     $logger->info("issuance-alter: updated issuance ".OpenSRF::Utils::JSON->perl2JSON($issuance));
300     return $editor->event unless $editor->update_serial_issuance($issuance);
301     return 0;
302 }
303
304 __PACKAGE__->register_method(
305     method  => "fleshed_serial_issuance_retrieve_batch",
306     authoritative => 1,
307     api_name    => "open-ils.serial.issuance.fleshed.batch.retrieve"
308 );
309
310 sub fleshed_serial_issuance_retrieve_batch {
311     my( $self, $client, $ids ) = @_;
312 # FIXME: permissions?
313     $logger->info("Fetching fleshed serial issuances @$ids");
314     return $U->cstorereq(
315         "open-ils.cstore.direct.serial.issuance.search.atomic",
316         { id => $ids },
317         { flesh => 1,
318           flesh_fields => {siss => [ qw/creator editor subscription/ ]}
319         });
320 }
321
322
323 ##########################################################################
324 # unit methods
325 #
326 __PACKAGE__->register_method(
327     method    => 'fleshed_sunit_alter',
328     api_name  => 'open-ils.serial.sunit.fleshed.batch.update',
329     api_level => 1,
330     argc      => 2,
331     signature => {
332         desc     => 'Receives an array of one or more Units and updates the database as needed',
333         'params' => [ {
334                  name => 'authtoken',
335                  desc => 'Authtoken for current user session',
336                  type => 'string'
337             },
338             {
339                  name => 'sunits',
340                  desc => 'Array of fleshed Units',
341                  type => 'array'
342             }
343
344         ],
345         'return' => {
346             desc => 'Returns 1 if successful, event if failed',
347             type => 'mixed'
348         }
349     }
350 );
351
352 sub fleshed_sunit_alter {
353     my( $self, $conn, $auth, $sunits ) = @_;
354     return 1 unless ref $sunits;
355     my( $reqr, $evt ) = $U->checkses($auth);
356     return $evt if $evt;
357     my $editor = new_editor(requestor => $reqr, xact => 1);
358     my $override = $self->api_name =~ /override/;
359
360 # TODO: permission support
361 #        return $editor->event unless
362 #            $editor->allowed('UPDATE_COPY', $class->copy_perm_org($vol, $copy));
363
364     for my $sunit (@$sunits) {
365         if( $sunit->isdeleted ) {
366             $evt = _delete_sunit( $editor, $override, $sunit );
367         } else {
368             $sunit->default_location( $sunit->default_location->id ) if ref $sunit->default_location;
369
370             if( $sunit->isnew ) {
371                 $evt = _create_sunit( $editor, $sunit );
372             } else {
373                 $evt = _update_sunit( $editor, $override, $sunit );
374             }
375         }
376     }
377
378     if( $evt ) {
379         $logger->info("fleshed sunit-alter failed with event: ".OpenSRF::Utils::JSON->perl2JSON($evt));
380         $editor->rollback;
381         return $evt;
382     }
383     $logger->debug("sunit-alter: done updating sunit batch");
384     $editor->commit;
385     $logger->info("fleshed sunit-alter successfully updated ".scalar(@$sunits)." Units");
386     return 1;
387 }
388
389 sub _delete_sunit {
390     my ($editor, $override, $sunit) = @_;
391     $logger->info("sunit-alter: delete sunit ".OpenSRF::Utils::JSON->perl2JSON($sunit));
392     return $editor->event unless $editor->delete_serial_unit($sunit);
393     return 0;
394 }
395
396 sub _create_sunit {
397     my ($editor, $sunit) = @_;
398
399     $logger->info("sunit-alter: new Unit ".OpenSRF::Utils::JSON->perl2JSON($sunit));
400     return $editor->event unless $editor->create_serial_unit($sunit);
401     return 0;
402 }
403
404 sub _update_sunit {
405     my ($editor, $override, $sunit) = @_;
406
407     $logger->info("sunit-alter: retrieving sunit ".$sunit->id);
408     my $orig_sunit = $editor->retrieve_serial_unit($sunit->id);
409
410     $logger->info("sunit-alter: original sunit ".OpenSRF::Utils::JSON->perl2JSON($orig_sunit));
411     $logger->info("sunit-alter: updated sunit ".OpenSRF::Utils::JSON->perl2JSON($sunit));
412     return $editor->event unless $editor->update_serial_unit($sunit);
413     return 0;
414 }
415
416 __PACKAGE__->register_method(
417         method  => "retrieve_unit_list",
418     authoritative => 1,
419         api_name        => "open-ils.serial.unit_list.retrieve"
420 );
421
422 sub retrieve_unit_list {
423
424         my( $self, $client, @sdist_ids ) = @_;
425
426         if(ref($sdist_ids[0])) { @sdist_ids = @{$sdist_ids[0]}; }
427
428         my $e = new_editor();
429
430     my $query = {
431         'select' => 
432             { 'sunit' => [ 'id', 'summary_contents', 'sort_key' ],
433               'sitem' => ['stream'],
434               'sstr' => ['distribution'],
435               'sdist' => [{'column' => 'label', 'alias' => 'sdist_label'}]
436             },
437         'from' =>
438             { 'sdist' =>
439                 { 'sstr' =>
440                     { 'join' =>
441                         { 'sitem' =>
442                             { 'join' => { 'sunit' => {} } }
443                         }
444                     }
445                 }
446             },
447         'distinct' => 'true',
448         'where' => { '+sdist' => {'id' => \@sdist_ids} },
449         'order_by' => [{'class' => 'sunit', 'field' => 'sort_key'}]
450     };
451
452     my $unit_list_entries = $e->json_query($query);
453     
454     my @entries;
455     foreach my $entry (@$unit_list_entries) {
456         my $value = {'sunit' => $entry->{id}, 'sstr' => $entry->{stream}, 'sdist' => $entry->{distribution}};
457         my $label = $entry->{summary_contents};
458         if (length($label) > 100) {
459             $label = substr($label, 0, 100) . '...'; # limited space in dropdown / menu
460         }
461         $label = "[$entry->{sdist_label}/$entry->{stream} #$entry->{id}] " . $label;
462         push (@entries, [$label, OpenSRF::Utils::JSON->perl2JSON($value)]);
463     }
464
465     return \@entries;
466 }
467
468
469
470 ##########################################################################
471 # predict and receive methods
472 #
473 __PACKAGE__->register_method(
474     method    => 'make_predictions',
475     api_name  => 'open-ils.serial.make_predictions',
476     api_level => 1,
477     argc      => 1,
478     signature => {
479         desc     => 'Receives an ssub id and populates the issuance and item tables',
480         'params' => [ {
481                  name => 'ssub_id',
482                  desc => 'Serial Subscription ID',
483                  type => 'int'
484             }
485         ]
486     }
487 );
488
489 sub make_predictions {
490     my ($self, $conn, $authtoken, $args) = @_;
491
492     my $editor = OpenILS::Utils::CStoreEditor->new();
493     my $ssub_id = $args->{ssub_id};
494     my $mfhd = MFHD->new(MARC::Record->new());
495
496     my $ssub = $editor->retrieve_serial_subscription([$ssub_id]);
497     my $scaps = $editor->search_serial_caption_and_pattern({ subscription => $ssub_id, active => 't'});
498     my $sdists = $editor->search_serial_distribution( [{ subscription => $ssub->id }, {  flesh => 1,
499               flesh_fields => {sdist => [ qw/ streams / ]}, limit => 1 }] ); #TODO: 'deleted' support?
500
501     my @predictions;
502     my $link_id = 1;
503     foreach my $scap (@$scaps) {
504         my $caption_field = _revive_caption($scap);
505         $caption_field->update('8' => $link_id);
506         $mfhd->append_fields($caption_field);
507         my $options = {
508                 'caption' => $caption_field,
509                 'scap_id' => $scap->id,
510                 'num_to_predict' => $args->{num_to_predict}
511                 };
512         if ($args->{base_issuance}) { # predict from a given issuance
513             $options->{predict_from} = _revive_holding($args->{base_issuance}->holding_code, $caption_field, 1); # fresh MFHD Record, so we simply default to 1 for seqno
514         } else { # default to predicting from last published
515             my $last_published = $editor->search_serial_issuance([
516                     {'caption_and_pattern' => $scap->id,
517                     'subscription' => $ssub_id},
518                 {limit => 1, order_by => { siss => "date_published DESC" }}]
519                 );
520             if ($last_published->[0]) {
521                 my $last_siss = $last_published->[0];
522                 $options->{predict_from} = _revive_holding($last_siss->holding_code, $caption_field, 1);
523             } else {
524                 #TODO: throw event (can't predict from nothing!)
525             }
526         }
527         push( @predictions, _generate_issuance_values($mfhd, $options) );
528         $link_id++;
529     }
530
531     my @issuances;
532     foreach my $prediction (@predictions) {
533         my $issuance = new Fieldmapper::serial::issuance;
534         $issuance->isnew(1);
535         $issuance->label($prediction->{label});
536         $issuance->date_published($prediction->{date_published}->strftime('%F'));
537         $issuance->holding_code(OpenSRF::Utils::JSON->perl2JSON($prediction->{holding_code}));
538         $issuance->holding_type($prediction->{holding_type});
539         $issuance->caption_and_pattern($prediction->{caption_and_pattern});
540         $issuance->subscription($ssub->id);
541         push (@issuances, $issuance);
542     }
543
544     fleshed_issuance_alter($self, $conn, $authtoken, \@issuances); # FIXME: catch events
545
546     my @items;
547     for (my $i = 0; $i < @issuances; $i++) {
548         my $date_expected = $predictions[$i]->{date_published}->add(seconds => interval_to_seconds($ssub->expected_date_offset))->strftime('%F');
549         my $issuance = $issuances[$i];
550         #$issuance->label(interval_to_seconds($ssub->expected_date_offset));
551         foreach my $sdist (@$sdists) {
552             my $streams = $sdist->streams;
553             foreach my $stream (@$streams) {
554                 my $item = new Fieldmapper::serial::item;
555                 $item->isnew(1);
556                 $item->stream($stream->id);
557                 $item->date_expected($date_expected);
558                 $item->issuance($issuance->id);
559                 push (@items, $item);
560             }
561         }
562     }
563     fleshed_item_alter($self, $conn, $authtoken, \@items); # FIXME: catch events
564     return \@items;
565 }
566
567 #
568 # _generate_issuance_values() is an initial attempt at a function which can be used
569 # to populate an issuance table with a list of predicted issues.  It accepts
570 # a hash ref of options initially defined as:
571 # caption : the caption field to predict on
572 # num_to_predict : the number of issues you wish to predict
573 # last_rec_date : the date of the last received issue, to be used as an offset
574 #                 for predicting future issues
575 #
576 # The basic method is to first convert to a single holding if compressed, then
577 # increment the holding and save the resulting values to @issuances.
578
579 # returns @issuance_values, an array of hashrefs containing (formatted
580 # label, formatted chronology date, formatted estimated arrival date, and an
581 # array ref of holding subfields as (key, value, key, value ...)) (not a hash
582 # to protect order and possible duplicate keys), and a holding type.
583 #
584 sub _generate_issuance_values {
585     my ($mfhd, $options) = @_;
586     my $caption = $options->{caption};
587     my $scap_id = $options->{scap_id};
588     my $num_to_predict = $options->{num_to_predict};
589     my $predict_from = $options->{predict_from};   # issuance to predict from
590     #my $last_rec_date = $options->{last_rec_date};   # expected or actual
591
592     # TODO: add support for predicting serials with no chronology by passing in
593     # a last_pub_date option?
594
595
596 # Only needed for 'real' MFHD records, not our temp records
597 #    my $link_id = $caption->link_id;
598 #    if(!$predict_from) {
599 #        my $htag = $caption->tag;
600 #        $htag =~ s/^85/86/;
601 #        my @holdings = $mfhd->holdings($htag, $link_id);
602 #        my $last_holding = $holdings[-1];
603 #
604 #        #if ($last_holding->is_compressed) {
605 #        #    $last_holding->compressed_to_last; # convert to last in range
606 #        #}
607 #        $predict_from = $last_holding;
608 #    }
609 #
610
611     $predict_from->notes('public',  []);
612 # add a note marker for system use (?)
613     $predict_from->notes('private', ['AUTOGEN']);
614
615     my $strp = new DateTime::Format::Strptime(pattern => '%F');
616     my $pub_date;
617     my @issuance_values;
618     my @predictions = $mfhd->generate_predictions({'base_holding' => $predict_from, 'num_to_predict' => $num_to_predict});
619     foreach my $prediction (@predictions) {
620         $pub_date = $strp->parse_datetime($prediction->chron_to_date);
621         push(
622                 @issuance_values,
623                 {
624                     #$link_id,
625                     label => $prediction->format,
626                     date_published => $pub_date,
627                     #date_expected => $date_expected->strftime('%F'),
628                     holding_code => [$prediction->indicator(1),$prediction->indicator(2),$prediction->subfields_list],
629                     holding_type => $MFHD_NAMES_BY_TAG{$caption->tag},
630                     caption_and_pattern => $scap_id
631                 }
632             );
633     }
634
635     return @issuance_values;
636 }
637
638 sub _revive_caption {
639     my $scap = shift;
640
641     my $pattern_code = $scap->pattern_code;
642
643     # build MARC::Field
644     my $pattern_parts = OpenSRF::Utils::JSON->JSON2perl($pattern_code);
645     unshift(@$pattern_parts, $MFHD_TAGS_BY_NAME{$scap->type});
646     my $pattern_field = new MARC::Field(@$pattern_parts);
647
648     # build MFHD::Caption
649     return new MFHD::Caption($pattern_field);
650 }
651
652 sub _revive_holding {
653     my $holding_code = shift;
654     my $caption_field = shift;
655     my $seqno = shift;
656
657     # build MARC::Field
658     my $holding_parts = OpenSRF::Utils::JSON->JSON2perl($holding_code);
659     my $captag = $caption_field->tag;
660     $captag =~ s/^85/86/;
661     unshift(@$holding_parts, $captag);
662     my $holding_field = new MARC::Field(@$holding_parts);
663
664     # build MFHD::Holding
665     return new MFHD::Holding($seqno, $holding_field, $caption_field);
666 }
667
668 __PACKAGE__->register_method(
669     method    => 'unitize_items',
670     api_name  => 'open-ils.serial.receive_items',
671     api_level => 1,
672     argc      => 1,
673     signature => {
674         desc     => 'Marks an item as received, updates the shelving unit (creating a new shelving unit if needed), and updates the summaries',
675         'params' => [ {
676                  name => 'items',
677                  desc => 'array of serial items',
678                  type => 'array'
679             }
680         ],
681         'return' => {
682             desc => 'Returns number of received items',
683             type => 'int'
684         }
685     }
686 );
687
688 sub unitize_items {
689     my ($self, $conn, $auth, $items) = @_;
690
691     my( $reqr, $evt ) = $U->checkses($auth);
692     return $evt if $evt;
693     my $editor = new_editor(requestor => $reqr, xact => 1);
694     $self->api_name =~ /serial\.(\w*)_items/;
695     my $mode = $1;
696     
697     my %found_unit_ids;
698     my %found_stream_ids;
699     my %found_types;
700
701     my %stream_ids_by_unit_id;
702
703     my %unit_map;
704     my %sdist_by_unit_id;
705     my %sdist_by_stream_id;
706
707     my $new_unit_id; # id for '-2' units to share
708     foreach my $item (@$items) {
709         # for debugging only, TODO: delete
710         if (!ref $item) { # hopefully we got an id instead
711             $item = $editor->retrieve_serial_item($item);
712         }
713         # get ids
714         my $unit_id = ref($item->unit) ? $item->unit->id : $item->unit;
715         my $stream_id = ref($item->stream) ? $item->stream->id : $item->stream;
716         my $issuance_id = ref($item->issuance) ? $item->issuance->id : $item->issuance;
717         #TODO: evt on any missing ids
718
719         if ($mode eq 'receive') {
720             $item->date_received('now');
721             $item->status('Received');
722         } else {
723             $item->status('Bindery');
724         }
725
726         # check for types to trigger summary updates
727         my $scap;
728         if (!ref $item->issuance) {
729             my $scaps = $editor->search_serial_caption_and_pattern([{"+siss" => {"id" => $issuance_id}}, { "join" => {"siss" => {}} }]);
730             $scap = $scaps->[0];
731         } elsif (!ref $item->issuance->caption_and_pattern) {
732             $scap = $editor->retrieve_serial_caption_and_pattern($item->issuance->caption_and_pattern);
733         } else {
734             $scap = $editor->issuance->caption_and_pattern;
735         }
736         if (!exists($found_types{$stream_id})) {
737             $found_types{$stream_id} = {};
738         }
739         $found_types{$stream_id}->{$scap->type} = 1;
740
741         # create unit if needed
742         if ($unit_id == -1 or (!$new_unit_id and $unit_id == -2)) { # create unit per item
743             my $unit;
744             my $sdists = $editor->search_serial_distribution([{"+sstr" => {"id" => $stream_id}}, { "join" => {"sstr" => {}} }]);
745             $unit = _build_unit($editor, $sdists->[0], $mode);
746             my $evt =  _create_sunit($editor, $unit);
747             return $evt if $evt;
748             if ($unit_id == -2) {
749                 $new_unit_id = $unit->id;
750                 $unit_id = $new_unit_id;
751             } else {
752                 $unit_id = $unit->id;
753             }
754             $item->unit($unit_id);
755             
756             # get unit with 'DEFAULT's and save unit and sdist for later use
757             $unit = $editor->retrieve_serial_unit($unit->id);
758             $unit_map{$unit_id} = $unit;
759             $sdist_by_unit_id{$unit_id} = $sdists->[0];
760             $sdist_by_stream_id{$stream_id} = $sdists->[0];
761         } elsif ($unit_id == -2) { # create one unit for all '-2' items
762             $unit_id = $new_unit_id;
763             $item->unit($unit_id);
764         }
765
766         $found_unit_ids{$unit_id} = 1;
767         $found_stream_ids{$stream_id} = 1;
768
769         # save the stream_id for this unit_id
770         # TODO: prevent items from different streams in same unit? (perhaps in interface)
771         $stream_ids_by_unit_id{$unit_id} = $stream_id;
772
773         my $evt = _update_sitem($editor, undef, $item);
774         return $evt if $evt;
775     }
776
777     # deal with unit level labels
778     foreach my $unit_id (keys %found_unit_ids) {
779
780         # get all the needed issuances for unit
781         my $issuances = $editor->search_serial_issuance([ {"+sitem" => {"unit" => $unit_id, "status" => "Received"}}, {"join" => {"sitem" => {}}, "order_by" => {"siss" => "date_published"}} ]);
782         #TODO: evt on search failure
783
784         my ($mfhd, $formatted_parts) = _summarize_contents($editor, $issuances);
785
786         # special case for single formatted_part (may have summarized version)
787         if (@$formatted_parts == 1) {
788             #TODO: MFHD.pm should have a 'format_summary' method for this
789         }
790
791         # retrieve and update unit contents
792         my $sunit;
793         my $sdist;
794
795         # if we just created the unit, we will already have it and the distribution stored
796         if (exists $unit_map{$unit_id}) {
797             $sunit = $unit_map{$unit_id};
798             $sdist = $sdist_by_unit_id{$unit_id};
799         } else {
800             $sunit = $editor->retrieve_serial_unit($unit_id);
801             $sdist = $editor->search_serial_distribution([{"+sstr" => {"id" => $stream_ids_by_unit_id{$unit_id}}}, { "join" => {"sstr" => {}} }]);
802             $sdist = $sdist->[0];
803         }
804
805         $sunit->detailed_contents($sdist->unit_label_prefix . ' '
806                     . join(', ', @$formatted_parts) . ' '
807                     . $sdist->unit_label_suffix);
808
809         $sunit->summary_contents($sunit->detailed_contents); #TODO: change this when real summary contents are available
810
811         # create sort_key by left padding numbers to 6 digits
812         my $sort_key = $sunit->detailed_contents;
813         $sort_key =~ s/(\d+)/sprintf '%06d', $1/eg; # this may need improvement
814         $sunit->sort_key($sort_key);
815         
816         if ($mode eq 'bind') {
817             $sunit->status(2); # set to 'Bindery' status
818         }
819
820         my $evt = _update_sunit($editor, undef, $sunit);
821         return $evt if $evt;
822     }
823
824     # TODO: cleanup 'dead' units (units which are now emptied of their items)
825
826     if ($mode eq 'receive') { # the summary holdings do not change when binding
827         # deal with stream level summaries
828         # summaries will be built from the "primary" stream only, that is, the stream with the lowest ID per distribution
829         # (TODO: consider direct designation)
830         my %primary_streams_by_sdist;
831         my %streams_by_sdist;
832
833         # see if we have primary streams, and if so, associate them with their distributions
834         foreach my $stream_id (keys %found_stream_ids) {
835             my $sdist;
836             if (exists $sdist_by_stream_id{$stream_id}) {
837                 $sdist = $sdist_by_stream_id{$stream_id};
838             } else {
839                 $sdist = $editor->search_serial_distribution([{"+sstr" => {"id" => $stream_id}}, { "join" => {"sstr" => {}} }]);
840                 $sdist = $sdist->[0];
841             }
842             my $streams;
843             if (!exists($streams_by_sdist{$sdist->id})) {
844                 $streams = $editor->search_serial_stream([{"distribution" => $sdist->id}, {"order_by" => {"sstr" => "id"}}]);
845                 $streams_by_sdist{$sdist->id} = $streams;
846             } else {
847                 $streams = $streams_by_sdist{$sdist->id};
848             }
849             $primary_streams_by_sdist{$sdist->id} = $streams->[0] if ($stream_id == $streams->[0]->id);
850         }
851
852         # retrieve and update summaries for each affected primary stream's distribution
853         foreach my $sdist_id (keys %primary_streams_by_sdist) {
854             my $stream = $primary_streams_by_sdist{$sdist_id};
855             my $stream_id = $stream->id;
856             # get all the needed issuances for stream
857             # FIXME: search in Bindery/Bound/Not Published? as well as Received
858             foreach my $type (keys %{$found_types{$stream_id}}) {
859                 my $issuances = $editor->search_serial_issuance([ {"+sitem" => {"stream" => $stream_id, "status" => "Received"}, "+scap" => {"type" => $type}}, {"join" => {"sitem" => {}, "scap" => {}}, "order_by" => {"siss" => "date_published"}} ]);
860                 #TODO: evt on search failure
861
862                 my ($mfhd, $formatted_parts) = _summarize_contents($editor, $issuances);
863
864                 # retrieve and update the generated_coverage of the summary
865                 my $search_method = "search_serial_${type}_summary";
866                 my $summary = $editor->$search_method([{"distribution" => $sdist_id}]);
867                 $summary = $summary->[0];
868                 $summary->generated_coverage(join(', ', @$formatted_parts));
869                 my $update_method = "update_serial_${type}_summary";
870                 return $editor->event unless $editor->$update_method($summary);
871             }
872         }
873     }
874
875     $editor->commit;
876     return {'num_items_received' => scalar @$items, 'new_unit_id' => $new_unit_id};
877 }
878
879 sub _build_unit {
880     my $editor = shift;
881     my $sdist = shift;
882     my $mode = shift;
883
884     my $attr = $mode . '_unit_template';
885     my $template = $editor->retrieve_asset_copy_template($sdist->$attr);
886
887     my @parts = qw( status location loan_duration fine_level age_protect circulate deposit ref holdable deposit_amount price circ_modifier circ_as_type alert_message opac_visible floating mint_condition );
888
889     my $unit = new Fieldmapper::serial::unit;
890     foreach my $part (@parts) {
891         my $value = $template->$part;
892         next if !defined($value);
893         $unit->$part($value);
894     }
895
896     # ignore circ_lib in template, set to distribution holding_lib
897     $unit->circ_lib($sdist->holding_lib);
898     $unit->creator($editor->requestor->id);
899     $unit->editor($editor->requestor->id);
900     $attr = $mode . '_call_number';
901     $unit->call_number($sdist->$attr);
902     $unit->barcode('AUTO');
903     $unit->sort_key('');
904     $unit->summary_contents('');
905     $unit->detailed_contents('');
906
907     return $unit;
908 }
909
910
911 sub _summarize_contents {
912     my $editor = shift;
913     my $issuances = shift;
914
915     # create MFHD record
916     my $mfhd = MFHD->new(MARC::Record->new());
917     my %scaps;
918     my %scap_fields;
919     my @scap_fields_ordered;
920     my $seqno = 1;
921     my $link_id = 1;
922     foreach my $issuance (@$issuances) {
923         my $scap_id = $issuance->caption_and_pattern;
924         next if (!$scap_id); # skip issuances with no caption/pattern
925
926         my $scap;
927         my $scap_field;
928         # if this is the first appearance of this scap, retrieve it and add it to the temporary record
929         if (!exists $scaps{$issuance->caption_and_pattern}) {
930             $scaps{$scap_id} = $editor->retrieve_serial_caption_and_pattern($scap_id);
931             $scap = $scaps{$scap_id};
932             $scap_field = _revive_caption($scap);
933             $scap_fields{$scap_id} = $scap_field;
934             push(@scap_fields_ordered, $scap_field);
935             $scap_field->update('8' => $link_id);
936             $mfhd->append_fields($scap_field);
937             $link_id++;
938         } else {
939             $scap = $scaps{$scap_id};
940             $scap_field = $scap_fields{$scap_id};
941         }
942
943         $mfhd->append_fields(_revive_holding($issuance->holding_code, $scap_field, $seqno));
944         $seqno++;
945     }
946
947     my @formatted_parts;
948     foreach my $scap_field (@scap_fields_ordered) { #TODO: use generic MFHD "summarize" method, once available
949        my @updated_holdings = $mfhd->get_compressed_holdings($scap_field);
950        foreach my $holding (@updated_holdings) {
951            push(@formatted_parts, $holding->format);
952        }
953     }
954
955     return ($mfhd, \@formatted_parts);
956 }
957
958 ##########################################################################
959 # note methods
960 #
961 __PACKAGE__->register_method(
962     method      => 'fetch_notes',
963     api_name        => 'open-ils.serial.item_note.retrieve.all',
964     signature   => q/
965         Returns an array of copy note objects.  
966         @param args A named hash of parameters including:
967             authtoken   : Required if viewing non-public notes
968             item_id      : The id of the item whose notes we want to retrieve
969             pub         : True if all the caller wants are public notes
970         @return An array of note objects
971     /
972 );
973
974 __PACKAGE__->register_method(
975     method      => 'fetch_notes',
976     api_name        => 'open-ils.serial.subscription_note.retrieve.all',
977     signature   => q/
978         Returns an array of copy note objects.  
979         @param args A named hash of parameters including:
980             authtoken       : Required if viewing non-public notes
981             subscription_id : The id of the item whose notes we want to retrieve
982             pub             : True if all the caller wants are public notes
983         @return An array of note objects
984     /
985 );
986
987 __PACKAGE__->register_method(
988     method      => 'fetch_notes',
989     api_name        => 'open-ils.serial.distribution_note.retrieve.all',
990     signature   => q/
991         Returns an array of copy note objects.  
992         @param args A named hash of parameters including:
993             authtoken       : Required if viewing non-public notes
994             distribution_id : The id of the item whose notes we want to retrieve
995             pub             : True if all the caller wants are public notes
996         @return An array of note objects
997     /
998 );
999
1000 # TODO: revisit this method to consider replacing cstore direct calls
1001 sub fetch_notes {
1002     my( $self, $connection, $args ) = @_;
1003     
1004     $self->api_name =~ /serial\.(\w*)_note/;
1005     my $type = $1;
1006
1007     my $id = $$args{object_id};
1008     my $authtoken = $$args{authtoken};
1009     my( $r, $evt);
1010
1011     if( $$args{pub} ) {
1012         return $U->cstorereq(
1013             'open-ils.cstore.direct.serial.'.$type.'_note.search.atomic',
1014             { $type => $id, pub => 't' } );
1015     } else {
1016         # FIXME: restore perm check
1017         # ( $r, $evt ) = $U->checksesperm($authtoken, 'VIEW_COPY_NOTES');
1018         # return $evt if $evt;
1019         return $U->cstorereq(
1020             'open-ils.cstore.direct.serial.'.$type.'_note.search.atomic', {$type => $id} );
1021     }
1022
1023     return undef;
1024 }
1025
1026 __PACKAGE__->register_method(
1027     method      => 'create_note',
1028     api_name        => 'open-ils.serial.item_note.create',
1029     signature   => q/
1030         Creates a new item note
1031         @param authtoken The login session key
1032         @param note The note object to create
1033         @return The id of the new note object
1034     /
1035 );
1036
1037 __PACKAGE__->register_method(
1038     method      => 'create_note',
1039     api_name        => 'open-ils.serial.subscription_note.create',
1040     signature   => q/
1041         Creates a new subscription note
1042         @param authtoken The login session key
1043         @param note The note object to create
1044         @return The id of the new note object
1045     /
1046 );
1047
1048 __PACKAGE__->register_method(
1049     method      => 'create_note',
1050     api_name        => 'open-ils.serial.distribution_note.create',
1051     signature   => q/
1052         Creates a new distribution note
1053         @param authtoken The login session key
1054         @param note The note object to create
1055         @return The id of the new note object
1056     /
1057 );
1058
1059 sub create_note {
1060     my( $self, $connection, $authtoken, $note ) = @_;
1061
1062     $self->api_name =~ /serial\.(\w*)_note/;
1063     my $type = $1;
1064
1065     my $e = new_editor(xact=>1, authtoken=>$authtoken);
1066     return $e->event unless $e->checkauth;
1067
1068     # FIXME: restore permission support
1069 #    my $item = $e->retrieve_serial_item(
1070 #        [
1071 #            $note->item
1072 #        ]
1073 #    );
1074 #
1075 #    return $e->event unless
1076 #        $e->allowed('CREATE_COPY_NOTE', $item->call_number->owning_lib);
1077
1078     $note->create_date('now');
1079     $note->creator($e->requestor->id);
1080     $note->pub( ($U->is_true($note->pub)) ? 't' : 'f' );
1081     $note->clear_id;
1082
1083     my $method = "create_serial_${type}_note";
1084     $e->$method($note) or return $e->event;
1085     $e->commit;
1086     return $note->id;
1087 }
1088
1089 __PACKAGE__->register_method(
1090     method      => 'delete_note',
1091     api_name        =>  'open-ils.serial.item_note.delete',
1092     signature   => q/
1093         Deletes an existing item note
1094         @param authtoken The login session key
1095         @param noteid The id of the note to delete
1096         @return 1 on success - Event otherwise.
1097         /
1098 );
1099
1100 __PACKAGE__->register_method(
1101     method      => 'delete_note',
1102     api_name        =>  'open-ils.serial.subscription_note.delete',
1103     signature   => q/
1104         Deletes an existing subscription note
1105         @param authtoken The login session key
1106         @param noteid The id of the note to delete
1107         @return 1 on success - Event otherwise.
1108         /
1109 );
1110
1111 __PACKAGE__->register_method(
1112     method      => 'delete_note',
1113     api_name        =>  'open-ils.serial.distribution_note.delete',
1114     signature   => q/
1115         Deletes an existing distribution note
1116         @param authtoken The login session key
1117         @param noteid The id of the note to delete
1118         @return 1 on success - Event otherwise.
1119         /
1120 );
1121
1122 sub delete_note {
1123     my( $self, $conn, $authtoken, $noteid ) = @_;
1124
1125     $self->api_name =~ /serial\.(\w*)_note/;
1126     my $type = $1;
1127
1128     my $e = new_editor(xact=>1, authtoken=>$authtoken);
1129     return $e->die_event unless $e->checkauth;
1130
1131     my $method = "retrieve_serial_${type}_note";
1132     my $note = $e->$method([
1133         $noteid,
1134     ]) or return $e->die_event;
1135
1136 # FIXME: restore permissions check
1137 #    if( $note->creator ne $e->requestor->id ) {
1138 #        return $e->die_event unless
1139 #            $e->allowed('DELETE_COPY_NOTE', $note->item->call_number->owning_lib);
1140 #    }
1141
1142     $method = "delete_serial_${type}_note";
1143     $e->$method($note) or return $e->die_event;
1144     $e->commit;
1145     return 1;
1146 }
1147
1148
1149 ##########################################################################
1150 # subscription methods
1151 #
1152 __PACKAGE__->register_method(
1153     method    => 'fleshed_ssub_alter',
1154     api_name  => 'open-ils.serial.subscription.fleshed.batch.update',
1155     api_level => 1,
1156     argc      => 2,
1157     signature => {
1158         desc     => 'Receives an array of one or more subscriptions and updates the database as needed',
1159         'params' => [ {
1160                  name => 'authtoken',
1161                  desc => 'Authtoken for current user session',
1162                  type => 'string'
1163             },
1164             {
1165                  name => 'subscriptions',
1166                  desc => 'Array of fleshed subscriptions',
1167                  type => 'array'
1168             }
1169
1170         ],
1171         'return' => {
1172             desc => 'Returns 1 if successful, event if failed',
1173             type => 'mixed'
1174         }
1175     }
1176 );
1177
1178 sub fleshed_ssub_alter {
1179     my( $self, $conn, $auth, $ssubs ) = @_;
1180     return 1 unless ref $ssubs;
1181     my( $reqr, $evt ) = $U->checkses($auth);
1182     return $evt if $evt;
1183     my $editor = new_editor(requestor => $reqr, xact => 1);
1184     my $override = $self->api_name =~ /override/;
1185
1186 # TODO: permission check
1187 #        return $editor->event unless
1188 #            $editor->allowed('UPDATE_COPY', $class->copy_perm_org($vol, $copy));
1189
1190     for my $ssub (@$ssubs) {
1191
1192         my $ssubid = $ssub->id;
1193
1194         if( $ssub->isdeleted ) {
1195             $evt = _delete_ssub( $editor, $override, $ssub);
1196         } elsif( $ssub->isnew ) {
1197             _cleanse_dates($ssub, ['start_date','end_date']);
1198             $evt = _create_ssub( $editor, $ssub );
1199         } else {
1200             _cleanse_dates($ssub, ['start_date','end_date']);
1201             $evt = _update_ssub( $editor, $override, $ssub );
1202         }
1203     }
1204
1205     if( $evt ) {
1206         $logger->info("fleshed subscription-alter failed with event: ".OpenSRF::Utils::JSON->perl2JSON($evt));
1207         $editor->rollback;
1208         return $evt;
1209     }
1210     $logger->debug("subscription-alter: done updating subscription batch");
1211     $editor->commit;
1212     $logger->info("fleshed subscription-alter successfully updated ".scalar(@$ssubs)." subscriptions");
1213     return 1;
1214 }
1215
1216 sub _delete_ssub {
1217     my ($editor, $override, $ssub) = @_;
1218     $logger->info("subscription-alter: delete subscription ".OpenSRF::Utils::JSON->perl2JSON($ssub));
1219     my $sdists = $editor->search_serial_distribution(
1220             { subscription => $ssub->id }, { limit => 1 } ); #TODO: 'deleted' support?
1221     my $cps = $editor->search_serial_caption_and_pattern(
1222             { subscription => $ssub->id }, { limit => 1 } ); #TODO: 'deleted' support?
1223     my $sisses = $editor->search_serial_issuance(
1224             { subscription => $ssub->id }, { limit => 1 } ); #TODO: 'deleted' support?
1225     return OpenILS::Event->new(
1226             'SERIAL_SUBSCRIPTION_NOT_EMPTY', payload => $ssub->id ) if (@$sdists or @$cps or @$sisses);
1227
1228     return $editor->event unless $editor->delete_serial_subscription($ssub);
1229     return 0;
1230 }
1231
1232 sub _create_ssub {
1233     my ($editor, $ssub) = @_;
1234
1235     $logger->info("subscription-alter: new subscription ".OpenSRF::Utils::JSON->perl2JSON($ssub));
1236     return $editor->event unless $editor->create_serial_subscription($ssub);
1237     return 0;
1238 }
1239
1240 sub _update_ssub {
1241     my ($editor, $override, $ssub) = @_;
1242
1243     $logger->info("subscription-alter: retrieving subscription ".$ssub->id);
1244     my $orig_ssub = $editor->retrieve_serial_subscription($ssub->id);
1245
1246     $logger->info("subscription-alter: original subscription ".OpenSRF::Utils::JSON->perl2JSON($orig_ssub));
1247     $logger->info("subscription-alter: updated subscription ".OpenSRF::Utils::JSON->perl2JSON($ssub));
1248     return $editor->event unless $editor->update_serial_subscription($ssub);
1249     return 0;
1250 }
1251
1252 __PACKAGE__->register_method(
1253     method  => "fleshed_serial_subscription_retrieve_batch",
1254     authoritative => 1,
1255     api_name    => "open-ils.serial.subscription.fleshed.batch.retrieve"
1256 );
1257
1258 sub fleshed_serial_subscription_retrieve_batch {
1259     my( $self, $client, $ids ) = @_;
1260 # FIXME: permissions?
1261     $logger->info("Fetching fleshed subscriptions @$ids");
1262     return $U->cstorereq(
1263         "open-ils.cstore.direct.serial.subscription.search.atomic",
1264         { id => $ids },
1265         { flesh => 1,
1266           flesh_fields => {ssub => [ qw/owning_lib notes/ ]}
1267         });
1268 }
1269
1270 __PACKAGE__->register_method(
1271         method  => "retrieve_sub_tree",
1272     authoritative => 1,
1273         api_name        => "open-ils.serial.subscription_tree.retrieve"
1274 );
1275
1276 __PACKAGE__->register_method(
1277         method  => "retrieve_sub_tree",
1278         api_name        => "open-ils.serial.subscription_tree.global.retrieve"
1279 );
1280
1281 sub retrieve_sub_tree {
1282
1283         my( $self, $client, $user_session, $docid, @org_ids ) = @_;
1284
1285         if(ref($org_ids[0])) { @org_ids = @{$org_ids[0]}; }
1286
1287         $docid = "$docid";
1288
1289         # TODO: permission support
1290         if(!@org_ids and $user_session) {
1291                 my $user_obj = 
1292                         OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
1293                         @org_ids = ($user_obj->home_ou);
1294         }
1295
1296         if( $self->api_name =~ /global/ ) {
1297                 return _build_subs_list( { record_entry => $docid } ); # TODO: filter for !deleted, or active?
1298
1299         } else {
1300
1301                 my @all_subs;
1302                 for my $orgid (@org_ids) {
1303                         my $subs = _build_subs_list( 
1304                                         { record_entry => $docid, owning_lib => $orgid } );# TODO: filter for !deleted, or active?
1305                         push( @all_subs, @$subs );
1306                 }
1307                 
1308                 return \@all_subs;
1309         }
1310
1311         return undef;
1312 }
1313
1314 sub _build_subs_list {
1315         my $search_hash = shift;
1316
1317         #$search_hash->{deleted} = 'f';
1318         my $e = new_editor();
1319
1320         my $subs = $e->search_serial_subscription([$search_hash, { 'order_by' => {'ssub' => 'id'} }]);
1321
1322         my @built_subs;
1323
1324         for my $sub (@$subs) {
1325
1326         # TODO: filter on !deleted?
1327                 my $dists = $e->search_serial_distribution(
1328             [{ subscription => $sub->id }, { 'order_by' => {'sdist' => 'label'} }]
1329             );
1330
1331                 #$dists = [ sort { $a->label cmp $b->label } @$dists  ];
1332
1333                 $sub->distributions($dists);
1334         
1335         # TODO: filter on !deleted?
1336                 my $issuances = $e->search_serial_issuance(
1337                         [{ subscription => $sub->id }, { 'order_by' => {'siss' => 'label'} }]
1338             );
1339
1340                 #$issuances = [ sort { $a->label cmp $b->label } @$issuances  ];
1341                 $sub->issuances($issuances);
1342
1343         # TODO: filter on !deleted?
1344                 my $scaps = $e->search_serial_caption_and_pattern(
1345                         [{ subscription => $sub->id }, { 'order_by' => {'scap' => 'id'} }]
1346             );
1347
1348                 #$scaps = [ sort { $a->id cmp $b->id } @$scaps  ];
1349                 $sub->scaps($scaps);
1350                 push( @built_subs, $sub );
1351         }
1352
1353         return \@built_subs;
1354
1355 }
1356
1357 __PACKAGE__->register_method(
1358     method  => "subscription_orgs_for_title",
1359     authoritative => 1,
1360     api_name    => "open-ils.serial.subscription.retrieve_orgs_by_title"
1361 );
1362
1363 sub subscription_orgs_for_title {
1364     my( $self, $client, $record_id ) = @_;
1365
1366     my $subs = $U->simple_scalar_request(
1367         "open-ils.cstore",
1368         "open-ils.cstore.direct.serial.subscription.search.atomic",
1369         { record_entry => $record_id }); # TODO: filter on !deleted?
1370
1371     my $orgs = { map {$_->owning_lib => 1 } @$subs };
1372     return [ keys %$orgs ];
1373 }
1374
1375
1376 ##########################################################################
1377 # distribution methods
1378 #
1379 __PACKAGE__->register_method(
1380     method    => 'fleshed_sdist_alter',
1381     api_name  => 'open-ils.serial.distribution.fleshed.batch.update',
1382     api_level => 1,
1383     argc      => 2,
1384     signature => {
1385         desc     => 'Receives an array of one or more distributions and updates the database as needed',
1386         'params' => [ {
1387                  name => 'authtoken',
1388                  desc => 'Authtoken for current user session',
1389                  type => 'string'
1390             },
1391             {
1392                  name => 'distributions',
1393                  desc => 'Array of fleshed distributions',
1394                  type => 'array'
1395             }
1396
1397         ],
1398         'return' => {
1399             desc => 'Returns 1 if successful, event if failed',
1400             type => 'mixed'
1401         }
1402     }
1403 );
1404
1405 sub fleshed_sdist_alter {
1406     my( $self, $conn, $auth, $sdists ) = @_;
1407     return 1 unless ref $sdists;
1408     my( $reqr, $evt ) = $U->checkses($auth);
1409     return $evt if $evt;
1410     my $editor = new_editor(requestor => $reqr, xact => 1);
1411     my $override = $self->api_name =~ /override/;
1412
1413 # TODO: permission check
1414 #        return $editor->event unless
1415 #            $editor->allowed('UPDATE_COPY', $class->copy_perm_org($vol, $copy));
1416
1417     for my $sdist (@$sdists) {
1418         my $sdistid = $sdist->id;
1419
1420         if( $sdist->isdeleted ) {
1421             $evt = _delete_sdist( $editor, $override, $sdist);
1422         } elsif( $sdist->isnew ) {
1423             $evt = _create_sdist( $editor, $sdist );
1424         } else {
1425             $evt = _update_sdist( $editor, $override, $sdist );
1426         }
1427     }
1428
1429     if( $evt ) {
1430         $logger->info("fleshed distribution-alter failed with event: ".OpenSRF::Utils::JSON->perl2JSON($evt));
1431         $editor->rollback;
1432         return $evt;
1433     }
1434     $logger->debug("distribution-alter: done updating distribution batch");
1435     $editor->commit;
1436     $logger->info("fleshed distribution-alter successfully updated ".scalar(@$sdists)." distributions");
1437     return 1;
1438 }
1439
1440 sub _delete_sdist {
1441     my ($editor, $override, $sdist) = @_;
1442     $logger->info("distribution-alter: delete distribution ".OpenSRF::Utils::JSON->perl2JSON($sdist));
1443     return $editor->event unless $editor->delete_serial_distribution($sdist);
1444     return 0;
1445 }
1446
1447 sub _create_sdist {
1448     my ($editor, $sdist) = @_;
1449
1450     $logger->info("distribution-alter: new distribution ".OpenSRF::Utils::JSON->perl2JSON($sdist));
1451     return $editor->event unless $editor->create_serial_distribution($sdist);
1452
1453     # create summaries too
1454     my $summary = new Fieldmapper::serial::basic_summary;
1455     $summary->distribution($sdist->id);
1456     $summary->generated_coverage('');
1457     return $editor->event unless $editor->create_serial_basic_summary($summary);
1458     $summary = new Fieldmapper::serial::supplement_summary;
1459     $summary->distribution($sdist->id);
1460     $summary->generated_coverage('');
1461     return $editor->event unless $editor->create_serial_supplement_summary($summary);
1462     $summary = new Fieldmapper::serial::index_summary;
1463     $summary->distribution($sdist->id);
1464     $summary->generated_coverage('');
1465     return $editor->event unless $editor->create_serial_index_summary($summary);
1466
1467     # create a starter stream (TODO: reconsider this)
1468     my $stream = new Fieldmapper::serial::stream;
1469     $stream->distribution($sdist->id);
1470     return $editor->event unless $editor->create_serial_stream($stream);
1471
1472     return 0;
1473 }
1474
1475 sub _update_sdist {
1476     my ($editor, $override, $sdist) = @_;
1477
1478     $logger->info("distribution-alter: retrieving distribution ".$sdist->id);
1479     my $orig_sdist = $editor->retrieve_serial_distribution($sdist->id);
1480
1481     $logger->info("distribution-alter: original distribution ".OpenSRF::Utils::JSON->perl2JSON($orig_sdist));
1482     $logger->info("distribution-alter: updated distribution ".OpenSRF::Utils::JSON->perl2JSON($sdist));
1483     return $editor->event unless $editor->update_serial_distribution($sdist);
1484     return 0;
1485 }
1486
1487 __PACKAGE__->register_method(
1488     method  => "fleshed_serial_distribution_retrieve_batch",
1489     authoritative => 1,
1490     api_name    => "open-ils.serial.distribution.fleshed.batch.retrieve"
1491 );
1492
1493 sub fleshed_serial_distribution_retrieve_batch {
1494     my( $self, $client, $ids ) = @_;
1495 # FIXME: permissions?
1496     $logger->info("Fetching fleshed distributions @$ids");
1497     return $U->cstorereq(
1498         "open-ils.cstore.direct.serial.distribution.search.atomic",
1499         { id => $ids },
1500         { flesh => 1,
1501           flesh_fields => {sdist => [ qw/ holding_lib receive_call_number receive_unit_template bind_call_number bind_unit_template streams / ]}
1502         });
1503 }
1504
1505 ##########################################################################
1506 # caption and pattern methods
1507 #
1508 __PACKAGE__->register_method(
1509     method    => 'scap_alter',
1510     api_name  => 'open-ils.serial.caption_and_pattern.batch.update',
1511     api_level => 1,
1512     argc      => 2,
1513     signature => {
1514         desc     => 'Receives an array of one or more caption and patterns and updates the database as needed',
1515         'params' => [ {
1516                  name => 'authtoken',
1517                  desc => 'Authtoken for current user session',
1518                  type => 'string'
1519             },
1520             {
1521                  name => 'scaps',
1522                  desc => 'Array of caption and patterns',
1523                  type => 'array'
1524             }
1525
1526         ],
1527         'return' => {
1528             desc => 'Returns 1 if successful, event if failed',
1529             type => 'mixed'
1530         }
1531     }
1532 );
1533
1534 sub scap_alter {
1535     my( $self, $conn, $auth, $scaps ) = @_;
1536     return 1 unless ref $scaps;
1537     my( $reqr, $evt ) = $U->checkses($auth);
1538     return $evt if $evt;
1539     my $editor = new_editor(requestor => $reqr, xact => 1);
1540     my $override = $self->api_name =~ /override/;
1541
1542 # TODO: permission check
1543 #        return $editor->event unless
1544 #            $editor->allowed('UPDATE_COPY', $class->copy_perm_org($vol, $copy));
1545
1546     for my $scap (@$scaps) {
1547         my $scapid = $scap->id;
1548
1549         if( $scap->isdeleted ) {
1550             $evt = _delete_scap( $editor, $override, $scap);
1551         } elsif( $scap->isnew ) {
1552             $evt = _create_scap( $editor, $scap );
1553         } else {
1554             $evt = _update_scap( $editor, $override, $scap );
1555         }
1556     }
1557
1558     if( $evt ) {
1559         $logger->info("caption_and_pattern-alter failed with event: ".OpenSRF::Utils::JSON->perl2JSON($evt));
1560         $editor->rollback;
1561         return $evt;
1562     }
1563     $logger->debug("caption_and_pattern-alter: done updating caption_and_pattern batch");
1564     $editor->commit;
1565     $logger->info("caption_and_pattern-alter successfully updated ".scalar(@$scaps)." caption_and_patterns");
1566     return 1;
1567 }
1568
1569 sub _delete_scap {
1570     my ($editor, $override, $scap) = @_;
1571     $logger->info("caption_and_pattern-alter: delete caption_and_pattern ".OpenSRF::Utils::JSON->perl2JSON($scap));
1572     my $sisses = $editor->search_serial_issuance(
1573             { caption_and_pattern => $scap->id }, { limit => 1 } ); #TODO: 'deleted' support?
1574     return OpenILS::Event->new(
1575             'SERIAL_CAPTION_AND_PATTERN_HAS_ISSUANCES', payload => $scap->id ) if (@$sisses);
1576
1577     return $editor->event unless $editor->delete_serial_caption_and_pattern($scap);
1578     return 0;
1579 }
1580
1581 sub _create_scap {
1582     my ($editor, $scap) = @_;
1583
1584     $logger->info("caption_and_pattern-alter: new caption_and_pattern ".OpenSRF::Utils::JSON->perl2JSON($scap));
1585     return $editor->event unless $editor->create_serial_caption_and_pattern($scap);
1586     return 0;
1587 }
1588
1589 sub _update_scap {
1590     my ($editor, $override, $scap) = @_;
1591
1592     $logger->info("caption_and_pattern-alter: retrieving caption_and_pattern ".$scap->id);
1593     my $orig_scap = $editor->retrieve_serial_caption_and_pattern($scap->id);
1594
1595     $logger->info("caption_and_pattern-alter: original caption_and_pattern ".OpenSRF::Utils::JSON->perl2JSON($orig_scap));
1596     $logger->info("caption_and_pattern-alter: updated caption_and_pattern ".OpenSRF::Utils::JSON->perl2JSON($scap));
1597     return $editor->event unless $editor->update_serial_caption_and_pattern($scap);
1598     return 0;
1599 }
1600
1601 __PACKAGE__->register_method(
1602     method  => "serial_caption_and_pattern_retrieve_batch",
1603     authoritative => 1,
1604     api_name    => "open-ils.serial.caption_and_pattern.batch.retrieve"
1605 );
1606
1607 sub serial_caption_and_pattern_retrieve_batch {
1608     my( $self, $client, $ids ) = @_;
1609     $logger->info("Fetching caption_and_patterns @$ids");
1610     return $U->cstorereq(
1611         "open-ils.cstore.direct.serial.caption_and_pattern.search.atomic",
1612         { id => $ids }
1613     );
1614 }
1615
1616 __PACKAGE__->register_method(
1617     "method" => "bre_by_identifier",
1618     "api_name" => "open-ils.serial.biblio.record_entry.by_identifier",
1619     "stream" => 1,
1620     "signature" => {
1621         "desc" => "Find instances of biblio.record_entry given a search token" .
1622             " that could be a value for any identifier defined in " .
1623             "config.metabib_field",
1624         "params" => [
1625             {"desc" => "Search token", "type" => "string"},
1626             {"desc" => "Options: require_subscriptions, add_mvr, is_actual_id" .
1627                 " (all boolean)", "type" => "object"}
1628         ],
1629         "return" => {
1630             "desc" => "Any matching BREs, or if the add_mvr option is true, " .
1631                 "objects with a 'bre' key/value pair, and an 'mvr' " .
1632                 "key-value pair.  BREs have subscriptions fleshed on.",
1633             "type" => "object"
1634         }
1635     }
1636 );
1637
1638 sub bre_by_identifier {
1639     my ($self, $client, $term, $options) = @_;
1640
1641     return new OpenILS::Event("BAD_PARAMS") unless $term;
1642
1643     $options ||= {};
1644     my $e = new_editor();
1645
1646     my @ids;
1647
1648     if ($options->{"is_actual_id"}) {
1649         @ids = ($term);
1650     } else {
1651         my $cmf =
1652             $e->search_config_metabib_field({"field_class" => "identifier"})
1653                 or return $e->die_event;
1654
1655         my @identifiers = map { $_->name } @$cmf;
1656         my $query = join(" || ", map { "id|$_: $term" } @identifiers);
1657
1658         my $search = create OpenSRF::AppSession("open-ils.search");
1659         my $search_result = $search->request(
1660             "open-ils.search.biblio.multiclass.query.staff", {}, $query
1661         )->gather(1);
1662         $search->disconnect;
1663
1664         # Un-nest results. They tend to look like [[1],[2],[3]] for some reason.
1665         @ids = map { @{$_} } @{$search_result->{"ids"}};
1666
1667         unless (@ids) {
1668             $e->disconnect;
1669             return undef;
1670         }
1671     }
1672
1673     my $bre = $e->search_biblio_record_entry([
1674         {"id" => \@ids}, {
1675             "flesh" => 2, "flesh_fields" => {
1676                 "bre" => ["subscriptions"],
1677                 "ssub" => ["owning_lib"]
1678             }
1679         }
1680     ]) or return $e->die_event;
1681
1682     if (@$bre && $options->{"require_subscriptions"}) {
1683         $bre = [ grep { @{$_->subscriptions} } @$bre ];
1684     }
1685
1686     $e->disconnect;
1687
1688     if (@$bre) { # re-evaluate after possible grep
1689         if ($options->{"add_mvr"}) {
1690             $client->respond(
1691                 {"bre" => $_, "mvr" => _get_mvr($_->id)}
1692             ) foreach (@$bre);
1693         } else {
1694             $client->respond($_) foreach (@$bre);
1695         }
1696     }
1697
1698     undef;
1699 }
1700
1701 __PACKAGE__->register_method(
1702     "method" => "get_receivable_items",
1703     "api_name" => "open-ils.serial.items.receivable.by_subscription",
1704     "stream" => 1,
1705     "signature" => {
1706         "desc" => "Return all receivable items under a given subscription",
1707         "params" => [
1708             {"desc" => "Authtoken", "type" => "string"},
1709             {"desc" => "Subscription ID", "type" => "number"},
1710         ],
1711         "return" => {
1712             "desc" => "All receivable items under a given subscription",
1713             "type" => "object"
1714         }
1715     }
1716 );
1717
1718 __PACKAGE__->register_method(
1719     "method" => "get_receivable_items",
1720     "api_name" => "open-ils.serial.items.receivable.by_issuance",
1721     "stream" => 1,
1722     "signature" => {
1723         "desc" => "Return all receivable items under a given issuance",
1724         "params" => [
1725             {"desc" => "Authtoken", "type" => "string"},
1726             {"desc" => "Issuance ID", "type" => "number"},
1727         ],
1728         "return" => {
1729             "desc" => "All receivable items under a given issuance",
1730             "type" => "object"
1731         }
1732     }
1733 );
1734
1735 sub get_receivable_items {
1736     my ($self, $client, $auth, $term)  = @_;
1737
1738     my $e = new_editor("authtoken" => $auth);
1739     return $e->die_event unless $e->checkauth;
1740
1741     # XXX permissions
1742
1743     my $by = ($self->api_name =~ /by_(\w+)$/)[0];
1744
1745     my %where = (
1746         "issuance" => {"issuance" => $term},
1747         "subscription" => {"+siss" => {"subscription" => $term}}
1748     );
1749
1750     my $item_ids = $e->json_query(
1751         {
1752             "select" => {"sitem" => ["id"]},
1753             "from" => {"sitem" => "siss"},
1754             "where" => {
1755                 %{$where{$by}}, "date_received" => undef
1756             },
1757             "order_by" => {"sitem" => ["id"]}
1758         }
1759     ) or return $e->die_event;
1760
1761     return undef unless @$item_ids;
1762
1763     foreach (map { $_->{"id"} } @$item_ids) {
1764         $client->respond(
1765             $e->retrieve_serial_item([
1766                 $_, {
1767                     "flesh" => 3,
1768                     "flesh_fields" => {
1769                         "sitem" => ["stream", "issuance"],
1770                         "sstr" => ["distribution"],
1771                         "sdist" => ["holding_lib"]
1772                     }
1773                 }
1774             ])
1775         );
1776     }
1777
1778     $e->disconnect;
1779     undef;
1780 }
1781
1782 __PACKAGE__->register_method(
1783     "method" => "get_receivable_issuances",
1784     "api_name" => "open-ils.serial.issuances.receivable",
1785     "stream" => 1,
1786     "signature" => {
1787         "desc" => "Return all issuances with receivable items given " .
1788             "a subscription ID",
1789         "params" => [
1790             {"desc" => "Authtoken", "type" => "string"},
1791             {"desc" => "Subscription ID", "type" => "number"},
1792         ],
1793         "return" => {
1794             "desc" => "All issuances with receivable items " .
1795                 "(but not the items themselves)", "type" => "object"
1796         }
1797     }
1798 );
1799
1800 sub get_receivable_issuances {
1801     my ($self, $client, $auth, $sub_id) = @_;
1802
1803     my $e = new_editor("authtoken" => $auth);
1804     return $e->die_event unless $e->checkauth;
1805
1806     # XXX permissions
1807
1808     my $issuance_ids = $e->json_query({
1809         "select" => {
1810             "siss" => [
1811                 {"transform" => "distinct", "column" => "id"}
1812             ]
1813         },
1814         "from" => {"siss" => "sitem"},
1815         "where" => {
1816             "subscription" => $sub_id,
1817             "+sitem" => {"date_received" => undef}
1818         }
1819     }) or return $e->die_event;
1820
1821     $client->respond($e->retrieve_serial_issuance($_->{"id"}))
1822         foreach (@$issuance_ids);
1823
1824     $e->disconnect;
1825     undef;
1826 }
1827
1828 __PACKAGE__->register_method(
1829     "method" => "receive_items_by_id",
1830     "api_name" => "open-ils.serial.items.receive_by_id",
1831     "stream" => 1,
1832     "signature" => {
1833         "desc" => "Given sitem IDs, just set their date_received to now()",
1834         "params" => [
1835             {"desc" => "Authtoken", "type" => "string"},
1836             {"desc" => "Serial Item IDs", "type" => "array"},
1837         ],
1838         "return" => {
1839             "desc" => "Stream of updated items", "type" => "object"
1840         }
1841     }
1842 );
1843
1844 sub receive_items_by_id {
1845     my ($self, $client, $auth, $id_list) = @_;
1846
1847     my $e = new_editor("authtoken" => $auth, "xact" => 1);
1848     return $e->die_event unless $e->checkauth;
1849
1850     # XXX permissions
1851
1852     # for now this function doesn't do nearly enough. simply sets
1853     # date_received to now()
1854
1855     my @results = ();
1856     foreach (@$id_list) {
1857         my $sitem = $e->retrieve_serial_item($_) or return $e->die_event;
1858
1859         $sitem->date_received("now");
1860         $e->update_serial_item($sitem) or return $e->die_event;
1861
1862         push @results, $sitem;
1863     }
1864
1865     $e->commit;
1866     $client->respond($_) foreach @results;
1867     undef;
1868 }
1869
1870 1;