From: erickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Date: Fri, 9 Oct 2009 19:47:59 +0000 (+0000)
Subject: initial support for applying distribution formulas to a set of lineitem_details.... 
X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=f41a3d38d03dfed7bf6d10442e6c46e707047ae1;p=evergreen%2Fmasslnc.git

initial support for applying distribution formulas to a set of lineitem_details.  TODO: need to clean up the styling, support formula chains (appying multiples), storing which formula was used (somehwere)

git-svn-id: svn://svn.open-ils.org/ILS/trunk@14340 dcc99617-32d9-48b4-a31d-7c20da2025e4
---

diff --git a/Open-ILS/web/js/ui/default/acq/common/li_table.js b/Open-ILS/web/js/ui/default/acq/common/li_table.js
index 3fa3643e2e..d52b453c79 100644
--- a/Open-ILS/web/js/ui/default/acq/common/li_table.js
+++ b/Open-ILS/web/js/ui/default/acq/common/li_table.js
@@ -501,6 +501,7 @@ function AcqLiTable() {
 
         this._fetchDistribFormulas(
             function() {
+                self._addDistribFormulaRow();
                 openils.acq.Lineitem.fetchAttrDefs(
                     function() { 
                         self._fetchLineitem(liId, function(li){self._drawCopies(li);}); 
@@ -510,6 +511,106 @@ function AcqLiTable() {
         );
     };
 
+    /**
+     * Insert a new row into the distribution formula selection form
+     */
+    this._addDistribFormulaRow = function() {
+        var self = this;
+
+        if(!self.distribFormulaStore) {
+            // no formulas, hide the form
+            openils.Util.hide('acq-lit-distrib-formula-tbody');
+            return;
+        }
+
+        if(!this.distribFormulaTemplate) 
+            this.distribFormulaTemplate = 
+                dojo.byId('acq-lit-distrib-formula-tbody').removeChild(dojo.byId('acq-lit-distrib-form-row'));
+
+        var row = dojo.byId('acq-lit-distrib-formula-tbody').appendChild(this.distribFormulaTemplate.cloneNode(true));
+
+        var selector = new dijit.form.FilteringSelect(
+            {store : self.distribFormulaStore}, 
+            nodeByName('selector', row)
+        );
+
+        var apply = new dijit.form.Button(
+            {label : 'Apply'},  // TODO i18n
+            nodeByName('set_button', row)
+        ); 
+
+        var release = new dijit.form.Button(
+            {label : 'Release', disabled: true}, // TODO i18n
+            nodeByName('rel_button', row)  
+        );
+
+        dojo.connect(apply, 'onClick', 
+            function() {
+                var form_id = selector.attr('value');
+                if(!form_id) return;
+                apply.attr('disabled', true);
+                release.attr('disabled', false);
+                self._applyDistribFormula(form_id);
+            }
+        );
+
+        dojo.connect(release, 'onClick', 
+            function() {
+                apply.attr('disabled', false);
+                release.attr('disabled', true);
+            }
+        );
+    };
+
+    /**
+     * Applies a distrib formula to the current set of copies
+     */
+    this._applyDistribFormula = function(formula) {
+        if(!formula) return;
+
+        formula = this.distribForms.filter(
+            function(form) {
+                return form.id() == formula;
+            }
+        )[0];
+
+        var copyRows = dojo.query('tr', self.copyTbody);
+
+        for(var rowIndex = 0; rowIndex < copyRows.length; rowIndex++) {
+            
+            var row = copyRows[rowIndex];
+            var copy_id = row.getAttribute('copy_id');
+            var copyWidgets = this.copyWidgetCache[copy_id];
+            var entryIndex = 0;
+            var entry = null;
+
+            // find the correct entry for the current row
+            dojo.forEach(formula.entries(), 
+                function(e) {
+                    if(!entry) {
+                        entryIndex += e.item_count();
+                        if(entryIndex > rowIndex)
+                            entry = e;
+                    }
+                }
+            );
+
+            if(entry) {
+                
+                //console.log("rowIndex = " + rowIndex + ", entry = " + entry.id() + ", entryIndex=" + 
+                //  entryIndex + ", owning_lib = " + entry.owning_lib() + ", location = " + entry.location());
+    
+                dojo.forEach(
+                    ['owning_lib', 'location'], 
+                    function(field) {
+                        if(entry[field]()) 
+                            copyWidgets[field].attr('value', (entry[field]()));
+                    }
+                );
+            }
+        }
+    };
+
     this._fetchDistribFormulas = function(onload) {
         if(this.distribForms) {
             onload();
diff --git a/Open-ILS/web/templates/default/acq/common/li_table.tt2 b/Open-ILS/web/templates/default/acq/common/li_table.tt2
index f0ac801e2e..52a798124d 100644
--- a/Open-ILS/web/templates/default/acq/common/li_table.tt2
+++ b/Open-ILS/web/templates/default/acq/common/li_table.tt2
@@ -202,20 +202,20 @@
                     </td>
                 </tr>
             </tbody>
-            <!--
-            <tbody><tr>
-                <td colspan='0'>
-                    <a href='javascript:void(0);' 
-                        onclick='openils.Util.toggle("acq-lit-distrib-formula-tbody");'>Distribution Formulas</a>
-                </td>
-            </tr></tbody>
-            -->
-            <tbody id='acq-lit-distrib-formula-tbody' class='hidden'>
+
+            <tbody><tr><td class='acq-lit-table-spacer' colspan='0'/></tr></tbody>
+
+            <tbody id='acq-lit-distrib-formula-tbody'>
                 <tr id='acq-lit-distrib-form-row'>
-                    <td name='selector'/>
-                    <td name='setbutton'/>
+                    <td colspan='0'>
+                        <span>Distribution Formulas</span>
+                        <div name='selector'></div>
+                        <div name='set_button'></div>
+                        <div name='rel_button'></div>
+                    </td>
                 </tr>
             </tbody>
+
             <tbody><tr><td class='acq-lit-table-spacer' colspan='0'/></tr></tbody>
             <tbody style='font-weight:bold;'>
                 <tr>