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