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