From 8a0204b4a129a3c26dcca18ba401b2de26d22d2b Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 5 Feb 2010 02:39:31 +0000 Subject: [PATCH] discounts, RT#6679 --- FS/FS/cust_pkg.pm | 48 +++++++++++++++++++----- FS/FS/cust_pkg_discount.pm | 44 +++++++++++++++++++++- FS/FS/discount.pm | 10 +++++ FS/FS/part_pkg/flat.pm | 4 +- httemplate/edit/cust_pkg_discount.html | 3 +- httemplate/edit/discount.html | 2 +- httemplate/edit/process/discount.html | 17 --------- httemplate/edit/process/quick-cust_pkg.cgi | 28 +++++++++----- httemplate/elements/tr-select-discount.html | 52 ++++++++++++++------------ httemplate/misc/order_pkg.html | 2 + httemplate/view/cust_main/order_pkg_link.html | 2 +- httemplate/view/cust_main/packages/status.html | 8 ++-- 12 files changed, 153 insertions(+), 67 deletions(-) diff --git a/FS/FS/cust_pkg.pm b/FS/FS/cust_pkg.pm index 5cdca09ac..acc73dfda 100644 --- a/FS/FS/cust_pkg.pm +++ b/FS/FS/cust_pkg.pm @@ -277,15 +277,7 @@ sub insert { ); if ( $self->discountnum ) { - #XXX new/custom discount case - my $cust_pkg_discount = new FS::cust_pkg_discount { - 'pkgnum' => $self->pkgnum, - 'discountnum' => $self->discountnum, - 'months_used' => 0, - 'end_date' => '', #XXX - 'otaker' => $self->otaker, - }; - my $error = $cust_pkg_discount->insert; + my $error = $self->insert_discount(); if ( $error ) { $dbh->rollback if $oldAutoCommit; return $error; @@ -2303,6 +2295,44 @@ sub insert_reason { $cust_pkg_reason->insert; } +=item insert_discount + +Associates this package with a discount (see L, possibly +inserting a new discount on the fly (see L). + +Available options are: + +=over 4 + +=item discountnum + +=back + +If there is an error, returns the error, otherwise returns false. + +=cut + +sub insert_discount { + #my ($self, %options) = @_; + my $self = shift; + + my $cust_pkg_discount = new FS::cust_pkg_discount { + 'pkgnum' => $self->pkgnum, + 'discountnum' => $self->discountnum, + 'months_used' => 0, + 'end_date' => '', #XXX + 'otaker' => $self->otaker, + #for the create a new discount case + '_type' => $self->discountnum__type, + 'amount' => $self->discountnum_amount, + 'percent' => $self->discountnum_percent, + 'months' => $self->discountnum_months, + #'disabled' => $self->discountnum_disabled, + }; + + $cust_pkg_discount->insert; +} + =item set_usage USAGE_VALUE_HASHREF USAGE_VALUE_HASHREF is a hashref of svc_acct usage columns and the amounts diff --git a/FS/FS/cust_pkg_discount.pm b/FS/FS/cust_pkg_discount.pm index 9fc618cab..8dd00de89 100644 --- a/FS/FS/cust_pkg_discount.pm +++ b/FS/FS/cust_pkg_discount.pm @@ -2,7 +2,7 @@ package FS::cust_pkg_discount; use strict; use base qw( FS::Record ); -use FS::Record qw( qsearchs ); # qsearch ); +use FS::Record qw( dbh qsearchs ); # qsearch ); use FS::cust_pkg; use FS::discount; @@ -85,7 +85,47 @@ otherwise returns false. =cut -# the insert method can be inherited from FS::Record +sub insert { + #my( $self, %options ) = @_; + my $self = shift; + + local $SIG{HUP} = 'IGNORE'; + local $SIG{INT} = 'IGNORE'; + local $SIG{QUIT} = 'IGNORE'; + local $SIG{TERM} = 'IGNORE'; + local $SIG{TSTP} = 'IGNORE'; + local $SIG{PIPE} = 'IGNORE'; + + my $oldAutoCommit = $FS::UID::AutoCommit; + local $FS::UID::AutoCommit = 0; + my $dbh = dbh; + + if ( $self->discountnum == -1 ) { + my $discount = new FS::discount { + '_type' => $self->_type, + 'amount' => $self->amount, + 'percent' => $self->percent, + 'months' => $self->months, + 'disabled' => 'Y', + }; + my $error = $discount->insert; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error; + } + $self->discountnum($discount->discountnum); + } + + my $error = $self->SUPER::insert; #(@_); #(%options); + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error; + } + + $dbh->commit or die $dbh->errstr if $oldAutoCommit; + ''; + +} =item delete diff --git a/FS/FS/discount.pm b/FS/FS/discount.pm index 6771510d0..8afeb2e0c 100644 --- a/FS/FS/discount.pm +++ b/FS/FS/discount.pm @@ -113,6 +113,16 @@ and replace methods. sub check { my $self = shift; + if ( $self->_type eq 'Select discount type' ) { + return 'Please select a discount type'; + } elsif ( $self->_type eq 'Amount' ) { + $self->percent('0'); + return 'Amount must be greater than 0' unless $self->amount > 0; + } elsif ( $self->_type eq 'Percentage' ) { + $self->amount('0.00'); + return 'Percentage must be greater than 0' unless $self->percent > 0; + } + my $error = $self->ut_numbern('discountnum') || $self->ut_textn('name') diff --git a/FS/FS/part_pkg/flat.pm b/FS/FS/part_pkg/flat.pm index 402730d3f..e5fc089c4 100644 --- a/FS/FS/part_pkg/flat.pm +++ b/FS/FS/part_pkg/flat.pm @@ -188,15 +188,17 @@ sub calc_discount { die "error discounting: $error" if $error; $amount *= $months; + $amount = sprintf('%.2f', $amount); #add details on discount to invoice my $conf = new FS::Conf; my $money_char = $conf->config('money_char') || '$'; + $months = sprintf('%.2f', $months) if $months =~ /\./; my $d = 'Includes '; $d .= $discount->name. ' ' if $discount->name; $d .= 'discount of '. $discount->description_short; - $d .= " for $months month". ( $months>1 ? 's' : '' ); + $d .= " for $months month". ( $months!=1 ? 's' : '' ); $d .= ": $money_char$amount" if $months != 1 || $discount->percent; push @$details, $d; diff --git a/httemplate/edit/cust_pkg_discount.html b/httemplate/edit/cust_pkg_discount.html index 22d8c632f..0bb84b8f2 100755 --- a/httemplate/edit/cust_pkg_discount.html +++ b/httemplate/edit/cust_pkg_discount.html @@ -28,7 +28,8 @@ <% include('/elements/tr-select-discount.html', 'empty_label' => ( $pkgdiscountnum ? '' : 'Select discount' ), - 'onchange' => 'enable_discount_pkg', + 'onchange' => 'enable_discount_pkg()', + 'cgi' => $cgi, ) %> diff --git a/httemplate/edit/discount.html b/httemplate/edit/discount.html index 4f440b2cb..6e0d8e1a7 100644 --- a/httemplate/edit/discount.html +++ b/httemplate/edit/discount.html @@ -29,7 +29,7 @@ '_type' => 'Type ', 'amount' => 'Amount ', 'percent' => 'Percentage ', - 'months' => '# of Months', + 'months' => 'Duration (months)', }, 'viewall_dir' => 'browse', 'new_callback' => $new_callback, diff --git a/httemplate/edit/process/discount.html b/httemplate/edit/process/discount.html index 54307b708..80dc0f5bd 100644 --- a/httemplate/edit/process/discount.html +++ b/httemplate/edit/process/discount.html @@ -1,7 +1,6 @@ <% include( 'elements/process.html', 'table' => 'discount', 'viewall_dir' => 'browse', - 'precheck_callback' => $precheck_callback, ) %> <%init> @@ -9,20 +8,4 @@ die "access denied" unless $FS::CurrentUser::CurrentUser->access_right('Configuration'); -my $precheck_callback = sub { - my( $cgi ) = @_; - - if ( $cgi->param('_type') eq 'Select discount type' ) { - return 'Please select a discount type'; - } elsif ( $cgi->param('_type') eq 'Amount' ) { - $cgi->param('percent', '0'); - return 'Amount must be greater than 0' unless $cgi->param('amount') > 0; - } elsif ( $cgi->param('_type') eq 'Percentage' ) { - $cgi->param('amount', '0.00'); - return 'Percentage must be greater than 0' unless $cgi->param('percent') > 0; - } - - ''; -}; - diff --git a/httemplate/edit/process/quick-cust_pkg.cgi b/httemplate/edit/process/quick-cust_pkg.cgi index c60156746..a0958922e 100644 --- a/httemplate/edit/process/quick-cust_pkg.cgi +++ b/httemplate/edit/process/quick-cust_pkg.cgi @@ -45,17 +45,27 @@ my $refnum = $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; + my $cust_pkg = new FS::cust_pkg { - 'custnum' => $custnum, - 'pkgpart' => $pkgpart, - 'start_date' => ( scalar($cgi->param('start_date')) - ? str2time($cgi->param('start_date')) - : '' - ), - 'discountnum' => scalar($cgi->param('discountnum')), - 'refnum' => $refnum, - 'locationnum' => $locationnum, + 'custnum' => $custnum, + 'pkgpart' => $pkgpart, + 'start_date' => ( scalar($cgi->param('start_date')) + ? str2time($cgi->param('start_date')) + : '' + ), + 'refnum' => $refnum, + '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_disabled' => scalar($cgi->param('discountnum_disabled')), }; my %opt = ( 'cust_pkg' => $cust_pkg ); diff --git a/httemplate/elements/tr-select-discount.html b/httemplate/elements/tr-select-discount.html index df221da96..258eeb349 100644 --- a/httemplate/elements/tr-select-discount.html +++ b/httemplate/elements/tr-select-discount.html @@ -6,11 +6,11 @@ % } else { - <% $opt{'label'} || 'Discount' %> + <% $opt{'label'} || 'Discount' %> > <% include( '/elements/select-discount.html', 'curr_value' => $discountnum, - 'onchange' => $name.'_changed', + 'onchange' => $onchange, %opt, ) %> @@ -19,7 +19,7 @@ % # a weird kind of false laziness w/edit/discount.html - +% # <% include( '/elements/tr-select.html', @@ -27,18 +27,18 @@ 'field' => $name. '__type', 'id' => $name. '__type', 'options' => \@_type_options, - #XXX 'curr_value' => - 'onchange' => '_type_changed', + 'curr_value' => scalar($cgi->param($name.'__type')), + 'onchange' => $name.'__type_changed', 'colspan' => $opt{'colspan'}, ) %> <% include( '/elements/tr-input-money.html', - 'label' => 'Discount Amount', + 'label' => 'Discount Amount ', 'field' => $name. '_amount', 'id' => $name. '_amount', 'default' => '0.00', - #XXX 'curr_value' => + 'curr_value' => scalar($cgi->param($name.'_amount')), 'colspan' => $opt{'colspan'}, ) %> @@ -48,18 +48,18 @@ 'field' => $name. '_percent', 'id' => $name. '_percent', 'default' => '0', - #XXX 'curr_value' => + 'curr_value' => scalar($cgi->param($name.'_percent')), 'colspan' => $opt{'colspan'}, ) %> <% include( '/elements/tr-input-text.html', - 'label' => 'Discount # of Months', + 'label' => 'Discount duration (months)', 'field' => $name. '_months', 'id' => $name. '_months', 'size' => 2, 'postfix' => qq((blank for non-expiring discount)), - #XXX 'curr_value' => + 'curr_value' => scalar($cgi->param($name.'_months')), 'colspan' => $opt{'colspan'}, ) %> @@ -93,14 +93,14 @@ % #XXX save visibility settings for amount, percent :/ <% $ge %>('<% $name %>_amount_label0').style.display = 'none'; <% $ge %>('<% $name %>_amount_label0').style.visibility = 'hidden'; - <% $ge %>('<% $name %>_amount').style.display = 'none'; - <% $ge %>('<% $name %>_amount').style.visibility = 'hidden'; + <% $ge %>('<% $name %>_amount_input0').style.display = 'none'; + <% $ge %>('<% $name %>_amount_input0').style.visibility = 'hidden'; <% $ge %>('<% $name %>_amount_input0').style.display = 'none'; <% $ge %>('<% $name %>_amount_input0').style.visibility = 'hidden'; <% $ge %>('<% $name %>_percent_label0').style.display = 'none'; <% $ge %>('<% $name %>_percent_label0').style.visibility = 'hidden'; - <% $ge %>('<% $name %>_percent').style.display = 'none'; - <% $ge %>('<% $name %>_percent').style.visibility = 'hidden'; + <% $ge %>('<% $name %>_percent_input0').style.display = 'none'; + <% $ge %>('<% $name %>_percent_input0').style.visibility = 'hidden'; <% $ge %>('<% $name %>_percent_input0').style.display = 'none'; <% $ge %>('<% $name %>_percent_input0').style.visibility = 'hidden'; @@ -116,7 +116,7 @@ } function <% $name %>__type_changed(what) { - var _type = what.options[what.selectedIndex].value; + var <% $name %>__type = what.options[what.selectedIndex].value; if ( <% $name %>__type == '<% $select %>' ) { <% $ge %>('<% $name %>_amount_label0').style.display = 'none'; @@ -130,31 +130,34 @@ } else if ( <% $name %>__type == 'Amount' ) { <% $ge %>('<% $name %>_amount_label0').style.display = ''; <% $ge %>('<% $name %>_amount_label0').style.visibility = ''; - <% $ge %>('<% $name %>_amount').style.display = ''; - <% $ge %>('<% $name %>_amount').style.visibility = ''; + <% $ge %>('<% $name %>_amount_input0').style.display = ''; + <% $ge %>('<% $name %>_amount_input0').style.visibility = ''; <% $ge %>('<% $name %>_percent_label0').style.display = 'none'; <% $ge %>('<% $name %>_percent_label0').style.visibility = 'hidden'; - <% $ge %>('<% $name %>_percent').style.display = 'none'; - <% $ge %>('<% $name %>_percent').style.visibility = 'hidden'; + <% $ge %>('<% $name %>_percent_input0').style.display = 'none'; + <% $ge %>('<% $name %>_percent_input0').style.visibility = 'hidden'; } else if ( <% $name %>__type == 'Percentage' ) { <% $ge %>('<% $name %>_amount_label0').style.display = 'none'; <% $ge %>('<% $name %>_amount_label0').style.visibility = 'hidden'; - <% $ge %>('<% $name %>_amount').style.display = 'none'; - <% $ge %>('<% $name %>_amount').style.visibility = 'hidden'; + <% $ge %>('<% $name %>_amount_input0').style.display = 'none'; + <% $ge %>('<% $name %>_amount_input0').style.visibility = 'hidden'; <% $ge %>('<% $name %>_percent_label0').style.display = ''; <% $ge %>('<% $name %>_percent_label0').style.visibility = ''; - <% $ge %>('<% $name %>_percent').style.display = ''; - <% $ge %>('<% $name %>_percent').style.visibility = ''; + <% $ge %>('<% $name %>_percent_input0').style.display = ''; + <% $ge %>('<% $name %>_percent_input0').style.visibility = ''; } } + <% $name %>_changed(<% $ge %>('<% $name %>')); + % } <%init> my %opt = @_; +my $cgi = $opt{'cgi'}; my $discountnum = $opt{'curr_value'} || $opt{'value'}; $opt{'discount'} ||= [ qsearch( 'discount', { disabled=>'' } ) ]; @@ -170,4 +173,7 @@ unshift @_type_options, $select; my $colspan = $opt{'colspan'} ? 'COLSPAN="'.$opt{'colspan'}.'"' : ''; +my $onchange = ( $opt{'onchange'} ? delete($opt{'onchange'}).';' : '' ). + $name.'_changed(this);'; + diff --git a/httemplate/misc/order_pkg.html b/httemplate/misc/order_pkg.html index e05205ba7..864e8328c 100644 --- a/httemplate/misc/order_pkg.html +++ b/httemplate/misc/order_pkg.html @@ -69,6 +69,8 @@ % if ( $curuser->access_right('Discount customer package') ) { <% include('/elements/tr-select-discount.html', 'element_etc' => 'DISABLED', + 'colspan' => 7, + 'cgi' => $cgi, ) %> % } diff --git a/httemplate/view/cust_main/order_pkg_link.html b/httemplate/view/cust_main/order_pkg_link.html index 9a12019ba..0c53f2a59 100644 --- a/httemplate/view/cust_main/order_pkg_link.html +++ b/httemplate/view/cust_main/order_pkg_link.html @@ -6,7 +6,7 @@ 'cust_main' => $cust_main, 'closetext' => 'Close', 'width' => 763, - 'height' => 408, + 'height' => 476, ) %> <%init> diff --git a/httemplate/view/cust_main/packages/status.html b/httemplate/view/cust_main/packages/status.html index c65eb70e9..7b1b53add 100644 --- a/httemplate/view/cust_main/packages/status.html +++ b/httemplate/view/cust_main/packages/status.html @@ -300,9 +300,11 @@ sub pkg_status_row_discount { my $discount = $cust_pkg_discount->discount; my $label = 'Discount: '. $discount->description; - $label .= ' ('. ( $discount->months - $cust_pkg_discount->months_used ). - ' months remaining)' - if $discount->months; + if ( $discount->months ) { + my $remaining = $discount->months - $cust_pkg_discount->months_used; + $remaining = sprintf('%.2f', $remaining) if $remaining =~ /\./; + $label .= " ($remaining months remaining)" + } $label .= ' ('. '