session monitor updates
[freeside.git] / FS / FS / part_pkg.pm
1 package FS::part_pkg;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearch );
6 use FS::pkg_svc;
7
8 @ISA = qw( FS::Record );
9
10 =head1 NAME
11
12 FS::part_pkg - Object methods for part_pkg objects
13
14 =head1 SYNOPSIS
15
16   use FS::part_pkg;
17
18   $record = new FS::part_pkg \%hash
19   $record = new FS::part_pkg { 'column' => 'value' };
20
21   $custom_record = $template_record->clone;
22
23   $error = $record->insert;
24
25   $error = $new_record->replace($old_record);
26
27   $error = $record->delete;
28
29   $error = $record->check;
30
31   @pkg_svc = $record->pkg_svc;
32
33   $svcnum = $record->svcpart;
34   $svcnum = $record->svcpart( 'svc_acct' );
35
36 =head1 DESCRIPTION
37
38 An FS::part_pkg object represents a billing item definition.  FS::part_pkg
39 inherits from FS::Record.  The following fields are currently supported:
40
41 =over 4
42
43 =item pkgpart - primary key (assigned automatically for new billing item definitions)
44
45 =item pkg - Text name of this billing item definition (customer-viewable)
46
47 =item comment - Text name of this billing item definition (non-customer-viewable)
48
49 =item setup - Setup fee
50
51 =item freq - Frequency of recurring fee
52
53 =item recur - Recurring fee
54
55 =back
56
57 setup and recur are evaluated as Safe perl expressions.  You can use numbers
58 just as you would normally.  More advanced semantics are not yet defined.
59
60 =head1 METHODS
61
62 =over 4 
63
64 =item new HASHREF
65
66 Creates a new billing item definition.  To add the billing item definition to
67 the database, see L<"insert">.
68
69 =cut
70
71 sub table { 'part_pkg'; }
72
73 =item clone
74
75 An alternate constructor.  Creates a new billing item definition by duplicating
76 an existing definition.  A new pkgpart is assigned and `(CUSTOM) ' is prepended
77 to the comment field.  To add the billing item definition to the database, see
78 L<"insert">.
79
80 =cut
81
82 sub clone {
83   my $self = shift;
84   my $class = ref($self);
85   my %hash = $self->hash;
86   $hash{'pkgpart'} = '';
87   $hash{'comment'} = "(CUSTOM) ". $hash{'comment'}
88     unless $hash{'comment'} =~ /^\(CUSTOM\) /;
89   #new FS::part_pkg ( \%hash ); # ?
90   new $class ( \%hash ); # ?
91 }
92
93 =item insert
94
95 Adds this billing item definition to the database.  If there is an error,
96 returns the error, otherwise returns false.
97
98 =item delete
99
100 Currently unimplemented.
101
102 =cut
103
104 sub delete {
105   return "Can't (yet?) delete package definitions.";
106 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
107 }
108
109 =item replace OLD_RECORD
110
111 Replaces OLD_RECORD with this one in the database.  If there is an error,
112 returns the error, otherwise returns false.
113
114 =item check
115
116 Checks all fields to make sure this is a valid billing item definition.  If
117 there is an error, returns the error, otherwise returns false.  Called by the
118 insert and replace methods.
119
120 =cut
121
122 sub check {
123   my $self = shift;
124
125   $self->ut_numbern('pkgpart')
126     || $self->ut_text('pkg')
127     || $self->ut_text('comment')
128     || $self->ut_anything('setup')
129     || $self->ut_number('freq')
130     || $self->ut_anything('recur')
131   ;
132 }
133
134 =item pkg_svc
135
136 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
137 definition (with non-zero quantity).
138
139 =cut
140
141 sub pkg_svc {
142   my $self = shift;
143   grep { $_->quantity } qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
144 }
145
146 =item svcpart [ SVCDB ]
147
148 Returns the svcpart of a single service definition (see L<FS::part_svc>)
149 associated with this billing item definition (see L<FS::pkg_svc>).  Returns
150 false if there not exactly one service definition with quantity 1, or if 
151 SVCDB is specified and does not match the svcdb of the service definition, 
152
153 =cut
154
155 sub svcpart {
156   my $self = shift;
157   my $svcdb = shift;
158   my @pkg_svc = $self->pkg_svc;
159   return '' if scalar(@pkg_svc) != 1
160                || $pkg_svc[0]->quantity != 1
161                || ( $svcdb && $pkg_svc[0]->part_svc->svcdb ne $svcdb );
162   $pkg_svc[0]->svcpart;
163 }
164
165 =back
166
167 =head1 VERSION
168
169 $Id: part_pkg.pm,v 1.2 1999-08-20 08:27:06 ivan Exp $
170
171 =head1 BUGS
172
173 The delete method is unimplemented.
174
175 setup and recur semantics are not yet defined (and are implemented in
176 FS::cust_bill.  hmm.).
177
178 =head1 SEE ALSO
179
180 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
181 schema.html from the base documentation.
182
183 =cut
184
185 1;
186