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