From 467b269c5cacef3e8486e2288f1b0eeec8fea088 Mon Sep 17 00:00:00 2001 From: ivan Date: Sat, 30 May 2009 03:14:13 +0000 Subject: [PATCH] add ability for prepaid packages to have usage limits and cancel if they're hit, RT#4995 --- FS/FS/part_pkg/flat.pm | 78 +++++++++++++++++++++++++++++------------------ FS/FS/part_pkg/prepaid.pm | 16 +++++++++- FS/FS/svc_acct.pm | 22 +++++++++++++ 3 files changed, 85 insertions(+), 31 deletions(-) diff --git a/FS/FS/part_pkg/flat.pm b/FS/FS/part_pkg/flat.pm index 009e54c78..171885770 100644 --- a/FS/FS/part_pkg/flat.pm +++ b/FS/FS/part_pkg/flat.pm @@ -1,7 +1,10 @@ package FS::part_pkg::flat; use strict; -use vars qw(@ISA %info); +use vars qw( @ISA %info + %usage_fields %usage_recharge_fields + @usage_fieldorder @usage_recharge_fieldorder + ); use Tie::IxHash; #use FS::Record qw(qsearch); use FS::UI::bytecount; @@ -14,30 +17,8 @@ tie my %temporalities, 'Tie::IxHash', 'preceding' => "Preceding (past)", ; -%info = ( - 'name' => 'Flat rate (anniversary billing)', - 'shortname' => 'Anniversary', - 'fields' => { - 'setup_fee' => { 'name' => 'Setup fee for this package', - 'default' => 0, - }, - 'recur_fee' => { 'name' => 'Recurring fee for this package', - 'default' => 0, - }, - - #false laziness w/voip_cdr.pm - 'recur_temporality' => { 'name' => 'Charge recurring fee for period', - 'type' => 'select', - 'select_options' => \%temporalities, - }, +%usage_fields = ( - 'unused_credit' => { 'name' => 'Credit the customer for the unused portion'. - ' of service at cancellation', - 'type' => 'checkbox', - }, - 'externalid' => { 'name' => 'Optional External ID', - 'default' => '', - }, 'seconds' => { 'name' => 'Time limit for this package', 'default' => '', 'check' => sub { shift =~ /^\d*$/ }, @@ -60,6 +41,10 @@ tie my %temporalities, 'Tie::IxHash', 'format' => \&FS::UI::bytecount::display_bytecount, 'parse' => \&FS::UI::bytecount::parse_bytecount, }, +); + +%usage_recharge_fields = ( + 'recharge_amount' => { 'name' => 'Cost of recharge for this package', 'default' => '', 'check' => sub { shift =~ /^\d*(\.\d{2})?$/ }, @@ -94,13 +79,46 @@ tie my %temporalities, 'Tie::IxHash', 'package recharge', 'type' => 'checkbox', }, +); + +@usage_fieldorder = qw( seconds upbytes downbytes totalbytes ); +@usage_recharge_fieldorder = qw( + recharge_amount recharge_seconds recharge_upbytes + recharge_downbytes recharge_totalbytes + usage_rollover recharge_reset +); + +%info = ( + 'name' => 'Flat rate (anniversary billing)', + 'shortname' => 'Anniversary', + 'fields' => { + 'setup_fee' => { 'name' => 'Setup fee for this package', + 'default' => 0, + }, + 'recur_fee' => { 'name' => 'Recurring fee for this package', + 'default' => 0, + }, + + #false laziness w/voip_cdr.pm + 'recur_temporality' => { 'name' => 'Charge recurring fee for period', + 'type' => 'select', + 'select_options' => \%temporalities, + }, + + %usage_fields, + %usage_recharge_fields, + + 'unused_credit' => { 'name' => 'Credit the customer for the unused portion'. + ' of service at cancellation', + 'type' => 'checkbox', + }, + 'externalid' => { 'name' => 'Optional External ID', + 'default' => '', + }, }, - 'fieldorder' => [qw( setup_fee recur_fee recur_temporality unused_credit - seconds upbytes downbytes totalbytes - recharge_amount recharge_seconds recharge_upbytes - recharge_downbytes recharge_totalbytes - usage_rollover recharge_reset externalid - ) + 'fieldorder' => [ qw( setup_fee recur_fee recur_temporality unused_credit ), + @usage_fieldorder, @usage_recharge_fieldorder, + qw( externalid ), ], 'weight' => 10, ); diff --git a/FS/FS/part_pkg/prepaid.pm b/FS/FS/part_pkg/prepaid.pm index 4499d0e52..23d340167 100644 --- a/FS/FS/part_pkg/prepaid.pm +++ b/FS/FS/part_pkg/prepaid.pm @@ -12,6 +12,11 @@ tie %recur_action, 'Tie::IxHash', 'cancel' => 'cancel', ; +tie my %overlimit_action, 'Tie::IxHash', + 'overlimit' => 'Default overlimit processing', + 'cancel' => 'Cancel', +; + %info = ( 'name' => 'Prepaid, flat rate', #'name' => 'Prepaid (no automatic recurring)', #maybe use it here too @@ -27,8 +32,17 @@ tie %recur_action, 'Tie::IxHash', 'type' => 'select', 'select_options' => \%recur_action, }, + %FS::part_pkg::flat::usage_fields, + 'overlimit_action' => { 'name' => 'Action to take upon reaching a usage limit.', + 'type' => 'select', + 'select_options' => \%overlimit_action, + }, + #XXX if you set overlimit_action to 'cancel', should also have the ability + # to select a reason }, - 'fieldorder' => [ 'setup_fee', 'recur_fee', 'recur_action', ], + 'fieldorder' => [ qw( setup_fee recur_fee recur_action ), + @FS::part_pkg::flat::usage_fieldorder, + ], 'weight' => 25, ); diff --git a/FS/FS/svc_acct.pm b/FS/FS/svc_acct.pm index e5be29473..4669588d5 100644 --- a/FS/FS/svc_acct.pm +++ b/FS/FS/svc_acct.pm @@ -1772,6 +1772,28 @@ sub _op_usage { die "Can't update $column for svcnum". $self->svcnum if $rv == 0; + #overlimit_action eq 'cancel' handling + my $cust_pkg = $self->cust_svc->cust_pkg; + if ( $cust_pkg + && $cust_pkg->part_pkg->option('overlimit_action', 1) eq 'cancel' + && $op eq '-' && &{$op2condition{$op}}($self, $column, $amount) + ) + { + + my $error = $cust_pkg->cancel; #XXX should have a reason + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return "Error cancelling: $error"; + } + + #nothing else is relevant if we're cancelling, so commit & return success + warn "$me update successful; committing\n" + if $DEBUG; + $dbh->commit or die $dbh->errstr if $oldAutoCommit; + return ''; + + } + my $action = $op2action{$op}; if ( &{$op2condition{$op}}($self, $column, $amount) && -- 2.11.0