2 % $cgi->param('error', $error);
3 % $cgi->redirect(popurl(3). 'edit/cust_pkg_discount.html?'. $cgi->query_string );
6 <& /elements/header-popup.html, "Discount applied" &>
7 <SCRIPT TYPE="text/javascript">
16 my $curuser = $FS::CurrentUser::CurrentUser;
17 my $can_discount = $curuser->access_right('Discount customer package');
18 my $can_waive_setup = $curuser->access_right('Waive setup fee');
20 #this search is really for security wrt agent virt...
21 #maybe move it to the cust_pkg_discount->insert call?
22 my $cust_pkg = qsearchs({
23 #'select' => 'cust_pkg.*',
24 'table' => 'cust_pkg',
25 'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
26 'hashref' => { 'pkgnum' => scalar($cgi->param('pkgnum')), },
27 'extra_sql' => ' AND '. $curuser->agentnums_sql,
29 die 'unknown pkgnum' unless $cust_pkg;
32 my %discountnum = (setup => '', recur => '');
33 if ( $cgi->param('setup_discountnum') == -2 ) {
35 die "access denied" unless $can_waive_setup; # UI protects against this
36 # waive setup fee (not really a discount but treated as one in the UI)
37 if ( !$cust_pkg->get('setup') and !$cust_pkg->waive_setup ) {
38 $cust_pkg->set('waive_setup' => 'Y');
39 $error = $cust_pkg->replace;
43 if ( $cgi->param('setup_discountnum') =~ /^(-?\d+)$/ ) {
44 $discountnum{setup} = $1;
46 if ( $cust_pkg->waive_setup ) {
47 $cust_pkg->set('waive_setup', '');
48 $error = $cust_pkg->replace;
52 if ( $cgi->param('recur_discountnum') =~ /^(-?\d+)$/ ) {
54 $discountnum{recur} = $1;
58 my @active_discounts = $cust_pkg->cust_pkg_discount_active;
60 foreach my $setuprecur (qw(setup recur)) {
62 if ( $cust_pkg->get('setup') and $setuprecur eq 'setup' ) {
63 # no point allowing setup discounts to be edited for a previously setup
68 my ($active) = grep { $_->setuprecur eq $setuprecur } @active_discounts;
71 if ( $active->discount ne $discountnum{$setuprecur} ) {
72 $active->set('disabled' => 'Y');
73 $error ||= $active->replace;
76 # it's the same discountnum; don't touch it
81 if ( $discountnum{$setuprecur} ) {
82 die "access_denied" unless $can_discount;
83 my $cust_pkg_discount = FS::cust_pkg_discount->new({
84 'pkgnum' => $cust_pkg->pkgnum,
85 'discountnum' => $discountnum{$setuprecur},
86 'setuprecur' => $setuprecur,
88 'end_date' => '', #XXX
89 #for the create a new discount case
90 '_type' => scalar($cgi->param($setuprecur.'_discountnum__type')),
91 'amount' => scalar($cgi->param($setuprecur.'_discountnum_amount')),
92 'percent' => scalar($cgi->param($setuprecur.'_discountnum_percent')),
94 if ( $setuprecur eq 'setup' ) {
95 $cust_pkg_discount->set('setup' => 'Y');
96 $cust_pkg_discount->set('months' => 1);
98 if ( $cgi->param($setuprecur.'_discountnum_months') =~ /^(\w+)$/ ) {
99 $cust_pkg_discount->set('months' => $1);
103 $error ||= $cust_pkg_discount->insert;
106 } # foreach $setuprecur