v4 style
[freeside.git] / httemplate / elements / tr-select-cust-part_pkg.html
index 767d232..f4af405 100644 (file)
@@ -1,5 +1,3 @@
-%if ( scalar(@pkg_class) > 1 && ! $conf->exists('disable-cust-pkg_class') ) {
-
   <& /elements/xmlhttp.html,
                 'url'  => $p.'misc/cust-part_pkg.cgi',
                 'subs' => [ 'get_part_pkg' ],
@@ -7,10 +5,12 @@
 
   <SCRIPT TYPE="text/javascript">
 
-    function part_pkg_opt(what, value, text, can_discount, can_start_date) {
+    function part_pkg_opt(what, value, text, can_discount, start_on_hold, can_start_date, start_date) {
       var optionName = new Option(text, value, false, false);
       optionName.setAttribute('data-can_discount',   can_discount);
+      optionName.setAttribute('data-start_on_hold',  start_on_hold);
       optionName.setAttribute('data-can_start_date', can_start_date);
+      optionName.setAttribute('data-start_date',     start_date || '');
       var length = what.length;
       what.options[length] = optionName;
     }
@@ -19,7 +19,7 @@
 
       what.form.pkgpart.disabled = 'disabled'; //disable part_pkg dropdown
       var submitButton = what.form.submitButton; // || what.form.submit;
-      if ( submitButton ) {
+      if ( submitButton && <% $opt{'curr_value'} ? 0 : 1 %> ) {
         submitButton.disabled = true; //disable the submit button
       }
       var discountnum = what.form.discountnum;
@@ -27,7 +27,7 @@
         discountnum.disabled = true; //disable discount dropdown
       }
 
-      classnum = what.options[what.selectedIndex].value;
+      classnum = what.value;
 
       function update_part_pkg(part_pkg) {
 
         // add the new packages
         opt(what.form.pkgpart, '', 'Select package');
         var packagesArray = eval('(' + part_pkg + ')' );
-        for ( var s = 0; s < packagesArray.length; s=s+4 ) {
-          var packagesLabel  = packagesArray[s+1];
-          var can_discount   = packagesArray[s+2];
-          var can_start_date = packagesArray[s+3];
+        while (packagesArray.length > 0) {
+          //surely this should be some kind of JSON structure
+          var pkgpart        = packagesArray.shift();
+          var label          = packagesArray.shift();
+          var can_discount   = packagesArray.shift();
+          var start_on_hold  = packagesArray.shift();
+          var can_start_date = packagesArray.shift();
+          var start_date     = packagesArray.shift();
           part_pkg_opt(
-            what.form.pkgpart, packagesArray[s], packagesLabel, can_discount, can_start_date
+            what.form.pkgpart, pkgpart, label, can_discount, start_on_hold, can_start_date, start_date
           );
         }
 
         what.form.pkgpart.disabled = ''; //re-enable part_pkg dropdown
+%       if ( $opt{'curr_value'} ) {
+          what.form.pkgpart.value = <% $opt{'curr_value'} %>;
+%       }
+
+        pkg_changed();
 
       }
 
-      get_part_pkg( <% $cust_main->custnum %>, classnum, update_part_pkg );
-    
+      get_part_pkg( <% $cust_main     ? $cust_main->custnum         : '0' %>,
+                    <% $prospect_main ? $prospect_main->prospectnum : '0' %>,
+                    classnum,
+                    update_part_pkg
+                  );
     }
 
+    <&| /elements/onload.js &>
+      classnum_changed(document.getElementById('classnum'));
+    </&>
+
   </SCRIPT>
 
+% if ( scalar(@pkg_class) > 1 && ! $conf->exists('disable-cust-pkg_class') ) {
   <TR>
     <TH ALIGN="right"><% mt('Package Class') |h %></TH>
     <TD COLSPAN=7>
     </TD>
   </TR>
 
-%}
+% } else { # so that the rest of the page works correctly
+  <INPUT TYPE="hidden" ID="classnum" NAME="classnum" VALUE="-1">
+% }
 
 <TR>
   <TH ALIGN="right"><% mt('Package') |h %></TH>
   <TD COLSPAN=7>
     <& /elements/select-cust-part_pkg.html,
-                 'curr_value' => $opt{'curr_value'}, #$pkgpart
-                 'classnum'   => $opt{'classnum'},
-                 'cust_main'  => $opt{'cust_main'},  #$cust_main
-                 'onchange'   => 'pkg_changed',
+                 'curr_value'     => $opt{'curr_value'}, #$pkgpart
+                 'classnum'       => $opt{'classnum'},
+                 'cust_main'      => $opt{'cust_main'},  #$cust_main
+                 'prospect_main'  => $opt{'prospect_main'},  #$prospect_main
+                 'onchange'       => 'pkg_changed',
+                 'label_callback' => sub { shift->pkg_comment },
     &>
   </TD>
 </TR>
@@ -91,8 +112,13 @@ my %opt = @_;
 my $pre_label = $opt{'pre_label'} || '';
 $pre_label .= ' ' if length($pre_label) && $pre_label =~ /\S$/;
 
-my $cust_main = $opt{'cust_main'}
-  or die "cust_main not specified";
+my $cust_main = $opt{'cust_main'};
+my $prospect_main = $opt{'prospect_main'};
+
+die "neither cust_main nor prospect_main specified"
+  unless $cust_main || $prospect_main;
+
+my $agent = $cust_main ? $cust_main->agent : $prospect_main->agent;
 
 #"normal" part_pkg agent virtualization (agentnum or type)
 my @part_pkg = qsearch({
@@ -101,7 +127,7 @@ my @part_pkg = qsearch({
   'hashref'   => { 'disabled' => '' },
   'extra_sql' =>
     ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql( 'null'=>1 ).
-    ' AND '. FS::part_pkg->agent_pkgs_sql( $opt{'cust_main'}->agent ),
+    ' AND '. FS::part_pkg->agent_pkgs_sql( $agent ),
 });
 
 my @pkg_class =