1 package FS::cust_pkg_discount;
4 use base qw( FS::otaker_Mixin FS::cust_main_Mixin FS::Record );
5 use FS::Record qw( dbh qsearchs ); # qsearch );
11 FS::cust_pkg_discount - Object methods for cust_pkg_discount records
15 use FS::cust_pkg_discount;
17 $record = new FS::cust_pkg_discount \%hash;
18 $record = new FS::cust_pkg_discount { 'column' => 'value' };
20 $error = $record->insert;
22 $error = $new_record->replace($old_record);
24 $error = $record->delete;
26 $error = $record->check;
30 An FS::cust_pkg_discount object represents the application of a discount to a
31 customer package. FS::cust_pkg_discount inherits from FS::Record. The
32 following fields are currently supported:
42 Customer package (see L<FS::cust_pkg>)
46 Discount (see L<FS::discount>)
58 order taker, see L<FS::access_user>
69 Creates a new discount application. To add the record to the database, see
72 Note that this stores the hash reference, not a distinct copy of the hash it
73 points to. You can ask the object for a copy with the I<hash> method.
77 # the new method can be inherited from FS::Record, if a table method is defined
79 sub table { 'cust_pkg_discount'; }
83 Adds this record to the database. If there is an error, returns the error,
84 otherwise returns false.
89 #my( $self, %options ) = @_;
92 local $SIG{HUP} = 'IGNORE';
93 local $SIG{INT} = 'IGNORE';
94 local $SIG{QUIT} = 'IGNORE';
95 local $SIG{TERM} = 'IGNORE';
96 local $SIG{TSTP} = 'IGNORE';
97 local $SIG{PIPE} = 'IGNORE';
99 my $oldAutoCommit = $FS::UID::AutoCommit;
100 local $FS::UID::AutoCommit = 0;
103 if ( $self->discountnum == -1 ) {
104 my $discount = new FS::discount {
105 '_type' => $self->_type,
106 'amount' => $self->amount,
107 'percent' => $self->percent,
108 'months' => $self->months,
109 'setup' => $self->setup,
110 #'linked' => $self->linked,
113 my $error = $discount->insert;
115 $dbh->rollback if $oldAutoCommit;
118 $self->discountnum($discount->discountnum);
121 my $error = $self->SUPER::insert; #(@_); #(%options);
123 $dbh->rollback if $oldAutoCommit;
127 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
134 Delete this record from the database.
138 # the delete method can be inherited from FS::Record
140 =item replace OLD_RECORD
142 Replaces the OLD_RECORD with this one in the database. If there is an error,
143 returns the error, otherwise returns false.
147 # the replace method can be inherited from FS::Record
151 Checks all fields to make sure this is a valid discount applciation. If there
152 is an error, returns the error, otherwise returns false. Called by the insert
157 # the check method should currently be supplied - FS::Record contains some
158 # data checking routines
164 $self->ut_numbern('pkgdiscountnum')
165 || $self->ut_foreign_key('pkgnum', 'cust_pkg', 'pkgnum')
166 || $self->ut_foreign_key('discountnum', 'discount', 'discountnum' )
167 || $self->ut_sfloat('months_used') #actually decimal, but this will do
168 || $self->ut_numbern('end_date')
169 || $self->ut_alphan('otaker')
170 || $self->ut_numbern('usernum')
171 || $self->ut_enum('disabled', [ '', 'Y' ] )
173 return $error if $error;
175 return "Discount does not apply to setup fees, and package has no recurring"
176 if ! $self->discount->setup && $self->cust_pkg->part_pkg->freq =~ /^0/;
178 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
185 Returns the customer package (see L<FS::cust_pkg>).
191 qsearchs('cust_pkg', { 'pkgnum' => $self->pkgnum } );
196 Returns the discount (see L<FS::discount>).
202 qsearchs('discount', { 'discountnum' => $self->discountnum } );
205 =item increment_months_used MONTHS
207 Increments months_used by the given parameter
211 sub increment_months_used {
212 my( $self, $used ) = @_;
213 #UPDATE cust_pkg_discount SET months_used = months_used + ?
214 #leaves no history, and billing is mutexed per-customer, so the dum way is ok
215 $self->months_used( $self->months_used + $used );
219 =item decrement_months_used MONTHS
221 Decrement months_used by the given parameter
223 (Note: as in, extending the length of the discount. Typically only used to
224 stack/extend a discount when the customer package has one active already.)
228 sub decrement_months_used {
229 my( $self, $recharged ) = @_;
230 #UPDATE cust_pkg_discount SET months_used = months_used - ?
231 #leaves no history, and billing is mutexed per-customer
233 #we're run from part_event/Action/referral_pkg_discount on behalf of a
234 # different customer, so we need to grab this customer's mutex.
235 # incidentally, that's some inelegant encapsulation breaking shit, and a
236 # great argument in favor of native-DB trigger history so we can trust
237 # in normal ACID like the SQL above instead of this
238 $self->cust_pkg->cust_main->select_for_update;
240 $self->months_used( $self->months_used - $recharged );
250 my $discount = $self->discount;
252 if ( $self->disabled ne 'Y'
253 and ( ! $discount->months || $self->months_used < $discount->months )
262 # Used by FS::Upgrade to migrate to a new database.
263 sub _upgrade_data { # class method
264 my ($class, %opts) = @_;
265 $class->_upgrade_otaker(%opts);
274 L<FS::discount>, L<FS::cust_pkg>, L<FS::Record>, schema.html from the base documentation.