torrus, RT#10574
[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 ); #recur_Common not a usable price plan directly
7
8 sub calc_recur {
9   my $self = shift;
10   my($cust_pkg, $sdate, $details, $param ) = @_;
11
12   my $charges = 0;
13
14   $charges += $self->calc_usage(@_);
15   $charges += $self->calc_prorate(@_, 1);
16   #$charges -= $self->calc_discount(@_);
17
18   $charges;
19
20 }
21
22 #sub calc_cancel {  #somehow trigger an early report?
23
24 #have to look at getting the discounts to apply to the usage charges
25 sub can_discount { 0; }
26
27 sub calc_usage {
28   my $self = shift;
29   my($cust_pkg, $sdate, $details, $param ) = @_;
30
31   my $rep_id = 2; #XXX find the one matching the timeframe
32   #SELECT id FROM WHERE reportname = 'MonthlyUsage' AND rep_date = ''
33
34   #XXX abort if ! iscomplete?
35
36   my $serviceid = 'TESTING_1'; #XXX from svc_port (loop?)
37
38   my $sql = "
39     SELECT value FROM reportfields
40       WHERE rep_id = $rep_id
41         AND name = ?
42         AND servciceid = ?
43   ";
44   
45   my $in  = $self->scalar_sql($sql, $self->_torrus_name, $serviceid.'_IN');
46   my $out = $self->scalar_sql($sql, $self->_torrus_name, $serviceid.'_OUT');
47
48   my $max = max($in,$out);
49
50   $max -= $self->option($self->_torrus_base);
51   return 0 if $max < 0;
52
53   #XXX add usage details
54
55   return sprintf('%.2f', $self->option($self->_torrus_rate) * $max );
56
57 }
58
59
60 1;