v4 style
[freeside.git] / httemplate / elements / tr-select-sales.html
index 2a2edf0..956a9d1 100644 (file)
@@ -13,7 +13,8 @@ Example:
 
     'label'         => 'Sales Person',
     'empty_label'   => 'Select sales person', #override default 
-    #on already# 'disable_empty' => 1,
+    'disable_empty' => 0, #on byd efault, pass 0 to disable
+    'field'         => 'salesnum', #HTML element name and ID
 
   &>
 
@@ -21,8 +22,8 @@ Example:
 % if ( scalar(@sales) == 0 || $opt{'fixed'} ) { 
 
   <INPUT TYPE  = "hidden"
-         NAME  = "<% $opt{'field'} || 'salesnum' %>"
-         ID    = "<% $opt{'field'} || 'salesnum' %>"
+         NAME  = "<% $field %>"
+         ID    = "<% $id %>"
          VALUE = "<% $salesnum %>"
   >
 
@@ -63,19 +64,106 @@ Example:
 
 % } 
 
+<& /elements/xmlhttp.html,
+              'url'  => $p.'misc/sales.cgi',
+              'subs' => [ 'get_sales' ],
+&>
+<SCRIPT TYPE="text/javascript">
+
+% # false laziness w/ elements/tr-select-cust-part_pkg.html
+
+  function <% $field %>_opt(what, value, text) {
+    var optionName = new Option(text, value, false, false);
+    var length = what.length;
+    what.options[length] = optionName;
+  }
+
+  function <% $field %>_agentnum_changed(what) {
+    what.form.<% $field %>.disabled = 'disabled'; //disable sales dropdown
+
+    if ( what.type == 'hidden' ) { 
+      agentnum = what.value;
+    } else {
+      agentnum = what.options[what.selectedIndex].value;
+    }
+
+    function update_<% $field %>(sales) {
+
+      if ( what.form.<% $field %>.type == 'hidden' ) {
+        what.form.<% $field %>.disabled = ''; //re-enable sales dropdown
+        return;
+      }
+
+      // save the current salesnum
+      var salesnum = what.form.<% $field %>.value;
+
+      // blank the current sales people
+      for ( var i = what.form.<% $field %>.length; i>= 0; i-- )
+        what.form.<% $field %>.options[i] = null;
+
+      // add the new sales people
+
+%     my @pre_options  = $opt{pre_options}  ? @{ $opt{pre_options} } : ();
+%     while ( @pre_options ) { 
+%       my $pre_opt   = shift(@pre_options);
+%       my $pre_label = shift(@pre_options);
+%       #my $selected =  ( $salesnum eq $pre_opt );
+        <% $field %>_opt( what.form.<% $field %>,
+                          <% $pre_opt   |js_string %>,
+                          <% $pre_label |js_string %>
+                        );
+%     }
+
+%     unless ( $opt{'disable_empty'} ) {
+        <% $field %>_opt( what.form.<% $field %>,
+                          '',
+                          <% $opt{'empty_label'} || '(none)' |js_string %>
+                        );
+%     }
+
+      var salesArray = eval('(' + sales + ')' );
+      for ( var s = 0; s < salesArray.length; s=s+2 ) {
+        //surely this should be some kind of JSON structure
+        var salesLabel  = salesArray[s+1];
+        <% $field %>_opt( what.form.<% $field %>, salesArray[s], salesLabel );
+      }
+
+      what.form.<% $field %>.disabled = ''; //re-enable sales dropdown
+
+      //restore salesnum if possible
+      what.form.<% $field %>.value = salesnum;
+
+    }
+
+    get_sales( agentnum,
+               update_<% $field %>
+             );
+  }
+
+  <&| /elements/onload.js &>
+    <% $field %>_agentnum_changed(document.getElementById('agentnum'));
+  </&>
+
+</SCRIPT>
+
 <%init>
 
 my $curuser = $FS::CurrentUser::CurrentUser;
 
 my %opt = @_;
 my $salesnum = $opt{'curr_value'} || $opt{'value'};
+my $field = $opt{'element_name'} || $opt{'field'} || 'salesnum';
+my $id = $opt{'id'} || $opt{'field'} || 'salesnum';
 
-my @sales = qsearch({ 'table'   => 'sales',
-                      'hashref' => { 'disabled' => '' },
+my $hashref = { disabled => '' };
+$hashref->{agentnum} = $opt{agentnum} if $opt{agentnum};
+
+my @sales = qsearch({ 'table'     => 'sales',
+                      'hashref'   => $hashref,
                       'extra_sql' => ' AND '. $curuser->agentnums_sql,
+                      'order_by'  => 'ORDER BY salesperson',
                    });
 
 my $colspan = $opt{'colspan'} ? 'COLSPAN="'.$opt{'colspan'}.'"' : '';
 
-
 </%init>