summaryrefslogtreecommitdiff
path: root/httemplate/edit
diff options
context:
space:
mode:
authorMark Wells <mark@freeside.biz>2015-10-16 15:32:32 -0700
committerMark Wells <mark@freeside.biz>2015-10-16 15:47:47 -0700
commitf641486e28214ad1eca18c47d2252701b83614f1 (patch)
treebc52ae1fedb9e088d5690940480bddd0ea5c8b6c /httemplate/edit
parentb6f16a22bd93ec66ffbb1da30e63f7e950b3b819 (diff)
separate setup and recur discounts, #14092
Diffstat (limited to 'httemplate/edit')
-rwxr-xr-xhttemplate/edit/cust_pkg_discount.html66
-rw-r--r--httemplate/edit/process/cust_pkg_discount.html96
-rw-r--r--httemplate/edit/process/quick-cust_pkg.cgi29
3 files changed, 132 insertions, 59 deletions
diff --git a/httemplate/edit/cust_pkg_discount.html b/httemplate/edit/cust_pkg_discount.html
index 0bb84b8f2..e1e3daede 100755
--- a/httemplate/edit/cust_pkg_discount.html
+++ b/httemplate/edit/cust_pkg_discount.html
@@ -1,18 +1,5 @@
-<% include('/elements/header-popup.html', "Discount Package") %>
-
-<SCRIPT TYPE="text/javascript">
-
- function enable_discount_pkg () {
- if ( document.DiscountPkgForm.discountnum.selectedIndex > 0 ) {
- document.DiscountPkgForm.submit.disabled = false;
- } else {
- document.DiscountPkgForm.submit.disabled = true;
- }
- }
-
-</SCRIPT>
-
-<% include('/elements/error.html') %>
+<& /elements/header-popup.html, "Discount Package" &>
+<& /elements/error.html &>
<FORM NAME="DiscountPkgForm" ACTION="<% $p %>edit/process/cust_pkg_discount.html" METHOD=POST>
<INPUT TYPE="hidden" NAME="pkgnum" VALUE="<% $pkgnum %>">
@@ -26,17 +13,17 @@
</TD>
</TR>
-<% include('/elements/tr-select-discount.html',
- 'empty_label' => ( $pkgdiscountnum ? '' : 'Select discount' ),
- 'onchange' => 'enable_discount_pkg()',
- 'cgi' => $cgi,
- )
-%>
-
+<& /elements/tr-select-pkg-discount.html,
+ curr_value_setup => $setup_discountnum,
+ curr_value_recur => $recur_discountnum,
+ disable_setup => $disable_setup,
+ disable_recur => $disable_recur,
+&>
+
</TABLE>
<BR>
-<INPUT NAME="submit" TYPE="submit" VALUE="Discount package" <% $pkgdiscountnum ? '' : 'DISABLED' %>>
+<INPUT NAME="submit" TYPE="submit" VALUE="Discount package">
</FORM>
</BODY>
@@ -44,14 +31,13 @@
<%init>
-#some false laziness w/misc/change_pkg.cgi
-
my $conf = new FS::Conf;
my $curuser = $FS::CurrentUser::CurrentUser;
die "access denied"
- unless $curuser->access_right('Discount customer package');
+ unless $curuser->access_right([ 'Discount customer package',
+ 'Waive setup fee']);
my $pkgnum = scalar($cgi->param('pkgnum'));
$pkgnum =~ /^(\d+)$/ or die "illegal pkgnum $pkgnum";
@@ -67,10 +53,30 @@ my $cust_pkg =
'extra_sql' => ' AND '. $curuser->agentnums_sql,
}) or die "unknown pkgnum $pkgnum";
-#my $cust_main = $cust_pkg->cust_main
-# or die "can't get cust_main record for custnum ". $cust_pkg->custnum.
-# " ( pkgnum ". cust_pkg->pkgnum. ")";
-
my $part_pkg = $cust_pkg->part_pkg;
+my @discounts = $cust_pkg->cust_pkg_discount_active;
+my ($setup_discountnum, $recur_discountnum);
+foreach (@discounts) {
+ if ( $_->setuprecur eq 'setup') {
+ die "multiple setup discounts on pkg#$pkgnum" if $setup_discountnum;
+ $setup_discountnum = $_->discountnum;
+ } elsif ( $_->setuprecur eq 'recur' ) {
+ die "multiple setup discounts on pkg#$pkgnum" if $recur_discountnum;
+ $recur_discountnum = $_->discountnum;
+ }
+}
+if ( $cust_pkg->waive_setup ) {
+ $setup_discountnum = -2;
+}
+
+my $disable_setup = 1;
+if ( !$cust_pkg->get('setup') and $cust_pkg->base_setup > 0 ) {
+ $disable_setup = 0;
+}
+my $disable_recur = 1;
+if ( $cust_pkg->base_recur > 0 ) {
+ $disable_recur = 0;
+}
+
</%init>
diff --git a/httemplate/edit/process/cust_pkg_discount.html b/httemplate/edit/process/cust_pkg_discount.html
index 4a71f6975..143611ef9 100644
--- a/httemplate/edit/process/cust_pkg_discount.html
+++ b/httemplate/edit/process/cust_pkg_discount.html
@@ -14,9 +14,8 @@
<%init>
my $curuser = $FS::CurrentUser::CurrentUser;
-
-die "access denied"
- unless $curuser->access_right('Discount customer package');
+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?
@@ -29,20 +28,81 @@ my $cust_pkg = qsearchs({
});
die 'unknown pkgnum' unless $cust_pkg;
-my $cust_pkg_discount = new FS::cust_pkg_discount {
- 'pkgnum' => $cust_pkg->pkgnum,
- 'discountnum' => scalar($cgi->param('discountnum')),
- 'months_used' => 0,
- 'end_date' => '', #XXX
- #for the create a new discount case
- '_type' => scalar($cgi->param('discountnum__type')),
- 'amount' => scalar($cgi->param('discountnum_amount')),
- 'percent' => scalar($cgi->param('discountnum_percent')),
- 'months' => scalar($cgi->param('discountnum_months')),
- 'setup' => scalar($cgi->param('discountnum_setup')),
- #'linked' => scalar($cgi->param('discountnum_linked')),
- #'disabled' => $self->discountnum_disabled,
-};
-my $error = $cust_pkg_discount->insert;
+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>
diff --git a/httemplate/edit/process/quick-cust_pkg.cgi b/httemplate/edit/process/quick-cust_pkg.cgi
index f1d8c2696..60352154a 100644
--- a/httemplate/edit/process/quick-cust_pkg.cgi
+++ b/httemplate/edit/process/quick-cust_pkg.cgi
@@ -79,9 +79,6 @@ my $contactnum = $1;
$cgi->param('locationnum') =~ /^(\-?\d*)$/
or die 'illegal locationnum '. $cgi->param('locationnum');
my $locationnum = $1;
-$cgi->param('discountnum') =~ /^(\-?\d*)$/
- or die 'illegal discountnum '. $cgi->param('discountnum');
-my $discountnum = $1;
# for going right to a provision service after ordering a package
my( $svcpart, $part_svc ) = ( '', '' );
@@ -114,19 +111,29 @@ my %hash = (
'refnum' => $refnum,
'contactnum' => $contactnum,
'locationnum' => $locationnum,
- 'discountnum' => $discountnum,
- #for the create a new discount case
- 'discountnum__type' => scalar($cgi->param('discountnum__type')),
- 'discountnum_amount' => scalar($cgi->param('discountnum_amount')),
- 'discountnum_percent' => scalar($cgi->param('discountnum_percent')),
- 'discountnum_months' => scalar($cgi->param('discountnum_months')),
- 'discountnum_setup' => scalar($cgi->param('discountnum_setup')),
'contract_end' => ( scalar($cgi->param('contract_end'))
? parse_datetime($cgi->param('contract_end'))
: ''
),
- 'waive_setup' => ( $cgi->param('waive_setup') eq 'Y' ? 'Y' : '' ),
);
+
+if ( $cgi->param('setup_discountnum') =~ /^(-?\d+)$/ ) {
+ if ( $1 == -2 ) {
+ $hash{waive_setup} = 'Y';
+ } else {
+ $hash{setup_discountnum} = $1;
+ $hash{setup_discountnum_amount} = $cgi->param('setup_discountnum_amount');
+ $hash{setup_discountnum_percent} = $cgi->param('setup_discountnum_percent');
+ }
+}
+
+if ( $cgi->param('recur_discountnum') =~ /^(-?\d+)$/ ) {
+ $hash{recur_discountnum} = $1;
+ $hash{recur_discountnum_amount} = $cgi->param('recur_discountnum_amount');
+ $hash{recur_discountnum_percent} = $cgi->param('recur_discountnum_percent');
+ $hash{recur_discountnum_months} = $cgi->param('recur_discountnum_months');
+}
+
$hash{'custnum'} = $cust_main->custnum if $cust_main;
if ( $cgi->param('start') eq 'on_hold' ) {