2 use base qw( FS::Record );
5 use FS::Record qw( qsearch qsearchs );
6 use FS::discount_class;
10 FS::discount - Object methods for discount records
16 $record = new FS::discount \%hash;
17 $record = new FS::discount { 'column' => 'value' };
19 $error = $record->insert;
21 $error = $new_record->replace($old_record);
23 $error = $record->delete;
25 $error = $record->check;
29 An FS::discount object represents a discount definition. FS::discount inherits
30 from FS::Record. The following fields are currently supported:
58 =item setup - apply discount to setup fee (not just to recurring fee)
60 If the discount is based on a percentage, then the % will be applied to the
61 setup and recurring portions.
71 Creates a new discount. To add the discount to the database, see L<"insert">.
73 Note that this stores the hash reference, not a distinct copy of the hash it
74 points to. You can ask the object for a copy with the I<hash> method.
78 # the new method can be inherited from FS::Record, if a table method is defined
80 sub table { 'discount'; }
84 Adds this record to the database. If there is an error, returns the error,
85 otherwise returns false.
89 # the insert method can be inherited from FS::Record
93 Delete this record from the database.
97 # the delete method can be inherited from FS::Record
99 =item replace OLD_RECORD
101 Replaces the OLD_RECORD with this one in the database. If there is an error,
102 returns the error, otherwise returns false.
106 # the replace method can be inherited from FS::Record
110 Checks all fields to make sure this is a valid discount. If there is
111 an error, returns the error, otherwise returns false. Called by the insert
116 # the check method should currently be supplied - FS::Record contains some
117 # data checking routines
122 if ( $self->_type eq 'Select discount type' ) {
123 return 'Please select a discount type';
124 } elsif ( $self->_type eq 'Amount' ) {
126 return 'Amount must be greater than 0' unless $self->amount > 0;
127 } elsif ( $self->_type eq 'Percentage' ) {
128 $self->amount('0.00');
129 return 'Percentage must be greater than 0' unless $self->percent > 0;
133 $self->ut_numbern('discountnum')
134 || $self->ut_foreign_keyn('classnum', 'discount_class', 'classnum')
135 || $self->ut_textn('name')
136 || $self->ut_money('amount')
137 || $self->ut_float('percent') #actually decimal, but this will do
138 || $self->ut_floatn('months') #actually decimal, but this will do
139 || $self->ut_enum('disabled', [ '', 'Y' ])
140 || $self->ut_enum('setup', [ '', 'Y' ])
141 #|| $self->ut_enum('linked', [ '', 'Y' ])
143 return $error if $error;
145 #causes "months must be integers greater than 1" errors when you go back and
146 # try to edit an existing discount (because the months format as NN.000)
147 #not worth whatever reason it came in with "prepayment discounts rt#5318" for
148 # #discourage non-integer months for package discounts
149 # if ($self->discountnum) {
151 # "SELECT count(*) FROM part_pkg_discount WHERE part_pkg_discount.discountnum = ".
152 # $self->discountnum;
154 # my $count = $self->scalar_sql($sql);
155 # return "months must be integers greater than 1"
156 # if ( $count && ($self->ut_number('months') || $self->months < 2) );
162 =item description_short
166 Returns a text description incorporating the amount, percent and months fields.
168 description_short omits term information
172 sub description_short {
175 my $conf = new FS::Conf;
176 my $money_char = $conf->config('money_char') || '$';
178 my $desc = $self->name ? $self->name.': ' : '';
179 $desc .= $money_char. sprintf('%.2f/month ', $self->amount)
180 if $self->amount > 0;
182 ( my $percent = $self->percent ) =~ s/\.0+$//;
183 $percent =~ s/(\.\d*[1-9])0+$/$1/;
184 $desc .= $percent. '% '
185 if $self->percent > 0;
192 my $desc = $self->description_short;
194 ( my $months = $self->months ) =~ s/\.0+$//;
195 $months =~ s/(\.\d*[1-9])0+$/$1/;
196 $desc .= " for $months months" if $months;
198 $desc .= ', applies to setup' if $self->setup;
205 my $discount_class = $self->discount_class;
206 $discount_class ? $discount_class->classname : '(none)';
211 qsearchs('discount_class', { 'classnum' => $self->classnum });
221 L<FS::cust_pkg_discount>, L<FS::Record>, schema.html from the base documentation.