1 package FS::usage_class;
5 use FS::Record qw( qsearch qsearchs );
8 my $conf = new FS::Conf;
10 @ISA = qw(FS::Record);
14 FS::usage_class - Object methods for usage_class records
20 $record = new FS::usage_class \%hash;
21 $record = new FS::usage_class { 'column' => 'value' };
23 $error = $record->insert;
25 $error = $new_record->replace($old_record);
27 $error = $record->delete;
29 $error = $record->check;
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:
41 Primary key (assigned automatically for new usage classes)
45 Text name of this usage class
49 Disabled flag, empty or 'Y'
60 Creates a new usage class. To add the usage class to the database,
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.
68 sub table { 'usage_class'; }
72 Adds this record to the database. If there is an error, returns the error,
73 otherwise returns false.
79 Delete this record from the database.
83 =item replace OLD_RECORD
85 Replaces the OLD_RECORD with this one in the database. If there is an error,
86 returns the error, otherwise returns false.
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
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' ])
108 return $error if $error;
113 =item summary_formats_labelhash
115 Returns a list of line item format descriptions suitable for assigning to
120 # transform hashes of arrays to arrays of hashes for false laziness removal?
121 my %summary_formats = (
123 'label' => [ qw( Description Calls Minutes Amount ) ],
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};
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
138 'label' => [ qw( Description Calls Amount ) ],
140 sub { shift->{description} },
141 sub { shift->{calls} },
142 sub { my($href, %opt) = @_;
143 ($opt{dollar} || ''). $href->{amount};
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
152 'label' => [ qw( Date Time Number Destination Duration Amount ) ],
159 sub { my $href = shift; #ugh! making bunk of 'normalization'
160 $href->{subtotal} ? $href->{subtotal} : ' '
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
169 'label' => [ qw( col1 col2 col3 col4 col5 col6 ) ],
176 sub { my $href = shift; #ugh! making bunk of 'normalization'
177 $href->{subtotal} ? $href->{subtotal} : ' '
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
186 'label' => [ qw( col1 col2 col3 col4 col5 col6 col7 ) ],
194 sub { my $href = shift; #ugh! making bunk of 'normalization'
195 $href->{subtotal} ? $href->{subtotal} : ' '
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
205 sub summary_formats_labelhash {
206 map { $_ => join(',', @{$summary_formats{$_}{label}}) }
207 grep { $summary_formats{$_}{show} }
208 keys %summary_formats;
211 =item header_generator FORMAT
213 Returns a coderef used for generation of an invoice line item header for this
214 usage_class. FORMAT is either html or latex
224 sub _generator_defaults {
225 my ( $self, $format, %opt ) = @_;
226 my %format = ( %{ $summary_formats{$self->format} }, %opt );
227 return ( \%format, ' ', ' ', ' ', sub { shift } );
230 sub header_generator {
231 my ( $self, $format, %opt ) = @_;
233 my ( $f, $prefix, $suffix, $separator, $column ) =
234 $self->_generator_defaults($format, %opt);
236 if ($format eq 'latex') {
237 $prefix = "\\hline\n\\rule{0pt}{2.5ex}\n\\makebox[1.4cm]{}&\n";
238 $suffix = "\\\\\n\\hline";
241 sub { my ($d,$a,$s,$w) = @_;
242 return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{\\textbf{$d}}}";
244 } elsif ( $format eq 'html' ) {
245 $prefix = '<th></th>';
249 sub { my ($d,$a,$s,$w) = @_;
250 return qq!<th align="$html_align{$a}">$d</th>!;
258 foreach (my $i = 0; exists($f->{label}->[$i]); $i++) {
260 &{$column}( map { $f->{$_}->[$i] } qw(label align span width) );
263 $prefix. join($separator, @result). $suffix;
268 =item description_generator FORMAT
270 Returns a coderef used for generation of invoice line items for this
271 usage_class. FORMAT is either html or latex
275 sub description_generator {
276 my ( $self, $format, %opt ) = @_;
278 my ( $f, $prefix, $suffix, $separator, $column ) =
279 $self->_generator_defaults($format, %opt);
281 my $money_char = '$';
282 if ($format eq 'latex') {
283 $prefix = "\\hline\n\\multicolumn{1}{c}{\\rule{0pt}{2.5ex}~} &\n";
285 $separator = " & \n";
287 sub { my ($d,$a,$s,$w) = @_;
288 return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{\\textbf{$d}}}";
290 $money_char = '\\dollar';
291 }elsif ( $format eq 'html' ) {
292 $prefix = '"><td align="center"></td>';
296 sub { my ($d,$a,$s,$w) = @_;
297 return qq!<td align="$html_align{$a}">$d</td>!;
299 $money_char = $conf->config('money_char') || '$';
307 foreach (my $i = 0; $f->{label}->[$i]; $i++) {
309 $dollar = $money_char if $i == scalar(@{$f->{label}})-1;
311 &{$column}( &{$f->{fields}->[$i]}($href, 'dollar' => $dollar),
312 map { $f->{$_}->[$i] } qw(align span width)
316 $prefix. join( $separator, @result ). $suffix;
321 =item total_generator FORMAT
323 Returns a coderef used for generation of invoice total lines for this
324 usage_class. FORMAT is either html or latex
328 sub total_generator {
329 my ( $self, $format, %opt ) = @_;
331 # $OUT .= '\FStotaldesc{' . $section->{'description'} . ' Total}' .
332 # '{' . $section->{'subtotal'} . '}' . "\n";
334 my ( $f, $prefix, $suffix, $separator, $column ) =
335 $self->_generator_defaults($format, %opt);
338 if ($format eq 'latex') {
341 $separator = " & \n";
343 sub { my ($d,$a,$s,$w) = @_;
344 return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{$d}}";
346 }elsif ( $format eq 'html' ) {
350 $style = 'border-top: 3px solid #000000;border-bottom: 3px solid #000000;';
352 sub { my ($d,$a,$s,$w) = @_;
353 return qq!<td align="$html_align{$a}" style="$style">$d</td>!;
362 # my $r = &{$f->{fields}->[$i]}(@args);
363 # $r .= ' Total' unless $i;
365 foreach (my $i = 0; $f->{label}->[$i]; $i++) {
367 &{$column}( &{$f->{fields}->[$i]}(@args). ($i ? '' : ' Total'),
368 map { $f->{$_}->[$i] } qw(align span width)
372 $prefix. join( $separator, @result ). $suffix;
377 =item total_line_generator FORMAT
379 Returns a coderef used for generation of invoice total line items for this
380 usage_class. FORMAT is either html or latex
384 # not used: will have issues with hash element names (description vs
385 # total_item and amount vs total_amount -- another array of functions?
387 sub total_line_generator {
388 my ( $self, $format, %opt ) = @_;
390 # $OUT .= '\FStotaldesc{' . $line->{'total_item'} . '}' .
391 # '{' . $line->{'total_amount'} . '}' . "\n";
393 my ( $f, $prefix, $suffix, $separator, $column ) =
394 $self->_generator_defaults($format, %opt);
397 if ($format eq 'latex') {
400 $separator = " & \n";
402 sub { my ($d,$a,$s,$w) = @_;
403 return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{$d}}";
405 }elsif ( $format eq 'html' ) {
409 $style = 'border-top: 3px solid #000000;border-bottom: 3px solid #000000;';
411 sub { my ($d,$a,$s,$w) = @_;
412 return qq!<td align="$html_align{$a}" style="$style">$d</td>!;
421 foreach (my $i = 0; $f->{label}->[$i]; $i++) {
423 &{$column}( &{$f->{fields}->[$i]}(@args),
424 map { $f->{$_}->[$i] } qw(align span width)
428 $prefix. join( $separator, @result ). $suffix;
435 sub _populate_initial_data {
436 my ($class, %opts) = @_;
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"
452 return $class->_populate_initial_data(@_)
453 unless scalar( qsearch( 'usage_class', {} ) );
465 L<FS::Record>, schema.html from the base documentation.