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