summaryrefslogtreecommitdiff
path: root/FS/FS/usage_class.pm
blob: 6c8a278ba58640ab0ef58b7e15d6660614ce8fda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package FS::usage_class;

use strict;
use vars qw( @ISA );
use FS::Record qw( qsearch qsearchs );
use FS::Conf;

my $conf = new FS::Conf;

@ISA = qw(FS::Record);

=head1 NAME

FS::usage_class - Object methods for usage_class records

=head1 SYNOPSIS

  use FS::usage_class;

  $record = new FS::usage_class \%hash;
  $record = new FS::usage_class { 'column' => 'value' };

  $error = $record->insert;

  $error = $new_record->replace($old_record);

  $error = $record->delete;

  $error = $record->check;

=head1 DESCRIPTION

An FS::usage_class object represents a usage class.  Every rate detail
(see L<FS::rate_detail>) has, optionally, a usage class.  FS::usage_class
inherits from FS::Record.  The following fields are currently supported:

=over 4

=item classnum

Primary key (assigned automatically for new usage classes)

=item classname

Text name of this usage class

=item disabled

Disabled flag, empty or 'Y'


=back

=head1 METHODS

=over 4

=item new HASHREF

Creates a new usage class.  To add the usage class to the database,
see L<"insert">.

Note that this stores the hash reference, not a distinct copy of the hash it
points to.  You can ask the object for a copy with the I<hash> method.

=cut

sub table { 'usage_class'; }

=item insert

Adds this record to the database.  If there is an error, returns the error,
otherwise returns false.

=cut

=item delete

Delete this record from the database.

=cut

=item replace OLD_RECORD

Replaces the OLD_RECORD with this one in the database.  If there is an error,
returns the error, otherwise returns false.

=cut

=item check

Checks all fields to make sure this is a valid usage class.  If there is
an error, returns the error, otherwise returns false.  Called by the insert
and replace methods.

=cut

sub check {
  my $self = shift;

  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;

  $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 { my($href, %opt) = @_; 
                        ($opt{dollar} || ''). $href->{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
    'show'   => 1,
  },
  'simpler' => { 
    'label' =>  [ qw( Description Calls Amount ) ],
    'fields' => [
                  sub { shift->{description} },
                  sub { shift->{calls} },
                  sub { my($href, %opt) = @_; 
                        ($opt{dollar} || ''). $href->{amount};
                      },
                ],
    'align'  => [ qw( l r r ) ],
    'span'   => [ qw( 5 1 1 ) ],
    'width'  => [ qw( 10.7cm 1.4cm 1.6cm ) ],   # don't like this
    'show'   => 1,
  },
  'usage_simple' => { 
    'label' => [ qw( Date Time Number Destination Duration Amount ) ],
    'fields' => [
                  sub { ' ' },
                  sub { ' ' },
                  sub { ' ' },
                  sub { ' ' },
                  sub { ' ' },
                  sub { my $href = shift;  #ugh! making bunk of 'normalization'
                        $href->{subtotal} ? $href->{subtotal} : ' '
                      },
                ],
    'align'  => [ qw( l l l l r r ) ],
    'span'   => [ qw( 1 1 1 1 1 2 ) ],            # unitprices?
    'width'  => [ qw( 4.3cm 1.4cm 2.5cm 2.5cm 1.4cm 1.6cm ) ],# don't like this
    'show'   => 0,
  },
  'usage_6col' => { 
    'label' => [ qw( col1 col2 col3 col4 col5 col6 ) ],
    'fields' => [
                  sub { ' ' },
                  sub { ' ' },
                  sub { ' ' },
                  sub { ' ' },
                  sub { ' ' },
                  sub { my $href = shift;  #ugh! making bunk of 'normalization'
                        $href->{subtotal} ? $href->{subtotal} : ' '
                      },
                ],
    'align'  => [ qw( l l l l r r ) ],
    'span'   => [ qw( 1 1 1 1 1 2 ) ],            # unitprices?
    'width'  => [ qw( 4.3cm 1.4cm 2.5cm 2.5cm 1.4cm 1.6cm ) ],# don't like this
    'show'   => 0,
   },
  'usage_4col' => { 
    'label' => [ qw( col1 col2 col3 col4 ) ],
    'fields' => [
                  sub { ' ' },
                  sub { ' ' },
                  sub { ' ' },
                  sub { ' ' },
                ],
    'align'  => [ qw( l l l l r r ) ],
    'span'   => [ qw( 1 1 1 1 1 2 ) ],          
    'width'  => [ qw( 4.3cm 1.4cm 2.5cm 2.5cm ) ],
    'show'   => 0,
  },
  'usage_7col' => { 
    'label' => [ qw( col1 col2 col3 col4 col5 col6 col7 ) ],
    'fields' => [
                  sub { ' ' },
                  sub { ' ' },
                  sub { ' ' },
                  sub { ' ' },
                  sub { ' ' },
                  sub { ' ' },
                  sub { my $href = shift;  #ugh! making bunk of 'normalization'
                        $href->{subtotal} ? $href->{subtotal} : ' '
                      },
                ],
    'align'  => [ qw( l l l l l r r ) ],
    'span'   => [ qw( 1 1 1 1 1 1 1 ) ],            # unitprices?
    'width'  => [ qw( 2.9cm 1.4cm 1.4cm 2.5cm 2.5cm 1.4cm 1.6cm ) ],# don't like this
    'show'   => 0,
  },
);

sub summary_formats_labelhash {
  map { $_ => join(',', @{$summary_formats{$_}{label}}) }
    grep { $summary_formats{$_}{show} }
    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, %opt ) = @_;
  my %format = ( %{ $summary_formats{$self->format} }, %opt );
  return ( \%format, ' ', ' ', ' ', sub { shift } );
}

sub header_generator {
  my ( $self, $format, %opt ) = @_;

  my ( $f, $prefix, $suffix, $separator, $column ) =
    $self->_generator_defaults($format, %opt);

  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 = '<th></th>';
    $suffix = '';
    $separator = '';
    $column =
      sub { my ($d,$a,$s,$w) = @_;
            return qq!<th align="$html_align{$a}">$d</th>!;
      };
  }

  sub {
    my @args = @_;
    my @result = ();

    foreach  (my $i = 0; exists($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, %opt ) = @_;

  my ( $f, $prefix, $suffix, $separator, $column ) =
    $self->_generator_defaults($format, %opt);

  my $money_char = '$';
  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}}}";
          };
    $money_char = '\\dollar';
  }elsif ( $format eq 'html' ) {
    $prefix = '"><td align="center"></td>';
    $suffix = '';
    $separator = '';
    $column =
      sub { my ($d,$a,$s,$w) = @_;
            return qq!<td align="$html_align{$a}">$d</td>!;
      };
    $money_char = $conf->config('money_char') || '$';
  }

  sub {
    #my @args = @_;
    my ($href) = shift;
    my @result = ();

    foreach  (my $i = 0; $f->{label}->[$i]; $i++) {
      my $dollar = '';
      $dollar = $money_char if $i == scalar(@{$f->{label}})-1;
      push @result,
        &{$column}( &{$f->{fields}->[$i]}($href, 'dollar' => $dollar),
                    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, %opt ) = @_;

#  $OUT .= '\FStotaldesc{' . $section->{'description'} . ' Total}' .
#          '{' . $section->{'subtotal'} . '}' . "\n";

  my ( $f, $prefix, $suffix, $separator, $column ) =
    $self->_generator_defaults($format, %opt);
  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!<td align="$html_align{$a}" style="$style">$d</td>!;
      };
  }
  

  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, %opt ) = @_;

#     $OUT .= '\FStotaldesc{' . $line->{'total_item'} . '}' .
#             '{' . $line->{'total_amount'} . '}' . "\n";

  my ( $f, $prefix, $suffix, $separator, $column ) =
    $self->_generator_defaults($format, %opt);
  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!<td align="$html_align{$a}" style="$style">$d</td>!;
      };
  }
  

  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;
  };

}

# Used by FS::Setup to initialize a new database.
sub _populate_initial_data {
  my ($class, %opts) = @_;

  foreach ("Intrastate", "Interstate", "International") {
    my $object = $class->new( { 'classname' => $_ } );
    my $error = $object->insert;
    die "error inserting $class into database: $error\n"
      if $error;
  }

  '';

}

# Used by FS::Upgrade to migrate to a new database.
sub _upgrade_data {
  my $class = shift;

  return $class->_populate_initial_data(@_)
    unless scalar( qsearch( 'usage_class', {} ) );

  '';

}

=back

=head1 BUGS

=head1 SEE ALSO

L<FS::Record>, schema.html from the base documentation.

=cut

1;