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;
138 my $desc = $discount->description_short;
139 $desc .= $self->mt(' each') if $self->cust_bill_pkg->quantity > 1;
141 if ($discount->months) {
142 # calculate months remaining on this cust_pkg_discount after this invoice
143 my $date = $self->cust_bill_pkg->cust_bill->_date;
144 my $used = FS::Record->scalar_sql(
145 'SELECT SUM(months) FROM cust_bill_pkg_discount
146 JOIN cust_bill_pkg USING (billpkgnum)
147 JOIN cust_bill USING (invnum)
148 WHERE pkgdiscountnum = ? AND _date <= ?',
149 $self->pkgdiscountnum,
153 my $remaining = sprintf('%.2f', $discount->months - $used);
154 $desc .= $self->mt(' for [quant,_1,month] ([quant,_2,month] remaining)',
168 L<FS::Record>, schema.html from the base documentation.