fix TeleAPI import (what kind of crack was Christopher smoking that he couldn't fix...
[freeside.git] / FS / FS / part_fee_usage.pm
1 package FS::part_fee_usage;
2
3 use strict;
4 use base qw( FS::Record );
5 use FS::Record qw( qsearch qsearchs );
6 use FS::Conf;
7
8 =head1 NAME
9
10 FS::part_fee_usage - Object methods for part_fee_usage records
11
12 =head1 SYNOPSIS
13
14   use FS::part_fee_usage;
15
16   $record = new FS::part_fee_usage \%hash;
17   $record = new FS::part_fee_usage { 'column' => 'value' };
18
19   $error = $record->insert;
20
21   $error = $new_record->replace($old_record);
22
23   $error = $record->delete;
24
25   $error = $record->check;
26
27 =head1 DESCRIPTION
28
29 An FS::part_fee_usage object is the part of a processing fee definition 
30 (L<FS::part_fee>) that applies to a specific telephone usage class 
31 (L<FS::usage_class>).  FS::part_fee_usage inherits from
32 FS::Record.  The following fields are currently supported:
33
34 =over 4
35
36 =item feepartusagenum - primary key
37
38 =item feepart - foreign key to L<FS::part_pkg>
39
40 =item classnum - foreign key to L<FS::usage_class>
41
42 =item amount - fixed amount to charge per usage record
43
44 =item percent - percentage of rated price to charge per usage record
45
46 =back
47
48 =head1 METHODS
49
50 =over 4
51
52 =cut
53
54 sub table { 'part_fee_usage'; }
55
56 sub check {
57   my $self = shift;
58
59   $self->set('amount', 0)  unless ($self->amount || 0) > 0;
60   $self->set('percent', 0) unless ($self->percent || 0) > 0;
61
62   my $error = 
63     $self->ut_numbern('feepartusagenum')
64     || $self->ut_foreign_key('feepart', 'part_fee', 'feepart')
65     || $self->ut_foreign_key('classnum', 'usage_class', 'classnum')
66     || $self->ut_money('amount')
67     || $self->ut_float('percent')
68   ;
69   return $error if $error;
70
71   $self->SUPER::check;
72 }
73
74 # silently discard records with percent = 0 and amount = 0
75
76 sub insert {
77   my $self = shift;
78   if ( $self->amount > 0 or $self->percent > 0 ) {
79     return $self->SUPER::insert;
80   }
81   '';
82 }
83
84 sub replace {
85   my ($new, $old) = @_;
86   $old ||= $new->replace_old;
87   if ( $new->amount > 0 or $new->percent > 0 ) {
88     return $new->SUPER::replace($old);
89   } elsif ( $old->feepartusagenum ) {
90     return $old->delete;
91   }
92   '';
93 }
94   
95 =item explanation
96
97 Returns a string describing how this fee is calculated.
98
99 =cut
100
101 sub explanation {
102   my $self = shift;
103   my $string = '';
104   my $money = (FS::Conf->new->config('money_char') || '$') . '%.2f';
105   my $percent = '%.1f%%';
106   if ( $self->amount > 0 ) {
107     $string = sprintf($money, $self->amount);
108   }
109   if ( $self->percent > 0 ) {
110     if ( $string ) {
111       $string .= ' plus ';
112     }
113     $string .= sprintf($percent, $self->percent);
114     $string .= ' of the rated charge';
115   }
116   $string .= ' per '.  $self->usage_class->classname . ' call';
117
118   return $string;
119 }
120
121 =back
122
123 =head1 SEE ALSO
124
125 L<FS::Record>, schema.html from the base documentation.
126
127 =cut
128
129 1;
130