1 package FS::cust_main::Packages;
4 use vars qw( $DEBUG $me );
5 use List::Util qw( min );
7 use FS::Record qw( qsearch );
12 $me = '[FS::cust_main::Packages]';
16 FS::cust_main::Packages - Packages mixin for cust_main
22 These methods are available on FS::cust_main objects;
28 =item order_pkg HASHREF | OPTION => VALUE ...
30 Orders a single package.
32 Note that if the package definition has supplemental packages, those will
35 Options may be passed as a list of key/value pairs or as a hash reference.
46 Optional FS::cust_location object. If not specified, the customer's
47 ship_location will be used.
51 Optional arryaref of FS::svc_* service objects.
55 If this option is set to a job queue jobnum (see L<FS::queue>), all provisioning
56 jobs will have a dependancy on the supplied job (they will not run until the
57 specific job completes). This can be used to defer provisioning until some
58 action completes (such as running the customer's credit card successfully).
62 Optional subject for a ticket created and attached to this customer
66 Optional queue name for ticket additions
74 my $opt = ref($_[0]) ? shift : { @_ };
76 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
78 warn "$me order_pkg called with options ".
79 join(', ', map { "$_: $opt->{$_}" } keys %$opt ). "\n"
82 my $cust_pkg = $opt->{'cust_pkg'};
83 my $svcs = $opt->{'svcs'} || [];
86 $svc_options{'depend_jobnum'} = $opt->{'depend_jobnum'}
87 if exists($opt->{'depend_jobnum'}) && $opt->{'depend_jobnum'};
89 my %insert_params = map { $opt->{$_} ? ( $_ => $opt->{$_} ) : () }
90 qw( ticket_subject ticket_queue allow_pkgpart );
92 local $SIG{HUP} = 'IGNORE';
93 local $SIG{INT} = 'IGNORE';
94 local $SIG{QUIT} = 'IGNORE';
95 local $SIG{TERM} = 'IGNORE';
96 local $SIG{TSTP} = 'IGNORE';
97 local $SIG{PIPE} = 'IGNORE';
99 my $oldAutoCommit = $FS::UID::AutoCommit;
100 local $FS::UID::AutoCommit = 0;
103 if ( $opt->{'locationnum'} and $opt->{'locationnum'} != -1 ) {
105 $cust_pkg->locationnum($opt->{'locationnum'});
107 } elsif ( $opt->{'cust_location'} ) {
109 if ( ! $opt->{'cust_location'}->locationnum ) {
111 my $error = $opt->{'cust_location'}->insert;
113 $dbh->rollback if $oldAutoCommit;
114 return "inserting cust_location (transaction rolled back): $error";
117 $cust_pkg->locationnum($opt->{'cust_location'}->locationnum);
121 $cust_pkg->locationnum($self->ship_locationnum);
125 $cust_pkg->custnum( $self->custnum );
127 my $error = $cust_pkg->insert( %insert_params );
129 $dbh->rollback if $oldAutoCommit;
130 return "inserting cust_pkg (transaction rolled back): $error";
133 foreach my $svc_something ( @{ $opt->{'svcs'} } ) {
134 if ( $svc_something->svcnum ) {
135 my $old_cust_svc = $svc_something->cust_svc;
136 my $new_cust_svc = new FS::cust_svc { $old_cust_svc->hash };
137 $new_cust_svc->pkgnum( $cust_pkg->pkgnum);
138 $error = $new_cust_svc->replace($old_cust_svc);
140 $svc_something->pkgnum( $cust_pkg->pkgnum );
141 if ( $svc_something->isa('FS::svc_acct') ) {
142 foreach ( grep { $opt->{$_.'_ref'} && ${ $opt->{$_.'_ref'} } }
143 qw( seconds upbytes downbytes totalbytes ) ) {
144 $svc_something->$_( $svc_something->$_() + ${ $opt->{$_.'_ref'} } );
145 ${ $opt->{$_.'_ref'} } = 0;
148 $error = $svc_something->insert(%svc_options);
151 $dbh->rollback if $oldAutoCommit;
152 return "inserting svc_ (transaction rolled back): $error";
156 # add supplemental packages, if any are needed
157 my $part_pkg = FS::part_pkg->by_key($cust_pkg->pkgpart);
158 foreach my $link ($part_pkg->supp_part_pkg_link) {
159 #warn "inserting supplemental package ".$link->dst_pkgpart;
160 my $pkg = FS::cust_pkg->new({
161 'pkgpart' => $link->dst_pkgpart,
162 'pkglinknum' => $link->pkglinknum,
163 'custnum' => $self->custnum,
164 'main_pkgnum' => $cust_pkg->pkgnum,
165 'locationnum' => $cust_pkg->locationnum,
166 # try to prevent as many surprises as possible
167 'pkgbatch' => $cust_pkg->pkgbatch,
168 'start_date' => $cust_pkg->start_date,
169 'order_date' => $cust_pkg->order_date,
170 'expire' => $cust_pkg->expire,
171 'adjourn' => $cust_pkg->adjourn,
172 'contract_end' => $cust_pkg->contract_end,
173 'refnum' => $cust_pkg->refnum,
174 'discountnum' => $cust_pkg->discountnum,
175 'waive_setup' => $cust_pkg->waive_setup,
176 'allow_pkgpart' => $opt->{'allow_pkgpart'},
178 $error = $self->order_pkg('cust_pkg' => $pkg);
180 $dbh->rollback if $oldAutoCommit;
181 return "inserting supplemental package: $error";
185 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
190 =item order_pkgs HASHREF [ , OPTION => VALUE ... ]
192 Like the insert method on an existing record, this method orders multiple
193 packages and included services atomicaly. Pass a Tie::RefHash data structure
194 to this method containing FS::cust_pkg and FS::svc_I<tablename> objects.
195 There should be a better explanation of this, but until then, here's an
199 tie %hash, 'Tie::RefHash'; #this part is important
201 $cust_pkg => [ $svc_acct ],
204 $cust_main->order_pkgs( \%hash, 'noexport'=>1 );
206 Services can be new, in which case they are inserted, or existing unaudited
207 services, in which case they are linked to the newly-created package.
209 Currently available options are: I<depend_jobnum>, I<noexport>, I<seconds_ref>,
210 I<upbytes_ref>, I<downbytes_ref>, and I<totalbytes_ref>.
212 If I<depend_jobnum> is set, all provisioning jobs will have a dependancy
213 on the supplied jobnum (they will not run until the specific job completes).
214 This can be used to defer provisioning until some action completes (such
215 as running the customer's credit card successfully).
217 The I<noexport> option is deprecated. If I<noexport> is set true, no
218 provisioning jobs (exports) are scheduled. (You can schedule them later with
219 the B<reexport> method for each cust_pkg object. Using the B<reexport> method
220 on the cust_main object is not recommended, as existing services will also be
223 If I<seconds_ref>, I<upbytes_ref>, I<downbytes_ref>, or I<totalbytes_ref> is
224 provided, the scalars (provided by references) will be incremented by the
225 values of the prepaid card.`
231 my $cust_pkgs = shift;
234 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
236 warn "$me order_pkgs called with options ".
237 join(', ', map { "$_: $options{$_}" } keys %options ). "\n"
240 local $SIG{HUP} = 'IGNORE';
241 local $SIG{INT} = 'IGNORE';
242 local $SIG{QUIT} = 'IGNORE';
243 local $SIG{TERM} = 'IGNORE';
244 local $SIG{TSTP} = 'IGNORE';
245 local $SIG{PIPE} = 'IGNORE';
247 my $oldAutoCommit = $FS::UID::AutoCommit;
248 local $FS::UID::AutoCommit = 0;
251 local $FS::svc_Common::noexport_hack = 1 if $options{'noexport'};
253 foreach my $cust_pkg ( keys %$cust_pkgs ) {
255 my $error = $self->order_pkg(
256 'cust_pkg' => $cust_pkg,
257 'svcs' => $cust_pkgs->{$cust_pkg},
258 map { $_ => $options{$_} }
259 qw( seconds_ref upbytes_ref downbytes_ref totalbytes_ref depend_jobnum )
262 $dbh->rollback if $oldAutoCommit;
268 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
272 =item all_pkgs [ OPTION => VALUE... | EXTRA_QSEARCH_PARAMS_HASHREF ]
274 Returns all packages (see L<FS::cust_pkg>) for this customer.
280 my $extra_qsearch = ref($_[0]) ? shift : { @_ };
282 return $self->num_pkgs unless wantarray || keys %$extra_qsearch;
285 if ( $self->{'_pkgnum'} && ! keys %$extra_qsearch ) {
286 @cust_pkg = values %{ $self->{'_pkgnum'}->cache };
288 @cust_pkg = $self->_cust_pkg($extra_qsearch);
291 map { $_ } sort sort_packages @cust_pkg;
296 Synonym for B<all_pkgs>.
304 =item ncancelled_pkgs [ EXTRA_QSEARCH_PARAMS_HASHREF ]
306 Returns all non-cancelled packages (see L<FS::cust_pkg>) for this customer.
310 sub ncancelled_pkgs {
312 my $extra_qsearch = ref($_[0]) ? shift : {};
314 local($DEBUG) = $FS::cust_main::DEBUG if $FS::cust_main::DEBUG > $DEBUG;
316 return $self->num_ncancelled_pkgs unless wantarray;
319 if ( $self->{'_pkgnum'} ) {
321 warn "$me ncancelled_pkgs: returning cached objects"
324 @cust_pkg = grep { ! $_->getfield('cancel') }
325 values %{ $self->{'_pkgnum'}->cache };
329 warn "$me ncancelled_pkgs: searching for packages with custnum ".
333 $extra_qsearch->{'extra_sql'} .= ' AND ( cancel IS NULL OR cancel = 0 ) ';
335 @cust_pkg = $self->_cust_pkg($extra_qsearch);
339 sort sort_packages @cust_pkg;
345 my $extra_qsearch = ref($_[0]) ? shift : {};
347 $extra_qsearch->{'select'} ||= '*';
348 $extra_qsearch->{'select'} .=
349 ',( SELECT COUNT(*) FROM cust_svc WHERE cust_pkg.pkgnum = cust_svc.pkgnum )
353 $_->{'_num_cust_svc'} = $_->get('_num_cust_svc');
358 'table' => 'cust_pkg',
359 'hashref' => { 'custnum' => $self->custnum },
364 # This should be generalized to use config options to determine order.
367 my $locationsort = ( $a->locationnum || 0 ) <=> ( $b->locationnum || 0 );
368 return $locationsort if $locationsort;
370 if ( $a->get('cancel') xor $b->get('cancel') ) {
371 return -1 if $b->get('cancel');
372 return 1 if $a->get('cancel');
373 #shouldn't get here...
376 my $a_num_cust_svc = $a->num_cust_svc;
377 my $b_num_cust_svc = $b->num_cust_svc;
378 return 0 if !$a_num_cust_svc && !$b_num_cust_svc;
379 return -1 if $a_num_cust_svc && !$b_num_cust_svc;
380 return 1 if !$a_num_cust_svc && $b_num_cust_svc;
381 my @a_cust_svc = $a->cust_svc;
382 my @b_cust_svc = $b->cust_svc;
383 return 0 if !scalar(@a_cust_svc) && !scalar(@b_cust_svc);
384 return -1 if scalar(@a_cust_svc) && !scalar(@b_cust_svc);
385 return 1 if !scalar(@a_cust_svc) && scalar(@b_cust_svc);
386 $a_cust_svc[0]->svc_x->label cmp $b_cust_svc[0]->svc_x->label;
393 Returns all suspended packages (see L<FS::cust_pkg>) for this customer.
399 return $self->num_suspended_pkgs unless wantarray;
400 grep { $_->susp } $self->ncancelled_pkgs;
403 =item unflagged_suspended_pkgs
405 Returns all unflagged suspended packages (see L<FS::cust_pkg>) for this
406 customer (thouse packages without the `manual_flag' set).
410 sub unflagged_suspended_pkgs {
412 return $self->suspended_pkgs
413 unless dbdef->table('cust_pkg')->column('manual_flag');
414 grep { ! $_->manual_flag } $self->suspended_pkgs;
417 =item unsuspended_pkgs
419 Returns all unsuspended (and uncancelled) packages (see L<FS::cust_pkg>) for
424 sub unsuspended_pkgs {
426 return $self->num_unsuspended_pkgs unless wantarray;
427 grep { ! $_->susp } $self->ncancelled_pkgs;
432 Returns all unsuspended (and uncancelled) packages (see L<FS::cust_pkg>) for
433 this customer that are active (recurring).
439 grep { my $part_pkg = $_->part_pkg;
440 $part_pkg->freq ne '' && $part_pkg->freq ne '0';
442 $self->unsuspended_pkgs;
447 Returns active packages, and also any suspended packages which are set to
448 continue billing while suspended.
454 grep { my $part_pkg = $_->part_pkg;
455 $part_pkg->freq ne '' && $part_pkg->freq ne '0'
456 && ( ! $_->susp || $_->option('suspend_bill',1)
457 || ( $part_pkg->option('suspend_bill', 1)
458 && ! $_->option('no_suspend_bill',1)
462 $self->ncancelled_pkgs;
467 Returns the next date this customer will be billed, as a UNIX timestamp, or
468 undef if no billing package has a next bill date.
474 min( map $_->get('bill'), grep $_->get('bill'), $self->billing_pkgs );
477 =item num_cancelled_pkgs
479 Returns the number of cancelled packages (see L<FS::cust_pkg>) for this
484 sub num_cancelled_pkgs {
485 shift->num_pkgs("cust_pkg.cancel IS NOT NULL AND cust_pkg.cancel != 0");
488 sub num_ncancelled_pkgs {
489 shift->num_pkgs("( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )");
492 sub num_suspended_pkgs {
493 shift->num_pkgs(" ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
494 AND cust_pkg.susp IS NOT NULL AND cust_pkg.susp != 0 ");
497 sub num_unsuspended_pkgs {
498 shift->num_pkgs(" ( cust_pkg.cancel IS NULL OR cust_pkg.cancel = 0 )
499 AND ( cust_pkg.susp IS NULL OR cust_pkg.susp = 0 ) ");
504 my $sql = scalar(@_) ? shift : '';
505 $sql = "AND $sql" if $sql && $sql !~ /^\s*$/ && $sql !~ /^\s*AND/i;
506 my $sth = dbh->prepare(
507 "SELECT COUNT(*) FROM cust_pkg WHERE custnum = ? $sql"
508 ) or die dbh->errstr;
509 $sth->execute($self->custnum) or die $sth->errstr;
510 $sth->fetchrow_arrayref->[0];
519 L<FS::cust_main>, L<FS::cust_pkg>