don't require an agent type to be specified when editing a disabled package
[freeside.git] / httemplate / edit / process / part_pkg.cgi
1 <% include( 'elements/process.html',
2               #'debug'             => 1,
3               'table'             => 'part_pkg',
4               'redirect'          => $redirect_callback,
5               'viewall_dir'       => 'browse',
6               'viewall_ext'       => 'cgi',
7               'edit_ext'          => 'cgi',
8               #XXX usable with cloning? #'agent_null_right'  => 'Edit global package definitions',
9               'precheck_callback' => $precheck_callback,
10               'args_callback'     => $args_callback,
11               'process_m2m'       => \@process_m2m,
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            || $cgi->param('disabled');
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($_) )
88     for (qw( setup_fee recur_fee ));
89   
90   push @args, 'options' => \%options;
91
92   ###
93   #pkg_svc
94   ###
95
96   my %pkg_svc = map { $_ => scalar($cgi->param("pkg_svc$_")) }
97                 map { $_->svcpart }
98                 qsearch('part_svc', {} );
99
100   push @args, 'pkg_svc' => \%pkg_svc;
101
102   ###
103   # cust_pkg and custnum_ref (inserts only)
104   ###
105   unless ( $cgi->param('pkgpart') ) {
106     push @args, 'cust_pkg'    => scalar($cgi->param('pkgnum')),
107                 'custnum_ref' => \$custnum;
108   }
109
110   warn "args: ".join('/', @args). "\n";
111
112   @args;
113
114 };
115
116 my $redirect_callback = sub {
117   #my( $cgi, $new ) = @_;
118   return '' unless $custnum;
119   popurl(3). "view/cust_main.cgi?keywords=$custnum;dummy=";
120 };
121
122 #these should probably move to @args above and be processed by part_pkg.pm...
123
124 $cgi->param('tax_override') =~ /^([\d,]+)$/;
125 my (@tax_overrides) = (grep "$_", split (",", $1));
126
127 my @process_m2m = (
128   {
129     'link_table'   => 'part_pkg_taxoverride',
130     'target_table' => 'tax_class',
131     'params'       => \@tax_overrides,
132   },
133   { 'link_table'   => 'part_pkg_link',
134     'target_table' => 'part_pkg',
135     'base_field'   => 'src_pkgpart',
136     'target_field' => 'dst_pkgpart',
137     'hashref'      => { 'link_type' => 'bill' },
138     'params'       => [ map $cgi->param($_), grep /^bill_dst_pkgpart/, $cgi->param ],
139   },
140   { 'link_table'   => 'part_pkg_link',
141     'target_table' => 'part_pkg',
142     'base_field'   => 'src_pkgpart',
143     'target_field' => 'dst_pkgpart',
144     'hashref'      => { 'link_type' => 'svc' },
145     'params'       => [ map $cgi->param($_), grep /^svc_dst_pkgpart/, $cgi->param ],
146   },
147 );
148
149 my $conf = new FS::Conf;
150
151 if ( $cgi->param('pkgpart') || ! $conf->exists('agent_defaultpkg') ) {
152   my @agents = ();
153   foreach ($cgi->param('agent_type')) {
154     /^(\d+)$/;
155     push @agents, $1 if $1;
156   }
157   push @process_m2m, {
158     'link_table'   => 'type_pkgs',
159     'target_table' => 'agent_type',
160     'params'       => \@agents,
161   };
162 }
163
164 </%init>