initial support for applying distribution formulas to a set of lineitem_details....
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 9 Oct 2009 19:47:59 +0000 (19:47 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 9 Oct 2009 19:47:59 +0000 (19:47 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@14340 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/web/js/ui/default/acq/common/li_table.js
Open-ILS/web/templates/default/acq/common/li_table.tt2

index 3fa3643..d52b453 100644 (file)
@@ -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();
index f0ac801..52a7981 100644 (file)
                     </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>