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