CAT-69 Volume merge bug
authorKyle Huckins <khuckins@catalystdevworks.com>
Fri, 12 Aug 2016 16:54:30 +0000 (09:54 -0700)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
Create function in update_items to handle
find_or_create_volume api call.

Create helper function to match acns and get
the amount of copies in supplied volume in PO.

Create helper function to get number copies from
current volume.

Clean up gather_copies().

Remove instances of direct attempts at modifying
IDL properties, replace with the proper method
calls.

Add helper function to set the acn for the items in the
current PO.

Add FM_ACN_RETRIEVE.authoritative call to find_or_create
helper function.

Set temp_acn's label class, prefix, and suffix
to their object's id within g.find_or_create_volume().

Remove unnecessary loop from g.render_batch_button()

Fix incorrect iteration style to
array-friendly one.

Signed-off-by: Kyle Huckins <khuckins@catalystdevworks.com>
 Changes to be committed:
modified:   Open-ILS/xul/staff_client/server/cat/update_items.js

Open-ILS/xul/staff_client/server/cat/update_items.js

index 9e7a318..5447a2a 100644 (file)
@@ -894,6 +894,74 @@ g.generate_barcodes = function() {
     }
 }
 
+//Set acn_id of applicable barcodes in the current PO to that of the new volume
+//Takes the array of barcode fields, the original acn_id, and the desired acn_id
+g.set_acn_id_for_current_po = function(barcodes, acn_id, new_acn_id) {
+    for(var i = 0; i < barcodes.length; i++) {
+        if(barcodes[i].getAttribute('acn_id') == acn_id) barcodes[i].setAttribute('acn_id', new_acn_id);
+    }
+}
+
+//Return an array with only items in a specific volume, and only ones that we're 
+//specifically working with(copies in a volume within the PO)
+g.match_acn = function(acn_id, barcodes) {
+    currentOrderBarcodes = [];
+    for(var i = 0; i < barcodes.length; i++) {
+        if(barcodes[i].getAttribute('acn_id') == acn_id) currentOrderBarcodes.push(barcodes[i]);
+    }
+    return currentOrderBarcodes;
+}
+
+//Return an integer value equal to the number of copies in a volume
+g.get_copies_in_volume = function(tcn, ou_id, callnumber) {
+    var ou_list = [];
+    ou_list.push(ou_id);
+    var copyInVolumeCount = 0;
+    var copiesInVolume = g.network.simple_request(
+        'FM_ACN_TREE_LIST_RETRIEVE_VIA_RECORD_ID_AND_ORG_IDS',
+        [ses(), tcn, ou_list]
+    );
+    if (typeof copiesInVolume.ilsevent != 'undefined') {
+        alert('error with get_copies_in_volume: ' + js2JSON(r));
+    }
+
+    for(x = 0; x < copiesInVolume.length; x++) {
+        if(copiesInVolume[x].label() == callnumber) {
+            copyInVolumeCount = copiesInVolume[x].copies().length;
+        }
+    }
+
+    return copyInVolumeCount;
+}
+
+//Find or Create Volume call helper function
+//If you find you need to use this API call, it may be simpler to call this function.
+//To use this, you'll need to supply a callnumber label, the TCN, an Org Unit, and acnp/s/c ids.
+g.find_or_create_volume = function(callnumber, tcn, ou_id) {
+    var r = g.network.simple_request(
+            'FM_ACN_FIND_OR_CREATE',
+            [ses(), callnumber, tcn, ou_id]
+    );
+    if (typeof r.ilsevent != 'undefined') {
+        alert('error with volume find/create: ' + js2JSON(r));
+    }
+
+    if (typeof g.map_acn[r.acn_id] == 'undefined') {
+        var temp_acn = g.network.simple_request(
+            'FM_ACN_RETRIEVE.authoritative',
+            [r.acn_id]
+        );
+
+        //Make sure we're only grabbing the id for acnc/p/s fields
+        temp_acn.label_class(temp_acn.label_class().id());
+        temp_acn.prefix(temp_acn.prefix().id());
+        temp_acn.suffix(temp_acn.suffix().id());
+
+        g.map_acn[r.acn_id] = temp_acn;
+    }
+    return g.map_acn[r.acn_id];
+}
+
 g.new_acp_id = -1;
 g.new_acn_id = -1;
 
@@ -903,56 +971,61 @@ g.gather_copies = function() {
         var nl = document.getElementsByTagName('textbox');
 
         var copies = g.existing_copies;
-
+        var callnumbers = [];
+        var currentOrderBarcodes = [];
+        var copiesInVolume = [];
         var barcodes = [];
         var notes = [];
-        var v_count = 0;
         g.notes = [];
         var copy_map = {};
 
-        // grab any copy_notes
+        // collect our text fields and notes
         for (var i = 0; i < nl.length; i++) {
+            if (nl[i].getAttribute('rel_vert_pos') == rel_vert_pos_copy_note) notes.push( nl[i] );
+            if (nl[i].getAttribute('rel_vert_pos') == rel_vert_pos_barcode) barcodes.push( nl[i] );
+            if (nl[i].getAttribute('rel_vert_pos') == rel_vert_pos_call_number) callnumbers.push(nl[i]);
+        }
 
-            if ( nl[i].getAttribute('rel_vert_pos') == rel_vert_pos_copy_note ){
+        //gather callnumber info
+        for (var i = 0; i < callnumbers.length; i++) {
+            var ou_id = callnumbers[i].getAttribute('ou_id');
+            var acn_id = callnumbers[i].getAttribute('acn_id');
 
-                notes.push( nl[i] );
+            if (!acn_id) {
+                acn_id = g.new_acn_id--;
+                callnumbers[i].setAttribute('acn_id',acn_id);
             }
 
-            if ( nl[i].getAttribute('rel_vert_pos') == rel_vert_pos_barcode ) barcodes.push( nl[i] );
-
-            if ( nl[i].getAttribute('rel_vert_pos') == rel_vert_pos_call_number )  {
-
-                v_count++;
-                var ou_id = nl[i].getAttribute('ou_id');
-                var acn_id = nl[i].getAttribute('acn_id');
-
-                if (!acn_id) {
-
-                    acn_id = g.new_acn_id--;
-                    nl[i].setAttribute('acn_id',acn_id);
-                }
-
-                var callnumber = nl[i].value;
-
-                if (!(acn_id in g.map_acn)){
-
-                    // FIXME This needs to create a valid and helpful acn
-                    //var new_volume = new acn();
-                    //new_volume.id(acn_id);
-                    //new_volume.id(acn_id);
-                    //new_volume.id(acn_id);
-                }
-
-                else{
-
-                    // If there's a new label, update it and mark volume as 'changed'
-                    if (g.map_acn[acn_id].label() != callnumber){
+            var callnumber = callnumbers[i].value;
+
+            if (!(acn_id in g.map_acn)){
+                // FIXME This needs to create a valid and helpful acn
+                //var new_volume = new acn();
+                //new_volume.id(acn_id);
+                //new_volume.id(acn_id);
+                //new_volume.id(acn_id);
+            } else {
+                // If there's a new label, update it and mark volume as 'changed'
+                if (g.map_acn[acn_id].label() != callnumber) {
+
+                    //Do a check to make sure we're only editing these specific copies
+                    currentOrderBarcodes = g.match_acn(acn_id, barcodes);
+                    copiesInVolume = g.get_copies_in_volume(g.map_acn[acn_id].record(), ou_id, g.map_acn[acn_id].label());
+
+                    //Either update the volume or find/create a new one, depending on 
+                    //volumes in if we're working with every copy in a volume or just a few
+                    if(copiesInVolume > currentOrderBarcodes.length) {
+                        var new_volume = g.find_or_create_volume(callnumber, g.map_acn[acn_id].record(), ou_id);
+                        g.set_acn_id_for_current_po(barcodes, acn_id, new_volume.id());
+                        new_volume.label(callnumber);
+                        new_volume.ischanged(get_db_true());
+                    } else {
                         g.map_acn[acn_id].label(callnumber);                       
                         g.map_acn[acn_id].ischanged(get_db_true());
                     }
                 }
             }
-        };
+        }
 
         // gather barcode info
         for (var i = 0; i < barcodes.length; i++) {
@@ -966,10 +1039,10 @@ g.gather_copies = function() {
             }
 
             var ou_id = barcodes[i].getAttribute('ou_id');
-            //var callnumber_callkey = barcodes[i].getAttribute('callkey');
             var barcode = barcodes[i].value;
 
-            copy_map[acp_id] = {"ou_id" : ou_id, "barcode" : barcode, "acn_id" : acn_id }
+            //Check if acn_id is being changed to a new/different volume
+            copy_map[acp_id] = {"ou_id" : ou_id, "barcode" : barcode, "acn_id" : acn_id }; 
 
             var note = null;
 
@@ -995,12 +1068,12 @@ g.gather_copies = function() {
 
                 if (copies[i].barcode() != copy_map[copies[i].id()].barcode){
                     copies[i].barcode(copy_map[copies[i].id()].barcode);
-                    copies[i].a[45] = get_db_true(); // setting ischanged to true
+                    copies[i].ischanged(get_db_true()); // setting ischanged to true
                 }
 
                 if (copies[i].call_number() != copy_map[copies[i].id()].acn_id){
                     copies[i].call_number(copy_map[copies[i].id()].acn_id);
-                    copies[i].a[45] = get_db_true(); // setting ischanged to true
+                    copies[i].ischanged(get_db_true()); // setting ischanged to true
                 }
 
                 delete copy_map[copies[i].id()];
@@ -1104,8 +1177,7 @@ g.stash_and_close = function(param, keepOpen) {
                // Check for merged volumes
         var copy_ids = [];
 
-               for (var c in copies){
-
+        for(var c = 0; c < copies.length; c++) {
             copy_ids.push(copies[c].id());
         }
 
@@ -1113,8 +1185,7 @@ g.stash_and_close = function(param, keepOpen) {
             'FM_ACP_UNFLESHED_BATCH_RETRIEVE',
             [ copy_ids ]
         );
-
-        if (typeof r.ilsevent != 'undefined') {
+        if (typeof copies_to_check.ilsevent != 'undefined') {
             alert('error with volume update: ' + js2JSON(r));
         }
 
@@ -1124,19 +1195,16 @@ g.stash_and_close = function(param, keepOpen) {
         }
 
         var label_editor_func;
-
         if (copies.length > 0) {
 
             var altered_copies = [];
             var remaining_copies = [];
 
-            for (var c in copies){
-
-                for (var i in copies_to_check){
-
-                    if (copies_to_check[i].id() == copies[c].id()){
+            for(var c = 0; c < copies.length; c++) {
+                for (var i = 0; i < copies_to_check; i++) {
 
-                        copies[c].call_number( copies_to_check[i].call_number() );
+                    if (copies_to_check[i].id() == copies[c].id()) {
+                        copies[c].call_number(copies_to_check[i].call_number());
                         break;
                     }
                 }
@@ -1166,7 +1234,6 @@ g.stash_and_close = function(param, keepOpen) {
 
             // User clicked 'Save'
             else {
-
                 // Update copies
                 var r = g.network.simple_request(
                     'FM_ACP_FLESHED_BATCH_UPDATE',
@@ -1462,13 +1529,6 @@ g.render_batch_button = function() {
                         nl[i].value = label; 
                     }
                 }
-
-                for (var i in g.map_acn){
-                    if (g.map_acn[i].label() != label){  
-                        g.map_acn[i].label(label); 
-                        g.map_acn[i].ischanged(get_db_true());
-                   }
-                } 
             }              
             if (g.last_focus){