typo
[freeside.git] / httemplate / elements / tr-select-cust-part_pkg.html
1   <& /elements/xmlhttp.html,
2                 'url'  => $p.'misc/cust-part_pkg.cgi',
3                 'subs' => [ 'get_part_pkg' ],
4   &>
5
6   <SCRIPT TYPE="text/javascript">
7
8     function part_pkg_opt(what, value, text, can_discount, start_on_hold, can_start_date, start_date) {
9       var optionName = new Option(text, value, false, false);
10       optionName.setAttribute('data-can_discount',   can_discount);
11       optionName.setAttribute('data-start_on_hold',  start_on_hold);
12       optionName.setAttribute('data-can_start_date', can_start_date);
13       optionName.setAttribute('data-start_date',     start_date || '');
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       var submitButton = what.form.submitButton; // || what.form.submit;
22       if ( submitButton && <% $opt{'curr_value'} ? 0 : 1 %> ) {
23         submitButton.disabled = true; //disable the submit button
24       }
25       var discountnum = what.form.discountnum;
26       if ( discountnum ) {
27         discountnum.disabled = true; //disable discount dropdown
28       }
29
30       classnum = what.value;
31
32       function update_part_pkg(part_pkg) {
33
34         // blank the current packages
35         for ( var i = what.form.pkgpart.length; i>= 0; i-- )
36           what.form.pkgpart.options[i] = null;
37
38         // add the new packages
39         opt(what.form.pkgpart, '', 'Select package');
40         var packagesArray = eval('(' + part_pkg + ')' );
41         while (packagesArray.length > 0) {
42           //surely this should be some kind of JSON structure
43           var pkgpart        = packagesArray.shift();
44           var label          = packagesArray.shift();
45           var can_discount   = packagesArray.shift();
46           var start_on_hold  = packagesArray.shift();
47           var can_start_date = packagesArray.shift();
48           var start_date     = packagesArray.shift();
49           part_pkg_opt(
50             what.form.pkgpart, pkgpart, label, can_discount, start_on_hold, can_start_date, start_date
51           );
52         }
53
54         what.form.pkgpart.disabled = ''; //re-enable part_pkg dropdown
55 %       if ( $opt{'curr_value'} ) {
56           what.form.pkgpart.value = <% $opt{'curr_value'} %>;
57 %       }
58
59         pkg_changed();
60
61       }
62
63       get_part_pkg( <% $cust_main     ? $cust_main->custnum         : '0' %>,
64                     <% $prospect_main ? $prospect_main->prospectnum : '0' %>,
65                     classnum,
66                     update_part_pkg
67                   );
68     }
69
70     <&| /elements/onload.js &>
71       classnum_changed(document.getElementById('classnum'));
72     </&>
73
74   </SCRIPT>
75
76 % if ( scalar(@pkg_class) > 1 && ! $conf->exists('disable-cust-pkg_class') ) {
77   <TR>
78     <TH ALIGN="right"><% mt('Package Class') |h %></TH>
79     <TD COLSPAN=7>
80       <& /elements/select-cust-pkg_class.html,
81                    'curr_value' => $opt{'classnum'},
82                    'pkg_class'  => \@pkg_class,
83                    'onchange'   => 'classnum_changed',
84       &>
85     </TD>
86   </TR>
87
88 % } else { # so that the rest of the page works correctly
89   <INPUT TYPE="hidden" ID="classnum" NAME="classnum" VALUE="-1">
90 % }
91
92 <TR>
93   <TH ALIGN="right"><% mt('Package') |h %></TH>
94   <TD COLSPAN=7>
95     <& /elements/select-cust-part_pkg.html,
96                  'curr_value'     => $opt{'curr_value'}, #$pkgpart
97                  'classnum'       => $opt{'classnum'},
98                  'cust_main'      => $opt{'cust_main'},  #$cust_main
99                  'prospect_main'  => $opt{'prospect_main'},  #$prospect_main
100                  'onchange'       => 'pkg_changed',
101                  'label_callback' => sub { shift->pkg_comment },
102     &>
103   </TD>
104 </TR>
105
106 <%init>
107
108 my $conf = new FS::Conf;
109
110 my %opt = @_;
111
112 my $pre_label = $opt{'pre_label'} || '';
113 $pre_label .= ' ' if length($pre_label) && $pre_label =~ /\S$/;
114
115 my $cust_main = $opt{'cust_main'};
116 my $prospect_main = $opt{'prospect_main'};
117
118 die "neither cust_main nor prospect_main specified"
119   unless $cust_main || $prospect_main;
120
121 my $agent = $cust_main ? $cust_main->agent : $prospect_main->agent;
122
123 #"normal" part_pkg agent virtualization (agentnum or type)
124 my @part_pkg = qsearch({
125   'select'    => 'DISTINCT classnum',
126   'table'     => 'part_pkg',
127   'hashref'   => { 'disabled' => '' },
128   'extra_sql' =>
129     ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql( 'null'=>1 ).
130     ' AND '. FS::part_pkg->agent_pkgs_sql( $agent ),
131 });
132
133 my @pkg_class =
134   sort { $a->classname cmp $b->classname } #should get a sort order in config
135   map  { $_->pkg_class || new FS::pkg_class { 'classnum'  => '',
136                                               'classname' => '(none)' }
137        }
138   @part_pkg;
139
140 </%init>