summaryrefslogtreecommitdiff
path: root/httemplate/elements/tr-select-sales.html
blob: 956a9d1590bebefb0d5169c3f85ab0316c034021 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<%doc>

Example:

  <& /elements/tr-select-sales.html',

    #recommended to keep things "sticky" on errors
    'curr_value'    => $curr_value,

    ##
    # optional
    ##

    'label'         => 'Sales Person',
    'empty_label'   => 'Select sales person', #override default 
    'disable_empty' => 0, #on byd efault, pass 0 to disable
    'field'         => 'salesnum', #HTML element name and ID

  &>

</%doc>
% if ( scalar(@sales) == 0 || $opt{'fixed'} ) { 

  <INPUT TYPE  = "hidden"
         NAME  = "<% $field %>"
         ID    = "<% $id %>"
         VALUE = "<% $salesnum %>"
  >

%   if ( scalar(@sales) > 0 ) {
      <TR>
%       if ( $opt{'th'} ) {
          <TH ALIGN="right"><% $opt{'label'} || emt('Sales Person') %></TH>
%       } else {
          <TD ALIGN="right"><% $opt{'label'} || emt('Sales Person') %></TD>
%       }
        <TD BGCOLOR="#dddddd" <% $colspan %>>
%         my $sales = qsearchs('sales', { 'salesnum' => $salesnum });
          <% $sales ? $sales->salesperson : '(none)' |h %>
        </TD>
      </TR>

%   } else { # YUCK.  empty row so we don't throw g_row in edit.html off :/
      <TR>
      </TR>
%   }
%
% } else { 

  <TR>
%   if ( $opt{'th'} ) {
      <TH ALIGN="right"><% $opt{'label'} || emt('Sales Person') %></TH>
%   } else {
      <TD ALIGN="right"><% $opt{'label'} || emt('Sales Person') %></TD>
%   }
    <TD <% $colspan %>>
      <& /elements/select-sales.html,
                     'curr_value' => $salesnum,
                     'sales'     => \@sales,
                     %opt,
      &>
    </TD>
  </TR>

% } 

<& /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 $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>