4 use vars qw( @ISA %plans $DEBUG );
5 use Carp qw(carp cluck confess);
6 use Scalar::Util qw( blessed );
9 use FS::Record qw( qsearch qsearchs dbh dbdef );
15 use FS::part_pkg_option;
18 use FS::part_pkg_taxoverride;
19 use FS::part_pkg_taxproduct;
20 use FS::part_pkg_link;
22 @ISA = qw( FS::m2m_Common FS::option_Common );
27 FS::part_pkg - Object methods for part_pkg objects
33 $record = new FS::part_pkg \%hash
34 $record = new FS::part_pkg { 'column' => 'value' };
36 $custom_record = $template_record->clone;
38 $error = $record->insert;
40 $error = $new_record->replace($old_record);
42 $error = $record->delete;
44 $error = $record->check;
46 @pkg_svc = $record->pkg_svc;
48 $svcnum = $record->svcpart;
49 $svcnum = $record->svcpart( 'svc_acct' );
53 An FS::part_pkg object represents a package definition. FS::part_pkg
54 inherits from FS::Record. The following fields are currently supported:
58 =item pkgpart - primary key (assigned automatically for new package definitions)
60 =item pkg - Text name of this package definition (customer-viewable)
62 =item comment - Text name of this package definition (non-customer-viewable)
64 =item classnum - Optional package class (see L<FS::pkg_class>)
66 =item promo_code - Promotional code
68 =item setup - Setup fee expression (deprecated)
70 =item freq - Frequency of recurring fee
72 =item recur - Recurring fee expression (deprecated)
74 =item setuptax - Setup fee tax exempt flag, empty or `Y'
76 =item recurtax - Recurring fee tax exempt flag, empty or `Y'
78 =item taxclass - Tax class
80 =item plan - Price plan
82 =item plandata - Price plan data (deprecated - see L<FS::part_pkg_option> instead)
84 =item disabled - Disabled flag, empty or `Y'
86 =item pay_weight - Weight (relative to credit_weight and other package definitions) that controls payment application to specific line items.
88 =item credit_weight - Weight (relative to other package definitions) that controls credit application to specific line items.
90 =item agentnum - Optional agentnum (see L<FS::agent>)
100 Creates a new package definition. To add the package definition to
101 the database, see L<"insert">.
105 sub table { 'part_pkg'; }
109 An alternate constructor. Creates a new package definition by duplicating
110 an existing definition. A new pkgpart is assigned and `(CUSTOM) ' is prepended
111 to the comment field. To add the package definition to the database, see
118 my $class = ref($self);
119 my %hash = $self->hash;
120 $hash{'pkgpart'} = '';
121 $hash{'comment'} = "(CUSTOM) ". $hash{'comment'}
122 unless $hash{'comment'} =~ /^\(CUSTOM\) /;
123 #new FS::part_pkg ( \%hash ); # ?
124 new $class ( \%hash ); # ?
127 =item insert [ , OPTION => VALUE ... ]
129 Adds this package definition to the database. If there is an error,
130 returns the error, otherwise returns false.
132 Currently available options are: I<pkg_svc>, I<primary_svc>, I<cust_pkg>,
133 I<custnum_ref> and I<options>.
135 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
136 values, appropriate FS::pkg_svc records will be inserted.
138 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
139 FS::pkg_svc record will be updated.
141 If I<cust_pkg> is set to a pkgnum of a FS::cust_pkg record (or the FS::cust_pkg
142 record itself), the object will be updated to point to this package definition.
144 In conjunction with I<cust_pkg>, if I<custnum_ref> is set to a scalar reference,
145 the scalar will be updated with the custnum value from the cust_pkg record.
147 If I<tax_overrides> is set to a hashref with usage classes as keys and comma
148 separated tax class numbers as values, appropriate FS::part_pkg_taxoverride
149 records will be inserted.
151 If I<options> is set to a hashref of options, appropriate FS::part_pkg_option
152 records will be inserted.
159 warn "FS::part_pkg::insert called on $self with options ".
160 join(', ', map "$_=>$options{$_}", keys %options)
163 local $SIG{HUP} = 'IGNORE';
164 local $SIG{INT} = 'IGNORE';
165 local $SIG{QUIT} = 'IGNORE';
166 local $SIG{TERM} = 'IGNORE';
167 local $SIG{TSTP} = 'IGNORE';
168 local $SIG{PIPE} = 'IGNORE';
170 my $oldAutoCommit = $FS::UID::AutoCommit;
171 local $FS::UID::AutoCommit = 0;
174 warn " inserting part_pkg record" if $DEBUG;
175 my $error = $self->SUPER::insert( $options{options} );
177 $dbh->rollback if $oldAutoCommit;
181 my $conf = new FS::Conf;
182 if ( $conf->exists('agent_defaultpkg') ) {
183 warn " agent_defaultpkg set; allowing all agents to purchase package"
185 foreach my $agent_type ( qsearch('agent_type', {} ) ) {
186 my $type_pkgs = new FS::type_pkgs({
187 'typenum' => $agent_type->typenum,
188 'pkgpart' => $self->pkgpart,
190 my $error = $type_pkgs->insert;
192 $dbh->rollback if $oldAutoCommit;
198 warn " inserting part_pkg_taxoverride records" if $DEBUG;
199 my %overrides = %{ $options{'tax_overrides'} || {} };
200 foreach my $usage_class ( keys %overrides ) {
201 my @overrides = (grep "$_", split (',', $overrides{$usage_class}) );
202 my $error = $self->process_m2m (
203 'link_table' => 'part_pkg_taxoverride',
204 'target_table' => 'tax_class',
205 'hashref' => { 'usage_class' => $usage_class },
206 'params' => \@overrides,
209 $dbh->rollback if $oldAutoCommit;
214 warn " inserting pkg_svc records" if $DEBUG;
215 my $pkg_svc = $options{'pkg_svc'} || {};
216 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
217 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
219 ( $options{'primary_svc'} && $options{'primary_svc'}==$part_svc->svcpart )
223 my $pkg_svc = new FS::pkg_svc( {
224 'pkgpart' => $self->pkgpart,
225 'svcpart' => $part_svc->svcpart,
226 'quantity' => $quantity,
227 'primary_svc' => $primary_svc,
229 my $error = $pkg_svc->insert;
231 $dbh->rollback if $oldAutoCommit;
236 if ( $options{'cust_pkg'} ) {
237 warn " updating cust_pkg record " if $DEBUG;
239 ref($options{'cust_pkg'})
240 ? $options{'cust_pkg'}
241 : qsearchs('cust_pkg', { pkgnum => $options{'cust_pkg'} } );
242 ${ $options{'custnum_ref'} } = $old_cust_pkg->custnum
243 if $options{'custnum_ref'};
244 my %hash = $old_cust_pkg->hash;
245 $hash{'pkgpart'} = $self->pkgpart,
246 my $new_cust_pkg = new FS::cust_pkg \%hash;
247 local($FS::cust_pkg::disable_agentcheck) = 1;
248 my $error = $new_cust_pkg->replace($old_cust_pkg);
250 $dbh->rollback if $oldAutoCommit;
251 return "Error modifying cust_pkg record: $error";
255 warn " commiting transaction" if $DEBUG;
256 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
263 Currently unimplemented.
268 return "Can't (yet?) delete package definitions.";
269 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
272 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
274 Replaces OLD_RECORD with this one in the database. If there is an error,
275 returns the error, otherwise returns false.
277 Currently available options are: I<pkg_svc>, I<primary_svc> and I<options>
279 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
280 values, the appropriate FS::pkg_svc records will be replaced.
282 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
283 FS::pkg_svc record will be updated.
285 If I<options> is set to a hashref, the appropriate FS::part_pkg_option records
293 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
298 ( ref($_[0]) eq 'HASH' )
302 $options->{options} = {} unless defined($options->{options});
304 warn "FS::part_pkg::replace called on $new to replace $old with options".
305 join(', ', map "$_ => ". $options->{$_}, keys %$options)
308 local $SIG{HUP} = 'IGNORE';
309 local $SIG{INT} = 'IGNORE';
310 local $SIG{QUIT} = 'IGNORE';
311 local $SIG{TERM} = 'IGNORE';
312 local $SIG{TSTP} = 'IGNORE';
313 local $SIG{PIPE} = 'IGNORE';
315 my $oldAutoCommit = $FS::UID::AutoCommit;
316 local $FS::UID::AutoCommit = 0;
319 #plandata shit stays in replace for upgrades until after 2.0 (or edit
321 warn " saving legacy plandata" if $DEBUG;
322 my $plandata = $new->get('plandata');
323 $new->set('plandata', '');
325 warn " deleting old part_pkg_option records" if $DEBUG;
326 foreach my $part_pkg_option ( $old->part_pkg_option ) {
327 my $error = $part_pkg_option->delete;
329 $dbh->rollback if $oldAutoCommit;
334 warn " replacing part_pkg record" if $DEBUG;
335 my $error = $new->SUPER::replace($old, $options->{options} );
337 $dbh->rollback if $oldAutoCommit;
341 warn " inserting part_pkg_option records for plandata: $plandata|" if $DEBUG;
342 foreach my $part_pkg_option (
343 map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
344 return "illegal plandata: $plandata";
346 new FS::part_pkg_option {
347 'pkgpart' => $new->pkgpart,
352 split("\n", $plandata)
354 my $error = $part_pkg_option->insert;
356 $dbh->rollback if $oldAutoCommit;
361 warn " replacing pkg_svc records" if $DEBUG;
362 my $pkg_svc = $options->{'pkg_svc'} || {};
363 foreach my $part_svc ( qsearch('part_svc', {} ) ) {
364 my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
366 ( defined($options->{'primary_svc'})
367 && $options->{'primary_svc'} == $part_svc->svcpart
373 my $old_pkg_svc = qsearchs('pkg_svc', {
374 'pkgpart' => $old->pkgpart,
375 'svcpart' => $part_svc->svcpart,
377 my $old_quantity = $old_pkg_svc ? $old_pkg_svc->quantity : 0;
378 my $old_primary_svc =
379 ( $old_pkg_svc && $old_pkg_svc->dbdef_table->column('primary_svc') )
380 ? $old_pkg_svc->primary_svc
382 next unless $old_quantity != $quantity || $old_primary_svc ne $primary_svc;
384 my $new_pkg_svc = new FS::pkg_svc( {
385 'pkgsvcnum' => ( $old_pkg_svc ? $old_pkg_svc->pkgsvcnum : '' ),
386 'pkgpart' => $new->pkgpart,
387 'svcpart' => $part_svc->svcpart,
388 'quantity' => $quantity,
389 'primary_svc' => $primary_svc,
391 my $error = $old_pkg_svc
392 ? $new_pkg_svc->replace($old_pkg_svc)
393 : $new_pkg_svc->insert;
395 $dbh->rollback if $oldAutoCommit;
400 warn " commiting transaction" if $DEBUG;
401 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
407 Checks all fields to make sure this is a valid package definition. If
408 there is an error, returns the error, otherwise returns false. Called by the
409 insert and replace methods.
415 warn "FS::part_pkg::check called on $self" if $DEBUG;
417 for (qw(setup recur plandata)) {
418 #$self->set($_=>0) if $self->get($_) =~ /^\s*$/; }
419 return "Use of $_ field is deprecated; set a plan and options: ".
421 if length($self->get($_));
425 if ( $self->dbdef_table->column('freq')->type =~ /(int)/i ) {
426 my $error = $self->ut_number('freq');
427 return $error if $error;
429 $self->freq =~ /^(\d+[hdw]?)$/
430 or return "Illegal or empty freq: ". $self->freq;
434 my $error = $self->ut_numbern('pkgpart')
435 || $self->ut_text('pkg')
436 || $self->ut_text('comment')
437 || $self->ut_textn('promo_code')
438 || $self->ut_alphan('plan')
439 || $self->ut_enum('setuptax', [ '', 'Y' ] )
440 || $self->ut_enum('recurtax', [ '', 'Y' ] )
441 || $self->ut_textn('taxclass')
442 || $self->ut_enum('disabled', [ '', 'Y' ] )
443 || $self->ut_floatn('pay_weight')
444 || $self->ut_floatn('credit_weight')
445 || $self->ut_numbern('taxproductnum')
446 || $self->ut_foreign_keyn('taxproductnum',
447 'part_pkg_taxproduct',
450 || $self->ut_agentnum_acl('agentnum', 'Edit global package definitions')
451 || $self->SUPER::check
453 return $error if $error;
455 if ( $self->classnum !~ /^$/ ) {
456 my $error = $self->ut_foreign_key('classnum', 'pkg_class', 'classnum');
457 return $error if $error;
462 return 'Unknown plan '. $self->plan
463 unless exists($plans{$self->plan});
465 my $conf = new FS::Conf;
466 return 'Taxclass is required'
467 if ! $self->taxclass && $conf->exists('require_taxclasses');
474 Returns an (internal) string representing this package. Currently,
475 "pkgpart: pkg - comment", is returned. "pkg - comment" may be returned in the
476 future, omitting pkgpart.
483 #$self->pkg. ' - '. $self->comment;
484 #$self->pkg. ' ('. $self->comment. ')';
485 $self->pkgpart. ': '. $self->pkg. ' - '. $self->comment;
490 Returns the package class, as an FS::pkg_class object, or the empty string
491 if there is no package class.
497 if ( $self->classnum ) {
498 qsearchs('pkg_class', { 'classnum' => $self->classnum } );
506 Returns the package category name, or the empty string if there is no package
513 my $pkg_class = $self->pkg_class;
515 ? $pkg_class->categoryname
521 Returns the package class name, or the empty string if there is no package
528 my $pkg_class = $self->pkg_class;
530 ? $pkg_class->classname
536 Returns the associated agent for this event, if any, as an FS::agent object.
542 qsearchs('agent', { 'agentnum' => $self->agentnum } );
545 =item pkg_svc [ HASHREF | OPTION => VALUE ]
547 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
548 definition (with non-zero quantity).
550 One option is available, I<disable_linked>. If set true it will return the
551 services for this package definition alone, omitting services from any add-on
559 # #sort { $b->primary cmp $a->primary }
560 # grep { $_->quantity }
561 # qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
563 my $opt = ref($_[0]) ? $_[0] : { @_ };
564 my %pkg_svc = map { $_->svcpart => $_ }
565 grep { $_->quantity }
566 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
568 unless ( $opt->{disable_linked} ) {
569 foreach my $dst_pkg ( map $_->dst_pkg, $self->svc_part_pkg_link ) {
570 my @pkg_svc = grep { $_->quantity }
571 qsearch( 'pkg_svc', { pkgpart=>$dst_pkg->pkgpart } );
572 foreach my $pkg_svc ( @pkg_svc ) {
573 if ( $pkg_svc{$pkg_svc->svcpart} ) {
574 my $quantity = $pkg_svc{$pkg_svc->svcpart}->quantity;
575 $pkg_svc{$pkg_svc->svcpart}->quantity($quantity + $pkg_svc->quantity);
577 $pkg_svc{$pkg_svc->svcpart} = $pkg_svc;
587 =item svcpart [ SVCDB ]
589 Returns the svcpart of the primary service definition (see L<FS::part_svc>)
590 associated with this package definition (see L<FS::pkg_svc>). Returns
591 false if there not a primary service definition or exactly one service
592 definition with quantity 1, or if SVCDB is specified and does not match the
593 svcdb of the service definition,
599 my $svcdb = scalar(@_) ? shift : '';
601 grep { ( $svcdb eq $_->part_svc->svcdb || !$svcdb ) } $self->pkg_svc;
603 @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc
604 if dbdef->table('pkg_svc')->column('primary_svc');
605 @pkg_svc = grep {$_->quantity == 1 } @svcdb_pkg_svc
607 return '' if scalar(@pkg_svc) != 1;
608 $pkg_svc[0]->svcpart;
613 Returns a list of the acceptable payment types for this package. Eventually
614 this should come out of a database table and be editable, but currently has the
615 following logic instead:
617 If the package is free, the single item B<BILL> is
618 returned, otherwise, the single item B<CARD> is returned.
620 (CHEK? LEC? Probably shouldn't accept those by default, prone to abuse)
626 if ( $self->is_free ) {
635 Returns true if this package is free.
641 unless ( $self->plan ) {
642 $self->setup =~ /^\s*0+(\.0*)?\s*$/
643 && $self->recur =~ /^\s*0+(\.0*)?\s*$/;
644 } elsif ( $self->can('is_free_options') ) {
645 not grep { $_ !~ /^\s*0*(\.0*)?\s*$/ }
646 map { $self->option($_) }
647 $self->is_free_options;
649 warn "FS::part_pkg::is_free: FS::part_pkg::". $self->plan. " subclass ".
650 "provides neither is_free_options nor is_free method; returning false";
657 #method, class method or sub? #my $self = shift;
659 tie my %freq, 'Tie::IxHash',
660 '0' => '(no recurring fee)',
663 '2d' => 'every two days',
664 '3d' => 'every three days',
666 '2w' => 'biweekly (every 2 weeks)',
668 '45d' => 'every 45 days',
669 '2' => 'bimonthly (every 2 months)',
670 '3' => 'quarterly (every 3 months)',
671 '4' => 'every 4 months',
672 '137d' => 'every 4 1/2 months (137 days)',
673 '6' => 'semiannually (every 6 months)',
675 '13' => 'every 13 months (annually +1 month)',
676 '24' => 'biannually (every 2 years)',
677 '36' => 'triannually (every 3 years)',
678 '48' => '(every 4 years)',
679 '60' => '(every 5 years)',
680 '120' => '(every 10 years)',
689 Returns an english representation of the I<freq> field, such as "monthly",
690 "weekly", "semi-annually", etc.
696 my $freq = $self->freq;
698 #my $freqs_href = $self->freqs_href;
699 my $freqs_href = freqs_href();
701 if ( exists($freqs_href->{$freq}) ) {
702 $freqs_href->{$freq};
704 my $interval = 'month';
705 if ( $freq =~ /^(\d+)([hdw])$/ ) {
706 my %interval = ( 'h' => 'hour', 'd'=>'day', 'w'=>'week' );
707 $interval = $interval{$2};
712 "every $freq ${interval}s";
719 For backwards compatibility, returns the plandata field as well as all options
720 from FS::part_pkg_option.
726 carp "plandata is deprecated";
728 $self->SUPER::plandata(@_);
730 my $plandata = $self->get('plandata');
731 my %options = $self->options;
732 $plandata .= join('', map { "$_=$options{$_}\n" } keys %options );
737 =item part_pkg_option
739 Returns all options as FS::part_pkg_option objects (see
740 L<FS::part_pkg_option>).
744 sub part_pkg_option {
746 qsearch('part_pkg_option', { 'pkgpart' => $self->pkgpart } );
751 Returns a list of option names and values suitable for assigning to a hash.
757 map { $_->optionname => $_->optionvalue } $self->part_pkg_option;
760 =item option OPTIONNAME
762 Returns the option value for the given name, or the empty string.
767 my( $self, $opt, $ornull ) = @_;
768 my $part_pkg_option =
769 qsearchs('part_pkg_option', {
770 pkgpart => $self->pkgpart,
773 return $part_pkg_option->optionvalue if $part_pkg_option;
774 my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
775 split("\n", $self->get('plandata') );
776 return $plandata{$opt} if exists $plandata{$opt};
777 cluck "WARNING: (pkgpart ". $self->pkgpart. ") Package def option $opt ".
778 "not found in options or plandata!\n"
783 =item bill_part_pkg_link
785 Returns the associated part_pkg_link records (see L<FS::part_pkg_link>).
789 sub bill_part_pkg_link {
790 shift->_part_pkg_link('bill', @_);
793 =item svc_part_pkg_link
795 Returns the associated part_pkg_link records (see L<FS::part_pkg_link>).
799 sub svc_part_pkg_link {
800 shift->_part_pkg_link('svc', @_);
804 my( $self, $type ) = @_;
805 qsearch('part_pkg_link', { 'src_pkgpart' => $self->pkgpart,
806 'link_type' => $type,
811 sub self_and_bill_linked {
812 shift->_self_and_linked('bill', @_);
815 sub _self_and_linked {
816 my( $self, $type ) = @_;
819 map { $_->dst_pkg->_self_and_linked($type) }
820 $self->_part_pkg_link($type)
824 =item part_pkg_taxoverride [ CLASS ]
826 Returns all associated FS::part_pkg_taxoverride objects (see
827 L<FS::part_pkg_taxoverride>). Limits the returned set to those
828 of class CLASS if defined. Class may be one of 'setup', 'recur',
829 the empty string (default), or a usage class number (see L<FS::usage_class>).
830 When a class is specified, the empty string class (default) is returned
831 if no more specific values exist.
835 sub part_pkg_taxoverride {
839 my $hashref = { 'pkgpart' => $self->pkgpart };
840 $hashref->{'usage_class'} = $class if defined($class);
841 my @overrides = qsearch('part_pkg_taxoverride', $hashref );
843 unless ( scalar(@overrides) || !defined($class) || !$class ){
844 $hashref->{'usage_class'} = '';
845 @overrides = qsearch('part_pkg_taxoverride', $hashref );
853 Returns true if this package has any taxproduct associated with it.
860 $self->taxproductnum ||
861 scalar(grep { $_ =~/^usage_taxproductnum_/ } keys %{ {$self->options} } )
866 =item taxproduct [ CLASS ]
868 Returns the associated tax product for this package definition (see
869 L<FS::part_pkg_taxproduct>). CLASS may be one of 'setup', 'recur' or
870 the usage classnum (see L<FS::usage_class>). Returns the default
871 tax product for this record if the more specific CLASS value does
880 my $part_pkg_taxproduct;
882 my $taxproductnum = $self->taxproductnum;
884 my $class_taxproductnum = $self->option("usage_taxproductnum_$class", 1);
885 $taxproductnum = $class_taxproductnum
886 if $class_taxproductnum
889 $part_pkg_taxproduct =
890 qsearchs( 'part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum } );
892 unless ($part_pkg_taxproduct || $taxproductnum eq $self->taxproductnum ) {
893 $taxproductnum = $self->taxproductnum;
894 $part_pkg_taxproduct =
895 qsearchs( 'part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum } );
898 $part_pkg_taxproduct;
901 =item taxproduct_description [ CLASS ]
903 Returns the description of the associated tax product for this package
904 definition (see L<FS::part_pkg_taxproduct>).
908 sub taxproduct_description {
910 my $part_pkg_taxproduct = $self->taxproduct(@_);
911 $part_pkg_taxproduct ? $part_pkg_taxproduct->description : '';
914 =item part_pkg_taxrate DATA_PROVIDER, GEOCODE, [ CLASS ]
916 Returns the package to taxrate m2m records for this package in the location
917 specified by GEOCODE (see L<FS::part_pkg_taxrate>) and usage class CLASS.
918 CLASS may be one of 'setup', 'recur', or one of the usage classes numbers
919 (see L<FS::usage_class>).
923 sub _expand_cch_taxproductnum {
926 my $part_pkg_taxproduct = $self->taxproduct($class);
928 my ($a,$b,$c,$d) = ( $part_pkg_taxproduct
929 ? ( split ':', $part_pkg_taxproduct->taxproduct )
932 $a = '' unless $a; $b = '' unless $b; $c = '' unless $c; $d = '' unless $d;
933 my $extra_sql = "AND ( taxproduct = '$a:$b:$c:$d'
934 OR taxproduct = '$a:$b:$c:'
935 OR taxproduct = '$a:$b:".":$d'
936 OR taxproduct = '$a:$b:".":' )";
937 map { $_->taxproductnum } qsearch( { 'table' => 'part_pkg_taxproduct',
938 'hashref' => { 'data_vendor'=>'cch' },
939 'extra_sql' => $extra_sql,
944 sub part_pkg_taxrate {
946 my ($data_vendor, $geocode, $class) = @_;
949 my $extra_sql = 'WHERE part_pkg_taxproduct.data_vendor = '.
950 dbh->quote($data_vendor);
953 $extra_sql .= ' AND ('.
954 join(' OR ', map{ 'geocode = '. $dbh->quote(substr($geocode, 0, $_)) }
958 # much more CCH oddness in m2m -- this is kludgy
959 $extra_sql .= ' AND ('.
960 join(' OR ', map{ "taxproductnum = $_" }
961 $self->_expand_cch_taxproductnum($class)
965 my $addl_from = 'LEFT JOIN part_pkg_taxproduct USING ( taxproductnum )';
966 my $order_by = 'ORDER BY taxclassnum, length(geocode) desc, length(taxproduct) desc';
967 my $select = 'DISTINCT ON(taxclassnum) *, taxproduct';
969 # should qsearch preface columns with the table to facilitate joins?
970 qsearch( { 'table' => 'part_pkg_taxrate',
972 'hashref' => { # 'data_vendor' => $data_vendor,
973 # 'taxproductnum' => $self->taxproductnum,
975 'addl_from' => $addl_from,
976 'extra_sql' => $extra_sql,
977 'order_by' => $order_by,
983 Reblesses the object into the FS::part_pkg::PLAN class (if available), where
984 PLAN is the object's I<plan> field. There should be better docs
985 on how to create new price plans, but until then, see L</NEW PLAN CLASSES>.
991 my $plan = $self->plan;
993 cluck "no price plan found for pkgpart ". $self->pkgpart. "\n"
997 return $self if ref($self) =~ /::$plan$/; #already blessed into plan subclass
998 my $class = ref($self). "::$plan";
999 warn "reblessing $self into $class" if $DEBUG;
1002 bless($self, $class) unless $@;
1006 #fallbacks that eval the setup and recur fields, for backwards compat
1010 warn 'no price plan class for '. $self->plan. ", eval-ing setup\n";
1011 $self->_calc_eval('setup', @_);
1016 warn 'no price plan class for '. $self->plan. ", eval-ing recur\n";
1017 $self->_calc_eval('recur', @_);
1020 use vars qw( $sdate @details );
1022 #my( $self, $field, $cust_pkg ) = @_;
1023 my( $self, $field, $cust_pkg, $sdateref, $detailsref ) = @_;
1025 *details = $detailsref;
1026 $self->$field() =~ /^(.*)$/
1027 or die "Illegal $field (pkgpart ". $self->pkgpart. '): '.
1028 $self->$field(). "\n";
1030 return 0 if $prog =~ /^\s*$/;
1031 my $value = eval $prog;
1036 #fallback that return 0 for old legacy packages with no plan
1038 sub calc_remain { 0; }
1039 sub calc_cancel { 0; }
1040 sub calc_units { 0; }
1048 # Used by FS::Upgrade to migrate to a new database.
1050 sub _upgrade_data { # class method
1051 my($class, %opts) = @_;
1053 warn "[FS::part_pkg] upgrading $class\n" if $DEBUG;
1055 my @part_pkg = qsearch({
1056 'table' => 'part_pkg',
1057 'extra_sql' => "WHERE ". join(' OR ',
1058 ( map "($_ IS NOT NULL AND $_ != '' )",
1059 qw( plandata setup recur ) ),
1060 'plan IS NULL', "plan = '' ",
1064 foreach my $part_pkg (@part_pkg) {
1066 unless ( $part_pkg->plan ) {
1068 $part_pkg->plan('flat');
1070 if ( $part_pkg->setup =~ /^\s*([\d\.]+)\s*$/ ) {
1072 my $opt = new FS::part_pkg_option {
1073 'pkgpart' => $part_pkg->pkgpart,
1074 'optionname' => 'setup_fee',
1075 'optionvalue' => $1,
1077 my $error = $opt->insert;
1078 die $error if $error;
1080 $part_pkg->setup('');
1083 die "Can't parse part_pkg.setup for fee; convert pkgnum ".
1084 $part_pkg->pkgnum. " manually: ". $part_pkg->setup. "\n";
1087 if ( $part_pkg->recur =~ /^\s*([\d\.]+)\s*$/ ) {
1089 my $opt = new FS::part_pkg_option {
1090 'pkgpart' => $part_pkg->pkgpart,
1091 'optionname' => 'recur_fee',
1092 'optionvalue' => $1,
1094 my $error = $opt->insert;
1095 die $error if $error;
1097 $part_pkg->recur('');
1100 die "Can't parse part_pkg.setup for fee; convert pkgnum ".
1101 $part_pkg->pkgnum. " manually: ". $part_pkg->setup. "\n";
1106 $part_pkg->replace; #this should take care of plandata, right?
1120 #false laziness w/part_export & cdr
1122 foreach my $INC ( @INC ) {
1123 warn "globbing $INC/FS/part_pkg/*.pm\n" if $DEBUG;
1124 foreach my $file ( glob("$INC/FS/part_pkg/*.pm") ) {
1125 warn "attempting to load plan info from $file\n" if $DEBUG;
1126 $file =~ /\/(\w+)\.pm$/ or do {
1127 warn "unrecognized file in $INC/FS/part_pkg/: $file\n";
1131 my $info = eval "use FS::part_pkg::$mod; ".
1132 "\\%FS::part_pkg::$mod\::info;";
1134 die "error using FS::part_pkg::$mod (skipping): $@\n" if $@;
1137 unless ( keys %$info ) {
1138 warn "no %info hash found in FS::part_pkg::$mod, skipping\n";
1141 warn "got plan info from FS::part_pkg::$mod: $info\n" if $DEBUG;
1142 if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
1143 warn "skipping disabled plan FS::part_pkg::$mod" if $DEBUG;
1146 $info{$mod} = $info;
1150 tie %plans, 'Tie::IxHash',
1151 map { $_ => $info{$_} }
1152 sort { $info{$a}->{'weight'} <=> $info{$b}->{'weight'} }
1159 =item format OPTION DATA
1161 Returns data formatted according to the function 'format' described
1162 in the plan info. Returns DATA if no such function exists.
1167 my ($self, $option, $data) = (shift, shift, shift);
1168 if (exists($plans{$self->plan}->{fields}->{$option}{format})) {
1169 &{$plans{$self->plan}->{fields}->{$option}{format}}($data);
1175 =item parse OPTION DATA
1177 Returns data parsed according to the function 'parse' described
1178 in the plan info. Returns DATA if no such function exists.
1183 my ($self, $option, $data) = (shift, shift, shift);
1184 if (exists($plans{$self->plan}->{fields}->{$option}{parse})) {
1185 &{$plans{$self->plan}->{fields}->{$option}{parse}}($data);
1194 =head1 NEW PLAN CLASSES
1196 A module should be added in FS/FS/part_pkg/ Eventually, an example may be
1197 found in eg/plan_template.pm. Until then, it is suggested that you use the
1198 other modules in FS/FS/part_pkg/ as a guide.
1202 The delete method is unimplemented.
1204 setup and recur semantics are not yet defined (and are implemented in
1205 FS::cust_bill. hmm.). now they're deprecated and need to go.
1209 part_pkg_taxrate is Pg specific
1213 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
1214 schema.html from the base documentation.