summaryrefslogtreecommitdiff
path: root/FS/FS
diff options
context:
space:
mode:
authorivan <ivan>2010-02-05 02:39:31 +0000
committerivan <ivan>2010-02-05 02:39:31 +0000
commit8a0204b4a129a3c26dcca18ba401b2de26d22d2b (patch)
treeacc110c2e41d74fefa6c038c6b0d414865fb29b2 /FS/FS
parent4cacb3fa439b56fc7c9a742b69ec3f6ffd660433 (diff)
discounts, RT#6679
Diffstat (limited to 'FS/FS')
-rw-r--r--FS/FS/cust_pkg.pm48
-rw-r--r--FS/FS/cust_pkg_discount.pm44
-rw-r--r--FS/FS/discount.pm10
-rw-r--r--FS/FS/part_pkg/flat.pm4
4 files changed, 94 insertions, 12 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<FS::cust_pkg_discount>, possibly
+inserting a new discount on the fly (see L<FS::discount>).
+
+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;