summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorivan <ivan>2010-01-31 02:57:14 +0000
committerivan <ivan>2010-01-31 02:57:14 +0000
commit9b6b116d4a492cb0edc31e53abf1fdb43be42d0e (patch)
tree2ee0f6c5b965912416269f57ce15729b411e078d
parent481011e14852aa62181bed0c7ae34b1e37ebff18 (diff)
discounts, RT#6679
-rw-r--r--FS/FS/cust_pkg_discount.pm18
-rw-r--r--FS/FS/part_pkg/agent.pm2
-rw-r--r--FS/FS/part_pkg/bulk.pm2
-rw-r--r--FS/FS/part_pkg/flat.pm58
-rw-r--r--FS/FS/part_pkg/flat_comission.pm2
-rw-r--r--FS/FS/part_pkg/flat_comission_cust.pm2
-rw-r--r--FS/FS/part_pkg/flat_comission_pkg.pm2
-rw-r--r--FS/FS/part_pkg/prorate.pm9
-rw-r--r--FS/FS/part_pkg/recur_Common.pm2
-rw-r--r--FS/FS/part_pkg/rt_time.pm2
-rw-r--r--FS/FS/part_pkg/sesmon_hour.pm2
-rw-r--r--FS/FS/part_pkg/sesmon_minute.pm2
-rw-r--r--FS/FS/part_pkg/sql_external.pm6
-rw-r--r--FS/FS/part_pkg/sql_generic.pm2
-rw-r--r--FS/FS/part_pkg/sqlradacct_hour.pm2
-rw-r--r--FS/FS/part_pkg/subscription.pm6
-rw-r--r--FS/FS/part_pkg/voip_sqlradacct.pm6
17 files changed, 114 insertions, 11 deletions
diff --git a/FS/FS/cust_pkg_discount.pm b/FS/FS/cust_pkg_discount.pm
index 87f8c52..9fc618c 100644
--- a/FS/FS/cust_pkg_discount.pm
+++ b/FS/FS/cust_pkg_discount.pm
@@ -133,6 +133,8 @@ sub check {
=item cust_pkg
+Returns the customer package (see L<FS::cust_pkg>).
+
=cut
sub cust_pkg {
@@ -142,6 +144,8 @@ sub cust_pkg {
=item discount
+Returns the discount (see L<FS::discount>).
+
=cut
sub discount {
@@ -149,6 +153,20 @@ sub discount {
qsearchs('discount', { 'discountnum' => $self->discountnum } );
}
+=item increment_months_used
+
+Increments months_used by the given parameter
+
+=cut
+
+sub increment_months_used {
+ my( $self, $used ) = @_;
+ #UPDATE cust_pkg_discount SET months_used = months_used + ?
+ #leaves no history, and billing is mutexed per-customer, so the dum way is ok
+ $self->months_used( $self->months_used + $used );
+ $self->replace();
+}
+
=back
=head1 BUGS
diff --git a/FS/FS/part_pkg/agent.pm b/FS/FS/part_pkg/agent.pm
index d41978c..e5bd163 100644
--- a/FS/FS/part_pkg/agent.pm
+++ b/FS/FS/part_pkg/agent.pm
@@ -163,6 +163,8 @@ sub calc_recur {
}
+sub can_discount { 0; }
+
sub hide_svc_detail {
1;
}
diff --git a/FS/FS/part_pkg/bulk.pm b/FS/FS/part_pkg/bulk.pm
index 1b52d9f..69fe98e 100644
--- a/FS/FS/part_pkg/bulk.pm
+++ b/FS/FS/part_pkg/bulk.pm
@@ -94,6 +94,8 @@ sub calc_recur {
sprintf('%.2f', $self->base_recur($cust_pkg) + $total_svc_charge );
}
+sub can_discount { 0; }
+
sub hide_svc_detail {
1;
}
diff --git a/FS/FS/part_pkg/flat.pm b/FS/FS/part_pkg/flat.pm
index 9bb45e6..402730d 100644
--- a/FS/FS/part_pkg/flat.pm
+++ b/FS/FS/part_pkg/flat.pm
@@ -6,8 +6,10 @@ use vars qw( @ISA %info
@usage_fieldorder @usage_recharge_fieldorder
);
use Tie::IxHash;
+use List::Util qw(min); # max);
#use FS::Record qw(qsearch);
use FS::UI::bytecount;
+use FS::Conf;
use FS::part_pkg;
@ISA = qw(FS::part_pkg);
@@ -153,7 +155,55 @@ sub calc_recur {
return 0
if $self->option('recur_temporality', 1) eq 'preceding' && $last_bill == 0;
- $self->base_recur(@_);
+ my $br = $self->base_recur(@_);
+
+ my $discount = $self->calc_discount(@_);
+
+ sprintf('%.2f', $br - $discount);
+}
+
+sub calc_discount {
+ my $self = shift;
+ my($cust_pkg, $sdate, $details, $param ) = @_;
+
+ my $br = $self->base_recur(@_);
+
+ my $tot_discount = 0;
+ #UI enforces just 1 for now, will need ordering when they can be stacked
+ foreach my $cust_pkg_discount ( $cust_pkg->cust_pkg_discount_active ) {
+ my $discount = $cust_pkg_discount->discount;
+ #UI enforces one or the other (for now? probably for good)
+ my $amount = 0;
+ $amount += $discount->amount;
+ $amount += sprintf('%.2f', $discount->percent * $br / 100 );
+
+ my $chg_months = $param->{'months'} || $cust_pkg->part_pkg->freq;
+
+ my $months = $discount->months
+ ? min( $chg_months,
+ $discount->months - $cust_pkg->months_used )
+ : $chg_months;
+
+ my $error = $cust_pkg_discount->increment_months_used($months);
+ die "error discounting: $error" if $error;
+
+ $amount *= $months;
+
+ #add details on discount to invoice
+ my $conf = new FS::Conf;
+ my $money_char = $conf->config('money_char') || '$';
+
+ my $d = 'Includes ';
+ $d .= $discount->name. ' ' if $discount->name;
+ $d .= 'discount of '. $discount->description_short;
+ $d .= " for $months month". ( $months>1 ? 's' : '' );
+ $d .= ": $money_char$amount" if $months != 1 || $discount->percent;
+ push @$details, $d;
+
+ $tot_discount += $amount;
+ }
+
+ sprintf('%.2f', $tot_discount);
}
sub base_recur {
@@ -212,7 +262,11 @@ sub is_free_options {
sub is_prepaid { 0; } #no, we're postpaid
-sub can_discount { 1; } #and anything that inherits from us
+#XXX discounts only on recurring fees for now (no setup/one-time or usage)
+sub can_discount {
+ my $self = shift;
+ $self->freq =~ /^\d+$/ && $self->freq > 0;
+}
sub usage_valuehash {
my $self = shift;
diff --git a/FS/FS/part_pkg/flat_comission.pm b/FS/FS/part_pkg/flat_comission.pm
index 1f57e4f..c05e455 100644
--- a/FS/FS/part_pkg/flat_comission.pm
+++ b/FS/FS/part_pkg/flat_comission.pm
@@ -64,4 +64,6 @@ sub calc_recur {
$self->option('recur_fee');
}
+sub can_discount { 0; }
+
1;
diff --git a/FS/FS/part_pkg/flat_comission_cust.pm b/FS/FS/part_pkg/flat_comission_cust.pm
index e2034cd..5db4658 100644
--- a/FS/FS/part_pkg/flat_comission_cust.pm
+++ b/FS/FS/part_pkg/flat_comission_cust.pm
@@ -62,4 +62,6 @@ sub calc_recur {
$self->option('recur_fee');
}
+sub can_discount { 0; }
+
1;
diff --git a/FS/FS/part_pkg/flat_comission_pkg.pm b/FS/FS/part_pkg/flat_comission_pkg.pm
index 0a66ff0..6f5ee69 100644
--- a/FS/FS/part_pkg/flat_comission_pkg.pm
+++ b/FS/FS/part_pkg/flat_comission_pkg.pm
@@ -55,4 +55,6 @@ sub calc_recur {
$self->option('recur_fee');
}
+sub can_discount { 0; }
+
1;
diff --git a/FS/FS/part_pkg/prorate.pm b/FS/FS/part_pkg/prorate.pm
index d3ca77a..894666d 100644
--- a/FS/FS/part_pkg/prorate.pm
+++ b/FS/FS/part_pkg/prorate.pm
@@ -95,7 +95,7 @@ use FS::part_pkg::flat;
);
sub calc_recur {
- my($self, $cust_pkg, $sdate ) = @_;
+ my($self, $cust_pkg, $sdate, $details, $param ) = @_;
my $cutoff_day = $self->option('cutoff_day', 1) || 1;
my $mnow = $$sdate;
my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($mnow) )[0,1,2,3,4,5];
@@ -117,7 +117,12 @@ sub calc_recur {
$$sdate = $mstart;
my $permonth = $self->option('recur_fee') / $self->freq;
- $permonth * ( ( $self->freq - 1 ) + ($mend-$mnow) / ($mend-$mstart) );
+ my $months = ( ( $self->freq - 1 ) + ($mend-$mnow) / ($mend-$mstart) );
+
+ $param->{'months'} = $months;
+ my $discount = $self->calc_discount( $cust_pkg, $sdate, $details, $param);
+
+ sprintf('%.2f', $permonth * $months - $discount);
}
1;
diff --git a/FS/FS/part_pkg/recur_Common.pm b/FS/FS/part_pkg/recur_Common.pm
index 2739cbc..8ed9eb6 100644
--- a/FS/FS/part_pkg/recur_Common.pm
+++ b/FS/FS/part_pkg/recur_Common.pm
@@ -48,6 +48,8 @@ sub calc_recur_Common {
}#$recur_method eq 'subscription'
+ $charges -= $self->calc_discount( $cust_pkg, $sdate, $details, $param );
+
}#$recur_method eq 'prorate'
}#increment_next_bill
diff --git a/FS/FS/part_pkg/rt_time.pm b/FS/FS/part_pkg/rt_time.pm
index 675a466..9452d44 100644
--- a/FS/FS/part_pkg/rt_time.pm
+++ b/FS/FS/part_pkg/rt_time.pm
@@ -39,6 +39,8 @@ sub calc_recur {
}
+sub can_discount { 0; }
+
sub calc_cancel {
my $self = shift;
my($cust_pkg, $sdate, $details, $param ) = @_;
diff --git a/FS/FS/part_pkg/sesmon_hour.pm b/FS/FS/part_pkg/sesmon_hour.pm
index 22ece95..e608d55 100644
--- a/FS/FS/part_pkg/sesmon_hour.pm
+++ b/FS/FS/part_pkg/sesmon_hour.pm
@@ -45,6 +45,8 @@ sub calc_recur {
}
+sub can_discount { 0; }
+
sub is_free_options {
qw( setup_fee recur_fee recur_hourly_charge );
}
diff --git a/FS/FS/part_pkg/sesmon_minute.pm b/FS/FS/part_pkg/sesmon_minute.pm
index 7386df6..654fdf5 100644
--- a/FS/FS/part_pkg/sesmon_minute.pm
+++ b/FS/FS/part_pkg/sesmon_minute.pm
@@ -44,6 +44,8 @@ sub calc_recur {
$self->option('recur_fee') + $min * $self->option('recur_minly_charge');
}
+sub can_discount { 0; }
+
sub is_free_options {
qw( setup_fee recur_fee recur_minly_charge );
}
diff --git a/FS/FS/part_pkg/sql_external.pm b/FS/FS/part_pkg/sql_external.pm
index 70f9f04..786600d 100644
--- a/FS/FS/part_pkg/sql_external.pm
+++ b/FS/FS/part_pkg/sql_external.pm
@@ -65,9 +65,9 @@ sub calc_recur {
$price;
}
-sub is_free {
- 0;
-}
+sub can_discount { 0; }
+
+sub is_free { 0; }
sub base_recur {
my($self, $cust_pkg) = @_;
diff --git a/FS/FS/part_pkg/sql_generic.pm b/FS/FS/part_pkg/sql_generic.pm
index 5a6a11a..eb80044 100644
--- a/FS/FS/part_pkg/sql_generic.pm
+++ b/FS/FS/part_pkg/sql_generic.pm
@@ -76,6 +76,8 @@ sub calc_recur {
$self->option('recur_fee') + $units * $self->option('recur_unit_charge');
}
+sub can_discount { 0; }
+
sub is_free_options {
qw( setup_fee recur_fee recur_unit_charge );
}
diff --git a/FS/FS/part_pkg/sqlradacct_hour.pm b/FS/FS/part_pkg/sqlradacct_hour.pm
index c86956a..15f678f 100644
--- a/FS/FS/part_pkg/sqlradacct_hour.pm
+++ b/FS/FS/part_pkg/sqlradacct_hour.pm
@@ -158,6 +158,8 @@ sub calc_recur {
$self->option('recur_fee') + $charges;
}
+sub can_discount { 0; }
+
sub is_free_options {
qw( setup_fee recur_fee recur_hourly_charge
recur_input_charge recur_output_charge recur_total_charge );
diff --git a/FS/FS/part_pkg/subscription.pm b/FS/FS/part_pkg/subscription.pm
index dbf6d79..825f5ca 100644
--- a/FS/FS/part_pkg/subscription.pm
+++ b/FS/FS/part_pkg/subscription.pm
@@ -103,7 +103,11 @@ sub calc_recur {
$$sdate = timelocal(0,0,0,$cutoff_day,$mon,$year);
- $self->option('recur_fee');
+ my $br = $self->base_recur(@_);
+
+ my $discount = $self->calc_discount(@_);
+
+ sprintf('%.2f', $br - $discount);
}
1;
diff --git a/FS/FS/part_pkg/voip_sqlradacct.pm b/FS/FS/part_pkg/voip_sqlradacct.pm
index b4f0cf9..441df58 100644
--- a/FS/FS/part_pkg/voip_sqlradacct.pm
+++ b/FS/FS/part_pkg/voip_sqlradacct.pm
@@ -181,9 +181,9 @@ sub calc_recur {
}
-sub is_free {
- 0;
-}
+sub can_discount { 0; }
+
+sub is_free { 0; }
sub base_recur {
my($self, $cust_pkg) = @_;