fix new link editing in 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           )
12 %>
13 <%init>
14
15 my $curuser = $FS::CurrentUser::CurrentUser;
16
17 die "access denied"
18   unless $curuser->access_right('Edit package definitions')
19       || $curuser->access_right('Edit global package definitions')
20       || ( ! $cgi->param('pkgpart') && $cgi->param('pkgnum') && $curuser->access_right('Customize customer package') );
21
22 my $precheck_callback = sub {
23   my( $cgi ) = @_;
24
25   my $conf = new FS::Conf;
26
27   foreach (qw( setuptax recurtax disabled )) {
28     $cgi->param($_, '') unless defined $cgi->param($_);
29   }
30
31   return 'Must select a tax class'
32     if $cgi->param('taxclass') eq '(select)';
33
34   my @agents = ();
35   foreach ($cgi->param('agent_type')) {
36     /^(\d+)$/;
37     push @agents, $1 if $1;
38   }
39   return "At least one agent type must be specified."
40     unless( scalar(@agents) ||
41             $cgi->param('clone') && $cgi->param('clone') =~ /^\d+$/ ||
42             !$cgi->param('pkgpart') && $conf->exists('agent-defaultpkg')
43           );
44
45   return '';
46
47 };
48
49 my $custnum = '';
50
51 my $args_callback = sub {
52   my( $cgi, $new ) = @_;
53   
54   my @args = ( 'primary_svc' => scalar($cgi->param('pkg_svc_primary')) );
55
56   ##
57   #options
58   ##
59   
60   $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
61   my $plan = $1;
62   
63   tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
64   my $href = $plans{$plan}->{'fields'};
65   
66   my $error = '';
67   my $options = $cgi->param($plan."__OPTIONS");
68   my @options = split(',', $options);
69   my %options =
70     map { my $optionname = $_;
71           my $param = $plan."__$optionname";
72           my $parser = exists($href->{$optionname}{parse})
73                          ? $href->{$optionname}{parse}
74                          : sub { shift };
75           my $value = join(', ', &$parser($cgi->param($param)));
76           my $check = $href->{$optionname}{check};
77           if ( $check && ! &$check($value) ) {
78             $value = join(', ', $cgi->param($param));
79             $error ||= "Illegal ".
80                          ($href->{$optionname}{name}||$optionname). ": $value";
81           }
82           ( $optionname => $value );
83         }
84         @options;
85
86   $options{$_} = scalar( $cgi->param($_) )
87     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 #these should probably move to @args above and be processed by part_pkg.pm...
114
115 $cgi->param('tax_override') =~ /^([\d,]+)$/;
116 my (@tax_overrides) = (grep "$_", split (",", $1));
117
118 my @process_m2m = (
119   {
120     'link_table'   => 'part_pkg_taxoverride',
121     'target_table' => 'tax_class',
122     'params'       => \@tax_overrides,
123   },
124   { 'link_table'   => 'part_pkg_link',
125     'target_table' => 'part_pkg',
126     'base_field'   => 'src_pkgpart',
127     'target_field' => 'dst_pkgpart',
128     'hashref'      => { 'link_type' => 'bill' },
129     'params'       => [ map $cgi->param($_), grep /^bill_dst_pkgpart/, $cgi->param ],
130   },
131   { 'link_table'   => 'part_pkg_link',
132     'target_table' => 'part_pkg',
133     'base_field'   => 'src_pkgpart',
134     'target_field' => 'dst_pkgpart',
135     'hashref'      => { 'link_type' => 'svc' },
136     'params'       => [ map $cgi->param($_), grep /^svc_dst_pkgpart/, $cgi->param ],
137   },
138 );
139
140 my $conf = new FS::Conf;
141
142 if ( $cgi->param('pkgpart') || ! $conf->exists('agent_defaultpkg') ) {
143   my @agents = ();
144   foreach ($cgi->param('agent_type')) {
145     /^(\d+)$/;
146     push @agents, $1 if $1;
147   }
148   push @process_m2m, {
149     'link_table'   => 'type_pkgs',
150     'target_table' => 'agent_type',
151     'params'       => \@agents,
152   };
153 }
154
155 </%init>