This commit was generated by cvs2svn to compensate for changes in r8690,
[freeside.git] / httemplate / edit / process / part_pkg.cgi
1 <% include( 'elements/process.html',
2               #'debug'             => 1,
3               'table'             => 'part_pkg',
4               'agent_virt'        => 1,
5               'agent_null_right'  => \@agent_null_right,
6               'redirect'          => $redirect_callback,
7               'viewall_dir'       => 'browse',
8               'viewall_ext'       => 'cgi',
9               'edit_ext'          => 'cgi',
10               'precheck_callback' => $precheck_callback,
11               'args_callback'     => $args_callback,
12               'process_m2m'       => \@process_m2m,
13           )
14 %>
15 <%init>
16
17 my $customizing = ( ! $cgi->param('pkgpart') && $cgi->param('pkgnum') );
18
19 my $curuser = $FS::CurrentUser::CurrentUser;
20
21 my $edit_global = 'Edit global package definitions';
22 my $customize   = 'Customize customer package';
23
24 die "access denied"
25   unless $curuser->access_right('Edit package definitions')
26       || $curuser->access_right($edit_global)
27       || ( $customizing && $curuser->access_right($customize) );
28
29 my @agent_null_right = ( $edit_global );
30 push @agent_null_right, $customize if $customizing;
31
32
33 my $precheck_callback = sub {
34   my( $cgi ) = @_;
35
36   my $conf = new FS::Conf;
37
38   foreach (qw( setuptax recurtax disabled )) {
39     $cgi->param($_, '') unless defined $cgi->param($_);
40   }
41
42   return 'Must select a tax class'
43     if $cgi->param('taxclass') eq '(select)';
44
45   my @agents = ();
46   foreach ($cgi->param('agent_type')) {
47     /^(\d+)$/;
48     push @agents, $1 if $1;
49   }
50   return "At least one agent type must be specified."
51     unless scalar(@agents)
52            || ( $cgi->param('clone') && $cgi->param('clone') =~ /^\d+$/ )
53            || ( !$cgi->param('pkgpart') && $conf->exists('agent-defaultpkg') )
54            || $cgi->param('disabled')
55            || $cgi->param('agentnum');
56
57   return '';
58
59 };
60
61 my $custnum = '';
62
63 my $args_callback = sub {
64   my( $cgi, $new ) = @_;
65   
66   my @args = ( 'primary_svc' => scalar($cgi->param('pkg_svc_primary')) );
67
68   ##
69   #options
70   ##
71   
72   $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
73   my $plan = $1;
74   
75   tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
76   my $href = $plans{$plan}->{'fields'};
77   
78   my $error = '';
79   my $options = $cgi->param($plan."__OPTIONS");
80   my @options = split(',', $options);
81   my %options =
82     map { my $optionname = $_;
83           my $param = $plan."__$optionname";
84           my $parser = exists($href->{$optionname}{parse})
85                          ? $href->{$optionname}{parse}
86                          : sub { shift };
87           my $value = join(', ', &$parser($cgi->param($param)));
88           my $check = $href->{$optionname}{check};
89           if ( $check && ! &$check($value) ) {
90             $value = join(', ', $cgi->param($param));
91             $error ||= "Illegal ".
92                          ($href->{$optionname}{name}||$optionname). ": $value";
93           }
94           ( $optionname => $value );
95         }
96         grep { $_ !~ /^report_option_/ }
97         @options;
98
99   foreach ( split(',', $cgi->param('taxproductnums') ) ) {
100     my $value = $cgi->param("taxproductnum_$_");
101     $error ||= "Illegal taxproductnum_$_: $value"
102       unless ( $value =~ /^\d*$/  );
103     $options{"usage_taxproductnum_$_"} = $value;
104   }
105
106   foreach ( $cgi->param('report_option') ) {
107     $error ||= "Illegal optional report class: $_" unless ( $_ =~ /^\d*$/  );
108     $options{"report_option_$_"} = 1;
109   }
110
111   $options{$_} = scalar( $cgi->param($_) )
112     for (qw( setup_fee recur_fee ));
113   
114   push @args, 'options' => \%options;
115
116   ###
117   #pkg_svc
118   ###
119
120   my %pkg_svc = map { $_ => scalar($cgi->param("pkg_svc$_")) }
121                 map { $_->svcpart }
122                 qsearch('part_svc', {} );
123
124   push @args, 'pkg_svc' => \%pkg_svc;
125
126   ###
127   # cust_pkg and custnum_ref (inserts only)
128   ###
129   unless ( $cgi->param('pkgpart') ) {
130     push @args, 'cust_pkg'    => scalar($cgi->param('pkgnum')),
131                 'custnum_ref' => \$custnum;
132   }
133
134   warn "args: ".join('/', @args). "\n";
135
136   @args;
137
138 };
139
140 my $redirect_callback = sub {
141   #my( $cgi, $new ) = @_;
142   return '' unless $custnum;
143   my $show = $curuser->default_customer_view =~ /^(jumbo|packages)$/
144                ? ''
145                : ';show=packages';
146   #my $frag = "cust_pkg$pkgnum"; #hack for IE ignoring real #fragment
147  
148   #can we link back to the specific customized package?  it would be nice...
149   popurl(3). "view/cust_main.cgi?custnum=$custnum$show;dummy=";
150 };
151
152 #these should probably move to @args above and be processed by part_pkg.pm...
153
154 $cgi->param('tax_override') =~ /^([\d,]+)$/;
155 my (@tax_overrides) = (grep "$_", split (",", $1));
156
157 my @process_m2m = (
158   {
159     'link_table'   => 'part_pkg_taxoverride',
160     'target_table' => 'tax_class',
161     'params'       => \@tax_overrides,
162   },
163   { 'link_table'   => 'part_pkg_link',
164     'target_table' => 'part_pkg',
165     'base_field'   => 'src_pkgpart',
166     'target_field' => 'dst_pkgpart',
167     'hashref'      => { 'link_type' => 'svc', 'hidden' => '' },
168     'params'       => [ map $cgi->param($_),
169                         grep /^svc_dst_pkgpart/, $cgi->param
170                       ],
171   },
172   map { 
173     my $hidden = $_;
174     { 'link_table'   => 'part_pkg_link',
175       'target_table' => 'part_pkg',
176       'base_field'   => 'src_pkgpart',
177       'target_field' => 'dst_pkgpart',
178       'hashref'      => { 'link_type' => 'bill', 'hidden' => $hidden },
179       'params'       => [ map { $cgi->param($_) }
180                           grep { my $param = "bill_dst_pkgpart__hidden";
181                                  my $digit = '';
182                                  (($digit) = /^bill_dst_pkgpart(\d+)/ ) &&
183                                  $cgi->param("$param$digit") eq $hidden;
184                                }
185                           $cgi->param
186                         ],
187     },
188   } ( '', 'Y' ),
189 );
190
191 foreach my $override_class ($cgi->param) {
192   next unless $override_class =~ /^tax_override_(\w+)$/;
193   my $class = $1;
194
195   my (@tax_overrides) = (grep "$_", split (",", $1))
196     if $cgi->param($override_class) =~ /^([\d,]+)$/;
197
198   push @process_m2m, {
199     'link_table'   => 'part_pkg_taxoverride',
200     'target_table' => 'tax_class',
201     'hashref'      => { 'usage_class' => $class },
202     'params'       => [ @tax_overrides ],
203   };
204
205 }
206
207 my $conf = new FS::Conf;
208
209 if ( $cgi->param('pkgpart') || ! $conf->exists('agent_defaultpkg') ) {
210   my @agents = ();
211   foreach ($cgi->param('agent_type')) {
212     /^(\d+)$/;
213     push @agents, $1 if $1;
214   }
215   push @process_m2m, {
216     'link_table'   => 'type_pkgs',
217     'target_table' => 'agent_type',
218     'params'       => \@agents,
219   };
220 }
221
222 </%init>