import torrus 1.0.9
[freeside.git] / FS / FS / part_pkg / sql_external.pm
1 package FS::part_pkg::sql_external;
2
3 use strict;
4 use base qw( FS::part_pkg::recur_Common );
5 use vars qw( %info );
6 use DBI;
7 #use FS::Record qw(qsearch qsearchs);
8
9 %info = (
10   'name' => 'Base charge plus additional fees for external services from a configurable SQL query',
11   'shortname' => 'External SQL query',
12   'inherit_fields' => [ 'global_Mixin' ],
13   'fields' => {
14     'cutoff_day'    => { 'name' => 'Billing Day (1 - 28) for prorating or '.
15                                    'subscription',
16                          'default' => '1',
17                        },
18     'add_full_period'=> { 'name' => 'When prorating first month, also bill '.
19                                     'for one full period after that',
20                           'type' => 'checkbox',
21                        },
22
23     'recur_method'  => { 'name' => 'Recurring fee method',
24                          #'type' => 'radio',
25                          #'options' => \%recur_method,
26                          'type' => 'select',
27                          'select_options' => \%FS::part_pkg::recur_Common::recur_method,
28                        },
29     'datasrc' => { 'name' => 'DBI data source',
30                    'default' => '',
31                  },
32     'db_username' => { 'name' => 'Database username',
33                        'default' => '',
34                      },
35     'db_password' => { 'name' => 'Database password',
36                        'default' => '',
37                      },
38     'query' => { 'name' => 'SQL query',
39                  'default' => '',
40                },
41   },
42   'fieldorder' => [qw( recur_method cutoff_day
43                       add_full_period datasrc db_username db_password query 
44                   )],
45   'weight' => '58',
46 );
47
48 sub calc_recur {
49   my $self = shift;
50   my($cust_pkg) = @_; #, $sdate, $details, $param ) = @_;
51
52   my $price = $self->calc_recur_Common(@_);
53
54   my $dbh = DBI->connect( map { $self->option($_) }
55                               qw( datasrc db_username db_password )
56                         )
57     or die $DBI::errstr;
58
59   my $sth = $dbh->prepare( $self->option('query') )
60     or die $dbh->errstr;
61
62   foreach my $cust_svc (
63     grep { $_->part_svc->svcdb eq "svc_external" } $cust_pkg->cust_svc
64   ) {
65     my $id = $cust_svc->svc_x->id;
66     $sth->execute($id) or die $sth->errstr;
67     $price += $sth->fetchrow_arrayref->[0];
68   }
69
70   $price;
71 }
72
73 sub can_discount { 0; }
74
75 sub is_free { 0; }
76
77 1;