ACLs: finish group edit (agents + rights) & browse
[freeside.git] / FS / FS / part_pkg.pm
1 package FS::part_pkg;
2
3 use strict;
4 use vars qw( @ISA %plans $DEBUG );
5 use Carp qw(carp cluck confess);
6 use Tie::IxHash;
7 use FS::Conf;
8 use FS::Record qw( qsearch qsearchs dbh dbdef );
9 use FS::pkg_svc;
10 use FS::part_svc;
11 use FS::cust_pkg;
12 use FS::agent_type;
13 use FS::type_pkgs;
14 use FS::part_pkg_option;
15 use FS::pkg_class;
16
17 @ISA = qw( FS::Record ); # FS::option_Common ); # this can use option_Common
18                                                 # when all the plandata bs is
19                                                 # gone
20
21 $DEBUG = 0;
22
23 =head1 NAME
24
25 FS::part_pkg - Object methods for part_pkg objects
26
27 =head1 SYNOPSIS
28
29   use FS::part_pkg;
30
31   $record = new FS::part_pkg \%hash
32   $record = new FS::part_pkg { 'column' => 'value' };
33
34   $custom_record = $template_record->clone;
35
36   $error = $record->insert;
37
38   $error = $new_record->replace($old_record);
39
40   $error = $record->delete;
41
42   $error = $record->check;
43
44   @pkg_svc = $record->pkg_svc;
45
46   $svcnum = $record->svcpart;
47   $svcnum = $record->svcpart( 'svc_acct' );
48
49 =head1 DESCRIPTION
50
51 An FS::part_pkg object represents a package definition.  FS::part_pkg
52 inherits from FS::Record.  The following fields are currently supported:
53
54 =over 4
55
56 =item pkgpart - primary key (assigned automatically for new package definitions)
57
58 =item pkg - Text name of this package definition (customer-viewable)
59
60 =item comment - Text name of this package definition (non-customer-viewable)
61
62 =item classnum - Optional package class (see L<FS::pkg_class>)
63
64 =item promo_code - Promotional code
65
66 =item setup - Setup fee expression (deprecated)
67
68 =item freq - Frequency of recurring fee
69
70 =item recur - Recurring fee expression (deprecated)
71
72 =item setuptax - Setup fee tax exempt flag, empty or `Y'
73
74 =item recurtax - Recurring fee tax exempt flag, empty or `Y'
75
76 =item taxclass - Tax class 
77
78 =item plan - Price plan
79
80 =item plandata - Price plan data (deprecated - see L<FS::part_pkg_option> instead)
81
82 =item disabled - Disabled flag, empty or `Y'
83
84 =back
85
86 =head1 METHODS
87
88 =over 4 
89
90 =item new HASHREF
91
92 Creates a new package definition.  To add the package definition to
93 the database, see L<"insert">.
94
95 =cut
96
97 sub table { 'part_pkg'; }
98
99 =item clone
100
101 An alternate constructor.  Creates a new package definition by duplicating
102 an existing definition.  A new pkgpart is assigned and `(CUSTOM) ' is prepended
103 to the comment field.  To add the package definition to the database, see
104 L<"insert">.
105
106 =cut
107
108 sub clone {
109   my $self = shift;
110   my $class = ref($self);
111   my %hash = $self->hash;
112   $hash{'pkgpart'} = '';
113   $hash{'comment'} = "(CUSTOM) ". $hash{'comment'}
114     unless $hash{'comment'} =~ /^\(CUSTOM\) /;
115   #new FS::part_pkg ( \%hash ); # ?
116   new $class ( \%hash ); # ?
117 }
118
119 =item insert [ , OPTION => VALUE ... ]
120
121 Adds this package definition to the database.  If there is an error,
122 returns the error, otherwise returns false.
123
124 Currently available options are: I<pkg_svc>, I<primary_svc>, I<cust_pkg>, 
125 I<custnum_ref> and I<options>.
126
127 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
128 values, appropriate FS::pkg_svc records will be inserted.
129
130 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
131 FS::pkg_svc record will be updated.
132
133 If I<cust_pkg> is set to a pkgnum of a FS::cust_pkg record (or the FS::cust_pkg
134 record itself), the object will be updated to point to this package definition.
135
136 In conjunction with I<cust_pkg>, if I<custnum_ref> is set to a scalar reference,
137 the scalar will be updated with the custnum value from the cust_pkg record.
138
139 If I<options> is set to a hashref of options, appropriate FS::part_pkg_option
140 records will be inserted.
141
142 =cut
143
144 sub insert {
145   my $self = shift;
146   my %options = @_;
147   warn "FS::part_pkg::insert called on $self with options ".
148        join(', ', map "$_=>$options{$_}", keys %options)
149     if $DEBUG;
150
151   local $SIG{HUP} = 'IGNORE';
152   local $SIG{INT} = 'IGNORE';
153   local $SIG{QUIT} = 'IGNORE';
154   local $SIG{TERM} = 'IGNORE';
155   local $SIG{TSTP} = 'IGNORE';
156   local $SIG{PIPE} = 'IGNORE';
157
158   my $oldAutoCommit = $FS::UID::AutoCommit;
159   local $FS::UID::AutoCommit = 0;
160   my $dbh = dbh;
161
162   warn "  saving legacy plandata" if $DEBUG;
163   my $plandata = $self->get('plandata');
164   $self->set('plandata', '');
165
166   warn "  inserting part_pkg record" if $DEBUG;
167   my $error = $self->SUPER::insert;
168   if ( $error ) {
169     $dbh->rollback if $oldAutoCommit;
170     return $error;
171   }
172
173   if ( $plandata ) {
174
175     warn "  inserting part_pkg_option records for plandata" if $DEBUG;
176     foreach my $part_pkg_option ( 
177       map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
178                                    return "illegal plandata: $plandata";
179                                  };
180             new FS::part_pkg_option {
181               'pkgpart'     => $self->pkgpart,
182               'optionname'  => $1,
183               'optionvalue' => $2,
184             };
185           }
186       split("\n", $plandata)
187     ) {
188       my $error = $part_pkg_option->insert;
189       if ( $error ) {
190         $dbh->rollback if $oldAutoCommit;
191         return $error;
192       }
193     }
194
195   } elsif ( $options{'options'} ) {
196
197     warn "  inserting part_pkg_option records for options hashref" if $DEBUG;
198     foreach my $optionname ( keys %{$options{'options'}} ) {
199
200       my $part_pkg_option =
201         new FS::part_pkg_option {
202           'pkgpart'     => $self->pkgpart,
203           'optionname'  => $optionname,
204           'optionvalue' => $options{'options'}->{$optionname},
205         };
206
207       my $error = $part_pkg_option->insert;
208       if ( $error ) {
209         $dbh->rollback if $oldAutoCommit;
210         return $error;
211       }
212
213     }
214
215   }
216
217   my $conf = new FS::Conf;
218   if ( $conf->exists('agent_defaultpkg') ) {
219     warn "  agent_defaultpkg set; allowing all agents to purchase package"
220       if $DEBUG;
221     foreach my $agent_type ( qsearch('agent_type', {} ) ) {
222       my $type_pkgs = new FS::type_pkgs({
223         'typenum' => $agent_type->typenum,
224         'pkgpart' => $self->pkgpart,
225       });
226       my $error = $type_pkgs->insert;
227       if ( $error ) {
228         $dbh->rollback if $oldAutoCommit;
229         return $error;
230       }
231     }
232   }
233
234   warn "  inserting pkg_svc records" if $DEBUG;
235   my $pkg_svc = $options{'pkg_svc'} || {};
236   foreach my $part_svc ( qsearch('part_svc', {} ) ) {
237     my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
238     my $primary_svc =
239       ( $options{'primary_svc'} && $options{'primary_svc'}==$part_svc->svcpart )
240         ? 'Y'
241         : '';
242
243     my $pkg_svc = new FS::pkg_svc( {
244       'pkgpart'     => $self->pkgpart,
245       'svcpart'     => $part_svc->svcpart,
246       'quantity'    => $quantity, 
247       'primary_svc' => $primary_svc,
248     } );
249     my $error = $pkg_svc->insert;
250     if ( $error ) {
251       $dbh->rollback if $oldAutoCommit;
252       return $error;
253     }
254   }
255
256   if ( $options{'cust_pkg'} ) {
257     warn "  updating cust_pkg record " if $DEBUG;
258     my $old_cust_pkg =
259       ref($options{'cust_pkg'})
260         ? $options{'cust_pkg'}
261         : qsearchs('cust_pkg', { pkgnum => $options{'cust_pkg'} } );
262     ${ $options{'custnum_ref'} } = $old_cust_pkg->custnum
263       if $options{'custnum_ref'};
264     my %hash = $old_cust_pkg->hash;
265     $hash{'pkgpart'} = $self->pkgpart,
266     my $new_cust_pkg = new FS::cust_pkg \%hash;
267     local($FS::cust_pkg::disable_agentcheck) = 1;
268     my $error = $new_cust_pkg->replace($old_cust_pkg);
269     if ( $error ) {
270       $dbh->rollback if $oldAutoCommit;
271       return "Error modifying cust_pkg record: $error";
272     }
273   }
274
275   warn "  commiting transaction" if $DEBUG;
276   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
277
278   '';
279 }
280
281 =item delete
282
283 Currently unimplemented.
284
285 =cut
286
287 sub delete {
288   return "Can't (yet?) delete package definitions.";
289 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
290 }
291
292 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
293
294 Replaces OLD_RECORD with this one in the database.  If there is an error,
295 returns the error, otherwise returns false.
296
297 Currently available options are: I<pkg_svc> and I<primary_svc>
298
299 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
300 values, the appropriate FS::pkg_svc records will be replace.
301
302 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
303 FS::pkg_svc record will be updated.
304
305 =cut
306
307 sub replace {
308   my( $new, $old ) = ( shift, shift );
309   my %options = @_;
310   warn "FS::part_pkg::replace called on $new to replace $old ".
311        "with options %options"
312     if $DEBUG;
313
314   local $SIG{HUP} = 'IGNORE';
315   local $SIG{INT} = 'IGNORE';
316   local $SIG{QUIT} = 'IGNORE';
317   local $SIG{TERM} = 'IGNORE';
318   local $SIG{TSTP} = 'IGNORE';
319   local $SIG{PIPE} = 'IGNORE';
320
321   my $oldAutoCommit = $FS::UID::AutoCommit;
322   local $FS::UID::AutoCommit = 0;
323   my $dbh = dbh;
324
325   warn "  saving legacy plandata" if $DEBUG;
326   my $plandata = $new->get('plandata');
327   $new->set('plandata', '');
328
329   warn "  deleting old part_pkg_option records" if $DEBUG;
330   foreach my $part_pkg_option ( $old->part_pkg_option ) {
331     my $error = $part_pkg_option->delete;
332     if ( $error ) {
333       $dbh->rollback if $oldAutoCommit;
334       return $error;
335     }
336   }
337
338   warn "  replacing part_pkg record" if $DEBUG;
339   my $error = $new->SUPER::replace($old);
340   if ( $error ) {
341     $dbh->rollback if $oldAutoCommit;
342     return $error;
343   }
344
345   warn "  inserting part_pkg_option records for plandata" if $DEBUG;
346   foreach my $part_pkg_option ( 
347     map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
348                                  return "illegal plandata: $plandata";
349                                };
350           new FS::part_pkg_option {
351             'pkgpart'     => $new->pkgpart,
352             'optionname'  => $1,
353             'optionvalue' => $2,
354           };
355         }
356     split("\n", $plandata)
357   ) {
358     my $error = $part_pkg_option->insert;
359     if ( $error ) {
360       $dbh->rollback if $oldAutoCommit;
361       return $error;
362     }
363   }
364
365   warn "  replacing pkg_svc records" if $DEBUG;
366   my $pkg_svc = $options{'pkg_svc'} || {};
367   foreach my $part_svc ( qsearch('part_svc', {} ) ) {
368     my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
369     my $primary_svc = $options{'primary_svc'} == $part_svc->svcpart ? 'Y' : '';
370
371     my $old_pkg_svc = qsearchs('pkg_svc', {
372       'pkgpart' => $old->pkgpart,
373       'svcpart' => $part_svc->svcpart,
374     } );
375     my $old_quantity = $old_pkg_svc ? $old_pkg_svc->quantity : 0;
376     my $old_primary_svc =
377       ( $old_pkg_svc && $old_pkg_svc->dbdef_table->column('primary_svc') )
378         ? $old_pkg_svc->primary_svc
379         : '';
380     next unless $old_quantity != $quantity || $old_primary_svc ne $primary_svc;
381   
382     my $new_pkg_svc = new FS::pkg_svc( {
383       'pkgsvcnum'   => ( $old_pkg_svc ? $old_pkg_svc->pkgsvcnum : '' ),
384       'pkgpart'     => $new->pkgpart,
385       'svcpart'     => $part_svc->svcpart,
386       'quantity'    => $quantity, 
387       'primary_svc' => $primary_svc,
388     } );
389     my $error = $old_pkg_svc
390                   ? $new_pkg_svc->replace($old_pkg_svc)
391                   : $new_pkg_svc->insert;
392     if ( $error ) {
393       $dbh->rollback if $oldAutoCommit;
394       return $error;
395     }
396   }
397
398   warn "  commiting transaction" if $DEBUG;
399   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
400   '';
401 }
402
403 =item check
404
405 Checks all fields to make sure this is a valid package definition.  If
406 there is an error, returns the error, otherwise returns false.  Called by the
407 insert and replace methods.
408
409 =cut
410
411 sub check {
412   my $self = shift;
413   warn "FS::part_pkg::check called on $self" if $DEBUG;
414
415   for (qw(setup recur plandata)) {
416     #$self->set($_=>0) if $self->get($_) =~ /^\s*$/; }
417     return "Use of $_ field is deprecated; set a plan and options"
418       if length($self->get($_));
419     $self->set($_, '');
420   }
421
422   if ( $self->dbdef_table->column('freq')->type =~ /(int)/i ) {
423     my $error = $self->ut_number('freq');
424     return $error if $error;
425   } else {
426     $self->freq =~ /^(\d+[hdw]?)$/
427       or return "Illegal or empty freq: ". $self->freq;
428     $self->freq($1);
429   }
430
431   my $error = $self->ut_numbern('pkgpart')
432     || $self->ut_text('pkg')
433     || $self->ut_text('comment')
434     || $self->ut_textn('promo_code')
435     || $self->ut_alphan('plan')
436     || $self->ut_enum('setuptax', [ '', 'Y' ] )
437     || $self->ut_enum('recurtax', [ '', 'Y' ] )
438     || $self->ut_textn('taxclass')
439     || $self->ut_enum('disabled', [ '', 'Y' ] )
440     || $self->SUPER::check
441   ;
442   return $error if $error;
443
444   if ( $self->classnum !~ /^$/ ) {
445     my $error = $self->ut_foreign_key('classnum', 'pkg_class', 'classnum');
446     return $error if $error;
447   } else {
448     $self->classnum('');
449   }
450
451   return 'Unknown plan '. $self->plan
452     unless exists($plans{$self->plan});
453
454   my $conf = new FS::Conf;
455   return 'Taxclass is required'
456     if ! $self->taxclass && $conf->exists('require_taxclasses');
457
458   '';
459 }
460
461 =item pkg_class
462
463 Returns the package class, as an FS::pkg_class object, or the empty string
464 if there is no package class.
465
466 =cut
467
468 sub pkg_class {
469   my $self = shift;
470   if ( $self->classnum ) {
471     qsearchs('pkg_class', { 'classnum' => $self->classnum } );
472   } else {
473     return '';
474   }
475 }
476
477 =item classname 
478
479 Returns the package class name, or the empty string if there is no package
480 class.
481
482 =cut
483
484 sub classname {
485   my $self = shift;
486   my $pkg_class = $self->pkg_class;
487   $pkg_class
488     ? $pkg_class->classname
489     : '';
490 }
491
492 =item pkg_svc
493
494 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
495 definition (with non-zero quantity).
496
497 =cut
498
499 sub pkg_svc {
500   my $self = shift;
501   #sort { $b->primary cmp $a->primary } 
502     grep { $_->quantity }
503       qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
504 }
505
506 =item svcpart [ SVCDB ]
507
508 Returns the svcpart of the primary service definition (see L<FS::part_svc>)
509 associated with this package definition (see L<FS::pkg_svc>).  Returns
510 false if there not a primary service definition or exactly one service
511 definition with quantity 1, or if SVCDB is specified and does not match the
512 svcdb of the service definition, 
513
514 =cut
515
516 sub svcpart {
517   my $self = shift;
518   my $svcdb = scalar(@_) ? shift : '';
519   my @svcdb_pkg_svc =
520     grep { ( $svcdb eq $_->part_svc->svcdb || !$svcdb ) } $self->pkg_svc;
521   my @pkg_svc = ();
522   @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc
523     if dbdef->table('pkg_svc')->column('primary_svc');
524   @pkg_svc = grep {$_->quantity == 1 } @svcdb_pkg_svc
525     unless @pkg_svc;
526   return '' if scalar(@pkg_svc) != 1;
527   $pkg_svc[0]->svcpart;
528 }
529
530 =item payby
531
532 Returns a list of the acceptable payment types for this package.  Eventually
533 this should come out of a database table and be editable, but currently has the
534 following logic instead:
535
536 If the package is free, the single item B<BILL> is
537 returned, otherwise, the single item B<CARD> is returned.
538
539 (CHEK?  LEC?  Probably shouldn't accept those by default, prone to abuse)
540
541 =cut
542
543 sub payby {
544   my $self = shift;
545   if ( $self->is_free ) {
546     ( 'BILL' );
547   } else {
548     ( 'CARD' );
549   }
550 }
551
552 =item is_free
553
554 Returns true if this package is free.  
555
556 =cut
557
558 sub is_free {
559   my $self = shift;
560   unless ( $self->plan ) {
561     $self->setup =~ /^\s*0+(\.0*)?\s*$/
562       && $self->recur =~ /^\s*0+(\.0*)?\s*$/;
563   } elsif ( $self->can('is_free_options') ) {
564     not grep { $_ !~ /^\s*0*(\.0*)?\s*$/ }
565          map { $self->option($_) } 
566              $self->is_free_options;
567   } else {
568     warn "FS::part_pkg::is_free: FS::part_pkg::". $self->plan. " subclass ".
569          "provides neither is_free_options nor is_free method; returning false";
570     0;
571   }
572 }
573
574
575 sub freqs_href {
576   #method, class method or sub? #my $self = shift;
577
578   tie my %freq, 'Tie::IxHash', 
579     '0'  => '(no recurring fee)',
580     '1h' => 'hourly',
581     '1d' => 'daily',
582     '1w' => 'weekly',
583     '2w' => 'biweekly (every 2 weeks)',
584     '1'  => 'monthly',
585     '2'  => 'bimonthly (every 2 months)',
586     '3'  => 'quarterly (every 3 months)',
587     '6'  => 'semiannually (every 6 months)',
588     '12' => 'annually',
589     '24' => 'biannually (every 2 years)',
590     '36' => 'triannually (every 3 years)',
591     '48' => '(every 4 years)',
592     '60' => '(every 5 years)',
593     '120' => '(every 10 years)',
594   ;
595
596   \%freq;
597
598 }
599
600 =item freq_pretty
601
602 Returns an english representation of the I<freq> field, such as "monthly",
603 "weekly", "semi-annually", etc.
604
605 =cut
606
607 sub freq_pretty {
608   my $self = shift;
609   my $freq = $self->freq;
610
611   my $freqs_href = $self->freqs_href;
612
613   if ( exists($freqs_href->{$freq}) ) {
614     $freqs_href->{$freq};
615   } else {
616     my $interval = 'month';
617     if ( $freq =~ /^(\d+)([hdw])$/ ) {
618       my %interval = ( 'h' => 'hour', 'd'=>'day', 'w'=>'week' );
619       $interval = $interval{$2};
620     }
621     if ( $1 == 1 ) {
622       "every $interval";
623     } else {
624       "every $freq ${interval}s";
625     }
626   }
627 }
628
629 =item plandata
630
631 For backwards compatibility, returns the plandata field as well as all options
632 from FS::part_pkg_option.
633
634 =cut
635
636 sub plandata {
637   my $self = shift;
638   carp "plandata is deprecated";
639   if ( @_ ) {
640     $self->SUPER::plandata(@_);
641   } else {
642     my $plandata = $self->get('plandata');
643     my %options = $self->options;
644     $plandata .= join('', map { "$_=$options{$_}\n" } keys %options );
645     $plandata;
646   }
647 }
648
649 =item part_pkg_option
650
651 Returns all options as FS::part_pkg_option objects (see
652 L<FS::part_pkg_option>).
653
654 =cut
655
656 sub part_pkg_option {
657   my $self = shift;
658   qsearch('part_pkg_option', { 'pkgpart' => $self->pkgpart } );
659 }
660
661 =item options 
662
663 Returns a list of option names and values suitable for assigning to a hash.
664
665 =cut
666
667 sub options {
668   my $self = shift;
669   map { $_->optionname => $_->optionvalue } $self->part_pkg_option;
670 }
671
672 =item option OPTIONNAME
673
674 Returns the option value for the given name, or the empty string.
675
676 =cut
677
678 sub option {
679   my( $self, $opt, $ornull ) = @_;
680   my $part_pkg_option =
681     qsearchs('part_pkg_option', {
682       pkgpart    => $self->pkgpart,
683       optionname => $opt,
684   } );
685   return $part_pkg_option->optionvalue if $part_pkg_option;
686   my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
687                      split("\n", $self->get('plandata') );
688   return $plandata{$opt} if exists $plandata{$opt};
689   cluck "Package definition option $opt not found in options or plandata!\n"
690     unless $ornull;
691   '';
692 }
693
694 =item _rebless
695
696 Reblesses the object into the FS::part_pkg::PLAN class (if available), where
697 PLAN is the object's I<plan> field.  There should be better docs
698 on how to create new price plans, but until then, see L</NEW PLAN CLASSES>.
699
700 =cut
701
702 sub _rebless {
703   my $self = shift;
704   my $plan = $self->plan;
705   unless ( $plan ) {
706     confess "no price plan found for pkgpart ". $self->pkgpart. "\n"
707       if $DEBUG;
708     return $self;
709   }
710   return $self if ref($self) =~ /::$plan$/; #already blessed into plan subclass
711   my $class = ref($self). "::$plan";
712   warn "reblessing $self into $class" if $DEBUG;
713   eval "use $class;";
714   die $@ if $@;
715   bless($self, $class) unless $@;
716   $self;
717 }
718
719 #fallbacks that eval the setup and recur fields, for backwards compat
720
721 sub calc_setup {
722   my $self = shift;
723   warn 'no price plan class for '. $self->plan. ", eval-ing setup\n";
724   $self->_calc_eval('setup', @_);
725 }
726
727 sub calc_recur {
728   my $self = shift;
729   warn 'no price plan class for '. $self->plan. ", eval-ing recur\n";
730   $self->_calc_eval('recur', @_);
731 }
732
733 use vars qw( $sdate @details );
734 sub _calc_eval {
735   #my( $self, $field, $cust_pkg ) = @_;
736   my( $self, $field, $cust_pkg, $sdateref, $detailsref ) = @_;
737   *sdate = $sdateref;
738   *details = $detailsref;
739   $self->$field() =~ /^(.*)$/
740     or die "Illegal $field (pkgpart ". $self->pkgpart. '): '.
741             $self->$field(). "\n";
742   my $prog = $1;
743   return 0 if $prog =~ /^\s*$/;
744   my $value = eval $prog;
745   die $@ if $@;
746   $value;
747 }
748
749 #fallback that return 0 for old legacy packages with no plan
750
751 sub calc_remain { 0; }
752 sub calc_cancel { 0; }
753
754 =back
755
756 =head1 SUBROUTINES
757
758 =over 4
759
760 =item plan_info
761
762 =cut
763
764 my %info;
765 foreach my $INC ( @INC ) {
766   warn "globbing $INC/FS/part_pkg/*.pm\n" if $DEBUG;
767   foreach my $file ( glob("$INC/FS/part_pkg/*.pm") ) {
768     warn "attempting to load plan info from $file\n" if $DEBUG;
769     $file =~ /\/(\w+)\.pm$/ or do {
770       warn "unrecognized file in $INC/FS/part_pkg/: $file\n";
771       next;
772     };
773     my $mod = $1;
774     my $info = eval "use FS::part_pkg::$mod; ".
775                     "\\%FS::part_pkg::$mod\::info;";
776     if ( $@ ) {
777       die "error using FS::part_pkg::$mod (skipping): $@\n" if $@;
778       next;
779     }
780     unless ( keys %$info ) {
781       warn "no %info hash found in FS::part_pkg::$mod, skipping\n"
782         unless $mod =~ /^(passwdfile|null)$/; #hack but what the heck
783       next;
784     }
785     warn "got plan info from FS::part_pkg::$mod: $info\n" if $DEBUG;
786     if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
787       warn "skipping disabled plan FS::part_pkg::$mod" if $DEBUG;
788       next;
789     }
790     $info{$mod} = $info;
791   }
792 }
793
794 tie %plans, 'Tie::IxHash',
795   map { $_ => $info{$_} }
796   sort { $info{$a}->{'weight'} <=> $info{$b}->{'weight'} }
797   keys %info;
798
799 sub plan_info {
800   \%plans;
801 }
802
803 =back
804
805 =head1 NEW PLAN CLASSES
806
807 A module should be added in FS/FS/part_pkg/  Eventually, an example may be
808 found in eg/plan_template.pm.  Until then, it is suggested that you use the
809 other modules in FS/FS/part_pkg/ as a guide.
810
811 =head1 BUGS
812
813 The delete method is unimplemented.
814
815 setup and recur semantics are not yet defined (and are implemented in
816 FS::cust_bill.  hmm.).  now they're deprecated and need to go.
817
818 plandata should go
819
820 =head1 SEE ALSO
821
822 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
823 schema.html from the base documentation.
824
825 =cut
826
827 1;
828