1 package FS::part_pkg::torrus_Common;
3 use base qw( FS::part_pkg::prorate );
4 use List::Util qw(max);
6 our %info = ( 'disabled' => 1 ); #torrus_Common not a usable price plan directly
10 sub recur_temporality { 'preceding'; }
14 my $str = $self->SUPER::price_info(@_);
15 $str .= " plus usage" if $str;
21 my($cust_pkg, $sdate, $details, $param ) = @_;
25 $charges += $self->calc_usage(@_);
26 $charges += $self->calc_prorate(@_, 1);
27 #$charges -= $self->calc_discount(@_);
33 #sub calc_cancel { #somehow trigger an early report?
35 #have to look at getting the discounts to apply to the usage charges
36 sub can_discount { 0; }
40 my($cust_pkg, $sdate, $details, $param ) = @_;
42 my @sdate = localtime($$sdate);
43 #sdate is next bill date, but we want the report from last month
44 my($m, $y) = ($sdate[4], $sdate[5]+1900);
45 if ( $m == 0 ) { $m=12; $y--; }
46 $m = "0$m" if length($m) == 1;
47 my $rep_date = "$y-$m-01";
48 warn "searching for MonthlyUsage report for $rep_date\n" if $DEBUG;
50 SELECT id FROM reports WHERE rep_date = ?
51 AND reportname = 'MonthlyUsage' and iscomplete = 1
53 my $rep_id = $self->scalar_sql($rep_sql, $rep_date) or return 0;
54 warn "report id $rep_id found\n" if $DEBUG;
56 #abort if ! iscomplete instead?
58 my $conf = new FS::Conf;
59 my $money_char = $conf->config('money_char') || '$';
62 SELECT value FROM reportfields
63 WHERE rep_id = $rep_id
69 foreach my $svc_port (
70 grep $_->table('svc_port'), map $_->svc_x, $cust_pkg->cust_svc
73 my $serviceid = $svc_port->serviceid;
75 warn "searching for $serviceid usage\n" if $DEBUG;
76 my $in = $self->scalar_sql($sql, $self->_torrus_name, $serviceid.'_IN');
77 my $out = $self->scalar_sql($sql, $self->_torrus_name, $serviceid.'_OUT');
79 my $max = max($in,$out);
80 warn "$serviceid usage is $max\n" if $DEBUG;
82 my $inc = $self->option($self->_torrus_base);#aggregate instead of per-port?
86 my $rate = $self->option($self->_torrus_rate);
87 my $amount = sprintf('%.2f', $rate * $max );
90 #add usage details to invoice
91 my $l = $self->_torrus_label;
92 my $d = "Last month's usage for $serviceid: ". sprintf('%.2f',$max). $l;
93 $d .= " (". ($max+$inc). "$l - $inc$l included)" if $inc;
94 $d .= " @ $money_char$rate/$l: $money_char$amount";
100 return sprintf('%.2f', $total );