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
12 my($cust_pkg, $sdate, $details, $param ) = @_;
16 $charges += $self->calc_usage(@_);
17 $charges += $self->calc_prorate(@_, 1);
18 #$charges -= $self->calc_discount(@_);
24 #sub calc_cancel { #somehow trigger an early report?
26 #have to look at getting the discounts to apply to the usage charges
27 sub can_discount { 0; }
31 my($cust_pkg, $sdate, $details, $param ) = @_;
33 my @sdate = localtime($$sdate);
34 #sdate is next bill date, but we want the report from last month
35 my($m, $y) = ($sdate[4], $sdate[5]+1900);
36 if ( $m == 0 ) { $m=12; $y--; }
37 $m = "0$m" if length($m) == 1;
38 my $rep_date = "$y-$m-01";
39 warn "searching for MonthlyUsage report for $rep_date\n" if $DEBUG;
41 SELECT id FROM reports WHERE rep_date = ?
42 AND reportname = 'MonthlyUsage' and iscomplete = 1
44 my $rep_id = $self->scalar_sql($rep_sql, $rep_date) or return 0;
45 warn "report id $rep_id found\n" if $DEBUG;
47 #abort if ! iscomplete instead?
49 my $conf = new FS::Conf;
50 my $money_char = $conf->config('money_char') || '$';
53 SELECT value FROM reportfields
54 WHERE rep_id = $rep_id
60 foreach my $svc_port (
61 grep $_->table('svc_port'), map $_->svc_x, $cust_pkg->cust_svc
64 my $serviceid = $svc_port->serviceid;
66 warn "searching for $serviceid usage\n" if $DEBUG;
67 my $in = $self->scalar_sql($sql, $self->_torrus_name, $serviceid.'_IN');
68 my $out = $self->scalar_sql($sql, $self->_torrus_name, $serviceid.'_OUT');
70 my $max = max($in,$out);
71 warn "$serviceid usage is $max\n" if $DEBUG;
73 my $inc = $self->option($self->_torrus_base);#aggregate instead of per-port?
77 my $rate = $self->option($self->_torrus_rate);
78 my $amount = sprintf('%.2f', $rate * $max );
81 #add usage details to invoice
82 my $l = $self->_torrus_label;
83 my $d = "Last month's usage for $serviceid: ". sprintf('%.2f',$max). $l;
84 $d .= " (". ($max+$inc). "$l - $inc$l included)" if $inc;
85 $d .= " @ $money_char$rate/$l: $money_char$amount";
91 return sprintf('%.2f', $total );