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