discounts, RT#6679
[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   my $error = 
117     $self->ut_numbern('discountnum')
118     || $self->ut_textn('name')
119     || $self->ut_money('amount')
120     || $self->ut_float('percent') #actually decimal, but this will do
121     || $self->ut_floatn('months') #actually decimal, but this will do
122     || $self->ut_enum('disabled', [ '', 'Y' ])
123   ;
124   return $error if $error;
125
126   $self->SUPER::check;
127 }
128
129 =item description_short
130
131 =item description
132
133 Returns a text description incorporating the amount, percent and months fields.
134
135 description_short omits term information
136
137 =cut
138
139 sub description_short {
140   my $self = shift;
141
142   my $conf = new FS::Conf;
143   my $money_char = $conf->config('money_char') || '$';  
144
145   my $desc = '';
146   $desc .= $money_char. sprintf('%.2f/month ', $self->amount)
147     if $self->amount > 0;
148   $desc .= $self->percent. '% '
149     if $self->percent > 0;
150
151   $desc;
152 }
153
154 sub description {
155   my $self = shift;
156   my $desc = $self->description_short;
157   $desc .= ' for '. $self->months. ' months' if $self->months;
158   $desc;
159 }
160
161 =back
162
163 =head1 BUGS
164
165 =head1 SEE ALSO
166
167 L<FS::cust_pkg_discount>, L<FS::Record>, schema.html from the base documentation.
168
169 =cut
170
171 1;
172