document trouble schema changes backported to 1.4.2
[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 #false laziness w/view/cust_main.cgi
70 my %freq;
71 tie %freq, 'Tie::IxHash', 
72   '0'  => '(no recurring fee)',
73   '1d' => 'daily',
74   '1w' => 'weekly',
75   '2w' => 'biweekly (every 2 weeks)',
76   '1'  => 'monthly',
77   '2'  => 'bimonthly (every 2 months)',
78   '3'  => 'quarterly (every 3 months)',
79   '6'  => 'semiannually (every 6 months)',
80   '12' => 'annually',
81   '24' => 'biannually (every 2 years)',
82 ;
83 if ( $part_pkg->dbdef_table->column('freq')->type =~ /(int)/i ) {
84   delete $freq{$_} foreach grep { ! /^\d+$/ } keys %freq;
85 }
86
87 %>
88 <%= ntable("#cccccc",2) %>
89   <TR>
90     <TD ALIGN="right">Package (customer-visible)</TD>
91     <TD>
92       <INPUT TYPE="text" NAME="pkg" SIZE=32 VALUE="<%= $part_pkg->pkg %>">
93     </TD>
94   </TR>
95   <TR>
96     <TD ALIGN="right">Comment (customer-hidden)</TD>
97     <TD>
98       <INPUT TYPE="text" NAME="comment" SIZE=32 VALUE="<%=$part_pkg->comment%>">
99     </TD>
100   </TR>
101   <TR>
102     <TD ALIGN="right">Recurring fee frequency </TD>
103     <TD>
104       <SELECT NAME="freq">
105         <% foreach my $freq ( keys %freq ) { %>
106           <OPTION VALUE="<%= $freq %>"<%= $freq eq $part_pkg->freq ? ' SELECTED' : '' %>><%= $freq{$freq} %>
107         <% } %>
108       </SELECT>
109     </TD>
110   </TR>
111   <TR>
112     <TD ALIGN="right">Setup fee tax exempt</TD>
113     <TD>
114 <%
115
116 print '<INPUT TYPE="checkbox" NAME="setuptax" VALUE="Y"';
117 print ' CHECKED' if $hashref->{setuptax} eq "Y";
118 print '>';
119
120 print <<END;
121 </TD></TR>
122 <TR><TD ALIGN="right">Recurring fee tax exempt</TD><TD>
123 END
124
125 print '<INPUT TYPE="checkbox" NAME="recurtax" VALUE="Y"';
126 print ' CHECKED' if $hashref->{recurtax} eq "Y";
127 print '>';
128
129 print '</TD></TR>';
130
131 my $conf = new FS::Conf;
132 #false laziness w/ view/cust_main.cgi quick order
133 if ( $conf->exists('enable_taxclasses') ) {
134   print '<TR><TD ALIGN="right">Tax class</TD><TD><SELECT NAME="taxclass">';
135   my $sth = dbh->prepare('SELECT DISTINCT taxclass FROM cust_main_county')
136     or die dbh->errstr;
137   $sth->execute or die $sth->errstr;
138   foreach my $taxclass ( map $_->[0], @{$sth->fetchall_arrayref} ) {
139     print qq!<OPTION VALUE="$taxclass"!;
140     print ' SELECTED' if $taxclass eq $hashref->{taxclass};
141     print qq!>$taxclass</OPTION>!;
142   }
143   print '</SELECT></TD></TR>';
144 } else {
145   print
146     '<INPUT TYPE="hidden" NAME="taxclass" VALUE="'. $hashref->{taxclass}. '">';
147 }
148
149 print '<TR><TD ALIGN="right">Disable new orders</TD><TD>';
150 print '<INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"';
151 print ' CHECKED' if $hashref->{disabled} eq "Y";
152 print '>';
153 print '</TD></TR></TABLE>';
154
155 my $thead =  "\n\n". ntable('#cccccc', 2).
156              '<TR><TH BGCOLOR="#dcdcdc"><FONT SIZE=-1>Quan.</FONT></TH>';
157 $thead .=  '<TH BGCOLOR="#dcdcdc"><FONT SIZE=-1>Primary</FONT></TH>'
158   if dbdef->table('pkg_svc')->column('primary_svc');
159 $thead .= '<TH BGCOLOR="#dcdcdc">Service</TH></TR>';
160
161 #unless ( $cgi->param('clone') ) {
162 #dunno why...
163 unless ( 0 ) {
164   #print <<END, $thead;
165   print <<END, itable(), '<TR><TD VALIGN="top">', $thead;
166 <BR><BR>Enter the quantity of each service this package includes.<BR><BR>
167 END
168 }
169
170 my @fixups = ();
171 my $count = 0;
172 my $columns = 3;
173 my @part_svc = qsearch( 'part_svc', { 'disabled' => '' } );
174 foreach my $part_svc ( @part_svc ) {
175   my $svcpart = $part_svc->svcpart;
176   my $pkgpart = $cgi->param('clone') || $part_pkg->pkgpart;
177   my $pkg_svc = $pkgpart && qsearchs( 'pkg_svc', {
178     'pkgpart'  => $pkgpart,
179     'svcpart'  => $svcpart,
180   } ) || new FS::pkg_svc ( {
181     'pkgpart'     => $pkgpart,
182     'svcpart'     => $svcpart,
183     'quantity'    => 0,
184     'primary_svc' => '',
185   });
186   #? #next unless $pkg_svc;
187
188   push @fixups, "pkg_svc$svcpart";
189
190   #unless ( defined ($cgi->param('clone')) && $cgi->param('clone') ) {
191   #dunno why...
192   unless ( 0 ) {
193     print '<TR>'; # if $count == 0 ;
194     print qq!<TD><INPUT TYPE="text" NAME="pkg_svc$svcpart" SIZE=4 MAXLENGTH=3 VALUE="!,
195           $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0,
196           qq!"></TD>!;
197     if ( dbdef->table('pkg_svc')->column('primary_svc') ) {
198       print qq!<TD><INPUT TYPE="radio" NAME="pkg_svc_primary" VALUE="$svcpart"!;
199       print ' CHECKED' if $pkg_svc->primary_svc =~ /^Y/i;
200       print '></TD>';
201     }
202     print qq!<TD><A HREF="part_svc.cgi?!,$part_svc->svcpart,
203           qq!">!, $part_svc->getfield('svc'), "</A></TD></TR>";
204 #    print "</TABLE></TD><TD>$thead" if ++$count == int(scalar(@part_svc) / 2);
205     $count+=1;
206     foreach ( 1 .. $columns-1 ) {
207       print "</TABLE></TD><TD VALIGN=\"top\">$thead"
208         if $count == int( $_ * scalar(@part_svc) / $columns );
209     }
210   } else {
211     print qq!<INPUT TYPE="hidden" NAME="pkg_svc$svcpart" VALUE="!,
212           $cgi->param("pkg_svc$svcpart") || $pkg_svc->quantity || 0, qq!">\n!;
213   }
214 }
215
216 #unless ( $cgi->param('clone') ) {
217 #dunno why...
218 unless ( 0 ) {
219   print "</TR></TABLE></TD></TR></TABLE>";
220   #print "</TR></TABLE>";
221 }
222
223 foreach my $f ( qw( clone pkgnum ) ) {
224   print qq!<INPUT TYPE="hidden" NAME="$f" VALUE="!. $cgi->param($f). '">';
225 }
226 print '<INPUT TYPE="hidden" NAME="pkgpart" VALUE="'. $part_pkg->pkgpart. '">';
227
228 # prolly should be in database
229 tie my %plans, 'Tie::IxHash',
230   'flat' => {
231     'name' => 'Flat rate (anniversary billing)',
232     'fields' => {
233       'setup_fee' => { 'name' => 'Setup fee for this package',
234                        'default' => 0,
235                      },
236       'recur_fee' => { 'name' => 'Recurring fee for this package',
237                        'default' => 0,
238                       },
239     },
240     'fieldorder' => [ 'setup_fee', 'recur_fee' ],
241     'setup' => 'what.setup_fee.value',
242     'recur' => 'what.recur_fee.value',
243   },
244
245   'flat_delayed' => {
246     'name' => 'Free for X days, then flat rate (anniversary billing)',
247     'fields' =>  {
248       'free_days' => { 'name' => 'Initial free days',
249                        'default' => 0,
250                      },
251       'setup_fee' => { 'name' => 'Setup fee for this package',
252                        'default' => 0,
253                      },
254       'recur_fee' => { 'name' => 'Recurring fee for this package',
255                        'default' => 0,
256                       },
257     },
258     'fieldorder' => [ 'free_days', 'setup_fee', 'recur_fee' ],
259     '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',
260     'recur' => 'what.recur_fee.value',
261   },
262
263   'prorate' => {
264     'name' => 'First partial month pro-rated, then flat-rate (1st of month billing)',
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     },
273     'fieldorder' => [ 'setup_fee', 'recur_fee' ],
274     'setup' => 'what.setup_fee.value',
275     '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) ; \'',
276   },
277
278   'subscription' => {
279     'name' => 'First partial month full charge, then flat-rate (1st of month billing)',
280     'fields' => {
281       'setup_fee' => { 'name' => 'Setup fee for this package',
282                        'default' => 0,
283                      },
284       'recur_fee' => { 'name' => 'Recurring fee for this package',
285                        'default' => 0,
286                       },
287     },
288     'fieldorder' => [ 'setup_fee', 'recur_fee' ],
289     'setup' => 'what.setup_fee.value',
290     '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',
291   },
292
293   'flat_comission_cust' => {
294     'name' => 'Flat rate with recurring commission per active customer',
295     'fields' => {
296       'setup_fee' => { 'name' => 'Setup fee for this package',
297                        'default' => 0,
298                      },
299       'recur_fee' => { 'name' => 'Recurring fee for this package',
300                        'default' => 0,
301                      },
302       'comission_amount' => { 'name' => 'Commission amount per month (per active customer)',
303                               'default' => 0,
304                             },
305       'comission_depth'  => { 'name' => 'Number of layers',
306                               'default' => 1,
307                             },
308     },
309     'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
310     'setup' => 'what.setup_fee.value',
311     '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 + \';\'',
312   },
313
314   'flat_comission' => {
315     'name' => 'Flat rate with recurring commission per (any) active package',
316     'fields' => {
317       'setup_fee' => { 'name' => 'Setup fee for this package',
318                        'default' => 0,
319                      },
320       'recur_fee' => { 'name' => 'Recurring fee for this package',
321                        'default' => 0,
322                      },
323       'comission_amount' => { 'name' => 'Commission amount per month (per active package)',
324                               'default' => 0,
325                             },
326       'comission_depth'  => { 'name' => 'Number of layers',
327                               'default' => 1,
328                             },
329     },
330     'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount' ],
331     'setup' => 'what.setup_fee.value',
332     '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 + \';\'',
333   },
334
335   'flat_comission_pkg' => {
336     'name' => 'Flat rate with recurring commission per (selected) active package',
337     'fields' => {
338       'setup_fee' => { 'name' => 'Setup fee for this package',
339                        'default' => 0,
340                      },
341       'recur_fee' => { 'name' => 'Recurring fee for this package',
342                        'default' => 0,
343                      },
344       'comission_amount' => { 'name' => 'Commission amount per month (per uncancelled package)',
345                               'default' => 0,
346                             },
347       'comission_depth'  => { 'name' => 'Number of layers',
348                               'default' => 1,
349                             },
350       'comission_pkgpart' => { 'name' => 'Applicable packages<BR><FONT SIZE="-1">(hold <b>ctrl</b> to select multiple packages)</FONT>',
351                                'type' => 'select_multiple',
352                                'select_table' => 'part_pkg',
353                                'select_hash'  => { 'disabled' => '' } ,
354                                'select_key'   => 'pkgpart',
355                                'select_label' => 'pkg',
356                              },
357     },
358     'fieldorder' => [ 'setup_fee', 'recur_fee', 'comission_depth', 'comission_amount', 'comission_pkgpart' ],
359     'setup' => 'what.setup_fee.value',
360     '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 + \';\'',
361   },
362
363
364
365   'sesmon_hour' => {
366     'name' => 'Base charge plus charge per-hour from the session monitor',
367     'fields' => {
368       'setup_fee' => { 'name' => 'Setup fee for this package',
369                        'default' => 0,
370                      },
371       'recur_flat' => { 'name' => 'Base monthly charge for this package',
372                         'default' => 0,
373                       },
374       'recur_included_hours' => { 'name' => 'Hours included',
375                                   'default' => 0,
376                                 },
377       'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
378                                  'default' => 0,
379                                },
380     },
381     'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_hours', 'recur_hourly_charge' ],
382     'setup' => 'what.setup_fee.value',
383     '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;\'',
384   },
385
386   'sesmon_minute' => {
387     'name' => 'Base charge plus charge per-minute from the session monitor',
388     'fields' => {
389       'setup_fee' => { 'name' => 'Setup fee for this package',
390                        'default' => 0,
391                      },
392       'recur_flat' => { 'name' => 'Base monthly charge for this package',
393                         'default' => 0,
394                       },
395       'recur_included_min' => { 'name' => 'Minutes included',
396                                 'default' => 0,
397                                 },
398       'recur_minly_charge' => { 'name' => 'Additional charge per minute',
399                                 'default' => 0,
400                               },
401     },
402     'fieldorder' => [ 'setup_fee', 'recur_flat', 'recur_included_min', 'recur_minly_charge' ],
403     'setup' => 'what.setup_fee.value',
404     '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;\'',
405
406   },
407
408   'sqlradacct_hour' => {
409     'name' => 'Base charge plus charge per-hour (and for data) from an external sqlradius radacct table',
410     'fields' => {
411       'setup_fee' => { 'name' => 'Setup fee for this package',
412                        'default' => 0,
413                      },
414       'recur_flat' => { 'name' => 'Base monthly charge for this package',
415                         'default' => 0,
416                       },
417       'recur_included_hours' => { 'name' => 'Hours included',
418                                   'default' => 0,
419                                 },
420       'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
421                                  'default' => 0,
422                                },
423       'recur_included_input' => { 'name' => 'Input megabytes included',
424                                   'default' => 0,
425                                 },
426       'recur_input_charge' => { 'name' =>
427                                         'Additional charge per input megabyte',
428                                 'default' => 0,
429                               },
430       'recur_included_output' => { 'name' => 'Output megabytes included',
431                                    'default' => 0,
432                                 },
433       'recur_output_charge' => { 'name' =>
434                                        'Additional charge per output megabyte',
435                                 'default' => 0,
436                               },
437       'recur_included_total' => { 'name' =>
438                                        'Total input+output megabytes included',
439                                   'default' => 0,
440                                 },
441       'recur_total_charge' => { 'name' =>
442                                  'Additional charge per input+output megabyte',
443                                 'default' => 0,
444                               },
445     },
446     'fieldorder' => [qw( setup_fee recur_flat recur_included_hours recur_hourly_charge recur_included_input recur_input_charge recur_included_output recur_output_charge recur_included_total recur_total_charge )],
447     'setup' => 'what.setup_fee.value',
448     'recur' => '\'my $last_bill = $cust_pkg->last_bill; my $hours = $cust_pkg->seconds_since_sqlradacct($last_bill, $sdate ) / 3600 - \' + what.recur_included_hours.value + \'; $hours = 0 if $hours < 0; my $input = $cust_pkg->attribute_since_sqlradacct($last_bill, $sdate, \"AcctInputOctets\" ) / 1048576; my $output = $cust_pkg->attribute_since_sqlradacct($last_bill, $sdate, \"AcctOutputOctets\" ) / 1048576; my $total = $input + $output - \' + what.recur_included_total.value + \'; $total = 0 if $total < 0; my $input = $input - \' + what.recur_included_input.value + \'; $input = 0 if $input < 0; my $output = $output - \' + what.recur_included_output.value + \'; $output = 0 if $output < 0; my $totalcharge = sprintf(\"%.2f\", \' + what.recur_total_charge.value + \' * $total); my $hourscharge = sprintf(\"%.2f\", \' + what.recur_hourly_charge.value + \' * $hours); push @details, \"Last month\\\'s excess data \". sprintf(\"%.1f\", $total). \" megs: \\\$$totalcharge\", \"Last month\\\'s excess time \". sprintf(\"%.1f\", $hours). \" hours: \\\$$hourscharge\"; \' + what.recur_flat.value + \' + $hourscharge + \' + what.recur_input_charge.value + \' * $input + \' + what.recur_output_charge.value + \' * $output + $totalcharge ;\'',
449   },
450
451   'sql_generic' => {
452     'name' => 'Base charge plus a metered rate from a configurable SQL query',
453     'fields' => {
454       'setup_fee' => { 'name' => 'Setup fee for this package',
455                        'default' => 0,
456                      },
457       'recur_flat' => { 'name' => 'Base monthly charge for this package',
458                         'default' => 0,
459                       },
460       'recur_included' => { 'name' => 'Units included',
461                             'default' => 0,
462                           },
463       'recur_unit_charge' => { 'name' => 'Additional charge per unit',
464                                'default' => 0,
465                              },
466       'datasrc' => { 'name' => 'DBI data source',
467                      'default' => '',
468                    },
469       'db_username' => { 'name' => 'Database username',
470                          'default' => '',
471                        },
472       'db_password' => { 'name' => 'Database username',
473                          'default' => '',
474                        },
475       'query' => { 'name' => 'SQL query',
476                    'default' => '',
477                  },
478     },
479     'fieldorder' => [qw( setup_fee recur_flat recur_included recur_unit_charge datasrc db_username db_password query )],
480     'setup' => 'what.setup_fee.value',
481    # 'recur' => '\'my $dbh = DBI->connect(\"\' + what.datasrc.value + \'\", \"\' + what.db_username.value + \'\") or die $DBI::errstr; \'',
482    'recur' => '\'my $dbh = DBI->connect(\"\' + what.datasrc.value + \'\", \"\' + what.db_username.value + \'\", \"\' + what.db_password.value + \'\" ) or die $DBI::errstr; my $sth = $dbh->prepare(\"\' + what.query.value + \'\") or die $dbh->errstr; my $units = 0; foreach my $cust_svc ( grep { $_->part_svc->svcdb eq \"svc_domain\" } $cust_pkg->cust_svc ) { my $domain = $cust_svc->svc_x->domain; $sth->execute($domain) or die $sth->errstr; $units += $sth->fetchrow_arrayref->[0]; } $units -= \' + what.recur_included.value + \'; $units = 0 if $units < 0; \' + what.recur_flat.value + \' + $units * \' + what.recur_unit_charge.value + \';\'',
483     #'recur' => '\'my $dbh = DBI->connect("\' + what.datasrc.value + \'", "\' + what.db_username.value + \'", "\' what.db_password.value + \'" ) or die $DBI::errstr; my $sth = $dbh->prepare("\' + what.query.value + \'") or die $dbh->errstr; my $units = 0; foreach my $cust_svc ( grep { $_->part_svc->svcdb eq "svc_domain" } $cust_pkg->cust_svc ) { my $domain = $cust_svc->svc_x->domain; $sth->execute($domain) or die $sth->errstr; $units += $sth->fetchrow_arrayref->[0]; } $units -= \' + what.recur_included.value + \'; $units = 0 if $units < 0; \' + what.recur_flat.value + \' + $units * \' + what.recur_unit_charge + \';\'',
484   },
485
486
487
488   'sql_external' => {
489     'name' => 'Base charge plus additional fees for external services from a configurable SQL query',
490     'fields' => {
491       'setup_fee' => { 'name' => 'Setup fee for this package',
492                        'default' => 0,
493                      },
494       'recur_flat' => { 'name' => 'Base monthly charge for this package',
495                         'default' => 0,
496                       },
497       'datasrc' => { 'name' => 'DBI data source',
498                      'default' => '',
499                    },
500       'db_username' => { 'name' => 'Database username',
501                          'default' => '',
502                        },
503       'db_password' => { 'name' => 'Database password',
504                          'default' => '',
505                        },
506       'query' => { 'name' => 'SQL query',
507                    'default' => '',
508                  },
509     },
510     'fieldorder' => [qw( setup_fee recur_flat datasrc db_username db_password query )],
511     'setup' => 'what.setup_fee.value',
512     'recur' => q!'my $dbh = DBI->connect("' + what.datasrc.value + '", "' + what.db_username.value + '", "' + what.db_password.value + '" ) or die $DBI::errstr; my $sth = $dbh->prepare("' + what.query.value + '") or die $dbh->errstr; my $price = ' + what.recur_flat.value + '; foreach my $cust_svc ( grep { $_->part_svc->svcdb eq "svc_external" } $cust_pkg->cust_svc ){ my $id = $cust_svc->svc_x->id; $sth->execute($id) or die $sth->errstr; $price += $sth->fetchrow_arrayref->[0]; } $price;'!,
513
514   },
515
516 ;
517
518 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
519                     split("\n", $part_pkg->plandata );
520
521 tie my %options, 'Tie::IxHash', map { $_=>$plans{$_}->{'name'} } keys %plans;
522
523 my @form_select = ();
524 if ( $conf->exists('enable_taxclasses') ) {
525   push @form_select, 'taxclass';
526 } else {
527   push @fixups, 'taxclass'; #hidden
528 }
529
530 my @form_radio = ();
531 if ( dbdef->table('pkg_svc')->column('primary_svc') ) {
532   push @form_radio, 'pkg_svc_primary';
533 }
534
535 my $widget = new HTML::Widgets::SelectLayers(
536   'selected_layer' => $part_pkg->plan,
537   'options'        => \%options,
538   'form_name'      => 'dummy',
539   'form_action'    => 'process/part_pkg.cgi',
540   'form_text'      => [ qw(pkg comment freq clone pkgnum pkgpart), @fixups ],
541   'form_checkbox'  => [ qw(setuptax recurtax disabled) ],
542   'form_radio'     => \@form_radio,
543   'form_select'    => \@form_select,
544   'fixup_callback' => sub {
545                         #my $ = @_;
546                         my $html = '';
547                         for my $p ( keys %plans ) {
548                           $html .= "if ( what.plan.value == \"$p\" ) {
549                                       what.setup.value = $plans{$p}->{setup} ;
550                                       what.recur.value = $plans{$p}->{recur} ;
551                                     }\n";
552                         }
553                         $html;
554                       },
555   'layer_callback' => sub {
556     my $layer = shift;
557     my $html = qq!<INPUT TYPE="hidden" NAME="plan" VALUE="$layer">!.
558                ntable("#cccccc",2);
559     my $href = $plans{$layer}->{'fields'};
560     foreach my $field ( exists($plans{$layer}->{'fieldorder'})
561                           ? @{$plans{$layer}->{'fieldorder'}}
562                           : keys %{ $href }
563                       ) {
564
565       $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
566
567       if ( ! exists($href->{$field}{'type'}) ) {
568         $html .= qq!<INPUT TYPE="text" NAME="$field" VALUE="!.
569                  ( exists($plandata{$field})
570                      ? $plandata{$field}
571                      : $href->{$field}{'default'} ).
572                  qq!" onChange="fchanged(this)">!;
573       } elsif ( $href->{$field}{'type'} eq 'select_multiple' ) {
574         $html .= qq!<SELECT MULTIPLE NAME="$field" onChange="fchanged(this)">!;
575         foreach my $record (
576           qsearch( $href->{$field}{'select_table'},
577                    $href->{$field}{'select_hash'}   )
578         ) {
579           my $value = $record->getfield($href->{$field}{'select_key'});
580           $html .= qq!<OPTION VALUE="$value"!.
581                    (  $plandata{$field} =~ /(^|, *)$value *(,|$)/
582                         ? ' SELECTED'
583                         : ''          ).
584                    '>'. $record->getfield($href->{$field}{'select_label'})
585         }
586         $html .= '</SELECT>';
587       }
588
589       $html .= '</TD></TR>';
590     }
591     $html .= '</TABLE>';
592
593     $html .= '<INPUT TYPE="hidden" NAME="plandata" VALUE="'.
594              join(',', keys %{ $href } ). '">'.
595              '<BR><BR>';
596              
597     $html .= '<INPUT TYPE="submit" VALUE="'.
598              ( $hashref->{pkgpart} ? "Apply changes" : "Add package" ).
599              '" onClick="fchanged(this)">';
600
601     $html .= '<BR><BR>don\'t edit this unless you know what you\'re doing '.
602              '<INPUT TYPE="button" VALUE="refresh expressions" '.
603                'onClick="fchanged(this)">'.
604              ntable("#cccccc",2).
605              '<TR><TD>'.
606              '<FONT SIZE="1">Setup expression<BR>'.
607              '<INPUT TYPE="text" NAME="setup" SIZE="160" VALUE="'.
608                encode_entities($hashref->{setup}). '" onLoad="fchanged(this)">'.
609              '</FONT><BR>'.
610              '<FONT SIZE="1">Recurring espression<BR>'.
611              '<INPUT TYPE="text" NAME="recur" SIZE="160" VALUE="'.
612                encode_entities($hashref->{recur}). '" onLoad="fchanged(this)">'.
613              '</FONT>'.
614              '</TR></TD>'.
615              '</TABLE>';
616
617     $html;
618
619   },
620 );
621
622 %>
623
624 <BR>
625 Price plan <%= $widget->html %>
626   </BODY>
627 </HTML>