summaryrefslogtreecommitdiff
path: root/FS/FS
diff options
context:
space:
mode:
authorlevinse <levinse>2011-04-29 12:12:45 +0000
committerlevinse <levinse>2011-04-29 12:12:45 +0000
commite774555a6ae69da65e7774c701232fdf9dd052b1 (patch)
tree3807379817a297a53b27b1f6d8c8c7ec758f6713 /FS/FS
parent91fd4ea0e91ee9e6717e0b12e01eebb8f8b21322 (diff)
apply discount to setup fees, part 1 of 2, RT11512
Diffstat (limited to 'FS/FS')
-rw-r--r--FS/FS/Schema.pm1
-rw-r--r--FS/FS/cust_pkg.pm3
-rw-r--r--FS/FS/cust_pkg_discount.pm1
-rw-r--r--FS/FS/discount.pm7
-rw-r--r--FS/FS/part_pkg/discount_Mixin.pm18
-rw-r--r--FS/FS/part_pkg/flat.pm6
6 files changed, 31 insertions, 5 deletions
diff --git a/FS/FS/Schema.pm b/FS/FS/Schema.pm
index 5d453ac27..3e2bb7673 100644
--- a/FS/FS/Schema.pm
+++ b/FS/FS/Schema.pm
@@ -1534,6 +1534,7 @@ sub tables_hashref {
'percent', 'decimal', '', '', '', '',
'months', 'decimal', 'NULL', '', '', '',
'disabled', 'char', 'NULL', 1, '', '',
+ 'setup', 'char', 'NULL', 1, '', '',
],
'primary_key' => 'discountnum',
'unique' => [],
diff --git a/FS/FS/cust_pkg.pm b/FS/FS/cust_pkg.pm
index 6c985cf19..deb1cf1fe 100644
--- a/FS/FS/cust_pkg.pm
+++ b/FS/FS/cust_pkg.pm
@@ -30,6 +30,7 @@ use FS::reason;
use FS::cust_pkg_discount;
use FS::discount;
use FS::UI::Web;
+use Data::Dumper;
# need to 'use' these instead of 'require' in sub { cancel, suspend, unsuspend,
# setup }
@@ -1416,7 +1417,6 @@ sub change {
}
-use Data::Dumper;
use Storable 'thaw';
use MIME::Base64;
sub process_bulk_cust_pkg {
@@ -2608,6 +2608,7 @@ sub insert_discount {
'amount' => $self->discountnum_amount,
'percent' => $self->discountnum_percent,
'months' => $self->discountnum_months,
+ 'setup' => $self->discountnum_setup,
#'disabled' => $self->discountnum_disabled,
};
diff --git a/FS/FS/cust_pkg_discount.pm b/FS/FS/cust_pkg_discount.pm
index 7b6b203b8..d41f497a6 100644
--- a/FS/FS/cust_pkg_discount.pm
+++ b/FS/FS/cust_pkg_discount.pm
@@ -106,6 +106,7 @@ sub insert {
'amount' => $self->amount,
'percent' => $self->percent,
'months' => $self->months,
+ 'setup' => $self->setup,
'disabled' => 'Y',
};
my $error = $discount->insert;
diff --git a/FS/FS/discount.pm b/FS/FS/discount.pm
index 4f42c5b72..6fc3391ca 100644
--- a/FS/FS/discount.pm
+++ b/FS/FS/discount.pm
@@ -54,6 +54,11 @@ months
disabled
+=item setup - apply discount to setup fee (not just to recurring fee)
+
+If the discount is based on a percentage, then the % will be applied to the
+setup and recurring portions.
+
=back
=head1 METHODS
@@ -130,6 +135,7 @@ sub check {
|| $self->ut_float('percent') #actually decimal, but this will do
|| $self->ut_floatn('months') #actually decimal, but this will do
|| $self->ut_enum('disabled', [ '', 'Y' ])
+ || $self->ut_enum('setup', [ '', 'Y' ])
;
return $error if $error;
@@ -176,6 +182,7 @@ sub description {
my $self = shift;
my $desc = $self->description_short;
$desc .= ' for '. $self->months. ' months' if $self->months;
+ $desc .= ', applies to setup' if $self->setup;
$desc;
}
diff --git a/FS/FS/part_pkg/discount_Mixin.pm b/FS/FS/part_pkg/discount_Mixin.pm
index 83f1a77c1..8ce5ba875 100644
--- a/FS/FS/part_pkg/discount_Mixin.pm
+++ b/FS/FS/part_pkg/discount_Mixin.pm
@@ -43,6 +43,8 @@ sub calc_discount {
my $br = $self->base_recur($cust_pkg, $sdate);
$br += $param->{'override_charges'} if $param->{'override_charges'};
+
+ return 0 if defined $param->{'setup_charge'} && $param->{'setup_charge'} == 0;
my $tot_discount = 0;
#UI enforces just 1 for now, will need ordering when they can be stacked
@@ -90,9 +92,19 @@ sub calc_discount {
? min( $chg_months,
$discount->months - $cust_pkg_discount->months_used )
: $chg_months;
+
+ if(defined $param->{'setup_charge'}) {
+ next unless $discount->setup;
+
+ if ( $discount->percent ) {
+ $amount = sprintf('%.2f', $discount->percent * $param->{'setup_charge'} / 100 );
+ $months = 1;
+ }
+ }
my $error = $cust_pkg_discount->increment_months_used($months)
- if $cust_pkg->pkgpart == $param->{real_pkgpart};
+ if ($cust_pkg->pkgpart == $param->{real_pkgpart}
+ && ! defined $param->{'setup_charge'});
die "error discounting: $error" if $error;
$amount *= $months;
@@ -114,9 +126,9 @@ sub calc_discount {
$months = sprintf('%.2f', $months) if $months =~ /\./;
my $d = 'Includes ';
- $d .= $discount->name. ' ' if $discount->name;
+ $d .= 'setup ' if defined $param->{'setup_charge'};
$d .= 'discount of '. $discount->description_short;
- $d .= " for $months month". ( $months!=1 ? 's' : '' );
+ $d .= " for $months month". ( $months!=1 ? 's' : '' ) unless defined $param->{'setup_charge'};
$d .= ": $money_char$amount" if $months != 1 || $discount->percent;
push @$details, $d;
diff --git a/FS/FS/part_pkg/flat.pm b/FS/FS/part_pkg/flat.pm
index 04f4951c5..78c576424 100644
--- a/FS/FS/part_pkg/flat.pm
+++ b/FS/FS/part_pkg/flat.pm
@@ -106,7 +106,11 @@ sub calc_setup {
my $quantity = $cust_pkg->quantity || 1;
my $charge = $quantity * $self->unit_setup($cust_pkg, $sdate, $details);
- sprintf('%.2f', $charge);
+
+ my $param = { 'setup_charge' => $charge };
+ my $discount = $self->calc_discount($cust_pkg, $sdate, $details, $param);
+
+ sprintf('%.2f', $charge - $discount);
}
sub unit_setup {