show credit balance on invoices, #11564
[freeside.git] / FS / FS / part_pkg / torrus_Common.pm
1 package FS::part_pkg::torrus_Common;
2
3 use base qw( FS::part_pkg::prorate );
4 use List::Util qw(max);
5
6 our %info = ( 'disabled' => 1 ); #torrus_Common not a usable price plan directly
7
8 our $DEBUG = 1;
9
10 sub price_info {
11     my $self = shift;
12     my $str = $self->SUPER::price_info;
13     $str .= " plus usage" if $str;
14     $str;
15 }
16
17 sub calc_recur {
18   my $self = shift;
19   my($cust_pkg, $sdate, $details, $param ) = @_;
20
21   my $charges = 0;
22
23   $charges += $self->calc_usage(@_);
24   $charges += $self->calc_prorate(@_, 1);
25   #$charges -= $self->calc_discount(@_);
26
27   $charges;
28
29 }
30
31 #sub calc_cancel {  #somehow trigger an early report?
32
33 #have to look at getting the discounts to apply to the usage charges
34 sub can_discount { 0; }
35
36 sub calc_usage {
37   my $self = shift;
38   my($cust_pkg, $sdate, $details, $param ) = @_;
39
40   my @sdate = localtime($$sdate);
41   #sdate is next bill date, but we want the report from last month
42   my($m, $y) = ($sdate[4], $sdate[5]+1900);
43   if ( $m == 0 ) { $m=12; $y--; }
44   $m = "0$m" if length($m) == 1;
45   my $rep_date = "$y-$m-01";
46   warn "searching for MonthlyUsage report for $rep_date\n" if $DEBUG;
47   my $rep_sql = "
48     SELECT id FROM reports WHERE rep_date = ?
49                              AND reportname = 'MonthlyUsage' and iscomplete = 1
50   ";
51   my $rep_id = $self->scalar_sql($rep_sql, $rep_date) or return 0;
52   warn "report id $rep_id found\n" if $DEBUG;
53
54   #abort if ! iscomplete instead?
55
56   my $conf = new FS::Conf;
57   my $money_char = $conf->config('money_char') || '$';
58
59   my $sql = "
60     SELECT value FROM reportfields
61       WHERE rep_id = $rep_id
62         AND name = ?
63         AND serviceid = ?
64   ";
65  
66   my $total = 0;
67   foreach my $svc_port (
68     grep $_->table('svc_port'), map $_->svc_x, $cust_pkg->cust_svc
69   ) {
70
71     my $serviceid = $svc_port->serviceid;
72
73     warn "searching for $serviceid usage\n" if $DEBUG;
74     my $in  = $self->scalar_sql($sql, $self->_torrus_name, $serviceid.'_IN');
75     my $out = $self->scalar_sql($sql, $self->_torrus_name, $serviceid.'_OUT');
76
77     my $max = max($in,$out);
78     warn "$serviceid usage is $max\n" if $DEBUG;
79
80     my $inc = $self->option($self->_torrus_base);#aggregate instead of per-port?
81     $max -= $inc;
82     next if $max < 0;
83
84     my $rate = $self->option($self->_torrus_rate);
85     my $amount = sprintf('%.2f', $rate * $max );
86     $total += $amount;
87
88     #add usage details to invoice
89     my $l = $self->_torrus_label;
90     my $d = "Last month's usage for $serviceid: ". sprintf('%.2f',$max). $l;
91     $d .= " (". ($max+$inc). "$l - $inc$l included)" if $inc;
92     $d .= " @ $money_char$rate/$l: $money_char$amount";
93
94     push @$details, $d;
95
96   }
97
98   return sprintf('%.2f', $total );
99
100 }
101
102 1;