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,
111 my $error = $discount->insert;
113 $dbh->rollback if $oldAutoCommit;
116 $self->discountnum($discount->discountnum);
119 my $error = $self->SUPER::insert; #(@_); #(%options);
121 $dbh->rollback if $oldAutoCommit;
125 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
132 Delete this record from the database.
136 # the delete method can be inherited from FS::Record
138 =item replace OLD_RECORD
140 Replaces the OLD_RECORD with this one in the database. If there is an error,
141 returns the error, otherwise returns false.
145 # the replace method can be inherited from FS::Record
149 Checks all fields to make sure this is a valid discount applciation. If there
150 is an error, returns the error, otherwise returns false. Called by the insert
155 # the check method should currently be supplied - FS::Record contains some
156 # data checking routines
162 $self->ut_numbern('pkgdiscountnum')
163 || $self->ut_foreign_key('pkgnum', 'cust_pkg', 'pkgnum')
164 || $self->ut_foreign_key('discountnum', 'discount', 'discountnum' )
165 || $self->ut_float('months_used') #actually decimal, but this will do
166 || $self->ut_numbern('end_date')
167 || $self->ut_alphan('otaker')
168 || $self->ut_numbern('usernum')
169 || $self->ut_enum('disabled', [ '', 'Y' ] )
171 return $error if $error;
173 $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->usernum;
180 Returns the customer package (see L<FS::cust_pkg>).
186 qsearchs('cust_pkg', { 'pkgnum' => $self->pkgnum } );
191 Returns the discount (see L<FS::discount>).
197 qsearchs('discount', { 'discountnum' => $self->discountnum } );
200 =item increment_months_used
202 Increments months_used by the given parameter
206 sub increment_months_used {
207 my( $self, $used ) = @_;
208 #UPDATE cust_pkg_discount SET months_used = months_used + ?
209 #leaves no history, and billing is mutexed per-customer, so the dum way is ok
210 $self->months_used( $self->months_used + $used );
220 my $discount = $self->discount;
222 if ( $self->disabled ne 'Y'
223 and ( ! $discount->months || $self->months_used < $discount->months )
232 # Used by FS::Upgrade to migrate to a new database.
233 sub _upgrade_data { # class method
234 my ($class, %opts) = @_;
235 $class->_upgrade_otaker(%opts);
244 L<FS::discount>, L<FS::cust_pkg>, L<FS::Record>, schema.html from the base documentation.