default to a session cookie instead of setting an explicit timeout, weird timezone...
[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 recur_temporality { 'preceding'; }
11
12 sub price_info {
13     my $self = shift;
14     my $str = $self->SUPER::price_info(@_);
15     $str .= " plus usage" if $str;
16     $str;
17 }
18
19 sub calc_recur {
20   my $self = shift;
21   my($cust_pkg, $sdate, $details, $param ) = @_;
22
23   my $charges = 0;
24
25   $charges += $self->calc_usage(@_);
26   $charges += $self->calc_prorate(@_, 1);
27   #$charges -= $self->calc_discount(@_);
28
29   $charges;
30
31 }
32
33 #sub calc_cancel {  #somehow trigger an early report?
34
35 #have to look at getting the discounts to apply to the usage charges
36 sub can_discount { 0; }
37
38 sub calc_usage {
39   my $self = shift;
40   my($cust_pkg, $sdate, $details, $param ) = @_;
41
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;
49   my $rep_sql = "
50     SELECT id FROM reports WHERE rep_date = ?
51                              AND reportname = 'MonthlyUsage' and iscomplete = 1
52   ";
53   my $rep_id = $self->scalar_sql($rep_sql, $rep_date) or return 0;
54   warn "report id $rep_id found\n" if $DEBUG;
55
56   #abort if ! iscomplete instead?
57
58   my $conf = new FS::Conf;
59   my $money_char = $conf->config('money_char') || '$';
60
61   my $sql = "
62     SELECT value FROM reportfields
63       WHERE rep_id = $rep_id
64         AND name = ?
65         AND serviceid = ?
66   ";
67  
68   my $total = 0;
69   foreach my $svc_port (
70     grep $_->table('svc_port'), map $_->svc_x, $cust_pkg->cust_svc
71   ) {
72
73     my $serviceid = $svc_port->serviceid;
74
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');
78
79     my $max = max($in,$out);
80     warn "$serviceid usage is $max\n" if $DEBUG;
81
82     my $inc = $self->option($self->_torrus_base);#aggregate instead of per-port?
83     $max -= $inc;
84     next if $max < 0;
85
86     my $rate = $self->option($self->_torrus_rate);
87     my $amount = sprintf('%.2f', $rate * $max );
88     $total += $amount;
89
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";
95
96     push @$details, $d;
97
98   }
99
100   return sprintf('%.2f', $total );
101
102 }
103
104 1;