summaryrefslogtreecommitdiff
path: root/FS/FS/part_fee_usage.pm
blob: a1b85ae9d5bb3fb82aa8b6c2576c1c7125683559 (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
package FS::part_fee_usage;

use strict;
use base qw( FS::Record );
use FS::Record qw( qsearch qsearchs );
use FS::Conf;

=head1 NAME

FS::part_fee_usage - Object methods for part_fee_usage records

=head1 SYNOPSIS

  use FS::part_fee_usage;

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

  $error = $record->insert;

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

  $error = $record->delete;

  $error = $record->check;

=head1 DESCRIPTION

An FS::part_fee_usage object is the part of a processing fee definition 
(L<FS::part_fee>) that applies to a specific telephone usage class 
(L<FS::usage_class>).  FS::part_fee_usage inherits from
FS::Record.  The following fields are currently supported:

=over 4

=item feepartusagenum - primary key

=item feepart - foreign key to L<FS::part_pkg>

=item classnum - foreign key to L<FS::usage_class>

=item amount - fixed amount to charge per usage record

=item percent - percentage of rated price to charge per usage record

=back

=head1 METHODS

=over 4

=cut

sub table { 'part_fee_usage'; }

sub check {
  my $self = shift;

  $self->set('amount', 0)  unless ($self->amount || 0) > 0;
  $self->set('percent', 0) unless ($self->percent || 0) > 0;

  my $error = 
    $self->ut_numbern('feepartusagenum')
    || $self->ut_foreign_key('feepart', 'part_fee', 'feepart')
    || $self->ut_foreign_key('classnum', 'usage_class', 'classnum')
    || $self->ut_money('amount')
    || $self->ut_float('percent')
  ;
  return $error if $error;

  $self->SUPER::check;
}

# silently discard records with percent = 0 and amount = 0

sub insert {
  my $self = shift;
  if ( $self->amount > 0 or $self->percent > 0 ) {
    return $self->SUPER::insert;
  }
  '';
}

sub replace {
  my ($new, $old) = @_;
  $old ||= $new->replace_old;
  if ( $new->amount > 0 or $new->percent > 0 ) {
    return $new->SUPER::replace($old);
  } elsif ( $old->feepartusagenum ) {
    return $old->delete;
  }
  '';
}
  
=item explanation

Returns a string describing how this fee is calculated.

=cut

sub explanation {
  my $self = shift;
  my $string = '';
  my $money = (FS::Conf->new->config('money_char') || '$') . '%.2f';
  my $percent = '%.1f%%';
  if ( $self->amount > 0 ) {
    $string = sprintf($money, $self->amount);
  }
  if ( $self->percent > 0 ) {
    if ( $string ) {
      $string .= ' plus ';
    }
    $string .= sprintf($percent, $self->percent);
    $string .= ' of the rated charge';
  }
  $string .= ' per '.  $self->usage_class->classname . ' call';

  return $string;
}

=back

=head1 SEE ALSO

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

=cut

1;