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