This commit was generated by cvs2svn to compensate for changes in r11022,
[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 =back
58
59 =head1 METHODS
60
61 =over 4
62
63 =item new HASHREF
64
65 Creates a new discount.  To add the discount to the database, see L<"insert">.
66
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.
69
70 =cut
71
72 # the new method can be inherited from FS::Record, if a table method is defined
73
74 sub table { 'discount'; }
75
76 =item insert
77
78 Adds this record to the database.  If there is an error, returns the error,
79 otherwise returns false.
80
81 =cut
82
83 # the insert method can be inherited from FS::Record
84
85 =item delete
86
87 Delete this record from the database.
88
89 =cut
90
91 # the delete method can be inherited from FS::Record
92
93 =item replace OLD_RECORD
94
95 Replaces the OLD_RECORD with this one in the database.  If there is an error,
96 returns the error, otherwise returns false.
97
98 =cut
99
100 # the replace method can be inherited from FS::Record
101
102 =item check
103
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
106 and replace methods.
107
108 =cut
109
110 # the check method should currently be supplied - FS::Record contains some
111 # data checking routines
112
113 sub check {
114   my $self = shift;
115
116   if ( $self->_type eq 'Select discount type' ) {
117     return 'Please select a discount type';
118   } elsif ( $self->_type eq 'Amount' ) {
119     $self->percent('0');
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;
124   }
125
126   my $error = 
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' ])
133   ;
134   return $error if $error;
135
136   #discourage non-integer months for package discounts
137   if ($self->discountnum) {
138     my $sql =
139       "SELECT count(*) FROM part_pkg_discount WHERE part_pkg_discount.discountnum = ".
140       $self->discountnum;
141
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) );
145   }
146     
147   $self->SUPER::check;
148 }
149
150 =item description_short
151
152 =item description
153
154 Returns a text description incorporating the amount, percent and months fields.
155
156 description_short omits term information
157
158 =cut
159
160 sub description_short {
161   my $self = shift;
162
163   my $conf = new FS::Conf;
164   my $money_char = $conf->config('money_char') || '$';  
165
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;
171
172   $desc;
173 }
174
175 sub description {
176   my $self = shift;
177   my $desc = $self->description_short;
178   $desc .= ' for '. $self->months. ' months' if $self->months;
179   $desc;
180 }
181
182 =back
183
184 =head1 BUGS
185
186 =head1 SEE ALSO
187
188 L<FS::cust_pkg_discount>, L<FS::Record>, schema.html from the base documentation.
189
190 =cut
191
192 1;
193