X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fusage_class.pm;h=b64d26ac4198c9bf3038c15aed43d8987377a8b8;hb=bf4223e554e0eb72f45f82392442780e8af4e0f9;hp=93a32df4740c6949399bb1ab63246a53f2e8909f;hpb=aa8d3305bddaca26d9222ebfa48af191d28f8230;p=freeside.git
diff --git a/FS/FS/usage_class.pm b/FS/FS/usage_class.pm
index 93a32df47..b64d26ac4 100644
--- a/FS/FS/usage_class.pm
+++ b/FS/FS/usage_class.pm
@@ -97,7 +97,9 @@ sub check {
my $error =
$self->ut_numbern('classnum')
+ || $self->ut_numbern('weight')
|| $self->ut_text('classname')
+ || $self->ut_textn('format')
|| $self->ut_enum('disabled', [ '', 'Y' ])
;
return $error if $error;
@@ -105,6 +107,274 @@ sub check {
$self->SUPER::check;
}
+=item summary_formats_labelhash
+
+Returns a list of line item format descriptions suitable for assigning to
+a hash.
+
+=cut
+
+# transform hashes of arrays to arrays of hashes for false laziness removal?
+my %summary_formats = (
+ 'simple' => {
+ 'label' => [ qw( Description Calls Minutes Amount ) ],
+ 'fields' => [
+ sub { shift->{description} },
+ sub { shift->{calls} },
+ sub { sprintf( '%.1f', shift->{duration}/60 ) },
+ sub { shift->{amount} },
+ ],
+ 'align' => [ qw( l r r r ) ],
+ 'span' => [ qw( 4 1 1 1 ) ], # unitprices?
+ 'width' => [ qw( 8.2cm 2.5cm 1.4cm 1.6cm ) ], # don't like this
+ },
+ 'simpler' => {
+ 'label' => [ qw( Description Calls Amount ) ],
+ 'fields' => [
+ sub { shift->{description} },
+ sub { shift->{calls} },
+ sub { shift->{amount} },
+ ],
+ 'align' => [ qw( l r r ) ],
+ 'span' => [ qw( 5 1 1 ) ],
+ 'width' => [ qw( 10.7cm 1.4cm 1.6cm ) ], # don't like this
+ },
+ 'usage_simple' => {
+ 'label' => [ qw( Date Time Number Destination Duration Amount ) ],
+ 'fields' => [
+ sub { ' ' },
+ sub { ' ' },
+ sub { ' ' },
+ sub { ' ' },
+ sub { ' ' },
+ sub { ' ' },
+ ],
+ 'align' => [ qw( l l l l r r ) ],
+ 'span' => [ qw( 2 1 1 1 1 1 ) ], # unitprices?
+ 'width' => [ qw( 4.3cm 1.4cm 2.5cm 2.5cm 1.4cm 1.6cm ) ],# don't like this
+ },
+);
+
+sub summary_formats_labelhash {
+ map { $_ => join(',', @{$summary_formats{$_}{label}}) } keys %summary_formats;
+}
+
+=item header_generator FORMAT
+
+Returns a coderef used for generation of an invoice line item header for this
+usage_class. FORMAT is either html or latex
+
+=cut
+
+my %html_align = (
+ 'c' => 'center',
+ 'l' => 'left',
+ 'r' => 'right',
+);
+
+sub _generator_defaults {
+ my ( $self, $format ) = ( shift, shift );
+ return ( $summary_formats{$self->format}, ' ', ' ', ' ', sub { shift } );
+}
+
+sub header_generator {
+ my ( $self, $format ) = ( shift, shift );
+
+ my ( $f, $prefix, $suffix, $separator, $column ) =
+ $self->_generator_defaults($format);
+
+ if ($format eq 'latex') {
+ $prefix = "\\hline\n\\rule{0pt}{2.5ex}\n\\makebox[1.4cm]{}&\n";
+ $suffix = "\\\\\n\\hline";
+ $separator = "&\n";
+ $column =
+ sub { my ($d,$a,$s,$w) = @_;
+ return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{\\textbf{$d}}}";
+ };
+ } elsif ( $format eq 'html' ) {
+ $prefix = '
| ';
+ $suffix = '';
+ $separator = '';
+ $column =
+ sub { my ($d,$a,$s,$w) = @_;
+ return qq!$d | !;
+ };
+ }
+
+ sub {
+ my @args = @_;
+ my @result = ();
+
+ foreach (my $i = 0; $f->{label}->[$i]; $i++) {
+ push @result,
+ &{$column}( map { $f->{$_}->[$i] } qw(label align span width) );
+ }
+
+ $prefix. join($separator, @result). $suffix;
+ };
+
+}
+
+=item description_generator FORMAT
+
+Returns a coderef used for generation of invoice line items for this
+usage_class. FORMAT is either html or latex
+
+=cut
+
+sub description_generator {
+ my ( $self, $format ) = ( shift, shift );
+
+ my ( $f, $prefix, $suffix, $separator, $column ) =
+ $self->_generator_defaults($format);
+
+ if ($format eq 'latex') {
+ $prefix = "\\hline\n\\multicolumn{1}{c}{\\rule{0pt}{2.5ex}~} &\n";
+ $suffix = '\\\\';
+ $separator = " & \n";
+ $column =
+ sub { my ($d,$a,$s,$w) = @_;
+ return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{\\textbf{$d}}}";
+ };
+ }elsif ( $format eq 'html' ) {
+ $prefix = '"> | ';
+ $suffix = '';
+ $separator = '';
+ $column =
+ sub { my ($d,$a,$s,$w) = @_;
+ return qq!$d | !;
+ };
+ }
+
+ sub {
+ my @args = @_;
+ my @result = ();
+
+ foreach (my $i = 0; $f->{label}->[$i]; $i++) {
+ push @result, &{$column}( &{$f->{fields}->[$i]}(@args),
+ map { $f->{$_}->[$i] } qw(align span width)
+ );
+ }
+
+ $prefix. join( $separator, @result ). $suffix;
+ };
+
+}
+
+=item total_generator FORMAT
+
+Returns a coderef used for generation of invoice total lines for this
+usage_class. FORMAT is either html or latex
+
+=cut
+
+sub total_generator {
+ my ( $self, $format ) = ( shift, shift );
+
+# $OUT .= '\FStotaldesc{' . $section->{'description'} . ' Total}' .
+# '{' . $section->{'subtotal'} . '}' . "\n";
+
+ my ( $f, $prefix, $suffix, $separator, $column ) =
+ $self->_generator_defaults($format);
+ my $style = '';
+
+ if ($format eq 'latex') {
+ $prefix = "& ";
+ $suffix = "\\\\\n";
+ $separator = " & \n";
+ $column =
+ sub { my ($d,$a,$s,$w) = @_;
+ return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{$d}}";
+ };
+ }elsif ( $format eq 'html' ) {
+ $prefix = '';
+ $suffix = '';
+ $separator = '';
+ $style = 'border-top: 3px solid #000000;border-bottom: 3px solid #000000;';
+ $column =
+ sub { my ($d,$a,$s,$w) = @_;
+ return qq!$d | !;
+ };
+ }
+
+
+ sub {
+ my @args = @_;
+ my @result = ();
+
+ # my $r = &{$f->{fields}->[$i]}(@args);
+ # $r .= ' Total' unless $i;
+
+ foreach (my $i = 0; $f->{label}->[$i]; $i++) {
+ push @result,
+ &{$column}( &{$f->{fields}->[$i]}(@args). ($i ? '' : ' Total'),
+ map { $f->{$_}->[$i] } qw(align span width)
+ );
+ }
+
+ $prefix. join( $separator, @result ). $suffix;
+ };
+
+}
+
+=item total_line_generator FORMAT
+
+Returns a coderef used for generation of invoice total line items for this
+usage_class. FORMAT is either html or latex
+
+=cut
+
+# not used: will have issues with hash element names (description vs
+# total_item and amount vs total_amount -- another array of functions?
+
+sub total_line_generator {
+ my ( $self, $format ) = ( shift, shift );
+
+# $OUT .= '\FStotaldesc{' . $line->{'total_item'} . '}' .
+# '{' . $line->{'total_amount'} . '}' . "\n";
+
+ my ( $f, $prefix, $suffix, $separator, $column ) =
+ $self->_generator_defaults($format);
+ my $style = '';
+
+ if ($format eq 'latex') {
+ $prefix = "& ";
+ $suffix = "\\\\\n";
+ $separator = " & \n";
+ $column =
+ sub { my ($d,$a,$s,$w) = @_;
+ return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{$d}}";
+ };
+ }elsif ( $format eq 'html' ) {
+ $prefix = '';
+ $suffix = '';
+ $separator = '';
+ $style = 'border-top: 3px solid #000000;border-bottom: 3px solid #000000;';
+ $column =
+ sub { my ($d,$a,$s,$w) = @_;
+ return qq!$d | !;
+ };
+ }
+
+
+ sub {
+ my @args = @_;
+ my @result = ();
+
+ foreach (my $i = 0; $f->{label}->[$i]; $i++) {
+ push @result,
+ &{$column}( &{$f->{fields}->[$i]}(@args),
+ map { $f->{$_}->[$i] } qw(align span width)
+ );
+ }
+
+ $prefix. join( $separator, @result ). $suffix;
+ };
+
+}
+
+
+
sub _populate_initial_data {
my ($class, %opts) = @_;