multiple usage classes checkpoint
[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   foreach ( split(',', $cgi->param('taxproductnums') ) ) {
88     my $value = $cgi->param("taxproductnum_$_");
89     $error ||= "Illegal taxproductnum_$_: $value"
90       unless ( $value =~ /^\d*$/  );
91     $options{"usage_taxproductnum_$_"} = $value;
92   }
93
94   $options{$_} = scalar( $cgi->param($_) )
95     for (qw( setup_fee recur_fee ));
96   
97   push @args, 'options' => \%options;
98
99   ###
100   #pkg_svc
101   ###
102
103   my %pkg_svc = map { $_ => scalar($cgi->param("pkg_svc$_")) }
104                 map { $_->svcpart }
105                 qsearch('part_svc', {} );
106
107   push @args, 'pkg_svc' => \%pkg_svc;
108
109   ###
110   # cust_pkg and custnum_ref (inserts only)
111   ###
112   unless ( $cgi->param('pkgpart') ) {
113     push @args, 'cust_pkg'    => scalar($cgi->param('pkgnum')),
114                 'custnum_ref' => \$custnum;
115   }
116
117   warn "args: ".join('/', @args). "\n";
118
119   @args;
120
121 };
122
123 my $redirect_callback = sub {
124   #my( $cgi, $new ) = @_;
125   return '' unless $custnum;
126   popurl(3). "view/cust_main.cgi?keywords=$custnum;dummy=";
127 };
128
129 #these should probably move to @args above and be processed by part_pkg.pm...
130
131 $cgi->param('tax_override') =~ /^([\d,]+)$/;
132 my (@tax_overrides) = (grep "$_", split (",", $1));
133
134 my @process_m2m = (
135   {
136     'link_table'   => 'part_pkg_taxoverride',
137     'target_table' => 'tax_class',
138     'params'       => \@tax_overrides,
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' => 'bill' },
145     'params'       => [ map $cgi->param($_), grep /^bill_dst_pkgpart/, $cgi->param ],
146   },
147   { 'link_table'   => 'part_pkg_link',
148     'target_table' => 'part_pkg',
149     'base_field'   => 'src_pkgpart',
150     'target_field' => 'dst_pkgpart',
151     'hashref'      => { 'link_type' => 'svc' },
152     'params'       => [ map $cgi->param($_), grep /^svc_dst_pkgpart/, $cgi->param ],
153   },
154 );
155
156 foreach my $override_class ($cgi->param) {
157   next unless $override_class =~ /^tax_override_(\w+)$/;
158   my $class = $1;
159
160   my (@tax_overrides) = (grep "$_", split (",", $1))
161     if $cgi->param($override_class) =~ /^([\d,]+)$/;
162
163   push @process_m2m, {
164     'link_table'   => 'part_pkg_taxoverride',
165     'target_table' => 'tax_class',
166     'hashref'      => { 'usage_class' => $class },
167     'params'       => \@tax_overrides,
168   };
169
170 }
171
172 my $conf = new FS::Conf;
173
174 if ( $cgi->param('pkgpart') || ! $conf->exists('agent_defaultpkg') ) {
175   my @agents = ();
176   foreach ($cgi->param('agent_type')) {
177     /^(\d+)$/;
178     push @agents, $1 if $1;
179   }
180   push @process_m2m, {
181     'link_table'   => 'type_pkgs',
182     'target_table' => 'agent_type',
183     'params'       => \@agents,
184   };
185 }
186
187 </%init>