summaryrefslogtreecommitdiff
path: root/httemplate/edit/process/cust_pkg_discount.html
blob: 9635463638d239daf3fad551292fd2439ad75ed7 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
% if ($error) {
%   $cgi->param('error', $error);
%   $cgi->redirect(popurl(3). 'edit/cust_pkg_discount.html?'. $cgi->query_string );
% } else {

    <% header("Discount applied") %>
      <SCRIPT TYPE="text/javascript">
        topreload();
      </SCRIPT>
    </BODY>
    </HTML>

% }
<%init>

my $curuser = $FS::CurrentUser::CurrentUser;
my $can_discount = $curuser->access_right('Discount customer package');
my $can_waive_setup = $curuser->access_right('Waive setup fee');

#this search is really for security wrt agent virt...
#maybe move it to the cust_pkg_discount->insert call?
my $cust_pkg = qsearchs({
  #'select'    => 'cust_pkg.*',
  'table'     => 'cust_pkg',
  'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
  'hashref'   => { 'pkgnum' => scalar($cgi->param('pkgnum')), },
  'extra_sql' => ' AND '. $curuser->agentnums_sql,
});
die 'unknown pkgnum' unless $cust_pkg;

my $error;
my %discountnum = (setup => '', recur => '');
if ( $cgi->param('setup_discountnum') == -2 ) {

  die "access denied" unless $can_waive_setup; # UI protects against this
  # waive setup fee (not really a discount but treated as one in the UI)
  if ( !$cust_pkg->get('setup') and !$cust_pkg->waive_setup ) {
    $cust_pkg->set('waive_setup' => 'Y');
    $error = $cust_pkg->replace;
  }

} else {
  if ( $cgi->param('setup_discountnum') =~ /^(-?\d+)$/ ) {
    $discountnum{setup} = $1;
  }
  if ( $cust_pkg->waive_setup ) {
    $cust_pkg->set('waive_setup', '');
    $error = $cust_pkg->replace;
  }
}

if ( $cgi->param('recur_discountnum') =~ /^(-?\d+)$/ ) {

  $discountnum{recur} = $1;

}

my @active_discounts = $cust_pkg->cust_pkg_discount_active;

foreach my $setuprecur (qw(setup recur)) {

  if ( $cust_pkg->get('setup') and $setuprecur eq 'setup' ) {
    # no point allowing setup discounts to be edited for a previously setup
    # package
    next;
  }

  my ($active) = grep { $_->setuprecur eq $setuprecur } @active_discounts;

  if ( $active ) {
    if ( $active->discount ne $discountnum{$setuprecur} ) {
      $active->set('disabled' => 'Y');
      $error ||= $active->replace;
      undef $active;
    } else {
      # it's the same discountnum; don't touch it
      next;
    }
  }

  if ( $discountnum{$setuprecur} ) {
    die "access_denied" unless $can_discount;
    my $cust_pkg_discount = FS::cust_pkg_discount->new({
      'pkgnum'      => $cust_pkg->pkgnum,
      'discountnum' => $discountnum{$setuprecur},
      'setuprecur'  => $setuprecur,
      'months_used' => 0,
      'end_date'    => '', #XXX
      #for the create a new discount case
      '_type'       => scalar($cgi->param($setuprecur.'_discountnum__type')),
      'amount'      => scalar($cgi->param($setuprecur.'_discountnum_amount')),
      'percent'     => scalar($cgi->param($setuprecur.'_discountnum_percent')),
    });
    if ( $setuprecur eq 'setup' ) {
      $cust_pkg_discount->set('setup' => 'Y');
      $cust_pkg_discount->set('months' => 1);
    } else {
      if ( $cgi->param($setuprecur.'_discountnum_months') =~ /^(\w+)$/ ) {
        $cust_pkg_discount->set('months' => $1);
      }
    }

    $error ||= $cust_pkg_discount->insert;

  }
} # foreach $setuprecur

</%init>