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