summaryrefslogtreecommitdiff
path: root/httemplate/elements/tr-select-cust-part_pkg.html
blob: f4af405fcf7875faff6938e3d133a024af6ad947 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  <& /elements/xmlhttp.html,
                'url'  => $p.'misc/cust-part_pkg.cgi',
                'subs' => [ 'get_part_pkg' ],
  &>

  <SCRIPT TYPE="text/javascript">

    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;
    }

    function classnum_changed(what) {

      what.form.pkgpart.disabled = 'disabled'; //disable part_pkg dropdown
      var submitButton = what.form.submitButton; // || what.form.submit;
      if ( submitButton && <% $opt{'curr_value'} ? 0 : 1 %> ) {
        submitButton.disabled = true; //disable the submit button
      }
      var discountnum = what.form.discountnum;
      if ( discountnum ) {
        discountnum.disabled = true; //disable discount dropdown
      }

      classnum = what.value;

      function update_part_pkg(part_pkg) {

        // blank the current packages
        for ( var i = what.form.pkgpart.length; i>= 0; i-- )
          what.form.pkgpart.options[i] = null;

        // add the new packages
        opt(what.form.pkgpart, '', 'Select package');
        var packagesArray = eval('(' + part_pkg + ')' );
        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, 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     ? $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>
      <& /elements/select-cust-pkg_class.html,
                   'curr_value' => $opt{'classnum'},
                   'pkg_class'  => \@pkg_class,
                   'onchange'   => 'classnum_changed',
      &>
    </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
                 'prospect_main'  => $opt{'prospect_main'},  #$prospect_main
                 'onchange'       => 'pkg_changed',
                 'label_callback' => sub { shift->pkg_comment },
    &>
  </TD>
</TR>

<%init>

my $conf = new FS::Conf;

my %opt = @_;

my $pre_label = $opt{'pre_label'} || '';
$pre_label .= ' ' if length($pre_label) && $pre_label =~ /\S$/;

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({
  'select'    => 'DISTINCT classnum',
  'table'     => 'part_pkg',
  'hashref'   => { 'disabled' => '' },
  'extra_sql' =>
    ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql( 'null'=>1 ).
    ' AND '. FS::part_pkg->agent_pkgs_sql( $agent ),
});

my @pkg_class =
  sort { $a->classname cmp $b->classname } #should get a sort order in config
  map  { $_->pkg_class || new FS::pkg_class { 'classnum'  => '',
                                              'classname' => '(none)' }
       }
  @part_pkg;

</%init>