sub handler {
my $self = shift;
my $env = shift;
+ $self->munge_acq_data($env);
return 1 if $self->run_TT($env);
return 0;
}
+# adds a field to the environment called "po_pre_process"
+sub munge_acq_data {
+ my ($self, $env) = @_;
+ my $helpers = $OpenILS::Application::Trigger::Reactor::_TT_helpers;
+ my $preproc = $env->{po_pre_process} = {
+ segment_count => 0,
+ lineitems => []
+ };
+
+ my $po = $env->{target};
+
+ return unless $po and $po->lineitems;
+
+ for my $li (@{$po->lineitems}) {
+
+ my $li_hash = {
+ id => $li->id,
+ estimated_unit_price => $li->estimated_unit_price,
+ all_copies => [],
+ copy_groups => [],
+ source_notes => $li->notes
+ };
+
+ push(@{$preproc->{lineitems}}, $li_hash);
+
+ # -----------------------------------------------------------
+ # find the order identifier type and value
+ my $ident_attr = $helpers->get_li_order_ident($li->attributes);
+ $li->{idqual} = 'EN';
+
+ if ($ident_attr) {
+ my $idname = $ident_attr->attr_name;
+ my $idvalue = $ident_attr->attr_value;
+ if ($idname eq 'isbn' and length($idvalue) != 13) {
+ $li->{idqual} = 'IB';
+ } elsif ($idname eq 'issn') {
+ $li->{idqual} = 'IS';
+ }
+ } else {
+ $li->{idqual} = 'IN';
+ $li->{idval} = $li->id;
+ }
+
+
+ # -----------------------------------------------------------
+ # Common EDI "display" fields
+ if ($li->attributes) {
+ $li_hash->{title} = $helpers->get_li_attr_edi('title', '', $li->attributes);
+ $li_hash->{author} = $helpers->get_li_attr_edi('author', '', $li->attributes);
+ $li_hash->{edition} = $helpers->get_li_attr_edi('edition', '', $li->attributes);
+ $li_hash->{pubdate} = $helpers->get_li_attr_edi('pubdate', '', $li->attributes);
+ $li_hash->{publisher} = $helpers->get_li_attr_edi('publisher', '', $li->attributes);
+ $li_hash->{pagination} = $helpers->get_li_attr_edi('pagination', '', $li->attributes);
+ }
+
+ next unless $li->lineitem_details;
+
+ # -----------------------------------------------------------
+ # Lineitem copy data
+
+ $li_hash->{quantity} = scalar(@{$li->lineitem_details});
+
+ for my $lid (@{$li->lineitem_details}) {
+
+ my $copy = {
+ copy_id => $lid->id,
+ fund => $helpers->escape_edi($lid->fund->code),
+ item_type => $helpers->escape_edi($lid->circ_modifier),
+ callnumber => $helpers->escape_edi($lid->cn_label),
+ owning_lib => $helpers->escape_edi($lid->owning_lib->shortname),
+ location => $helpers->escape_edi($lid->location),
+ collection_code => $helpers->escape_edi($lid->collection_code),
+ source_note => $lid->note,
+ quantity => 1
+ };
+
+ if (my $acp = $lid->eg_copy_id) {
+ $copy->{item_type} = $acp->circ_modifier;
+ $copy->{callnumber} = $acp->call_number->label;
+ $copy->{location} = $acp->location->name;
+ }
+
+ # see if already have a copy_groups item that matches the current copy
+ my ($match) = grep {
+ ($_->{fund} eq $copy->{fund} or
+ (!$_->{fund} and !$copy->fund))
+ and
+ ($_->{item_type} eq $copy->{item_type} or
+ (!$_->{item_type} and !$copy->{item_type}))
+ and
+ ($_->{callnumber} eq $copy->{callnumber} or
+ (!$_->{callnumber} and !$copy->{callnumber}))
+ and
+ ($_->{owning_lib} eq $copy->{owning_lib} or
+ (!$_->{owning_lib} and !$copy->{owning_lib}))
+ and
+ ($_->{location} eq $copy->{location} or
+ (!$_->{location} and !$copy->{location}))
+ and
+ ($_->{collection_code} eq $copy->{collection_code} or
+ (!$_->{collection_code} and !$copy->{collection_code}))
+ } @{$li->{copy_groups}};
+
+ if ($match) {
+ # if we have a match, just increment the counter
+ # of copies we want to order.
+ $match->{quantity}++;
+
+ } else {
+
+ # a copy group with these values is not yet represented, add it.
+ push(@{$li->{copy_groups}}, $copy);
+ }
+
+ push(@{$li->{all_copies}, $copy);
+ }
+ }
+}
+
1;
# munge the lineitem / copy data for easier template integration
LINEITEMS = [];
-FOR li IN target.lineitems;
+FOR li_hash IN po_pre_process.lineitems;
SEGMENT_COUNT = SEGMENT_COUNT + 10; # needs verification
- li_hash = {};
- li_hash.id = li.id;
-
- # extract the ISBN, etc.
- idval = '';
- idqual = 'EN'; # default ISBN/UPC/EAN-13
- ident_attr = helpers.get_li_order_ident(li.attributes);
- IF ident_attr;
- idname = ident_attr.attr_name;
- idval = ident_attr.attr_value;
- IF idname == 'isbn' AND idval.length != 13;
- idqual = 'IB';
- ELSIF idname == 'issn';
- idqual = 'IS';
- END;
- ELSE;
- idqual = 'IN';
- idval = li.id;
- END;
-
- li_hash.idval = idval;
- li_hash.idqual = idqual;
- li_hash.title = helpers.get_li_attr_edi('title', '', li.attributes);
- li_hash.author = helpers.get_li_attr_edi('author', '', li.attributes);
- li_hash.edition = helpers.get_li_attr_edi('edition', '', li.attributes);
- li_hash.pubdate = helpers.get_li_attr_edi('pubdate', '', li.attributes);
- li_hash.publisher = helpers.get_li_attr_edi('publisher', '', li.attributes);
- li_hash.pagination = helpers.get_li_attr_edi('pagination', '', li.attributes);
- li_hash.quantity = li.lineitem_details.size;
- li_hash.estimated_unit_price = li.estimated_unit_price;
- li_hash.copies = [];
-
ftx_vals = [];
- FOR note IN li.lineitem_notes;
+ FOR note IN li_hash.source_notes;
NEXT UNLESS note.vendor_public == 't';
ftx_vals.push(note.value);
END;
END;
END;
END;
- IF xtra_ftx; ftx_vals.unshift(xtra_ftx); END;
notes = [];
li_hash.notes = notes;
- IF INC_COPIES;
- SEGMENT_COUNT = SEGMENT_COUNT + 1;
- FOR lid IN li.lineitem_details;
- fund = lid.fund.code;
- item_type = lid.circ_modifier;
- callnumber = lid.cn_label;
- owning_lib = lid.owning_lib.shortname;
- location = lid.location;
- collection_code = lid.collection_code;
-
- # when we have real copy data, treat it as authoritative for some fields
- acp = lid.eg_copy_id;
- IF acp;
- item_type = acp.circ_modifier;
- callnumber = acp.call_number.label;
- location = acp.location.name;
- END ;
-
-
- # collapse like copies into groups w/ quantity
-
- found_match = 0;
- IF !INC_COPY_ID; # INC_COPY_ID implies 1 copy per GIR
- FOR copy IN li_hash.copies;
- IF (fund == copy.fund OR (!fund AND !copy.fund)) AND
- (item_type == copy.item_type OR (!item_type AND !copy.item_type)) AND
- (callnumber == copy.callnumber OR (!callnumber AND !copy.callnumber)) AND
- (owning_lib == copy.owning_lib OR (!owning_lib AND !copy.owning_lib)) AND
- (location == copy.location OR (!location AND !copy.location)) AND
- (collection_code == copy.collection_code OR (!collection_code AND !copy.collection_code));
-
- copy.quantity = copy.quantity + 1;
- found_match = 1;
- END;
- END;
- END;
-
- IF !found_match;
- li_hash.copies.push({
- fund => helpers.escape_edi(fund),
- item_type => helpers.escape_edi(item_type),
- callnumber => helpers.escape_edi(callnumber),
- owning_lib => helpers.escape_edi(owning_lib),
- location => helpers.escape_edi(location),
- collection_code => helpers.escape_edi(collection_code),
- copy_id => lid.id, # for INC_COPY_ID
- quantity => 1
- });
- END;
- END; # copies loop
- END; # inc_copies
-
- LINEITEMS.push(li_hash);
-
END; # for lineitem
# IMD fields are limited to 70 chars per value. Any values longer