2 use base qw( FS::Record );
8 FS::discount - Object methods for discount records
14 $record = new FS::discount \%hash;
15 $record = new FS::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::discount object represents a discount definition. FS::discount inherits
28 from FS::Record. The following fields are currently supported:
56 =item setup - apply discount to setup fee (not just to recurring fee)
58 If the discount is based on a percentage, then the % will be applied to the
59 setup and recurring portions.
69 Creates a new discount. To add the discount to the database, see L<"insert">.
71 Note that this stores the hash reference, not a distinct copy of the hash it
72 points to. You can ask the object for a copy with the I<hash> method.
76 # the new method can be inherited from FS::Record, if a table method is defined
78 sub table { 'discount'; }
82 Adds this record to the database. If there is an error, returns the error,
83 otherwise returns false.
87 # the insert method can be inherited from FS::Record
91 Delete this record from the database.
95 # the delete method can be inherited from FS::Record
97 =item replace OLD_RECORD
99 Replaces the OLD_RECORD with this one in the database. If there is an error,
100 returns the error, otherwise returns false.
104 # the replace method can be inherited from FS::Record
108 Checks all fields to make sure this is a valid discount. If there is
109 an error, returns the error, otherwise returns false. Called by the insert
114 # the check method should currently be supplied - FS::Record contains some
115 # data checking routines
120 if ( $self->_type eq 'Select discount type' ) {
121 return 'Please select a discount type';
122 } elsif ( $self->amount > 0 ) {
123 $self->set('percent', '0');
124 } elsif ( $self->percent > 0 ) {
125 $self->set('amount', '0.00');
127 return "Discount amount or percentage must be > 0";
131 $self->ut_numbern('discountnum')
132 || $self->ut_foreign_keyn('classnum', 'discount_class', 'classnum')
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' ])
139 #|| $self->ut_enum('linked', [ '', 'Y' ])
141 return $error if $error;
143 #causes "months must be integers greater than 1" errors when you go back and
144 # try to edit an existing discount (because the months format as NN.000)
145 #not worth whatever reason it came in with "prepayment discounts rt#5318" for
146 # #discourage non-integer months for package discounts
147 # if ($self->discountnum) {
149 # "SELECT count(*) FROM part_pkg_discount WHERE part_pkg_discount.discountnum = ".
150 # $self->discountnum;
152 # my $count = $self->scalar_sql($sql);
153 # return "months must be integers greater than 1"
154 # if ( $count && ($self->ut_number('months') || $self->months < 2) );
160 =item description_short
164 Returns a text description incorporating the amount, percent and months fields.
166 description_short omits term information
170 sub description_short {
173 my $conf = new FS::Conf;
174 my $money_char = $conf->config('money_char') || '$';
178 $desc = $self->name . ': ';
180 $desc = 'Discount of ';
182 $desc .= $money_char. sprintf('%.2f/month', $self->amount)
183 if $self->amount > 0;
185 ( my $percent = $self->percent ) =~ s/\.0+$//;
186 $percent =~ s/(\.\d*[1-9])0+$/$1/;
187 $desc .= $percent. '%'
188 if $self->percent > 0;
195 my $desc = $self->description_short;
197 ( my $months = $self->months ) =~ s/\.0+$//;
198 $months =~ s/(\.\d*[1-9])0+$/$1/;
201 $desc .= " for 1 month";
203 $desc .= " for $months months";
207 $desc .= ', applies to setup' if $self->setup;
214 my $discount_class = $self->discount_class;
215 $discount_class ? $discount_class->classname : '(none)';
224 L<FS::cust_pkg_discount>, L<FS::Record>, schema.html from the base documentation.