fix A/R report
[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     if ( what.type == 'hidden' ) { 
85       agentnum = what.value;
86     } else {
87       agentnum = what.options[what.selectedIndex].value;
88     }
89
90     function update_<% $field %>(sales) {
91
92       if ( what.form.<% $field %>.type == 'hidden' ) {
93         what.form.<% $field %>.disabled = ''; //re-enable sales dropdown
94         return;
95       }
96
97       // save the current salesnum
98       var salesnum = what.form.<% $field %>.value;
99
100       // blank the current sales people
101       for ( var i = what.form.<% $field %>.length; i>= 0; i-- )
102         what.form.<% $field %>.options[i] = null;
103
104       // add the new sales people
105
106 %     my @pre_options  = $opt{pre_options}  ? @{ $opt{pre_options} } : ();
107 %     while ( @pre_options ) { 
108 %       my $pre_opt   = shift(@pre_options);
109 %       my $pre_label = shift(@pre_options);
110 %       #my $selected =  ( $salesnum eq $pre_opt );
111         <% $field %>_opt( what.form.<% $field %>,
112                           <% $pre_opt   |js_string %>,
113                           <% $pre_label |js_string %>
114                         );
115 %     }
116
117 %     unless ( $opt{'disable_empty'} ) {
118         <% $field %>_opt( what.form.<% $field %>,
119                           '',
120                           <% $opt{'empty_label'} || '(none)' |js_string %>
121                         );
122 %     }
123
124       var salesArray = eval('(' + sales + ')' );
125       for ( var s = 0; s < salesArray.length; s=s+2 ) {
126         //surely this should be some kind of JSON structure
127         var salesLabel  = salesArray[s+1];
128         <% $field %>_opt( what.form.<% $field %>, salesArray[s], salesLabel );
129       }
130
131       what.form.<% $field %>.disabled = ''; //re-enable sales dropdown
132
133       //restore salesnum if possible
134       what.form.<% $field %>.value = salesnum;
135
136     }
137
138     get_sales( agentnum,
139                update_<% $field %>
140              );
141   }
142
143   <&| /elements/onload.js &>
144     <% $field %>_agentnum_changed(document.getElementById('agentnum'));
145   </&>
146
147 </SCRIPT>
148
149 <%init>
150
151 my $curuser = $FS::CurrentUser::CurrentUser;
152
153 my %opt = @_;
154 my $salesnum = $opt{'curr_value'} || $opt{'value'};
155 my $field = $opt{'element_name'} || $opt{'field'} || 'salesnum';
156 my $id = $opt{'id'} || $opt{'field'} || 'salesnum';
157
158 my $hashref = { disabled => '' };
159 $hashref->{agentnum} = $opt{agentnum} if $opt{agentnum};
160
161 my @sales = qsearch({ 'table'     => 'sales',
162                       'hashref'   => $hashref,
163                       'extra_sql' => ' AND '. $curuser->agentnums_sql,
164                       'order_by'  => 'ORDER BY salesperson',
165                    });
166
167 my $colspan = $opt{'colspan'} ? 'COLSPAN="'.$opt{'colspan'}.'"' : '';
168
169 </%init>