"subscription" price plan from "Luke Pfeifer" <freeside@globalli.com>
[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></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 print '<TR><TD ALIGN="right">Disable new orders</TD><TD>';
92 print '<INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"';
93 print ' CHECKED' if $hashref->{disabled} eq "Y";
94 print '>';
95 print '</TD></TR></TABLE>';
96
97 my $thead =  "\n\n". ntable('#cccccc', 2). <<END;
98 <TR><TH BGCOLOR="#dcdcdc"><FONT SIZE=-1>Quan.</FONT></TH><TH BGCOLOR="#dcdcdc">Service</TH></TR>
99 END
100
101 #unless ( $cgi->param('clone') ) {
102 #dunno why...
103 unless ( 0 ) {
104   #print <<END, $thead;
105   print <<END, itable(), '<TR><TD VALIGN="top">', $thead;
106 <BR><BR>Enter the quantity of each service this package includes.<BR><BR>
107 END
108 }
109
110 my @fixups = ();
111 my $count = 0;
112 my $columns = 3;
113 my @part_svc = qsearch( 'part_svc', { 'disabled' => '' } );
114 foreach my $part_svc ( @part_svc ) {
115   my $svcpart = $part_svc->svcpart;
116   my $pkg_svc = qsearchs( 'pkg_svc', {
117     'pkgpart'  => $cgi->param('clone') || $part_pkg->pkgpart,
118     'svcpart'  => $svcpart,
119   } ) || new FS::pkg_svc ( {
120     'pkgpart'  => $cgi->param('clone') || $part_pkg->pkgpart,
121     'svcpart'  => $svcpart,
122     'quantity' => 0,
123   });
124   #? #next unless $pkg_svc;
125
126   push @fixups, "pkg_svc$svcpart";
127
128   #unless ( defined ($cgi->param('clone')) && $cgi->param('clone') ) {
129   #dunno why...
130   unless ( 0 ) {
131     print '<TR>'; # if $count == 0 ;
132     print qq!<TD><INPUT TYPE="text" NAME="pkg_svc$svcpart" SIZE=4 MAXLENGTH=3 VALUE="!,
133           $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0,
134           qq!"></TD><TD><A HREF="part_svc.cgi?!,$part_svc->svcpart,
135           qq!">!, $part_svc->getfield('svc'), "</A></TD></TR>";
136 #    print "</TABLE></TD><TD>$thead" if ++$count == int(scalar(@part_svc) / 2);
137     $count+=1;
138     foreach ( 1 .. $columns-1 ) {
139       print "</TABLE></TD><TD VALIGN=\"top\">$thead"
140         if $count == int( $_ * scalar(@part_svc) / $columns );
141     }
142   } else {
143     print qq!<INPUT TYPE="hidden" NAME="pkg_svc$svcpart" VALUE="!,
144           $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0, qq!">\n!;
145   }
146 }
147
148 #unless ( $cgi->param('clone') ) {
149 #dunno why...
150 unless ( 0 ) {
151   print "</TR></TABLE></TD></TR></TABLE>";
152   #print "</TR></TABLE>";
153 }
154
155 foreach my $f ( qw( clone pkgnum ) ) {
156   print qq!<INPUT TYPE="hidden" NAME="$f" VALUE="!. $cgi->param($f). '">';
157 }
158 print '<INPUT TYPE="hidden" NAME="pkgpart" VALUE="'. $part_pkg->pkgpart. '">';
159
160 # prolly should be in database
161 tie my %plans, 'Tie::IxHash',
162   'flat' => {
163     'name' => 'Flat rate (anniversary billing)',
164     'fields' => {
165       'setup_fee' => { 'name' => 'Setup fee for this package',
166                        'default' => 0,
167                      },
168       'recur_fee' => { 'name' => 'Recurring fee for this package',
169                        'default' => 0,
170                       },
171     },
172     'fieldorder' => [ 'setup_fee', 'recur_fee' ],
173     'setup' => 'what.setup_fee.value',
174     'recur' => 'what.recur_fee.value',
175   },
176
177   'prorate' => {
178     'name' => 'First partial month pro-rated, then flat-rate (1st of month billing)',
179     'fields' =>  {
180       'setup_fee' => { 'name' => 'Setup fee for this package',
181                        'default' => 0,
182                      },
183       'recur_fee' => { 'name' => 'Recurring fee for this package',
184                        'default' => 0,
185                       },
186     },
187     'fieldorder' => [ 'setup_fee', 'recur_fee' ],
188     'setup' => 'what.setup_fee.value',
189     '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) ; \'',
190   },
191
192   'subscription' => {
193     'name' => 'First partial month full charge, then flat-rate (1st of month billing)',
194     'fields' => {
195       'setup_fee' => { 'name' => 'Setup fee for this package',
196                        'default' => 0,
197                      },
198       'recur_fee' => { 'name' => 'Recurring fee for this package',
199                        'default' => 0,
200                       },
201     },
202     'fieldorder' => [ 'setup_fee', 'recur_fee' ],
203     'setup' => 'what.setup_fee.value',
204     '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',
205   },
206
207   'flat_comission_cust' => {
208     'name' => 'Flat rate with recurring comission per active customer',
209     'fields' => {
210       'setup_fee' => { 'name' => 'Setup fee for this package',
211                        'default' => 0,
212                      },
213       'recur_fee' => { 'name' => 'Recurring fee for this package',
214                        'default' => 0,
215                      },
216       'comission_amount' => { 'name' => 'Comission amount per month (per active customer)',
217                               'default' => 0,
218                             },
219       'comission_depth'  => { 'name' => 'Number of layers',
220                               'default' => 1,
221                             },
222     },
223     'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
224     'setup' => 'what.setup_fee.value',
225     '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 + \';\'',
226   },
227
228   'flat_comission' => {
229     'name' => 'Flat rate with recurring comission per (any) active package',
230     'fields' => {
231       'setup_fee' => { 'name' => 'Setup fee for this package',
232                        'default' => 0,
233                      },
234       'recur_fee' => { 'name' => 'Recurring fee for this package',
235                        'default' => 0,
236                      },
237       'comission_amount' => { 'name' => 'Comission amount per month (per active package)',
238                               'default' => 0,
239                             },
240       'comission_depth'  => { 'name' => 'Number of layers',
241                               'default' => 1,
242                             },
243     },
244     'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
245     'setup' => 'what.setup_fee.value',
246     '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 + \';\'',
247   },
248
249   'flat_comission_pkg' => {
250     'name' => 'Flat rate with recurring comission per (selected) active package',
251     'fields' => {
252       'setup_fee' => { 'name' => 'Setup fee for this package',
253                        'default' => 0,
254                      },
255       'recur_fee' => { 'name' => 'Recurring fee for this package',
256                        'default' => 0,
257                      },
258       'comission_amount' => { 'name' => 'Comission amount per month (per uncancelled package)',
259                               'default' => 0,
260                             },
261       'comission_depth'  => { 'name' => 'Number of layers',
262                               'default' => 1,
263                             },
264       'comission_pkgpart' => { 'name' => 'Applicable packages<BR><FONT SIZE="-1">(hold <b>ctrl</b> to select multiple packages)</FONT>',
265                                'type' => 'select_multiple',
266                                'select_table' => 'part_pkg',
267                                'select_hash'  => { 'disabled' => '' } ,
268                                'select_key'   => 'pkgpart',
269                                'select_label' => 'pkg',
270                              },
271     },
272     'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount', 'comission_pkgpart' ],
273     'setup' => 'what.setup_fee.value',
274     '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 + \';\'',
275   },
276
277
278
279   'sesmon_hour' => {
280     'name' => 'Base charge plus charge per-hour from the session monitor',
281     'fields' => {
282       'setup_fee' => { 'name' => 'Setup fee for this package',
283                        'default' => 0,
284                      },
285       'recur_flat' => { 'name' => 'Base monthly charge for this package',
286                         'default' => 0,
287                       },
288       'recur_included_hours' => { 'name' => 'Hours included',
289                                   'default' => 0,
290                                 },
291       'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
292                                  'default' => 0,
293                                },
294     },
295     'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_hours', 'recur_hourly_charge' ],
296     'setup' => 'what.setup_fee.value',
297     '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;\'',
298   },
299
300   'sesmon_minute' => {
301     'name' => 'Base charge plus charge per-minute from the session monitor',
302     'fields' => {
303       'setup_fee' => { 'name' => 'Setup fee for this package',
304                        'default' => 0,
305                      },
306       'recur_flat' => { 'name' => 'Base monthly charge for this package',
307                         'default' => 0,
308                       },
309       'recur_included_min' => { 'name' => 'Minutes included',
310                                 'default' => 0,
311                                 },
312       'recur_minly_charge' => { 'name' => 'Additional charge per minute',
313                                 'default' => 0,
314                               },
315     },
316     'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_min', 'recur_minly_charge' ],
317     'setup' => 'what.setup_fee.value',
318     '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;\'',
319
320   },
321
322 ;
323
324 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
325                     split("\n", $part_pkg->plandata );
326
327 tie my %options, 'Tie::IxHash', map { $_=>$plans{$_}->{'name'} } keys %plans;
328
329 my $widget = new HTML::Widgets::SelectLayers(
330   'selected_layer' => $part_pkg->plan,
331   'options'        => \%options,
332   'form_name'      => 'dummy',
333   'form_action'    => 'process/part_pkg.cgi',
334   'form_text'      => [ qw(pkg comment freq clone pkgnum pkgpart), @fixups ],
335   'form_checkbox'  => [ qw(setuptax recurtax disabled) ],
336   'fixup_callback' => sub {
337                         #my $ = @_;
338                         my $html = '';
339                         for my $p ( keys %plans ) {
340                           $html .= "if ( what.plan.value == \"$p\" ) {
341                                       what.setup.value = $plans{$p}->{setup} ;
342                                       what.recur.value = $plans{$p}->{recur} ;
343                                     }\n";
344                         }
345                         $html;
346                       },
347   'layer_callback' => sub {
348     my $layer = shift;
349     my $html = qq!<INPUT TYPE="hidden" NAME="plan" VALUE="$layer">!.
350                ntable("#cccccc",2);
351     my $href = $plans{$layer}->{'fields'};
352     foreach my $field ( exists($plans{$layer}->{'fieldorder'})
353                           ? @{$plans{$layer}->{'fieldorder'}}
354                           : keys %{ $href }
355                       ) {
356
357       $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
358
359       if ( ! exists($href->{$field}{'type'}) ) {
360         $html .= qq!<INPUT TYPE="text" NAME="$field" VALUE="!.
361                  ( exists($plandata{$field})
362                      ? $plandata{$field}
363                      : $href->{$field}{'default'} ).
364                  qq!" onChange="fchanged(this)">!;
365       } elsif ( $href->{$field}{'type'} eq 'select_multiple' ) {
366         $html .= qq!<SELECT MULTIPLE NAME="$field" onChange="fchanged(this)">!;
367         foreach my $record (
368           qsearch( $href->{$field}{'select_table'},
369                    $href->{$field}{'select_hash'}   )
370         ) {
371           my $value = $record->getfield($href->{$field}{'select_key'});
372           $html .= qq!<OPTION VALUE="$value"!.
373                    (  $plandata{$field} =~ /(^|, *)$value *(,|$)/
374                         ? ' SELECTED'
375                         : ''          ).
376                    '>'. $record->getfield($href->{$field}{'select_label'})
377         }
378         $html .= '</SELECT>';
379       }
380
381       $html .= '</TD></TR>';
382     }
383     $html .= '</TABLE>';
384
385     $html .= '<INPUT TYPE="hidden" NAME="plandata" VALUE="'.
386              join(',', keys %{ $href } ). '">'.
387              '<BR><BR>';
388              
389     $html .= '<INPUT TYPE="submit" VALUE="'.
390              ( $hashref->{pkgpart} ? "Apply changes" : "Add package" ).
391              '" onClick="fchanged(this)">';
392
393     $html .= '<BR><BR>don\'t edit this unless you know what you\'re doing '.
394              '<INPUT TYPE="button" VALUE="refresh expressions" '.
395                'onClick="fchanged(this)">'.
396              ntable("#cccccc",2).
397              '<TR><TD>'.
398              '<FONT SIZE="1">Setup expression<BR>'.
399              '<INPUT TYPE="text" NAME="setup" SIZE="160" VALUE="'.
400                $hashref->{setup}. '" onLoad="fchanged(this)">'.
401              '</FONT><BR>'.
402              '<FONT SIZE="1">Recurring espression<BR>'.
403              '<INPUT TYPE="text" NAME="recur" SIZE="160" VALUE="'.
404                $hashref->{recur}. '" onLoad="fchanged(this)">'.
405              '</FONT>'.
406              '</TR></TD>'.
407              '</TABLE>';
408
409     $html;
410
411   },
412 );
413
414 %>
415
416 <BR>
417 Price plan <%= $widget->html %>
418   </BODY>
419 </HTML>