summaryrefslogtreecommitdiff
path: root/FS/FS/pkg_discount_Mixin.pm
blob: c6fe00845214a45fedf70feab45e349f355b250f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package FS::pkg_discount_Mixin;

use strict;
use NEXT;
use FS::Record qw(dbh);

=head1 NAME

FS::pkg_discount_Mixin - mixin class for package-discount link objects.

=head1 DESCRIPTION

Implements some behavior that's common to cust_pkg_discount and 
quotation_pkg_discount objects. The only required field is "discountnum",
a foreign key to L<FS::discount>.

=head1 METHODS

=over 4

=item insert

Inserts the record. If the 'discountnum' field is -1, this will first create
a discount using the contents of the '_type', 'amount', 'percent', 'months',
and 'setup' field. The new discount will be disabled, since it's a one-off
discount.

=cut

sub insert {
  my $self = shift;
  my $oldAutoCommit = $FS::UID::AutoCommit;
  local $FS::UID::AutoCommit = 0;
  my $dbh = dbh;
  
  if ( $self->discountnum == -1 ) {
    my $discount = new FS::discount {
      '_type'    => $self->_type,
      'amount'   => $self->amount,
      'percent'  => $self->percent,
      'months'   => $self->months,
      'setup'    => $self->setup,
      #'linked'   => $self->linked,
      'disabled' => 'Y',
    };
    my $error = $discount->insert;
    if ( $error ) {
      $dbh->rollback if $oldAutoCommit;
      return $error; 
    } 
    $self->set('discountnum', $discount->discountnum);
  }

  my $error = $self->NEXT::insert;
  if ( $error ) {
    $dbh->rollback if $oldAutoCommit;
    return $error;
  }

  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
  '';
  
} 

=back

=cut

1;