fix discount display after mysql schema change fix
[freeside.git] / FS / FS / discount.pm
1 package FS::discount;
2
3 use strict;
4 use base qw( FS::Record );
5 use FS::Record qw( qsearch qsearchs );
6
7 =head1 NAME
8
9 FS::discount - Object methods for discount records
10
11 =head1 SYNOPSIS
12
13   use FS::discount;
14
15   $record = new FS::discount \%hash;
16   $record = new FS::discount { 'column' => 'value' };
17
18   $error = $record->insert;
19
20   $error = $new_record->replace($old_record);
21
22   $error = $record->delete;
23
24   $error = $record->check;
25
26 =head1 DESCRIPTION
27
28 An FS::discount object represents a discount definition.  FS::discount inherits
29 from FS::Record.  The following fields are currently supported:
30
31 =over 4
32
33 =item discountnum
34
35 primary key
36
37 =item name
38
39 name
40
41 =item amount
42
43 amount
44
45 =item percent
46
47 percent
48
49 =item months
50
51 months
52
53 =item disabled
54
55 disabled
56
57 =item setup - apply discount to setup fee (not just to recurring fee)
58
59 If the discount is based on a percentage, then the % will be applied to the
60 setup and recurring portions. 
61
62 =back
63
64 =head1 METHODS
65
66 =over 4
67
68 =item new HASHREF
69
70 Creates a new discount.  To add the discount to the database, see L<"insert">.
71
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.
74
75 =cut
76
77 # the new method can be inherited from FS::Record, if a table method is defined
78
79 sub table { 'discount'; }
80
81 =item insert
82
83 Adds this record to the database.  If there is an error, returns the error,
84 otherwise returns false.
85
86 =cut
87
88 # the insert method can be inherited from FS::Record
89
90 =item delete
91
92 Delete this record from the database.
93
94 =cut
95
96 # the delete method can be inherited from FS::Record
97
98 =item replace OLD_RECORD
99
100 Replaces the OLD_RECORD with this one in the database.  If there is an error,
101 returns the error, otherwise returns false.
102
103 =cut
104
105 # the replace method can be inherited from FS::Record
106
107 =item check
108
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
111 and replace methods.
112
113 =cut
114
115 # the check method should currently be supplied - FS::Record contains some
116 # data checking routines
117
118 sub check {
119   my $self = shift;
120
121   if ( $self->_type eq 'Select discount type' ) {
122     return 'Please select a discount type';
123   } elsif ( $self->_type eq 'Amount' ) {
124     $self->percent('0');
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;
129   }
130
131   my $error = 
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' ])
139   ;
140   return $error if $error;
141
142   #discourage non-integer months for package discounts
143   if ($self->discountnum) {
144     my $sql =
145       "SELECT count(*) FROM part_pkg_discount WHERE part_pkg_discount.discountnum = ".
146       $self->discountnum;
147
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) );
151   }
152     
153   $self->SUPER::check;
154 }
155
156 =item description_short
157
158 =item description
159
160 Returns a text description incorporating the amount, percent and months fields.
161
162 description_short omits term information
163
164 =cut
165
166 sub description_short {
167   my $self = shift;
168
169   my $conf = new FS::Conf;
170   my $money_char = $conf->config('money_char') || '$';  
171
172   my $desc = $self->name ? $self->name.': ' : '';
173   $desc .= $money_char. sprintf('%.2f/month ', $self->amount)
174     if $self->amount > 0;
175
176   ( my $percent = $self->percent ) =~ s/\.0+$//;
177   $percent =~ s/(\.\d*[1-9])0+$/$1/;
178   $desc .= $percent. '% '
179     if $self->percent > 0;
180
181   $desc;
182 }
183
184 sub description {
185   my $self = shift;
186   my $desc = $self->description_short;
187
188   ( my $months = $self->months ) =~ s/\.0+$//;
189   $months =~ s/(\.\d*[1-9])0+$/$1/;
190   $desc .= " for $months months" if $months;
191
192   $desc .= ', applies to setup' if $self->setup;
193
194   $desc;
195 }
196
197 =back
198
199 =head1 BUGS
200
201 =head1 SEE ALSO
202
203 L<FS::cust_pkg_discount>, L<FS::Record>, schema.html from the base documentation.
204
205 =cut
206
207 1;
208