1 package FS::cust_bill_pkg_discount;
2 use base qw( FS::cust_main_Mixin FS::Record );
5 use FS::Record qw( dbh );
9 FS::cust_bill_pkg_discount - Object methods for cust_bill_pkg_discount records
13 use FS::cust_bill_pkg_discount;
15 $record = new FS::cust_bill_pkg_discount \%hash;
16 $record = new FS::cust_bill_pkg_discount { 'column' => 'value' };
18 $error = $record->insert;
20 $error = $new_record->replace($old_record);
22 $error = $record->delete;
24 $error = $record->check;
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:
34 =item billpkgdiscountnum
40 Line item (see L<FS::cust_bill_pkg>)
44 Customer discount (see L<FS::cust_pkg_discount>)
48 Amount discounted from the line itme.
52 Number of months of discount this represents.
62 Creates a new record. To add the record to the database, see L<"insert">.
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.
69 # the new method can be inherited from FS::Record, if a table method is defined
71 sub table { 'cust_bill_pkg_discount'; }
75 Adds this record to the database. If there is an error, returns the error,
76 otherwise returns false.
80 # the insert method can be inherited from FS::Record
84 Delete this record from the database.
88 # the delete method can be inherited from FS::Record
90 =item replace OLD_RECORD
92 Replaces the OLD_RECORD with this one in the database. If there is an error,
93 returns the error, otherwise returns false.
97 # the replace method can be inherited from FS::Record
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
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')
117 return $error if $error;
124 Returns the associated line item (see L<FS::cust_bill_pkg>).
126 =item cust_pkg_discount
128 Returns the associated customer discount (see L<FS::cust_pkg_discount>).
132 Returns a string describing the discount (for use on an invoice).
138 my $discount = $self->cust_pkg_discount->discount;
140 if ( $self->months == 0 ) {
141 # then this is a setup discount
142 my $desc = $discount->name;
146 $desc = $self->mt('Setup discount of ');
148 if ( (my $percent = $discount->percent) > 0 ) {
149 $percent = sprintf('%.1f', $percent) if $percent > int($percent);
150 $percent =~ s/\.0+$//;
151 $desc .= $percent . '%';
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);
160 # don't show "/month", months remaining or used, etc., as for setup
161 # discounts it doesn't matter.
165 my $desc = $discount->description_short;
166 $desc .= $self->mt(' each') if $self->cust_bill_pkg->quantity > 1;
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,
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),
189 sub _upgrade_schema {
190 my ($class, %opts) = @_;
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 )
197 my $sth = dbh->prepare($sql) or die dbh->errstr;
198 $sth->execute or die $sth->errstr;
208 L<FS::Record>, schema.html from the base documentation.