invoice formatting: add sections for usage, add sections per svc_phone, add folding...
[freeside.git] / FS / FS / usage_class.pm
1 package FS::usage_class;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearch qsearchs );
6
7 @ISA = qw(FS::Record);
8
9 =head1 NAME
10
11 FS::usage_class - Object methods for usage_class records
12
13 =head1 SYNOPSIS
14
15   use FS::usage_class;
16
17   $record = new FS::usage_class \%hash;
18   $record = new FS::usage_class { 'column' => 'value' };
19
20   $error = $record->insert;
21
22   $error = $new_record->replace($old_record);
23
24   $error = $record->delete;
25
26   $error = $record->check;
27
28 =head1 DESCRIPTION
29
30 An FS::usage_class object represents a usage class.  Every rate detail
31 (see L<FS::rate_detail>) has, optionally, a usage class.  FS::usage_class
32 inherits from FS::Record.  The following fields are currently supported:
33
34 =over 4
35
36 =item classnum
37
38 Primary key (assigned automatically for new usage classes)
39
40 =item classname
41
42 Text name of this usage class
43
44 =item disabled
45
46 Disabled flag, empty or 'Y'
47
48
49 =back
50
51 =head1 METHODS
52
53 =over 4
54
55 =item new HASHREF
56
57 Creates a new usage class.  To add the usage class to the database,
58 see L<"insert">.
59
60 Note that this stores the hash reference, not a distinct copy of the hash it
61 points to.  You can ask the object for a copy with the I<hash> method.
62
63 =cut
64
65 sub table { 'usage_class'; }
66
67 =item insert
68
69 Adds this record to the database.  If there is an error, returns the error,
70 otherwise returns false.
71
72 =cut
73
74 =item delete
75
76 Delete this record from the database.
77
78 =cut
79
80 =item replace OLD_RECORD
81
82 Replaces the OLD_RECORD with this one in the database.  If there is an error,
83 returns the error, otherwise returns false.
84
85 =cut
86
87 =item check
88
89 Checks all fields to make sure this is a valid usage class.  If there is
90 an error, returns the error, otherwise returns false.  Called by the insert
91 and replace methods.
92
93 =cut
94
95 sub check {
96   my $self = shift;
97
98   my $error = 
99     $self->ut_numbern('classnum')
100     || $self->ut_numbern('weight')
101     || $self->ut_text('classname')
102     || $self->ut_textn('format')
103     || $self->ut_enum('disabled', [ '', 'Y' ])
104   ;
105   return $error if $error;
106
107   $self->SUPER::check;
108 }
109
110 =item summary_formats_labelhash
111
112 Returns a list of line item format descriptions suitable for assigning to
113 a hash. 
114
115 =cut
116
117 # transform hashes of arrays to arrays of hashes for false laziness removal?
118 my %summary_formats = (
119   'simple' => { 
120     'label' => [ qw( Description Calls Minutes Amount ) ],
121     'fields' => [
122                   sub { shift->{description} },
123                   sub { shift->{calls} },
124                   sub { sprintf( '%.1f', shift->{duration}/60 ) },
125                   sub { shift->{amount} },
126                 ],
127     'align'  => [ qw( l r r r ) ],
128     'span'   => [ qw( 4 1 1 1 ) ],            # unitprices?
129     'width'  => [ qw( 8.2cm 2.5cm 1.4cm 1.6cm ) ],   # don't like this
130   },
131   'simpler' => { 
132     'label' =>  [ qw( Description Calls Amount ) ],
133     'fields' => [
134                   sub { shift->{description} },
135                   sub { shift->{calls} },
136                   sub { shift->{amount} },
137                 ],
138     'align'  => [ qw( l r r ) ],
139     'span'   => [ qw( 5 1 1 ) ],
140     'width'  => [ qw( 10.7cm 1.4cm 1.6cm ) ],   # don't like this
141   },
142   'minimal' => { 
143     'label' => [ qw( Amount ) ],
144     'fields' => [
145                   sub { '' },
146                 ],
147     'align'  => [ qw( r ) ],
148     'span'   => [ qw( 7 ) ],            # unitprices?
149     'width'  => [ qw( 13.8cm ) ],   # don't like this
150   },
151 );
152
153 sub summary_formats_labelhash {
154   map { $_ => join(',', @{$summary_formats{$_}{label}}) } keys %summary_formats;
155 }
156
157 =item header_generator FORMAT
158
159 Returns a coderef used for generation of an invoice line item header for this
160 usage_class. FORMAT is either html or latex
161
162 =cut
163
164 my %html_align = (
165   'c' => 'center',
166   'l' => 'left',
167   'r' => 'right',
168 );
169
170 sub _generator_defaults {
171   my ( $self, $format ) = ( shift, shift );
172   return ( $summary_formats{$self->format}, ' ', ' ', ' ', sub { shift } );
173 }
174
175 sub header_generator {
176   my ( $self, $format ) = ( shift, shift );
177
178   my ( $f, $prefix, $suffix, $separator, $column ) =
179     $self->_generator_defaults($format);
180
181   if ($format eq 'latex') {
182     $prefix = "\\hline\n\\rule{0pt}{2.5ex}\n\\makebox[1.4cm]{}&\n";
183     $suffix = "\\\\\n\\hline";
184     $separator = "&\n";
185     $column =
186       sub { my ($d,$a,$s,$w) = @_;
187             return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{\\textbf{$d}}}";
188           };
189   } elsif ( $format eq 'html' ) {
190     $prefix = '<th></th>';
191     $suffix = '';
192     $separator = '';
193     $column =
194       sub { my ($d,$a,$s,$w) = @_;
195             return qq!<th align="$html_align{$a}">$d</th>!;
196       };
197   }
198
199   sub {
200     my @args = @_;
201     my @result = ();
202
203     foreach  (my $i = 0; $f->{label}->[$i]; $i++) {
204       push @result,
205         &{$column}( map { $f->{$_}->[$i] } qw(label align span width) );
206     }
207
208     $prefix. join($separator, @result). $suffix;  
209   };
210
211 }
212
213 =item description_generator FORMAT
214
215 Returns a coderef used for generation of invoice line items for this
216 usage_class.  FORMAT is either html or latex
217
218 =cut
219
220 sub description_generator {
221   my ( $self, $format ) = ( shift, shift );
222
223   my ( $f, $prefix, $suffix, $separator, $column ) =
224     $self->_generator_defaults($format);
225
226   if ($format eq 'latex') {
227     $prefix = "\\hline\n\\multicolumn{1}{c}{\\rule{0pt}{2.5ex}~} &\n";
228     $suffix = '\\\\';
229     $separator = " & \n";
230     $column =
231       sub { my ($d,$a,$s,$w) = @_;
232             return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{\\textbf{$d}}}";
233           };
234   }elsif ( $format eq 'html' ) {
235     $prefix = '"><td align="center"></td>';
236     $suffix = '';
237     $separator = '';
238     $column =
239       sub { my ($d,$a,$s,$w) = @_;
240             return qq!<td align="$html_align{$a}">$d</td>!;
241       };
242   }
243
244   sub {
245     my @args = @_;
246     my @result = ();
247
248     foreach  (my $i = 0; $f->{label}->[$i]; $i++) {
249       push @result, &{$column}( &{$f->{fields}->[$i]}(@args),
250                                 map { $f->{$_}->[$i] } qw(align span width)
251                               );
252     }
253
254     $prefix. join( $separator, @result ). $suffix;
255   };
256
257 }
258
259 =item total_generator FORMAT
260
261 Returns a coderef used for generation of invoice total lines for this
262 usage_class.  FORMAT is either html or latex
263
264 =cut
265
266 sub total_generator {
267   my ( $self, $format ) = ( shift, shift );
268
269 #  $OUT .= '\FStotaldesc{' . $section->{'description'} . ' Total}' .
270 #          '{' . $section->{'subtotal'} . '}' . "\n";
271
272   my ( $f, $prefix, $suffix, $separator, $column ) =
273     $self->_generator_defaults($format);
274   my $style = '';
275
276   if ($format eq 'latex') {
277     $prefix = "& ";
278     $suffix = "\\\\\n";
279     $separator = " & \n";
280     $column =
281       sub { my ($d,$a,$s,$w) = @_;
282             return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{$d}}";
283           };
284   }elsif ( $format eq 'html' ) {
285     $prefix = '';
286     $suffix = '';
287     $separator = '';
288     $style = 'border-top: 3px solid #000000;border-bottom: 3px solid #000000;';
289     $column =
290       sub { my ($d,$a,$s,$w) = @_;
291             return qq!<td align="$html_align{$a}" style="$style">$d</td>!;
292       };
293   }
294   
295
296   sub {
297     my @args = @_;
298     my @result = ();
299
300     #  my $r = &{$f->{fields}->[$i]}(@args);
301     #  $r .= ' Total' unless $i;
302
303     foreach  (my $i = 0; $f->{label}->[$i]; $i++) {
304       push @result,
305         &{$column}( &{$f->{fields}->[$i]}(@args). ($i ? '' : ' Total'),
306                     map { $f->{$_}->[$i] } qw(align span width)
307                   );
308     }
309
310     $prefix. join( $separator, @result ). $suffix;
311   };
312
313 }
314
315 =item total_line_generator FORMAT
316
317 Returns a coderef used for generation of invoice total line items for this
318 usage_class.  FORMAT is either html or latex
319
320 =cut
321
322 # not used: will have issues with hash element names (description vs
323 # total_item and amount vs total_amount -- another array of functions?
324
325 sub total_line_generator {
326   my ( $self, $format ) = ( shift, shift );
327
328 #     $OUT .= '\FStotaldesc{' . $line->{'total_item'} . '}' .
329 #             '{' . $line->{'total_amount'} . '}' . "\n";
330
331   my ( $f, $prefix, $suffix, $separator, $column ) =
332     $self->_generator_defaults($format);
333   my $style = '';
334
335   if ($format eq 'latex') {
336     $prefix = "& ";
337     $suffix = "\\\\\n";
338     $separator = " & \n";
339     $column =
340       sub { my ($d,$a,$s,$w) = @_;
341             return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{$d}}";
342           };
343   }elsif ( $format eq 'html' ) {
344     $prefix = '';
345     $suffix = '';
346     $separator = '';
347     $style = 'border-top: 3px solid #000000;border-bottom: 3px solid #000000;';
348     $column =
349       sub { my ($d,$a,$s,$w) = @_;
350             return qq!<td align="$html_align{$a}" style="$style">$d</td>!;
351       };
352   }
353   
354
355   sub {
356     my @args = @_;
357     my @result = ();
358
359     foreach  (my $i = 0; $f->{label}->[$i]; $i++) {
360       push @result,
361         &{$column}( &{$f->{fields}->[$i]}(@args),
362                     map { $f->{$_}->[$i] } qw(align span width)
363                   );
364     }
365
366     $prefix. join( $separator, @result ). $suffix;
367   };
368
369 }
370
371
372
373 sub _populate_initial_data {
374   my ($class, %opts) = @_;
375
376   foreach ("Intrastate", "Interstate", "International") {
377     my $object = $class->new( { 'classname' => $_ } );
378     my $error = $object->insert;
379     die "error inserting $class into database: $error\n"
380       if $error;
381   }
382
383   '';
384
385 }
386
387 sub _upgrade_data {
388   my $class = shift;
389
390   return $class->_populate_initial_data(@_)
391     unless scalar( qsearch( 'usage_class', {} ) );
392
393   '';
394
395 }
396
397 =back
398
399 =head1 BUGS
400
401 =head1 SEE ALSO
402
403 L<FS::Record>, schema.html from the base documentation.
404
405 =cut
406
407 1;
408