fix cloning w/new package 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   } else {
214     $part_pkg = FS::part_pkg->new( $hashref );
215   }
216
217   $part_pkg->set($_ => '0')
218     foreach (qw( setup_fee recur_fee ));
219
220   $part_pkg;
221
222 };
223
224 my $edit_callback = sub {
225   my( $cgi, $object, $fields ) = @_;
226
227   $recur_disabled = $object->freq ? 0 : 1;
228
229   (@agent_type) = map {$_->typenum} qsearch('type_pkgs',{'pkgpart'=>$1});
230   $tax_override =
231     join (",", map {$_->taxclassnum}
232                qsearch( 'part_pkg_taxoverride', {'pkgpart' => $1} )
233          );
234
235 #    join (",", map {$_->taxclassnum}
236 #               $part_pkg->part_pkg_taxrate( 'cch', $conf->config('defaultloc')
237 #         );
238 #      unless $tax_override;
239
240   %options = $object->options;
241
242   $object->set($_ => $object->option($_))
243     foreach (qw( setup_fee recur_fee ));
244
245 };
246
247 my $new_callback = sub {
248   my( $cgi, $object, $fields ) = @_;
249
250   my $conf = new FS::Conf; 
251   if ( $conf->exists('agent_defaultpkg') ) {
252     #my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
253     @agent_type = map {$_->typenum} qsearch('agent_type',{});
254   }
255
256 };
257
258 my $m2_error_callback_maker = sub {
259   my $link_type = shift; #yay closures
260   return sub {
261     my( $cgi, $object ) = @_;
262       map  {
263              new FS::part_pkg_link {
264                'link_type'   => $link_type,
265                'src_pkgpart' => $object->pkgpart,
266                'dst_pkgpart' => $_,
267              };
268            }
269       grep $_,
270       map  $cgi->param($_),
271       grep /^${link_type}_dst_pkgpart(\d+)$/, $cgi->param;
272   };
273 };
274
275 my $freq_changed = <<'END';
276   <SCRIPT TYPE="text/javascript">
277
278     function freq_changed(what) {
279       var freq = what.options[what.selectedIndex].value;
280
281       if ( freq == '0' ) {
282         what.form.recur_fee.disabled = true;
283         what.form.recur_fee.style.backgroundColor = '#dddddd';
284       } else {
285         what.form.recur_fee.disabled = false;
286         what.form.recur_fee.style.backgroundColor = '#ffffff';
287       }
288
289     }
290
291   </SCRIPT>
292 END
293
294 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
295
296 tie my %plan_labels, 'Tie::IxHash',
297   map {  $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
298       keys %plans;
299
300 my $html_bottom = sub {
301   my( $object ) = @_;
302
303   #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
304
305   my $layer_callback = sub {
306   
307     my $layer = shift;
308     my $html = ntable("#cccccc",2);
309   
310     #$html .= '
311     #  <TR>
312     #    <TD ALIGN="right">Recurring fee frequency </TD>
313     #    <TD><SELECT NAME="freq">
314     #';
315     #
316     #my @freq = keys %freq;
317     #@freq = grep { /^\d+$/ } @freq
318   #XXX this bit#  #  if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
319     #foreach my $freq ( @freq ) {
320     #  $html .= qq(<OPTION VALUE="$freq");
321     #  $html .= ' SELECTED' if $freq eq $part_pkg->freq;
322     #  $html .= ">$freq{$freq}";
323     #}
324     #$html .= '</SELECT></TD></TR>';
325   
326     my $href = $plans{$layer}->{'fields'};
327     my @fields = exists($plans{$layer}->{'fieldorder'})
328                    ? @{$plans{$layer}->{'fieldorder'}}
329                    : keys %{ $href };
330   
331     foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
332   
333       $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
334   
335       my $format = sub { shift };
336       $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
337
338       #XXX these should use elements/ fields... (or this whole thing should
339       #just use layer_fields instead of layer_callback)
340   
341       if ( ! exists($href->{$field}{'type'}) ) {
342   
343         $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
344                  ( exists($options{$field})
345                      ? &$format($options{$field})
346                      : $href->{$field}{'default'} ).
347                  qq!">!;
348   
349       } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
350   
351         $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
352                  ( exists($options{$field}) && $options{$field}
353                    ? ' CHECKED'
354                    : ''
355                  ). '>';
356   
357       } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
358   
359         $html .= '<SELECT';
360         $html .= ' MULTIPLE'
361           if $href->{$field}{'type'} eq 'select_multiple';
362         $html .= qq! NAME="${layer}__$field">!;
363   
364         if ( $href->{$field}{'select_table'} ) {
365           foreach my $record (
366             qsearch( $href->{$field}{'select_table'},
367                      $href->{$field}{'select_hash'}   )
368           ) {
369             my $value = $record->getfield($href->{$field}{'select_key'});
370             $html .= qq!<OPTION VALUE="$value"!.
371                      (  $options{$field} =~ /(^|, *)$value *(,|$)/ #?
372                           ? ' SELECTED'
373                           : ''
374                      ).
375                      '>'. $record->getfield($href->{$field}{'select_label'});
376           }
377         } elsif ( $href->{$field}{'select_options'} ) {
378           foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
379             my $label = $href->{$field}{'select_options'}{$key};
380             $html .= qq!<OPTION VALUE="$key"!.
381                      ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
382                          ? ' SELECTED'
383                          : ''
384                      ).
385                      '>'. $label;
386           }
387   
388         } else {
389           $html .= '<font color="#ff0000">warning: '.
390                    "don't know how to retreive options for $field select field".
391                    '</font>';
392         }
393         $html .= '</SELECT>';
394   
395       } elsif ( $href->{$field}{'type'} eq 'radio' ) {
396   
397         my $radio =
398           qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
399   
400         foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
401           my $label = $href->{$field}{'options'}{$key};
402           $html .= qq!$radio VALUE="$key"!.
403                    ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
404                        ? ' CHECKED'
405                        : ''
406                    ).
407                    "> $label<BR>";
408         }
409   
410       }
411   
412       $html .= '</TD></TR>';
413     }
414     $html .= '</TABLE>';
415   
416     $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
417              join(',', keys %{ $href } ). '">';
418   
419     $html;
420   
421   };
422
423   my %selectlayers = (
424     field          => 'plan',
425     options        => [ keys %plan_labels ],
426     labels         => \%plan_labels,
427     curr_value     => $object->plan,
428     layer_callback => $layer_callback,
429   );
430
431   include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
432   '<SCRIPT TYPE="text/javascript">'.
433     include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 ).
434   '</SCRIPT>';
435
436 };
437
438 </%init>