RT# 81249 - Fixed error where empty field becomes 0.
[freeside.git] / FS / FS / part_pkg.pm
1 package FS::part_pkg;
2 use base qw( FS::m2m_Common FS::o2m_Common FS::option_Common );
3
4 use strict;
5 use vars qw( %plans $DEBUG $setup_hack $skip_pkg_svc_hack
6              $cache_enabled %cache_link %cache_pkg_svc
7            );
8 use Carp qw(carp cluck confess);
9 use Scalar::Util qw( blessed );
10 use DateTime;
11 use Time::Local qw( timelocal timelocal_nocheck ); # eventually replace with DateTime
12 use Tie::IxHash;
13 use FS::Conf;
14 use FS::Record qw( qsearch qsearchs dbh dbdef );
15 use FS::Cursor; # for upgrade
16 use FS::pkg_svc;
17 use FS::part_svc;
18 use FS::cust_pkg;
19 use FS::agent_type;
20 use FS::type_pkgs;
21 use FS::part_pkg_option;
22 use FS::part_pkg_fcc_option;
23 use FS::pkg_class;
24 use FS::agent;
25 use FS::part_pkg_msgcat;
26 use FS::part_pkg_taxrate;
27 use FS::part_pkg_taxoverride;
28 use FS::part_pkg_taxproduct;
29 use FS::part_pkg_link;
30 use FS::part_pkg_discount;
31 use FS::part_pkg_usage;
32 use FS::part_pkg_vendor;
33
34 $DEBUG = 0;
35
36 $setup_hack = 0;
37 $skip_pkg_svc_hack = 0;
38
39 $cache_enabled = 0;
40 %cache_link = ();
41 %cache_pkg_svc = ();
42
43 =head1 NAME
44
45 FS::part_pkg - Object methods for part_pkg objects
46
47 =head1 SYNOPSIS
48
49   use FS::part_pkg;
50
51   $record = new FS::part_pkg \%hash
52   $record = new FS::part_pkg { 'column' => 'value' };
53
54   $custom_record = $template_record->clone;
55
56   $error = $record->insert;
57
58   $error = $new_record->replace($old_record);
59
60   $error = $record->delete;
61
62   $error = $record->check;
63
64   @pkg_svc = $record->pkg_svc;
65
66   $svcnum = $record->svcpart;
67   $svcnum = $record->svcpart( 'svc_acct' );
68
69 =head1 DESCRIPTION
70
71 An FS::part_pkg object represents a package definition.  FS::part_pkg
72 inherits from FS::Record.  The following fields are currently supported:
73
74 =over 4
75
76 =item pkgpart - primary key (assigned automatically for new package definitions)
77
78 =item pkg - Text name of this package definition (customer-viewable)
79
80 =item comment - Text name of this package definition (non-customer-viewable)
81
82 =item classnum - Optional package class (see L<FS::pkg_class>)
83
84 =item promo_code - Promotional code
85
86 =item setup - Setup fee expression (deprecated)
87
88 =item freq - Frequency of recurring fee
89
90 =item recur - Recurring fee expression (deprecated)
91
92 =item setuptax - Setup fee tax exempt flag, empty or `Y'
93
94 =item recurtax - Recurring fee tax exempt flag, empty or `Y'
95
96 =item taxclass - Tax class 
97
98 =item plan - Price plan
99
100 =item plandata - Price plan data (deprecated - see L<FS::part_pkg_option> instead)
101
102 =item disabled - Disabled flag, empty or `Y'
103
104 =item custom - Custom flag, empty or `Y'
105
106 =item setup_cost - for cost tracking
107
108 =item recur_cost - for cost tracking
109
110 =item pay_weight - Weight (relative to credit_weight and other package definitions) that controls payment application to specific line items.
111
112 =item credit_weight - Weight (relative to other package definitions) that controls credit application to specific line items.
113
114 =item agentnum - Optional agentnum (see L<FS::agent>)
115
116 =item fcc_ds0s - Optional DS0 equivalency number for FCC form 477
117
118 =item fcc_voip_class - Which column of FCC form 477 part II.B this package 
119 belongs in.
120
121 =item successor - Foreign key for the part_pkg that replaced this record.
122 If this record is not obsolete, will be null.
123
124 =item family_pkgpart - Foreign key for the part_pkg that was the earliest
125 ancestor of this record.  If this record is not a successor to another 
126 part_pkg, will be equal to pkgpart.
127
128 =item delay_start - Number of days to delay package start, by default
129
130 =item start_on_hold - 'Y' to suspend this package immediately when it is 
131 ordered. The package will not start billing or have a setup fee charged 
132 until it is manually unsuspended.
133
134 =back
135
136 =head1 METHODS
137
138 =over 4 
139
140 =item new HASHREF
141
142 Creates a new package definition.  To add the package definition to
143 the database, see L<"insert">.
144
145 =cut
146
147 sub table { 'part_pkg'; }
148
149 =item clone
150
151 An alternate constructor.  Creates a new package definition by duplicating
152 an existing definition.  A new pkgpart is assigned and the custom flag is
153 set to Y.  To add the package definition to the database, see L<"insert">.
154
155 =cut
156
157 sub clone {
158   my $self = shift;
159   my $class = ref($self);
160   my %hash = $self->hash;
161   $hash{'pkgpart'} = '';
162   $hash{'custom'} = 'Y';
163   #new FS::part_pkg ( \%hash ); # ?
164   new $class ( \%hash ); # ?
165 }
166
167 =item insert [ , OPTION => VALUE ... ]
168
169 Adds this package definition to the database.  If there is an error,
170 returns the error, otherwise returns false.
171
172 Currently available options are: I<pkg_svc>, I<primary_svc>, I<cust_pkg>, 
173 I<custnum_ref> and I<options>.
174
175 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
176 values, appropriate FS::pkg_svc records will be inserted.  I<hidden_svc> can 
177 be set to a hashref of svcparts and flag values ('Y' or '') to set the 
178 'hidden' field in these records, and I<provision_hold> can be set similarly
179 for the 'provision_hold' field in these records.
180
181 If I<primary_svc> is set to the svcpart of the primary service, the appropriate
182 FS::pkg_svc record will be updated.
183
184 If I<cust_pkg> is set to a pkgnum of a FS::cust_pkg record (or the FS::cust_pkg
185 record itself), the object will be updated to point to this package definition.
186
187 In conjunction with I<cust_pkg>, if I<custnum_ref> is set to a scalar reference,
188 the scalar will be updated with the custnum value from the cust_pkg record.
189
190 If I<tax_overrides> is set to a hashref with usage classes as keys and comma
191 separated tax class numbers as values, appropriate FS::part_pkg_taxoverride
192 records will be inserted.
193
194 If I<options> is set to a hashref of options, appropriate FS::part_pkg_option
195 records will be inserted.
196
197 =cut
198
199 sub insert {
200   my $self = shift;
201   my %options = @_;
202   warn "FS::part_pkg::insert called on $self with options ".
203        join(', ', map "$_=>$options{$_}", keys %options)
204     if $DEBUG;
205
206   local $SIG{HUP} = 'IGNORE';
207   local $SIG{INT} = 'IGNORE';
208   local $SIG{QUIT} = 'IGNORE';
209   local $SIG{TERM} = 'IGNORE';
210   local $SIG{TSTP} = 'IGNORE';
211   local $SIG{PIPE} = 'IGNORE';
212
213   my $oldAutoCommit = $FS::UID::AutoCommit;
214   local $FS::UID::AutoCommit = 0;
215   my $dbh = dbh;
216
217   warn "  inserting part_pkg record" if $DEBUG;
218   my $error = $self->SUPER::insert( $options{options} );
219   if ( $error ) {
220     $dbh->rollback if $oldAutoCommit;
221     return $error;
222   }
223
224   # set family_pkgpart
225   if ( $self->get('family_pkgpart') eq '' ) {
226     $self->set('family_pkgpart' => $self->pkgpart);
227     $error = $self->SUPER::replace;
228     if ( $error ) {
229       $dbh->rollback if $oldAutoCommit;
230       return $error;
231     }
232   }
233
234   warn "  inserting part_pkg_taxoverride records" if $DEBUG;
235   my %overrides = %{ $options{'tax_overrides'} || {} };
236   foreach my $usage_class ( keys %overrides ) {
237     my $override =
238       ( exists($overrides{$usage_class}) && defined($overrides{$usage_class}) )
239         ? $overrides{$usage_class}
240         : '';
241     my @overrides = (grep "$_", split(',', $override) );
242     my $error = $self->process_m2m (
243                   'link_table'   => 'part_pkg_taxoverride',
244                   'target_table' => 'tax_class',
245                   'hashref'      => { 'usage_class' => $usage_class },
246                   'params'       => \@overrides,
247                 );
248     if ( $error ) {
249       $dbh->rollback if $oldAutoCommit;
250       return $error;
251     }
252   }
253
254   unless ( $skip_pkg_svc_hack ) {
255
256     warn "  inserting pkg_svc records" if $DEBUG;
257     my $pkg_svc = $options{'pkg_svc'} || {};
258     my $hidden_svc = $options{'hidden_svc'} || {};
259     my $provision_hold = $options{'provision_hold'} || {};
260     foreach my $part_svc ( qsearch('part_svc', {} ) ) {
261       my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
262       my $primary_svc =
263         ( $options{'primary_svc'} && $options{'primary_svc'}==$part_svc->svcpart )
264           ? 'Y'
265           : '';
266
267       my $pkg_svc = new FS::pkg_svc( {
268         'pkgpart'     => $self->pkgpart,
269         'svcpart'     => $part_svc->svcpart,
270         'quantity'    => $quantity, 
271         'primary_svc' => $primary_svc,
272         'hidden'      => $hidden_svc->{$part_svc->svcpart},
273         'provision_hold' => $provision_hold->{$part_svc->svcpart},
274       } );
275       my $error = $pkg_svc->insert;
276       if ( $error ) {
277         $dbh->rollback if $oldAutoCommit;
278         return $error;
279       }
280     }
281
282   }
283
284   if ( $options{'cust_pkg'} ) {
285     warn "  updating cust_pkg record " if $DEBUG;
286     my $old_cust_pkg =
287       ref($options{'cust_pkg'})
288         ? $options{'cust_pkg'}
289         : qsearchs('cust_pkg', { pkgnum => $options{'cust_pkg'} } );
290     ${ $options{'custnum_ref'} } = $old_cust_pkg->custnum
291       if $options{'custnum_ref'};
292     my %hash = $old_cust_pkg->hash;
293     $hash{'pkgpart'} = $self->pkgpart,
294     my $new_cust_pkg = new FS::cust_pkg \%hash;
295     local($FS::cust_pkg::disable_agentcheck) = 1;
296     my $error = $new_cust_pkg->replace($old_cust_pkg);
297     if ( $error ) {
298       $dbh->rollback if $oldAutoCommit;
299       return "Error modifying cust_pkg record: $error";
300     }
301   }
302
303   if ( $options{'part_pkg_vendor'} ) {
304       while ( my ($exportnum, $vendor_pkg_id) =
305                 each %{ $options{part_pkg_vendor} }
306             )
307       {
308             my $ppv = new FS::part_pkg_vendor( {
309                     'pkgpart' => $self->pkgpart,
310                     'exportnum' => $exportnum,
311                     'vendor_pkg_id' => $vendor_pkg_id, 
312                 } );
313             my $error = $ppv->insert;
314             if ( $error ) {
315               $dbh->rollback if $oldAutoCommit;
316               return "Error inserting part_pkg_vendor record: $error";
317             }
318       }
319   }
320
321   if ( $options{fcc_options} ) {
322     warn "  updating fcc options " if $DEBUG;
323     $self->set_fcc_options( $options{fcc_options} );
324   }
325
326   warn "  committing transaction" if $DEBUG and $oldAutoCommit;
327   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
328
329   '';
330 }
331
332 =item delete
333
334 Currently unimplemented.
335
336 =cut
337
338 sub delete {
339   return "Can't (yet?) delete package definitions.";
340 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
341 }
342
343 =item replace OLD_RECORD [ , OPTION => VALUE ... ]
344
345 Replaces OLD_RECORD with this one in the database.  If there is an error,
346 returns the error, otherwise returns false.
347
348 Currently available options are: I<pkg_svc>, I<hidden_svc>, I<primary_svc>,
349 I<provision_hold> and I<options>
350
351 If I<pkg_svc> is set to a hashref with svcparts as keys and quantities as
352 values, the appropriate FS::pkg_svc records will be replaced.  I<hidden_svc>
353 can be set to a hashref of svcparts and flag values ('Y' or '') to set the 
354 'hidden' field in these records.  I<provision_hold> can be set 
355 to a hashref of svcparts and flag values ('Y' or '') to set the field 
356 in those records.
357
358 If I<primary_svc> is set to the svcpart of the primary service, the
359 appropriate FS::pkg_svc record will be updated.
360
361 If I<options> is set to a hashref, the appropriate FS::part_pkg_option
362 records will be replaced.
363
364 =cut
365
366 sub replace {
367   my $new = shift;
368
369   my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
370               ? shift
371               : $new->replace_old;
372
373   my $options = 
374     ( ref($_[0]) eq 'HASH' )
375       ? shift
376       : { @_ };
377
378   $options->{options} = { $old->options } unless defined($options->{options});
379
380   warn "FS::part_pkg::replace called on $new to replace $old with options".
381        join(', ', map "$_ => ". $options->{$_}, keys %$options)
382     if $DEBUG;
383
384   local $SIG{HUP} = 'IGNORE';
385   local $SIG{INT} = 'IGNORE';
386   local $SIG{QUIT} = 'IGNORE';
387   local $SIG{TERM} = 'IGNORE';
388   local $SIG{TSTP} = 'IGNORE';
389   local $SIG{PIPE} = 'IGNORE';
390
391   my $oldAutoCommit = $FS::UID::AutoCommit;
392   local $FS::UID::AutoCommit = 0;
393   my $dbh = dbh;
394   
395   my $conf = new FS::Conf;
396   if ( $conf->exists('part_pkg-lineage') ) {
397     if ( grep { $options->{options}->{$_} ne $old->option($_, 1) }
398           qw(setup_fee recur_fee) #others? config?
399         ) { 
400     
401       warn "  superseding package" if $DEBUG;
402
403       my $error = $new->supersede($old, %$options);
404       if ( $error ) {
405         $dbh->rollback if $oldAutoCommit;
406         return $error;
407       }
408       else {
409         warn "  committing transaction" if $DEBUG and $oldAutoCommit;
410         $dbh->commit if $oldAutoCommit;
411         return $error;
412       }
413     }
414     #else nothing
415   }
416
417   #plandata shit stays in replace for upgrades until after 2.0 (or edit
418   #_upgrade_data)
419   warn "  saving legacy plandata" if $DEBUG;
420   my $plandata = $new->get('plandata');
421   $new->set('plandata', '');
422
423   warn "  deleting old part_pkg_option records" if $DEBUG;
424   foreach my $part_pkg_option ( $old->part_pkg_option ) {
425     my $error = $part_pkg_option->delete;
426     if ( $error ) {
427       $dbh->rollback if $oldAutoCommit;
428       return $error;
429     }
430   }
431
432   warn "  replacing part_pkg record" if $DEBUG;
433   my $error = $new->SUPER::replace($old, $options->{options} );
434   if ( $error ) {
435     $dbh->rollback if $oldAutoCommit;
436     return $error;
437   }
438
439   warn "  inserting part_pkg_option records for plandata: $plandata|" if $DEBUG;
440   foreach my $part_pkg_option ( 
441     map { /^(\w+)=(.*)$/ or do { $dbh->rollback if $oldAutoCommit;
442                                  return "illegal plandata: $plandata";
443                                };
444           new FS::part_pkg_option {
445             'pkgpart'     => $new->pkgpart,
446             'optionname'  => $1,
447             'optionvalue' => $2,
448           };
449         }
450     split("\n", $plandata)
451   ) {
452     my $error = $part_pkg_option->insert;
453     if ( $error ) {
454       $dbh->rollback if $oldAutoCommit;
455       return $error;
456     }
457   }
458
459   warn "  replacing pkg_svc records" if $DEBUG;
460   my $pkg_svc = $options->{'pkg_svc'};
461   my $hidden_svc = $options->{'hidden_svc'} || {};
462   my $provision_hold = $options->{'provision_hold'} || {};
463   if ( $pkg_svc ) { # if it wasn't passed, don't change existing pkg_svcs
464     foreach my $part_svc ( qsearch('part_svc', {} ) ) {
465       my $quantity = $pkg_svc->{$part_svc->svcpart} || 0;
466       my $hidden = $hidden_svc->{$part_svc->svcpart} || '';
467       my $provision_hold = $provision_hold->{$part_svc->svcpart} || '';
468       my $primary_svc =
469         ( defined($options->{'primary_svc'}) && $options->{'primary_svc'}
470           && $options->{'primary_svc'} == $part_svc->svcpart
471         )
472           ? 'Y'
473           : '';
474
475       my $old_pkg_svc = qsearchs('pkg_svc', {
476           'pkgpart' => $old->pkgpart,
477           'svcpart' => $part_svc->svcpart,
478         }
479       );
480       my $old_quantity = 0;
481       my $old_primary_svc = '';
482       my $old_hidden = '';
483       my $old_provision_hold = '';
484       if ( $old_pkg_svc ) {
485         $old_quantity = $old_pkg_svc->quantity;
486         $old_primary_svc = $old_pkg_svc->primary_svc 
487           if $old_pkg_svc->dbdef_table->column('primary_svc'); # is this needed?
488         $old_hidden = $old_pkg_svc->hidden;
489         $old_provision_hold = $old_pkg_svc->provision_hold;
490       }
491    
492       next unless $old_quantity != $quantity || 
493                   $old_primary_svc ne $primary_svc ||
494                   $old_hidden ne $hidden ||
495                   $old_provision_hold ne $provision_hold;
496     
497       my $new_pkg_svc = new FS::pkg_svc( {
498         'pkgsvcnum'   => ( $old_pkg_svc ? $old_pkg_svc->pkgsvcnum : '' ),
499         'pkgpart'     => $new->pkgpart,
500         'svcpart'     => $part_svc->svcpart,
501         'quantity'    => $quantity, 
502         'primary_svc' => $primary_svc,
503         'hidden'      => $hidden,
504         'provision_hold' => $provision_hold,
505       } );
506       my $error = $old_pkg_svc
507                     ? $new_pkg_svc->replace($old_pkg_svc)
508                     : $new_pkg_svc->insert;
509       if ( $error ) {
510         $dbh->rollback if $oldAutoCommit;
511         return $error;
512       }
513     } #foreach $part_svc
514   } #if $options->{pkg_svc}
515   
516   my @part_pkg_vendor = $old->part_pkg_vendor;
517   my @current_exportnum = ();
518   if ( $options->{'part_pkg_vendor'} ) {
519       my($exportnum,$vendor_pkg_id);
520       while ( ($exportnum,$vendor_pkg_id) 
521                                 = each %{$options->{'part_pkg_vendor'}} ) {
522           my $noinsert = 0;
523           foreach my $part_pkg_vendor ( @part_pkg_vendor ) {
524             if($exportnum == $part_pkg_vendor->exportnum
525                 && $vendor_pkg_id ne $part_pkg_vendor->vendor_pkg_id) {
526                 $part_pkg_vendor->vendor_pkg_id($vendor_pkg_id);
527                 my $error = $part_pkg_vendor->replace;
528                 if ( $error ) {
529                   $dbh->rollback if $oldAutoCommit;
530                   return "Error replacing part_pkg_vendor record: $error";
531                 }
532                 $noinsert = 1;
533                 last;
534             }
535             elsif($exportnum == $part_pkg_vendor->exportnum
536                 && $vendor_pkg_id eq $part_pkg_vendor->vendor_pkg_id) {
537                 $noinsert = 1;
538                 last;
539             }
540           }
541           unless ( $noinsert ) {
542             my $ppv = new FS::part_pkg_vendor( {
543                     'pkgpart' => $new->pkgpart,
544                     'exportnum' => $exportnum,
545                     'vendor_pkg_id' => $vendor_pkg_id, 
546                 } );
547             my $error = $ppv->insert;
548             if ( $error ) {
549               $dbh->rollback if $oldAutoCommit;
550               return "Error inserting part_pkg_vendor record: $error";
551             }
552           }
553           push @current_exportnum, $exportnum;
554       }
555   }
556   foreach my $part_pkg_vendor ( @part_pkg_vendor ) {
557       unless ( grep($_ eq $part_pkg_vendor->exportnum, @current_exportnum) ) {
558         my $error = $part_pkg_vendor->delete;
559         if ( $error ) {
560           $dbh->rollback if $oldAutoCommit;
561           return "Error deleting part_pkg_vendor record: $error";
562         }
563       }
564   }
565   
566   # propagate changes to certain core fields
567   if ( $conf->exists('part_pkg-lineage') ) {
568     warn "  propagating changes to family" if $DEBUG;
569     my $error = $new->propagate($old);
570     if ( $error ) {
571       $dbh->rollback if $oldAutoCommit;
572       return $error;
573     }
574   }
575
576   if ( $options->{fcc_options} ) {
577     warn "  updating fcc options " if $DEBUG;
578     $new->set_fcc_options( $options->{fcc_options} );
579   }
580
581   warn "  committing transaction" if $DEBUG and $oldAutoCommit;
582   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
583   '';
584 }
585
586 sub validate_number {
587   my ($option, $valref) = @_;
588
589   $$valref = 0 unless $$valref;
590   return "Invalid $option"
591     unless ($$valref) = ($$valref =~ /^\s*(\d+)\s*$/);
592   return '';
593 }
594
595 sub validate_number_blank {
596   my ($option, $valref) = @_;
597
598   if ($$valref) {
599     return "Invalid $option"
600       unless ($$valref) = ($$valref =~ /^\s*(\d+)\s*$/);
601   }
602   return '';
603 }
604
605 =item check
606
607 Checks all fields to make sure this is a valid package definition.  If
608 there is an error, returns the error, otherwise returns false.  Called by the
609 insert and replace methods.
610
611 =cut
612
613 sub check {
614   my $self = shift;
615   warn "FS::part_pkg::check called on $self" if $DEBUG;
616
617   for (qw(setup recur plandata)) {
618     #$self->set($_=>0) if $self->get($_) =~ /^\s*$/; }
619     return "Use of $_ field is deprecated; set a plan and options: ".
620            $self->get($_)
621       if length($self->get($_));
622     $self->set($_, '');
623   }
624
625   if ( $self->dbdef_table->column('freq')->type =~ /(int)/i ) {
626     my $error = $self->ut_number('freq');
627     return $error if $error;
628   } else {
629     $self->freq =~ /^(\d+[hdw]?)$/
630       or return "Illegal or empty freq: ". $self->freq;
631     $self->freq($1);
632   }
633
634   my @null_agentnum_right = ( 'Edit global package definitions' );
635   push @null_agentnum_right, 'One-time charge'
636     if $self->freq =~ /^0/;
637   push @null_agentnum_right, 'Customize customer package'
638     if $self->disabled eq 'Y'; #good enough
639
640   my $error = $self->ut_numbern('pkgpart')
641     || $self->ut_text('pkg')
642     || $self->ut_textn('comment')
643     || $self->ut_textn('promo_code')
644     || $self->ut_alphan('plan')
645     || $self->ut_flag('setuptax')
646     || $self->ut_flag('recurtax')
647     || $self->ut_textn('taxclass')
648     || $self->ut_flag('disabled')
649     || $self->ut_flag('custom')
650     || $self->ut_flag('no_auto')
651     || $self->ut_flag('recur_show_zero')
652     || $self->ut_flag('setup_show_zero')
653     || $self->ut_flag('start_on_hold')
654     #|| $self->ut_moneyn('setup_cost')
655     #|| $self->ut_moneyn('recur_cost')
656     || $self->ut_floatn('setup_cost')
657     || $self->ut_floatn('recur_cost')
658     || $self->ut_floatn('pay_weight')
659     || $self->ut_floatn('credit_weight')
660     || $self->ut_numbern('taxproductnum')
661     || $self->ut_foreign_keyn('classnum',       'pkg_class', 'classnum')
662     || $self->ut_foreign_keyn('addon_classnum', 'pkg_class', 'classnum')
663     || $self->ut_foreign_keyn('taxproductnum',
664                               'part_pkg_taxproduct',
665                               'taxproductnum'
666                              )
667     || ( $setup_hack
668            ? $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum' )
669            : $self->ut_agentnum_acl('agentnum', \@null_agentnum_right)
670        )
671     || $self->ut_numbern('fcc_ds0s')
672     || $self->ut_numbern('fcc_voip_class')
673     || $self->ut_numbern('delay_start')
674     || $self->ut_foreign_keyn('successor', 'part_pkg', 'pkgpart')
675     || $self->ut_foreign_keyn('family_pkgpart', 'part_pkg', 'pkgpart')
676     || $self->ut_alphan('agent_pkgpartid')
677     || $self->SUPER::check
678   ;
679   return $error if $error;
680
681   return 'Unknown plan '. $self->plan
682     unless exists($plans{$self->plan});
683
684   my $conf = new FS::Conf;
685   return 'Taxclass is required'
686     if ! $self->taxclass && $conf->exists('require_taxclasses');
687
688   '';
689 }
690
691 =item check_options
692
693 Pass an I<$options> hashref that contains the values to be
694 inserted or updated for any FS::part_pkg::MODULE.pm.
695
696 For each key in I<$options>, validates the value by calling
697 the 'validate' subroutine defined for that option e.g.
698 FS::part_pkg::MODULE::plan_info()->{$KEY}->{validate}.  The
699 option validation function is only called when the hashkey for
700 that option exists in I<$options>.
701
702 Then the module validation function is called, from
703 FS::part_pkg::MODULE::plan_info()->{validate}
704
705 Returns error message, or empty string if valid.
706
707 Invoked by L</insert> and L</replace> via the equivalent
708 methods in L<FS::option_Common>.
709
710 =cut
711
712 sub check_options {
713   my ($self,$options) = @_;
714   foreach my $option (keys %$options) {
715     if (exists $plans{ $self->plan }->{fields}->{$option}) {
716       if (exists($plans{$self->plan}->{fields}->{$option}->{'validate'})) {
717         # pass option name for use in error message
718         # pass a reference to the $options value, so it can be cleaned up
719         my $error = &{$plans{$self->plan}->{fields}->{$option}->{'validate'}}($option,\($options->{$option}));
720         return $error if $error;
721       }
722     } # else "option does not exist" error?
723   }
724   if (exists($plans{$self->plan}->{'validate'})) {
725     my $error = &{$plans{$self->plan}->{'validate'}}($options);
726     return $error if $error;
727   }
728   return '';
729 }
730
731 =item supersede OLD [, OPTION => VALUE ... ]
732
733 Inserts this package as a successor to the package OLD.  All options are as
734 for C<insert>.  After inserting, disables OLD and sets the new package as its
735 successor.
736
737 =cut
738
739 sub supersede {
740   my ($new, $old, %options) = @_;
741   my $error;
742
743   $new->set('pkgpart' => '');
744   $new->set('family_pkgpart' => $old->family_pkgpart);
745   warn "    inserting successor package\n" if $DEBUG;
746   $error = $new->insert(%options);
747   return $error if $error;
748  
749   warn "    disabling superseded package\n" if $DEBUG; 
750   $old->set('successor' => $new->pkgpart);
751   $old->set('disabled' => 'Y');
752   $error = $old->SUPER::replace; # don't change its options/pkg_svc records
753   return $error if $error;
754
755   warn "  propagating changes to family" if $DEBUG;
756   $new->propagate($old);
757 }
758
759 =item propagate OLD
760
761 If any of certain fields have changed from OLD to this package, then,
762 for all packages in the same lineage as this one, sets those fields 
763 to their values in this package.
764
765 =cut
766
767 my @propagate_fields = (
768   qw( pkg classnum setup_cost recur_cost taxclass
769   setuptax recurtax pay_weight credit_weight
770   )
771 );
772
773 sub propagate {
774   my $new = shift;
775   my $old = shift;
776   my %fields = (
777     map { $_ => $new->get($_) }
778     grep { $new->get($_) ne $old->get($_) }
779     @propagate_fields
780   );
781
782   my @part_pkg = qsearch('part_pkg', { 
783       'family_pkgpart' => $new->family_pkgpart 
784   });
785   my @error;
786   foreach my $part_pkg ( @part_pkg ) {
787     my $pkgpart = $part_pkg->pkgpart;
788     next if $pkgpart == $new->pkgpart; # don't modify $new
789     warn "    propagating to pkgpart $pkgpart\n" if $DEBUG;
790     foreach ( keys %fields ) {
791       $part_pkg->set($_, $fields{$_});
792     }
793     # SUPER::replace to avoid changing non-core fields
794     my $error = $part_pkg->SUPER::replace;
795     push @error, "pkgpart $pkgpart: $error"
796       if $error;
797   }
798   join("\n", @error);
799 }
800
801 =item set_fcc_options HASHREF
802
803 Sets the FCC options on this package definition to the values specified
804 in HASHREF.
805
806 =cut
807
808 sub set_fcc_options {
809   my $self = shift;
810   my $pkgpart = $self->pkgpart;
811   my $options;
812   if (ref $_[0]) {
813     $options = shift;
814   } else {
815     $options = { @_ };
816   }
817
818   my %existing_num = map { $_->fccoptionname => $_->num }
819                      qsearch('part_pkg_fcc_option', { pkgpart => $pkgpart });
820
821   local $FS::Record::nowarn_identical = 1;
822   # set up params for process_o2m
823   my $i = 0;
824   my $params = {};
825   foreach my $name (keys %$options ) {
826     $params->{ "num$i" } = $existing_num{$name} || '';
827     $params->{ "num$i".'_fccoptionname' } = $name;
828     $params->{ "num$i".'_optionvalue'   } = $options->{$name};
829     $i++;
830   }
831
832   $self->process_o2m(
833     table   => 'part_pkg_fcc_option',
834     fields  => [qw( fccoptionname optionvalue )],
835     params  => $params,
836   );
837 }
838
839 =item pkg_locale LOCALE
840
841 Returns a customer-viewable string representing this package for the given
842 locale, from the part_pkg_msgcat table.  If the given locale is empty or no
843 localized string is found, returns the base pkg field.
844
845 =cut
846
847 sub pkg_locale {
848   my( $self, $locale ) = @_;
849   return $self->pkg unless $locale;
850   my $part_pkg_msgcat = $self->part_pkg_msgcat($locale) or return $self->pkg;
851   $part_pkg_msgcat->pkg;
852 }
853
854 =item part_pkg_msgcat LOCALE
855
856 Like pkg_locale, but returns the FS::part_pkg_msgcat object itself.
857
858 =cut
859
860 sub part_pkg_msgcat {
861   my( $self, $locale ) = @_;
862   qsearchs( 'part_pkg_msgcat', {
863     pkgpart => $self->pkgpart,
864     locale  => $locale,
865   });
866 }
867
868 =item pkg_comment [ OPTION => VALUE... ]
869
870 Returns an (internal) string representing this package.  Currently,
871 "pkgpart: pkg - comment", is returned.  "pkg - comment" may be returned in the
872 future, omitting pkgpart.  The comment will have '(CUSTOM) ' prepended if
873 custom is Y.
874
875 If the option nopkgpart is true then the "pkgpart: ' is omitted.
876
877 =cut
878
879 sub pkg_comment {
880   my $self = shift;
881   my %opt = @_;
882
883   #$self->pkg. ' - '. $self->comment;
884   #$self->pkg. ' ('. $self->comment. ')';
885   my $pre = $opt{nopkgpart} ? '' : $self->pkgpart. ': ';
886   my $custom_comment = $self->custom_comment(%opt);
887   $pre. $self->pkg. ( $custom_comment ? " - $custom_comment" : '' );
888 }
889
890 #without price info (so without hitting the DB again)
891 sub pkg_comment_only {
892   my $self = shift;
893   my %opt = @_;
894
895   my $pre = $opt{nopkgpart} ? '' : $self->pkgpart. ': ';
896   my $comment = $self->comment;
897   $pre. $self->pkg. ( $comment ? " - $comment" : '' );
898 }
899
900 sub price_info { # safety, in case a part_pkg hasn't defined price_info
901     '';
902 }
903
904 sub custom_comment {
905   my $self = shift;
906   my $price_info = $self->price_info(@_);
907   ( $self->custom ? '(CUSTOM) ' : '' ).
908     $self->comment.
909     ( ( ($self->custom || $self->comment) && $price_info ) ? ' - ' : '' ).
910     $price_info;
911 }
912
913 sub pkg_price_info {
914   my $self = shift;
915   $self->pkg. ' - '. ($self->price_info || 'No charge');
916 }
917
918 =item pkg_class
919
920 Returns the package class, as an FS::pkg_class object, or the empty string
921 if there is no package class.
922
923 =cut
924
925 sub pkg_class {
926   my $self = shift;
927   if ( $self->classnum ) {
928     qsearchs('pkg_class', { 'classnum' => $self->classnum } );
929   } else {
930     return '';
931   }
932 }
933
934 =item addon_pkg_class
935
936 Returns the add-on package class, as an FS::pkg_class object, or the empty
937 string if there is no add-on package class.
938
939 =cut
940
941 sub addon_pkg_class {
942   my $self = shift;
943   if ( $self->addon_classnum ) {
944     qsearchs('pkg_class', { 'classnum' => $self->addon_classnum } );
945   } else {
946     return '';
947   }
948 }
949
950 =item categoryname 
951
952 Returns the package category name, or the empty string if there is no package
953 category.
954
955 =cut
956
957 sub categoryname {
958   my $self = shift;
959   my $pkg_class = $self->pkg_class;
960   $pkg_class
961     ? $pkg_class->categoryname
962     : '';
963 }
964
965 =item classname 
966
967 Returns the package class name, or the empty string if there is no package
968 class.
969
970 =cut
971
972 sub classname {
973   my $self = shift;
974   my $pkg_class = $self->pkg_class;
975   $pkg_class
976     ? $pkg_class->classname
977     : '';
978 }
979
980 =item addon_classname 
981
982 Returns the add-on package class name, or the empty string if there is no
983 add-on package class.
984
985 =cut
986
987 sub addon_classname {
988   my $self = shift;
989   my $pkg_class = $self->addon_pkg_class;
990   $pkg_class
991     ? $pkg_class->classname
992     : '';
993 }
994
995 =item agent 
996
997 Returns the associated agent for this event, if any, as an FS::agent object.
998
999 =cut
1000
1001 sub agent {
1002   my $self = shift;
1003   qsearchs('agent', { 'agentnum' => $self->agentnum } );
1004 }
1005
1006 =item pkg_svc [ HASHREF | OPTION => VALUE ]
1007
1008 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
1009 definition (with non-zero quantity).
1010
1011 One option is available, I<disable_linked>.  If set true it will return the
1012 services for this package definition alone, omitting services from any add-on
1013 packages.
1014
1015 =cut
1016
1017 =item type_pkgs
1018
1019 Returns all FS::type_pkgs objects (see L<FS::type_pkgs>) for this package
1020 definition.
1021
1022 =cut
1023
1024 sub type_pkgs {
1025   my $self = shift;
1026   qsearch('type_pkgs', { 'pkgpart' => $self->pkgpart } );
1027 }
1028
1029 sub pkg_svc {
1030   my $self = shift;
1031
1032   return @{ $cache_pkg_svc{$self->pkgpart} }
1033     if $cache_enabled && $cache_pkg_svc{$self->pkgpart};
1034
1035 #  #sort { $b->primary cmp $a->primary } 
1036 #    grep { $_->quantity }
1037 #      qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
1038
1039   my $opt = ref($_[0]) ? $_[0] : { @_ };
1040   my %pkg_svc = map  { $_->svcpart => $_ } $self->_pkg_svc;
1041
1042   unless ( $opt->{disable_linked} ) {
1043     foreach my $dst_pkg ( map $_->dst_pkg, $self->svc_part_pkg_link ) {
1044       my @pkg_svc = $dst_pkg->_pkg_svc;
1045       foreach my $pkg_svc ( @pkg_svc ) {
1046         if ( $pkg_svc{$pkg_svc->svcpart} ) {
1047           my $quantity = $pkg_svc{$pkg_svc->svcpart}->quantity;
1048           $pkg_svc{$pkg_svc->svcpart}->quantity($quantity + $pkg_svc->quantity);
1049         } else {
1050           $pkg_svc{$pkg_svc->svcpart} = $pkg_svc;
1051         }
1052       }
1053     }
1054   }
1055
1056   my @pkg_svc = values(%pkg_svc);
1057
1058   $cache_pkg_svc{$self->pkgpart} = \@pkg_svc if $cache_enabled;
1059
1060   @pkg_svc;
1061
1062 }
1063
1064 sub _pkg_svc {
1065   my $self = shift;
1066   grep { $_->quantity }
1067     qsearch({
1068       'select'    => 'pkg_svc.*, part_svc.*',
1069       'table'     => 'pkg_svc',
1070       'addl_from' => 'LEFT JOIN part_svc USING ( svcpart )',
1071       'hashref'   => { 'pkgpart' => $self->pkgpart },
1072     });
1073 }
1074
1075 =item svcpart [ SVCDB ]
1076
1077 Returns the svcpart of the primary service definition (see L<FS::part_svc>)
1078 associated with this package definition (see L<FS::pkg_svc>).  Returns
1079 false if there not a primary service definition or exactly one service
1080 definition with quantity 1, or if SVCDB is specified and does not match the
1081 svcdb of the service definition.  SVCDB can be specified as a scalar table
1082 name, such as 'svc_acct', or as an arrayref of possible table names.
1083
1084 =cut
1085
1086 sub svcpart {
1087   my $pkg_svc = shift->_primary_pkg_svc(@_);
1088   $pkg_svc ? $pkg_svc->svcpart : '';
1089 }
1090
1091 =item part_svc [ SVCDB ]
1092
1093 Like the B<svcpart> method, but returns the FS::part_svc object (see
1094 L<FS::part_svc>).
1095
1096 =cut
1097
1098 sub part_svc {
1099   my $pkg_svc = shift->_primary_pkg_svc(@_);
1100   $pkg_svc ? $pkg_svc->part_svc : '';
1101 }
1102
1103 sub _primary_pkg_svc {
1104   my $self = shift;
1105
1106   my $svcdb = scalar(@_) ? shift : [];
1107   $svcdb = ref($svcdb) ? $svcdb : [ $svcdb ];
1108   my %svcdb = map { $_=>1 } @$svcdb;
1109
1110   my @svcdb_pkg_svc =
1111     grep { !scalar(@$svcdb) || $svcdb{ $_->part_svc->svcdb } }
1112          $self->pkg_svc;
1113
1114   my @pkg_svc = grep { $_->primary_svc =~ /^Y/i } @svcdb_pkg_svc;
1115   @pkg_svc = grep {$_->quantity == 1 } @svcdb_pkg_svc
1116     unless @pkg_svc;
1117   return '' if scalar(@pkg_svc) != 1;
1118   $pkg_svc[0];
1119 }
1120
1121 =item svcpart_unique_svcdb SVCDB
1122
1123 Returns the svcpart of a service definition (see L<FS::part_svc>) matching
1124 SVCDB associated with this package definition (see L<FS::pkg_svc>).  Returns
1125 false if there not a primary service definition for SVCDB or there are multiple
1126 service definitions for SVCDB.
1127
1128 =cut
1129
1130 sub svcpart_unique_svcdb {
1131   my( $self, $svcdb ) = @_;
1132   my @svcdb_pkg_svc = grep { ( $svcdb eq $_->part_svc->svcdb ) } $self->pkg_svc;
1133   return '' if scalar(@svcdb_pkg_svc) != 1;
1134   $svcdb_pkg_svc[0]->svcpart;
1135 }
1136
1137 =item payby
1138
1139 Returns a list of the acceptable payment types for this package.  Eventually
1140 this should come out of a database table and be editable, but currently has the
1141 following logic instead:
1142
1143 If the package is free, the single item B<BILL> is
1144 returned, otherwise, the single item B<CARD> is returned.
1145
1146 (CHEK?  LEC?  Probably shouldn't accept those by default, prone to abuse)
1147
1148 =cut
1149
1150 sub payby {
1151   my $self = shift;
1152   if ( $self->is_free ) {
1153     ( 'BILL' );
1154   } else {
1155     ( 'CARD' );
1156   }
1157 }
1158
1159 =item is_free
1160
1161 Returns true if this package is free.  
1162
1163 =cut
1164
1165 sub is_free {
1166   my $self = shift;
1167   if ( $self->can('is_free_options') ) {
1168     not grep { $_ !~ /^\s*0*(\.0*)?\s*$/ }
1169          map { $self->option($_) } 
1170              $self->is_free_options;
1171   } else {
1172     warn "FS::part_pkg::is_free: FS::part_pkg::". $self->plan. " subclass ".
1173          "provides neither is_free_options nor is_free method; returning false";
1174     0;
1175   }
1176 }
1177
1178 # whether the plan allows discounts to be applied to this package
1179 sub can_discount { 0; }
1180
1181 # whether the plan allows changing the start date
1182 sub can_start_date {
1183   my $self = shift;
1184   $self->start_on_hold ? 0 : 1;
1185 }
1186
1187 # the delay start date if present
1188 sub delay_start_date {
1189   my $self = shift;
1190
1191   my $delay = $self->delay_start or return '';
1192
1193   # avoid timelocal silliness  
1194   my $dt = DateTime->today(time_zone => 'local');
1195   $dt->add(days => $delay);
1196   $dt->epoch;
1197 }
1198
1199 sub freqs_href {
1200   # moved to FS::Misc to make this accessible to other packages
1201   # at initialization
1202   FS::Misc::pkg_freqs();
1203 }
1204
1205 =item freq_pretty
1206
1207 Returns an english representation of the I<freq> field, such as "monthly",
1208 "weekly", "semi-annually", etc.
1209
1210 =cut
1211
1212 sub freq_pretty {
1213   my $self = shift;
1214   my $freq = $self->freq;
1215
1216   #my $freqs_href = $self->freqs_href;
1217   my $freqs_href = freqs_href();
1218
1219   if ( exists($freqs_href->{$freq}) ) {
1220     $freqs_href->{$freq};
1221   } else {
1222     my $interval = 'month';
1223     if ( $freq =~ /^(\d+)([hdw])$/ ) {
1224       my %interval = ( 'h' => 'hour', 'd'=>'day', 'w'=>'week' );
1225       $interval = $interval{$2};
1226     }
1227     if ( $1 == 1 ) {
1228       "every $interval";
1229     } else {
1230       "every $freq ${interval}s";
1231     }
1232   }
1233 }
1234
1235 =item add_freq TIMESTAMP [ FREQ ]
1236
1237 Adds a billing period of some frequency to the provided timestamp and 
1238 returns the resulting timestamp, or -1 if the frequency could not be 
1239 parsed (shouldn't happen).  By default, the frequency of this package 
1240 will be used; to override this, pass a different frequency as a second 
1241 argument.
1242
1243 =cut
1244
1245 sub add_freq {
1246   my( $self, $date, $freq ) = @_;
1247   $freq = $self->freq unless $freq;
1248
1249   #change this bit to use Date::Manip? CAREFUL with timezones (see
1250   # mailing list archive)
1251   my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($date) )[0,1,2,3,4,5];
1252
1253   if ( $freq =~ /^\d+$/ ) {
1254     $mon += $freq;
1255     until ( $mon < 12 ) { $mon -= 12; $year++; }
1256
1257     $mday = 28 if $mday > 28 && FS::Conf->new->exists('anniversary-rollback');
1258
1259   } elsif ( $freq =~ /^(\d+)w$/ ) {
1260     my $weeks = $1;
1261     $mday += $weeks * 7;
1262   } elsif ( $freq =~ /^(\d+)d$/ ) {
1263     my $days = $1;
1264     $mday += $days;
1265   } elsif ( $freq =~ /^(\d+)h$/ ) {
1266     my $hours = $1;
1267     $hour += $hours;
1268   } else {
1269     return -1;
1270   }
1271
1272   timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year);
1273 }
1274
1275 =item plandata
1276
1277 For backwards compatibility, returns the plandata field as well as all options
1278 from FS::part_pkg_option.
1279
1280 =cut
1281
1282 sub plandata {
1283   my $self = shift;
1284   carp "plandata is deprecated";
1285   if ( @_ ) {
1286     $self->SUPER::plandata(@_);
1287   } else {
1288     my $plandata = $self->get('plandata');
1289     my %options = $self->options;
1290     $plandata .= join('', map { "$_=$options{$_}\n" } keys %options );
1291     $plandata;
1292   }
1293 }
1294
1295 =item part_pkg_vendor
1296
1297 Returns all vendor/external package ids as FS::part_pkg_vendor objects (see
1298 L<FS::part_pkg_vendor>).
1299
1300 =cut
1301
1302 sub part_pkg_vendor {
1303   my $self = shift;
1304   qsearch('part_pkg_vendor', { 'pkgpart' => $self->pkgpart } );
1305 }
1306
1307 =item vendor_pkg_ids
1308
1309 Returns a list of vendor/external package ids by exportnum
1310
1311 =cut
1312
1313 sub vendor_pkg_ids {
1314   my $self = shift;
1315   map { $_->exportnum => $_->vendor_pkg_id } $self->part_pkg_vendor;
1316 }
1317
1318 =item part_pkg_option
1319
1320 Returns all options as FS::part_pkg_option objects (see
1321 L<FS::part_pkg_option>).
1322
1323 =cut
1324
1325 sub part_pkg_option {
1326   my $self = shift;
1327   qsearch('part_pkg_option', { 'pkgpart' => $self->pkgpart } );
1328 }
1329
1330 =item options 
1331
1332 Returns a list of option names and values suitable for assigning to a hash.
1333
1334 =cut
1335
1336 sub options {
1337   my $self = shift;
1338   map { $_->optionname => $_->optionvalue } $self->part_pkg_option;
1339 }
1340
1341 =item option OPTIONNAME [ QUIET ]
1342
1343 Returns the option value for the given name, or the empty string.  If a true
1344 value is passed as the second argument, warnings about missing the option
1345 will be suppressed.
1346
1347 =cut
1348
1349 sub option {
1350   my( $self, $opt, $ornull ) = @_;
1351
1352   #cache: was pulled up in the original part_pkg query
1353   return $self->hashref->{"_opt_$opt"}
1354     if exists $self->hashref->{"_opt_$opt"};
1355
1356   cluck "$self -> option: searching for $opt" if $DEBUG;
1357   my $part_pkg_option =
1358     qsearchs('part_pkg_option', {
1359       pkgpart    => $self->pkgpart,
1360       optionname => $opt,
1361   } );
1362   return $part_pkg_option->optionvalue if $part_pkg_option;
1363
1364   my %plandata = map { /^(\w+)=(.*)$/; ( $1 => $2 ); }
1365                      split("\n", $self->get('plandata') );
1366   return $plandata{$opt} if exists $plandata{$opt};
1367   cluck "WARNING: (pkgpart ". $self->pkgpart. ") Package def option $opt ".
1368         "not found in options or plandata!\n"
1369     unless $ornull;
1370
1371   '';
1372 }
1373
1374 =item fcc_option OPTIONNAME
1375
1376 Returns the FCC 477 report option value for the given name, or the empty 
1377 string.
1378
1379 =cut
1380
1381 sub fcc_option {
1382   my ($self, $name) = @_;
1383   my $part_pkg_fcc_option =
1384     qsearchs('part_pkg_fcc_option', {
1385         pkgpart => $self->pkgpart,
1386         fccoptionname => $name,
1387     });
1388   $part_pkg_fcc_option ? $part_pkg_fcc_option->optionvalue : '';
1389 }
1390
1391 =item fcc_options
1392
1393 Returns all FCC 477 report options for this package, as a hash-like list.
1394
1395 =cut
1396
1397 sub fcc_options {
1398   my $self = shift;
1399   map { $_->fccoptionname => $_->optionvalue }
1400     qsearch('part_pkg_fcc_option', { pkgpart => $self->pkgpart });
1401 }
1402
1403 =item bill_part_pkg_link
1404
1405 Returns the associated part_pkg_link records (see L<FS::part_pkg_link>).
1406
1407 =cut
1408
1409 sub bill_part_pkg_link {
1410   shift->_part_pkg_link('bill', @_);
1411 }
1412
1413 =item svc_part_pkg_link
1414
1415 Returns the associated part_pkg_link records (see L<FS::part_pkg_link>).
1416
1417 =cut
1418
1419 sub svc_part_pkg_link {
1420   shift->_part_pkg_link('svc', @_);
1421 }
1422
1423 =item supp_part_pkg_link
1424
1425 Returns the associated part_pkg_link records of type 'supp' (supplemental
1426 packages).
1427
1428 =cut
1429
1430 sub supp_part_pkg_link {
1431   shift->_part_pkg_link('supp', @_);
1432 }
1433
1434 sub _part_pkg_link {
1435   my( $self, $type ) = @_;
1436
1437   return @{ $cache_link{$type}->{$self->pkgpart} }
1438     if $cache_enabled && $cache_link{$type}->{$self->pkgpart};
1439
1440   cluck $type.'_part_pkg_link called' if $DEBUG;
1441
1442   my @ppl = 
1443     qsearch({ table    => 'part_pkg_link',
1444               hashref  => { src_pkgpart => $self->pkgpart,
1445                             link_type   => $type,
1446                             #protection against infinite recursive links
1447                             dst_pkgpart => { op=>'!=', value=> $self->pkgpart },
1448                           },
1449               order_by => "ORDER BY hidden",
1450            });
1451
1452   $cache_link{$type}->{$self->pkgpart} = \@ppl if $cache_enabled;
1453
1454   return @ppl;
1455 }
1456
1457 sub self_and_bill_linked {
1458   shift->_self_and_linked('bill', @_);
1459 }
1460
1461 sub self_and_svc_linked {
1462   shift->_self_and_linked('svc', @_);
1463 }
1464
1465 sub _self_and_linked {
1466   my( $self, $type, $hidden ) = @_;
1467   $hidden ||= '';
1468
1469   my @result = ();
1470   foreach ( ( $self, map { $_->dst_pkg->_self_and_linked($type, $_->hidden) }
1471                      $self->_part_pkg_link($type) ) )
1472   {
1473     $_->hidden($hidden) if $hidden;
1474     push @result, $_;
1475   }
1476
1477   (@result);
1478 }
1479
1480 =item part_pkg_taxoverride [ CLASS ]
1481
1482 Returns all associated FS::part_pkg_taxoverride objects (see
1483 L<FS::part_pkg_taxoverride>).  Limits the returned set to those
1484 of class CLASS if defined.  Class may be one of 'setup', 'recur',
1485 the empty string (default), or a usage class number (see L<FS::usage_class>).
1486 When a class is specified, the empty string class (default) is returned
1487 if no more specific values exist.
1488
1489 =cut
1490
1491 sub part_pkg_taxoverride {
1492   my $self = shift;
1493   my $class = shift;
1494
1495   my $hashref = { 'pkgpart' => $self->pkgpart };
1496   $hashref->{'usage_class'} = $class if defined($class);
1497   my @overrides = qsearch('part_pkg_taxoverride', $hashref );
1498
1499   unless ( scalar(@overrides) || !defined($class) || !$class ){
1500     $hashref->{'usage_class'} = '';
1501     @overrides = qsearch('part_pkg_taxoverride', $hashref );
1502   }
1503
1504   @overrides;
1505 }
1506
1507 =item has_taxproduct
1508
1509 Returns true if this package has any taxproduct associated with it.  
1510
1511 =cut
1512
1513 sub has_taxproduct {
1514   my $self = shift;
1515
1516   $self->taxproductnum ||
1517   scalar( grep { $_ =~/^usage_taxproductnum_/ && $self->option($_) } 
1518           keys %{ {$self->options} }
1519   )
1520
1521 }
1522
1523
1524 =item taxproduct [ CLASS ]
1525
1526 Returns the associated tax product for this package definition (see
1527 L<FS::part_pkg_taxproduct>).  CLASS may be one of 'setup', 'recur' or
1528 the usage classnum (see L<FS::usage_class>).  Returns the default
1529 tax product for this record if the more specific CLASS value does
1530 not exist.
1531
1532 =cut
1533
1534 sub taxproduct {
1535   my $self = shift;
1536   my $class = shift;
1537
1538   my $part_pkg_taxproduct;
1539
1540   my $taxproductnum = $self->taxproductnum;
1541   if ($class) { 
1542     my $class_taxproductnum = $self->option("usage_taxproductnum_$class", 1);
1543     $taxproductnum = $class_taxproductnum
1544       if $class_taxproductnum
1545   }
1546   
1547   $part_pkg_taxproduct =
1548     qsearchs( 'part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum } );
1549
1550   unless ($part_pkg_taxproduct || $taxproductnum eq $self->taxproductnum ) {
1551     $taxproductnum = $self->taxproductnum;
1552     $part_pkg_taxproduct =
1553       qsearchs( 'part_pkg_taxproduct', { 'taxproductnum' => $taxproductnum } );
1554   }
1555
1556   $part_pkg_taxproduct;
1557 }
1558
1559 =item taxproduct_description [ CLASS ]
1560
1561 Returns the description of the associated tax product for this package
1562 definition (see L<FS::part_pkg_taxproduct>).
1563
1564 =cut
1565
1566 sub taxproduct_description {
1567   my $self = shift;
1568   my $part_pkg_taxproduct = $self->taxproduct(@_);
1569   $part_pkg_taxproduct ? $part_pkg_taxproduct->description : '';
1570 }
1571
1572
1573 =item tax_rates DATA_PROVIDER, GEOCODE, [ CLASS ]
1574
1575 Returns the tax table entries (L<FS::tax_rate> objects) that apply to this
1576 package in the location specified by GEOCODE, for usage class CLASS (one of
1577 'setup', 'recur', null, or a C<usage_class> number).
1578
1579 =cut
1580
1581 sub tax_rates {
1582   my $self = shift;
1583   my ($vendor, $geocode, $class) = @_;
1584   # if this part_pkg is overridden into a specific taxclass, get that class
1585   my @taxclassnums = map { $_->taxclassnum } 
1586                      $self->part_pkg_taxoverride($class);
1587   # otherwise, get its tax product category
1588   if (!@taxclassnums) {
1589     my $part_pkg_taxproduct = $self->taxproduct($class);
1590     # If this isn't defined, then the class has no taxproduct designation,
1591     # so return no tax rates.
1592     return () if !$part_pkg_taxproduct;
1593
1594     # convert the taxproduct to the tax classes that might apply to it in 
1595     # $geocode
1596     @taxclassnums = map { $_->taxclassnum }
1597                     grep { $_->taxable eq 'Y' } # why do we need this?
1598                     $part_pkg_taxproduct->part_pkg_taxrate($geocode);
1599   }
1600   return unless @taxclassnums;
1601
1602   # then look up the actual tax_rate entries
1603   warn "Found taxclassnum values of ". join(',', @taxclassnums) ."\n"
1604       if $DEBUG;
1605   my $extra_sql = "AND taxclassnum IN (". join(',', @taxclassnums) . ")";
1606   my @taxes = qsearch({ 'table'     => 'tax_rate',
1607                         'hashref'   => { 'geocode'     => $geocode,
1608                                          'data_vendor' => $vendor,
1609                                          'disabled'    => '' },
1610                         'extra_sql' => $extra_sql,
1611                       });
1612   warn "Found taxes ". join(',', map {$_->taxnum} @taxes) ."\n"
1613       if $DEBUG;
1614
1615   return @taxes;
1616 }
1617
1618 =item part_pkg_discount
1619
1620 Returns the package to discount m2m records (see L<FS::part_pkg_discount>)
1621 for this package.
1622
1623 =cut
1624
1625 sub part_pkg_discount {
1626   my $self = shift;
1627   qsearch('part_pkg_discount', { 'pkgpart' => $self->pkgpart });
1628 }
1629
1630 =item part_pkg_usage
1631
1632 Returns the voice usage pools (see L<FS::part_pkg_usage>) defined for 
1633 this package.
1634
1635 =cut
1636
1637 sub part_pkg_usage {
1638   my $self = shift;
1639   qsearch('part_pkg_usage', { 'pkgpart' => $self->pkgpart });
1640 }
1641
1642 =item _rebless
1643
1644 Reblesses the object into the FS::part_pkg::PLAN class (if available), where
1645 PLAN is the object's I<plan> field.  There should be better docs
1646 on how to create new price plans, but until then, see L</NEW PLAN CLASSES>.
1647
1648 =cut
1649
1650 sub _rebless {
1651   my $self = shift;
1652   my $plan = $self->plan;
1653   unless ( $plan ) {
1654     cluck "no price plan found for pkgpart ". $self->pkgpart. "\n"
1655       if $DEBUG;
1656     return $self;
1657   }
1658   return $self if ref($self) =~ /::$plan$/; #already blessed into plan subclass
1659   my $class = ref($self). "::$plan";
1660   warn "reblessing $self into $class" if $DEBUG > 1;
1661   eval "use $class;";
1662   die $@ if $@;
1663   bless($self, $class) unless $@;
1664   $self;
1665 }
1666
1667 #fatal fallbacks
1668 sub calc_setup { die 'no calc_setup for '. shift->plan. "\n"; }
1669 sub calc_recur { die 'no calc_recur for '. shift->plan. "\n"; }
1670
1671 #fallback that return 0 for old legacy packages with no plan
1672 sub calc_remain { 0; }
1673 sub calc_units  { 0; }
1674
1675 #fallback for everything not based on flat.pm
1676 sub recur_temporality { 'upcoming'; }
1677 sub calc_cancel { 0; }
1678
1679 #fallback for everything except bulk.pm
1680 sub hide_svc_detail { 0; }
1681
1682 #fallback for packages that can't/won't summarize usage
1683 sub sum_usage { 0; }
1684
1685 =item recur_cost_permonth CUST_PKG
1686
1687 recur_cost divided by freq (only supported for monthly and longer frequencies)
1688
1689 =cut
1690
1691 sub recur_cost_permonth {
1692   my($self, $cust_pkg) = @_;
1693   return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
1694   sprintf('%.2f', ($self->recur_cost || 0) / $self->freq );
1695 }
1696
1697 =item cust_bill_pkg_recur CUST_PKG
1698
1699 Actual recurring charge for the specified customer package from customer's most
1700 recent invoice
1701
1702 =cut
1703
1704 sub cust_bill_pkg_recur {
1705   my($self, $cust_pkg) = @_;
1706   my $cust_bill_pkg = qsearchs({
1707     'table'     => 'cust_bill_pkg',
1708     'addl_from' => 'LEFT JOIN cust_bill USING ( invnum )',
1709     'hashref'   => { 'pkgnum' => $cust_pkg->pkgnum,
1710                      'recur'  => { op=>'>', value=>'0' },
1711                    },
1712     'order_by'  => 'ORDER BY cust_bill._date     DESC,
1713                              cust_bill_pkg.sdate DESC
1714                      LIMIT 1
1715                    ',
1716   }) or return 0; #die "use cust_bill_pkg_recur credits with once_perinv condition";
1717   $cust_bill_pkg->recur;
1718 }
1719
1720 =item unit_setup CUST_PKG
1721
1722 Returns the setup fee for one unit of the package.
1723
1724 =cut
1725
1726 sub unit_setup {
1727   my ($self, $cust_pkg) = @_;
1728   $self->option('setup_fee') || 0;
1729 }
1730
1731 =item setup_margin
1732
1733 unit_setup minus setup_cost
1734
1735 =cut
1736
1737 sub setup_margin {
1738   my $self = shift;
1739   $self->unit_setup(@_) - ($self->setup_cost || 0);
1740 }
1741
1742 =item recur_margin_permonth
1743
1744 base_recur_permonth minus recur_cost_permonth
1745
1746 =cut
1747
1748 sub recur_margin_permonth {
1749   my $self = shift;
1750   $self->base_recur_permonth(@_) - $self->recur_cost_permonth(@_);
1751 }
1752
1753 =item intro_end PACKAGE
1754
1755 Takes an L<FS::cust_pkg> object.  If this plan has an introductory rate,
1756 returns the expected date the intro period will end. If there is no intro
1757 rate, returns zero.
1758
1759 =cut
1760
1761 sub intro_end {
1762   0;
1763 }
1764
1765 =item format OPTION DATA
1766
1767 Returns data formatted according to the function 'format' described
1768 in the plan info.  Returns DATA if no such function exists.
1769
1770 =cut
1771
1772 sub format {
1773   my ($self, $option, $data) = (shift, shift, shift);
1774   if (exists($plans{$self->plan}->{fields}->{$option}{format})) {
1775     &{$plans{$self->plan}->{fields}->{$option}{format}}($data);
1776   }else{
1777     $data;
1778   }
1779 }
1780
1781 =item parse OPTION DATA
1782
1783 Returns data parsed according to the function 'parse' described
1784 in the plan info.  Returns DATA if no such function exists.
1785
1786 =cut
1787
1788 sub parse {
1789   my ($self, $option, $data) = (shift, shift, shift);
1790   if (exists($plans{$self->plan}->{fields}->{$option}{parse})) {
1791     &{$plans{$self->plan}->{fields}->{$option}{parse}}($data);
1792   }else{
1793     $data;
1794   }
1795 }
1796
1797 =back
1798
1799 =cut
1800
1801 =head1 CLASS METHODS
1802
1803 =over 4
1804
1805 =cut
1806
1807 # _upgrade_data
1808 #
1809 # Used by FS::Upgrade to migrate to a new database.
1810
1811 sub _upgrade_data { # class method
1812   my($class, %opts) = @_;
1813
1814   warn "[FS::part_pkg] upgrading $class\n" if $DEBUG;
1815
1816   my @part_pkg = qsearch({
1817     'table'     => 'part_pkg',
1818     'extra_sql' => "WHERE ". join(' OR ',
1819                      'plan IS NULL', "plan = '' ",
1820                    ),
1821   });
1822
1823   foreach my $part_pkg (@part_pkg) {
1824
1825     unless ( $part_pkg->plan ) {
1826       $part_pkg->plan('flat');
1827     }
1828
1829     $part_pkg->replace;
1830
1831   }
1832   # the rest can be done asynchronously
1833 }
1834
1835 sub queueable_upgrade {
1836   # now upgrade to the explicit custom flag
1837
1838   my $search = FS::Cursor->new({
1839     'table'     => 'part_pkg',
1840     'hashref'   => { disabled => 'Y', custom => '' },
1841     'extra_sql' => "AND comment LIKE '(CUSTOM) %'",
1842   });
1843   my $dbh = dbh;
1844
1845   while (my $part_pkg = $search->fetch) {
1846     my $new = new FS::part_pkg { $part_pkg->hash };
1847     $new->custom('Y');
1848     my $comment = $part_pkg->comment;
1849     $comment =~ s/^\(CUSTOM\) //;
1850     $comment = '(none)' unless $comment =~ /\S/;
1851     $new->comment($comment);
1852
1853     my $pkg_svc = { map { $_->svcpart => $_->quantity } $part_pkg->pkg_svc };
1854     my $primary = $part_pkg->svcpart;
1855     my $options = { $part_pkg->options };
1856
1857     my $error = $new->replace( $part_pkg,
1858                                'pkg_svc'     => $pkg_svc,
1859                                'primary_svc' => $primary,
1860                                'options'     => $options,
1861                              );
1862     if ($error) {
1863       warn "pkgpart#".$part_pkg->pkgpart.": $error\n";
1864       $dbh->rollback;
1865     } else {
1866       $dbh->commit;
1867     }
1868   }
1869
1870   # set family_pkgpart on any packages that don't have it
1871   $search = FS::Cursor->new('part_pkg', { 'family_pkgpart' => '' });
1872   while (my $part_pkg = $search->fetch) {
1873     $part_pkg->set('family_pkgpart' => $part_pkg->pkgpart);
1874     my $error = $part_pkg->SUPER::replace;
1875     if ($error) {
1876       warn "pkgpart#".$part_pkg->pkgpart.": $error\n";
1877       $dbh->rollback;
1878     } else {
1879       $dbh->commit;
1880     }
1881   }
1882
1883   my @part_pkg_option = qsearch('part_pkg_option',
1884     { 'optionname'  => 'unused_credit',
1885       'optionvalue' => 1,
1886     });
1887   foreach my $old_opt (@part_pkg_option) {
1888     my $pkgpart = $old_opt->pkgpart;
1889     my $error = $old_opt->delete;
1890     die $error if $error;
1891
1892     foreach (qw(unused_credit_cancel unused_credit_change)) {
1893       my $new_opt = new FS::part_pkg_option {
1894         'pkgpart'     => $pkgpart,
1895         'optionname'  => $_,
1896         'optionvalue' => 1,
1897       };
1898       $error = $new_opt->insert;
1899       die $error if $error;
1900     }
1901   }
1902
1903   # migrate use_disposition_taqua and use_disposition to disposition_in
1904   @part_pkg_option = qsearch('part_pkg_option',
1905     { 'optionname'  => { op => 'LIKE',
1906                          value => 'use_disposition%',
1907                        },
1908       'optionvalue' => 1,
1909     });
1910   my %newopts = map { $_->pkgpart => $_ } 
1911     qsearch('part_pkg_option',  { 'optionname'  => 'disposition_in', } );
1912   foreach my $old_opt (@part_pkg_option) {
1913         my $pkgpart = $old_opt->pkgpart;
1914         my $newval = $old_opt->optionname eq 'use_disposition_taqua' ? '100' 
1915                                                                   : 'ANSWERED';
1916         my $error = $old_opt->delete;
1917         die $error if $error;
1918
1919         if ( exists($newopts{$pkgpart}) ) {
1920             my $opt = $newopts{$pkgpart};
1921             $opt->optionvalue($opt->optionvalue.",$newval");
1922             $error = $opt->replace;
1923             die $error if $error;
1924         } else {
1925             my $new_opt = new FS::part_pkg_option {
1926                 'pkgpart'     => $pkgpart,
1927                 'optionname'  => 'disposition_in',
1928                 'optionvalue' => $newval,
1929               };
1930               $error = $new_opt->insert;
1931               die $error if $error;
1932               $newopts{$pkgpart} = $new_opt;
1933         }
1934   }
1935
1936   # set any package with FCC voice lines to the "VoIP with broadband" category
1937   # for backward compatibility
1938   #
1939   # recover from a bad upgrade bug
1940   my $upgrade = 'part_pkg_fcc_voip_class_FIX';
1941   if (!FS::upgrade_journal->is_done($upgrade)) {
1942     my $bad_upgrade = qsearchs('upgrade_journal', 
1943       { upgrade => 'part_pkg_fcc_voip_class' }
1944     );
1945     if ( $bad_upgrade ) {
1946       my $where = 'WHERE history_date <= '.$bad_upgrade->_date.
1947                   ' AND  history_date >  '.($bad_upgrade->_date - 3600);
1948       my @h_part_pkg_option = map { FS::part_pkg_option->new($_->hashref) }
1949         qsearch({
1950           'select'    => '*',
1951           'table'     => 'h_part_pkg_option',
1952           'hashref'   => {},
1953           'extra_sql' => "$where AND history_action = 'delete'",
1954           'order_by'  => 'ORDER BY history_date ASC',
1955         });
1956       my @h_pkg_svc = map { FS::pkg_svc->new($_->hashref) }
1957         qsearch({
1958           'select'    => '*',
1959           'table'     => 'h_pkg_svc',
1960           'hashref'   => {},
1961           'extra_sql' => "$where AND history_action = 'replace_old'",
1962           'order_by'  => 'ORDER BY history_date ASC',
1963         });
1964       my %opt;
1965       foreach my $deleted (@h_part_pkg_option, @h_pkg_svc) {
1966         my $pkgpart ||= $deleted->pkgpart;
1967         $opt{$pkgpart} ||= {
1968           options => {},
1969           pkg_svc => {},
1970           primary_svc => '',
1971           hidden_svc => {},
1972         };
1973         if ( $deleted->isa('FS::part_pkg_option') ) {
1974           $opt{$pkgpart}{options}{ $deleted->optionname } = $deleted->optionvalue;
1975         } else { # pkg_svc
1976           my $svcpart = $deleted->svcpart;
1977           $opt{$pkgpart}{pkg_svc}{$svcpart} = $deleted->quantity;
1978           $opt{$pkgpart}{hidden_svc}{$svcpart} ||= $deleted->hidden;
1979           $opt{$pkgpart}{primary_svc} = $svcpart if $deleted->primary_svc;
1980         }
1981       }
1982       foreach my $pkgpart (keys %opt) {
1983         my $part_pkg = FS::part_pkg->by_key($pkgpart);
1984         my $error = $part_pkg->replace( $part_pkg->replace_old, $opt{$pkgpart} );
1985         if ( $error ) {
1986           die "error recovering damaged pkgpart $pkgpart:\n$error\n";
1987         }
1988       }
1989     } # $bad_upgrade exists
1990     else { # do the original upgrade, but correctly this time
1991       my @part_pkg = qsearch('part_pkg', {
1992           fcc_ds0s        => { op => '>', value => 0 },
1993           fcc_voip_class  => ''
1994       });
1995       foreach my $part_pkg (@part_pkg) {
1996         $part_pkg->set(fcc_voip_class => 2);
1997         my @pkg_svc = $part_pkg->pkg_svc;
1998         my %quantity = map {$_->svcpart, $_->quantity} @pkg_svc;
1999         my %hidden   = map {$_->svcpart, $_->hidden  } @pkg_svc;
2000         my $error = $part_pkg->replace(
2001           $part_pkg->replace_old,
2002           options     => { $part_pkg->options },
2003           pkg_svc     => \%quantity,
2004           hidden_svc  => \%hidden,
2005           primary_svc => ($part_pkg->svcpart || ''),
2006         );
2007         die $error if $error;
2008       }
2009     }
2010     FS::upgrade_journal->set_done($upgrade);
2011   }
2012
2013   # remove custom flag from one-time charge packages that were accidentally
2014   # flagged as custom
2015   $search = FS::Cursor->new({
2016     'table'   => 'part_pkg',
2017     'hashref' => { 'freq'   => '0',
2018                    'custom' => 'Y',
2019                    'family_pkgpart' => { op => '!=', value => '' },
2020                  },
2021     'addl_from' => ' JOIN
2022   (select pkgpart from cust_pkg group by pkgpart having count(*) = 1)
2023     AS singular_pkg USING (pkgpart)',
2024   });
2025   my @fields = grep {     $_ ne 'pkgpart'
2026                       and $_ ne 'custom'
2027                       and $_ ne 'disabled' } FS::part_pkg->fields;
2028   PKGPART: while (my $part_pkg = $search->fetch) {
2029     # can't merge the package back into its parent (too late for that)
2030     # but we can remove the custom flag if it's not actually customized,
2031     # i.e. nothing has been changed.
2032
2033     my $family_pkgpart = $part_pkg->family_pkgpart;
2034     next PKGPART if $family_pkgpart == $part_pkg->pkgpart;
2035     my $parent_pkg = FS::part_pkg->by_key($family_pkgpart);
2036     foreach my $field (@fields) {
2037       if ($part_pkg->get($field) ne $parent_pkg->get($field)) {
2038         next PKGPART;
2039       }
2040     }
2041     # options have to be identical too
2042     # but links, FCC options, discount plans, and usage packages can't be
2043     # changed through the "modify charge" UI, so skip them
2044     my %newopt = $part_pkg->options;
2045     my %oldopt = $parent_pkg->options;
2046     OPTION: foreach my $option (keys %newopt) {
2047       if (delete $newopt{$option} ne delete $oldopt{$option}) {
2048         next PKGPART;
2049       }
2050     }
2051     if (keys(%newopt) or keys(%oldopt)) {
2052       next PKGPART;
2053     }
2054     # okay, now replace it
2055     warn "Removing custom flag from part_pkg#".$part_pkg->pkgpart."\n";
2056     $part_pkg->set('custom', '');
2057     my $error = $part_pkg->replace;
2058     die $error if $error;
2059   } # $search->fetch
2060
2061   return;
2062 }
2063
2064 =item curuser_pkgs_sql
2065
2066 Returns an SQL fragment for searching for packages the current user can
2067 use, either via part_pkg.agentnum directly, or via agent type (see
2068 L<FS::type_pkgs>).
2069
2070 =cut
2071
2072 sub curuser_pkgs_sql {
2073   my $class = shift;
2074
2075   $class->_pkgs_sql( $FS::CurrentUser::CurrentUser->agentnums );
2076
2077 }
2078
2079 =item agent_pkgs_sql AGENT | AGENTNUM, ...
2080
2081 Returns an SQL fragment for searching for packages the provided agent or agents
2082 can use, either via part_pkg.agentnum directly, or via agent type (see
2083 L<FS::type_pkgs>).
2084
2085 =cut
2086
2087 sub agent_pkgs_sql {
2088   my $class = shift;  #i'm a class method, not a sub (the question is... why??)
2089   my @agentnums = map { ref($_) ? $_->agentnum : $_ } @_;
2090
2091   $class->_pkgs_sql(@agentnums); #is this why
2092
2093 }
2094
2095 sub _pkgs_sql {
2096   my( $class, @agentnums ) = @_;
2097   my $agentnums = join(',', @agentnums);
2098
2099   "
2100     (
2101       ( agentnum IS NOT NULL AND agentnum IN ($agentnums) )
2102       OR ( agentnum IS NULL
2103            AND EXISTS ( SELECT 1
2104                           FROM type_pkgs
2105                             LEFT JOIN agent_type USING ( typenum )
2106                             LEFT JOIN agent AS typeagent USING ( typenum )
2107                           WHERE type_pkgs.pkgpart = part_pkg.pkgpart
2108                             AND typeagent.agentnum IN ($agentnums)
2109                       )
2110          )
2111     )
2112   ";
2113
2114 }
2115
2116 =back
2117
2118 =head1 SUBROUTINES
2119
2120 =over 4
2121
2122 =item plan_info
2123
2124 =cut
2125
2126 #false laziness w/part_export & cdr
2127 my %info;
2128 foreach my $INC ( @INC ) {
2129   warn "globbing $INC/FS/part_pkg/[a-z]*.pm\n" if $DEBUG;
2130   foreach my $file ( glob("$INC/FS/part_pkg/[a-z]*.pm") ) {
2131     warn "attempting to load plan info from $file\n" if $DEBUG;
2132     $file =~ /\/(\w+)\.pm$/ or do {
2133       warn "unrecognized file in $INC/FS/part_pkg/: $file\n";
2134       next;
2135     };
2136     my $mod = $1;
2137     my $info = eval "use FS::part_pkg::$mod; ".
2138                     "\\%FS::part_pkg::$mod\::info;";
2139     if ( $@ ) {
2140       die "error using FS::part_pkg::$mod (skipping): $@\n" if $@;
2141       next;
2142     }
2143     unless ( keys %$info ) {
2144       warn "no %info hash found in FS::part_pkg::$mod, skipping\n";
2145       next;
2146     }
2147     warn "got plan info from FS::part_pkg::$mod: $info\n" if $DEBUG;
2148     #if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
2149     #  warn "skipping disabled plan FS::part_pkg::$mod" if $DEBUG;
2150     #  next;
2151     #}
2152     $info{$mod} = $info;
2153     $info->{'weight'} ||= 0; # quiet warnings
2154   }
2155 }
2156
2157 # copy one level deep to allow replacement of fields and fieldorder
2158 tie %plans, 'Tie::IxHash',
2159   map  { my %infohash = %{ $info{$_} }; 
2160           $_ => \%infohash }
2161   sort { $info{$a}->{'weight'} <=> $info{$b}->{'weight'} }
2162   keys %info;
2163
2164 # inheritance of plan options
2165 foreach my $name (keys(%info)) {
2166   if (exists($info{$name}->{'disabled'}) and $info{$name}->{'disabled'}) {
2167     warn "skipping disabled plan FS::part_pkg::$name" if $DEBUG;
2168     delete $plans{$name};
2169     next;
2170   }
2171   my $parents = $info{$name}->{'inherit_fields'} || [];
2172   my (%fields, %field_exists, @fieldorder);
2173   foreach my $parent ($name, @$parents) {
2174     if ( !exists($info{$parent}) ) {
2175       warn "$name tried to inherit from nonexistent '$parent'\n";
2176       next;
2177     }
2178     %fields = ( # avoid replacing existing fields
2179       %{ $info{$parent}->{'fields'} || {} },
2180       %fields
2181     );
2182     foreach (@{ $info{$parent}->{'fieldorder'} || [] }) {
2183       # avoid duplicates
2184       next if $field_exists{$_};
2185       $field_exists{$_} = 1;
2186       # allow inheritors to remove inherited fields from the fieldorder
2187       push @fieldorder, $_ if !exists($fields{$_}) or
2188                               !exists($fields{$_}->{'disabled'});
2189     }
2190   }
2191   $plans{$name}->{'fields'} = \%fields;
2192   $plans{$name}->{'fieldorder'} = \@fieldorder;
2193 }
2194
2195 sub plan_info {
2196   \%plans;
2197 }
2198
2199
2200 =back
2201
2202 =head1 NEW PLAN CLASSES
2203
2204 A module should be added in FS/FS/part_pkg/  Eventually, an example may be
2205 found in eg/plan_template.pm.  Until then, it is suggested that you use the
2206 other modules in FS/FS/part_pkg/ as a guide.
2207
2208 =head1 BUGS
2209
2210 The delete method is unimplemented.
2211
2212 setup and recur semantics are not yet defined (and are implemented in
2213 FS::cust_bill.  hmm.).  now they're deprecated and need to go.
2214
2215 plandata should go
2216
2217 part_pkg_taxrate is Pg specific
2218
2219 replace should be smarter about managing the related tables (options, pkg_svc)
2220
2221 =head1 SEE ALSO
2222
2223 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
2224 schema.html from the base documentation.
2225
2226 =cut
2227
2228 1;