price plans web gui 1st pass, oh my
[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 expression
50
51 =item freq - Frequency of recurring fee
52
53 =item recur - Recurring fee expression
54
55 =item plan - Price plan
56
57 =item plandata - Price plan data
58
59 =back
60
61 setup and recur are evaluated as Safe perl expressions.  You can use numbers
62 just as you would normally.  More advanced semantics are not yet defined.
63
64 =head1 METHODS
65
66 =over 4 
67
68 =item new HASHREF
69
70 Creates a new billing item definition.  To add the billing item definition to
71 the database, see L<"insert">.
72
73 =cut
74
75 sub table { 'part_pkg'; }
76
77 =item clone
78
79 An alternate constructor.  Creates a new billing item definition by duplicating
80 an existing definition.  A new pkgpart is assigned and `(CUSTOM) ' is prepended
81 to the comment field.  To add the billing item definition to the database, see
82 L<"insert">.
83
84 =cut
85
86 sub clone {
87   my $self = shift;
88   my $class = ref($self);
89   my %hash = $self->hash;
90   $hash{'pkgpart'} = '';
91   $hash{'comment'} = "(CUSTOM) ". $hash{'comment'}
92     unless $hash{'comment'} =~ /^\(CUSTOM\) /;
93   #new FS::part_pkg ( \%hash ); # ?
94   new $class ( \%hash ); # ?
95 }
96
97 =item insert
98
99 Adds this billing item definition to the database.  If there is an error,
100 returns the error, otherwise returns false.
101
102 =item delete
103
104 Currently unimplemented.
105
106 =cut
107
108 sub delete {
109   return "Can't (yet?) delete package definitions.";
110 # check & make sure the pkgpart isn't in cust_pkg or type_pkgs?
111 }
112
113 =item replace OLD_RECORD
114
115 Replaces OLD_RECORD with this one in the database.  If there is an error,
116 returns the error, otherwise returns false.
117
118 =item check
119
120 Checks all fields to make sure this is a valid billing item definition.  If
121 there is an error, returns the error, otherwise returns false.  Called by the
122 insert and replace methods.
123
124 =cut
125
126 sub check {
127   my $self = shift;
128
129   $self->ut_numbern('pkgpart')
130     || $self->ut_text('pkg')
131     || $self->ut_text('comment')
132     || $self->ut_anything('setup')
133     || $self->ut_number('freq')
134     || $self->ut_anything('recur')
135     || $self->ut_alphan('plan')
136     || $self->ut_anything('plandata')
137   ;
138 }
139
140 =item pkg_svc
141
142 Returns all FS::pkg_svc objects (see L<FS::pkg_svc>) for this package
143 definition (with non-zero quantity).
144
145 =cut
146
147 sub pkg_svc {
148   my $self = shift;
149   grep { $_->quantity } qsearch( 'pkg_svc', { 'pkgpart' => $self->pkgpart } );
150 }
151
152 =item svcpart [ SVCDB ]
153
154 Returns the svcpart of a single service definition (see L<FS::part_svc>)
155 associated with this billing item definition (see L<FS::pkg_svc>).  Returns
156 false if there not exactly one service definition with quantity 1, or if 
157 SVCDB is specified and does not match the svcdb of the service definition, 
158
159 =cut
160
161 sub svcpart {
162   my $self = shift;
163   my $svcdb = shift;
164   my @pkg_svc = $self->pkg_svc;
165   return '' if scalar(@pkg_svc) != 1
166                || $pkg_svc[0]->quantity != 1
167                || ( $svcdb && $pkg_svc[0]->part_svc->svcdb ne $svcdb );
168   $pkg_svc[0]->svcpart;
169 }
170
171 =back
172
173 =head1 VERSION
174
175 $Id: part_pkg.pm,v 1.3 2001-10-15 10:42:28 ivan Exp $
176
177 =head1 BUGS
178
179 The delete method is unimplemented.
180
181 setup and recur semantics are not yet defined (and are implemented in
182 FS::cust_bill.  hmm.).
183
184 =head1 SEE ALSO
185
186 L<FS::Record>, L<FS::cust_pkg>, L<FS::type_pkgs>, L<FS::pkg_svc>, L<Safe>.
187 schema.html from the base documentation.
188
189 =cut
190
191 1;
192