5 use FS::Record qw( qsearch dbh );
11 @ISA = qw( FS::Record );
15 FS::part_pkg - Object methods for part_pkg objects
21 $record = new FS::part_pkg \%hash
22 $record = new FS::part_pkg { 'column' => 'value' };
24 $custom_record = $template_record->clone;
26 $error = $record->insert;
28 $error = $new_record->replace($old_record);
30 $error = $record->delete;
32 $error = $record->check;
34 @pkg_svc = $record->pkg_svc;
36 $svcnum = $record->svcpart;
37 $svcnum = $record->svcpart( 'svc_acct' );
41 An FS::part_pkg object represents a billing item definition. FS::part_pkg
42 inherits from FS::Record. The following fields are currently supported:
46 =item pkgpart - primary key (assigned automatically for new billing item definitions)
48 =item pkg - Text name of this billing item definition (customer-viewable)
50 =item comment - Text name of this billing item definition (non-customer-viewable)
52 =item setup - Setup fee expression
54 =item freq - Frequency of recurring fee
56 =item recur - Recurring fee expression
58 =item setuptax - Setup fee tax exempt flag, empty or `Y'
60 =item recurtax - Recurring fee tax exempt flag, empty or `Y'
62 =item taxclass - Texas tax class flag, empty or "none", "access", or "hosting"
64 =item plan - Price plan
66 =item plandata - Price plan data
68 =item disabled - Disabled flag, empty or `Y'
72 setup and recur are evaluated as Safe perl expressions. You can use numbers
73 just as you would normally. More advanced semantics are not yet defined.
81 Creates a new billing item definition. To add the billing item definition to
82 the database, see L<"insert">.
86 sub table { 'part_pkg'; }
90 An alternate constructor. Creates a new billing item definition by duplicating
91 an existing definition. A new pkgpart is assigned and `(CUSTOM) ' is prepended
92 to the comment field. To add the billing item definition to the database, see
99 my $class = ref($self);
100 my %hash = $self->hash;
101 $hash{'pkgpart'} = '';
102 $hash{'comment'} = "(CUSTOM) ". $hash{'comment'}
103 unless $hash{'comment'} =~ /^\(CUSTOM\) /;
104 #new FS::part_pkg ( \%hash ); # ?
105 new $class ( \%hash ); # ?
110 Adds this billing item definition to the database. If there is an error,
111 returns the error, otherwise returns false.
118 local $SIG{HUP} = 'IGNORE';
119 local $SIG{INT} = 'IGNORE';
120 local $SIG{QUIT} = 'IGNORE';
121 local $SIG{TERM} = 'IGNORE';
122 local $SIG{TSTP} = 'IGNORE';
123 local $SIG{PIPE} = 'IGNORE';
125 my $oldAutoCommit = $FS::UID::AutoCommit;
126 local $FS::UID::AutoCommit = 0;
129 my $error = $self->SUPER::insert;
131 $dbh->rollback if $oldAutoCommit;
135 my $conf = new FS::Conf;
137 if ( $conf->exists('agent_defaultpkg') ) {
138 foreach my $agent_type ( qsearch('agent_type', {} ) ) {
139 my $type_pkgs = new FS::type_pkgs({
140 'typenum' => $agent_type->typenum,
141 'pkgpart' => $self->pkgpart,
143 my $error = $type_pkgs->insert;
145 $dbh->rollback if $oldAutoCommit;
151 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
158 Currently unimplemented.
163 return "Can't (yet?) delete package definitions.";
164 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
167 =item replace OLD_RECORD
169 Replaces OLD_RECORD with this one in the database. If there is an error,
170 returns the error, otherwise returns false.
174 Checks all fields to make sure this is a valid billing item definition. If
175 there is an error, returns the error, otherwise returns false. Called by the
176 insert and replace methods.
183 my $conf = new FS::Conf;
184 if ( $conf->exists('safe-part_pkg') ) {
186 my $error = $self->ut_anything('setup')
187 || $self->ut_anything('recur');
188 return $error if $error;
190 my $s = $self->setup;
192 $s =~ /^\s*\d*\.?\d*\s*$/ or do {
194 return "illegal setup: $s";
197 my $r = $self->recur;
199 $r =~ /^\s*\d*\.?\d*\s*$/
201 or $r =~ /^\$sdate += 86400 \* \s*\d+\s*; \s*\d*\.?\d*\s*$/
203 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*$/
205 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*$/
207 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*$/
209 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*$/
211 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*$/
213 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*$/
215 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*$/
219 return "illegal recur: $r";
224 $self->ut_numbern('pkgpart')
225 || $self->ut_text('pkg')
226 || $self->ut_text('comment')
227 || $self->ut_anything('setup')
228 || $self->ut_number('freq')
229 || $self->ut_anything('recur')
230 || $self->ut_alphan('plan')
231 || $self->ut_anything('plandata')
232 || $self->ut_enum('setuptax', [ '', 'Y' ] )
233 || $self->ut_enum('recurtax', [ '', 'Y' ] )
234 || $self->ut_enum('texastax', [ '', 'none', 'access', 'hosting' ] )
235 || $self->ut_enum('disabled', [ '', 'Y' ] )
241 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
242 definition (with non-zero quantity).
248 grep { $_->quantity } qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
251 =item svcpart [ SVCDB ]
253 Returns the svcpart of a single service definition (see L<FS::part_svc>)
254 associated with this billing item definition (see L<FS::pkg_svc>). Returns
255 false if there not exactly one service definition with quantity 1, or if
256 SVCDB is specified and does not match the svcdb of the service definition,
263 my @pkg_svc = $self->pkg_svc;
264 return '' if scalar(@pkg_svc) != 1
265 || $pkg_svc[0]->quantity != 1
266 || ( $svcdb && $pkg_svc[0]->part_svc->svcdb ne $svcdb );
267 $pkg_svc[0]->svcpart;
272 Returns a list of the acceptable payment types for this package. Eventually
273 this should come out of a database table and be editable, but currently has the
274 following logic instead;
276 If the package has B<0> setup and B<0> recur, the single item B<BILL> is
277 returned, otherwise, the single item B<CARD> is returned.
283 #if ( $self->setup == 0 && $self->recur == 0 ) {
284 if ( $self->setup =~ /^\s*0+(\.0*)?\s*$/
285 && $self->recur =~ /^\s*0+(\.0*)?\s*$/ ) {
296 $Id: part_pkg.pm,v 1.11 2002-04-25 09:47:25 ivan Exp $
300 The delete method is unimplemented.
302 setup and recur semantics are not yet defined (and are implemented in
303 FS::cust_bill. hmm.).
307 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
308 schema.html from the base documentation.