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