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( Amount ) ],
147 'align' => [ qw( r ) ],
148 'span' => [ qw( 7 ) ], # unitprices?
149 'width' => [ qw( 13.8cm ) ], # don't like this
153 sub summary_formats_labelhash {
154 map { $_ => join(',', @{$summary_formats{$_}{label}}) } keys %summary_formats;
157 =item header_generator FORMAT
159 Returns a coderef used for generation of an invoice line item header for this
160 usage_class. FORMAT is either html or latex
170 sub _generator_defaults {
171 my ( $self, $format ) = ( shift, shift );
172 return ( $summary_formats{$self->format}, ' ', ' ', ' ', sub { shift } );
175 sub header_generator {
176 my ( $self, $format ) = ( shift, shift );
178 my ( $f, $prefix, $suffix, $separator, $column ) =
179 $self->_generator_defaults($format);
181 if ($format eq 'latex') {
182 $prefix = "\\hline\n\\rule{0pt}{2.5ex}\n\\makebox[1.4cm]{}&\n";
183 $suffix = "\\\\\n\\hline";
186 sub { my ($d,$a,$s,$w) = @_;
187 return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{\\textbf{$d}}}";
189 } elsif ( $format eq 'html' ) {
190 $prefix = '<th></th>';
194 sub { my ($d,$a,$s,$w) = @_;
195 return qq!<th align="$html_align{$a}">$d</th>!;
203 foreach (my $i = 0; $f->{label}->[$i]; $i++) {
205 &{$column}( map { $f->{$_}->[$i] } qw(label align span width) );
208 $prefix. join($separator, @result). $suffix;
213 =item description_generator FORMAT
215 Returns a coderef used for generation of invoice line items for this
216 usage_class. FORMAT is either html or latex
220 sub description_generator {
221 my ( $self, $format ) = ( shift, shift );
223 my ( $f, $prefix, $suffix, $separator, $column ) =
224 $self->_generator_defaults($format);
226 if ($format eq 'latex') {
227 $prefix = "\\hline\n\\multicolumn{1}{c}{\\rule{0pt}{2.5ex}~} &\n";
229 $separator = " & \n";
231 sub { my ($d,$a,$s,$w) = @_;
232 return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{\\textbf{$d}}}";
234 }elsif ( $format eq 'html' ) {
235 $prefix = '"><td align="center"></td>';
239 sub { my ($d,$a,$s,$w) = @_;
240 return qq!<td align="$html_align{$a}">$d</td>!;
248 foreach (my $i = 0; $f->{label}->[$i]; $i++) {
249 push @result, &{$column}( &{$f->{fields}->[$i]}(@args),
250 map { $f->{$_}->[$i] } qw(align span width)
254 $prefix. join( $separator, @result ). $suffix;
259 =item total_generator FORMAT
261 Returns a coderef used for generation of invoice total lines for this
262 usage_class. FORMAT is either html or latex
266 sub total_generator {
267 my ( $self, $format ) = ( shift, shift );
269 # $OUT .= '\FStotaldesc{' . $section->{'description'} . ' Total}' .
270 # '{' . $section->{'subtotal'} . '}' . "\n";
272 my ( $f, $prefix, $suffix, $separator, $column ) =
273 $self->_generator_defaults($format);
276 if ($format eq 'latex') {
279 $separator = " & \n";
281 sub { my ($d,$a,$s,$w) = @_;
282 return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{$d}}";
284 }elsif ( $format eq 'html' ) {
288 $style = 'border-top: 3px solid #000000;border-bottom: 3px solid #000000;';
290 sub { my ($d,$a,$s,$w) = @_;
291 return qq!<td align="$html_align{$a}" style="$style">$d</td>!;
300 # my $r = &{$f->{fields}->[$i]}(@args);
301 # $r .= ' Total' unless $i;
303 foreach (my $i = 0; $f->{label}->[$i]; $i++) {
305 &{$column}( &{$f->{fields}->[$i]}(@args). ($i ? '' : ' Total'),
306 map { $f->{$_}->[$i] } qw(align span width)
310 $prefix. join( $separator, @result ). $suffix;
315 =item total_line_generator FORMAT
317 Returns a coderef used for generation of invoice total line items for this
318 usage_class. FORMAT is either html or latex
322 # not used: will have issues with hash element names (description vs
323 # total_item and amount vs total_amount -- another array of functions?
325 sub total_line_generator {
326 my ( $self, $format ) = ( shift, shift );
328 # $OUT .= '\FStotaldesc{' . $line->{'total_item'} . '}' .
329 # '{' . $line->{'total_amount'} . '}' . "\n";
331 my ( $f, $prefix, $suffix, $separator, $column ) =
332 $self->_generator_defaults($format);
335 if ($format eq 'latex') {
338 $separator = " & \n";
340 sub { my ($d,$a,$s,$w) = @_;
341 return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{$d}}";
343 }elsif ( $format eq 'html' ) {
347 $style = 'border-top: 3px solid #000000;border-bottom: 3px solid #000000;';
349 sub { my ($d,$a,$s,$w) = @_;
350 return qq!<td align="$html_align{$a}" style="$style">$d</td>!;
359 foreach (my $i = 0; $f->{label}->[$i]; $i++) {
361 &{$column}( &{$f->{fields}->[$i]}(@args),
362 map { $f->{$_}->[$i] } qw(align span width)
366 $prefix. join( $separator, @result ). $suffix;
373 sub _populate_initial_data {
374 my ($class, %opts) = @_;
376 foreach ("Intrastate", "Interstate", "International") {
377 my $object = $class->new( { 'classname' => $_ } );
378 my $error = $object->insert;
379 die "error inserting $class into database: $error\n"
390 return $class->_populate_initial_data(@_)
391 unless scalar( qsearch( 'usage_class', {} ) );
403 L<FS::Record>, schema.html from the base documentation.