-<%
+<% include( 'elements/process.html',
+ #'debug' => 1,
+ 'table' => 'part_pkg',
+ 'agent_virt' => 1,
+ 'agent_null_right' => \@agent_null_right,
+ 'redirect' => $redirect_callback,
+ 'viewall_dir' => 'browse',
+ 'viewall_ext' => 'cgi',
+ 'edit_ext' => 'cgi',
+ 'precheck_callback' => $precheck_callback,
+ 'args_callback' => $args_callback,
+ 'process_m2m' => \@process_m2m,
+ )
+%>
+<%init>
-my $dbh = dbh;
+my $customizing = ( ! $cgi->param('pkgpart') && $cgi->param('pkgnum') );
-my $pkgpart = $cgi->param('pkgpart');
+my $curuser = $FS::CurrentUser::CurrentUser;
-my $old = qsearchs('part_pkg',{'pkgpart'=>$pkgpart}) if $pkgpart;
+my $edit_global = 'Edit global package definitions';
+my $customize = 'Customize customer package';
-#fixup plandata
-my $plandata = $cgi->param('plandata');
-my @plandata = split(',', $plandata);
-$cgi->param('plandata',
- join('', map { "$_=". join(', ', $cgi->param($_)). "\n" } @plandata )
-);
+die "access denied"
+ unless $curuser->access_right('Edit package definitions')
+ || $curuser->access_right($edit_global)
+ || ( $customizing && $curuser->access_right($customize) );
-foreach (qw( setuptax recurtax disabled )) {
- $cgi->param($_, '') unless defined $cgi->param($_);
-}
+my @agent_null_right = ( $edit_global );
+push @agent_null_right, $customize if $customizing;
-my $new = new FS::part_pkg ( {
- map {
- $_, scalar($cgi->param($_));
- } fields('part_pkg')
-} );
-#warn "setuptax: ". $new->setuptax;
-#warn "recurtax: ". $new->recurtax;
+my $precheck_callback = sub {
+ my( $cgi ) = @_;
-#most of the stuff below should move to part_pkg.pm
+ my $conf = new FS::Conf;
-foreach my $part_svc ( qsearch('part_svc', {} ) ) {
- my $quantity = $cgi->param('pkg_svc'. $part_svc->svcpart) || 0;
- unless ( $quantity =~ /^(\d+)$/ ) {
- $cgi->param('error', "Illegal quantity" );
- print $cgi->redirect(popurl(2). "part_pkg.cgi?". $cgi->query_string );
- myexit();
+ foreach (qw( setuptax recurtax disabled )) {
+ $cgi->param($_, '') unless defined $cgi->param($_);
}
-}
-local $SIG{HUP} = 'IGNORE';
-local $SIG{INT} = 'IGNORE';
-local $SIG{QUIT} = 'IGNORE';
-local $SIG{TERM} = 'IGNORE';
-local $SIG{TSTP} = 'IGNORE';
-local $SIG{PIPE} = 'IGNORE';
-
-local $FS::UID::AutoCommit = 0;
-
-my $error;
-if ( $pkgpart ) {
- $error = $new->replace($old);
-} else {
- $error = $new->insert;
- $pkgpart=$new->pkgpart;
-}
-if ( $error ) {
- $dbh->rollback;
- $cgi->param('error', $error );
- print $cgi->redirect(popurl(2). "part_pkg.cgi?". $cgi->query_string );
- myexit();
-}
+ return 'Must select a tax class'
+ if $cgi->param('taxclass') eq '(select)';
-foreach my $part_svc (qsearch('part_svc',{})) {
- my $quantity = $cgi->param('pkg_svc'. $part_svc->svcpart) || 0;
- my $old_pkg_svc = qsearchs('pkg_svc', {
- 'pkgpart' => $pkgpart,
- 'svcpart' => $part_svc->svcpart,
- } );
- my $old_quantity = $old_pkg_svc ? $old_pkg_svc->quantity : 0;
- next unless $old_quantity != $quantity; #!here
- my $new_pkg_svc = new FS::pkg_svc( {
- 'pkgpart' => $pkgpart,
- 'svcpart' => $part_svc->svcpart,
- 'quantity' => $quantity,
- } );
- if ( $old_pkg_svc ) {
- my $myerror = $new_pkg_svc->replace($old_pkg_svc);
- if ( $myerror ) {
- $dbh->rollback;
- die $myerror;
- }
- } else {
- my $myerror = $new_pkg_svc->insert;
- if ( $myerror ) {
- $dbh->rollback;
- die $myerror;
- }
+ my @agents = ();
+ foreach ($cgi->param('agent_type')) {
+ /^(\d+)$/;
+ push @agents, $1 if $1;
}
-}
+ return "At least one agent type must be specified."
+ unless scalar(@agents)
+ || ( $cgi->param('clone') && $cgi->param('clone') =~ /^\d+$/ )
+ || ( !$cgi->param('pkgpart') && $conf->exists('agent-defaultpkg') )
+ || $cgi->param('disabled')
+ || $cgi->param('agentnum');
+
+ return '';
+
+};
+
+my $custnum = '';
+
+my $args_callback = sub {
+ my( $cgi, $new ) = @_;
+
+ my @args = ( 'primary_svc' => scalar($cgi->param('pkg_svc_primary')) );
+
+ ##
+ #options
+ ##
+
+ $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
+ my $plan = $1;
+
+ tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
+ my $href = $plans{$plan}->{'fields'};
+
+ my $error = '';
+ my $options = $cgi->param($plan."__OPTIONS");
+ my @options = split(',', $options);
+ my %options =
+ map { my $optionname = $_;
+ my $param = $plan."__$optionname";
+ my $parser = exists($href->{$optionname}{parse})
+ ? $href->{$optionname}{parse}
+ : sub { shift };
+ my $value = join(', ', &$parser($cgi->param($param)));
+ my $check = $href->{$optionname}{check};
+ if ( $check && ! &$check($value) ) {
+ $value = join(', ', $cgi->param($param));
+ $error ||= "Illegal ".
+ ($href->{$optionname}{name}||$optionname). ": $value";
+ }
+ ( $optionname => $value );
+ }
+ grep { $_ !~ /^report_option_/ }
+ @options;
+
+ foreach ( split(',', $cgi->param('taxproductnums') ) ) {
+ my $value = $cgi->param("taxproductnum_$_");
+ $error ||= "Illegal taxproductnum_$_: $value"
+ unless ( $value =~ /^\d*$/ );
+ $options{"usage_taxproductnum_$_"} = $value;
+ }
+
+ foreach ( $cgi->param('report_option') ) {
+ $error ||= "Illegal optional report class: $_" unless ( $_ =~ /^\d*$/ );
+ $options{"report_option_$_"} = 1;
+ }
+
+ $options{$_} = scalar( $cgi->param($_) )
+ for (qw( setup_fee recur_fee ));
+
+ push @args, 'options' => \%options;
+
+ ###
+ #pkg_svc
+ ###
-unless ( $cgi->param('pkgnum') && $cgi->param('pkgnum') =~ /^(\d+)$/ ) {
- $dbh->commit or die $dbh->errstr;
- print $cgi->redirect(popurl(3). "browse/part_pkg.cgi");
-} else {
- my($old_cust_pkg) = qsearchs( 'cust_pkg', { 'pkgnum' => $1 } );
- my %hash = $old_cust_pkg->hash;
- $hash{'pkgpart'} = $pkgpart;
- my($new_cust_pkg) = new FS::cust_pkg \%hash;
- my $myerror = $new_cust_pkg->replace($old_cust_pkg);
- if ( $myerror ) {
- $dbh->rollback;
- die "Error modifying cust_pkg record: $myerror\n";
+ my %pkg_svc = map { $_ => scalar($cgi->param("pkg_svc$_")) }
+ map { $_->svcpart }
+ qsearch('part_svc', {} );
+
+ push @args, 'pkg_svc' => \%pkg_svc;
+
+ ###
+ # cust_pkg and custnum_ref (inserts only)
+ ###
+ unless ( $cgi->param('pkgpart') ) {
+ push @args, 'cust_pkg' => scalar($cgi->param('pkgnum')),
+ 'custnum_ref' => \$custnum;
}
- $dbh->commit or die $dbh->errstr;
- print $cgi->redirect(popurl(3). "view/cust_main.cgi?". $new_cust_pkg->custnum);
+ #warn "args: ".join('/', @args). "\n";
+
+ @args;
+
+};
+
+my $redirect_callback = sub {
+ #my( $cgi, $new ) = @_;
+ return '' unless $custnum;
+ my $show = $curuser->default_customer_view =~ /^(jumbo|packages)$/
+ ? ''
+ : ';show=packages';
+ #my $frag = "cust_pkg$pkgnum"; #hack for IE ignoring real #fragment
+
+ #can we link back to the specific customized package? it would be nice...
+ popurl(3). "view/cust_main.cgi?custnum=$custnum$show;dummy=";
+};
+
+#these should probably move to @args above and be processed by part_pkg.pm...
+
+$cgi->param('tax_override') =~ /^([\d,]+)$/;
+my (@tax_overrides) = (grep "$_", split (",", $1));
+
+my @process_m2m = (
+ {
+ 'link_table' => 'part_pkg_taxoverride',
+ 'target_table' => 'tax_class',
+ 'params' => \@tax_overrides,
+ },
+ { 'link_table' => 'part_pkg_discount',
+ 'target_table' => 'discount',
+ 'params' => [ map $cgi->param($_),
+ grep /^discountnum/, $cgi->param
+ ],
+ },
+ { 'link_table' => 'part_pkg_link',
+ 'target_table' => 'part_pkg',
+ 'base_field' => 'src_pkgpart',
+ 'target_field' => 'dst_pkgpart',
+ 'hashref' => { 'link_type' => 'svc', 'hidden' => '' },
+ 'params' => [ map $cgi->param($_),
+ grep /^svc_dst_pkgpart/, $cgi->param
+ ],
+ },
+ map {
+ my $hidden = $_;
+ { 'link_table' => 'part_pkg_link',
+ 'target_table' => 'part_pkg',
+ 'base_field' => 'src_pkgpart',
+ 'target_field' => 'dst_pkgpart',
+ 'hashref' => { 'link_type' => 'bill', 'hidden' => $hidden },
+ 'params' => [ map { $cgi->param($_) }
+ grep { my $param = "bill_dst_pkgpart__hidden";
+ my $digit = '';
+ (($digit) = /^bill_dst_pkgpart(\d+)/ ) &&
+ $cgi->param("$param$digit") eq $hidden;
+ }
+ $cgi->param
+ ],
+ },
+ } ( '', 'Y' ),
+);
+
+foreach my $override_class ($cgi->param) {
+ next unless $override_class =~ /^tax_override_(\w+)$/;
+ my $class = $1;
+
+ my (@tax_overrides) = (grep "$_", split (",", $1))
+ if $cgi->param($override_class) =~ /^([\d,]+)$/;
+
+ push @process_m2m, {
+ 'link_table' => 'part_pkg_taxoverride',
+ 'target_table' => 'tax_class',
+ 'hashref' => { 'usage_class' => $class },
+ 'params' => [ @tax_overrides ],
+ };
+
}
-%>
+my $conf = new FS::Conf;
+
+if ( $cgi->param('pkgpart') || ! $conf->exists('agent_defaultpkg') ) {
+ my @agents = ();
+ foreach ($cgi->param('agent_type')) {
+ /^(\d+)$/;
+ push @agents, $1 if $1;
+ }
+ push @process_m2m, {
+ 'link_table' => 'type_pkgs',
+ 'target_table' => 'agent_type',
+ 'params' => \@agents,
+ };
+}
+
+</%init>