planet telesis invoice fixups RT 8707,8406
[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     'show'   => 1,
131   },
132   'simpler' => { 
133     'label' =>  [ qw( Description Calls Amount ) ],
134     'fields' => [
135                   sub { shift->{description} },
136                   sub { shift->{calls} },
137                   sub { shift->{amount} },
138                 ],
139     'align'  => [ qw( l r r ) ],
140     'span'   => [ qw( 5 1 1 ) ],
141     'width'  => [ qw( 10.7cm 1.4cm 1.6cm ) ],   # don't like this
142     'show'   => 1,
143   },
144   'usage_simple' => { 
145     'label' => [ qw( Date Time Number Destination Duration Amount ) ],
146     'fields' => [
147                   sub { ' ' },
148                   sub { ' ' },
149                   sub { ' ' },
150                   sub { ' ' },
151                   sub { ' ' },
152                   sub { my $href = shift;  #ugh! making bunk of 'normalization'
153                         $href->{subtotal} ? $href->{subtotal} : ' '
154                       },
155                 ],
156     'align'  => [ qw( l l l l r r ) ],
157     'span'   => [ qw( 1 1 1 1 1 2 ) ],            # unitprices?
158     'width'  => [ qw( 4.3cm 1.4cm 2.5cm 2.5cm 1.4cm 1.6cm ) ],# don't like this
159     'show'   => 0,
160   },
161   'usage_6col' => { 
162     'label' => [ qw( col1 col2 col3 col4 col5 col6 ) ],
163     'fields' => [
164                   sub { ' ' },
165                   sub { ' ' },
166                   sub { ' ' },
167                   sub { ' ' },
168                   sub { ' ' },
169                   sub { my $href = shift;  #ugh! making bunk of 'normalization'
170                         $href->{subtotal} ? $href->{subtotal} : ' '
171                       },
172                 ],
173     'align'  => [ qw( l l l l r r ) ],
174     'span'   => [ qw( 1 1 1 1 1 2 ) ],            # unitprices?
175     'width'  => [ qw( 4.3cm 1.4cm 2.5cm 2.5cm 1.4cm 1.6cm ) ],# don't like this
176     'show'   => 0,
177   },
178   'usage_7col' => { 
179     'label' => [ qw( col1 col2 col3 col4 col5 col6 col7 ) ],
180     'fields' => [
181                   sub { ' ' },
182                   sub { ' ' },
183                   sub { ' ' },
184                   sub { ' ' },
185                   sub { ' ' },
186                   sub { ' ' },
187                   sub { my $href = shift;  #ugh! making bunk of 'normalization'
188                         $href->{subtotal} ? $href->{subtotal} : ' '
189                       },
190                 ],
191     'align'  => [ qw( l l l l l r r ) ],
192     'span'   => [ qw( 1 1 1 1 1 1 1 ) ],            # unitprices?
193     'width'  => [ qw( 2.9cm 1.4cm 1.4cm 2.5cm 2.5cm 1.4cm 1.6cm ) ],# don't like this
194     'show'   => 0,
195   },
196 );
197
198 sub summary_formats_labelhash {
199   map { $_ => join(',', @{$summary_formats{$_}{label}}) }
200     grep { $summary_formats{$_}{show} }
201     keys %summary_formats;
202 }
203
204 =item header_generator FORMAT
205
206 Returns a coderef used for generation of an invoice line item header for this
207 usage_class. FORMAT is either html or latex
208
209 =cut
210
211 my %html_align = (
212   'c' => 'center',
213   'l' => 'left',
214   'r' => 'right',
215 );
216
217 sub _generator_defaults {
218   my ( $self, $format, %opt ) = @_;
219   my %format = ( %{ $summary_formats{$self->format} }, %opt );
220   return ( \%format, ' ', ' ', ' ', sub { shift } );
221 }
222
223 sub header_generator {
224   my ( $self, $format, %opt ) = @_;
225
226   my ( $f, $prefix, $suffix, $separator, $column ) =
227     $self->_generator_defaults($format, %opt);
228
229   if ($format eq 'latex') {
230     $prefix = "\\hline\n\\rule{0pt}{2.5ex}\n\\makebox[1.4cm]{}&\n";
231     $suffix = "\\\\\n\\hline";
232     $separator = "&\n";
233     $column =
234       sub { my ($d,$a,$s,$w) = @_;
235             return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{\\textbf{$d}}}";
236           };
237   } elsif ( $format eq 'html' ) {
238     $prefix = '<th></th>';
239     $suffix = '';
240     $separator = '';
241     $column =
242       sub { my ($d,$a,$s,$w) = @_;
243             return qq!<th align="$html_align{$a}">$d</th>!;
244       };
245   }
246
247   sub {
248     my @args = @_;
249     my @result = ();
250
251     foreach  (my $i = 0; exists($f->{label}->[$i]); $i++) {
252       push @result,
253         &{$column}( map { $f->{$_}->[$i] } qw(label align span width) );
254     }
255
256     $prefix. join($separator, @result). $suffix;  
257   };
258
259 }
260
261 =item description_generator FORMAT
262
263 Returns a coderef used for generation of invoice line items for this
264 usage_class.  FORMAT is either html or latex
265
266 =cut
267
268 sub description_generator {
269   my ( $self, $format, %opt ) = @_;
270
271   my ( $f, $prefix, $suffix, $separator, $column ) =
272     $self->_generator_defaults($format, %opt);
273
274   if ($format eq 'latex') {
275     $prefix = "\\hline\n\\multicolumn{1}{c}{\\rule{0pt}{2.5ex}~} &\n";
276     $suffix = '\\\\';
277     $separator = " & \n";
278     $column =
279       sub { my ($d,$a,$s,$w) = @_;
280             return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{\\textbf{$d}}}";
281           };
282   }elsif ( $format eq 'html' ) {
283     $prefix = '"><td align="center"></td>';
284     $suffix = '';
285     $separator = '';
286     $column =
287       sub { my ($d,$a,$s,$w) = @_;
288             return qq!<td align="$html_align{$a}">$d</td>!;
289       };
290   }
291
292   sub {
293     my @args = @_;
294     my @result = ();
295
296     foreach  (my $i = 0; $f->{label}->[$i]; $i++) {
297       push @result, &{$column}( &{$f->{fields}->[$i]}(@args),
298                                 map { $f->{$_}->[$i] } qw(align span width)
299                               );
300     }
301
302     $prefix. join( $separator, @result ). $suffix;
303   };
304
305 }
306
307 =item total_generator FORMAT
308
309 Returns a coderef used for generation of invoice total lines for this
310 usage_class.  FORMAT is either html or latex
311
312 =cut
313
314 sub total_generator {
315   my ( $self, $format, %opt ) = @_;
316
317 #  $OUT .= '\FStotaldesc{' . $section->{'description'} . ' Total}' .
318 #          '{' . $section->{'subtotal'} . '}' . "\n";
319
320   my ( $f, $prefix, $suffix, $separator, $column ) =
321     $self->_generator_defaults($format, %opt);
322   my $style = '';
323
324   if ($format eq 'latex') {
325     $prefix = "& ";
326     $suffix = "\\\\\n";
327     $separator = " & \n";
328     $column =
329       sub { my ($d,$a,$s,$w) = @_;
330             return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{$d}}";
331           };
332   }elsif ( $format eq 'html' ) {
333     $prefix = '';
334     $suffix = '';
335     $separator = '';
336     $style = 'border-top: 3px solid #000000;border-bottom: 3px solid #000000;';
337     $column =
338       sub { my ($d,$a,$s,$w) = @_;
339             return qq!<td align="$html_align{$a}" style="$style">$d</td>!;
340       };
341   }
342   
343
344   sub {
345     my @args = @_;
346     my @result = ();
347
348     #  my $r = &{$f->{fields}->[$i]}(@args);
349     #  $r .= ' Total' unless $i;
350
351     foreach  (my $i = 0; $f->{label}->[$i]; $i++) {
352       push @result,
353         &{$column}( &{$f->{fields}->[$i]}(@args). ($i ? '' : ' Total'),
354                     map { $f->{$_}->[$i] } qw(align span width)
355                   );
356     }
357
358     $prefix. join( $separator, @result ). $suffix;
359   };
360
361 }
362
363 =item total_line_generator FORMAT
364
365 Returns a coderef used for generation of invoice total line items for this
366 usage_class.  FORMAT is either html or latex
367
368 =cut
369
370 # not used: will have issues with hash element names (description vs
371 # total_item and amount vs total_amount -- another array of functions?
372
373 sub total_line_generator {
374   my ( $self, $format, %opt ) = @_;
375
376 #     $OUT .= '\FStotaldesc{' . $line->{'total_item'} . '}' .
377 #             '{' . $line->{'total_amount'} . '}' . "\n";
378
379   my ( $f, $prefix, $suffix, $separator, $column ) =
380     $self->_generator_defaults($format, %opt);
381   my $style = '';
382
383   if ($format eq 'latex') {
384     $prefix = "& ";
385     $suffix = "\\\\\n";
386     $separator = " & \n";
387     $column =
388       sub { my ($d,$a,$s,$w) = @_;
389             return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{$d}}";
390           };
391   }elsif ( $format eq 'html' ) {
392     $prefix = '';
393     $suffix = '';
394     $separator = '';
395     $style = 'border-top: 3px solid #000000;border-bottom: 3px solid #000000;';
396     $column =
397       sub { my ($d,$a,$s,$w) = @_;
398             return qq!<td align="$html_align{$a}" style="$style">$d</td>!;
399       };
400   }
401   
402
403   sub {
404     my @args = @_;
405     my @result = ();
406
407     foreach  (my $i = 0; $f->{label}->[$i]; $i++) {
408       push @result,
409         &{$column}( &{$f->{fields}->[$i]}(@args),
410                     map { $f->{$_}->[$i] } qw(align span width)
411                   );
412     }
413
414     $prefix. join( $separator, @result ). $suffix;
415   };
416
417 }
418
419
420
421 sub _populate_initial_data {
422   my ($class, %opts) = @_;
423
424   foreach ("Intrastate", "Interstate", "International") {
425     my $object = $class->new( { 'classname' => $_ } );
426     my $error = $object->insert;
427     die "error inserting $class into database: $error\n"
428       if $error;
429   }
430
431   '';
432
433 }
434
435 sub _upgrade_data {
436   my $class = shift;
437
438   return $class->_populate_initial_data(@_)
439     unless scalar( qsearch( 'usage_class', {} ) );
440
441   '';
442
443 }
444
445 =back
446
447 =head1 BUGS
448
449 =head1 SEE ALSO
450
451 L<FS::Record>, schema.html from the base documentation.
452
453 =cut
454
455 1;
456