From e774555a6ae69da65e7774c701232fdf9dd052b1 Mon Sep 17 00:00:00 2001 From: levinse Date: Fri, 29 Apr 2011 12:12:45 +0000 Subject: [PATCH] apply discount to setup fees, part 1 of 2, RT11512 --- FS/FS/Schema.pm | 1 + FS/FS/cust_pkg.pm | 3 ++- FS/FS/cust_pkg_discount.pm | 1 + FS/FS/discount.pm | 7 +++++++ FS/FS/part_pkg/discount_Mixin.pm | 18 +++++++++++++++--- FS/FS/part_pkg/flat.pm | 6 +++++- httemplate/edit/discount.html | 2 ++ httemplate/edit/process/cust_pkg_discount.html | 1 + httemplate/edit/process/quick-cust_pkg.cgi | 1 + httemplate/elements/tr-select-discount.html | 19 +++++++++++++++++++ 10 files changed, 54 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 { diff --git a/httemplate/edit/discount.html b/httemplate/edit/discount.html index 6e0d8e1a7..b195eb37b 100644 --- a/httemplate/edit/discount.html +++ b/httemplate/edit/discount.html @@ -21,6 +21,7 @@ { field => 'months', type => 'text', size => 2, postfix => '
(blank for non-expiring discount)', }, + { field => 'setup', type => 'checkbox', value=>'Y', }, ], 'labels' => { 'discountnum' => 'Discount #', @@ -30,6 +31,7 @@ 'amount' => 'Amount ', 'percent' => 'Percentage ', 'months' => 'Duration (months)', + 'setup' => 'Apply to setup fees', }, 'viewall_dir' => 'browse', 'new_callback' => $new_callback, diff --git a/httemplate/edit/process/cust_pkg_discount.html b/httemplate/edit/process/cust_pkg_discount.html index ad9842a89..6f97a791e 100644 --- a/httemplate/edit/process/cust_pkg_discount.html +++ b/httemplate/edit/process/cust_pkg_discount.html @@ -39,6 +39,7 @@ my $cust_pkg_discount = new FS::cust_pkg_discount { 'amount' => scalar($cgi->param('discountnum_amount')), 'percent' => scalar($cgi->param('discountnum_percent')), 'months' => scalar($cgi->param('discountnum_months')), + 'setup' => scalar($cgi->param('discountnum_setup')), #'disabled' => $self->discountnum_disabled, }; my $error = $cust_pkg_discount->insert; diff --git a/httemplate/edit/process/quick-cust_pkg.cgi b/httemplate/edit/process/quick-cust_pkg.cgi index bf5087c5e..4ab56e19a 100644 --- a/httemplate/edit/process/quick-cust_pkg.cgi +++ b/httemplate/edit/process/quick-cust_pkg.cgi @@ -91,6 +91,7 @@ my $cust_pkg = new FS::cust_pkg { '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')) : '' diff --git a/httemplate/elements/tr-select-discount.html b/httemplate/elements/tr-select-discount.html index 258eeb349..e8be3938c 100644 --- a/httemplate/elements/tr-select-discount.html +++ b/httemplate/elements/tr-select-discount.html @@ -64,6 +64,16 @@ ) %> + <% include( '/elements/tr-checkbox.html', + 'label' => 'Apply discount to setup fee', + 'field' => $name.'_setup', + 'id' => $name.'_setup', + 'curr_value' => scalar($cgi->param($name.'_setup')), + 'value' => 'Y', + 'colspan' => $opt{'colspan'}, + ) + %> +