d27ddb0b0d3c8d9cd32f4f43f25ac5579b5adc27
[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 ( 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 ( grep $_, $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 disable_line_item_date_ranges ));
113   
114   push @args, 'options' => \%options;
115
116   ###
117   #part_pkg_currency
118   ###
119
120   my %part_pkg_currency = (
121     map { $_ => scalar($cgi->param($_)) }
122       #grep /._[A-Z]{3}$/, #support other options
123       grep /^(setup|recur)_fee_[A-Z]{3}$/,
124         $cgi->param
125   );
126
127   push @args, 'part_pkg_currency' => \%part_pkg_currency;
128
129   ###
130   # fcc options
131   ###
132   my $fcc_options_string = $cgi->param('fcc_options_string');
133   if ($fcc_options_string) {
134     push @args, 'fcc_options' => decode_json($fcc_options_string);
135   }
136
137   ###
138   #pkg_svc
139   ###
140
141   my @svcparts = map { $_->svcpart } qsearch('part_svc', {});
142   my %pkg_svc    = map { $_ => scalar($cgi->param("pkg_svc$_"  )) } @svcparts;
143   my %hidden_svc = map { $_ => scalar($cgi->param("hidden$_"   )) } @svcparts;
144   my %bulk_skip  = map { $_ => ( $cgi->param("no_bulk_skip$_") eq 'Y'
145                                    ? '' : 'Y'
146                                )
147                                                                   } @svcparts;
148
149   push @args, 'pkg_svc'    => \%pkg_svc,
150               'hidden_svc' => \%hidden_svc,
151               'bulk_skip'  => \%bulk_skip;
152
153   ###
154   # cust_pkg and custnum_ref (inserts only)
155   ###
156   unless ( $cgi->param('pkgpart') ) {
157     push @args, 'cust_pkg'    => scalar($cgi->param('pkgnum')),
158                 'custnum_ref' => \$custnum;
159   }
160
161   my %part_pkg_vendor;
162   foreach my $param ( $cgi->param ) {
163     if ( $param =~ /^export(\d+)$/ && length($cgi->param($param)) > 0 ) {
164         $part_pkg_vendor{$1} = $cgi->param($param);
165     }
166   }
167   if ( keys %part_pkg_vendor > 0 ) {
168     push @args, 'part_pkg_vendor' => \%part_pkg_vendor;
169   }
170
171   #warn "args: ".join('/', @args). "\n";
172
173   @args;
174
175 };
176
177 my $redirect_callback = sub {
178   #my( $cgi, $new ) = @_;
179   return '' unless $custnum;
180   my $show = $curuser->default_customer_view =~ /^(jumbo|packages)$/
181                ? ''
182                : ';show=packages';
183   #my $frag = "cust_pkg$pkgnum"; #hack for IE ignoring real #fragment
184  
185   #can we link back to the specific customized package?  it would be nice...
186   popurl(3). "view/cust_main.cgi?custnum=$custnum$show;dummy=";
187 };
188
189 #these should probably move to @args above and be processed by part_pkg.pm...
190
191 $cgi->param('tax_override') =~ /^([\d,]+)$/;
192 my (@tax_overrides) = (grep "$_", split (",", $1));
193
194 my @process_m2m = (
195   {
196     'link_table'   => 'part_pkg_taxoverride',
197     'target_table' => 'tax_class',
198     'params'       => \@tax_overrides,
199   },
200   { 'link_table'   => 'part_pkg_discount',
201     'target_table' => 'discount',
202     'params'       => [ map $cgi->param($_),
203                         grep /^discountnum/, $cgi->param
204                       ],
205   },
206   { 'link_table'   => 'part_pkg_link',
207     'target_table' => 'part_pkg',
208     'base_field'   => 'src_pkgpart',
209     'target_field' => 'dst_pkgpart',
210     'hashref'      => { 'link_type' => 'svc', 'hidden' => '' },
211     'params'       => [ map $cgi->param($_),
212                         grep /^svc_dst_pkgpart/, $cgi->param
213                       ],
214   },
215   { 'link_table'   => 'part_pkg_link',
216     'target_table' => 'part_pkg',
217     'base_field'   => 'src_pkgpart',
218     'target_field' => 'dst_pkgpart',
219     'hashref'      => { 'link_type' => 'supp', 'hidden' => '' },
220     'params'       => [ map $cgi->param($_),
221                         grep /^supp_dst_pkgpart/, $cgi->param
222                       ],
223   },
224   map { 
225     my $hidden = $_;
226     { 'link_table'   => 'part_pkg_link',
227       'target_table' => 'part_pkg',
228       'base_field'   => 'src_pkgpart',
229       'target_field' => 'dst_pkgpart',
230       'hashref'      => { 'link_type' => 'bill', 'hidden' => $hidden },
231       'params'       => [ map { $cgi->param($_) }
232                           grep { my $param = "bill_dst_pkgpart__hidden";
233                                  my $digit = '';
234                                  (($digit) = /^bill_dst_pkgpart(\d+)/ ) &&
235                                  $cgi->param("$param$digit") eq $hidden;
236                                }
237                           $cgi->param
238                         ],
239     },
240   } ( '', 'Y' ),
241 );
242
243 foreach my $override_class ($cgi->param) {
244   next unless $override_class =~ /^tax_override_(\w+)$/;
245   my $class = $1;
246
247   my (@tax_overrides) = (grep "$_", split (",", $1))
248     if $cgi->param($override_class) =~ /^([\d,]+)$/;
249
250   push @process_m2m, {
251     'link_table'   => 'part_pkg_taxoverride',
252     'target_table' => 'tax_class',
253     'hashref'      => { 'usage_class' => $class },
254     'params'       => [ @tax_overrides ],
255   };
256
257 }
258
259 my $conf = new FS::Conf;
260
261 my @agents = ();
262 foreach ($cgi->param('agent_type')) {
263   /^(\d+)$/;
264   push @agents, $1 if $1;
265 }
266 push @process_m2m, {
267   'link_table'   => 'type_pkgs',
268   'target_table' => 'agent_type',
269   'params'       => \@agents,
270 };
271
272 my $targets = FS::part_pkg_usageprice->targets;
273 foreach my $amount_param ( grep /^usagepricepart(\d+)_amount$/, $cgi->param ) {
274   $amount_param =~ /^usagepricepart(\d+)_amount$/ or die 'unpossible';
275   my $num = $1;
276   my $amount = $cgi->param($amount_param);
277   if ( ! $amount && ! $cgi->param("usagepricepart${num}_price") ) {
278     #don't add empty rows just because the dropdowns have a value
279     $cgi->param("usagepricepart${num}_$_", '') for qw( currency action target );
280     next;
281   } 
282   my $target = $cgi->param("usagepricepart${num}_target");
283   $amount *= $targets->{$target}{multiplier} if $targets->{$target}{multiplier};
284   $cgi->param($amount_param, $amount);
285 }
286
287 my @process_o2m = (
288   {
289     'table'  => 'part_pkg_msgcat',
290     'fields' => [qw( locale pkg )],
291   },
292   {
293     'table'  => 'part_pkg_usageprice',
294     'fields' => [qw( price currency action target amount )],
295
296   }
297 );
298
299 </%init>