1 package FS::cust_pkg_discount;
4 use base qw( FS::otaker_Mixin
8 use FS::Record qw( dbh qsearchs ); # qsearch );
14 FS::cust_pkg_discount - Object methods for cust_pkg_discount records
18 use FS::cust_pkg_discount;
20 $record = new FS::cust_pkg_discount \%hash;
21 $record = new FS::cust_pkg_discount { 'column' => 'value' };
23 $error = $record->insert;
25 $error = $new_record->replace($old_record);
27 $error = $record->delete;
29 $error = $record->check;
33 An FS::cust_pkg_discount object represents the application of a discount to a
34 customer package. FS::cust_pkg_discount inherits from FS::Record. The
35 following fields are currently supported:
45 Customer package (see L<FS::cust_pkg>)
49 Discount (see L<FS::discount>)
61 order taker, see L<FS::access_user>
72 Creates a new discount application. To add the record to the database, see
75 Note that this stores the hash reference, not a distinct copy of the hash it
76 points to. You can ask the object for a copy with the I<hash> method.
80 # the new method can be inherited from FS::Record, if a table method is defined
82 sub table { 'cust_pkg_discount'; }
86 Adds this record to the database. If there is an error, returns the error,
87 otherwise returns false.
91 Delete this record from the database.
95 # the delete method can be inherited from FS::Record
97 =item replace OLD_RECORD
99 Replaces the OLD_RECORD with this one in the database. If there is an error,
100 returns the error, otherwise returns false.
104 # the replace method can be inherited from FS::Record
108 Checks all fields to make sure this is a valid discount applciation. If there
109 is an error, returns the error, otherwise returns false. Called by the insert
114 # the check method should currently be supplied - FS::Record contains some
115 # data checking routines
121 $self->ut_numbern('pkgdiscountnum')
122 || $self->ut_foreign_key('pkgnum', 'cust_pkg', 'pkgnum')
123 || $self->ut_foreign_key('discountnum', 'discount', 'discountnum' )
124 || $self->ut_sfloat('months_used') #actually decimal, but this will do
125 || $self->ut_numbern('end_date')
126 || $self->ut_alphan('otaker')
127 || $self->ut_numbern('usernum')
128 || $self->ut_enum('disabled', [ '', 'Y' ] )
130 return $error if $error;
132 return "Discount does not apply to setup fees, and package has no recurring"
133 if ! $self->discount->setup && $self->cust_pkg->part_pkg->freq =~ /^0/;
135 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
142 Returns the customer package (see L<FS::cust_pkg>).
148 qsearchs('cust_pkg', { 'pkgnum' => $self->pkgnum } );
153 Returns the discount (see L<FS::discount>).
159 qsearchs('discount', { 'discountnum' => $self->discountnum } );
162 =item increment_months_used MONTHS
164 Increments months_used by the given parameter
168 sub increment_months_used {
169 my( $self, $used ) = @_;
170 #UPDATE cust_pkg_discount SET months_used = months_used + ?
171 #leaves no history, and billing is mutexed per-customer, so the dum way is ok
172 $self->months_used( $self->months_used + $used );
176 =item decrement_months_used MONTHS
178 Decrement months_used by the given parameter
180 (Note: as in, extending the length of the discount. Typically only used to
181 stack/extend a discount when the customer package has one active already.)
185 sub decrement_months_used {
186 my( $self, $recharged ) = @_;
187 #UPDATE cust_pkg_discount SET months_used = months_used - ?
188 #leaves no history, and billing is mutexed per-customer
190 #we're run from part_event/Action/referral_pkg_discount on behalf of a
191 # different customer, so we need to grab this customer's mutex.
192 # incidentally, that's some inelegant encapsulation breaking shit, and a
193 # great argument in favor of native-DB trigger history so we can trust
194 # in normal ACID like the SQL above instead of this
195 $self->cust_pkg->cust_main->select_for_update;
197 $self->months_used( $self->months_used - $recharged );
207 my $discount = $self->discount;
209 if ( $self->disabled ne 'Y'
210 and ( ! $discount->months || $self->months_used < $discount->months )
219 # Used by FS::Upgrade to migrate to a new database.
220 sub _upgrade_data { # class method
221 my ($class, %opts) = @_;
222 $class->_upgrade_otaker(%opts);
231 L<FS::discount>, L<FS::cust_pkg>, L<FS::Record>, schema.html from the base documentation.