Difference between revisions of "Freeside:3:Documentation:Developer/FS/cust pkg"

From Freeside
Jump to: navigation, search
m (Edit via perl MediaWiki framework (1.13))
(No difference)

Revision as of 20:04, 27 June 2012

NAME

FS::cust_pkg - Object methods for cust_pkg objects

SYNOPSIS

 use FS::cust_pkg;

 $record = new FS::cust_pkg \%hash;
 $record = new FS::cust_pkg { 'column' => 'value' };

 $error = $record->insert;

 $error = $new_record->replace($old_record);

 $error = $record->delete;

 $error = $record->check;

 $error = $record->cancel;

 $error = $record->suspend;

 $error = $record->unsuspend;

 $part_pkg = $record->part_pkg;

 @labels = $record->labels;

 $seconds = $record->seconds_since($timestamp);

 $error = FS::cust_pkg::order( $custnum, \@pkgparts );
 $error = FS::cust_pkg::order( $custnum, \@pkgparts, \@remove_pkgnums ] );

DESCRIPTION

An FS::cust_pkg object represents a customer billing item. FS::cust_pkg inherits from FS::Record. The following fields are currently supported:

pkgnum
Primary key (assigned automatically for new billing items)
custnum
Customer (see FS::cust_main)
pkgpart
Billing item definition (see FS::part_pkg)
locationnum
Optional link to package location (see FS::location)
order_date
date package was ordered (also remains same on changes)
start_date
date
setup
date
bill
date (next bill date)
last_bill
last bill date
adjourn
date
susp
date
expire
date
contract_end
date
cancel
date
usernum
order taker (see FS::access_user)
manual_flag
If this field is set to 1, disables the automatic unsuspension of this package when using the unsuspendauto config option.
quantity
If not set, defaults to 1
change_date
Date of change from previous package
change_pkgnum
Previous pkgnum
change_pkgpart
Previous pkgpart
change_locationnum
Previous locationnum
waive_setup

Note: setup, last_bill, bill, adjourn, susp, expire, cancel and change_date are specified as UNIX timestamps; see "time" in perlfunc. Also see Time::Local and Date::Parse for conversion functions.

METHODS

new HASHREF
Create a new billing item. To add the item to the database, see "insert".
insert [ OPTION => VALUE ... ]
Adds this billing item to the database ("Orders" the item). If there is an error, returns the error, otherwise returns false.
If the additional field promo_code is defined instead of pkgpart, it will be used to look up the package definition and agent restrictions will be ignored.
If the additional field refnum is defined, an FS::pkg_referral record will be created and inserted. Multiple FS::pkg_referral records can be created by setting refnum to an array reference of refnums or a hash reference with refnums as keys. If no refnum is defined, a default FS::pkg_referral record will be created corresponding to cust_main.refnum.
The following options are available:
change
If set true, supresses any referral credit to a referring customer.
options
cust_pkg_option records will be created
ticket_subject
a ticket will be added to this customer with this subject
ticket_queue
an optional queue name for ticket additions
delete
This method now works but you probably shouldn't use it.
You don't want to delete packages, because there would then be no record the customer ever purchased the package. Instead, see the cancel method and hide cancelled packages.
replace [ OLD_RECORD ] [ HASHREF | OPTION => VALUE ... ]
Replaces the OLD_RECORD with this one in the database. If there is an error, returns the error, otherwise returns false.
Currently, custnum, setup, bill, adjourn, susp, expire, and cancel may be changed.
Changing pkgpart may have disasterous effects. See the order subroutine.
setup and bill are normally updated by calling the bill method of a customer object (see FS::cust_main).
suspend is normally updated by the suspend and unsuspend methods.
cancel is normally updated by the cancel method (and also the order subroutine in some cases).
Available options are:
reason
can be set to a cancellation reason (see FS:reason), either a reasonnum of an existing reason, or passing a hashref will create a new reason. The hashref should have the following keys: typenum - Reason type (see FS::reason_type, reason - Text of the new reason.
reason_otaker
the access_user (see FS::access_user) providing the reason
options
hashref of keys and values - cust_pkg_option records will be created, updated or removed as appopriate
check
Checks all fields to make sure this is a valid billing item. If there is an error, returns the error, otherwise returns false. Called by the insert and replace methods.
check_pkgpart; cancel [ OPTION => VALUE ... ]
Cancels and removes all services (see FS::cust_svc and FS::part_svc) in this package, then cancels the package itself (sets the cancel field to now).
Available options are:
quiet - can be set true to supress email cancellation notices.
; time - can be set to cancel the package based on a specific future or historical date. Using time ensures that the remaining amount is calculated correctly. Note however that this is an immediate cancel and just changes the date. You are PROBABLY looking to expire the account instead of using this.:; reason - can be set to a cancellation reason (see FS:reason), either a reasonnum of an existing reason, or passing a hashref will create a new reason. The hashref should have the following keys: typenum - Reason type (see FS::reason_type, reason - Text of the new reason.:; date - can be set to a unix style timestamp to specify when to cancel (expire):; nobill - can be set true to skip billing if it might otherwise be done.:; unused_credit - can be set to 1 to credit the remaining time, or 0 to not credit it. This must be set (by change()) when changing the package to a different pkgpart or location, and probably shouldn't be in any other case. If it's not set, the 'unused_credit_cancel' part_pkg option will be used.
If there is an error, returns the error, otherwise returns false.
cancel_if_expired [ NOW_TIMESTAMP ]
Cancels this package if its expire date has been reached.
uncancel
"Un-cancels" this package: Orders a new package with the same custnum, pkgpart, locationnum, (other fields?). Attempts to re-provision cancelled services using history information (errors at this stage are not fatal).
cust_pkg: pass a scalar reference, will be filled in with the new cust_pkg object
svc_fatal: service provisioning errors are fatal
svc_errors: pass an array reference, will be filled in with any provisioning errors
unexpire
Cancels any pending expiration (sets the expire field to null).
If there is an error, returns the error, otherwise returns false.
suspend [ OPTION => VALUE ... ]
Suspends all services (see FS::cust_svc and FS::part_svc) in this package, then suspends the package itself (sets the susp field to now).
Available options are:
reason - can be set to a cancellation reason (see FS
reason), either a reasonnum of an existing reason, or passing a hashref will create a new reason. The hashref should have the following keys: - typenum - Reason type (see FS::reason_type - reason - Text of the new reason.:; date - can be set to a unix style timestamp to specify when to suspend (adjourn):; time - can be set to override the current time, for calculation of final invoices or unused-time credits:; resume_date - can be set to a time when the package should be unsuspended. This may be more convenient than calling unsuspend() separately.
If there is an error, returns the error, otherwise returns false.
credit_remaining MODE TIME
Generate a credit for this package for the time remaining in the current billing period. MODE is either "suspend" or "cancel" (determines the credit type). TIME is the time of suspension/cancellation. Both arguments are mandatory.
unsuspend [ OPTION => VALUE ... ]
Unsuspends all services (see FS::cust_svc and FS::part_svc) in this package, then unsuspends the package itself (clears the susp field and the adjourn field if it is in the past).
Available options are:
date
Can be set to a date to unsuspend the package in the future (the 'resume' field).
adjust_next_bill
Can be set true to adjust the next bill date forward by the amount of time the account was inactive. This was set true by default since 1.4.2 and 1.5.0pre6; however, starting with 1.7.0 this needs to be explicitly requested. Price plans for which this makes sense (anniversary-date based than prorate or subscription) could have an option to enable this behaviour?
If there is an error, returns the error, otherwise returns false.
unadjourn
Cancels any pending suspension (sets the adjourn field to null).
If there is an error, returns the error, otherwise returns false.
change HASHREF | OPTION => VALUE ...
Changes this package: cancels it and creates a new one, with a different pkgpart or locationnum or both. All services are transferred to the new package (no change will be made if this is not possible).
Options may be passed as a list of key/value pairs or as a hash reference. Options are:
locationnum
New locationnum, to change the location for this package.
cust_location
New FS::cust_location object, to create a new location and assign it to this package.
pkgpart
New pkgpart (see FS::part_pkg).
refnum
New refnum (see FS::part_referral).
keep_dates
Set to true to transfer billing dates (start_date, setup, last_bill, bill, susp, adjourn, cancel, expire, and contract_end) to the new package.
At least one of locationnum, cust_location, pkgpart, refnum must be specified (otherwise, what's the point?)
Returns either the new FS::cust_pkg object or a scalar error.
For example:

 my $err_or_new_cust_pkg = $old_cust_pkg->change

last_bill
Returns the last bill date, or if there is no last bill date, the setup date. Useful for billing metered services.
last_cust_pkg_reason ACTION
Returns the most recent ACTION FS::cust_pkg_reason associated with the package. Returns false if there is no reason or the package is not currenly ACTION'd ACTION is one of adjourn, susp, cancel, or expire.
last_reason ACTION
Returns the most recent ACTION FS::reason associated with the package. Returns false if there is no reason or the package is not currenly ACTION'd ACTION is one of adjourn, susp, cancel, or expire.
part_pkg
Returns the definition for this billing item, as an FS::part_pkg object (see FS::part_pkg).
old_cust_pkg
Returns the cancelled package this package was changed from, if any.
calc_setup
Calls the calc_setup of the FS::part_pkg object associated with this billing item.
calc_recur
Calls the calc_recur of the FS::part_pkg object associated with this billing item.
base_recur
Calls the base_recur of the FS::part_pkg object associated with this billing item.
calc_remain
Calls the calc_remain of the FS::part_pkg object associated with this billing item.
calc_cancel
Calls the calc_cancel of the FS::part_pkg object associated with this billing item.
cust_bill_pkg
Returns any invoice line items for this package (see FS::cust_bill_pkg).
cust_pkg_detail [ DETAILTYPE ]
Returns any customer package details for this package (see FS::cust_pkg_detail).
DETAILTYPE can be set to "I" for invoice details or "C" for comments.
set_cust_pkg_detail DETAILTYPE [ DETAIL, DETAIL, ... ]
Sets customer package details for this package (see FS::cust_pkg_detail).
DETAILTYPE can be set to "I" for invoice details or "C" for comments.
If there is an error, returns the error, otherwise returns false.
cust_event
Returns the new-style customer billing events (see FS::cust_event) for this invoice.
num_cust_event
Returns the number of new-style customer billing events (see FS::cust_event) for this invoice.
cust_svc [ SVCPART ] (old, deprecated usage); cust_svc [ OPTION => VALUE ... ] (current usage)
Returns the services for this package, as FS::cust_svc objects (see FS::cust_svc). Available options are svcpart and svcdb. If either is spcififed, returns only the matching services.
overlimit [ SVCPART ]
Returns the services for this package which have exceeded their usage limit as FS::cust_svc objects (see FS::cust_svc). If a svcpart is specified, return only the matching services.
h_cust_svc END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
Returns historical services for this package created before END TIMESTAMP and (optionally) not cancelled before START_TIMESTAMP, as FS::h_cust_svc objects (see FS::h_cust_svc). If MODE is 'I' (for 'invoice'), services with the pkg_svc.hidden flag will be omitted.
num_cust_svc [ SVCPART ] (old, deprecated usage); num_cust_svc [ OPTION => VALUE ... ] (current usage)
Returns the number of services for this package. Available options are svcpart and svcdb. If either is spcififed, returns only the matching services.
available_part_svc
Returns a list of FS::part_svc objects representing services included in this package but not yet provisioned. Each FS::part_svc object also has an extra field, num_avail, which specifies the number of available services.
part_svc [ OPTION => VALUE ... ]
Returns a list of FS::part_svc objects representing provisioned and available services included in this package. Each FS::part_svc object also has the following extra fields:
num_cust_svc (count)
; num_avail (quantity - count):; cust_pkg_svc (services) - array reference containing the provisioned services, as cust_svc objects
Accepts one option: summarize_size. If specified and non-zero, will omit the extra cust_pkg_svc option for objects where num_cust_svc is this size or greater.
extra_part_svc
Returns a list of FS::part_svc objects corresponding to services in this package which are still provisioned but not (any longer) available in the package definition.
status
Returns a short status string for this package, currently:
not yet billed
; one-time charge:; active:; suspended:; cancelled; ucfirst_status
Returns the status with the first character capitalized.
statuses
Class method that returns the list of possible status strings for packages (see the status method). For example:

 @statuses = FS::cust_pkg->statuses();

statuscolor
Returns a hex triplet color string for this package's status.
pkg_label
Returns a label for this package. (Currently "pkgnum: pkg - comment" or "pkg-comment" depending on user preference).
pkg_label_long
Returns a long label for this package, adding the primary service's label to pkg_label.
primary_cust_svc
Returns a primary service (as FS::cust_svc object) if one can be identified.
labels
Returns a list of lists, calling the label method for all services (see FS::cust_svc) of this billing item.
h_labels END_TIMESTAMP [ START_TIMESTAMP ] [ MODE ]
Like the labels method, but returns historical information on services that were active as of END_TIMESTAMP and (optionally) not cancelled before START_TIMESTAMP. If MODE is 'I' (for 'invoice'), services with the pkg_svc.hidden flag will be omitted.
Returns a list of lists, calling the label method for all (historical) services (see FS::h_cust_svc) of this billing item.
labels_short
Like labels, except returns a simple flat list, and shortens long (currently >5 or the cust_bill-max_same_services configuration value) lists of identical services to one line that lists the service label and the number of individual services rather than individual items.
h_labels_short END_TIMESTAMP [ START_TIMESTAMP ]
Like h_labels, except returns a simple flat list, and shortens long (currently >5 or the cust_bill-max_same_services configuration value) lists of identical services to one line that lists the service label and the number of individual services rather than individual items.
cust_main
Returns the parent customer object (see FS::cust_main).
cust_location
Returns the location object, if any (see FS::cust_location).
cust_location_or_main
If this package is associated with a location, returns the locaiton (see FS::cust_location), otherwise returns the customer (see FS::cust_main).
location_label [ OPTION => VALUE ... ]
Returns the label of the location object (see FS::cust_location).
tax_locationnum
Returns the foreign key to a FS::cust_location object for calculating tax on this package, as determined by the tax-pkg_address and tax-ship_address configuration flags.
tax_location
Returns the FS::cust_location object for tax_locationnum.
seconds_since TIMESTAMP
Returns the number of seconds all accounts (see FS::svc_acct) in this package have been online since TIMESTAMP, according to the session monitor.
TIMESTAMP is specified as a UNIX timestamp; see "time" in perlfunc. Also see Time::Local and Date::Parse for conversion functions.
seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
Returns the numbers of seconds all accounts (see FS::svc_acct) in this package have been online between TIMESTAMP_START (inclusive) and TIMESTAMP_END (exclusive).
TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see "time" in perlfunc. Also see Time::Local and Date::Parse for conversion functions.
attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
Returns the sum of the given attribute for all accounts (see FS::svc_acct) in this package for sessions ending between TIMESTAMP_START (inclusive) and TIMESTAMP_END (exclusive).
TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see "time" in perlfunc. Also see Time::Local and Date::Parse for conversion functions.
quantity; transfer DEST_PKGNUM | DEST_CUST_PKG, [ OPTION => VALUE ... ]
Transfers as many services as possible from this package to another package.
The destination package can be specified by pkgnum by passing an FS::cust_pkg object. The destination package must already exist.
Services are moved only if the destination allows services with the correct svcpart (not svcdb), unless the change_svcpart option is set true. Use this option with caution! No provision is made for export differences between the old and new service definitions. Probably only should be used when your exports for all service definitions of a given svcdb are identical. (attempt a transfer without it first, to move all possible svcpart-matching services)
Any services that can't be moved remain in the original package.
Returns an error, if there is one; otherwise, returns the number of services that couldn't be moved.
reexport
This method is deprecated. See the depend_jobnum option to the insert and order_pkgs methods in FS::cust_main for a better way to defer provisioning.
insert_reason
Associates this package with a (suspension or cancellation) reason (see FS::cust_pkg_reason, possibly inserting a new reason on the fly (see FS::reason).
Available options are:
reason
can be set to a cancellation reason (see FS:reason), either a reasonnum of an existing reason, or passing a hashref will create a new reason. The hashref should have the following keys: typenum - Reason type (see FS::reason_type, reason - Text of the new reason.
reason_otaker
the access_user (see FS::access_user) providing the reason
date
a unix timestamp
action
the action (cancel, susp, adjourn, expire) associated with the reason
If there is an error, returns the error, otherwise returns false.
insert_discount
Associates this package with a discount (see FS::cust_pkg_discount, possibly inserting a new discount on the fly (see FS::discount).
Available options are:
discountnum
If there is an error, returns the error, otherwise returns false.
set_usage USAGE_VALUE_HASHREF
USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts to which they should be set (see FS::svc_acct). Currently seconds, upbytes, downbytes, and totalbytes are appropriate keys.
All svc_accts which are part of this package have their values reset.
recharge USAGE_VALUE_HASHREF
USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts to which they should be set (see FS::svc_acct). Currently seconds, upbytes, downbytes, and totalbytes are appropriate keys.
All svc_accts which are part of this package have their values incremented.
cust_pkg_discount; cust_pkg_discount_active

CLASS METHODS

recurring_sql
Returns an SQL expression identifying recurring packages.
onetime_sql
Returns an SQL expression identifying one-time packages.
ordered_sql
Returns an SQL expression identifying ordered packages (recurring packages not yet billed).
active_sql
Returns an SQL expression identifying active packages.
not_yet_billed_sql
Returns an SQL expression identifying packages which have not yet been billed.
inactive_sql
Returns an SQL expression identifying inactive packages (one-time packages that are otherwise unsuspended/uncancelled).
susp_sql =item suspended_sql
Returns an SQL expression identifying suspended packages.
cancel_sql =item cancelled_sql
Returns an SQL exprression identifying cancelled packages.
status_sql
Returns an SQL expression to give the package status as a string.
search HASHREF
(Class method)
Returns a qsearch hash expression to search for parameters specified in HASHREF. Valid parameters are
agentnum
; magic
active, inactive, suspended, cancel (or cancelled)
status
active, inactive, suspended, one-time charge, inactive, cancel (or cancelled)
custom
boolean selects custom packages

classnum
; pkgpart
pkgpart or arrayref or hashref of pkgparts
setup
arrayref of beginning and ending epoch date
last_bill
arrayref of beginning and ending epoch date
bill
arrayref of beginning and ending epoch date
adjourn
arrayref of beginning and ending epoch date
susp
arrayref of beginning and ending epoch date
expire
arrayref of beginning and ending epoch date
cancel
arrayref of beginning and ending epoch date
query
pkgnum or APKG_pkgnum
cust_fields
a value suited to passing to FS::UI::Web::cust_header
CurrentUser
specifies the user for agent virtualization
fcc_line
boolean selects packages containing fcc form 477 telco lines

fcc_477_count
Returns a list of two package counts. The first is a count of packages based on the supplied criteria and the second is the count of residential packages with those same criteria. Criteria are specified as in the search method.
tax_locationnum_sql
Returns an SQL expression for the tax location for a package, based on the settings of 'tax-pkg_address' and 'tax-ship_address'.
location_sql
Returns a list: the first item is an SQL fragment identifying matching packages/customers via location (taking into account shipping and package address taxation, if enabled), and subsequent items are the parameters to substitute for the placeholders in that fragment.

SUBROUTINES

order CUSTNUM, PKGPARTS_ARYREF, [ REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF [ REFNUM ] ] ]
CUSTNUM is a customer (see FS::cust_main)
PKGPARTS is a list of pkgparts specifying the the billing item definitions (see FS::part_pkg) to order for this customer. Duplicates are of course permitted.
REMOVE_PKGNUMS is an optional list of pkgnums specifying the billing items to remove for this customer. The services (see FS::cust_svc) are moved to the new billing items. An error is returned if this is not possible (see FS::pkg_svc). An empty arrayref is equivalent to not specifying this parameter.
RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the newly-created cust_pkg objects.
REFNUM, if specified, will specify the FS::pkg_referral record to be created and inserted. Multiple FS::pkg_referral records can be created by setting refnum to an array reference of refnums or a hash reference with refnums as keys. If no refnum is defined, a default FS::pkg_referral record will be created corresponding to cust_main.refnum.
bulk_change PKGPARTS_ARYREF, REMOVE_PKGNUMS_ARYREF [ RETURN_CUST_PKG_ARRAYREF ]
A bulk change method to change packages for multiple customers.
PKGPARTS is a list of pkgparts specifying the the billing item definitions (see FS::part_pkg) to order for each customer. Duplicates are of course permitted.
REMOVE_PKGNUMS is an list of pkgnums specifying the billing items to replace. The services (see FS::cust_svc) are moved to the new billing items. An error is returned if this is not possible (see FS::pkg_svc).
RETURN_CUST_PKG_ARRAYREF, if specified, will be filled in with the newly-created cust_pkg objects.

BUGS

sub order is not OO. Perhaps it should be moved to FS::cust_main and made so?

In sub order, the @pkgparts array (passed by reference) is clobbered.

Also in sub order, no money is adjusted. Once FS::part_pkg defines a standard method to pass dates to the recur_prog expression, it should do so.

FS::svc_acct, FS::svc_domain, FS::svc_www, FS::svc_ip and FS::svc_forward are loaded via 'use' at compile time, rather than via 'require' in sub { setup, suspend, unsuspend, cancel } because they use %FS::UID::callback to load configuration values. Probably need a subroutine which decides what to do based on whether or not we've fetched the user yet, rather than a hash. See FS::UID and the TODO.

Now that things are transactional should the check in the insert method be moved to check ?

SEE ALSO

FS::Record, FS::cust_main, FS::part_pkg, FS::cust_svc, FS::pkg_svc, schema.html from the base documentation

POD ERRORS

Hey! The above document had some coding errors, which are explained below:

Around line 3770:
You forgot a '=back' before '=head1'