This commit was manufactured by cvs2svn to create tag 'freeside_2_1_1'.
[freeside.git] / FS / FS / part_pkg.pm
1 package FS::part_pkg;
2
3 use strict;
4 use vars qw( @ISA %plans $DEBUG $setup_hack $skip_pkg_svc_hack );
5 use Carp qw(carp cluck confess);
6 use Scalar::Util qw( blessed );
7 use Time::Local qw( timelocal_nocheck );
8 use Tie::IxHash;
9 use FS::Conf;
10 use FS::Record qw( qsearch qsearchs dbh dbdef );
11 use FS::pkg_svc;
12 use FS::part_svc;
13 use FS::cust_pkg;
14 use FS::agent_type;
15 use FS::type_pkgs;
16 use FS::part_pkg_option;
17 use FS::pkg_class;
18 use FS::agent;
19 use FS::part_pkg_taxrate;
20 use FS::part_pkg_taxoverride;
21 use FS::part_pkg_taxproduct;
22 use FS::part_pkg_link;
23 use FS::part_pkg_discount;
24
25 @ISA = qw( FS::m2m_Common FS::option_Common );
26 $DEBUG = 0;
27 $setup_hack = 0;
28 $skip_pkg_svc_hack = 0;
29
30 =head1 NAME
31
32 FS::part_pkg - Object methods for part_pkg objects
33
34 =head1 SYNOPSIS
35
36   use FS::part_pkg;
37
38   $record = new FS::part_pkg \%hash
39   $record = new FS::part_pkg { 'column' => 'value' };
40
41   $custom_record = $template_record->clone;
42
43   $error = $record->insert;
44
45   $error = $new_record->replace($old_record);
46
47   $error = $record->delete;
48
49   $error = $record->check;
50
51   @pkg_svc = $record->pkg_svc;
52
53   $svcnum = $record->svcpart;
54   $svcnum = $record->svcpart( 'svc_acct' );
55
56 =head1 DESCRIPTION
57
58 An FS::part_pkg object represents a package definition.  FS::part_pkg
59 inherits from FS::Record.  The following fields are currently supported:
60
61 =over 4
62
63 =item pkgpart - primary key (assigned automatically for new package definitions)
64
65 =item pkg - Text name of this package definition (customer-viewable)
66
67 =item comment - Text name of this package definition (non-customer-viewable)
68
69 =item classnum - Optional package class (see L<FS::pkg_class>)
70
71 =item promo_code - Promotional code
72
73 =item setup - Setup fee expression (deprecated)
74
75 =item freq - Frequency of recurring fee
76
77 =item recur - Recurring fee expression (deprecated)
78
79 =item setuptax - Setup fee tax exempt flag, empty or `Y'
80
81 =item recurtax - Recurring fee tax exempt flag, empty or `Y'
82
83 =item taxclass - Tax class 
84
85 =item plan - Price plan
86
87 =item plandata - Price plan data (deprecated - see L<FS::part_pkg_option> instead)
88
89 =item disabled - Disabled flag, empty or `Y'
90
91 =item custom - Custom flag, empty or `Y'
92
93 =item setup_cost - for cost tracking
94
95 =item recur_cost - for cost tracking
96
97 =item pay_weight - Weight (relative to credit_weight and other package definitions) that controls payment application to specific line items.
98
99 =item credit_weight - Weight (relative to other package definitions) that controls credit application to specific line items.
100
101 =item agentnum - Optional agentnum (see L<FS::agent>)
102
103 =item fcc_ds0s - Optional DS0 equivalency number for FCC form 477
104
105 =back
106
107 =head1 METHODS
108
109 =over 4 
110
111 =item new HASHREF
112
113 Creates a new package definition.  To add the package definition to
114 the database, see L<"insert">.
115
116 =cut
117
118 sub table { 'part_pkg'; }
119
120 =item clone
121
122 An alternate constructor.  Creates a new package definition by duplicating
123 an existing definition.  A new pkgpart is assigned and the custom flag is
124 set to Y.  To add the package definition to the database, see L<"insert">.
125
126 =cut
127
128 sub clone {
129   my $self = shift;
130   my $class = ref($self);
131   my %hash = $self->hash;
132   $hash{'pkgpart'} = '';
133   $hash{'custom'} = 'Y';
134   #new FS::part_pkg ( \%hash ); # ?
135   new $class ( \%hash ); # ?
136 }
137
138 =item insert [ , OPTION => VALUE ... ]
139
140 Adds this package definition to the database.  If there is an error,
141 returns the error, otherwise returns false.
142
143 Currently available options are: I<pkg_svc>, I<primary_svc>, I<cust_pkg>, 
144 I<custnum_ref> and I<options>.
145
146 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
147 values, appropriate FS::pkg_svc records will be inserted.
148
149 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
150 FS::pkg_svc record will be updated.
151
152 If I<cust_pkg> is set to a pkgnum of a FS::cust_pkg record (or the FS::cust_pkg
153 record itself), the object will be updated to point to this package definition.
154
155 In conjunction with I<cust_pkg>, if I<custnum_ref> is set to a scalar reference,
156 the scalar will be updated with the custnum value from the cust_pkg record.
157
158 If I<tax_overrides> is set to a hashref with usage classes as keys and comma
159 separated tax class numbers as values, appropriate FS::part_pkg_taxoverride
160 records will be inserted.
161
162 If I<options> is set to a hashref of options, appropriate FS::part_pkg_option
163 records will be inserted.
164
165 =cut
166
167 sub insert {
168   my $self = shift;
169   my %options = @_;
170   warn "FS::part_pkg::insert called on $self with options ".
171        join(', ', map "$_=>$options{$_}", keys %options)
172     if $DEBUG;
173
174   local $SIG{HUP} = 'IGNORE';
175   local $SIG{INT} = 'IGNORE';
176   local $SIG{QUIT} = 'IGNORE';
177   local $SIG{TERM} = 'IGNORE';
178   local $SIG{TSTP} = 'IGNORE';
179   local $SIG{PIPE} = 'IGNORE';
180
181   my $oldAutoCommit = $FS::UID::AutoCommit;
182   local $FS::UID::AutoCommit = 0;
183   my $dbh = dbh;
184
185   warn "  inserting part_pkg record" if $DEBUG;
186   my $error = $self->SUPER::insert( $options{options} );
187   if ( $error ) {
188     $dbh->rollback if $oldAutoCommit;
189     return $error;
190   }
191
192   my $conf = new FS::Conf;
193   if ( $conf->exists('agent_defaultpkg') ) {
194     warn "  agent_defaultpkg set; allowing all agents to purchase package"
195       if $DEBUG;
196     foreach my $agent_type ( qsearch('agent_type', {} ) ) {
197       my $type_pkgs = new FS::type_pkgs({
198         'typenum' => $agent_type->typenum,
199         'pkgpart' => $self->pkgpart,
200       });
201       my $error = $type_pkgs->insert;
202       if ( $error ) {
203         $dbh->rollback if $oldAutoCommit;
204         return $error;
205       }
206     }
207   }
208
209   warn "  inserting part_pkg_taxoverride records" if $DEBUG;
210   my %overrides = %{ $options{'tax_overrides'} || {} };
211   foreach my $usage_class ( keys %overrides ) {
212     my $override =
213       ( exists($overrides{$usage_class}) && defined($overrides{$usage_class}) )
214         ? $overrides{$usage_class}
215         : '';
216     my @overrides = (grep "$_", split(',', $override) );
217     my $error = $self->process_m2m (
218                   'link_table'   => 'part_pkg_taxoverride',
219                   'target_table' => 'tax_class',
220                   'hashref'      => { 'usage_class' => $usage_class },
221                   'params'       => \@overrides,
222                 );
223     if ( $error ) {
224       $dbh->rollback if $oldAutoCommit;
225       return $error;
226     }
227   }
228
229   unless ( $skip_pkg_svc_hack ) {
230
231     warn "  inserting pkg_svc records" if $DEBUG;
232     my $pkg_svc = $options{'pkg_svc'} || {};
233     foreach my $part_svc ( qsearch('part_svc', {} ) ) {
234       my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
235       my $primary_svc =
236         ( $options{'primary_svc'} && $options{'primary_svc'}==$part_svc->svcpart )
237           ? 'Y'
238           : '';
239
240       my $pkg_svc = new FS::pkg_svc( {
241         'pkgpart'     => $self->pkgpart,
242         'svcpart'     => $part_svc->svcpart,
243         'quantity'    => $quantity, 
244         'primary_svc' => $primary_svc,
245       } );
246       my $error = $pkg_svc->insert;
247       if ( $error ) {
248         $dbh->rollback if $oldAutoCommit;
249         return $error;
250       }
251     }
252
253   }
254
255   if ( $options{'cust_pkg'} ) {
256     warn "  updating cust_pkg record " if $DEBUG;
257     my $old_cust_pkg =
258       ref($options{'cust_pkg'})
259         ? $options{'cust_pkg'}
260         : qsearchs('cust_pkg', { pkgnum => $options{'cust_pkg'} } );
261     ${ $options{'custnum_ref'} } = $old_cust_pkg->custnum
262       if $options{'custnum_ref'};
263     my %hash = $old_cust_pkg->hash;
264     $hash{'pkgpart'} = $self->pkgpart,
265     my $new_cust_pkg = new FS::cust_pkg \%hash;
266     local($FS::cust_pkg::disable_agentcheck) = 1;
267     my $error = $new_cust_pkg->replace($old_cust_pkg);
268     if ( $error ) {
269       $dbh->rollback if $oldAutoCommit;
270       return "Error modifying cust_pkg record: $error";
271     }
272   }
273
274   warn "  commiting transaction" if $DEBUG;
275   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
276
277   '';
278 }
279
280 =item delete
281
282 Currently unimplemented.
283
284 =cut
285
286 sub delete {
287   return "Can't (yet?) delete package definitions.";
288 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
289 }
290
291 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
292
293 Replaces OLD_RECORD with this one in the database.  If there is an error,
294 returns the error, otherwise returns false.
295
296 Currently available options are: I<pkg_svc>, I<primary_svc> and I<options>
297
298 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
299 values, the appropriate FS::pkg_svc records will be replaced.
300
301 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
302 FS::pkg_svc record will be updated.
303
304 If I<options> is set to a hashref, the appropriate FS::part_pkg_option records
305 will be replaced.
306
307 =cut
308
309 sub replace {
310   my $new = shift;
311
312   my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
313               ? shift
314               : $new->replace_old;
315
316   my $options = 
317     ( ref($_[0]) eq 'HASH' )
318       ? shift
319       : { @_ };
320
321   $options->{options} = {} unless defined($options->{options});
322
323   warn "FS::part_pkg::replace called on $new to replace $old with options".
324        join(', ', map "$_ => ". $options->{$_}, keys %$options)
325     if $DEBUG;
326
327   local $SIG{HUP} = 'IGNORE';
328   local $SIG{INT} = 'IGNORE';
329   local $SIG{QUIT} = 'IGNORE';
330   local $SIG{TERM} = 'IGNORE';
331   local $SIG{TSTP} = 'IGNORE';
332   local $SIG{PIPE} = 'IGNORE';
333
334   my $oldAutoCommit = $FS::UID::AutoCommit;
335   local $FS::UID::AutoCommit = 0;
336   my $dbh = dbh;
337
338   #plandata shit stays in replace for upgrades until after 2.0 (or edit
339   #_upgrade_data)
340   warn "  saving legacy plandata" if $DEBUG;
341   my $plandata = $new->get('plandata');
342   $new->set('plandata', '');
343
344   warn "  deleting old part_pkg_option records" if $DEBUG;
345   foreach my $part_pkg_option ( $old->part_pkg_option ) {
346     my $error = $part_pkg_option->delete;
347     if ( $error ) {
348       $dbh->rollback if $oldAutoCommit;
349       return $error;
350     }
351   }
352
353   warn "  replacing part_pkg record" if $DEBUG;
354   my $error = $new->SUPER::replace($old, $options->{options} );
355   if ( $error ) {
356     $dbh->rollback if $oldAutoCommit;
357     return $error;
358   }
359
360   warn "  inserting part_pkg_option records for plandata: $plandata|" if $DEBUG;
361   foreach my $part_pkg_option ( 
362     map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
363                                  return "illegal plandata: $plandata";
364                                };
365           new FS::part_pkg_option {
366             'pkgpart'     => $new->pkgpart,
367             'optionname'  => $1,
368             'optionvalue' => $2,
369           };
370         }
371     split("\n", $plandata)
372   ) {
373     my $error = $part_pkg_option->insert;
374     if ( $error ) {
375       $dbh->rollback if $oldAutoCommit;
376       return $error;
377     }
378   }
379
380   warn "  replacing pkg_svc records" if $DEBUG;
381   my $pkg_svc = $options->{'pkg_svc'} || {};
382   foreach my $part_svc ( qsearch('part_svc', {} ) ) {
383     my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
384     my $primary_svc =
385       ( defined($options->{'primary_svc'}) && $options->{'primary_svc'}
386         && $options->{'primary_svc'} == $part_svc->svcpart
387       )
388         ? 'Y'
389         : '';
390
391
392     my $old_pkg_svc = qsearchs('pkg_svc', {
393       'pkgpart' => $old->pkgpart,
394       'svcpart' => $part_svc->svcpart,
395     } );
396     my $old_quantity = $old_pkg_svc ? $old_pkg_svc->quantity : 0;
397     my $old_primary_svc =
398       ( $old_pkg_svc && $old_pkg_svc->dbdef_table->column('primary_svc') )
399         ? $old_pkg_svc->primary_svc
400         : '';
401     next unless $old_quantity != $quantity || $old_primary_svc ne $primary_svc;
402   
403     my $new_pkg_svc = new FS::pkg_svc( {
404       'pkgsvcnum'   => ( $old_pkg_svc ? $old_pkg_svc->pkgsvcnum : '' ),
405       'pkgpart'     => $new->pkgpart,
406       'svcpart'     => $part_svc->svcpart,
407       'quantity'    => $quantity, 
408       'primary_svc' => $primary_svc,
409     } );
410     my $error = $old_pkg_svc
411                   ? $new_pkg_svc->replace($old_pkg_svc)
412                   : $new_pkg_svc->insert;
413     if ( $error ) {
414       $dbh->rollback if $oldAutoCommit;
415       return $error;
416     }
417   }
418
419   warn "  commiting transaction" if $DEBUG;
420   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
421   '';
422 }
423
424 =item check
425
426 Checks all fields to make sure this is a valid package definition.  If
427 there is an error, returns the error, otherwise returns false.  Called by the
428 insert and replace methods.
429
430 =cut
431
432 sub check {
433   my $self = shift;
434   warn "FS::part_pkg::check called on $self" if $DEBUG;
435
436   for (qw(setup recur plandata)) {
437     #$self->set($_=>0) if $self->get($_) =~ /^\s*$/; }
438     return "Use of $_ field is deprecated; set a plan and options: ".
439            $self->get($_)
440       if length($self->get($_));
441     $self->set($_, '');
442   }
443
444   if ( $self->dbdef_table->column('freq')->type =~ /(int)/i ) {
445     my $error = $self->ut_number('freq');
446     return $error if $error;
447   } else {
448     $self->freq =~ /^(\d+[hdw]?)$/
449       or return "Illegal or empty freq: ". $self->freq;
450     $self->freq($1);
451   }
452
453   my @null_agentnum_right = ( 'Edit global package definitions' );
454   push @null_agentnum_right, 'One-time charge'
455     if $self->freq =~ /^0/;
456   push @null_agentnum_right, 'Customize customer package'
457     if $self->disabled eq 'Y'; #good enough
458
459   my $error = $self->ut_numbern('pkgpart')
460     || $self->ut_text('pkg')
461     || $self->ut_text('comment')
462     || $self->ut_textn('promo_code')
463     || $self->ut_alphan('plan')
464     || $self->ut_enum('setuptax', [ '', 'Y' ] )
465     || $self->ut_enum('recurtax', [ '', 'Y' ] )
466     || $self->ut_textn('taxclass')
467     || $self->ut_enum('disabled', [ '', 'Y' ] )
468     || $self->ut_enum('custom', [ '', 'Y' ] )
469     || $self->ut_enum('no_auto', [ '', 'Y' ])
470     #|| $self->ut_moneyn('setup_cost')
471     #|| $self->ut_moneyn('recur_cost')
472     || $self->ut_floatn('setup_cost')
473     || $self->ut_floatn('recur_cost')
474     || $self->ut_floatn('pay_weight')
475     || $self->ut_floatn('credit_weight')
476     || $self->ut_numbern('taxproductnum')
477     || $self->ut_foreign_keyn('classnum',       'pkg_class', 'classnum')
478     || $self->ut_foreign_keyn('addon_classnum', 'pkg_class', 'classnum')
479     || $self->ut_foreign_keyn('taxproductnum',
480                               'part_pkg_taxproduct',
481                               'taxproductnum'
482                              )
483     || ( $setup_hack
484            ? $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum' )
485            : $self->ut_agentnum_acl('agentnum', \@null_agentnum_right)
486        )
487     || $self->ut_numbern('fcc_ds0s')
488     || $self->SUPER::check
489   ;
490   return $error if $error;
491
492   return 'Unknown plan '. $self->plan
493     unless exists($plans{$self->plan});
494
495   my $conf = new FS::Conf;
496   return 'Taxclass is required'
497     if ! $self->taxclass && $conf->exists('require_taxclasses');
498
499   '';
500 }
501
502 =item pkg_comment [ OPTION => VALUE... ]
503
504 Returns an (internal) string representing this package.  Currently,
505 "pkgpart: pkg - comment", is returned.  "pkg - comment" may be returned in the
506 future, omitting pkgpart.  The comment will have '(CUSTOM) ' prepended if
507 custom is Y.
508
509 If the option nopkgpart is true then the "pkgpart: ' is omitted.
510
511 =cut
512
513 sub pkg_comment {
514   my $self = shift;
515   my %opt = @_;
516
517   #$self->pkg. ' - '. $self->comment;
518   #$self->pkg. ' ('. $self->comment. ')';
519   my $pre = $opt{nopkgpart} ? '' : $self->pkgpart. ': ';
520   $pre. $self->pkg. ' - '. $self->custom_comment;
521 }
522
523 sub custom_comment {
524   my $self = shift;
525   ( $self->custom ? '(CUSTOM) ' : '' ). $self->comment;
526 }
527
528 =item pkg_class
529
530 Returns the package class, as an FS::pkg_class object, or the empty string
531 if there is no package class.
532
533 =cut
534
535 sub pkg_class {
536   my $self = shift;
537   if ( $self->classnum ) {
538     qsearchs('pkg_class', { 'classnum' => $self->classnum } );
539   } else {
540     return '';
541   }
542 }
543
544 =item addon_pkg_class
545
546 Returns the add-on package class, as an FS::pkg_class object, or the empty
547 string if there is no add-on package class.
548
549 =cut
550
551 sub addon_pkg_class {
552   my $self = shift;
553   if ( $self->addon_classnum ) {
554     qsearchs('pkg_class', { 'classnum' => $self->addon_classnum } );
555   } else {
556     return '';
557   }
558 }
559
560 =item categoryname 
561
562 Returns the package category name, or the empty string if there is no package
563 category.
564
565 =cut
566
567 sub categoryname {
568   my $self = shift;
569   my $pkg_class = $self->pkg_class;
570   $pkg_class
571     ? $pkg_class->categoryname
572     : '';
573 }
574
575 =item classname 
576
577 Returns the package class name, or the empty string if there is no package
578 class.
579
580 =cut
581
582 sub classname {
583   my $self = shift;
584   my $pkg_class = $self->pkg_class;
585   $pkg_class
586     ? $pkg_class->classname
587     : '';
588 }
589
590 =item addon_classname 
591
592 Returns the add-on package class name, or the empty string if there is no
593 add-on package class.
594
595 =cut
596
597 sub addon_classname {
598   my $self = shift;
599   my $pkg_class = $self->addon_pkg_class;
600   $pkg_class
601     ? $pkg_class->classname
602     : '';
603 }
604
605 =item agent 
606
607 Returns the associated agent for this event, if any, as an FS::agent object.
608
609 =cut
610
611 sub agent {
612   my $self = shift;
613   qsearchs('agent', { 'agentnum' => $self->agentnum } );
614 }
615
616 =item pkg_svc [ HASHREF | OPTION => VALUE ]
617
618 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
619 definition (with non-zero quantity).
620
621 One option is available, I<disable_linked>.  If set true it will return the
622 services for this package definition alone, omitting services from any add-on
623 packages.
624
625 =cut
626
627 =item type_pkgs
628
629 Returns all FS::type_pkgs objects (see L<FS::type_pkgs>) for this package
630 definition.
631
632 =cut
633
634 sub type_pkgs {
635   my $self = shift;
636   qsearch('type_pkgs', { 'pkgpart' => $self->pkgpart } );
637 }
638
639 sub pkg_svc {
640   my $self = shift;
641
642 #  #sort { $b->primary cmp $a->primary } 
643 #    grep { $_->quantity }
644 #      qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
645
646   my $opt = ref($_[0]) ? $_[0] : { @_ };
647   my %pkg_svc = map  { $_->svcpart => $_ }
648                 grep { $_->quantity }
649                 qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
650
651   unless ( $opt->{disable_linked} ) {
652     foreach my $dst_pkg ( map $_->dst_pkg, $self->svc_part_pkg_link ) {
653       my @pkg_svc = grep { $_->quantity }
654                     qsearch( 'pkg_svc', { pkgpart=>$dst_pkg->pkgpart } );
655       foreach my $pkg_svc ( @pkg_svc ) {
656         if ( $pkg_svc{$pkg_svc->svcpart} ) {
657           my $quantity = $pkg_svc{$pkg_svc->svcpart}->quantity;
658           $pkg_svc{$pkg_svc->svcpart}->quantity($quantity + $pkg_svc->quantity);
659         } else {
660           $pkg_svc{$pkg_svc->svcpart} = $pkg_svc;
661         }
662       }
663     }
664   }
665
666   values(%pkg_svc);
667
668 }
669
670 =item svcpart [ SVCDB ]
671
672 Returns the svcpart of the primary service definition (see L<FS::part_svc>)
673 associated with this package definition (see L<FS::pkg_svc>).  Returns
674 false if there not a primary service definition or exactly one service
675 definition with quantity 1, or if SVCDB is specified and does not match the
676 svcdb of the service definition.  SVCDB can be specified as a scalar table
677 name, such as 'svc_acct', or as an arrayref of possible table names.
678
679 =cut
680
681 sub svcpart {
682   my $pkg_svc = shift->_primary_pkg_svc(@_);
683   $pkg_svc ? $pkg_svc->svcpart : '';
684 }
685
686 =item part_svc [ SVCDB ]
687
688 Like the B<svcpart> method, but returns the FS::part_svc object (see
689 L<FS::part_svc>).
690
691 =cut
692
693 sub part_svc {
694   my $pkg_svc = shift->_primary_pkg_svc(@_);
695   $pkg_svc ? $pkg_svc->part_svc : '';
696 }
697
698 sub _primary_pkg_svc {
699   my $self = shift;
700
701   my $svcdb = scalar(@_) ? shift : [];
702   $svcdb = ref($svcdb) ? $svcdb : [ $svcdb ];
703   my %svcdb = map { $_=>1 } @$svcdb;
704
705   my @svcdb_pkg_svc =
706     grep { !scalar(@$svcdb) || $svcdb{ $_->part_svc->svcdb } }
707          $self->pkg_svc;
708
709   my @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc;
710   @pkg_svc = grep {$_->quantity == 1 } @svcdb_pkg_svc
711     unless @pkg_svc;
712   return '' if scalar(@pkg_svc) != 1;
713   $pkg_svc[0];
714 }
715
716 =item svcpart_unique_svcdb SVCDB
717
718 Returns the svcpart of a service definition (see L<FS::part_svc>) matching
719 SVCDB associated with this package definition (see L<FS::pkg_svc>).  Returns
720 false if there not a primary service definition for SVCDB or there are multiple
721 service definitions for SVCDB.
722
723 =cut
724
725 sub svcpart_unique_svcdb {
726   my( $self, $svcdb ) = @_;
727   my @svcdb_pkg_svc = grep { ( $svcdb eq $_->part_svc->svcdb ) } $self->pkg_svc;
728   return '' if scalar(@svcdb_pkg_svc) != 1;
729   $svcdb_pkg_svc[0]->svcpart;
730 }
731
732 =item payby
733
734 Returns a list of the acceptable payment types for this package.  Eventually
735 this should come out of a database table and be editable, but currently has the
736 following logic instead:
737
738 If the package is free, the single item B<BILL> is
739 returned, otherwise, the single item B<CARD> is returned.
740
741 (CHEK?  LEC?  Probably shouldn't accept those by default, prone to abuse)
742
743 =cut
744
745 sub payby {
746   my $self = shift;
747   if ( $self->is_free ) {
748     ( 'BILL' );
749   } else {
750     ( 'CARD' );
751   }
752 }
753
754 =item is_free
755
756 Returns true if this package is free.  
757
758 =cut
759
760 sub is_free {
761   my $self = shift;
762   unless ( $self->plan ) {
763     $self->setup =~ /^\s*0+(\.0*)?\s*$/
764       && $self->recur =~ /^\s*0+(\.0*)?\s*$/;
765   } elsif ( $self->can('is_free_options') ) {
766     not grep { $_ !~ /^\s*0*(\.0*)?\s*$/ }
767          map { $self->option($_) } 
768              $self->is_free_options;
769   } else {
770     warn "FS::part_pkg::is_free: FS::part_pkg::". $self->plan. " subclass ".
771          "provides neither is_free_options nor is_free method; returning false";
772     0;
773   }
774 }
775
776 sub can_discount { 0; }
777
778 sub freqs_href {
779   # moved to FS::Misc to make this accessible to other packages
780   # at initialization
781   FS::Misc::pkg_freqs();
782 }
783
784 =item freq_pretty
785
786 Returns an english representation of the I<freq> field, such as "monthly",
787 "weekly", "semi-annually", etc.
788
789 =cut
790
791 sub freq_pretty {
792   my $self = shift;
793   my $freq = $self->freq;
794
795   #my $freqs_href = $self->freqs_href;
796   my $freqs_href = freqs_href();
797
798   if ( exists($freqs_href->{$freq}) ) {
799     $freqs_href->{$freq};
800   } else {
801     my $interval = 'month';
802     if ( $freq =~ /^(\d+)([hdw])$/ ) {
803       my %interval = ( 'h' => 'hour', 'd'=>'day', 'w'=>'week' );
804       $interval = $interval{$2};
805     }
806     if ( $1 == 1 ) {
807       "every $interval";
808     } else {
809       "every $freq ${interval}s";
810     }
811   }
812 }
813
814 =item add_freq TIMESTAMP [ FREQ ]
815
816 Adds a billing period of some frequency to the provided timestamp and 
817 returns the resulting timestamp, or -1 if the frequency could not be 
818 parsed (shouldn't happen).  By default, the frequency of this package 
819 will be used; to override this, pass a different frequency as a second 
820 argument.
821
822 =cut
823
824 sub add_freq {
825   my( $self, $date, $freq ) = @_;
826   $freq = $self->freq unless $freq;
827
828   #change this bit to use Date::Manip? CAREFUL with timezones (see
829   # mailing list archive)
830   my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($date) )[0,1,2,3,4,5];
831
832   if ( $freq =~ /^\d+$/ ) {
833     $mon += $freq;
834     until ( $mon < 12 ) { $mon -= 12; $year++; }
835   } elsif ( $freq =~ /^(\d+)w$/ ) {
836     my $weeks = $1;
837     $mday += $weeks * 7;
838   } elsif ( $freq =~ /^(\d+)d$/ ) {
839     my $days = $1;
840     $mday += $days;
841   } elsif ( $freq =~ /^(\d+)h$/ ) {
842     my $hours = $1;
843     $hour += $hours;
844   } else {
845     return -1;
846   }
847
848   timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year);
849 }
850
851 =item plandata
852
853 For backwards compatibility, returns the plandata field as well as all options
854 from FS::part_pkg_option.
855
856 =cut
857
858 sub plandata {
859   my $self = shift;
860   carp "plandata is deprecated";
861   if ( @_ ) {
862     $self->SUPER::plandata(@_);
863   } else {
864     my $plandata = $self->get('plandata');
865     my %options = $self->options;
866     $plandata .= join('', map { "$_=$options{$_}\n" } keys %options );
867     $plandata;
868   }
869 }
870
871 =item part_pkg_option
872
873 Returns all options as FS::part_pkg_option objects (see
874 L<FS::part_pkg_option>).
875
876 =cut
877
878 sub part_pkg_option {
879   my $self = shift;
880   qsearch('part_pkg_option', { 'pkgpart' => $self->pkgpart } );
881 }
882
883 =item options 
884
885 Returns a list of option names and values suitable for assigning to a hash.
886
887 =cut
888
889 sub options {
890   my $self = shift;
891   map { $_->optionname => $_->optionvalue } $self->part_pkg_option;
892 }
893
894 =item option OPTIONNAME [ QUIET ]
895
896 Returns the option value for the given name, or the empty string.  If a true
897 value is passed as the second argument, warnings about missing the option
898 will be suppressed.
899
900 =cut
901
902 sub option {
903   my( $self, $opt, $ornull ) = @_;
904   my $part_pkg_option =
905     qsearchs('part_pkg_option', {
906       pkgpart    => $self->pkgpart,
907       optionname => $opt,
908   } );
909   return $part_pkg_option->optionvalue if $part_pkg_option;
910   my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
911                      split("\n", $self->get('plandata') );
912   return $plandata{$opt} if exists $plandata{$opt};
913   cluck "WARNING: (pkgpart ". $self->pkgpart. ") Package def option $opt ".
914         "not found in options or plandata!\n"
915     unless $ornull;
916   '';
917 }
918
919 =item bill_part_pkg_link
920
921 Returns the associated part_pkg_link records (see L<FS::part_pkg_link>).
922
923 =cut
924
925 sub bill_part_pkg_link {
926   shift->_part_pkg_link('bill', @_);
927 }
928
929 =item svc_part_pkg_link
930
931 Returns the associated part_pkg_link records (see L<FS::part_pkg_link>).
932
933 =cut
934
935 sub svc_part_pkg_link {
936   shift->_part_pkg_link('svc', @_);
937 }
938
939 sub _part_pkg_link {
940   my( $self, $type ) = @_;
941   qsearch({ table    => 'part_pkg_link',
942             hashref  => { 'src_pkgpart' => $self->pkgpart,
943                           'link_type'   => $type,
944                         },
945             order_by => "ORDER BY hidden",
946          });
947 }
948
949 sub self_and_bill_linked {
950   shift->_self_and_linked('bill', @_);
951 }
952
953 sub _self_and_linked {
954   my( $self, $type, $hidden ) = @_;
955   $hidden ||= '';
956
957   my @result = ();
958   foreach ( ( $self, map { $_->dst_pkg->_self_and_linked($type, $_->hidden) }
959                      $self->_part_pkg_link($type) ) )
960   {
961     $_->hidden($hidden) if $hidden;
962     push @result, $_;
963   }
964
965   (@result);
966 }
967
968 =item part_pkg_taxoverride [ CLASS ]
969
970 Returns all associated FS::part_pkg_taxoverride objects (see
971 L<FS::part_pkg_taxoverride>).  Limits the returned set to those
972 of class CLASS if defined.  Class may be one of 'setup', 'recur',
973 the empty string (default), or a usage class number (see L<FS::usage_class>).
974 When a class is specified, the empty string class (default) is returned
975 if no more specific values exist.
976
977 =cut
978
979 sub part_pkg_taxoverride {
980   my $self = shift;
981   my $class = shift;
982
983   my $hashref = { 'pkgpart' => $self->pkgpart };
984   $hashref->{'usage_class'} = $class if defined($class);
985   my @overrides = qsearch('part_pkg_taxoverride', $hashref );
986
987   unless ( scalar(@overrides) || !defined($class) || !$class ){
988     $hashref->{'usage_class'} = '';
989     @overrides = qsearch('part_pkg_taxoverride', $hashref );
990   }
991
992   @overrides;
993 }
994
995 =item has_taxproduct
996
997 Returns true if this package has any taxproduct associated with it.  
998
999 =cut
1000
1001 sub has_taxproduct {
1002   my $self = shift;
1003
1004   $self->taxproductnum ||
1005   scalar( grep { $_ =~/^usage_taxproductnum_/ && $self->option($_) } 
1006           keys %{ {$self->options} }
1007   )
1008
1009 }
1010
1011
1012 =item taxproduct [ CLASS ]
1013
1014 Returns the associated tax product for this package definition (see
1015 L<FS::part_pkg_taxproduct>).  CLASS may be one of 'setup', 'recur' or
1016 the usage classnum (see L<FS::usage_class>).  Returns the default
1017 tax product for this record if the more specific CLASS value does
1018 not exist.
1019
1020 =cut
1021
1022 sub taxproduct {
1023   my $self = shift;
1024   my $class = shift;
1025
1026   my $part_pkg_taxproduct;
1027
1028   my $taxproductnum = $self->taxproductnum;
1029   if ($class) { 
1030     my $class_taxproductnum = $self->option("usage_taxproductnum_$class", 1);
1031     $taxproductnum = $class_taxproductnum
1032       if $class_taxproductnum
1033   }
1034   
1035   $part_pkg_taxproduct =
1036     qsearchs( 'part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum } );
1037
1038   unless ($part_pkg_taxproduct || $taxproductnum eq $self->taxproductnum ) {
1039     $taxproductnum = $self->taxproductnum;
1040     $part_pkg_taxproduct =
1041       qsearchs( 'part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum } );
1042   }
1043
1044   $part_pkg_taxproduct;
1045 }
1046
1047 =item taxproduct_description [ CLASS ]
1048
1049 Returns the description of the associated tax product for this package
1050 definition (see L<FS::part_pkg_taxproduct>).
1051
1052 =cut
1053
1054 sub taxproduct_description {
1055   my $self = shift;
1056   my $part_pkg_taxproduct = $self->taxproduct(@_);
1057   $part_pkg_taxproduct ? $part_pkg_taxproduct->description : '';
1058 }
1059
1060 =item part_pkg_taxrate DATA_PROVIDER, GEOCODE, [ CLASS ]
1061
1062 Returns the package to taxrate m2m records for this package in the location
1063 specified by GEOCODE (see L<FS::part_pkg_taxrate>) and usage class CLASS.
1064 CLASS may be one of 'setup', 'recur', or one of the usage classes numbers
1065 (see L<FS::usage_class>).
1066
1067 =cut
1068
1069 sub _expand_cch_taxproductnum {
1070   my $self = shift;
1071   my $class = shift;
1072   my $part_pkg_taxproduct = $self->taxproduct($class);
1073
1074   my ($a,$b,$c,$d) = ( $part_pkg_taxproduct
1075                          ? ( split ':', $part_pkg_taxproduct->taxproduct )
1076                          : ()
1077                      );
1078   $a = '' unless $a; $b = '' unless $b; $c = '' unless $c; $d = '' unless $d;
1079   my $extra_sql = "AND ( taxproduct = '$a:$b:$c:$d'
1080                       OR taxproduct = '$a:$b:$c:'
1081                       OR taxproduct = '$a:$b:".":$d'
1082                       OR taxproduct = '$a:$b:".":' )";
1083   map { $_->taxproductnum } qsearch( { 'table'     => 'part_pkg_taxproduct',
1084                                        'hashref'   => { 'data_vendor'=>'cch' },
1085                                        'extra_sql' => $extra_sql,
1086                                    } );
1087                                      
1088 }
1089
1090 sub part_pkg_taxrate {
1091   my $self = shift;
1092   my ($data_vendor, $geocode, $class) = @_;
1093
1094   my $dbh = dbh;
1095   my $extra_sql = 'WHERE part_pkg_taxproduct.data_vendor = '.
1096                   dbh->quote($data_vendor);
1097   
1098   # CCH oddness in m2m
1099   $extra_sql .= ' AND ('.
1100     join(' OR ', map{ 'geocode = '. $dbh->quote(substr($geocode, 0, $_)) }
1101                  qw(10 5 2)
1102         ).
1103     ')';
1104   # much more CCH oddness in m2m -- this is kludgy
1105   my @tpnums = $self->_expand_cch_taxproductnum($class);
1106   if (scalar(@tpnums)) {
1107     $extra_sql .= ' AND ('.
1108                             join(' OR ', map{ "taxproductnum = $_" } @tpnums ).
1109                        ')';
1110   } else {
1111     $extra_sql .= ' AND ( 0 = 1 )';
1112   }
1113
1114   my $addl_from = 'LEFT JOIN part_pkg_taxproduct USING ( taxproductnum )';
1115   my $order_by = 'ORDER BY taxclassnum, length(geocode) desc, length(taxproduct) desc';
1116   my $select   = 'DISTINCT ON(taxclassnum) *, taxproduct';
1117
1118   # should qsearch preface columns with the table to facilitate joins?
1119   qsearch( { 'table'     => 'part_pkg_taxrate',
1120              'select'    => $select,
1121              'hashref'   => { # 'data_vendor'   => $data_vendor,
1122                               # 'taxproductnum' => $self->taxproductnum,
1123                             },
1124              'addl_from' => $addl_from,
1125              'extra_sql' => $extra_sql,
1126              'order_by'  => $order_by,
1127          } );
1128 }
1129
1130 =item part_pkg_discount
1131
1132 Returns the package to discount m2m records (see L<FS::part_pkg_discount>)
1133 for this package.
1134
1135 =cut
1136
1137 sub part_pkg_discount {
1138   my $self = shift;
1139   qsearch('part_pkg_discount', { 'pkgpart' => $self->pkgpart });
1140 }
1141
1142 =item _rebless
1143
1144 Reblesses the object into the FS::part_pkg::PLAN class (if available), where
1145 PLAN is the object's I<plan> field.  There should be better docs
1146 on how to create new price plans, but until then, see L</NEW PLAN CLASSES>.
1147
1148 =cut
1149
1150 sub _rebless {
1151   my $self = shift;
1152   my $plan = $self->plan;
1153   unless ( $plan ) {
1154     cluck "no price plan found for pkgpart ". $self->pkgpart. "\n"
1155       if $DEBUG;
1156     return $self;
1157   }
1158   return $self if ref($self) =~ /::$plan$/; #already blessed into plan subclass
1159   my $class = ref($self). "::$plan";
1160   warn "reblessing $self into $class" if $DEBUG;
1161   eval "use $class;";
1162   die $@ if $@;
1163   bless($self, $class) unless $@;
1164   $self;
1165 }
1166
1167 #fallbacks that eval the setup and recur fields, for backwards compat
1168
1169 sub calc_setup {
1170   my $self = shift;
1171   warn 'no price plan class for '. $self->plan. ", eval-ing setup\n";
1172   $self->_calc_eval('setup', @_);
1173 }
1174
1175 sub calc_recur {
1176   my $self = shift;
1177   warn 'no price plan class for '. $self->plan. ", eval-ing recur\n";
1178   $self->_calc_eval('recur', @_);
1179 }
1180
1181 use vars qw( $sdate @details );
1182 sub _calc_eval {
1183   #my( $self, $field, $cust_pkg ) = @_;
1184   my( $self, $field, $cust_pkg, $sdateref, $detailsref ) = @_;
1185   *sdate = $sdateref;
1186   *details = $detailsref;
1187   $self->$field() =~ /^(.*)$/
1188     or die "Illegal $field (pkgpart ". $self->pkgpart. '): '.
1189             $self->$field(). "\n";
1190   my $prog = $1;
1191   return 0 if $prog =~ /^\s*$/;
1192   my $value = eval $prog;
1193   die $@ if $@;
1194   $value;
1195 }
1196
1197 #fallback that return 0 for old legacy packages with no plan
1198
1199 sub calc_remain { 0; }
1200 sub calc_cancel { 0; }
1201 sub calc_units  { 0; }
1202
1203 #fallback for everything except bulk.pm
1204 sub hide_svc_detail { 0; }
1205
1206 =item recur_cost_permonth CUST_PKG
1207
1208 recur_cost divided by freq (only supported for monthly and longer frequencies)
1209
1210 =cut
1211
1212 sub recur_cost_permonth {
1213   my($self, $cust_pkg) = @_;
1214   return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
1215   sprintf('%.2f', $self->recur_cost / $self->freq );
1216 }
1217
1218 =item format OPTION DATA
1219
1220 Returns data formatted according to the function 'format' described
1221 in the plan info.  Returns DATA if no such function exists.
1222
1223 =cut
1224
1225 sub format {
1226   my ($self, $option, $data) = (shift, shift, shift);
1227   if (exists($plans{$self->plan}->{fields}->{$option}{format})) {
1228     &{$plans{$self->plan}->{fields}->{$option}{format}}($data);
1229   }else{
1230     $data;
1231   }
1232 }
1233
1234 =item parse OPTION DATA
1235
1236 Returns data parsed according to the function 'parse' described
1237 in the plan info.  Returns DATA if no such function exists.
1238
1239 =cut
1240
1241 sub parse {
1242   my ($self, $option, $data) = (shift, shift, shift);
1243   if (exists($plans{$self->plan}->{fields}->{$option}{parse})) {
1244     &{$plans{$self->plan}->{fields}->{$option}{parse}}($data);
1245   }else{
1246     $data;
1247   }
1248 }
1249
1250 =back
1251
1252 =cut
1253
1254 =head1 CLASS METHODS
1255
1256 =over 4
1257
1258 =cut
1259
1260 # _upgrade_data
1261 #
1262 # Used by FS::Upgrade to migrate to a new database.
1263
1264 sub _upgrade_data { # class method
1265   my($class, %opts) = @_;
1266
1267   warn "[FS::part_pkg] upgrading $class\n" if $DEBUG;
1268
1269   my @part_pkg = qsearch({
1270     'table'     => 'part_pkg',
1271     'extra_sql' => "WHERE ". join(' OR ',
1272                      ( map "($_ IS NOT NULL AND $_ != '' )",
1273                            qw( plandata setup recur ) ),
1274                      'plan IS NULL', "plan = '' ",
1275                    ),
1276   });
1277
1278   foreach my $part_pkg (@part_pkg) {
1279
1280     unless ( $part_pkg->plan ) {
1281       $part_pkg->plan('flat');
1282     }
1283
1284     if ( length($part_pkg->option('setup_fee')) == 0 
1285          && $part_pkg->setup =~ /^\s*([\d\.]+)\s*$/ ) {
1286
1287       my $opt = new FS::part_pkg_option {
1288         'pkgpart'     => $part_pkg->pkgpart,
1289         'optionname'  => 'setup_fee',
1290         'optionvalue' => $1,
1291       };
1292       my $error = $opt->insert;
1293       die $error if $error;
1294
1295
1296       #} else {
1297       #  die "Can't parse part_pkg.setup for fee; convert pkgnum ".
1298       #      $part_pkg->pkgnum. " manually: ". $part_pkg->setup. "\n";
1299     }
1300     $part_pkg->setup('');
1301
1302     if ( length($part_pkg->option('recur_fee')) == 0
1303          && $part_pkg->recur =~ /^\s*([\d\.]+)\s*$/ ) {
1304
1305         my $opt = new FS::part_pkg_option {
1306           'pkgpart'     => $part_pkg->pkgpart,
1307           'optionname'  => 'recur_fee',
1308           'optionvalue' => $1,
1309         };
1310         my $error = $opt->insert;
1311         die $error if $error;
1312
1313
1314       #} else {
1315       #  die "Can't parse part_pkg.setup for fee; convert pkgnum ".
1316       #      $part_pkg->pkgnum. " manually: ". $part_pkg->setup. "\n";
1317     }
1318     $part_pkg->recur('');
1319
1320     $part_pkg->replace; #this should take care of plandata, right?
1321
1322   }
1323
1324   # now upgrade to the explicit custom flag
1325
1326   @part_pkg = qsearch({
1327     'table'     => 'part_pkg',
1328     'hashref'   => { disabled => 'Y', custom => '' },
1329     'extra_sql' => "AND comment LIKE '(CUSTOM) %'",
1330   });
1331
1332   foreach my $part_pkg (@part_pkg) {
1333     my $new = new FS::part_pkg { $part_pkg->hash };
1334     $new->custom('Y');
1335     my $comment = $part_pkg->comment;
1336     $comment =~ s/^\(CUSTOM\) //;
1337     $comment = '(none)' unless $comment =~ /\S/;
1338     $new->comment($comment);
1339
1340     my $pkg_svc = { map { $_->svcpart => $_->quantity } $part_pkg->pkg_svc };
1341     my $primary = $part_pkg->svcpart;
1342     my $options = { $part_pkg->options };
1343
1344     my $error = $new->replace( $part_pkg,
1345                                'pkg_svc'     => $pkg_svc,
1346                                'primary_svc' => $primary,
1347                                'options'     => $options,
1348                              );
1349     die $error if $error;
1350   }
1351
1352 }
1353
1354 =item curuser_pkgs_sql
1355
1356 Returns an SQL fragment for searching for packages the current user can
1357 use, either via part_pkg.agentnum directly, or via agent type (see
1358 L<FS::type_pkgs>).
1359
1360 =cut
1361
1362 sub curuser_pkgs_sql {
1363   my $class = shift;
1364
1365   $class->_pkgs_sql( $FS::CurrentUser::CurrentUser->agentnums );
1366
1367 }
1368
1369 =item agent_pkgs_sql AGENT | AGENTNUM, ...
1370
1371 Returns an SQL fragment for searching for packages the provided agent or agents
1372 can use, either via part_pkg.agentnum directly, or via agent type (see
1373 L<FS::type_pkgs>).
1374
1375 =cut
1376
1377 sub agent_pkgs_sql {
1378   my $class = shift;  #i'm a class method, not a sub (the question is... why??)
1379   my @agentnums = map { ref($_) ? $_->agentnum : $_ } @_;
1380
1381   $class->_pkgs_sql(@agentnums); #is this why
1382
1383 }
1384
1385 sub _pkgs_sql {
1386   my( $class, @agentnums ) = @_;
1387   my $agentnums = join(',', @agentnums);
1388
1389   "
1390     (
1391       ( agentnum IS NOT NULL AND agentnum IN ($agentnums) )
1392       OR ( agentnum IS NULL
1393            AND EXISTS ( SELECT 1
1394                           FROM type_pkgs
1395                             LEFT JOIN agent_type USING ( typenum )
1396                             LEFT JOIN agent AS typeagent USING ( typenum )
1397                           WHERE type_pkgs.pkgpart = part_pkg.pkgpart
1398                             AND typeagent.agentnum IN ($agentnums)
1399                       )
1400          )
1401     )
1402   ";
1403
1404 }
1405
1406 =back
1407
1408 =head1 SUBROUTINES
1409
1410 =over 4
1411
1412 =item plan_info
1413
1414 =cut
1415
1416 #false laziness w/part_export & cdr
1417 my %info;
1418 foreach my $INC ( @INC ) {
1419   warn "globbing $INC/FS/part_pkg/*.pm\n" if $DEBUG;
1420   foreach my $file ( glob("$INC/FS/part_pkg/*.pm") ) {
1421     warn "attempting to load plan info from $file\n" if $DEBUG;
1422     $file =~ /\/(\w+)\.pm$/ or do {
1423       warn "unrecognized file in $INC/FS/part_pkg/: $file\n";
1424       next;
1425     };
1426     my $mod = $1;
1427     my $info = eval "use FS::part_pkg::$mod; ".
1428                     "\\%FS::part_pkg::$mod\::info;";
1429     if ( $@ ) {
1430       die "error using FS::part_pkg::$mod (skipping): $@\n" if $@;
1431       next;
1432     }
1433     unless ( keys %$info ) {
1434       warn "no %info hash found in FS::part_pkg::$mod, skipping\n";
1435       next;
1436     }
1437     warn "got plan info from FS::part_pkg::$mod: $info\n" if $DEBUG;
1438     if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
1439       warn "skipping disabled plan FS::part_pkg::$mod" if $DEBUG;
1440       next;
1441     }
1442     $info{$mod} = $info;
1443   }
1444 }
1445
1446 tie %plans, 'Tie::IxHash',
1447   map  { $_ => $info{$_} }
1448   sort { $info{$a}->{'weight'} <=> $info{$b}->{'weight'} }
1449   keys %info;
1450
1451 sub plan_info {
1452   \%plans;
1453 }
1454
1455
1456 =back
1457
1458 =head1 NEW PLAN CLASSES
1459
1460 A module should be added in FS/FS/part_pkg/  Eventually, an example may be
1461 found in eg/plan_template.pm.  Until then, it is suggested that you use the
1462 other modules in FS/FS/part_pkg/ as a guide.
1463
1464 =head1 BUGS
1465
1466 The delete method is unimplemented.
1467
1468 setup and recur semantics are not yet defined (and are implemented in
1469 FS::cust_bill.  hmm.).  now they're deprecated and need to go.
1470
1471 plandata should go
1472
1473 part_pkg_taxrate is Pg specific
1474
1475 replace should be smarter about managing the related tables (options, pkg_svc)
1476
1477 =head1 SEE ALSO
1478
1479 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
1480 schema.html from the base documentation.
1481
1482 =cut
1483
1484 1;
1485