1 <% include( 'elements/process.html',
5 'agent_null_right' => \@agent_null_right,
6 'redirect' => $redirect_callback,
7 'viewall_dir' => 'browse',
8 'viewall_ext' => 'cgi',
10 'precheck_callback' => $precheck_callback,
11 'args_callback' => $args_callback,
12 'update_svc' => $update_svc,
13 'process_locale' => 'pkg',
14 'process_m2m' => \@process_m2m,
15 'process_o2m' => \@process_o2m,
20 my $customizing = ( ! $cgi->param('pkgpart') && $cgi->param('pkgnum') );
22 my $curuser = $FS::CurrentUser::CurrentUser;
24 my $edit_global = 'Edit global package definitions';
25 my $customize = 'Customize customer package';
28 unless $curuser->access_right('Edit package definitions')
29 || $curuser->access_right($edit_global)
30 || ( $customizing && $curuser->access_right($customize) );
32 my @agent_null_right = ( $edit_global );
33 push @agent_null_right, $customize if $customizing;
36 my $precheck_callback = sub {
39 my $conf = new FS::Conf;
41 foreach (qw( setuptax recurtax disabled )) {
42 $cgi->param($_, '') unless defined $cgi->param($_);
45 return 'Must select a tax class'
46 if $cgi->param('taxclass') eq '(select)';
49 foreach ($cgi->param('agent_type')) {
51 push @agents, $1 if $1;
53 return "At least one agent type must be specified."
54 unless scalar(@agents)
55 #wtf? || ( $cgi->param('clone') && $cgi->param('clone') =~ /^\d+$/ )
56 || $cgi->param('disabled')
57 || $cgi->param('agentnum');
65 my $args_callback = sub {
66 my( $cgi, $new ) = @_;
68 my @args = ( 'primary_svc' => scalar($cgi->param('pkg_svc_primary')) );
74 $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
77 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
78 my $href = $plans{$plan}->{'fields'};
81 my $options = $cgi->param($plan."__OPTIONS");
82 my @options = split(',', $options);
84 map { my $optionname = $_;
85 my $param = $plan."__$optionname";
86 my $parser = exists($href->{$optionname}{parse})
87 ? $href->{$optionname}{parse}
89 my $value = join(', ', &$parser($cgi->param($param)));
90 my $check = $href->{$optionname}{check};
91 if ( $check && ! &$check($value) ) {
92 $value = join(', ', $cgi->param($param));
93 $error ||= "Illegal ".
94 ($href->{$optionname}{name}||$optionname). ": $value";
96 ( $optionname => $value );
98 grep { $_ !~ /^report_option_/ }
101 foreach my $class ( '', split(',', $cgi->param('taxproductnums') ) ) {
102 my $param = "taxproductnum_$class";
103 my $value = $cgi->param($param);
105 if ( $value == -1 ) {
106 my $desc = $cgi->param($param.'_description');
107 # insert a new part_pkg_taxproduct
108 my $engine = FS::TaxEngine->new;
109 my $obj_or_error = $engine->add_taxproduct($desc);
110 if (ref $obj_or_error) {
111 $value = $obj_or_error->taxproductnum;
112 $cgi->param($param, $value); # for error handling
114 die "$obj_or_error (adding tax product)";
118 $error ||= "Illegal $param: $value"
119 unless ( $value =~ /^\d*$/ );
120 if (length($class)) {
121 $options{"usage_taxproductnum_$class"} = $value;
123 $new->set('taxproductnum', $value);
127 foreach ( grep $_, $cgi->param('report_option') ) {
128 $error ||= "Illegal optional report class: $_" unless ( $_ =~ /^\d*$/ );
129 $options{"report_option_$_"} = 1;
132 $options{$_} = scalar( $cgi->param($_) )
133 for (qw( setup_fee recur_fee disable_line_item_date_ranges ));
135 push @args, 'options' => \%options;
141 my %part_pkg_currency = (
142 map { $_ => scalar($cgi->param($_)) }
143 #grep /._[A-Z]{3}$/, #support other options
144 grep /^(setup|recur)_fee_[A-Z]{3}$/,
148 push @args, 'part_pkg_currency' => \%part_pkg_currency;
153 my $fcc_options_string = $cgi->param('fcc_options_string');
154 if ($fcc_options_string) {
155 push @args, 'fcc_options' => decode_json($fcc_options_string);
162 my @svcparts = map { $_->svcpart } qsearch('part_svc', {});
163 my %pkg_svc = map { $_ => scalar($cgi->param("pkg_svc$_" )) } @svcparts;
164 my %hidden_svc = map { $_ => scalar($cgi->param("hidden$_" )) } @svcparts;
165 my %provision_hold = map { $_ => scalar($cgi->param("provision_hold$_" )) } @svcparts;
166 my %bulk_skip = map { $_ => ( $cgi->param("no_bulk_skip$_") eq 'Y'
171 push @args, 'pkg_svc' => \%pkg_svc,
172 'hidden_svc' => \%hidden_svc,
173 'provision_hold' => \%provision_hold,
174 'bulk_skip' => \%bulk_skip;
177 # cust_pkg and custnum_ref (inserts only)
179 unless ( $cgi->param('pkgpart') ) {
180 push @args, 'cust_pkg' => scalar($cgi->param('pkgnum')),
181 'custnum_ref' => \$custnum;
185 foreach my $param ( $cgi->param ) {
186 if ( $param =~ /^export(\d+)$/ && length($cgi->param($param)) > 0 ) {
187 $part_pkg_vendor{$1} = $cgi->param($param);
190 if ( keys %part_pkg_vendor > 0 ) {
191 push @args, 'part_pkg_vendor' => \%part_pkg_vendor;
194 push @args, 'part_pkg_restrict_soft_override' => 1
195 if $cgi->param('part_pkg_restrict_soft_override');
197 #warn "args: ".join('/', @args). "\n";
203 ## update services upon package change.
204 my $update_svc = sub {
210 my @svcs = $new->pkg_svc();
212 ## update broadband services getting their up and down speeds from package fcc_477 options
213 foreach my $svc_part(@svcs) {
214 my @part_svc_column = qsearch('part_svc_column',{ 'svcpart' => $svc_part->{Hash}->{svcpart}, 'columnflag' => 'P' });
216 if ($svc_part->{Hash}->{svcdb} eq "svc_broadband" && (keys %{ $args{fcc_options} }) && @part_svc_column ) {
217 ## find provisioned services to update
218 my @svc_svcdb = qsearch({
219 'table' => 'svc_broadband',
220 'select' => 'svc_broadband.*, cust_svc.svcpart',
221 'addl_from' => 'LEFT JOIN cust_svc USING (svcnum) LEFT JOIN cust_pkg USING (pkgnum)',
222 'extra_sql' => " WHERE cust_svc.svcpart = '".$svc_part->{Hash}->{svcpart}."' AND cust_pkg.pkgpart = '".$svc_part->{Hash}->{pkgpart}."'",
224 foreach my $svc (@svc_svcdb) {
225 next if ($svc->{Hash}->{speed_down} == $args{fcc_options}->{broadband_downstream} * 1000 && $svc->{Hash}->{speed_up} == $args{fcc_options}->{broadband_upstream} * 1000);
226 $svc->{Hash}->{speed_down} = $args{fcc_options}->{broadband_downstream} * 1000;
227 $svc->{Hash}->{speed_up} = $args{fcc_options}->{broadband_upstream} * 1000;
228 $error = $svc->replace();
235 my $redirect_callback = sub {
236 #my( $cgi, $new ) = @_;
237 return '' unless $custnum;
238 my $show = $curuser->default_customer_view =~ /^(jumbo|packages)$/
241 #my $frag = "cust_pkg$pkgnum"; #hack for IE ignoring real #fragment
243 #can we link back to the specific customized package? it would be nice...
244 popurl(3). "view/cust_main.cgi?custnum=$custnum$show;dummy=";
247 #these should probably move to @args above and be processed by part_pkg.pm...
249 $cgi->param('tax_override') =~ /^([\d,]+)$/;
250 my (@tax_overrides) = (grep "$_", split (",", $1));
254 'link_table' => 'part_pkg_taxoverride',
255 'target_table' => 'tax_class',
256 'params' => \@tax_overrides,
258 { 'link_table' => 'part_pkg_discount',
259 'target_table' => 'discount',
260 'params' => [ map $cgi->param($_),
261 grep /^discountnum/, $cgi->param
264 { 'link_table' => 'part_pkg_link',
265 'target_table' => 'part_pkg',
266 'base_field' => 'src_pkgpart',
267 'target_field' => 'dst_pkgpart',
268 'hashref' => { 'link_type' => 'svc', 'hidden' => '' },
269 'params' => [ map $cgi->param($_),
270 grep /^svc_dst_pkgpart/, $cgi->param
273 { 'link_table' => 'part_pkg_link',
274 'target_table' => 'part_pkg',
275 'base_field' => 'src_pkgpart',
276 'target_field' => 'dst_pkgpart',
277 'hashref' => { 'link_type' => 'supp', 'hidden' => '' },
278 'params' => [ map $cgi->param($_),
279 grep /^supp_dst_pkgpart/, $cgi->param
284 { 'link_table' => 'part_pkg_link',
285 'target_table' => 'part_pkg',
286 'base_field' => 'src_pkgpart',
287 'target_field' => 'dst_pkgpart',
288 'hashref' => { 'link_type' => 'bill', 'hidden' => $hidden },
289 'params' => [ map { $cgi->param($_) }
290 grep { my $param = "bill_dst_pkgpart__hidden";
292 (($digit) = /^bill_dst_pkgpart(\d+)/ ) &&
293 $cgi->param("$param$digit") eq $hidden;
301 foreach my $override_class ($cgi->param) {
302 next unless $override_class =~ /^tax_override_(\w+)$/;
305 my (@tax_overrides) = (grep "$_", split (",", $1))
306 if $cgi->param($override_class) =~ /^([\d,]+)$/;
309 'link_table' => 'part_pkg_taxoverride',
310 'target_table' => 'tax_class',
311 'hashref' => { 'usage_class' => $class },
312 'params' => [ @tax_overrides ],
317 my $conf = new FS::Conf;
320 foreach ($cgi->param('agent_type')) {
322 push @agents, $1 if $1;
325 'link_table' => 'type_pkgs',
326 'target_table' => 'agent_type',
327 'params' => \@agents,
330 my $targets = FS::part_pkg_usageprice->targets;
331 foreach my $amount_param ( grep /^usagepricepart(\d+)_amount$/, $cgi->param ) {
332 $amount_param =~ /^usagepricepart(\d+)_amount$/ or die 'unpossible';
334 my $amount = $cgi->param($amount_param);
335 if ( ! $amount && ! $cgi->param("usagepricepart${num}_price") ) {
336 #don't add empty rows just because the dropdowns have a value
337 $cgi->param("usagepricepart${num}_$_", '') for qw( currency action target );
340 my $target = $cgi->param("usagepricepart${num}_target");
341 $amount *= $targets->{$target}{multiplier} if $targets->{$target}{multiplier};
342 $cgi->param($amount_param, $amount);
347 'table' => 'part_pkg_usageprice',
348 'fields' => [qw( price currency action target amount )],