fix broken pagenation
[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       'format' => (ref($detail) ? $detail->[0] : '' ),
116       'detail' => (ref($detail) ? $detail->[1] : $detail ),
117     };
118     $error = $cust_bill_pkg_detail->insert;
119     if ( $error ) {
120       $dbh->rollback if $oldAutoCommit;
121       return $error;
122     }
123   }
124
125   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
126   '';
127
128 }
129
130 =item delete
131
132 Currently unimplemented.  I don't remove line items because there would then be
133 no record the items ever existed (which is bad, no?)
134
135 =cut
136
137 sub delete {
138   return "Can't delete cust_bill_pkg records!";
139 }
140
141 =item replace OLD_RECORD
142
143 Currently unimplemented.  This would be even more of an accounting nightmare
144 than deleteing the items.  Just don't do it.
145
146 =cut
147
148 sub replace {
149   return "Can't modify cust_bill_pkg records!";
150 }
151
152 =item check
153
154 Checks all fields to make sure this is a valid line item.  If there is an
155 error, returns the error, otherwise returns false.  Called by the insert
156 method.
157
158 =cut
159
160 sub check {
161   my $self = shift;
162
163   my $error =
164          $self->ut_numbern('billpkgnum')
165       || $self->ut_snumber('pkgnum')
166       || $self->ut_number('invnum')
167       || $self->ut_money('setup')
168       || $self->ut_money('recur')
169       || $self->ut_numbern('sdate')
170       || $self->ut_numbern('edate')
171       || $self->ut_textn('itemdesc')
172   ;
173   return $error if $error;
174
175   #if ( $self->pkgnum != 0 ) { #allow unchecked pkgnum 0 for tax! (add to part_pkg?)
176   if ( $self->pkgnum > 0 ) { #allow -1 for non-pkg line items and 0 for tax (add to part_pkg?)
177     return "Unknown pkgnum ". $self->pkgnum
178       unless qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
179   }
180
181   return "Unknown invnum"
182     unless qsearchs( 'cust_bill' ,{ 'invnum' => $self->invnum } );
183
184   $self->SUPER::check;
185 }
186
187 =item cust_pkg
188
189 Returns the package (see L<FS::cust_pkg>) for this invoice line item.
190
191 =cut
192
193 sub cust_pkg {
194   my $self = shift;
195   qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
196 }
197
198 =item part_pkg
199
200 Returns the package definition for this invoice line item.
201
202 =cut
203
204 sub part_pkg {
205   my $self = shift;
206   if ( $self->pkgpart_override ) {
207     qsearchs('part_pkg', { 'pkgpart' => $self->pkgpart_override } );
208   } else {
209     $self->cust_pkg->part_pkg;
210   }
211 }
212
213 =item cust_bill
214
215 Returns the invoice (see L<FS::cust_bill>) for this invoice line item.
216
217 =cut
218
219 sub cust_bill {
220   my $self = shift;
221   qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
222 }
223
224 =item details [ OPTION => VALUE ... ]
225
226 Returns an array of detail information for the invoice line item.
227
228 Currently available options are: I<format> I<escape_function>
229
230 If I<format> is set to html or latex then the array members are improved
231 for tabular appearance in those environments if possible.
232
233 If I<escape_function> is set then the array members are processed by this
234 function before being returned.
235
236 =cut
237
238 sub details {
239   my ( $self, %opt ) = @_;
240   my $format = $opt{format} || '';
241   my $escape_function = $opt{escape_function} || sub { shift };
242   return () unless defined dbdef->table('cust_bill_pkg_detail');
243
244   eval "use Text::CSV_XS;";
245   die $@ if $@;
246   my $csv = new Text::CSV_XS;
247
248   my $format_sub = sub { my $detail = shift;
249                          $csv->parse($detail) or return "can't parse $detail";
250                          join(' - ', map { &$escape_function($_) }
251                                      $csv->fields
252                              );
253                        };
254
255   $format_sub = sub { my $detail = shift;
256                       $csv->parse($detail) or return "can't parse $detail";
257                       join('</TD><TD>', map { &$escape_function($_) }
258                                         $csv->fields
259                           );
260                     }
261     if $format eq 'html';
262
263   $format_sub = sub { my $detail = shift;
264                       $csv->parse($detail) or return "can't parse $detail";
265                       join(' & ', map { '\small{'. &$escape_function($_). '}' }
266                                   $csv->fields );
267                     }
268     if $format eq 'latex';
269
270   map { ( $_->format eq 'C'
271           ? &{$format_sub}( $_->detail )
272           : &{$escape_function}( $_->detail )
273         )
274       }
275     qsearch ({ 'table'    => 'cust_bill_pkg_detail',
276                'hashref'  => { 'pkgnum' => $self->pkgnum,
277                                'invnum' => $self->invnum,
278                              },
279                'order_by' => 'ORDER BY detailnum',
280             });
281     #qsearch ( 'cust_bill_pkg_detail', { 'lineitemnum' => $self->lineitemnum });
282 }
283
284 =item desc
285
286 Returns a description for this line item.  For typical line items, this is the
287 I<pkg> field of the corresponding B<FS::part_pkg> object (see L<FS::part_pkg>).
288 For one-shot line items and named taxes, it is the I<itemdesc> field of this
289 line item, and for generic taxes, simply returns "Tax".
290
291 =cut
292
293 sub desc {
294   my $self = shift;
295
296   if ( $self->pkgnum > 0 ) {
297     $self->part_pkg->pkg;
298   } else {
299     $self->itemdesc || 'Tax';
300   }
301 }
302
303 =item owed_setup
304
305 Returns the amount owed (still outstanding) on this line item's setup fee,
306 which is the amount of the line item minus all payment applications (see
307 L<FS::cust_bill_pay_pkg> and credit applications (see
308 L<FS::cust_credit_bill_pkg>).
309
310 =cut
311
312 sub owed_setup {
313   my $self = shift;
314   $self->owed('setup', @_);
315 }
316
317 =item owed_recur
318
319 Returns the amount owed (still outstanding) on this line item's recurring fee,
320 which is the amount of the line item minus all payment applications (see
321 L<FS::cust_bill_pay_pkg> and credit applications (see
322 L<FS::cust_credit_bill_pkg>).
323
324 =cut
325
326 sub owed_recur {
327   my $self = shift;
328   $self->owed('recur', @_);
329 }
330
331 # modeled after cust_bill::owed...
332 sub owed {
333   my( $self, $field ) = @_;
334   my $balance = $self->$field();
335   $balance -= $_->amount foreach ( $self->cust_bill_pay_pkg($field) );
336   $balance -= $_->amount foreach ( $self->cust_credit_bill_pkg($field) );
337   $balance = sprintf( '%.2f', $balance );
338   $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
339   $balance;
340 }
341
342 sub cust_bill_pay_pkg {
343   my( $self, $field ) = @_;
344   qsearch( 'cust_bill_pay_pkg', { 'billpkgnum' => $self->billpkgnum,
345                                   'setuprecur' => $field,
346                                 }
347          );
348 }
349
350 sub cust_credit_bill_pkg {
351   my( $self, $field ) = @_;
352   qsearch( 'cust_credit_bill_pkg', { 'billpkgnum' => $self->billpkgnum,
353                                      'setuprecur' => $field,
354                                    }
355          );
356 }
357
358 =item units
359
360 Returns the number of billing units (for tax purposes) represented by this,
361 line item.
362
363 =cut
364
365 sub units {
366   my $self = shift;
367   $self->part_pkg->calc_units($self->cust_pkg);
368 }
369
370 =back
371
372 =head1 BUGS
373
374 setup and recur shouldn't be separate fields.  There should be one "amount"
375 field and a flag to tell you if it is a setup/one-time fee or a recurring fee.
376
377 A line item with both should really be two separate records (preserving
378 sdate and edate for setup fees for recurring packages - that information may
379 be valuable later).  Invoice generation (cust_main::bill), invoice printing
380 (cust_bill), tax reports (report_tax.cgi) and line item reports 
381 (cust_bill_pkg.cgi) would need to be updated.
382
383 owed_setup and owed_recur could then be repaced by just owed, and
384 cust_bill::open_cust_bill_pkg and
385 cust_bill_ApplicationCommon::apply_to_lineitems could be simplified.
386
387 =head1 SEE ALSO
388
389 L<FS::Record>, L<FS::cust_bill>, L<FS::cust_pkg>, L<FS::cust_main>, schema.html
390 from the base documentation.
391
392 =cut
393
394 1;
395