enable CardFortress in test database, #71513
[freeside.git] / FS / FS / cust_bill_pkg_discount.pm
1 package FS::cust_bill_pkg_discount;
2 use base qw( FS::cust_main_Mixin FS::Record );
3
4 use strict;
5
6 =head1 NAME
7
8 FS::cust_bill_pkg_discount - Object methods for cust_bill_pkg_discount records
9
10 =head1 SYNOPSIS
11
12   use FS::cust_bill_pkg_discount;
13
14   $record = new FS::cust_bill_pkg_discount \%hash;
15   $record = new FS::cust_bill_pkg_discount { 'column' => 'value' };
16
17   $error = $record->insert;
18
19   $error = $new_record->replace($old_record);
20
21   $error = $record->delete;
22
23   $error = $record->check;
24
25 =head1 DESCRIPTION
26
27 An FS::cust_bill_pkg_discount object represents the slice of a customer
28 discount applied to a specific line item.  FS::cust_bill_pkg_discount inherits
29 from FS::Record.  The following fields are currently supported:
30
31 =over 4
32
33 =item billpkgdiscountnum
34
35 primary key
36
37 =item billpkgnum
38
39 Line item (see L<FS::cust_bill_pkg>)
40
41 =item pkgdiscountnum
42
43 Customer discount (see L<FS::cust_pkg_discount>)
44
45 =item amount
46
47 Amount discounted from the line itme.
48
49 =item months
50
51 Number of months of discount this represents.
52
53 =back
54
55 =head1 METHODS
56
57 =over 4
58
59 =item new HASHREF
60
61 Creates a new record.  To add the record to the database, see L<"insert">.
62
63 Note that this stores the hash reference, not a distinct copy of the hash it
64 points to.  You can ask the object for a copy with the I<hash> method.
65
66 =cut
67
68 # the new method can be inherited from FS::Record, if a table method is defined
69
70 sub table { 'cust_bill_pkg_discount'; }
71
72 =item insert
73
74 Adds this record to the database.  If there is an error, returns the error,
75 otherwise returns false.
76
77 =cut
78
79 # the insert method can be inherited from FS::Record
80
81 =item delete
82
83 Delete this record from the database.
84
85 =cut
86
87 # the delete method can be inherited from FS::Record
88
89 =item replace OLD_RECORD
90
91 Replaces the OLD_RECORD with this one in the database.  If there is an error,
92 returns the error, otherwise returns false.
93
94 =cut
95
96 # the replace method can be inherited from FS::Record
97
98 =item check
99
100 Checks all fields to make sure this is a valid record.  If there is
101 an error, returns the error, otherwise returns false.  Called by the insert
102 and replace methods.
103
104 =cut
105
106 sub check {
107   my $self = shift;
108
109   my $error = 
110     $self->ut_numbern('billpkgdiscountnum')
111     || $self->ut_foreign_key('billpkgnum', 'cust_bill_pkg', 'billpkgnum' )
112     || $self->ut_foreign_key('pkgdiscountnum', 'cust_pkg_discount', 'pkgdiscountnum' )
113     || $self->ut_money('amount')
114     || $self->ut_float('months')
115   ;
116   return $error if $error;
117
118   $self->SUPER::check;
119 }
120
121 =item cust_bill_pkg
122
123 Returns the associated line item (see L<FS::cust_bill_pkg>).
124
125 =item cust_pkg_discount
126
127 Returns the associated customer discount (see L<FS::cust_pkg_discount>).
128
129 =item description
130
131 Returns a string describing the discount (for use on an invoice).
132
133 =cut
134
135 sub description {
136   my $self = shift;
137   my $discount = $self->cust_pkg_discount->discount;
138
139   if ( $self->months == 0 ) {
140     # then this is a setup discount
141     my $desc = $discount->name;
142     if ( $desc ) {
143       $desc .= ': ';
144     } else {
145       $desc = $self->mt('Setup discount of ');
146     }
147     if ( (my $percent = $discount->percent) > 0 ) {
148       $percent = sprintf('%.1f', $percent) if $percent > int($percent);
149       $percent =~ s/\.0+$//;
150       $desc .= $percent . '%';
151     } else {
152       # note "$self->amount", not $discount->amount. if a flat discount
153       # is applied to the setup fee, show the amount actually discounted.
154       # we might do this for all types of discounts.
155       my $money_char = FS::Conf->new->config('money_char') || '$';
156       $desc .= $money_char . sprintf('%.2f', $self->amount);
157     }
158   
159     # don't show "/month", months remaining or used, etc., as for setup
160     # discounts it doesn't matter.
161     return $desc;
162   }
163
164   my $desc = $discount->description_short;
165   $desc .= $self->mt(' each') if $self->cust_bill_pkg->quantity > 1;
166
167   if ( $discount->months and $self->months > 0 ) {
168     # calculate months remaining on this cust_pkg_discount after this invoice
169     my $date = $self->cust_bill_pkg->cust_bill->_date;
170     my $used = FS::Record->scalar_sql(
171       'SELECT SUM(months) FROM cust_bill_pkg_discount
172       JOIN cust_bill_pkg USING (billpkgnum)
173       JOIN cust_bill USING (invnum)
174       WHERE pkgdiscountnum = ? AND _date <= ?',
175       $self->pkgdiscountnum,
176       $date
177     );
178     $used ||= 0;
179     my $remaining = sprintf('%.2f', $discount->months - $used);
180     $desc .= $self->mt(' for [quant,_1,month] ([quant,_2,month] remaining)',
181               sprintf('%.2f', $self->months),
182               $remaining
183              );
184   }
185   return $desc;
186 }
187
188 =back
189
190 =head1 BUGS
191
192 =head1 SEE ALSO
193
194 L<FS::Record>, schema.html from the base documentation.
195
196 =cut
197
198 1;
199