b8042026a93c0842d90fe984c6103ca0669a37a7
[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               'process_o2m'       => \@process_o2m,
14           )
15 %>
16 <%init>
17
18 my $customizing = ( ! $cgi->param('pkgpart') && $cgi->param('pkgnum') );
19
20 my $curuser = $FS::CurrentUser::CurrentUser;
21
22 my $edit_global = 'Edit global package definitions';
23 my $customize   = 'Customize customer package';
24
25 die "access denied"
26   unless $curuser->access_right('Edit package definitions')
27       || $curuser->access_right($edit_global)
28       || ( $customizing && $curuser->access_right($customize) );
29
30 my @agent_null_right = ( $edit_global );
31 push @agent_null_right, $customize if $customizing;
32
33
34 my $precheck_callback = sub {
35   my( $cgi ) = @_;
36
37   my $conf = new FS::Conf;
38
39   foreach (qw( setuptax recurtax disabled )) {
40     $cgi->param($_, '') unless defined $cgi->param($_);
41   }
42
43   return 'Must select a tax class'
44     if $cgi->param('taxclass') eq '(select)';
45
46   my @agents = ();
47   foreach ($cgi->param('agent_type')) {
48     /^(\d+)$/;
49     push @agents, $1 if $1;
50   }
51   return "At least one agent type must be specified."
52     unless scalar(@agents)
53            #wtf? || ( $cgi->param('clone') && $cgi->param('clone') =~ /^\d+$/ )
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 my $class ( '', split(',', $cgi->param('taxproductnums') ) ) {
100     my $param = "taxproductnum_$class";
101     my $value = $cgi->param($param);
102
103     if ( $value == -1 ) {
104       my $desc = $cgi->param($param.'_description');
105       # insert a new part_pkg_taxproduct
106       my $engine = FS::TaxEngine->new;
107       my $obj_or_error = $engine->add_taxproduct($desc);
108       if (ref $obj_or_error) {
109         $value = $obj_or_error->taxproductnum;
110         $cgi->param($param, $value); # for error handling
111       } else {
112         die "$obj_or_error (adding tax product)";
113       }
114     }
115
116     $error ||= "Illegal $param: $value"
117       unless ( $value =~ /^\d*$/  );
118     if (length($class)) {
119       $options{"usage_taxproductnum_$class"} = $value;
120     } else {
121       $new->set('taxproductnum', $value);
122     }
123   }
124
125   foreach ( grep $_, $cgi->param('report_option') ) {
126     $error ||= "Illegal optional report class: $_" unless ( $_ =~ /^\d*$/  );
127     $options{"report_option_$_"} = 1;
128   }
129
130   $options{$_} = scalar( $cgi->param($_) )
131     for (qw( setup_fee recur_fee disable_line_item_date_ranges ));
132   
133   push @args, 'options' => \%options;
134
135   ###
136   #part_pkg_currency
137   ###
138
139   my %part_pkg_currency = (
140     map { $_ => scalar($cgi->param($_)) }
141       #grep /._[A-Z]{3}$/, #support other options
142       grep /^(setup|recur)_fee_[A-Z]{3}$/,
143         $cgi->param
144   );
145
146   push @args, 'part_pkg_currency' => \%part_pkg_currency;
147
148   ###
149   # fcc options
150   ###
151   my $fcc_options_string = $cgi->param('fcc_options_string');
152   if ($fcc_options_string) {
153     push @args, 'fcc_options' => decode_json($fcc_options_string);
154   }
155
156   ###
157   #pkg_svc
158   ###
159
160   my @svcparts = map { $_->svcpart } qsearch('part_svc', {});
161   my %pkg_svc    = map { $_ => scalar($cgi->param("pkg_svc$_"  )) } @svcparts;
162   my %hidden_svc = map { $_ => scalar($cgi->param("hidden$_"   )) } @svcparts;
163   my %provision_hold = map { $_ => scalar($cgi->param("provision_hold$_"   )) } @svcparts;
164   my %bulk_skip  = map { $_ => ( $cgi->param("no_bulk_skip$_") eq 'Y'
165                                    ? '' : 'Y'
166                                )
167                                                                   } @svcparts;
168
169   push @args, 'pkg_svc'    => \%pkg_svc,
170               'hidden_svc' => \%hidden_svc,
171               'provision_hold' => \%provision_hold,
172               'bulk_skip'  => \%bulk_skip;
173
174   ###
175   # cust_pkg and custnum_ref (inserts only)
176   ###
177   unless ( $cgi->param('pkgpart') ) {
178     push @args, 'cust_pkg'    => scalar($cgi->param('pkgnum')),
179                 'custnum_ref' => \$custnum;
180   }
181
182   my %part_pkg_vendor;
183   foreach my $param ( $cgi->param ) {
184     if ( $param =~ /^export(\d+)$/ && length($cgi->param($param)) > 0 ) {
185         $part_pkg_vendor{$1} = $cgi->param($param);
186     }
187   }
188   if ( keys %part_pkg_vendor > 0 ) {
189     push @args, 'part_pkg_vendor' => \%part_pkg_vendor;
190   }
191
192   push @args, 'part_pkg_restrict_soft_override' => 1
193     if $cgi->param('part_pkg_restrict_soft_override');
194
195   #warn "args: ".join('/', @args). "\n";
196
197   @args;
198
199 };
200
201 my $redirect_callback = sub {
202   #my( $cgi, $new ) = @_;
203   return '' unless $custnum;
204   my $show = $curuser->default_customer_view =~ /^(jumbo|packages)$/
205                ? ''
206                : ';show=packages';
207   #my $frag = "cust_pkg$pkgnum"; #hack for IE ignoring real #fragment
208  
209   #can we link back to the specific customized package?  it would be nice...
210   popurl(3). "view/cust_main.cgi?custnum=$custnum$show;dummy=";
211 };
212
213 #these should probably move to @args above and be processed by part_pkg.pm...
214
215 $cgi->param('tax_override') =~ /^([\d,]+)$/;
216 my (@tax_overrides) = (grep "$_", split (",", $1));
217
218 my @process_m2m = (
219   {
220     'link_table'   => 'part_pkg_taxoverride',
221     'target_table' => 'tax_class',
222     'params'       => \@tax_overrides,
223   },
224   { 'link_table'   => 'part_pkg_discount',
225     'target_table' => 'discount',
226     'params'       => [ map $cgi->param($_),
227                         grep /^discountnum/, $cgi->param
228                       ],
229   },
230   { 'link_table'   => 'part_pkg_link',
231     'target_table' => 'part_pkg',
232     'base_field'   => 'src_pkgpart',
233     'target_field' => 'dst_pkgpart',
234     'hashref'      => { 'link_type' => 'svc', 'hidden' => '' },
235     'params'       => [ map $cgi->param($_),
236                         grep /^svc_dst_pkgpart/, $cgi->param
237                       ],
238   },
239   { 'link_table'   => 'part_pkg_link',
240     'target_table' => 'part_pkg',
241     'base_field'   => 'src_pkgpart',
242     'target_field' => 'dst_pkgpart',
243     'hashref'      => { 'link_type' => 'supp', 'hidden' => '' },
244     'params'       => [ map $cgi->param($_),
245                         grep /^supp_dst_pkgpart/, $cgi->param
246                       ],
247   },
248   map { 
249     my $hidden = $_;
250     { 'link_table'   => 'part_pkg_link',
251       'target_table' => 'part_pkg',
252       'base_field'   => 'src_pkgpart',
253       'target_field' => 'dst_pkgpart',
254       'hashref'      => { 'link_type' => 'bill', 'hidden' => $hidden },
255       'params'       => [ map { $cgi->param($_) }
256                           grep { my $param = "bill_dst_pkgpart__hidden";
257                                  my $digit = '';
258                                  (($digit) = /^bill_dst_pkgpart(\d+)/ ) &&
259                                  $cgi->param("$param$digit") eq $hidden;
260                                }
261                           $cgi->param
262                         ],
263     },
264   } ( '', 'Y' ),
265 );
266
267 foreach my $override_class ($cgi->param) {
268   next unless $override_class =~ /^tax_override_(\w+)$/;
269   my $class = $1;
270
271   my (@tax_overrides) = (grep "$_", split (",", $1))
272     if $cgi->param($override_class) =~ /^([\d,]+)$/;
273
274   push @process_m2m, {
275     'link_table'   => 'part_pkg_taxoverride',
276     'target_table' => 'tax_class',
277     'hashref'      => { 'usage_class' => $class },
278     'params'       => [ @tax_overrides ],
279   };
280
281 }
282
283 my $conf = new FS::Conf;
284
285 my @agents = ();
286 foreach ($cgi->param('agent_type')) {
287   /^(\d+)$/;
288   push @agents, $1 if $1;
289 }
290 push @process_m2m, {
291   'link_table'   => 'type_pkgs',
292   'target_table' => 'agent_type',
293   'params'       => \@agents,
294 };
295
296 my $targets = FS::part_pkg_usageprice->targets;
297 foreach my $amount_param ( grep /^usagepricepart(\d+)_amount$/, $cgi->param ) {
298   $amount_param =~ /^usagepricepart(\d+)_amount$/ or die 'unpossible';
299   my $num = $1;
300   my $amount = $cgi->param($amount_param);
301   if ( ! $amount && ! $cgi->param("usagepricepart${num}_price") ) {
302     #don't add empty rows just because the dropdowns have a value
303     $cgi->param("usagepricepart${num}_$_", '') for qw( currency action target );
304     next;
305   } 
306   my $target = $cgi->param("usagepricepart${num}_target");
307   $amount *= $targets->{$target}{multiplier} if $targets->{$target}{multiplier};
308   $cgi->param($amount_param, $amount);
309 }
310
311 my @process_o2m = (
312   {
313     'table'  => 'part_pkg_msgcat',
314     'fields' => [qw( locale pkg )],
315   },
316   {
317     'table'  => 'part_pkg_usageprice',
318     'fields' => [qw( price currency action target amount )],
319
320   }
321 );
322
323 </%init>