ui
[freeside.git] / httemplate / edit / part_pkg.cgi
1 <!-- mason kludge -->
2 <%
3
4 if ( $cgi->param('clone') && $cgi->param('clone') =~ /^(\d+)$/ ) {
5   $cgi->param('clone', $1);
6 } else {
7   $cgi->param('clone', '');
8 }
9 if ( $cgi->param('pkgnum') && $cgi->param('pkgnum') =~ /^(\d+)$/ ) {
10   $cgi->param('pkgnum', $1);
11 } else {
12   $cgi->param('pkgnum', '');
13 }
14
15 my ($query) = $cgi->keywords;
16 my $action = '';
17 my $part_pkg = '';
18 if ( $cgi->param('error') ) {
19   $part_pkg = new FS::part_pkg ( {
20     map { $_, scalar($cgi->param($_)) } fields('part_pkg')
21   } );
22 }
23 if ( $cgi->param('clone') ) {
24   $action='Custom Pricing';
25   my $old_part_pkg =
26     qsearchs('part_pkg', { 'pkgpart' => $cgi->param('clone') } );
27   $part_pkg ||= $old_part_pkg->clone;
28   $part_pkg->disabled('Y');
29 } elsif ( $query && $query =~ /^(\d+)$/ ) {
30   $part_pkg ||= qsearchs('part_pkg',{'pkgpart'=>$1});
31 } else {
32   unless ( $part_pkg ) {
33     $part_pkg = new FS::part_pkg {};
34     $part_pkg->plan('flat');
35   }
36 }
37 unless ( $part_pkg->plan ) { #backwards-compat
38   $part_pkg->plan('flat');
39   $part_pkg->plandata("setup_fee=". $part_pkg->setup. "\n".
40                       "recur_fee=". $part_pkg->recur. "\n");
41 }
42 $action ||= $part_pkg->pkgpart ? 'Edit' : 'Add';
43 my $hashref = $part_pkg->hashref;
44
45
46 print header("$action Package Definition", menubar(
47   'Main Menu' => popurl(2),
48   'View all packages' => popurl(2). 'browse/part_pkg.cgi',
49 ));
50 #), ' onLoad="visualize()"');
51
52 print qq!<FONT SIZE="+1" COLOR="#ff0000">Error: !, $cgi->param('error'),
53       "</FONT>"
54   if $cgi->param('error');
55
56 #print '<FORM ACTION="', popurl(1), 'process/part_pkg.cgi" METHOD=POST>';
57 print '<FORM NAME="dummy">';
58
59 #if ( $cgi->param('clone') ) {
60 #  print qq!<INPUT TYPE="hidden" NAME="clone" VALUE="!, $cgi->param('clone'), qq!">!;
61 #}
62 #if ( $cgi->param('pkgnum') ) {
63 #  print qq!<INPUT TYPE="hidden" NAME="pkgnum" VALUE="!, $cgi->param('pkgnum'), qq!">!;
64 #}
65 #
66 #print qq!<INPUT TYPE="hidden" NAME="pkgpart" VALUE="$hashref->{pkgpart}">!,
67 print "Package Part #", $hashref->{pkgpart} ? $hashref->{pkgpart} : "(NEW)";
68
69 print ntable("#cccccc",2), <<END;
70 <TR><TD ALIGN="right">Package (customer-visable)</TD><TD><INPUT TYPE="text" NAME="pkg" SIZE=32 VALUE="$hashref->{pkg}"></TD></TR>
71 <TR><TD ALIGN="right">Comment (customer-hidden)</TD><TD><INPUT TYPE="text" NAME="comment" SIZE=32 VALUE="$hashref->{comment}"></TD></TR>
72 <TR><TD ALIGN="right">Frequency (months) of recurring fee</TD><TD><INPUT TYPE="text" NAME="freq" VALUE="$hashref->{freq}" SIZE=3>&nbsp;&nbsp;<I>0=no recurring fee, 1=monthly, 3=quarterly, 12=yearly</TD></TR>
73 <TR><TD ALIGN="right">Setup fee tax exempt</TD><TD>
74 END
75
76 print '<INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y"';
77 print ' CHECKED' if $hashref->{setuptax} eq "Y";
78 print '>';
79
80 print <<END;
81 </TD></TR>
82 <TR><TD ALIGN="right">Recurring fee tax exempt</TD><TD>
83 END
84
85 print '<INPUT TYPE="checkbox" NAME="recurtax" VALUE="Y"';
86 print ' CHECKED' if $hashref->{recurtax} eq "Y";
87 print '>';
88
89 print '</TD></TR>';
90
91 my $conf = new FS::Conf;
92 if ( $conf->exists('enable_taxclasses') ) {
93   print '<TR><TD ALIGN="right">Tax class</TD><TD><SELECT NAME="taxclass">';
94   my $sth = dbh->prepare('SELECT DISTINCT taxclass FROM cust_main_county')
95     or die dbh->errstr;
96   $sth->execute or die $sth->errstr;
97   foreach my $taxclass ( map $_->[0], @{$sth->fetchall_arrayref} ) {
98     print qq!<OPTION VALUE="$taxclass"!;
99     print ' SELECTED' if $taxclass eq $hashref->{taxclass};
100     print qq!>$taxclass</OPTION>!;
101   }
102   print '</SELECT></TD></TR>';
103 } else {
104   print
105     '<INPUT TYPE="hidden" NAME="taxclass" VALUE="'. $hashref->{taxclass}. '">';
106 }
107
108 print '<TR><TD ALIGN="right">Disable new orders</TD><TD>';
109 print '<INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"';
110 print ' CHECKED' if $hashref->{disabled} eq "Y";
111 print '>';
112 print '</TD></TR></TABLE>';
113
114 my $thead =  "\n\n". ntable('#cccccc', 2). <<END;
115 <TR><TH BGCOLOR="#dcdcdc"><FONT SIZE=-1>Quan.</FONT></TH><TH BGCOLOR="#dcdcdc">Service</TH></TR>
116 END
117
118 #unless ( $cgi->param('clone') ) {
119 #dunno why...
120 unless ( 0 ) {
121   #print <<END, $thead;
122   print <<END, itable(), '<TR><TD VALIGN="top">', $thead;
123 <BR><BR>Enter the quantity of each service this package includes.<BR><BR>
124 END
125 }
126
127 my @fixups = ();
128 my $count = 0;
129 my $columns = 3;
130 my @part_svc = qsearch( 'part_svc', { 'disabled' => '' } );
131 foreach my $part_svc ( @part_svc ) {
132   my $svcpart = $part_svc->svcpart;
133   my $pkg_svc = qsearchs( 'pkg_svc', {
134     'pkgpart'  => $cgi->param('clone') || $part_pkg->pkgpart,
135     'svcpart'  => $svcpart,
136   } ) || new FS::pkg_svc ( {
137     'pkgpart'  => $cgi->param('clone') || $part_pkg->pkgpart,
138     'svcpart'  => $svcpart,
139     'quantity' => 0,
140   });
141   #? #next unless $pkg_svc;
142
143   push @fixups, "pkg_svc$svcpart";
144
145   #unless ( defined ($cgi->param('clone')) && $cgi->param('clone') ) {
146   #dunno why...
147   unless ( 0 ) {
148     print '<TR>'; # if $count == 0 ;
149     print qq!<TD><INPUT TYPE="text" NAME="pkg_svc$svcpart" SIZE=4 MAXLENGTH=3 VALUE="!,
150           $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0,
151           qq!"></TD><TD><A HREF="part_svc.cgi?!,$part_svc->svcpart,
152           qq!">!, $part_svc->getfield('svc'), "</A></TD></TR>";
153 #    print "</TABLE></TD><TD>$thead" if ++$count == int(scalar(@part_svc) / 2);
154     $count+=1;
155     foreach ( 1 .. $columns-1 ) {
156       print "</TABLE></TD><TD VALIGN=\"top\">$thead"
157         if $count == int( $_ * scalar(@part_svc) / $columns );
158     }
159   } else {
160     print qq!<INPUT TYPE="hidden" NAME="pkg_svc$svcpart" VALUE="!,
161           $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0, qq!">\n!;
162   }
163 }
164
165 #unless ( $cgi->param('clone') ) {
166 #dunno why...
167 unless ( 0 ) {
168   print "</TR></TABLE></TD></TR></TABLE>";
169   #print "</TR></TABLE>";
170 }
171
172 foreach my $f ( qw( clone pkgnum ) ) {
173   print qq!<INPUT TYPE="hidden" NAME="$f" VALUE="!. $cgi->param($f). '">';
174 }
175 print '<INPUT TYPE="hidden" NAME="pkgpart" VALUE="'. $part_pkg->pkgpart. '">';
176
177 # prolly should be in database
178 tie my %plans, 'Tie::IxHash',
179   'flat' => {
180     'name' => 'Flat rate (anniversary billing)',
181     'fields' => {
182       'setup_fee' => { 'name' => 'Setup fee for this package',
183                        'default' => 0,
184                      },
185       'recur_fee' => { 'name' => 'Recurring fee for this package',
186                        'default' => 0,
187                       },
188     },
189     'fieldorder' => [ 'setup_fee', 'recur_fee' ],
190     'setup' => 'what.setup_fee.value',
191     'recur' => 'what.recur_fee.value',
192   },
193
194   'flat_delayed' => {
195     'name' => 'Free for X days, then flat rate (anniversary billing)',
196     'fields' =>  {
197       'free_days' => { 'name' => 'Initial free days',
198                        'default' => 0,
199                      },
200       'setup_fee' => { 'name' => 'Setup fee for this package',
201                        'default' => 0,
202                      },
203       'recur_fee' => { 'name' => 'Recurring fee for this package',
204                        'default' => 0,
205                       },
206     },
207     'fieldorder' => [ 'free_days', 'setup_fee', 'recur_fee' ],
208     'setup' => '\'my $d = $cust_pkg->bill || $time; $d += 86400 * \' + what.free_days.value + \'; $cust_pkg->bill($d); $cust_pkg_mod_flag=1; \' + what.setup_fee.value',
209     'recur' => 'what.recur_fee.value',
210   },
211
212   'prorate' => {
213     'name' => 'First partial month pro-rated, then flat-rate (1st of month billing)',
214     'fields' =>  {
215       'setup_fee' => { 'name' => 'Setup fee for this package',
216                        'default' => 0,
217                      },
218       'recur_fee' => { 'name' => 'Recurring fee for this package',
219                        'default' => 0,
220                       },
221     },
222     'fieldorder' => [ 'setup_fee', 'recur_fee' ],
223     'setup' => 'what.setup_fee.value',
224     'recur' => '\'my $mnow = $sdate; my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($sdate) )[0,1,2,3,4,5]; my $mstart = timelocal(0,0,0,1,$mon,$year); my $mend = timelocal(0,0,0,1, $mon == 11 ? 0 : $mon+1, $year+($mon==11)); $sdate = $mstart; ( $part_pkg->freq - 1 ) * \' + what.recur_fee.value + \' / $part_pkg->freq + \' + what.recur_fee.value + \' / $part_pkg->freq * ($mend-$mnow) / ($mend-$mstart) ; \'',
225   },
226
227   'subscription' => {
228     'name' => 'First partial month full charge, then flat-rate (1st of month billing)',
229     'fields' => {
230       'setup_fee' => { 'name' => 'Setup fee for this package',
231                        'default' => 0,
232                      },
233       'recur_fee' => { 'name' => 'Recurring fee for this package',
234                        'default' => 0,
235                       },
236     },
237     'fieldorder' => [ 'setup_fee', 'recur_fee' ],
238     'setup' => 'what.setup_fee.value',
239     'recur' => '\'my $mnow = $sdate; my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($sdate) )[0,1,2,3,4,5]; $sdate = timelocal(0,0,0,1,$mon,$year); \' + what.recur_fee.value',
240   },
241
242   'flat_comission_cust' => {
243     'name' => 'Flat rate with recurring comission per active customer',
244     'fields' => {
245       'setup_fee' => { 'name' => 'Setup fee for this package',
246                        'default' => 0,
247                      },
248       'recur_fee' => { 'name' => 'Recurring fee for this package',
249                        'default' => 0,
250                      },
251       'comission_amount' => { 'name' => 'Comission amount per month (per active customer)',
252                               'default' => 0,
253                             },
254       'comission_depth'  => { 'name' => 'Number of layers',
255                               'default' => 1,
256                             },
257     },
258     'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
259     'setup' => 'what.setup_fee.value',
260     'recur' => '\'my $error = $cust_pkg->cust_main->credit( \' + what.comission_amount.value + \' * scalar($cust_pkg->cust_main->referral_cust_main_ncancelled(\' + what.comission_depth.value+ \')), "commission" ); die $error if $error; \' + what.recur_fee.value + \';\'',
261   },
262
263   'flat_comission' => {
264     'name' => 'Flat rate with recurring comission per (any) active package',
265     'fields' => {
266       'setup_fee' => { 'name' => 'Setup fee for this package',
267                        'default' => 0,
268                      },
269       'recur_fee' => { 'name' => 'Recurring fee for this package',
270                        'default' => 0,
271                      },
272       'comission_amount' => { 'name' => 'Comission amount per month (per active package)',
273                               'default' => 0,
274                             },
275       'comission_depth'  => { 'name' => 'Number of layers',
276                               'default' => 1,
277                             },
278     },
279     'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
280     'setup' => 'what.setup_fee.value',
281     'recur' => '\'my $error = $cust_pkg->cust_main->credit( \' + what.comission_amount.value + \' * scalar($cust_pkg->cust_main->referral_cust_pkg(\' + what.comission_depth.value+ \')), "commission" ); die $error if $error; \' + what.recur_fee.value + \';\'',
282   },
283
284   'flat_comission_pkg' => {
285     'name' => 'Flat rate with recurring comission per (selected) active package',
286     'fields' => {
287       'setup_fee' => { 'name' => 'Setup fee for this package',
288                        'default' => 0,
289                      },
290       'recur_fee' => { 'name' => 'Recurring fee for this package',
291                        'default' => 0,
292                      },
293       'comission_amount' => { 'name' => 'Comission amount per month (per uncancelled package)',
294                               'default' => 0,
295                             },
296       'comission_depth'  => { 'name' => 'Number of layers',
297                               'default' => 1,
298                             },
299       'comission_pkgpart' => { 'name' => 'Applicable packages<BR><FONT SIZE="-1">(hold <b>ctrl</b> to select multiple packages)</FONT>',
300                                'type' => 'select_multiple',
301                                'select_table' => 'part_pkg',
302                                'select_hash'  => { 'disabled' => '' } ,
303                                'select_key'   => 'pkgpart',
304                                'select_label' => 'pkg',
305                              },
306     },
307     'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount', 'comission_pkgpart' ],
308     'setup' => 'what.setup_fee.value',
309     'recur' => '""; var pkgparts = ""; for ( var c=0; c < document.flat_comission_pkg.comission_pkgpart.options.length; c++ ) { if (document.flat_comission_pkg.comission_pkgpart.options[c].selected) { pkgparts = pkgparts + document.flat_comission_pkg.comission_pkgpart.options[c].value + \', \'; } } what.recur.value = \'my $error = $cust_pkg->cust_main->credit( \' + what.comission_amount.value + \' * scalar( grep { my $pkgpart = $_->pkgpart; grep { $_ == $pkgpart } ( \' + pkgparts + \'  ) } $cust_pkg->cust_main->referral_cust_pkg(\' + what.comission_depth.value+ \')), "commission" ); die $error if $error; \' + what.recur_fee.value + \';\'',
310   },
311
312
313
314   'sesmon_hour' => {
315     'name' => 'Base charge plus charge per-hour from the session monitor',
316     'fields' => {
317       'setup_fee' => { 'name' => 'Setup fee for this package',
318                        'default' => 0,
319                      },
320       'recur_flat' => { 'name' => 'Base monthly charge for this package',
321                         'default' => 0,
322                       },
323       'recur_included_hours' => { 'name' => 'Hours included',
324                                   'default' => 0,
325                                 },
326       'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
327                                  'default' => 0,
328                                },
329     },
330     'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_hours', 'recur_hourly_charge' ],
331     'setup' => 'what.setup_fee.value',
332     'recur' => '\'my $hours = $cust_pkg->seconds_since($cust_pkg->bill || 0) / 3600 - \' + what.recur_included_hours.value + \'; $hours = 0 if $hours < 0; \' + what.recur_flat.value + \' + \' + what.recur_hourly_charge.value + \' * $hours;\'',
333   },
334
335   'sesmon_minute' => {
336     'name' => 'Base charge plus charge per-minute from the session monitor',
337     'fields' => {
338       'setup_fee' => { 'name' => 'Setup fee for this package',
339                        'default' => 0,
340                      },
341       'recur_flat' => { 'name' => 'Base monthly charge for this package',
342                         'default' => 0,
343                       },
344       'recur_included_min' => { 'name' => 'Minutes included',
345                                 'default' => 0,
346                                 },
347       'recur_minly_charge' => { 'name' => 'Additional charge per minute',
348                                 'default' => 0,
349                               },
350     },
351     'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_min', 'recur_minly_charge' ],
352     'setup' => 'what.setup_fee.value',
353     'recur' => '\'my $min = $cust_pkg->seconds_since($cust_pkg->bill || 0) / 60 - \' + what.recur_included_min.value + \'; $min = 0 if $min < 0; \' + what.recur_flat.value + \' + \' + what.recur_minly_charge.value + \' * $min;\'',
354
355   },
356
357 ;
358
359 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
360                     split("\n", $part_pkg->plandata );
361
362 tie my %options, 'Tie::IxHash', map { $_=>$plans{$_}->{'name'} } keys %plans;
363
364 my @form_select = ();
365 if ( $conf->exists('enable_taxclasses') ) {
366   push @form_select, 'taxclass';
367 } else {
368   push @fixups, 'taxclass'; #hidden
369 }
370
371
372 my $widget = new HTML::Widgets::SelectLayers(
373   'selected_layer' => $part_pkg->plan,
374   'options'        => \%options,
375   'form_name'      => 'dummy',
376   'form_action'    => 'process/part_pkg.cgi',
377   'form_text'      => [ qw(pkg comment freq clone pkgnum pkgpart), @fixups ],
378   'form_checkbox'  => [ qw(setuptax recurtax disabled) ],
379   'form_select'    => [ @form_select ],
380   'fixup_callback' => sub {
381                         #my $ = @_;
382                         my $html = '';
383                         for my $p ( keys %plans ) {
384                           $html .= "if ( what.plan.value == \"$p\" ) {
385                                       what.setup.value = $plans{$p}->{setup} ;
386                                       what.recur.value = $plans{$p}->{recur} ;
387                                     }\n";
388                         }
389                         $html;
390                       },
391   'layer_callback' => sub {
392     my $layer = shift;
393     my $html = qq!<INPUT TYPE="hidden" NAME="plan" VALUE="$layer">!.
394                ntable("#cccccc",2);
395     my $href = $plans{$layer}->{'fields'};
396     foreach my $field ( exists($plans{$layer}->{'fieldorder'})
397                           ? @{$plans{$layer}->{'fieldorder'}}
398                           : keys %{ $href }
399                       ) {
400
401       $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
402
403       if ( ! exists($href->{$field}{'type'}) ) {
404         $html .= qq!<INPUT TYPE="text" NAME="$field" VALUE="!.
405                  ( exists($plandata{$field})
406                      ? $plandata{$field}
407                      : $href->{$field}{'default'} ).
408                  qq!" onChange="fchanged(this)">!;
409       } elsif ( $href->{$field}{'type'} eq 'select_multiple' ) {
410         $html .= qq!<SELECT MULTIPLE NAME="$field" onChange="fchanged(this)">!;
411         foreach my $record (
412           qsearch( $href->{$field}{'select_table'},
413                    $href->{$field}{'select_hash'}   )
414         ) {
415           my $value = $record->getfield($href->{$field}{'select_key'});
416           $html .= qq!<OPTION VALUE="$value"!.
417                    (  $plandata{$field} =~ /(^|, *)$value *(,|$)/
418                         ? ' SELECTED'
419                         : ''          ).
420                    '>'. $record->getfield($href->{$field}{'select_label'})
421         }
422         $html .= '</SELECT>';
423       }
424
425       $html .= '</TD></TR>';
426     }
427     $html .= '</TABLE>';
428
429     $html .= '<INPUT TYPE="hidden" NAME="plandata" VALUE="'.
430              join(',', keys %{ $href } ). '">'.
431              '<BR><BR>';
432              
433     $html .= '<INPUT TYPE="submit" VALUE="'.
434              ( $hashref->{pkgpart} ? "Apply changes" : "Add package" ).
435              '" onClick="fchanged(this)">';
436
437     $html .= '<BR><BR>don\'t edit this unless you know what you\'re doing '.
438              '<INPUT TYPE="button" VALUE="refresh expressions" '.
439                'onClick="fchanged(this)">'.
440              ntable("#cccccc",2).
441              '<TR><TD>'.
442              '<FONT SIZE="1">Setup expression<BR>'.
443              '<INPUT TYPE="text" NAME="setup" SIZE="160" VALUE="'.
444                $hashref->{setup}. '" onLoad="fchanged(this)">'.
445              '</FONT><BR>'.
446              '<FONT SIZE="1">Recurring espression<BR>'.
447              '<INPUT TYPE="text" NAME="recur" SIZE="160" VALUE="'.
448                $hashref->{recur}. '" onLoad="fchanged(this)">'.
449              '</FONT>'.
450              '</TR></TD>'.
451              '</TABLE>';
452
453     $html;
454
455   },
456 );
457
458 %>
459
460 <BR>
461 Price plan <%= $widget->html %>
462   </BODY>
463 </HTML>