package start_on_hold flag, and better behavior for automatic timers + packages on...
[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       }
60
61       get_part_pkg( <% $cust_main     ? $cust_main->custnum         : '0' %>,
62                     <% $prospect_main ? $prospect_main->prospectnum : '0' %>,
63                     classnum,
64                     update_part_pkg
65                   );
66     }
67
68     <&| /elements/onload.js &>
69       classnum_changed(document.getElementById('classnum'));
70     </&>
71
72   </SCRIPT>
73
74 % if ( scalar(@pkg_class) > 1 && ! $conf->exists('disable-cust-pkg_class') ) {
75   <TR>
76     <TH ALIGN="right"><% mt('Package Class') |h %></TH>
77     <TD COLSPAN=7>
78       <& /elements/select-cust-pkg_class.html,
79                    'curr_value' => $opt{'classnum'},
80                    'pkg_class'  => \@pkg_class,
81                    'onchange'   => 'classnum_changed',
82       &>
83     </TD>
84   </TR>
85
86 % } else { # so that the rest of the page works correctly
87   <INPUT TYPE="hidden" ID="classnum" NAME="classnum" VALUE="-1`">
88 % }
89
90 <TR>
91   <TH ALIGN="right"><% mt('Package') |h %></TH>
92   <TD COLSPAN=7>
93     <& /elements/select-cust-part_pkg.html,
94                  'curr_value'    => $opt{'curr_value'}, #$pkgpart
95                  'classnum'      => $opt{'classnum'},
96                  'cust_main'     => $opt{'cust_main'},  #$cust_main
97                  'prospect_main' => $opt{'prospect_main'},  #$prospect_main
98                  'onchange'      => 'pkg_changed',
99     &>
100   </TD>
101 </TR>
102
103 <%init>
104
105 my $conf = new FS::Conf;
106
107 my %opt = @_;
108
109 my $pre_label = $opt{'pre_label'} || '';
110 $pre_label .= ' ' if length($pre_label) && $pre_label =~ /\S$/;
111
112 my $cust_main = $opt{'cust_main'};
113 my $prospect_main = $opt{'prospect_main'};
114
115 die "neither cust_main nor prospect_main specified"
116   unless $cust_main || $prospect_main;
117
118 my $agent = $cust_main ? $cust_main->agent : $prospect_main->agent;
119
120 #"normal" part_pkg agent virtualization (agentnum or type)
121 my @part_pkg = qsearch({
122   'select'    => 'DISTINCT classnum',
123   'table'     => 'part_pkg',
124   'hashref'   => { 'disabled' => '' },
125   'extra_sql' =>
126     ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql( 'null'=>1 ).
127     ' AND '. FS::part_pkg->agent_pkgs_sql( $agent ),
128 });
129
130 my @pkg_class =
131   sort { $a->classname cmp $b->classname } #should get a sort order in config
132   map  { $_->pkg_class || new FS::pkg_class { 'classnum'  => '',
133                                               'classname' => '(none)' }
134        }
135   @part_pkg;
136
137 </%init>