1 package FS::usage_class;
5 use FS::Record qw( qsearch qsearchs );
11 FS::usage_class - Object methods for usage_class records
17 $record = new FS::usage_class \%hash;
18 $record = new FS::usage_class { 'column' => 'value' };
20 $error = $record->insert;
22 $error = $new_record->replace($old_record);
24 $error = $record->delete;
26 $error = $record->check;
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:
38 Primary key (assigned automatically for new usage classes)
42 Text name of this usage class
46 Disabled flag, empty or 'Y'
57 Creates a new usage class. To add the usage class to the database,
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.
65 sub table { 'usage_class'; }
69 Adds this record to the database. If there is an error, returns the error,
70 otherwise returns false.
76 Delete this record from the database.
80 =item replace OLD_RECORD
82 Replaces the OLD_RECORD with this one in the database. If there is an error,
83 returns the error, otherwise returns false.
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
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' ])
105 return $error if $error;
110 =item summary_formats_labelhash
112 Returns a list of line item format descriptions suitable for assigning to
117 # transform hashes of arrays to arrays of hashes for false laziness removal?
118 my %summary_formats = (
120 'label' => [ qw( Description Calls Minutes Amount ) ],
122 sub { shift->{description} },
123 sub { shift->{calls} },
124 sub { sprintf( '%.1f', shift->{duration}/60 ) },
125 sub { shift->{amount} },
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
133 'label' => [ qw( Description Calls Amount ) ],
135 sub { shift->{description} },
136 sub { shift->{calls} },
137 sub { shift->{amount} },
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
145 'label' => [ qw( Date Time Number Destination Duration Amount ) ],
152 sub { my $href = shift; #ugh! making bunk of 'normalization'
153 $href->{subtotal} ? $href->{subtotal} : ' '
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
162 'label' => [ qw( col1 col2 col3 col4 col5 col6 ) ],
169 sub { my $href = shift; #ugh! making bunk of 'normalization'
170 $href->{subtotal} ? $href->{subtotal} : ' '
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
179 'label' => [ qw( col1 col2 col3 col4 col5 col6 col7 ) ],
187 sub { my $href = shift; #ugh! making bunk of 'normalization'
188 $href->{subtotal} ? $href->{subtotal} : ' '
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
198 sub summary_formats_labelhash {
199 map { $_ => join(',', @{$summary_formats{$_}{label}}) }
200 grep { $summary_formats{$_}{show} }
201 keys %summary_formats;
204 =item header_generator FORMAT
206 Returns a coderef used for generation of an invoice line item header for this
207 usage_class. FORMAT is either html or latex
217 sub _generator_defaults {
218 my ( $self, $format, %opt ) = @_;
219 my %format = ( %{ $summary_formats{$self->format} }, %opt );
220 return ( \%format, ' ', ' ', ' ', sub { shift } );
223 sub header_generator {
224 my ( $self, $format, %opt ) = @_;
226 my ( $f, $prefix, $suffix, $separator, $column ) =
227 $self->_generator_defaults($format, %opt);
229 if ($format eq 'latex') {
230 $prefix = "\\hline\n\\rule{0pt}{2.5ex}\n\\makebox[1.4cm]{}&\n";
231 $suffix = "\\\\\n\\hline";
234 sub { my ($d,$a,$s,$w) = @_;
235 return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{\\textbf{$d}}}";
237 } elsif ( $format eq 'html' ) {
238 $prefix = '<th></th>';
242 sub { my ($d,$a,$s,$w) = @_;
243 return qq!<th align="$html_align{$a}">$d</th>!;
251 foreach (my $i = 0; exists($f->{label}->[$i]); $i++) {
253 &{$column}( map { $f->{$_}->[$i] } qw(label align span width) );
256 $prefix. join($separator, @result). $suffix;
261 =item description_generator FORMAT
263 Returns a coderef used for generation of invoice line items for this
264 usage_class. FORMAT is either html or latex
268 sub description_generator {
269 my ( $self, $format, %opt ) = @_;
271 my ( $f, $prefix, $suffix, $separator, $column ) =
272 $self->_generator_defaults($format, %opt);
274 if ($format eq 'latex') {
275 $prefix = "\\hline\n\\multicolumn{1}{c}{\\rule{0pt}{2.5ex}~} &\n";
277 $separator = " & \n";
279 sub { my ($d,$a,$s,$w) = @_;
280 return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{\\textbf{$d}}}";
282 }elsif ( $format eq 'html' ) {
283 $prefix = '"><td align="center"></td>';
287 sub { my ($d,$a,$s,$w) = @_;
288 return qq!<td align="$html_align{$a}">$d</td>!;
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)
302 $prefix. join( $separator, @result ). $suffix;
307 =item total_generator FORMAT
309 Returns a coderef used for generation of invoice total lines for this
310 usage_class. FORMAT is either html or latex
314 sub total_generator {
315 my ( $self, $format, %opt ) = @_;
317 # $OUT .= '\FStotaldesc{' . $section->{'description'} . ' Total}' .
318 # '{' . $section->{'subtotal'} . '}' . "\n";
320 my ( $f, $prefix, $suffix, $separator, $column ) =
321 $self->_generator_defaults($format, %opt);
324 if ($format eq 'latex') {
327 $separator = " & \n";
329 sub { my ($d,$a,$s,$w) = @_;
330 return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{$d}}";
332 }elsif ( $format eq 'html' ) {
336 $style = 'border-top: 3px solid #000000;border-bottom: 3px solid #000000;';
338 sub { my ($d,$a,$s,$w) = @_;
339 return qq!<td align="$html_align{$a}" style="$style">$d</td>!;
348 # my $r = &{$f->{fields}->[$i]}(@args);
349 # $r .= ' Total' unless $i;
351 foreach (my $i = 0; $f->{label}->[$i]; $i++) {
353 &{$column}( &{$f->{fields}->[$i]}(@args). ($i ? '' : ' Total'),
354 map { $f->{$_}->[$i] } qw(align span width)
358 $prefix. join( $separator, @result ). $suffix;
363 =item total_line_generator FORMAT
365 Returns a coderef used for generation of invoice total line items for this
366 usage_class. FORMAT is either html or latex
370 # not used: will have issues with hash element names (description vs
371 # total_item and amount vs total_amount -- another array of functions?
373 sub total_line_generator {
374 my ( $self, $format, %opt ) = @_;
376 # $OUT .= '\FStotaldesc{' . $line->{'total_item'} . '}' .
377 # '{' . $line->{'total_amount'} . '}' . "\n";
379 my ( $f, $prefix, $suffix, $separator, $column ) =
380 $self->_generator_defaults($format, %opt);
383 if ($format eq 'latex') {
386 $separator = " & \n";
388 sub { my ($d,$a,$s,$w) = @_;
389 return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{$d}}";
391 }elsif ( $format eq 'html' ) {
395 $style = 'border-top: 3px solid #000000;border-bottom: 3px solid #000000;';
397 sub { my ($d,$a,$s,$w) = @_;
398 return qq!<td align="$html_align{$a}" style="$style">$d</td>!;
407 foreach (my $i = 0; $f->{label}->[$i]; $i++) {
409 &{$column}( &{$f->{fields}->[$i]}(@args),
410 map { $f->{$_}->[$i] } qw(align span width)
414 $prefix. join( $separator, @result ). $suffix;
421 sub _populate_initial_data {
422 my ($class, %opts) = @_;
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"
438 return $class->_populate_initial_data(@_)
439 unless scalar( qsearch( 'usage_class', {} ) );
451 L<FS::Record>, schema.html from the base documentation.