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
132 'label' => [ qw( Description Calls Amount ) ],
134 sub { shift->{description} },
135 sub { shift->{calls} },
136 sub { shift->{amount} },
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
143 'label' => [ qw( Date Time Number Destination Duration Amount ) ],
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
158 sub summary_formats_labelhash {
159 map { $_ => join(',', @{$summary_formats{$_}{label}}) } keys %summary_formats;
162 =item header_generator FORMAT
164 Returns a coderef used for generation of an invoice line item header for this
165 usage_class. FORMAT is either html or latex
175 sub _generator_defaults {
176 my ( $self, $format ) = ( shift, shift );
177 return ( $summary_formats{$self->format}, ' ', ' ', ' ', sub { shift } );
180 sub header_generator {
181 my ( $self, $format ) = ( shift, shift );
183 my ( $f, $prefix, $suffix, $separator, $column ) =
184 $self->_generator_defaults($format);
186 if ($format eq 'latex') {
187 $prefix = "\\hline\n\\rule{0pt}{2.5ex}\n\\makebox[1.4cm]{}&\n";
188 $suffix = "\\\\\n\\hline";
191 sub { my ($d,$a,$s,$w) = @_;
192 return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{\\textbf{$d}}}";
194 } elsif ( $format eq 'html' ) {
195 $prefix = '<th></th>';
199 sub { my ($d,$a,$s,$w) = @_;
200 return qq!<th align="$html_align{$a}">$d</th>!;
208 foreach (my $i = 0; $f->{label}->[$i]; $i++) {
210 &{$column}( map { $f->{$_}->[$i] } qw(label align span width) );
213 $prefix. join($separator, @result). $suffix;
218 =item description_generator FORMAT
220 Returns a coderef used for generation of invoice line items for this
221 usage_class. FORMAT is either html or latex
225 sub description_generator {
226 my ( $self, $format ) = ( shift, shift );
228 my ( $f, $prefix, $suffix, $separator, $column ) =
229 $self->_generator_defaults($format);
231 if ($format eq 'latex') {
232 $prefix = "\\hline\n\\multicolumn{1}{c}{\\rule{0pt}{2.5ex}~} &\n";
234 $separator = " & \n";
236 sub { my ($d,$a,$s,$w) = @_;
237 return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{\\textbf{$d}}}";
239 }elsif ( $format eq 'html' ) {
240 $prefix = '"><td align="center"></td>';
244 sub { my ($d,$a,$s,$w) = @_;
245 return qq!<td align="$html_align{$a}">$d</td>!;
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)
259 $prefix. join( $separator, @result ). $suffix;
264 =item total_generator FORMAT
266 Returns a coderef used for generation of invoice total lines for this
267 usage_class. FORMAT is either html or latex
271 sub total_generator {
272 my ( $self, $format ) = ( shift, shift );
274 # $OUT .= '\FStotaldesc{' . $section->{'description'} . ' Total}' .
275 # '{' . $section->{'subtotal'} . '}' . "\n";
277 my ( $f, $prefix, $suffix, $separator, $column ) =
278 $self->_generator_defaults($format);
281 if ($format eq 'latex') {
284 $separator = " & \n";
286 sub { my ($d,$a,$s,$w) = @_;
287 return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{$d}}";
289 }elsif ( $format eq 'html' ) {
293 $style = 'border-top: 3px solid #000000;border-bottom: 3px solid #000000;';
295 sub { my ($d,$a,$s,$w) = @_;
296 return qq!<td align="$html_align{$a}" style="$style">$d</td>!;
305 # my $r = &{$f->{fields}->[$i]}(@args);
306 # $r .= ' Total' unless $i;
308 foreach (my $i = 0; $f->{label}->[$i]; $i++) {
310 &{$column}( &{$f->{fields}->[$i]}(@args). ($i ? '' : ' Total'),
311 map { $f->{$_}->[$i] } qw(align span width)
315 $prefix. join( $separator, @result ). $suffix;
320 =item total_line_generator FORMAT
322 Returns a coderef used for generation of invoice total line items for this
323 usage_class. FORMAT is either html or latex
327 # not used: will have issues with hash element names (description vs
328 # total_item and amount vs total_amount -- another array of functions?
330 sub total_line_generator {
331 my ( $self, $format ) = ( shift, shift );
333 # $OUT .= '\FStotaldesc{' . $line->{'total_item'} . '}' .
334 # '{' . $line->{'total_amount'} . '}' . "\n";
336 my ( $f, $prefix, $suffix, $separator, $column ) =
337 $self->_generator_defaults($format);
340 if ($format eq 'latex') {
343 $separator = " & \n";
345 sub { my ($d,$a,$s,$w) = @_;
346 return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{$d}}";
348 }elsif ( $format eq 'html' ) {
352 $style = 'border-top: 3px solid #000000;border-bottom: 3px solid #000000;';
354 sub { my ($d,$a,$s,$w) = @_;
355 return qq!<td align="$html_align{$a}" style="$style">$d</td>!;
364 foreach (my $i = 0; $f->{label}->[$i]; $i++) {
366 &{$column}( &{$f->{fields}->[$i]}(@args),
367 map { $f->{$_}->[$i] } qw(align span width)
371 $prefix. join( $separator, @result ). $suffix;
378 sub _populate_initial_data {
379 my ($class, %opts) = @_;
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"
395 return $class->_populate_initial_data(@_)
396 unless scalar( qsearch( 'usage_class', {} ) );
408 L<FS::Record>, schema.html from the base documentation.