1 package FS::cust_pkg_discount;
2 use base qw( FS::otaker_Mixin
8 use FS::Record qw( dbh ); # qsearch qsearchs dbh );
13 FS::cust_pkg_discount - Object methods for cust_pkg_discount records
17 use FS::cust_pkg_discount;
19 $record = new FS::cust_pkg_discount \%hash;
20 $record = new FS::cust_pkg_discount { 'column' => 'value' };
22 $error = $record->insert;
24 $error = $new_record->replace($old_record);
26 $error = $record->delete;
28 $error = $record->check;
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:
44 Customer package (see L<FS::cust_pkg>)
48 Discount (see L<FS::discount>)
60 order taker, see L<FS::access_user>
71 Creates a new discount application. To add the record to the database, see
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.
79 # the new method can be inherited from FS::Record, if a table method is defined
81 sub table { 'cust_pkg_discount'; }
85 Adds this record to the database. If there is an error, returns the error,
86 otherwise returns false.
90 Delete this record from the database.
94 # the delete method can be inherited from FS::Record
96 =item replace OLD_RECORD
98 Replaces the OLD_RECORD with this one in the database. If there is an error,
99 returns the error, otherwise returns false.
103 # the replace method can be inherited from FS::Record
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
113 # the check method should currently be supplied - FS::Record contains some
114 # data checking routines
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' ] )
129 return $error if $error;
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/;
134 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
141 Returns the customer package (see L<FS::cust_pkg>).
145 Returns the discount (see L<FS::discount>).
147 =item increment_months_used MONTHS
149 Increments months_used by the given parameter
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 );
161 =item decrement_months_used MONTHS
163 Decrement months_used by the given parameter
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.)
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
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;
182 $self->months_used( $self->months_used - $recharged );
192 my $discount = $self->discount;
194 if ( $self->disabled ne 'Y'
195 and ( ! $discount->months || $self->months_used < $discount->months )
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);
216 L<FS::discount>, L<FS::cust_pkg>, L<FS::Record>, schema.html from the base documentation.