new package editor
[freeside.git] / httemplate / edit / process / part_pkg.cgi
1 <% include( 'elements/process.html',
2               #'debug'             => 1,
3               'table'             => 'part_pkg',
4               'viewall_dir'       => 'browse',
5               'viewall_ext'       => 'cgi',
6               'edit_ext'          => 'cgi',
7               #XXX usable with cloning? #'agent_null_right'  => 'Edit global package definitions',
8               'precheck_callback' => $precheck_callback,
9               'args_callback'     => $args_callback,
10               'process_m2m'       => \@process_m2m,
11               'debug' => 1,
12           )
13 %>
14 <%init>
15
16 my $curuser = $FS::CurrentUser::CurrentUser;
17
18 die "access denied"
19   unless $curuser->access_right('Edit package definitions')
20       || $curuser->access_right('Edit global package definitions')
21       || ( ! $cgi->param('pkgpart') && $cgi->param('pkgnum') && $curuser->access_right('Customize customer package') );
22
23 my $precheck_callback = sub {
24   my( $cgi ) = @_;
25
26   my $conf = new FS::Conf;
27
28   foreach (qw( setuptax recurtax disabled )) {
29     $cgi->param($_, '') unless defined $cgi->param($_);
30   }
31
32   return 'Must select a tax class'
33     if $cgi->param('taxclass') eq '(select)';
34
35   my @agents = ();
36   foreach ($cgi->param('agent_type')) {
37     /^(\d+)$/;
38     push @agents, $1 if $1;
39   }
40   return "At least one agent type must be specified."
41     unless( scalar(@agents) ||
42             $cgi->param('clone') && $cgi->param('clone') =~ /^\d+$/ ||
43             !$cgi->param('pkgpart') && $conf->exists('agent-defaultpkg')
44           );
45
46   return '';
47
48 };
49
50 my $custnum = '';
51
52 my $args_callback = sub {
53   my( $cgi, $new ) = @_;
54   
55   my @args = ( 'primary_svc' => scalar($cgi->param('pkg_svc_primary')) );
56
57   ##
58   #options
59   ##
60   
61   $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
62   my $plan = $1;
63   
64   tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
65   my $href = $plans{$plan}->{'fields'};
66   
67   my $error = '';
68   my $options = $cgi->param($plan."__OPTIONS");
69   my @options = split(',', $options);
70   my %options =
71     map { my $optionname = $_;
72           my $param = $plan."__$optionname";
73           my $parser = exists($href->{$optionname}{parse})
74                          ? $href->{$optionname}{parse}
75                          : sub { shift };
76           my $value = join(', ', &$parser($cgi->param($param)));
77           my $check = $href->{$optionname}{check};
78           if ( $check && ! &$check($value) ) {
79             $value = join(', ', $cgi->param($param));
80             $error ||= "Illegal ".
81                          ($href->{$optionname}{name}||$optionname). ": $value";
82           }
83           ( $optionname => $value );
84         }
85         @options;
86
87   $options{$_} = scalar($cgi->param($_)) for (qw( setup_fee recur_fee ));
88   
89   push @args, 'options' => \%options;
90
91   ###
92   #pkg_svc
93   ###
94
95   my %pkg_svc = map { $_ => scalar($cgi->param("pkg_svc$_")) }
96                 map { $_->svcpart }
97                 qsearch('part_svc', {} );
98
99   push @args, 'pkg_svc' => \%pkg_svc;
100
101   ###
102   # cust_pkg and custnum_ref (inserts only)
103   ###
104   unless ( $cgi->param('pkgpart') ) {
105     push @args, 'cust_pkg'    => scalar($cgi->param('pkgnum')),
106                 'custnum_ref' => \$custnum;
107   }
108
109   @args;
110
111 };
112
113 $cgi->param('tax_override') =~ /^([\d,]+)$/;
114 my (@tax_overrides) = (grep "$_", split (",", $1));
115
116 my @process_m2m = (
117   {
118     'link_table'   => 'part_pkg_taxoverride',
119     'target_table' => 'tax_class',
120     'params'       => \@tax_overrides,
121   }
122 );
123
124 my $conf = new FS::Conf;
125
126 if ( $cgi->param('pkgpart') || ! $conf->exists('agent_defaultpkg') ) {
127   my @agents = ();
128   foreach ($cgi->param('agent_type')) {
129     /^(\d+)$/;
130     push @agents, $1 if $1;
131   }
132   warn "AGENTS: @agents";
133   push @process_m2m, {
134     'link_table'   => 'type_pkgs',
135     'target_table' => 'agent_type',
136     'params'       => \@agents,
137   };
138 }
139
140 </%init>