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 ) = @_;
34 my @sdate = localtime($$sdate);
35 my $rep_date = ($sdate[5]+1900). '-'. ($sdate[4]+1). '-01';
36 warn "searching for MonthlyUsage report for $rep_date\n" if $DEBUG;
38 SELECT id FROM reports WHERE rep_date = ?
39 AND reportname = 'MonthlyUsage' and iscomplete = 1
41 my $rep_id = $self->scalar_sql($rep_sql, $rep_date) or return 0;
42 warn "report id $rep_id found\n" if $DEBUG;
44 #abort if ! iscomplete instead?
46 my $conf = new FS::Conf;
47 my $money_char = $conf->config('money_char') || '$';
50 SELECT value FROM reportfields
51 WHERE rep_id = $rep_id
57 foreach my $svc_port (
58 grep $_->table('svc_port'), map $_->svc_x, $cust_pkg->cust_svc
61 my $serviceid = $svc_port->serviceid;
63 warn "searching for $serviceid usage\n" if $DEBUG;
64 my $in = $self->scalar_sql($sql, $self->_torrus_name, $serviceid.'_IN');
65 my $out = $self->scalar_sql($sql, $self->_torrus_name, $serviceid.'_OUT');
67 my $max = max($in,$out);
68 warn "$serviceid usage is $max\n" if $DEBUG;
70 my $inc = $self->option($self->_torrus_base);#aggregate instead of per-port?
74 my $amount = sprintf('%.2f', $self->option($self->_torrus_rate) * $max );
77 #add usage details to invoice
78 my $l = $self->_torrus_label;
79 my $d = "Last month's usage for $serviceid: $max$l";
80 $d .= " (". ($max+$inc). "$l - $inc$l included)" if $inc;
81 $d .= ": $money_char$amount";
87 return sprintf('%.2f', $total );