4 use base qw( FS::Record );
5 use FS::Record qw( qsearch qsearchs );
9 FS::discount - Object methods for discount records
15 $record = new FS::discount \%hash;
16 $record = new FS::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::discount object represents a discount definition. FS::discount inherits
29 from FS::Record. The following fields are currently supported:
65 Creates a new discount. To add the discount to the database, see L<"insert">.
67 Note that this stores the hash reference, not a distinct copy of the hash it
68 points to. You can ask the object for a copy with the I<hash> method.
72 # the new method can be inherited from FS::Record, if a table method is defined
74 sub table { 'discount'; }
78 Adds this record to the database. If there is an error, returns the error,
79 otherwise returns false.
83 # the insert method can be inherited from FS::Record
87 Delete this record from the database.
91 # the delete method can be inherited from FS::Record
93 =item replace OLD_RECORD
95 Replaces the OLD_RECORD with this one in the database. If there is an error,
96 returns the error, otherwise returns false.
100 # the replace method can be inherited from FS::Record
104 Checks all fields to make sure this is a valid discount. If there is
105 an error, returns the error, otherwise returns false. Called by the insert
110 # the check method should currently be supplied - FS::Record contains some
111 # data checking routines
116 if ( $self->_type eq 'Select discount type' ) {
117 return 'Please select a discount type';
118 } elsif ( $self->_type eq 'Amount' ) {
120 return 'Amount must be greater than 0' unless $self->amount > 0;
121 } elsif ( $self->_type eq 'Percentage' ) {
122 $self->amount('0.00');
123 return 'Percentage must be greater than 0' unless $self->percent > 0;
127 $self->ut_numbern('discountnum')
128 || $self->ut_textn('name')
129 || $self->ut_money('amount')
130 || $self->ut_float('percent') #actually decimal, but this will do
131 || $self->ut_floatn('months') #actually decimal, but this will do
132 || $self->ut_enum('disabled', [ '', 'Y' ])
134 return $error if $error;
136 #discourage non-integer months for package discounts
137 if ($self->discountnum) {
139 "SELECT count(*) FROM part_pkg_discount WHERE part_pkg_discount.discountnum = ".
142 my $count = $self->scalar_sql($sql);
143 return "months must be integers greater than 1"
144 if ( $count && ($self->ut_number('months') || $self->months < 2) );
150 =item description_short
154 Returns a text description incorporating the amount, percent and months fields.
156 description_short omits term information
160 sub description_short {
163 my $conf = new FS::Conf;
164 my $money_char = $conf->config('money_char') || '$';
166 my $desc = $self->name ? $self->name.': ' : '';
167 $desc .= $money_char. sprintf('%.2f/month ', $self->amount)
168 if $self->amount > 0;
169 $desc .= $self->percent. '% '
170 if $self->percent > 0;
177 my $desc = $self->description_short;
178 $desc .= ' for '. $self->months. ' months' if $self->months;
188 L<FS::cust_pkg_discount>, L<FS::Record>, schema.html from the base documentation.