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