5 use FS::Record qw( qsearch dbh dbdef );
13 @ISA = qw( FS::Record );
17 FS::part_pkg - Object methods for part_pkg objects
23 $record = new FS::part_pkg \%hash
24 $record = new FS::part_pkg { 'column' => 'value' };
26 $custom_record = $template_record->clone;
28 $error = $record->insert;
30 $error = $new_record->replace($old_record);
32 $error = $record->delete;
34 $error = $record->check;
36 @pkg_svc = $record->pkg_svc;
38 $svcnum = $record->svcpart;
39 $svcnum = $record->svcpart( 'svc_acct' );
43 An FS::part_pkg object represents a billing item definition. FS::part_pkg
44 inherits from FS::Record. The following fields are currently supported:
48 =item pkgpart - primary key (assigned automatically for new billing item definitions)
50 =item pkg - Text name of this billing item definition (customer-viewable)
52 =item comment - Text name of this billing item definition (non-customer-viewable)
54 =item setup - Setup fee expression
56 =item freq - Frequency of recurring fee
58 =item recur - Recurring fee expression
60 =item setuptax - Setup fee tax exempt flag, empty or `Y'
62 =item recurtax - Recurring fee tax exempt flag, empty or `Y'
64 =item taxclass - Tax class flag
66 =item plan - Price plan
68 =item plandata - Price plan data
70 =item disabled - Disabled flag, empty or `Y'
74 setup and recur are evaluated as Safe perl expressions. You can use numbers
75 just as you would normally. More advanced semantics are not yet defined.
83 Creates a new billing item definition. To add the billing item definition to
84 the database, see L<"insert">.
88 sub table { 'part_pkg'; }
92 An alternate constructor. Creates a new billing item definition by duplicating
93 an existing definition. A new pkgpart is assigned and `(CUSTOM) ' is prepended
94 to the comment field. To add the billing item definition to the database, see
101 my $class = ref($self);
102 my %hash = $self->hash;
103 $hash{'pkgpart'} = '';
104 $hash{'comment'} = "(CUSTOM) ". $hash{'comment'}
105 unless $hash{'comment'} =~ /^\(CUSTOM\) /;
106 #new FS::part_pkg ( \%hash ); # ?
107 new $class ( \%hash ); # ?
110 =item insert [ , OPTION => VALUE ... ]
112 Adds this billing item definition to the database. If there is an error,
113 returns the error, otherwise returns false.
115 Currently available options are: I<pkg_svc>, I<primary_svc>, I<cust_pkg> and
118 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
119 values, appropriate FS::pkg_svc records will be inserted.
121 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
122 FS::pkg_svc record will be updated.
124 If I<cust_pkg> is set to a pkgnum of a FS::cust_pkg record (or the FS::cust_pkg
125 record itself), the object will be updated to point to this package definition.
127 In conjunction with I<cust_pkg>, if I<custnum_ref> is set to a scalar reference,
128 the scalar will be updated with the custnum value from the cust_pkg record.
135 warn "FS::part_pkg::insert called on $self with options %options" if $DEBUG;
137 local $SIG{HUP} = 'IGNORE';
138 local $SIG{INT} = 'IGNORE';
139 local $SIG{QUIT} = 'IGNORE';
140 local $SIG{TERM} = 'IGNORE';
141 local $SIG{TSTP} = 'IGNORE';
142 local $SIG{PIPE} = 'IGNORE';
144 my $oldAutoCommit = $FS::UID::AutoCommit;
145 local $FS::UID::AutoCommit = 0;
148 my $error = $self->SUPER::insert;
150 $dbh->rollback if $oldAutoCommit;
154 my $conf = new FS::Conf;
156 if ( $conf->exists('agent_defaultpkg') ) {
157 foreach my $agent_type ( qsearch('agent_type', {} ) ) {
158 my $type_pkgs = new FS::type_pkgs({
159 'typenum' => $agent_type->typenum,
160 'pkgpart' => $self->pkgpart,
162 my $error = $type_pkgs->insert;
164 $dbh->rollback if $oldAutoCommit;
170 warn " inserting pkg_svc records" if $DEBUG;
171 my $pkg_svc = $options{'pkg_svc'} || {};
172 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
173 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
174 my $primary_svc = $options{'primary_svc'} == $part_svc->svcpart ? 'Y' : '';
176 my $pkg_svc = new FS::pkg_svc( {
177 'pkgpart' => $self->pkgpart,
178 'svcpart' => $part_svc->svcpart,
179 'quantity' => $quantity,
180 'primary_svc' => $primary_svc,
182 my $error = $pkg_svc->insert;
184 $dbh->rollback if $oldAutoCommit;
189 if ( $options{'cust_pkg'} ) {
190 warn " updating cust_pkg record " if $DEBUG;
192 ref($options{'cust_pkg'})
193 ? $options{'cust_pkg'}
194 : qsearchs('cust_pkg', { pkgnum => $options{'cust_pkg'} } );
195 ${ $options{'custnum_ref'} } = $old_cust_pkg->custnum
196 if $options{'custnum_ref'};
197 my %hash = $old_cust_pkg->hash;
198 $hash{'pkgpart'} = $self->pkgpart,
199 my $new_cust_pkg = new FS::cust_pkg \%hash;
200 local($FS::cust_pkg::disable_agentcheck) = 1;
201 my $error = $new_cust_pkg->replace($old_cust_pkg);
203 $dbh->rollback if $oldAutoCommit;
204 return "Error modifying cust_pkg record: $error";
208 warn " commiting transaction" if $DEBUG;
209 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
216 Currently unimplemented.
221 return "Can't (yet?) delete package definitions.";
222 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
225 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
227 Replaces OLD_RECORD with this one in the database. If there is an error,
228 returns the error, otherwise returns false.
230 Currently available options are: I<pkg_svc> and I<primary_svc>
232 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
233 values, the appropriate FS::pkg_svc records will be replace.
235 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
236 FS::pkg_svc record will be updated.
241 my( $new, $old ) = ( shift, shift );
243 warn "FS::part_pkg::replace called on $new to replace $old ".
244 "with options %options"
247 local $SIG{HUP} = 'IGNORE';
248 local $SIG{INT} = 'IGNORE';
249 local $SIG{QUIT} = 'IGNORE';
250 local $SIG{TERM} = 'IGNORE';
251 local $SIG{TSTP} = 'IGNORE';
252 local $SIG{PIPE} = 'IGNORE';
254 my $oldAutoCommit = $FS::UID::AutoCommit;
255 local $FS::UID::AutoCommit = 0;
258 warn " replacing part_pkg record" if $DEBUG;
259 my $error = $new->SUPER::replace($old);
261 $dbh->rollback if $oldAutoCommit;
265 warn " replacing pkg_svc records" if $DEBUG;
266 my $pkg_svc = $options{'pkg_svc'} || {};
267 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
268 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
269 my $primary_svc = $options{'primary_svc'} == $part_svc->svcpart ? 'Y' : '';
271 my $old_pkg_svc = qsearchs('pkg_svc', {
272 'pkgpart' => $old->pkgpart,
273 'svcpart' => $part_svc->svcpart,
275 my $old_quantity = $old_pkg_svc ? $old_pkg_svc->quantity : 0;
276 my $old_primary_svc =
277 ( $old_pkg_svc && $old_pkg_svc->dbdef_table->column('primary_svc') )
278 ? $old_pkg_svc->primary_svc
280 next unless $old_quantity != $quantity || $old_primary_svc ne $primary_svc;
282 my $new_pkg_svc = new FS::pkg_svc( {
283 'pkgpart' => $new->pkgpart,
284 'svcpart' => $part_svc->svcpart,
285 'quantity' => $quantity,
286 'primary_svc' => $primary_svc,
288 my $error = $old_pkg_svc
289 ? $new_pkg_svc->replace($old_pkg_svc)
290 : $new_pkg_svc->insert;
292 $dbh->rollback if $oldAutoCommit;
297 warn " commiting transaction" if $DEBUG;
298 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
304 Checks all fields to make sure this is a valid billing item definition. If
305 there is an error, returns the error, otherwise returns false. Called by the
306 insert and replace methods.
313 for (qw(setup recur)) { $self->set($_=>0) if $self->get($_) =~ /^\s*$/; }
315 my $conf = new FS::Conf;
316 if ( $conf->exists('safe-part_pkg') ) {
318 my $error = $self->ut_anything('setup')
319 || $self->ut_anything('recur');
320 return $error if $error;
322 my $s = $self->setup;
324 $s =~ /^\s*\d*\.?\d*\s*$/
326 or $s =~ /^my \$d = \$cust_pkg->bill || \$time; \$d += 86400 \* \s*\d+\s*; \$cust_pkg->bill\(\$d\); \$cust_pkg_mod_flag=1; \s*\d*\.?\d*\s*$/
330 return "illegal setup: $s";
333 my $r = $self->recur;
335 $r =~ /^\s*\d*\.?\d*\s*$/
337 #or $r =~ /^\$sdate += 86400 \* \s*\d+\s*; \s*\d*\.?\d*\s*$/
339 or $r =~ /^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 \) \* \d*\.?\d* \/ \$part_pkg\-\>freq \+ \d*\.?\d* \/ \$part_pkg\-\>freq \* \(\$mend\-\$mnow\) \/ \(\$mend\-\$mstart\) ;\s*$/
341 or $r =~ /^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\); \s*\d*\.?\d*\s*;\s*$/
343 or $r =~ /^my \$error = \$cust_pkg\->cust_main\->credit\( \s*\d*\.?\d*\s* \* scalar\(\$cust_pkg\->cust_main\->referral_cust_main_ncancelled\(\s*\d+\s*\)\), "commission" \); die \$error if \$error; \s*\d*\.?\d*\s*;\s*$/
345 or $r =~ /^my \$error = \$cust_pkg\->cust_main\->credit\( \s*\d*\.?\d*\s* \* scalar\(\$cust_pkg\->cust_main->referral_cust_pkg\(\s*\d+\s*\)\), "commission" \); die \$error if \$error; \s*\d*\.?\d*\s*;\s*$/
347 or $r =~ /^my \$error = \$cust_pkg\->cust_main\->credit\( \s*\d*\.?\d*\s* \* scalar\( grep \{ my \$pkgpart = \$_\->pkgpart; grep \{ \$_ == \$pkgpart \} \(\s*(\s*\d+,\s*)*\s*\) \} \$cust_pkg\->cust_main->referral_cust_pkg\(\s*\d+\s*\)\), "commission" \); die \$error if \$error; \s*\d*\.?\d*\s*;\s*$/
349 or $r =~ /^my \$hours = \$cust_pkg\->seconds_since\(\$cust_pkg\->bill \|\| 0\) \/ 3600 \- \s*\d*\.?\d*\s*; \$hours = 0 if \$hours < 0; \s*\d*\.?\d*\s* \+ \s*\d*\.?\d*\s* \* \$hours;\s*$/
351 or $r =~ /^my \$min = \$cust_pkg\->seconds_since\(\$cust_pkg\->bill \|\| 0\) \/ 60 \- \s*\d*\.?\d*\s*; \$min = 0 if \$min < 0; \s*\d*\.?\d*\s* \+ \s*\d*\.?\d*\s* \* \$min;\s*$/
353 or $r =~ /^my \$last_bill = \$cust_pkg\->last_bill; my \$hours = \$cust_pkg\->seconds_since_sqlradacct\(\$last_bill, \$sdate \) \/ 3600 - \s*\d\.?\d*\s*; \$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 \- \s*\d\.?\d*\s*; \$total = 0 if \$total < 0; my \$input = \$input - \s*\d\.?\d*\s*; \$input = 0 if \$input < 0; my \$output = \$output - \s*\d\.?\d*\s*; \$output = 0 if \$output < 0; \s*\d\.?\d*\s* \+ \s*\d\.?\d*\s* \* \$hours \+ \s*\d\.?\d*\s* \* \$input \+ \s*\d\.?\d*\s* \* \$output \+ \s*\d\.?\d*\s* \* \$total *;\s*$/
357 return "illegal recur: $r";
362 if ( $self->dbdef_table->column('freq')->type =~ /(int)/i ) {
363 my $error = $self->ut_number('freq');
364 return $error if $error;
366 $self->freq =~ /^(\d+[dw]?)$/
367 or return "Illegal or empty freq: ". $self->freq;
371 $self->ut_numbern('pkgpart')
372 || $self->ut_text('pkg')
373 || $self->ut_text('comment')
374 || $self->ut_anything('setup')
375 || $self->ut_anything('recur')
376 || $self->ut_alphan('plan')
377 || $self->ut_anything('plandata')
378 || $self->ut_enum('setuptax', [ '', 'Y' ] )
379 || $self->ut_enum('recurtax', [ '', 'Y' ] )
380 || $self->ut_textn('taxclass')
381 || $self->ut_enum('disabled', [ '', 'Y' ] )
387 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
388 definition (with non-zero quantity).
394 grep { $_->quantity } qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
397 =item svcpart [ SVCDB ]
399 Returns the svcpart of the primary service definition (see L<FS::part_svc>)
400 associated with this billing item definition (see L<FS::pkg_svc>). Returns
401 false if there not a primary service definition or exactly one service
402 definition with quantity 1, or if SVCDB is specified and does not match the
403 svcdb of the service definition,
409 my $svcdb = scalar(@_) ? shift : '';
411 grep { ( $svcdb eq $_->part_svc->svcdb || !$svcdb ) } $self->pkg_svc;
413 @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc
414 if dbdef->table('pkg_svc')->column('primary_svc');
415 @pkg_svc = grep {$_->quantity == 1 } @svcdb_pkg_svc
417 return '' if scalar(@pkg_svc) != 1;
418 $pkg_svc[0]->svcpart;
423 Returns a list of the acceptable payment types for this package. Eventually
424 this should come out of a database table and be editable, but currently has the
425 following logic instead;
427 If the package has B<0> setup and B<0> recur, the single item B<BILL> is
428 returned, otherwise, the single item B<CARD> is returned.
430 (CHEK? LEC? Probably shouldn't accept those by default, prone to abuse)
436 #if ( $self->setup == 0 && $self->recur == 0 ) {
437 if ( $self->setup =~ /^\s*0+(\.0*)?\s*$/
438 && $self->recur =~ /^\s*0+(\.0*)?\s*$/ ) {
449 The delete method is unimplemented.
451 setup and recur semantics are not yet defined (and are implemented in
452 FS::cust_bill. hmm.).
456 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
457 schema.html from the base documentation.