LP#1373690 rearrange experiment
authorBill Erickson <berickxx@gmail.com>
Thu, 5 Nov 2015 18:50:50 +0000 (13:50 -0500)
committerBill Erickson <berickxx@gmail.com>
Thu, 5 Nov 2015 18:50:50 +0000 (13:50 -0500)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor/GeneratePurchaseOrderJEDI.pm
Open-ILS/src/sql/Pg/upgrade/XXXX.data.acq-order-edi.sql

index c6d761a..f7a35d4 100644 (file)
@@ -37,7 +37,7 @@ sub get_li_attr {
 }
 
 # helper functions inserted into the TT environment
-my $_TT_helpers; # define first so one helper can use another
+our $_TT_helpers; # define first so one helper can use another
 $_TT_helpers = {
 
     # turns a date into something TT can understand
index f7e9fc5..4a6e8cd 100644 (file)
@@ -14,9 +14,129 @@ ABOUT
 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;
 
index e7169c1..78c3208 100644 (file)
@@ -81,43 +81,11 @@ SEGMENT_COUNT = 13;
 # 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; 
@@ -129,7 +97,6 @@ FOR li IN target.lineitems;
             END;
         END;
     END; 
-    IF xtra_ftx; ftx_vals.unshift(xtra_ftx); END; 
 
     notes = [];
 
@@ -150,60 +117,6 @@ FOR li IN target.lineitems;
 
     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