continue sales person work: customer and package selection, commissions, reporting...
[freeside.git] / httemplate / elements / tr-select-sales.html
1 <%doc>
2
3 Example:
4
5   <& /elements/tr-select-sales.html',
6
7     #recommended to keep things "sticky" on errors
8     'curr_value'    => $curr_value,
9
10     ##
11     # optional
12     ##
13
14     'label'         => 'Sales Person',
15     'empty_label'   => 'Select sales person', #override default 
16     'disable_empty' => 0, #on byd efault, pass 0 to disable
17     'field'         => 'salesnum', #HTML element name and ID
18
19   &>
20
21 </%doc>
22 % if ( scalar(@sales) == 0 || $opt{'fixed'} ) { 
23
24   <INPUT TYPE  = "hidden"
25          NAME  = "<% $field %>"
26          ID    = "<% $id %>"
27          VALUE = "<% $salesnum %>"
28   >
29
30 %   if ( scalar(@sales) > 0 ) {
31       <TR>
32 %       if ( $opt{'th'} ) {
33           <TH ALIGN="right"><% $opt{'label'} || emt('Sales Person') %></TH>
34 %       } else {
35           <TD ALIGN="right"><% $opt{'label'} || emt('Sales Person') %></TD>
36 %       }
37         <TD BGCOLOR="#dddddd" <% $colspan %>>
38 %         my $sales = qsearchs('sales', { 'salesnum' => $salesnum });
39           <% $sales ? $sales->salesperson : '(none)' |h %>
40         </TD>
41       </TR>
42
43 %   } else { # YUCK.  empty row so we don't throw g_row in edit.html off :/
44       <TR>
45       </TR>
46 %   }
47 %
48 % } else { 
49
50   <TR>
51 %   if ( $opt{'th'} ) {
52       <TH ALIGN="right"><% $opt{'label'} || emt('Sales Person') %></TH>
53 %   } else {
54       <TD ALIGN="right"><% $opt{'label'} || emt('Sales Person') %></TD>
55 %   }
56     <TD <% $colspan %>>
57       <& /elements/select-sales.html,
58                      'curr_value' => $salesnum,
59                      'sales'     => \@sales,
60                      %opt,
61       &>
62     </TD>
63   </TR>
64
65 % } 
66
67 <& /elements/xmlhttp.html,
68               'url'  => $p.'misc/sales.cgi',
69               'subs' => [ 'get_sales' ],
70 &>
71 <SCRIPT TYPE="text/javascript">
72
73 % # false laziness w/ elements/tr-select-cust-part_pkg.html
74
75   function <% $field %>_opt(what, value, text) {
76     var optionName = new Option(text, value, false, false);
77     var length = what.length;
78     what.options[length] = optionName;
79   }
80
81   function <% $field %>_agentnum_changed(what) {
82     what.form.<% $field %>.disabled = 'disabled'; //disable sales dropdown
83
84     agentnum = what.options[what.selectedIndex].value;
85
86     function update_<% $field %>(sales) {
87
88       if ( what.form.<% $field %>.type == 'hidden' ) {
89         what.form.<% $field %>.disabled = ''; //re-enable sales dropdown
90         return;
91       }
92
93       // save the current salesnum
94       var salesnum = what.form.<% $field %>.value;
95
96       // blank the current sales people
97       for ( var i = what.form.<% $field %>.length; i>= 0; i-- )
98         what.form.<% $field %>.options[i] = null;
99
100       // add the new sales people
101
102 %     my @pre_options  = $opt{pre_options}  ? @{ $opt{pre_options} } : ();
103 %     while ( @pre_options ) { 
104 %       my $pre_opt   = shift(@pre_options);
105 %       my $pre_label = shift(@pre_options);
106 %       #my $selected =  ( $salesnum eq $pre_opt );
107         <% $field %>_opt( what.form.<% $field %>,
108                           <% $pre_opt   |js_string %>,
109                           <% $pre_label |js_string %>
110                         );
111 %     }
112
113 %     unless ( $opt{'disable_empty'} ) {
114         <% $field %>_opt( what.form.<% $field %>,
115                           '',
116                           <% $opt{'empty_label'} || '(none)' |js_string %>
117                         );
118 %     }
119
120       var salesArray = eval('(' + sales + ')' );
121       for ( var s = 0; s < salesArray.length; s=s+2 ) {
122         //surely this should be some kind of JSON structure
123         var salesLabel  = salesArray[s+1];
124         <% $field %>_opt( what.form.<% $field %>, salesArray[s], salesLabel );
125       }
126
127       what.form.<% $field %>.disabled = ''; //re-enable sales dropdown
128
129       //restore salesnum if possible
130       what.form.<% $field %>.value = salesnum;
131
132     }
133
134     get_sales( agentnum,
135                update_<% $field %>
136              );
137   }
138
139   <&| /elements/onload.js &>
140     <% $field %>_agentnum_changed(document.getElementById('agentnum'));
141   </&>
142
143 </SCRIPT>
144
145 <%init>
146
147 my $curuser = $FS::CurrentUser::CurrentUser;
148
149 my %opt = @_;
150 my $salesnum = $opt{'curr_value'} || $opt{'value'};
151 my $field = $opt{'element_name'} || $opt{'field'} || 'salesnum';
152 my $id = $opt{'id'} || $opt{'field'} || 'salesnum';
153
154 my @sales = qsearch({ 'table'   => 'sales',
155                       'hashref' => { 'disabled' => '' },
156                       'extra_sql' => ' AND '. $curuser->agentnums_sql,
157                       'order_by'  => 'ORDER BY salesperson',
158                    });
159
160 my $colspan = $opt{'colspan'} ? 'COLSPAN="'.$opt{'colspan'}.'"' : '';
161
162 </%init>