fix setup/recur fees on cloning (customizing) package definitions w/new editor
[freeside.git] / httemplate / edit / part_pkg.cgi
1 <% include( 'elements/edit.html',
2               'post_url'    => popurl(1).'process/part_pkg.cgi',
3               'name'        => "Package definition",
4               'table'       => 'part_pkg',
5               #'viewall_dir' => 'browse',
6               'viewall_url' => $p.'browse/part_pkg.cgi',
7               'html_init'   => include('/elements/init_overlib.html').
8                                $freq_changed,
9               'html_bottom' => $html_bottom,
10               'new_hashref_callback' => $new_hashref_callback,
11               'new_object_callback'  => $new_object_callback,
12               'new_callback'         => $new_callback,
13               'edit_callback'        => $edit_callback,
14               'error_callback'       => $error_callback,
15
16               'labels' => { 
17                             'pkgpart'          => 'Package Definition',
18                             'pkg'              => 'Package (customer-visible)',
19                             'comment'          => 'Comment (customer-hidden)',
20                             'classnum'         => 'Package class',
21                             'promo_code'       => 'Promotional code',
22                             'freq'             => 'Recurring fee frequency',
23                             'setuptax'         => 'Setup fee tax exempt',
24                             'recurtax'         => 'Recurring fee tax exempt',
25                             'taxclass'         => 'Tax class',
26                             'plan'             => 'Price plan',
27                             'disabled'         => 'Disable new orders',
28                             'pay_weight'       => 'Payment weight',
29                             'credit_weight'    => 'Credit weight',
30                             'agentnum'         => '',
31                             'setup_fee'        => 'Setup fee',
32                             'recur_fee'        => 'Recurring fee',
33                             'bill_dst_pkgpart' => 'Include line item(s) from package',
34                             'svc_dst_pkgpart'  => 'Include services of package',
35                           },
36
37               'fields' => [
38                             { field=>'clone',  type=>'hidden',
39                               curr_value_callback =>
40                                 sub { shift->param('clone') },
41                             },
42                             { field=>'pkgnum', type=>'hidden',
43                               curr_value_callback =>
44                                 sub { shift->param('pkgnum') },
45                             },
46
47                             { type => 'columnstart' },
48                             
49                               {field=>'pkg',      type=>'text', size=>40 }, #32
50                               {field=>'comment',  type=>'text', size=>40 }, #32
51                               {field=>'classnum', type=>'select-pkg_class' },
52                               {field=>'disabled', type=>'checkbox', value=>'Y'},
53
54                               { type  => 'tablebreak-tr-title',
55                                 value => 'Pricing', #better name?
56                               },
57                               { field => 'plan',
58                                 type  => 'selectlayers-select',
59                                 options => [ keys %plan_labels ],
60                                 labels  => \%plan_labels,
61                               },
62                               { field => 'setup_fee',
63                                 type  => 'money',
64                               },
65                               { field    => 'freq',
66                                 type     => 'part_pkg_freq',
67                                 onchange => 'freq_changed',
68                               },
69                               { field    => 'recur_fee',
70                                 type     => 'money',
71                                 disabled => sub { $recur_disabled },
72                               },
73                                 
74                               #price plan
75                               #setup fee
76                               #recurring frequency
77                               #recurring fee (auto-disable)
78
79                             { type => 'columnnext' },
80
81                               {type=>'justtitle', value=>'Taxation' },
82                               {field=>'setuptax', type=>'checkbox', value=>'Y'},
83                               {field=>'recurtax', type=>'checkbox', value=>'Y'},
84                               {field=>'taxclass', type=>'select-taxclass' },
85                               {field=>'taxproductnum', type=>'select-taxproduct' },
86
87                               { type  => 'tablebreak-tr-title',
88                                 value => 'Promotions', #better name?
89                               },
90                               { field=>'promo_code', type=>'text', size=>15 },
91
92                               { type  => 'tablebreak-tr-title',
93                                 value => 'Line-item revenue recogition', #better name?
94                               },
95                               { field=>'pay_weight',    type=>'text', size=>6 },
96                               { field=>'credit_weight', type=>'text', size=>6 },
97
98                             { type => 'columnnext' },
99
100                               { field=>'agent_type',
101                                 type => 'select-agent_types',
102                                 curr_value_callback => sub {
103                                   my($cgi, $object, $field) = @_;
104                                   #in the other callbacks..?  hmm.
105                                   \@agent_type;
106                                 },
107                               },
108
109                             { type => 'columnend' },
110
111                             { 'type'  => 'tablebreak-tr-title',
112                               'value' => 'Pricing add-ons',
113                             },
114                             { 'field'      => 'bill_dst_pkgpart',
115                               'type'       => 'select-part_pkg',
116                               'm2_label'   => 'Include line item(s) from package',
117                               'm2m_method' => 'bill_part_pkg_link',
118                               'm2m_dstcol' => 'dst_pkgpart',
119                               'm2_error_callback' =>
120                                 &{$m2_error_callback_maker}('bill'),
121                             },
122
123                             { type  => 'tablebreak-tr-title',
124                               value => 'Services',
125                             },
126                             { type => 'pkg_svc', },
127
128                             { 'field'      => 'svc_dst_pkgpart',
129                               'label'      => 'Also include services from package: ',
130                               'type'       => 'select-part_pkg',
131                               'm2_label'   => 'Include services of package: ',
132                               'm2m_method' => 'svc_part_pkg_link',
133                               'm2m_dstcol' => 'dst_pkgpart',
134                               'm2_error_callback' =>
135                                 &{$m2_error_callback_maker}('svc'),
136                             },
137
138                             { type  => 'tablebreak-tr-title',
139                               value => 'Price plan options',
140                             },
141
142                           ],
143
144            )
145 %>
146 <%init>
147
148 my $curuser = $FS::CurrentUser::CurrentUser;
149
150 die "access denied"
151   unless $curuser->access_right('Edit package definitions')
152       || $curuser->access_right('Edit global package definitions')
153       || ( $cgi->param('pkgnum') && $curuser->access_right('Customize customer package') );
154
155 #XXX
156 # - tr-part_pkg_freq: month_increments_only (from price plans)
157 # - test cloning
158 # - test errors cloning
159 # - test custom pricing
160 # - move the selectlayer divs away from lame layer_callback
161
162 #my ($query) = $cgi->keywords;
163 #
164 #my $part_pkg = '';
165
166 my @agent_type = ();
167 my $tax_override;
168
169 my $clone_part_pkg = '';
170
171 my %options = ();
172 my $recur_disabled = 1;
173 my $error_callback = sub {
174   my($cgi, $object, $fields, $opt ) = @_;
175   (@agent_type) = $cgi->param('agent_type');
176   $tax_override = $cgi->param('tax_override');
177   $opt->{action} = 'Custom' if $cgi->param('clone');
178   $clone_part_pkg= qsearchs('part_pkg', { 'pkgpart' => $cgi->param('clone') } );
179
180   $recur_disabled = $cgi->param('freq') ? 0 : 1;
181
182   #some false laziness w/process
183   $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
184   my $plan = $1;
185   my $options = $cgi->param($plan."__OPTIONS");
186   my @options = split(',', $options);
187   %options =
188     map { my $optionname = $_;
189           my $param = $plan."__$optionname";
190           my $value = join(', ', $cgi->param($param));
191           ( $optionname => $value );
192         }
193         @options;
194
195   #$cgi->param($_, $options{$_}) foreach (qw( setup_fee recur_fee ));
196   $object->set($_ => scalar($cgi->param($_)) )
197     foreach (qw( setup_fee recur_fee ));
198
199 };
200
201 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
202
203 my $new_object_callback = sub {
204   my( $cgi, $hashref, $fields, $opt ) = @_;
205
206   my $part_pkg = '';
207   if ( $cgi->param('clone') ) {
208     $opt->{action} = 'Custom';
209     $clone_part_pkg = qsearchs('part_pkg', { pkgpart=>$cgi->param('clone') } );
210     $part_pkg = $clone_part_pkg->clone;
211     $part_pkg->disabled('Y');
212     %options = $clone_part_pkg->options;
213     $part_pkg->set($_ => $option{$_})
214       foreach (qw( setup_fee recur_fee ));
215   } else {
216     $part_pkg = FS::part_pkg->new( $hashref );
217     $part_pkg->set($_ => '0')
218       foreach (qw( setup_fee recur_fee ));
219   }
220
221   $part_pkg;
222
223 };
224
225 my $edit_callback = sub {
226   my( $cgi, $object, $fields ) = @_;
227
228   $recur_disabled = $object->freq ? 0 : 1;
229
230   (@agent_type) = map {$_->typenum} qsearch('type_pkgs',{'pkgpart'=>$1});
231   $tax_override =
232     join (",", map {$_->taxclassnum}
233                qsearch( 'part_pkg_taxoverride', {'pkgpart' => $1} )
234          );
235
236 #    join (",", map {$_->taxclassnum}
237 #               $part_pkg->part_pkg_taxrate( 'cch', $conf->config('defaultloc')
238 #         );
239 #      unless $tax_override;
240
241   %options = $object->options;
242
243   $object->set($_ => $object->option($_))
244     foreach (qw( setup_fee recur_fee ));
245
246 };
247
248 my $new_callback = sub {
249   my( $cgi, $object, $fields ) = @_;
250
251   my $conf = new FS::Conf; 
252   if ( $conf->exists('agent_defaultpkg') ) {
253     #my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
254     @agent_type = map {$_->typenum} qsearch('agent_type',{});
255   }
256
257 };
258
259 my $m2_error_callback_maker = sub {
260   my $link_type = shift; #yay closures
261   return sub {
262     my( $cgi, $object ) = @_;
263       map  {
264              new FS::part_pkg_link {
265                'link_type'   => $link_type,
266                'src_pkgpart' => $object->pkgpart,
267                'dst_pkgpart' => $_,
268              };
269            }
270       grep $_,
271       map  $cgi->param($_),
272       grep /^${link_type}_dst_pkgpart(\d+)$/, $cgi->param;
273   };
274 };
275
276 my $freq_changed = <<'END';
277   <SCRIPT TYPE="text/javascript">
278
279     function freq_changed(what) {
280       var freq = what.options[what.selectedIndex].value;
281
282       if ( freq == '0' ) {
283         what.form.recur_fee.disabled = true;
284         what.form.recur_fee.style.backgroundColor = '#dddddd';
285       } else {
286         what.form.recur_fee.disabled = false;
287         what.form.recur_fee.style.backgroundColor = '#ffffff';
288       }
289
290     }
291
292   </SCRIPT>
293 END
294
295 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
296
297 tie my %plan_labels, 'Tie::IxHash',
298   map {  $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
299       keys %plans;
300
301 my $html_bottom = sub {
302   my( $object ) = @_;
303
304   #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
305
306   my $layer_callback = sub {
307   
308     my $layer = shift;
309     my $html = ntable("#cccccc",2);
310   
311     #$html .= '
312     #  <TR>
313     #    <TD ALIGN="right">Recurring fee frequency </TD>
314     #    <TD><SELECT NAME="freq">
315     #';
316     #
317     #my @freq = keys %freq;
318     #@freq = grep { /^\d+$/ } @freq
319   #XXX this bit#  #  if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
320     #foreach my $freq ( @freq ) {
321     #  $html .= qq(<OPTION VALUE="$freq");
322     #  $html .= ' SELECTED' if $freq eq $part_pkg->freq;
323     #  $html .= ">$freq{$freq}";
324     #}
325     #$html .= '</SELECT></TD></TR>';
326   
327     my $href = $plans{$layer}->{'fields'};
328     my @fields = exists($plans{$layer}->{'fieldorder'})
329                    ? @{$plans{$layer}->{'fieldorder'}}
330                    : keys %{ $href };
331   
332     foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
333   
334       $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
335   
336       my $format = sub { shift };
337       $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
338
339       #XXX these should use elements/ fields... (or this whole thing should
340       #just use layer_fields instead of layer_callback)
341   
342       if ( ! exists($href->{$field}{'type'}) ) {
343   
344         $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
345                  ( exists($options{$field})
346                      ? &$format($options{$field})
347                      : $href->{$field}{'default'} ).
348                  qq!">!;
349   
350       } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
351   
352         $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
353                  ( exists($options{$field}) && $options{$field}
354                    ? ' CHECKED'
355                    : ''
356                  ). '>';
357   
358       } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
359   
360         $html .= '<SELECT';
361         $html .= ' MULTIPLE'
362           if $href->{$field}{'type'} eq 'select_multiple';
363         $html .= qq! NAME="${layer}__$field">!;
364   
365         if ( $href->{$field}{'select_table'} ) {
366           foreach my $record (
367             qsearch( $href->{$field}{'select_table'},
368                      $href->{$field}{'select_hash'}   )
369           ) {
370             my $value = $record->getfield($href->{$field}{'select_key'});
371             $html .= qq!<OPTION VALUE="$value"!.
372                      (  $options{$field} =~ /(^|, *)$value *(,|$)/ #?
373                           ? ' SELECTED'
374                           : ''
375                      ).
376                      '>'. $record->getfield($href->{$field}{'select_label'});
377           }
378         } elsif ( $href->{$field}{'select_options'} ) {
379           foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
380             my $label = $href->{$field}{'select_options'}{$key};
381             $html .= qq!<OPTION VALUE="$key"!.
382                      ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
383                          ? ' SELECTED'
384                          : ''
385                      ).
386                      '>'. $label;
387           }
388   
389         } else {
390           $html .= '<font color="#ff0000">warning: '.
391                    "don't know how to retreive options for $field select field".
392                    '</font>';
393         }
394         $html .= '</SELECT>';
395   
396       } elsif ( $href->{$field}{'type'} eq 'radio' ) {
397   
398         my $radio =
399           qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
400   
401         foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
402           my $label = $href->{$field}{'options'}{$key};
403           $html .= qq!$radio VALUE="$key"!.
404                    ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
405                        ? ' CHECKED'
406                        : ''
407                    ).
408                    "> $label<BR>";
409         }
410   
411       }
412   
413       $html .= '</TD></TR>';
414     }
415     $html .= '</TABLE>';
416   
417     $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
418              join(',', keys %{ $href } ). '">';
419   
420     $html;
421   
422   };
423
424   my %selectlayers = (
425     field          => 'plan',
426     options        => [ keys %plan_labels ],
427     labels         => \%plan_labels,
428     curr_value     => $object->plan,
429     layer_callback => $layer_callback,
430   );
431
432   include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
433   '<SCRIPT TYPE="text/javascript">'.
434     include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 ).
435   '</SCRIPT>';
436
437 };
438
439 </%init>