implement line item bundling
[freeside.git] / FS / FS / cust_bill_pkg.pm
1 package FS::cust_bill_pkg;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearch qsearchs dbdef dbh );
6 use FS::cust_main_Mixin;
7 use FS::cust_pkg;
8 use FS::part_pkg;
9 use FS::cust_bill;
10 use FS::cust_bill_pkg_detail;
11 use FS::cust_bill_pay_pkg;
12 use FS::cust_credit_bill_pkg;
13
14 @ISA = qw( FS::cust_main_Mixin FS::Record );
15
16 =head1 NAME
17
18 FS::cust_bill_pkg - Object methods for cust_bill_pkg records
19
20 =head1 SYNOPSIS
21
22   use FS::cust_bill_pkg;
23
24   $record = new FS::cust_bill_pkg \%hash;
25   $record = new FS::cust_bill_pkg { 'column' => 'value' };
26
27   $error = $record->insert;
28
29   $error = $new_record->replace($old_record);
30
31   $error = $record->delete;
32
33   $error = $record->check;
34
35 =head1 DESCRIPTION
36
37 An FS::cust_bill_pkg object represents an invoice line item.
38 FS::cust_bill_pkg inherits from FS::Record.  The following fields are currently
39 supported:
40
41 =over 4
42
43 =item billpkgnum - primary key
44
45 =item invnum - invoice (see L<FS::cust_bill>)
46
47 =item pkgnum - package (see L<FS::cust_pkg>) or 0 for the special virtual sales tax package, or -1 for the virtual line item (itemdesc is used for the line)
48
49 =item pkgpart_override - optional package definition (see L<FS::part_pkg>) override
50 =item setup - setup fee
51
52 =item recur - recurring fee
53
54 =item sdate - starting date of recurring fee
55
56 =item edate - ending date of recurring fee
57
58 =item itemdesc - Line item description (currentlty used only when pkgnum is 0 or -1)
59
60 =back
61
62 sdate and edate are specified as UNIX timestamps; see L<perlfunc/"time">.  Also
63 see L<Time::Local> and L<Date::Parse> for conversion functions.
64
65 =head1 METHODS
66
67 =over 4
68
69 =item new HASHREF
70
71 Creates a new line item.  To add the line item to the database, see
72 L<"insert">.  Line items are normally created by calling the bill method of a
73 customer object (see L<FS::cust_main>).
74
75 =cut
76
77 sub table { 'cust_bill_pkg'; }
78
79 =item insert
80
81 Adds this line item to the database.  If there is an error, returns the error,
82 otherwise returns false.
83
84 =cut
85
86 sub insert {
87   my $self = shift;
88
89   local $SIG{HUP} = 'IGNORE';
90   local $SIG{INT} = 'IGNORE';
91   local $SIG{QUIT} = 'IGNORE';
92   local $SIG{TERM} = 'IGNORE';
93   local $SIG{TSTP} = 'IGNORE';
94   local $SIG{PIPE} = 'IGNORE';
95
96   my $oldAutoCommit = $FS::UID::AutoCommit;
97   local $FS::UID::AutoCommit = 0;
98   my $dbh = dbh;
99
100   my $error = $self->SUPER::insert;
101   if ( $error ) {
102     $dbh->rollback if $oldAutoCommit;
103     return $error;
104   }
105
106   unless ( defined dbdef->table('cust_bill_pkg_detail') && $self->get('details') ) {
107     $dbh->commit or die $dbh->errstr if $oldAutoCommit;
108     return '';
109   }
110
111   foreach my $detail ( @{$self->get('details')} ) {
112     my $cust_bill_pkg_detail = new FS::cust_bill_pkg_detail {
113       'pkgnum' => $self->pkgnum,
114       'invnum' => $self->invnum,
115       'detail' => $detail,
116     };
117     $error = $cust_bill_pkg_detail->insert;
118     if ( $error ) {
119       $dbh->rollback if $oldAutoCommit;
120       return $error;
121     }
122   }
123
124   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
125   '';
126
127 }
128
129 =item delete
130
131 Currently unimplemented.  I don't remove line items because there would then be
132 no record the items ever existed (which is bad, no?)
133
134 =cut
135
136 sub delete {
137   return "Can't delete cust_bill_pkg records!";
138 }
139
140 =item replace OLD_RECORD
141
142 Currently unimplemented.  This would be even more of an accounting nightmare
143 than deleteing the items.  Just don't do it.
144
145 =cut
146
147 sub replace {
148   return "Can't modify cust_bill_pkg records!";
149 }
150
151 =item check
152
153 Checks all fields to make sure this is a valid line item.  If there is an
154 error, returns the error, otherwise returns false.  Called by the insert
155 method.
156
157 =cut
158
159 sub check {
160   my $self = shift;
161
162   my $error =
163          $self->ut_numbern('billpkgnum')
164       || $self->ut_snumber('pkgnum')
165       || $self->ut_number('invnum')
166       || $self->ut_money('setup')
167       || $self->ut_money('recur')
168       || $self->ut_numbern('sdate')
169       || $self->ut_numbern('edate')
170       || $self->ut_textn('itemdesc')
171   ;
172   return $error if $error;
173
174   #if ( $self->pkgnum != 0 ) { #allow unchecked pkgnum 0 for tax! (add to part_pkg?)
175   if ( $self->pkgnum > 0 ) { #allow -1 for non-pkg line items and 0 for tax (add to part_pkg?)
176     return "Unknown pkgnum ". $self->pkgnum
177       unless qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
178   }
179
180   return "Unknown invnum"
181     unless qsearchs( 'cust_bill' ,{ 'invnum' => $self->invnum } );
182
183   $self->SUPER::check;
184 }
185
186 =item cust_pkg
187
188 Returns the package (see L<FS::cust_pkg>) for this invoice line item.
189
190 =cut
191
192 sub cust_pkg {
193   my $self = shift;
194   qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
195 }
196
197 =item part_pkg
198
199 Returns the package definition for this invoice line item.
200
201 =cut
202
203 sub part_pkg {
204   my $self = shift;
205   if ( $self->pkgpart_override ) {
206     qsearchs('part_pkg', { 'pkgpart' => $self->pkgpart_override } );
207   } else {
208     $self->cust_pkg->part_pkg;
209   }
210 }
211
212 =item cust_bill
213
214 Returns the invoice (see L<FS::cust_bill>) for this invoice line item.
215
216 =cut
217
218 sub cust_bill {
219   my $self = shift;
220   qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
221 }
222
223 =item details
224
225 Returns an array of detail information for the invoice line item.
226
227 =cut
228
229 sub details {
230   my $self = shift;
231   return () unless defined dbdef->table('cust_bill_pkg_detail');
232   map { $_->detail }
233     qsearch ( 'cust_bill_pkg_detail', { 'pkgnum' => $self->pkgnum,
234                                         'invnum' => $self->invnum, } );
235     #qsearch ( 'cust_bill_pkg_detail', { 'lineitemnum' => $self->lineitemnum });
236 }
237
238 =item desc
239
240 Returns a description for this line item.  For typical line items, this is the
241 I<pkg> field of the corresponding B<FS::part_pkg> object (see L<FS::part_pkg>).
242 For one-shot line items and named taxes, it is the I<itemdesc> field of this
243 line item, and for generic taxes, simply returns "Tax".
244
245 =cut
246
247 sub desc {
248   my $self = shift;
249
250   if ( $self->pkgnum > 0 ) {
251     $self->part_pkg->pkg;
252   } else {
253     $self->itemdesc || 'Tax';
254   }
255 }
256
257 =item owed_setup
258
259 Returns the amount owed (still outstanding) on this line item's setup fee,
260 which is the amount of the line item minus all payment applications (see
261 L<FS::cust_bill_pay_pkg> and credit applications (see
262 L<FS::cust_credit_bill_pkg>).
263
264 =cut
265
266 sub owed_setup {
267   my $self = shift;
268   $self->owed('setup', @_);
269 }
270
271 =item owed_recur
272
273 Returns the amount owed (still outstanding) on this line item's recurring fee,
274 which is the amount of the line item minus all payment applications (see
275 L<FS::cust_bill_pay_pkg> and credit applications (see
276 L<FS::cust_credit_bill_pkg>).
277
278 =cut
279
280 sub owed_recur {
281   my $self = shift;
282   $self->owed('recur', @_);
283 }
284
285 # modeled after cust_bill::owed...
286 sub owed {
287   my( $self, $field ) = @_;
288   my $balance = $self->$field();
289   $balance -= $_->amount foreach ( $self->cust_bill_pay_pkg($field) );
290   $balance -= $_->amount foreach ( $self->cust_credit_bill_pkg($field) );
291   $balance = sprintf( '%.2f', $balance );
292   $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
293   $balance;
294 }
295
296 sub cust_bill_pay_pkg {
297   my( $self, $field ) = @_;
298   qsearch( 'cust_bill_pay_pkg', { 'billpkgnum' => $self->billpkgnum,
299                                   'setuprecur' => $field,
300                                 }
301          );
302 }
303
304 sub cust_credit_bill_pkg {
305   my( $self, $field ) = @_;
306   qsearch( 'cust_credit_bill_pkg', { 'billpkgnum' => $self->billpkgnum,
307                                      'setuprecur' => $field,
308                                    }
309          );
310 }
311
312 =back
313
314 =head1 BUGS
315
316 setup and recur shouldn't be separate fields.  There should be one "amount"
317 field and a flag to tell you if it is a setup/one-time fee or a recurring fee.
318
319 A line item with both should really be two separate records (preserving
320 sdate and edate for setup fees for recurring packages - that information may
321 be valuable later).  Invoice generation (cust_main::bill), invoice printing
322 (cust_bill), tax reports (report_tax.cgi) and line item reports 
323 (cust_bill_pkg.cgi) would need to be updated.
324
325 owed_setup and owed_recur could then be repaced by just owed, and
326 cust_bill::open_cust_bill_pkg and
327 cust_bill_ApplicationCommon::apply_to_lineitems could be simplified.
328
329 =head1 SEE ALSO
330
331 L<FS::Record>, L<FS::cust_bill>, L<FS::cust_pkg>, L<FS::cust_main>, schema.html
332 from the base documentation.
333
334 =cut
335
336 1;
337