Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / FS / FS / cust_pkg_discount.pm
1 package FS::cust_pkg_discount;
2 use base qw( FS::otaker_Mixin
3              FS::cust_main_Mixin
4              FS::pkg_discount_Mixin
5              FS::Record );
6
7 use strict;
8 use FS::Record qw( dbh ); # qsearch qsearchs dbh );
9 use FS::discount;
10
11 =head1 NAME
12
13 FS::cust_pkg_discount - Object methods for cust_pkg_discount records
14
15 =head1 SYNOPSIS
16
17   use FS::cust_pkg_discount;
18
19   $record = new FS::cust_pkg_discount \%hash;
20   $record = new FS::cust_pkg_discount { 'column' => 'value' };
21
22   $error = $record->insert;
23
24   $error = $new_record->replace($old_record);
25
26   $error = $record->delete;
27
28   $error = $record->check;
29
30 =head1 DESCRIPTION
31
32 An FS::cust_pkg_discount object represents the application of a discount to a
33 customer package.  FS::cust_pkg_discount inherits from FS::Record.  The
34 following fields are currently supported:
35
36 =over 4
37
38 =item pkgdiscountnum
39
40 primary key
41
42 =item pkgnum
43
44 Customer package (see L<FS::cust_pkg>)
45
46 =item discountnum
47
48 Discount (see L<FS::discount>)
49
50 =item months_used
51
52 months_used
53
54 =item end_date
55
56 end_date
57
58 =item usernum
59
60 order taker, see L<FS::access_user>
61
62
63 =back
64
65 =head1 METHODS
66
67 =over 4
68
69 =item new HASHREF
70
71 Creates a new discount application.  To add the record to the database, see
72  L<"insert">.
73
74 Note that this stores the hash reference, not a distinct copy of the hash it
75 points to.  You can ask the object for a copy with the I<hash> method.
76
77 =cut
78
79 # the new method can be inherited from FS::Record, if a table method is defined
80
81 sub table { 'cust_pkg_discount'; }
82
83 =item insert
84
85 Adds this record to the database.  If there is an error, returns the error,
86 otherwise returns false.
87
88 =item delete
89
90 Delete this record from the database.
91
92 =cut
93
94 # the delete method can be inherited from FS::Record
95
96 =item replace OLD_RECORD
97
98 Replaces the OLD_RECORD with this one in the database.  If there is an error,
99 returns the error, otherwise returns false.
100
101 =cut
102
103 # the replace method can be inherited from FS::Record
104
105 =item check
106
107 Checks all fields to make sure this is a valid discount applciation.  If there
108 is an error, returns the error, otherwise returns false.  Called by the insert
109 and replace methods.
110
111 =cut
112
113 # the check method should currently be supplied - FS::Record contains some
114 # data checking routines
115
116 sub check {
117   my $self = shift;
118
119   my $error = 
120     $self->ut_numbern('pkgdiscountnum')
121     || $self->ut_foreign_key('pkgnum', 'cust_pkg', 'pkgnum')
122     || $self->ut_foreign_key('discountnum', 'discount', 'discountnum' )
123     || $self->ut_sfloat('months_used') #actually decimal, but this will do
124     || $self->ut_numbern('end_date')
125     || $self->ut_alphan('otaker')
126     || $self->ut_numbern('usernum')
127     || $self->ut_enum('disabled', [ '', 'Y' ] )
128   ;
129   return $error if $error;
130
131   return "Discount does not apply to setup fees, and package has no recurring"
132     if ! $self->discount->setup && $self->cust_pkg->part_pkg->freq =~ /^0/;
133
134   $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
135
136   $self->SUPER::check;
137 }
138
139 =item cust_pkg
140
141 Returns the customer package (see L<FS::cust_pkg>).
142
143 =item discount
144
145 Returns the discount (see L<FS::discount>).
146
147 =item increment_months_used MONTHS
148
149 Increments months_used by the given parameter
150
151 =cut
152
153 sub increment_months_used {
154   my( $self, $used ) = @_;
155   #UPDATE cust_pkg_discount SET months_used = months_used + ?
156   #leaves no history, and billing is mutexed per-customer, so the dum way is ok
157   $self->months_used( $self->months_used + $used );
158   $self->replace();
159 }
160
161 =item decrement_months_used MONTHS
162
163 Decrement months_used by the given parameter
164
165 (Note: as in, extending the length of the discount.  Typically only used to
166 stack/extend a discount when the customer package has one active already.)
167
168 =cut
169
170 sub decrement_months_used {
171   my( $self, $recharged ) = @_;
172   #UPDATE cust_pkg_discount SET months_used = months_used - ?
173   #leaves no history, and billing is mutexed per-customer
174
175   #we're run from part_event/Action/referral_pkg_discount on behalf of a
176   # different customer, so we need to grab this customer's mutex.
177   #   incidentally, that's some inelegant encapsulation breaking shit, and a
178   #   great argument in favor of native-DB trigger history so we can trust
179   #   in normal ACID like the SQL above instead of this
180   $self->cust_pkg->cust_main->select_for_update;
181
182   $self->months_used( $self->months_used - $recharged );
183   $self->replace();
184 }
185
186 =item status
187
188 =cut
189
190 sub status {
191   my $self = shift;
192   my $discount = $self->discount;
193
194   if ( $self->disabled ne 'Y' 
195        and ( ! $discount->months || $self->months_used < $discount->months )
196              #XXX also end date
197      ) {
198     'active';
199   } else {
200     'expired';
201   }
202 }
203
204 # Used by FS::Upgrade to migrate to a new database.
205 sub _upgrade_data {  # class method
206   my ($class, %opts) = @_;
207   $class->_upgrade_otaker(%opts);
208 }
209
210 =back
211
212 =head1 BUGS
213
214 =head1 SEE ALSO
215
216 L<FS::discount>, L<FS::cust_pkg>, L<FS::Record>, schema.html from the base documentation.
217
218 =cut
219
220 1;
221