1 # ---------------------------------------------------------------
2 # Copyright (C) 2016 King County Library System
3 # Author: Bill Erickson <berickxx@gmail.com>
5 # Copied heavily from Application/Trigger/Reactor.pm
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 # ---------------------------------------------------------------
17 package OpenILS::Utils::EDIWriter;
18 use strict; use warnings;
19 use OpenILS::Utils::CStoreEditor qw/:funcs/;
20 use OpenILS::Application::AppUtils;
22 my $U = 'OpenILS::Application::AppUtils';
25 my ($class, $args) = @_;
27 return bless($args, $class);
30 # Returns EDI string on success, undef on error.
32 my ($self, $po_id, $msg_type) = @_;
33 $msg_type ||= 'order';
35 my $po = $self->get_po($po_id);
36 return undef unless $po;
38 $self->compile_po($po);
39 return undef unless $self->{compiled};
41 my $edi = $self->build_order_edi if $msg_type eq 'order';
43 # remove the newlines unless we are pretty printing
44 $edi =~ s/\n//g unless $self->{pretty};
50 my ($self, $po_id) = @_;
51 return new_editor()->retrieve_acq_purchase_order([
55 acqpo => [qw/lineitems ordering_agency provider/],
56 acqpro => [qw/edi_default/],
57 acqedi => [qw/attr_set/],
58 aeas => [qw/attr_maps/],
59 jub => [qw/lineitem_details lineitem_notes attributes/],
60 acqlid => [qw/owning_lib location fund eg_copy_id/],
61 acp => [qw/location call_number/],
62 aou => [qw/mailing_address/]
69 my ($self, $value) = @_;
70 return '' if (not defined $value || ref($value));
72 # Typical vendors dealing with EDIFACT (or is the problem with
73 # our EDI translator itself?) would seem not to want
74 # any characters outside the ASCII range, so trash them.
75 $value =~ s/[^[:ascii:]]//g;
77 # What the heck, get rid of [ ] too (although I couldn't get them
78 # to cause any problems for me, problems have been reported. See
80 $value =~ s/[\[\]]//g;
82 # Characters [? + ' \ : <newline>] are all potentially problematic for
83 # EDI messages, regardless of their position in the string.
84 # Safest to simply remove them.
85 $value =~ s/[\\\?\+':]//g;
87 # Replace newlines with spaces.
93 # Returns an EDI-escaped version of the requested lineitem attribute
94 # value. If $attr_type is not set, the first attribute found matching
95 # the requested $attr_name will be used.
97 my ($self, $li, $attr_name, $attr_type) = @_;
99 for my $attr (@{$li->attributes}) {
100 next unless $attr->attr_name eq $attr_name;
101 next if $attr_type && $attr->attr_type ne $attr_type;
102 return $self->escape_edi($attr->attr_value);
108 # Generates a HASH version of the PO with all of the data necessary
109 # to generate an EDI message from the PO.
111 my ($self, $po) = @_;
113 # Cannot generate EDI if the PO has no linked EDI account.
114 return undef unless $po->provider->edi_default;
118 po_name => $self->escape_edi($po->name),
119 provider_id => $po->provider->id,
120 vendor_san => $po->provider->san || '',
121 org_unit_san => $po->ordering_agency->mailing_address->san || '',
122 currency_type => $po->provider->currency_type,
127 $self->{compiled} = \%compiled;
129 if ($po->provider->edi_default->attr_set) {
130 $compiled{edi_attrs}{$_->attr} = 1
131 for @{$po->provider->edi_default->attr_set->attr_maps}
134 $compiled{buyer_code} =
135 $compiled{edi_attrs}->{BUYER_ID_INCLUDE_VENDCODE} ? # B&T
136 $compiled{vendor_san}.' '.$po->provider->edi_default->vendcode :
137 $po->provider->edi_default->vendacct;
139 push(@{$compiled{lineitems}},
140 $self->compile_li($_)) for @{$po->lineitems};
145 # Translate a lineitem order identifier attribute into an
146 # EDI ID value and ID qualifier.
147 sub set_li_order_ident {
148 my ($self, $li, $li_hash) = @_;
150 my $idqual = 'EN'; # ISBN13
153 if ($self->{compiled}->{edi_attr}->{LINEITEM_IDENT_VENDOR_NUMBER}) {
154 # See if we have a vendor-specific lineitem identifier value
155 $idval = $self->get_li_attr($li, 'vendor_num');
160 my $attr = $self->get_li_order_ident_attr($li->attributes);
163 my $name = $attr->attr_name;
164 $idval = $attr->attr_value;
166 if ($name eq 'isbn' && length($idval) != 13) {
168 } elsif ($name eq 'issn') {
177 $li_hash->{idqual} = $idqual;
178 $li_hash->{idval} = $idval;
181 # Find the acq.lineitem_attr object that represents the identifier
183 sub get_li_order_ident_attr {
184 my ($self, $attrs) = @_;
186 # preferred identifier
187 my ($attr) = grep { $U->is_true($_->order_ident) } @$attrs;
188 return $attr if $attr;
190 # note we're not using get_li_attr, since we need the
191 # attr object and not just the attr value
195 $_->attr_name eq 'isbn' and
196 $_->attr_type eq 'lineitem_marc_attr_definition' and
197 length($_->attr_value) == 13
199 return $attr if $attr;
201 for my $name (qw/isbn issn upc/) {
203 $_->attr_name eq $name and
204 $_->attr_type eq 'lineitem_marc_attr_definition'
206 return $attr if $attr;
209 # any 'identifier' attr
210 return (grep { $_->attr_name eq 'identifier' } @$attrs)[0];
213 # Collect FTX notes and chop them into FTX-compatible values.
215 my ($self, $li) = @_;
217 # all vendor-public, non-empty lineitem notes
220 grep { $U->is_true($_->vendor_public) && $_->value }
221 @{$li->lineitem_notes};
223 if ($self->{compiled}->{edi_attr}->{COPY_SPEC_CODES}) {
224 for my $lid (@{$li->lineitem_details}) {
225 push(@notes, $lid->note)
226 if ($lid->note || '') =~ /spec code [a-zA-Z0-9_]/;
232 if (!@notes && $self->{compiled}->{edi_attr}->{INCLUDE_EMPTY_LI_NOTE}) {
233 # lineitem has no notes. Add a blank note if needed.
234 push(@trimmed_notes, '');
237 # EDI FTX fields have a max length of 512
238 # While we're in here, EDI-escape the note values
239 for my $note (@notes) {
240 $note = $self->escape_edi($note);
241 my @parts = ($note =~ m/.{1,512}/g);
242 push(@trimmed_notes, @parts);
246 return \@trimmed_notes;
250 my ($self, $li) = @_;
254 quantity => scalar(@{$li->lineitem_details}),
255 estimated_unit_price => $li->estimated_unit_price || '0.00',
256 notes => $self->get_li_ftx($li),
260 $self->set_li_order_ident($li, $li_hash);
262 for my $name (qw/title author edition pubdate publisher pagination/) {
263 $li_hash->{$name} = $self->get_li_attr($li, $name);
266 $self->compile_copies($li, $li_hash);
272 my ($self, $li, $li_hash) = @_;
274 # does this EDI account want copy data?
275 return unless $self->{compiled}->{edi_attrs}->{INCLUDE_COPIES};
277 for my $copy (@{$li->lineitem_details}) {
278 $self->compile_copy($li, $li_hash, $copy);
283 my ($self, $li, $li_hash, $copy) = @_;
285 my $fund = $copy->fund ? $copy->fund->code : '';
286 my $item_type = $copy->circ_modifier || '';
287 my $call_number = $copy->cn_label || '';
288 my $owning_lib = $copy->owning_lib ? $copy->owning_lib->shortname : '';
289 my $location = $copy->location ? $copy->location->name : '';
290 my $collection_code = $copy->collection_code || '';
291 my $barcode = $copy->barcode || '';
294 # When an ACQ copy links to a real copy (acp), treat the real
295 # copy as authoritative for certain fields.
296 my $acp = $copy->eg_copy_id;
298 $item_type = $acp->circ_modifier || '';
299 $call_number = $acp->call_number->label;
300 $location = $acp->location->name;
305 # Collapse like copies into groups with a quantity value.
306 # INCLUDE_COPY_ID implies one GIR row per copy, no collapsing.
307 if (!$self->{compiled}->{edi_attrs}->{INCLUDE_COPY_ID}) {
309 for my $e_copy (@{$li_hash->{copies}}) {
311 ($fund eq $e_copy->{fund}) &&
312 ($item_type eq $e_copy->{item_type}) &&
313 ($call_number eq $e_copy->{call_number}) &&
314 ($owning_lib eq $e_copy->{owning_lib}) &&
315 ($location eq $e_copy->{location}) &&
316 ($barcode eq $e_copy->{barcode}) &&
317 ($collection_code eq $e_copy->{collection_code})
319 $e_copy->{quantity}++;
326 return if $found_match; # nothing left to do.
328 # No matching copy found. Add it as a new copy to the lineitem
331 push(@{$li_hash->{copies}}, {
332 fund => $self->escape_edi($fund),
333 item_type => $self->escape_edi($item_type),
334 call_number => $self->escape_edi($call_number),
335 owning_lib => $self->escape_edi($owning_lib),
336 location => $self->escape_edi($location),
337 barcode => $self->escape_edi($barcode),
338 collection_code => $self->escape_edi($collection_code),
339 copy_id => $copy->id, # for INCLUDE_COPY_ID
344 # IMD fields are limited to 70 chars per value. Any values longer
345 # should be carried via repeating IMD fields.
346 # IMD fields should only display the +::: when a value is present
348 my ($self, $code, $value) = @_;
351 for my $part ($value =~ m/.{1,70}/g) {
352 $s .= "IMD+F+$code+:::$part'\n"; }
356 return "IMD+F+$code'\n"
370 # <lineitems and copies>
375 sub build_order_edi {
377 my %c = %{$self->{compiled}};
378 my $date = DateTime->now->strftime("%Y%m%d");
379 my $datetime = DateTime->now->strftime("%y%m%d:%H%M");
380 my @lis = @{$c{lineitems}};
385 UNB+UNOB:3+$c{org_unit_san}:31B+$c{vendor_san}:31B+$datetime+1'
386 UNH+1+ORDERS:D:96A:UN'
391 $edi .= "NAD+BY+$c{org_unit_san}::31B'\n"
392 unless $self->{compiled}->{edi_attrs}->{BUYER_ID_INCLUDE_VENDCODE};
395 NAD+BY+$c{buyer_code}::91'
396 NAD+SU+$c{vendor_san}::31B'
397 NAD+SU+$c{provider_id}::92'
398 CUX+2:$c{currency_type}:9'
401 # EDI lineitem segments
402 $edi .= $self->build_lineitem_segments($_) for @lis;
404 my $li_count = scalar(@lis);
406 # Count the number of segments in the EDI message by counting the
407 # number of newlines. Add to count for lines below, not including
409 my $segments = $edi =~ tr/\n//;
410 $segments += 1; # UNS, CNT, UNT, but not UNA or UNB
436 sub build_lineitem_segments {
437 my ($self, $li_hash) = @_;
438 my %c = %{$self->{compiled}};
440 my $id = $li_hash->{id};
441 my $idval = $li_hash->{idval};
442 my $idqual = $li_hash->{idqual};
443 my $quantity = $li_hash->{quantity};
444 my $price = $li_hash->{estimated_unit_price};
446 # Line item identifier segments
447 my $edi = "LIN+$id++$idval:$idqual'\n";
448 $edi .= "PIA+5+$idval:$idqual'\n";
450 $edi .= $self->IMD('BTI', $li_hash->{title});
451 $edi .= $self->IMD('BPU', $li_hash->{publisher});
452 $edi .= $self->IMD('BPD', $li_hash->{pubdate});
454 $edi .= $self->IMD('BEN', $li_hash->{edition})
455 if $c{edi_attrs}->{INCLUDE_BIB_EDITION};
457 $edi .= $self->IMD('BAU', $li_hash->{author})
458 if $c{edi_attrs}->{INCLUDE_BIB_AUTHOR};
460 $edi .= $self->IMD('BPH', $li_hash->{pagination})
461 if $c{edi_attrs}->{INCLUDE_BIB_PAGINATION};
463 $edi .= "QTY+21:$quantity'\n";
465 $edi .= $self->build_gir_segments($li_hash);
467 for my $note (@{$li_hash->{notes}}) {
469 $edi .= "FTX+LIN+1+$note'\n"
471 $edi .= "FTX+LIN+1'\n"
475 $edi .= "PRI+AAB:$price'\n";
478 my $rff = "$c{po_id}/$id";
480 if ($c{edi_attrs}->{LINEITEM_REF_ID_ONLY}) {
481 # RFF with lineitem ID only (typically B&T)
483 } elsif ($c{edi_attrs}->{INCLUDE_PO_NAME}) {
484 # RFF with PO name instead of PO ID
485 $rff = "$c{po_name}/$id";
488 $edi .= "RFF+LI:$rff'\n";
494 # Map of GIR segment codes, copy field names, inclusion attributes,
495 # and include-if-empty attributes for encoding copy data.
498 field => 'owning_lib',
499 attr => 'INCLUDE_OWNING_LIB'},
501 field => 'collection_code',
502 attr => 'INCLUDE_COLLECTION_CODE',
503 empty_attr => 'INCLUDE_EMPTY_COLLECTION_CODE'},
506 attr => 'INCLUDE_QUANTITY'},
509 attr => 'INCLUDE_COPY_ID'},
511 field => 'item_type',
512 attr => 'INCLUDE_ITEM_TYPE',
513 empty_attr => 'INCLUDE_EMPTY_ITEM_TYPE'},
515 field => 'call_number',
516 attr => 'INCLUDE_CALL_NUMBER',
517 empty_attr => 'INCLUDE_EMPTY_CALL_NUMBER'},
520 attr => 'INCLUDE_FUND'},
523 attr => 'INCLUDE_LOCATION',
524 empty_attr => 'INCLUDE_EMPTY_LOCATION'},
527 attr => 'INCLUDE_ITEM_BARCODE'}
540 sub build_gir_segments {
541 my ($self, $li_hash) = @_;
542 my %c = %{$self->{compiled}};
546 for my $copy (@{$li_hash->{copies}}) {
548 my $gir_idx_str = sprintf("%03d", $gir_index);
551 for my $field (@gir_fields) {
552 next unless $c{edi_attrs}->{$field->{attr}};
554 my $val = $copy->{$field->{field}};
555 my $code = $field->{code};
557 # include the GIR component if we have a value or this
558 # EDI account is configured to include the empty value
559 next unless $val || $c{edi_attrs}->{$field->{empty_attr} || ''};
561 # EDI only allows 5 fields per GIR segment. When we exceed
562 # 5, finalize the in-process GIR segment and add a new one
564 if ($field_count == 5) {
566 # Finalize this GIR segment with a ' and newline
572 # Starting a new GIR line for the current copy.
573 $edi .= "GIR+$gir_idx_str" if $field_count == 1;
575 # Add the field-specific value
576 $edi .= "+$val:$code";
579 # End the final GIR segment with a ' and newline