continue sales person work: customer and package selection, commissions, reporting...
authorIvan Kohler <ivan@freeside.biz>
Sun, 25 Aug 2013 23:23:17 +0000 (16:23 -0700)
committerIvan Kohler <ivan@freeside.biz>
Sun, 25 Aug 2013 23:23:17 +0000 (16:23 -0700)
httemplate/edit/cust_main/top_misc.html
httemplate/elements/tr-select-sales.html
httemplate/elements/xmlhttp.html
httemplate/misc/sales.cgi [new file with mode: 0644]
httemplate/search/report_cust_pkg.html

index d8141b5..ebd9b92 100644 (file)
@@ -20,7 +20,9 @@
        <% $cust_main->residential_commercial eq 'Commercial' ? 'CHECKED' : '' %>
   ></TD>
 </TR>
+
 <SCRIPT TYPE="text/javascript">
+
   function rescom_changed(what) {
     if ( what.checked == (what.value == 'Commercial' ) ) {
       document.getElementById('company_row').style.display = '';
     }
   }
 
+
   var ship_locked_agents = <% encode_json(\%ship_locked_agents) %>;
   var ship_fields = ['address1', 'city', 'state', 'zip', 'country', 
     'latitude', 'longitude', 'district'];
+
   function agent_changed(what) {
     var agentnum = what.value;
+
+%   # unlock/lock service location
+
     var f = what.form;
     if ( ship_locked_agents[agentnum] ) {
 %     # For this agent, the service location (except address2)
       f['same'].disabled = false;
     }
     samechanged(f['same']);
+
+%   # update sales dropdown
+    salesnum_agentnum_changed(what);
+
   }
+
   <&| /elements/onload.js &>
   agent_changed(document.getElementById('agentnum'))
   </&>
index 2a2edf0..b69b3d8 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,99 @@ 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
+
+    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' => '' },
                       'extra_sql' => ' AND '. $curuser->agentnums_sql,
+                      'order_by'  => 'ORDER BY salesperson',
                    });
 
 my $colspan = $opt{'colspan'} ? 'COLSPAN="'.$opt{'colspan'}.'"' : '';
 
-
 </%init>
index a9e65c7..2f4f0d5 100644 (file)
@@ -14,17 +14,19 @@ Example:
   );
 
 </%doc>
+<%shared>
+my %initialized = ();#won't work if component is "preloaded"... so don't do that
+</%shared>
 <& /elements/rs_init_object.html &>
 <& /elements/init_overlib.html &>
 <SCRIPT TYPE="text/javascript">
 
 % foreach my $func ( @{$opt{'subs'}} ) { 
 %
-%       my $furl = $url;
-%       $furl =~ s/\"/\\\\\"/; #javascript escape
-%#"
-%  
-
+%   next if $initialized{$key.$func}++;
+%
+%   my $furl = $url;
+%   $furl =~ s/\"/\\\\\"/; #javascript escape (fix gvim syntax: ")
 
     function <%$key%><%$func%>() {
         // count args; build URL
diff --git a/httemplate/misc/sales.cgi b/httemplate/misc/sales.cgi
new file mode 100644 (file)
index 0000000..1e56371
--- /dev/null
@@ -0,0 +1,27 @@
+<% encode_json( \@return ) %>\
+<%init>
+
+my( $agentnum ) = $cgi->param('arg');
+
+my %hash = ( 'disabled' => '' );
+if ( $agentnum > 0 ) {
+  $hash{'agentnum'} = $agentnum;
+}
+my @sales = qsearch({
+  'table'     => 'sales',
+  'hashref'   => \%hash,
+  'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql,
+  'order_by'  => 'ORDER BY salesperson',
+});
+
+warn scalar(@sales);
+
+my @return = map  {
+                    ( $_->salesnum,
+                      $_->salesperson,
+                    )
+                  }
+                  #sort { $a->salesperson cmp $b->salesperson }
+                  @sales;
+
+</%init>
index 6cf297c..0a17344 100755 (executable)
     <% include( '/elements/tr-select-agent.html',
                    'curr_value'    => scalar( $cgi->param('agentnum') ),
                    'disable_empty' => 0,
+                   'onchange'      => 'agent_changed(this)',
                )
     %>
 
+    <SCRIPT TYPE="text/javascript">
+
+      function agent_changed(what) {
+%       # update sales dropdowns
+        salesnum_agentnum_changed(what);
+        cust_main_salesnum_agentnum_changed(what);
+      }
+
+      <&| /elements/onload.js &>
+      agent_changed(document.getElementById('agentnum'))
+      </&>
+    </SCRIPT>
+
     <& /elements/tr-select-sales.html,
                   'label'         => 'Customer sales person',
                   'element_name'  => 'cust_main_salesnum',