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