package selector, split by package class, RT#5077
[freeside.git] / httemplate / elements / tr-select-cust-part_pkg.html
1 %if ( scalar(@pkg_class) > 1 && ! $conf->exists('disable-cust-pkg_class') ) {
2
3   <% include('/elements/xmlhttp.html',
4                 'url'  => $p.'misc/cust-part_pkg.cgi',
5                 'subs' => [ 'get_part_pkg' ],
6             )
7   %>
8
9   <SCRIPT TYPE="text/javascript">
10
11     function opt(what,value,text) {
12       var optionName = new Option(text, value, false, false);
13       var length = what.length;
14       what.options[length] = optionName;
15     }
16
17     function classnum_changed(what) {
18
19       what.form.pkgpart.disabled = 'disabled'; //disable part_pkg dropdown
20       what.form.submit.disabled = true; //disable the submit button
21
22       classnum = what.options[what.selectedIndex].value;
23
24       function update_part_pkg(part_pkg) {
25
26         // blank the current packages
27         for ( var i = what.form.pkgpart.length; i>= 0; i-- )
28           what.form.pkgpart.options[i] = null;
29
30         // add the new packages
31         opt(what.form.pkgpart, '', 'Select package');
32         var packagesArray = eval('(' + part_pkg + ')' );
33         for ( var s = 0; s < packagesArray.length; s=s+2 ) {
34           var packagesLabel = packagesArray[s+1];
35           opt(what.form.pkgpart, packagesArray[s], packagesLabel);
36         }
37
38         what.form.pkgpart.disabled = ''; //re-enable part_pkg dropdown
39
40       }
41
42       get_part_pkg( <% $cust_main->custnum %>, classnum, update_part_pkg );
43     
44     }
45
46   </SCRIPT>
47
48   <TR>
49     <TH ALIGN="right">Package Class</TH>
50     <TD COLSPAN=7>
51       <% include('/elements/select-cust-pkg_class.html',
52                    'curr_value' => $opt{'classnum'},
53                    'pkg_class'  => \@pkg_class,
54                    'onchange'   => 'classnum_changed',
55                 )
56       %>
57     </TD>
58   </TR>
59
60 %}
61
62 <TR>
63   <TH ALIGN="right">Package</TH>
64   <TD COLSPAN=7>
65     <% include('/elements/select-cust-part_pkg.html',
66                  'curr_value' => $opt{'curr_value'}, #$pkgpart
67                  'classnum'   => $opt{'classnum'},
68                  'cust_main'  => $opt{'cust_main'},  #$cust_main
69                  'onchange'   => 'enable_order_pkg',
70               )
71     %>
72   </TD>
73 </TR>
74
75 <%init>
76
77 my $conf = new FS::Conf;
78
79 my %opt = @_;
80
81 my $pre_label = $opt{'pre_label'} || '';
82 $pre_label .= ' ' if length($pre_label) && $pre_label =~ /\S$/;
83
84 my $cust_main = $opt{'cust_main'}
85   or die "cust_main not specified";
86
87 #my @pkg_class = sort { $a->classname cmp $b->classname }
88 #                     qsearch( 'pkg_class', { 'disabled' => '' } );
89
90 #"normal" part_pkg agent virtualization (agentnum or type)
91 my @part_pkg = qsearch({
92   'select'    => 'DISTINCT classnum',
93   'table'     => 'part_pkg',
94   'hashref'   => { 'disabled' => '' },
95   'extra_sql' =>
96     ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql( 'null'=>1 ).
97     ' AND '. FS::part_pkg->agent_pkgs_sql( $opt{'cust_main'}->agent ),
98 });
99
100 my @pkg_class =
101   sort { $a->classname cmp $b->classname } #should get a sort order in config
102   map  { $_->pkg_class || new FS::pkg_class { 'classnum'  => '',
103                                               'classname' => '(none)' }
104        }
105   @part_pkg;
106
107 </%init>