add plan & pricing to package browse...
[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 # - display add-ons in browse... yeah
152 # -QIS- thank goodness
153 # - test cloning
154 # - test custom pricing
155 #recur_flat->recur_fee migration, ugh
156 # - move the selectlayer divs away from lame layer_callback
157
158 #my ($query) = $cgi->keywords;
159 #
160 #my $part_pkg = '';
161
162 my @agent_type = ();
163 my $tax_override;
164
165 my $clone_part_pkg = '';
166
167 my %options = ();
168 my $recur_disabled = 1;
169 my $error_callback = sub {
170   my($cgi, $object, $fields) = @_;
171   (@agent_type) = $cgi->param('agent_type');
172   $tax_override = $cgi->param('tax_override');
173   $clone_part_pkg= qsearchs('part_pkg', { 'pkgpart' => $cgi->param('clone') } );
174
175   $recur_disabled = $cgi->param('freq') ? 0 : 1;
176
177   #some false laziness w/process
178   $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
179   my $plan = $1;
180   my $options = $cgi->param($plan."__OPTIONS");
181   my @options = split(',', $options);
182   %options =
183     map { my $optionname = $_;
184           my $param = $plan."__$optionname";
185           my $value = join(', ', $cgi->param($param));
186           ( $optionname => $value );
187         }
188         @options;
189
190   #$cgi->param($_, $options{$_}) foreach (qw( setup_fee recur_fee ));
191   $object->set($_ => scalar($cgi->param($_)) )
192     foreach (qw( setup_fee recur_fee ));
193
194 };
195
196 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
197
198 my $new_object_callback = sub {
199   my( $cgi, $hashref, $fields, $opt ) = @_;
200
201   my $part_pkg = '';
202   if ( $cgi->param('clone') ) {
203     $opt->{action} = 'Custom';
204     $clone_part_pkg = qsearchs('part_pkg', { pkgpart=>$cgi->param('clone') } );
205     $part_pkg = $clone_part_pkg->clone;
206     $part_pkg->disabled('Y');
207     %options = $clone_part_pkg->options;
208   } else {
209     $part_pkg = FS::part_pkg->new( $hashref );
210   }
211
212   $part_pkg->set($_ => '0')
213     foreach (qw( setup_fee recur_fee ));
214
215   $part_pkg;
216
217 };
218
219 my $edit_callback = sub {
220   my( $cgi, $object, $fields ) = @_;
221
222   $recur_disabled = $object->freq ? 0 : 1;
223
224   (@agent_type) = map {$_->typenum} qsearch('type_pkgs',{'pkgpart'=>$1});
225   $tax_override =
226     join (",", map {$_->taxclassnum}
227                qsearch( 'part_pkg_taxoverride', {'pkgpart' => $1} )
228          );
229
230 #    join (",", map {$_->taxclassnum}
231 #               $part_pkg->part_pkg_taxrate( 'cch', $conf->config('defaultloc')
232 #         );
233 #      unless $tax_override;
234
235   %options = $object->options;
236
237   $object->set($_ => $object->option($_))
238     foreach (qw( setup_fee recur_fee ));
239
240 };
241
242 my $new_callback = sub {
243   my( $cgi, $object, $fields ) = @_;
244
245   my $conf = new FS::Conf; 
246   if ( $conf->exists('agent_defaultpkg') ) {
247     #my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
248     @agent_type = map {$_->typenum} qsearch('agent_type',{});
249   }
250
251 };
252
253 my $m2_error_callback_maker = sub {
254   my $link_type = shift; #yay closures
255   return sub {
256     my( $cgi, $object ) = @_;
257       map  {
258              new FS::part_pkg_link {
259                'link_type'   => $link_type,
260                'src_pkgpart' => $object->pkgpart,
261                'dst_pkgpart' => $_,
262              };
263            }
264       grep $_,
265       map  $cgi->param($_),
266       grep /^${link_type}_dst_pkgpart(\d+)$/, $cgi->param;
267   };
268 };
269
270 my $freq_changed = <<'END';
271   <SCRIPT TYPE="text/javascript">
272
273     function freq_changed(what) {
274       var freq = what.options[what.selectedIndex].value;
275
276       if ( freq == '0' ) {
277         what.form.recur_fee.disabled = true;
278         what.form.recur_fee.style.backgroundColor = '#dddddd';
279       } else {
280         what.form.recur_fee.disabled = false;
281         what.form.recur_fee.style.backgroundColor = '#ffffff';
282       }
283
284     }
285
286   </SCRIPT>
287 END
288
289 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
290
291 tie my %plan_labels, 'Tie::IxHash',
292   map {  $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
293       keys %plans;
294
295 my $html_bottom = sub {
296   my( $object ) = @_;
297
298   #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
299
300   my $layer_callback = sub {
301   
302     my $layer = shift;
303     my $html = ntable("#cccccc",2);
304   
305     #$html .= '
306     #  <TR>
307     #    <TD ALIGN="right">Recurring fee frequency </TD>
308     #    <TD><SELECT NAME="freq">
309     #';
310     #
311     #my @freq = keys %freq;
312     #@freq = grep { /^\d+$/ } @freq
313   #XXX this bit#  #  if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
314     #foreach my $freq ( @freq ) {
315     #  $html .= qq(<OPTION VALUE="$freq");
316     #  $html .= ' SELECTED' if $freq eq $part_pkg->freq;
317     #  $html .= ">$freq{$freq}";
318     #}
319     #$html .= '</SELECT></TD></TR>';
320   
321     my $href = $plans{$layer}->{'fields'};
322     my @fields = exists($plans{$layer}->{'fieldorder'})
323                    ? @{$plans{$layer}->{'fieldorder'}}
324                    : keys %{ $href };
325   
326     foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
327   
328       $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
329   
330       my $format = sub { shift };
331       $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
332
333       #XXX these should use elements/ fields... (or this whole thing should
334       #just use layer_fields instead of layer_callback)
335   
336       if ( ! exists($href->{$field}{'type'}) ) {
337   
338         $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
339                  ( exists($options{$field})
340                      ? &$format($options{$field})
341                      : $href->{$field}{'default'} ).
342                  qq!">!;
343   
344       } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
345   
346         $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
347                  ( exists($options{$field}) && $options{$field}
348                    ? ' CHECKED'
349                    : ''
350                  ). '>';
351   
352       } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
353   
354         $html .= '<SELECT';
355         $html .= ' MULTIPLE'
356           if $href->{$field}{'type'} eq 'select_multiple';
357         $html .= qq! NAME="${layer}__$field">!;
358   
359         if ( $href->{$field}{'select_table'} ) {
360           foreach my $record (
361             qsearch( $href->{$field}{'select_table'},
362                      $href->{$field}{'select_hash'}   )
363           ) {
364             my $value = $record->getfield($href->{$field}{'select_key'});
365             $html .= qq!<OPTION VALUE="$value"!.
366                      (  $options{$field} =~ /(^|, *)$value *(,|$)/ #?
367                           ? ' SELECTED'
368                           : ''
369                      ).
370                      '>'. $record->getfield($href->{$field}{'select_label'});
371           }
372         } elsif ( $href->{$field}{'select_options'} ) {
373           foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
374             my $label = $href->{$field}{'select_options'}{$key};
375             $html .= qq!<OPTION VALUE="$key"!.
376                      ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
377                          ? ' SELECTED'
378                          : ''
379                      ).
380                      '>'. $label;
381           }
382   
383         } else {
384           $html .= '<font color="#ff0000">warning: '.
385                    "don't know how to retreive options for $field select field".
386                    '</font>';
387         }
388         $html .= '</SELECT>';
389   
390       } elsif ( $href->{$field}{'type'} eq 'radio' ) {
391   
392         my $radio =
393           qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
394   
395         foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
396           my $label = $href->{$field}{'options'}{$key};
397           $html .= qq!$radio VALUE="$key"!.
398                    ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
399                        ? ' CHECKED'
400                        : ''
401                    ).
402                    "> $label<BR>";
403         }
404   
405       }
406   
407       $html .= '</TD></TR>';
408     }
409     $html .= '</TABLE>';
410   
411     $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
412              join(',', keys %{ $href } ). '">';
413   
414     $html;
415   
416   };
417
418   my %selectlayers = (
419     field          => 'plan',
420     options        => [ keys %plan_labels ],
421     labels         => \%plan_labels,
422     curr_value     => $object->plan,
423     layer_callback => $layer_callback,
424   );
425
426   include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
427   '<SCRIPT TYPE="text/javascript">'.
428     include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 ).
429   '</SCRIPT>';
430
431 };
432
433 </%init>