1 package FS::cust_bill_pkg_discount;
2 use base qw( FS::cust_main_Mixin FS::Record );
8 FS::cust_bill_pkg_discount - Object methods for cust_bill_pkg_discount records
12 use FS::cust_bill_pkg_discount;
14 $record = new FS::cust_bill_pkg_discount \%hash;
15 $record = new FS::cust_bill_pkg_discount { 'column' => 'value' };
17 $error = $record->insert;
19 $error = $new_record->replace($old_record);
21 $error = $record->delete;
23 $error = $record->check;
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:
33 =item billpkgdiscountnum
39 Line item (see L<FS::cust_bill_pkg>)
43 Customer discount (see L<FS::cust_pkg_discount>)
47 Amount discounted from the line itme.
51 Number of months of discount this represents.
61 Creates a new record. To add the record to the database, see L<"insert">.
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.
68 # the new method can be inherited from FS::Record, if a table method is defined
70 sub table { 'cust_bill_pkg_discount'; }
74 Adds this record to the database. If there is an error, returns the error,
75 otherwise returns false.
79 # the insert method can be inherited from FS::Record
83 Delete this record from the database.
87 # the delete method can be inherited from FS::Record
89 =item replace OLD_RECORD
91 Replaces the OLD_RECORD with this one in the database. If there is an error,
92 returns the error, otherwise returns false.
96 # the replace method can be inherited from FS::Record
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
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')
116 return $error if $error;
123 Returns the associated line item (see L<FS::cust_bill_pkg>).
125 =item cust_pkg_discount
127 Returns the associated customer discount (see L<FS::cust_pkg_discount>).
131 Returns a string describing the discount (for use on an invoice).
137 my $discount = $self->cust_pkg_discount->discount;
139 if ( $self->months == 0 ) {
140 # then this is a setup discount
141 my $desc = $discount->name;
145 $desc = $self->mt('Setup discount of ');
147 if ( (my $percent = $discount->percent) > 0 ) {
148 $percent = sprintf('%.1f', $percent) if $percent > int($percent);
149 $percent =~ s/\.0+$//;
150 $desc .= $percent . '%';
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);
159 # don't show "/month", months remaining or used, etc., as for setup
160 # discounts it doesn't matter.
164 my $desc = $discount->description_short;
165 $desc .= $self->mt(' each') if $self->cust_bill_pkg->quantity > 1;
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,
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),
194 L<FS::Record>, schema.html from the base documentation.