X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fpart_pkg%2Ftorrus_Common.pm;h=b32deab04d43fab2edbfca6fbb940f44b7be578f;hb=57bb423fe457ba4e13726877f53bcdf944f828f8;hp=229a0c32b4bb29b3e9df96533a274aea9aae0d50;hpb=8fad52859c516d8657653007f0fa602f7c857651;p=freeside.git diff --git a/FS/FS/part_pkg/torrus_Common.pm b/FS/FS/part_pkg/torrus_Common.pm index 229a0c32b..b32deab04 100644 --- a/FS/FS/part_pkg/torrus_Common.pm +++ b/FS/FS/part_pkg/torrus_Common.pm @@ -3,7 +3,18 @@ package FS::part_pkg::torrus_Common; use base qw( FS::part_pkg::prorate ); use List::Util qw(max); -our %info = ( 'disabled' => 1 ); #recur_Common not a usable price plan directly +our %info = ( 'disabled' => 1 ); #torrus_Common not a usable price plan directly + +our $DEBUG = 1; + +sub recur_temporality { 'preceding'; } + +sub price_info { + my $self = shift; + my $str = $self->SUPER::price_info(@_); + $str .= " plus usage" if $str; + $str; +} sub calc_recur { my $self = shift; @@ -28,33 +39,66 @@ sub calc_usage { my $self = shift; my($cust_pkg, $sdate, $details, $param ) = @_; - my $serviceid = 'TESTING_1'; #XXX from svc_port (loop?) + my @sdate = localtime($$sdate); + #sdate is next bill date, but we want the report from last month + my($m, $y) = ($sdate[4], $sdate[5]+1900); + if ( $m == 0 ) { $m=12; $y--; } + $m = "0$m" if length($m) == 1; + my $rep_date = "$y-$m-01"; + warn "searching for MonthlyUsage report for $rep_date\n" if $DEBUG; + my $rep_sql = " + SELECT id FROM reports WHERE rep_date = ? + AND reportname = 'MonthlyUsage' and iscomplete = 1 + "; + my $rep_id = $self->scalar_sql($rep_sql, $rep_date) or return 0; + warn "report id $rep_id found\n" if $DEBUG; - my $rep_id = 2; #XXX find the one matching the timeframe - #SELECT id FROM WHERE reportname = 'MonthlyUsage' AND rep_date = '' + #abort if ! iscomplete instead? - #XXX abort if ! iscomplete? + my $conf = new FS::Conf; + my $money_char = $conf->config('money_char') || '$'; my $sql = " SELECT value FROM reportfields WHERE rep_id = $rep_id AND name = ? - AND servciceid = ? + AND serviceid = ? "; - - my $in = $self->scalar_sql($sql, $self->_torrus_name, $serviceid.'_IN'); - my $out = $self->scalar_sql($sql, $self->_torrus_name, $serviceid.'_OUT'); + + my $total = 0; + foreach my $svc_port ( + grep $_->table('svc_port'), map $_->svc_x, $cust_pkg->cust_svc + ) { - my $max = max($in,$out); + my $serviceid = $svc_port->serviceid; - $max -= $self->option($self->_torrus_base); - return 0 if $max < 0; + warn "searching for $serviceid usage\n" if $DEBUG; + my $in = $self->scalar_sql($sql, $self->_torrus_name, $serviceid.'_IN'); + my $out = $self->scalar_sql($sql, $self->_torrus_name, $serviceid.'_OUT'); - #XXX add usage details + my $max = max($in,$out); + warn "$serviceid usage is $max\n" if $DEBUG; - return sprintf('%.2f', $self->option($self->_torrus_rate) * $max ); + my $inc = $self->option($self->_torrus_base);#aggregate instead of per-port? + $max -= $inc; + next if $max < 0; -} + my $rate = $self->option($self->_torrus_rate); + my $amount = sprintf('%.2f', $rate * $max ); + $total += $amount; + #add usage details to invoice + my $l = $self->_torrus_label; + my $d = "Last month's usage for $serviceid: ". sprintf('%.2f',$max). $l; + $d .= " (". ($max+$inc). "$l - $inc$l included)" if $inc; + $d .= " @ $money_char$rate/$l: $money_char$amount"; + + push @$details, $d; + + } + + return sprintf('%.2f', $total ); + +} 1;