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:
57 =item setup - apply discount to setup fee (not just to recurring fee)
59 If the discount is based on a percentage, then the % will be applied to the
60 setup and recurring portions.
70 Creates a new discount. To add the discount to the database, see L<"insert">.
72 Note that this stores the hash reference, not a distinct copy of the hash it
73 points to. You can ask the object for a copy with the I<hash> method.
77 # the new method can be inherited from FS::Record, if a table method is defined
79 sub table { 'discount'; }
83 Adds this record to the database. If there is an error, returns the error,
84 otherwise returns false.
88 # the insert method can be inherited from FS::Record
92 Delete this record from the database.
96 # the delete method can be inherited from FS::Record
98 =item replace OLD_RECORD
100 Replaces the OLD_RECORD with this one in the database. If there is an error,
101 returns the error, otherwise returns false.
105 # the replace method can be inherited from FS::Record
109 Checks all fields to make sure this is a valid discount. If there is
110 an error, returns the error, otherwise returns false. Called by the insert
115 # the check method should currently be supplied - FS::Record contains some
116 # data checking routines
121 if ( $self->_type eq 'Select discount type' ) {
122 return 'Please select a discount type';
123 } elsif ( $self->_type eq 'Amount' ) {
125 return 'Amount must be greater than 0' unless $self->amount > 0;
126 } elsif ( $self->_type eq 'Percentage' ) {
127 $self->amount('0.00');
128 return 'Percentage must be greater than 0' unless $self->percent > 0;
132 $self->ut_numbern('discountnum')
133 || $self->ut_textn('name')
134 || $self->ut_money('amount')
135 || $self->ut_float('percent') #actually decimal, but this will do
136 || $self->ut_floatn('months') #actually decimal, but this will do
137 || $self->ut_enum('disabled', [ '', 'Y' ])
138 || $self->ut_enum('setup', [ '', 'Y' ])
140 return $error if $error;
142 #discourage non-integer months for package discounts
143 if ($self->discountnum) {
145 "SELECT count(*) FROM part_pkg_discount WHERE part_pkg_discount.discountnum = ".
148 my $count = $self->scalar_sql($sql);
149 return "months must be integers greater than 1"
150 if ( $count && ($self->ut_number('months') || $self->months < 2) );
156 =item description_short
160 Returns a text description incorporating the amount, percent and months fields.
162 description_short omits term information
166 sub description_short {
169 my $conf = new FS::Conf;
170 my $money_char = $conf->config('money_char') || '$';
172 my $desc = $self->name ? $self->name.': ' : '';
173 $desc .= $money_char. sprintf('%.2f/month ', $self->amount)
174 if $self->amount > 0;
176 ( my $percent = $self->percent ) =~ s/\.0+$//;
177 $percent =~ s/(\.\d*[1-9])0+$/$1/;
178 $desc .= $percent. '% '
179 if $self->percent > 0;
186 my $desc = $self->description_short;
188 ( my $months = $self->months ) =~ s/\.0+$//;
189 $months =~ s/(\.\d*[1-9])0+$/$1/;
190 $desc .= " for $months months" if $months;
192 $desc .= ', applies to setup' if $self->setup;
203 L<FS::cust_pkg_discount>, L<FS::Record>, schema.html from the base documentation.